Bug 1272101 - Fix nsINetworkInfoService implementation on Android to use Linux C APIs instead of Android Java APIs. r=snorp

This commit is contained in:
Kannan Vijayan 2016-06-02 16:42:36 -04:00
Родитель c50d0b99ce
Коммит b21283649e
9 изменённых файлов: 4 добавлений и 283 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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<String> addresses = new HashSet<String>();
try {
Enumeration<NetworkInterface> ifaceEnum = NetworkInterface.getNetworkInterfaces();
if (ifaceEnum != null) {
while (ifaceEnum.hasMoreElements()) {
NetworkInterface iface = ifaceEnum.nextElement();
Enumeration<InetAddress> 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;
}
}

Просмотреть файл

@ -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',

Просмотреть файл

@ -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

Просмотреть файл

@ -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);
}
});
}
}

Просмотреть файл

@ -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']:

Просмотреть файл

@ -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]);

Просмотреть файл

@ -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}

Просмотреть файл

@ -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