OSDN Git Service

Add navigator.networkType to allow apps to detect the connection type.
authorAndrei Popescu <andreip@google.com>
Thu, 14 Jan 2010 19:19:31 +0000 (19:19 +0000)
committerAndrei Popescu <andreip@google.com>
Fri, 15 Jan 2010 20:05:20 +0000 (20:05 +0000)
The online event is fired when the networkType changes.

Bug: 2368650

15 files changed:
WebCore/Android.derived.jscbindings.mk
WebCore/Android.derived.v8bindings.mk
WebCore/Android.mk
WebCore/bindings/v8/DOMObjectsInclude.h
WebCore/bindings/v8/V8Index.cpp
WebCore/bindings/v8/V8Index.h
WebCore/page/Connection.cpp [new file with mode: 0644]
WebCore/page/Connection.h [new file with mode: 0644]
WebCore/page/Connection.idl [new file with mode: 0644]
WebCore/page/Navigator.cpp
WebCore/page/Navigator.h
WebCore/page/Navigator.idl
WebCore/platform/network/NetworkStateNotifier.h
WebCore/platform/network/android/NetworkStateNotifierAndroid.cpp
WebKit/android/jni/JavaBridge.cpp

index d7ca0a4..cc7ba28 100644 (file)
@@ -289,6 +289,7 @@ $(patsubst %.h,%.cpp,$(GEN)): $(intermediates)/loader/appcache/%.cpp : $(interme
 # page
 GEN := \
     $(intermediates)/page/JSBarInfo.h \
+    $(intermediates)/page/JSConnection.h \
     $(intermediates)/page/JSConsole.h \
     $(intermediates)/page/JSCoordinates.h \
     $(intermediates)/page/JSDOMSelection.h \
index a9f403a..e3bd4ca 100644 (file)
@@ -281,6 +281,7 @@ $(patsubst %.h,%.cpp,$(GEN)): $(intermediates)/bindings/%.cpp : $(intermediates)
 # page
 GEN := \
     $(intermediates)/bindings/V8BarInfo.h \
+    $(intermediates)/bindings/V8Connection.h \
     $(intermediates)/bindings/V8Console.h \
     $(intermediates)/bindings/V8Coordinates.h \
     $(intermediates)/bindings/V8DOMSelection.h \
index d5bc740..19e3fd2 100644 (file)
@@ -326,6 +326,7 @@ LOCAL_SRC_FILES := $(LOCAL_SRC_FILES) \
        \
        page/BarInfo.cpp \
        page/Chrome.cpp \
+       page/Connection.cpp \
        page/Console.cpp \
        page/ContextMenuController.cpp \
        page/DOMSelection.cpp \
index 6ed14be..bded7f2 100644 (file)
 #endif // SVG
 
 #if PLATFORM(ANDROID)
+#include "Connection.h"
 // TODO: Upstream TOUCH_EVENTS guard.
 #if ENABLE(TOUCH_EVENTS)
 #include "Touch.h"
index 9686dc6..ea13d2f 100644 (file)
 #include "V8PositionError.h"
 
 #if PLATFORM(ANDROID)
+#include "V8Connection.h"
 // TODO: Upstream these guards to webkit.org
 #if ENABLE(TOUCH_EVENTS)
 #include "V8Touch.h"
index d786aab..a3aebdf 100644 (file)
@@ -512,6 +512,8 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)();
 #else
 #define DOM_OBJECT_TOUCH_EVENT_TYPES(V)
 #endif
+#define DOM_OBJECT_CONNECTION_TYPES(V)                                  \
+    V(CONNECTION, Connection)
 #endif
 
 #if PLATFORM(ANDROID)
@@ -535,6 +537,7 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)();
     DOM_OBJECT_INSPECTOR_TYPES(V)                                       \
     DOM_OBJECT_GEOLOCATION_TYPES(V)                                     \
     DOM_OBJECT_TOUCH_EVENT_TYPES(V)                                     \
+    DOM_OBJECT_CONNECTION_TYPES(V)                                      \
     DOM_OBJECT_VOIDCALLBACK_TYPES(V)
 #endif
 
