From b21283649e18cd5b16b8d6e72e7dc222f2b835d2 Mon Sep 17 00:00:00 2001 From: Kannan Vijayan Date: Thu, 2 Jun 2016 16:42:36 -0400 Subject: [PATCH] Bug 1272101 - Fix nsINetworkInfoService implementation on Android to use Linux C APIs instead of Android Java APIs. r=snorp --- .../org/mozilla/gecko/GeckoApplication.java | 1 - .../org/mozilla/gecko/NetworkInfoService.java | 144 ------------------ mobile/android/base/moz.build | 1 - mobile/android/installer/package-manifest.in | 2 - netwerk/base/NetworkInfoServiceAndroid.jsm | 67 -------- netwerk/base/moz.build | 9 +- netwerk/base/nsNetworkInfoService.js | 56 ------- netwerk/base/nsNetworkInfoService.manifest | 4 - netwerk/build/nsNetModule.cpp | 3 +- 9 files changed, 4 insertions(+), 283 deletions(-) delete mode 100644 mobile/android/base/java/org/mozilla/gecko/NetworkInfoService.java delete mode 100644 netwerk/base/NetworkInfoServiceAndroid.jsm delete mode 100644 netwerk/base/nsNetworkInfoService.js delete mode 100644 netwerk/base/nsNetworkInfoService.manifest diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java index 3ada0c610424..2297a8bdf095 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java @@ -155,7 +155,6 @@ public class GeckoApplication extends Application NotificationHelper.getInstance(context).init(); MulticastDNSManager.getInstance(context).init(); - NetworkInfoService.getInstance(context).init(); // Make sure that all browser-ish applications default to the real LocalBrowserDB. // GeckoView consumers use their own Application class, so this doesn't affect them. diff --git a/mobile/android/base/java/org/mozilla/gecko/NetworkInfoService.java b/mobile/android/base/java/org/mozilla/gecko/NetworkInfoService.java deleted file mode 100644 index fca3c7e2f6ed..000000000000 --- a/mobile/android/base/java/org/mozilla/gecko/NetworkInfoService.java +++ /dev/null @@ -1,144 +0,0 @@ -/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.gecko; - -import org.mozilla.gecko.EventDispatcher; -import org.mozilla.gecko.util.EventCallback; -import org.mozilla.gecko.util.NativeEventListener; -import org.mozilla.gecko.util.NativeJSObject; -import org.mozilla.gecko.util.ThreadUtils; - -import org.json.JSONArray; - -import android.content.Context; -import android.os.Build; -import android.support.annotation.UiThread; -import android.util.Log; - -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; - -public class NetworkInfoService implements NativeEventListener { - static final String LOGTAG = "NetworkInfoService"; - static NetworkInfoService instance = null; - - static final String MSG_LIST_NETWORK_ADDRESSES = "NetworkInfoService:ListNetworkAddresses"; - static final String MSG_GET_HOSTNAME = "NetworkInfoService:GetHostname"; - - public static NetworkInfoService getInstance(final Context context) { - if (instance == null) { - instance = new NetworkInfoService(context); - } - return instance; - } - - NetworkInfoService(final Context context) { - } - - @UiThread - public void init() { - ThreadUtils.assertOnUiThread(); - EventDispatcher.getInstance().registerGeckoThreadListener(this, - MSG_LIST_NETWORK_ADDRESSES, - MSG_GET_HOSTNAME); - } - - @UiThread - public void tearDown() { - ThreadUtils.assertOnUiThread(); - EventDispatcher.getInstance().unregisterGeckoThreadListener(this, - MSG_LIST_NETWORK_ADDRESSES, - MSG_GET_HOSTNAME); - } - - @Override - public void handleMessage(final String event, final NativeJSObject message, - final EventCallback callback) - { - Log.v(LOGTAG, "handleMessage: " + event); - - switch (event) { - case MSG_LIST_NETWORK_ADDRESSES: { - handleListNetworkAddresses(callback); - break; - } - case MSG_GET_HOSTNAME: { - handleGetHostname(callback); - break; - } - } - } - - void handleListNetworkAddresses(final EventCallback callback) { - Set addresses = new HashSet(); - try { - Enumeration ifaceEnum = NetworkInterface.getNetworkInterfaces(); - if (ifaceEnum != null) { - while (ifaceEnum.hasMoreElements()) { - NetworkInterface iface = ifaceEnum.nextElement(); - Enumeration addrList = iface.getInetAddresses(); - while (addrList.hasMoreElements()) { - InetAddress addr = addrList.nextElement(); - addresses.add(addr.getHostAddress()); - } - } - } - } catch (SocketException exc) { - callback.sendError(-1); - return; - } - - JSONArray array = new JSONArray(); - for (String addr : addresses) { - array.put(addr); - } - callback.sendSuccess(array); - } - - void handleGetHostname(final EventCallback callback) { - // callback.sendError(-1); - callback.sendSuccess(getDeviceName()); - } - - private static String getDeviceName() { - String manufacturer = Build.MANUFACTURER; - String model = Build.MODEL; - if (model.startsWith(manufacturer)) { - return capitalize(model); - } - return capitalize(manufacturer) + " " + model; - } - - private static String capitalize(String str) { - if (str.length() <= 1) - return str; - - // Capitalize the manufacturer's first letter. - char ch0 = str.charAt(0); - if (Character.isLetter(ch0) && Character.isLowerCase(ch0)) { - boolean upcase = true; - // But don't capitalize the first letter if it's an 'i' followed - // by a non-lowercase letter. Sheesh. - if (ch0 == 'i') { - if (str.length() >= 2) { - char ch1 = str.charAt(1); - if (!Character.isLetter(ch1) || !Character.isLowerCase(ch1)) { - upcase = false; - } - } - } - if (upcase) { - return Character.toUpperCase(ch0) + str.substring(1); - } - } - return str; - } - -} diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index fce5b58f924a..2f5b1464f388 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -469,7 +469,6 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [ 'menu/MenuPanel.java', 'menu/MenuPopup.java', 'MotionEventInterceptor.java', - 'NetworkInfoService.java', 'NotificationClient.java', 'NotificationHandler.java', 'NotificationHelper.java', diff --git a/mobile/android/installer/package-manifest.in b/mobile/android/installer/package-manifest.in index 3131cf1304d0..a31e1c97020b 100644 --- a/mobile/android/installer/package-manifest.in +++ b/mobile/android/installer/package-manifest.in @@ -262,8 +262,6 @@ @BINPATH@/components/zipwriter.xpt ; JavaScript components -@BINPATH@/components/nsNetworkInfoService.manifest -@BINPATH@/components/nsNetworkInfoService.js @BINPATH@/components/ChromeNotifications.js @BINPATH@/components/ChromeNotifications.manifest @BINPATH@/components/ConsoleAPI.manifest diff --git a/netwerk/base/NetworkInfoServiceAndroid.jsm b/netwerk/base/NetworkInfoServiceAndroid.jsm deleted file mode 100644 index af363082ff5f..000000000000 --- a/netwerk/base/NetworkInfoServiceAndroid.jsm +++ /dev/null @@ -1,67 +0,0 @@ -// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -this.EXPORTED_SYMBOLS = ["NetworkInfoServiceAndroid"]; - -const { classes: Cc, interfaces: Ci, utils: Cu } = Components; - -Cu.import("resource://gre/modules/Messaging.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); -Cu.import("resource://gre/modules/AndroidLog.jsm"); - -var log = AndroidLog.d.bind(null, "NetworkInfoServiceAndroid"); - -const FAILURE_INTERNAL_ERROR = -65537; -const MSG_TAG = 'NetworkInfoService'; - -// Helper function for sending commands to Java. -function send(type, data, callback) { - if (type[0] == ":") { - type = MSG_TAG + type; - } - let msg = { type }; - - for (let i in data) { - try { - msg[i] = data[i]; - } catch (e) { - } - } - - Messaging.sendRequestForResult(msg) - .then(result => callback(result, null), - err => callback(null, typeof err === "number" ? err : FAILURE_INTERNAL_ERROR)); -} - -class NetworkInfoServiceAndroid { - constructor() { - } - - listNetworkAddresses(aListener) { - send(":ListNetworkAddresses", {}, (result, err) => { - if (err) { - log("ListNetworkAddresses Failed: (" + err + ")"); - aListener.onListNetworkAddressesFailed(); - } else { - log("ListNetworkAddresses Succeeded: (" + JSON.stringify(result) + ")"); - aListener.onListedNetworkAddresses(result); - } - }); - } - - getHostname(aListener) { - send(":GetHostname", {}, (result, err) => { - if (err) { - log("GetHostname Failed: (" + err + ")"); - aListener.onGetHostnameFailed(); - } else { - log("GetHostname Succeeded: (" + JSON.stringify(result) + ")"); - aListener.onGotHostname(result); - } - }); - } -} diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build index e2fcea3f292f..9db7d568312c 100644 --- a/netwerk/base/moz.build +++ b/netwerk/base/moz.build @@ -294,12 +294,9 @@ elif CONFIG['OS_TARGET'] == 'Linux': 'nsNetworkInfoService.cpp', ] elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android': - EXTRA_COMPONENTS += [ - 'nsNetworkInfoService.js', - 'nsNetworkInfoService.manifest' - ] - EXTRA_JS_MODULES += [ - 'NetworkInfoServiceAndroid.jsm', + SOURCES += [ + 'NetworkInfoServiceLinux.cpp', + 'nsNetworkInfoService.cpp', ] if CONFIG['MOZ_ENABLE_QTNETWORK']: diff --git a/netwerk/base/nsNetworkInfoService.js b/netwerk/base/nsNetworkInfoService.js deleted file mode 100644 index e39edf377f2b..000000000000 --- a/netwerk/base/nsNetworkInfoService.js +++ /dev/null @@ -1,56 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -"use strict"; - -const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; - -Cu.import('resource://gre/modules/Services.jsm'); -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -Cu.import("resource://gre/modules/NetworkInfoServiceAndroid.jsm"); - -const NETWORKINFOSERVICE_CID = Components.ID("{0d8245af-d5d3-4100-a6b5-1cc026aa19f2}"); -const NETWORKINFOSERVICE_CONTRACT_ID = "@mozilla.org/network-info-service;1"; - -function log(aMsg) { - dump("-*- nsNetworkInfoService.js : " + aMsg + "\n"); -} - -function setQueryInterface(cls, ...aQIList) { - cls.prototype.QueryInterface = XPCOMUtils.generateQI(aQIList); -} - -class nsNetworkInfoService { - constructor() { - this.impl = new NetworkInfoServiceAndroid(); - log("nsNetworkInfoService"); - } - - listNetworkAddresses(aListener) { - this.impl.listNetworkAddresses({ - onListNetworkAddressesFailed(err) { - aListener.onListNetworkAddressesFailed(); - }, - onListedNetworkAddresses(addresses) { - aListener.onListedNetworkAddresses(addresses, addresses.length); - } - }); - } - - getHostname(aListener) { - this.impl.getHostname({ - onGetHostnameFailed(err) { - aListener.onGetHostnameFailed(); - }, - onGotHostname(hostname) { - aListener.onGotHostname(hostname); - } - }); - } -} -setQueryInterface(nsNetworkInfoService, Ci.nsISupportsWeakReference, - Ci.nsINetworkInfoService); -nsNetworkInfoService.prototype.classID = NETWORKINFOSERVICE_CID; - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsNetworkInfoService]); diff --git a/netwerk/base/nsNetworkInfoService.manifest b/netwerk/base/nsNetworkInfoService.manifest deleted file mode 100644 index 5c262ef2be0c..000000000000 --- a/netwerk/base/nsNetworkInfoService.manifest +++ /dev/null @@ -1,4 +0,0 @@ -# nsNetworkInfoService.js - -component {0d8245af-d5d3-4100-a6b5-1cc026aa19f2} nsNetworkInfoService.js -contract @mozilla.org/network-info-service;1 {0d8245af-d5d3-4100-a6b5-1cc026aa19f2} diff --git a/netwerk/build/nsNetModule.cpp b/netwerk/build/nsNetModule.cpp index acd6a74b8c97..4b50d155c558 100644 --- a/netwerk/build/nsNetModule.cpp +++ b/netwerk/build/nsNetModule.cpp @@ -47,8 +47,7 @@ #define BUILD_BINHEX_DECODER 1 #endif -#if defined(XP_MACOSX) || defined(XP_WIN) || \ - (defined(XP_LINUX) && !defined(MOZ_WIDGET_ANDROID)) +#if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX) #define BUILD_NETWORK_INFO_SERVICE 1 #endif