diff --git a/WebCore/page/Connection.cpp b/WebCore/page/Connection.cpp
new file mode 100644 (file)
index 0000000..ffbb838
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "Connection.h"
+
+#include "NetworkStateNotifier.h"
+
+namespace WebCore {
+
+Connection::ConnectionType Connection::type() const
+{
+    return networkStateNotifier().type();
+}
+
+};
\ No newline at end of file
diff --git a/WebCore/page/Connection.h b/WebCore/page/Connection.h
new file mode 100644 (file)
index 0000000..837a36f
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef Connection_h
+#define Connection_h
+
+#include <wtf/PassRefPtr.h>
+#include <wtf/RefCounted.h>
+
+namespace WebCore {
+
+class Connection : public RefCounted<Connection> {
+public:
+    enum ConnectionType {
+        Unknown = 0,
+        Ethernet = 1,
+        WiFi = 2,
+        Cell_2G = 3,
+        Cell_3G = 4,
+    };
+
+    static PassRefPtr<Connection> create() { return adoptRef(new Connection()); }
+
+    ConnectionType type() const;
+
+private:
+    Connection() { }
+};
+
+} // namespace WebCore
+
+#endif // Connection_h
diff --git a/WebCore/page/Connection.idl b/WebCore/page/Connection.idl
new file mode 100644 (file)
index 0000000..b4cfbd1
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+ module core {
+
+    interface Connection {
+        readonly attribute unsigned short type;
+
+        const unsigned short UNKNOWN = 0;
+        const unsigned short ETHERNET = 1;
+        const unsigned short WIFI = 2;
+        const unsigned short CELL_2G = 3;
+        const unsigned short CELL_3G = 4;
+    };
+
+}
index a4193fc..8563a0e 100644 (file)
@@ -24,6 +24,9 @@
 #include "Navigator.h"
 
 #include "CookieJar.h"
+#if PLATFORM(ANDROID)
+#include "Connection.h"
+#endif
 #include "ExceptionCode.h"
 #include "Frame.h"
 #include "FrameLoader.h"
@@ -155,6 +158,15 @@ Geolocation* Navigator::geolocation() const
     return m_geolocation.get();
 }
 
+#if PLATFORM(ANDROID)
+Connection* Navigator::connection() const
+{
+    if (!m_connection)
+        m_connection = Connection::create();
+    return m_connection.get();
+}
+#endif
+
 #if ENABLE(DOM_STORAGE)
 void Navigator::getStorageUpdates()
 {
index 107082b..9967fba 100644 (file)
@@ -27,6 +27,9 @@
 
 namespace WebCore {
 
+#if PLATFORM(ANDROID)
+    class Connection;
+#endif
     class Frame;
     class Geolocation;
     class MimeTypeArray;
@@ -57,6 +60,10 @@ namespace WebCore {
         // This is used for GC marking.
         Geolocation* optionalGeolocation() const { return m_geolocation.get(); }
 
+#if PLATFORM(ANDROID)
+        Connection* connection() const;
+#endif
+
 #if ENABLE(DOM_STORAGE)
         // Relinquishes the storage lock, if one exists.
         void getStorageUpdates();
@@ -71,6 +78,9 @@ namespace WebCore {
         mutable RefPtr<PluginArray> m_plugins;
         mutable RefPtr<MimeTypeArray> m_mimeTypes;
         mutable RefPtr<Geolocation> m_geolocation;
+#if PLATFORM(ANDROID)
+        mutable RefPtr<Connection> m_connection;
+#endif
     };
 
 }
index 99b22af..f3079de 100644 (file)
@@ -39,6 +39,9 @@ module window {
         
         readonly attribute boolean onLine;
 
+        // ANDROID-only for now, upstreaming in progress.
+        readonly attribute Connection connection;
+
 #if defined(ENABLE_GEOLOCATION) && ENABLE_GEOLOCATION
         readonly attribute Geolocation geolocation;
 #endif
index 570ced0..d0463d4 100644 (file)
@@ -27,6 +27,9 @@
 #define NetworkStateNotifier_h
 
 #include <wtf/Noncopyable.h>
+#if PLATFORM(ANDROID)
+#include "Connection.h"
+#endif
 
 #if PLATFORM(MAC)
 
@@ -54,9 +57,15 @@ public:
     void setNetworkStateChangedFunction(void (*)());
     
     bool onLine() const { return m_isOnLine; }
+#if PLATFORM(ANDROID)
+    Connection::ConnectionType type() const { return m_type; }
+#endif
     
 private:    
     bool m_isOnLine;
+#if PLATFORM(ANDROID)
+    Connection::ConnectionType m_type;
+#endif
     void (*m_networkStateChangedFunction)();
 
     void updateState();
@@ -84,6 +93,7 @@ private:
 #elif PLATFORM(ANDROID)
 public:
     void networkStateChange(bool online);
+    void networkTypeChange(Connection::ConnectionType type);
 #endif
 };
 
@@ -91,6 +101,9 @@ public:
 
 inline NetworkStateNotifier::NetworkStateNotifier()
     : m_isOnLine(true)
+#if PLATFORM(ANDROID)
+    , m_type(Connection::Unknown)
+#endif
     , m_networkStateChangedFunction(0)
 {    
 }
index 3aa5578..134e206 100644 (file)
@@ -39,4 +39,15 @@ void NetworkStateNotifier::networkStateChange(bool online)
         m_networkStateChangedFunction();
 }
 
+void NetworkStateNotifier::networkTypeChange(Connection::ConnectionType type)
+{
+    if (m_type == type)
+        return;
+
+    m_type = type;
+
+    if (m_networkStateChangedFunction)
+        m_networkStateChangedFunction();
+}
+
 }
index 049a7da..9e83d6a 100644 (file)
 
 #include <config.h>
 #include <wtf/Platform.h>
+#include <wtf/StdLibExtras.h>
 
+#include "AtomicString.h"
 #include "Cache.h"
+#include "Connection.h"
 #include "CookieClient.h"
 #include "JavaSharedClient.h"
 #include "KeyGeneratorClient.h"
@@ -96,6 +99,7 @@ public:
     static void SharedTimerFired(JNIEnv* env, jobject);
     static void SetCacheSize(JNIEnv* env, jobject obj, jint bytes);
     static void SetNetworkOnLine(JNIEnv* env, jobject obj, jboolean online);
+    static void SetNetworkType(JNIEnv* env, jobject obj, jstring type, jstring subtype);
     static void SetDeferringTimers(JNIEnv* env, jobject obj, jboolean defer);
     static void ServiceFuncPtrQueue(JNIEnv*);
     static void UpdatePluginDirectories(JNIEnv* env, jobject obj, jobjectArray array, jboolean reload);
@@ -344,6 +348,29 @@ void JavaBridge::SetNetworkOnLine(JNIEnv* env, jobject obj, jboolean online)
        WebCore::networkStateNotifier().networkStateChange(online);
 }
 
+void JavaBridge::SetNetworkType(JNIEnv* env, jobject obj, jstring javatype, jstring javasubtype)
+{
+    DEFINE_STATIC_LOCAL(AtomicString, wifi, ("wifi"));
+    DEFINE_STATIC_LOCAL(AtomicString, mobile, ("mobile"));
+    DEFINE_STATIC_LOCAL(AtomicString, mobileSupl, ("mobile_supl"));
+    DEFINE_STATIC_LOCAL(AtomicString, gprs, ("gprs"));
+    DEFINE_STATIC_LOCAL(AtomicString, edge, ("edge"));
+    DEFINE_STATIC_LOCAL(AtomicString, umts, ("umts"));
+
+    String type = to_string(env, javatype);
+    String subtype = to_string(env, javasubtype);
+    Connection::ConnectionType connectionType = Connection::Unknown;
+    if (type == wifi)
+        connectionType = Connection::WiFi;
+    else if (type == mobile || type == mobileSupl) {
+        if (subtype == edge || subtype == gprs)
+            connectionType = Connection::Cell_2G;
+        else if (subtype == umts)
+            connectionType = Connection::Cell_3G;
+    }
+    WebCore::networkStateNotifier().networkTypeChange(connectionType);
+}
+
 void JavaBridge::ServiceFuncPtrQueue(JNIEnv*)
 {
     JavaSharedClient::ServiceFunctionPtrQueue();
@@ -383,6 +410,8 @@ static JNINativeMethod gWebCoreJavaBridgeMethods[] = {
         (void*) JavaBridge::SetCacheSize },
     { "setNetworkOnLine", "(Z)V",
         (void*) JavaBridge::SetNetworkOnLine },
+    { "setNetworkType", "(Ljava/lang/String;Ljava/lang/String;)V",
+        (void*) JavaBridge::SetNetworkType },
     { "nativeServiceFuncPtrQueue", "()V",
         (void*) JavaBridge::ServiceFuncPtrQueue },
     { "nativeUpdatePluginDirectories", "([Ljava/lang/String;Z)V",