Bug 1516529 - Restore connections for NetworkRuntimes;r=daisuke

Differential Revision: https://phabricator.services.mozilla.com/D15409

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Julian Descottes 2019-01-04 12:44:09 +00:00
parent d8e384fc5e
commit b3a52f0804
6 changed files with 80 additions and 59 deletions

View File

@ -78,8 +78,7 @@ const AboutDebugging = {
this.mount
);
this.actions.updateNetworkLocations(getNetworkLocations());
this.onNetworkLocationsUpdated();
addNetworkLocationsObserver(this.onNetworkLocationsUpdated);
// Listen to USB runtime updates and retrieve the initial list of runtimes.

View File

@ -7,6 +7,7 @@
const Actions = require("./index");
const {
getAllRuntimes,
getCurrentRuntime,
findRuntimeById,
} = require("../modules/runtimes-state-helper");
@ -25,6 +26,7 @@ const {
DISCONNECT_RUNTIME_FAILURE,
DISCONNECT_RUNTIME_START,
DISCONNECT_RUNTIME_SUCCESS,
REMOTE_RUNTIMES_UPDATED,
RUNTIME_PREFERENCE,
RUNTIMES,
UNWATCH_RUNTIME_FAILURE,
@ -33,7 +35,6 @@ const {
UPDATE_CONNECTION_PROMPT_SETTING_FAILURE,
UPDATE_CONNECTION_PROMPT_SETTING_START,
UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS,
USB_RUNTIMES_UPDATED,
WATCH_RUNTIME_FAILURE,
WATCH_RUNTIME_START,
WATCH_RUNTIME_SUCCESS,
@ -195,12 +196,48 @@ function unwatchRuntime(id) {
};
}
function updateUSBRuntimes(runtimes) {
function updateNetworkRuntimes(locations) {
const runtimes = locations.map(location => {
const [ host, port ] = location.split(":");
return {
id: location,
extra: {
connectionParameters: { host, port: parseInt(port, 10) },
},
isUnknown: false,
name: location,
type: RUNTIMES.NETWORK,
};
});
return updateRemoteRuntimes(runtimes, RUNTIMES.NETWORK);
}
function updateUSBRuntimes(adbRuntimes) {
const runtimes = adbRuntimes.map(adbRuntime => {
// Set connectionParameters only for known runtimes.
const socketPath = adbRuntime._socketPath;
const deviceId = adbRuntime.deviceId;
const connectionParameters = adbRuntime.isUnknown() ? null : { deviceId, socketPath };
return {
id: adbRuntime.id,
extra: {
connectionParameters,
deviceName: adbRuntime.deviceName,
},
isUnknown: adbRuntime.isUnknown(),
name: adbRuntime.shortName,
type: RUNTIMES.USB,
};
});
return updateRemoteRuntimes(runtimes, RUNTIMES.USB);
}
function updateRemoteRuntimes(runtimes, type) {
return async (dispatch, getState) => {
const currentRuntime = getCurrentRuntime(getState().runtimes);
if (currentRuntime &&
currentRuntime.type === RUNTIMES.USB &&
currentRuntime.type === type &&
!runtimes.find(runtime => currentRuntime.id === runtime.id)) {
// Since current USB runtime was invalid, move to this firefox page.
// This case is considered as followings and so on:
@ -215,18 +252,33 @@ function updateUSBRuntimes(runtimes) {
await dispatch(Actions.selectPage(RUNTIMES.THIS_FIREFOX, RUNTIMES.THIS_FIREFOX));
}
// Retrieve runtimeDetails from existing runtimes.
runtimes.forEach(runtime => {
const existingRuntime = findRuntimeById(runtime.id, getState().runtimes);
runtime.runtimeDetails = existingRuntime ? existingRuntime.runtimeDetails : null;
});
// Disconnect runtimes that were no longer valid
const validIds = runtimes.map(r => r.id);
const existingRuntimes = getState().runtimes.usbRuntimes;
const invalidRuntimes = existingRuntimes.filter(r => !validIds.includes(r.id));
const existingRuntimes = getAllRuntimes(getState().runtimes);
const invalidRuntimes = existingRuntimes.filter(r => {
return r.type === type && !validIds.includes(r.id);
});
for (const invalidRuntime of invalidRuntimes) {
await dispatch(disconnectRuntime(invalidRuntime.id));
const isConnected = !!invalidRuntime.runtimeDetails;
if (isConnected) {
await dispatch(disconnectRuntime(invalidRuntime.id));
}
}
dispatch({ type: USB_RUNTIMES_UPDATED, runtimes });
dispatch({ type: REMOTE_RUNTIMES_UPDATED, runtimes, runtimeType: type });
for (const runtime of getAllRuntimes(getState().runtimes)) {
if (runtime.type !== type) {
continue;
}
for (const runtime of getState().runtimes.usbRuntimes) {
const isConnected = !!runtime.runtimeDetails;
const hasConnectedClient = remoteClientManager.hasClient(runtime.id, runtime.type);
if (!isConnected && hasConnectedClient) {
@ -259,6 +311,7 @@ module.exports = {
removeRuntimeListeners,
unwatchRuntime,
updateConnectionPromptSetting,
updateNetworkRuntimes,
updateUSBRuntimes,
watchRuntime,
};

View File

@ -80,7 +80,10 @@ function updateAdbAddonStatus(adbAddonStatus) {
}
function updateNetworkLocations(locations) {
return { type: NETWORK_LOCATIONS_UPDATED, locations };
return (dispatch, getState) => {
dispatch(Actions.updateNetworkRuntimes(locations));
dispatch({ type: NETWORK_LOCATIONS_UPDATED, locations });
};
}
function installAdbAddon() {

View File

@ -22,6 +22,7 @@ const actionTypes = {
MULTI_E10S_UPDATED: "MULTI_E10S_UPDATED",
NETWORK_LOCATIONS_UPDATED: "NETWORK_LOCATIONS_UPDATED",
PAGE_SELECTED: "PAGE_SELECTED",
REMOTE_RUNTIMES_UPDATED: "REMOTE_RUNTIMES_UPDATED",
REQUEST_EXTENSIONS_FAILURE: "REQUEST_EXTENSIONS_FAILURE",
REQUEST_EXTENSIONS_START: "REQUEST_EXTENSIONS_START",
REQUEST_EXTENSIONS_SUCCESS: "REQUEST_EXTENSIONS_SUCCESS",
@ -42,7 +43,6 @@ const actionTypes = {
UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS: "UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS",
USB_RUNTIMES_SCAN_START: "USB_RUNTIMES_SCAN_START",
USB_RUNTIMES_SCAN_SUCCESS: "USB_RUNTIMES_SCAN_SUCCESS",
USB_RUNTIMES_UPDATED: "USB_RUNTIMES_UPDATED",
WATCH_RUNTIME_FAILURE: "WATCH_RUNTIME_FAILURE",
WATCH_RUNTIME_START: "WATCH_RUNTIME_START",
WATCH_RUNTIME_SUCCESS: "WATCH_RUNTIME_SUCCESS",

View File

@ -29,14 +29,18 @@ function getCurrentConnectionPromptSetting(runtimesState) {
exports.getCurrentConnectionPromptSetting = getCurrentConnectionPromptSetting;
function findRuntimeById(id, runtimesState) {
const allRuntimes = [
return getAllRuntimes(runtimesState).find(r => r.id === id);
}
exports.findRuntimeById = findRuntimeById;
function getAllRuntimes(runtimesState) {
return [
...runtimesState.networkRuntimes,
...runtimesState.thisFirefoxRuntimes,
...runtimesState.usbRuntimes,
];
return allRuntimes.find(r => r.id === id);
}
exports.findRuntimeById = findRuntimeById;
exports.getAllRuntimes = getAllRuntimes;
function getCurrentRuntimeDetails(runtimesState) {
const runtime = getCurrentRuntime(runtimesState);

View File

@ -7,11 +7,10 @@
const {
CONNECT_RUNTIME_SUCCESS,
DISCONNECT_RUNTIME_SUCCESS,
NETWORK_LOCATIONS_UPDATED,
RUNTIMES,
UNWATCH_RUNTIME_SUCCESS,
UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS,
USB_RUNTIMES_UPDATED,
REMOTE_RUNTIMES_UPDATED,
WATCH_RUNTIME_SUCCESS,
} = require("../constants");
@ -85,23 +84,6 @@ function runtimesReducer(state = RuntimesState(), action) {
return _updateRuntimeById(id, { runtimeDetails: null }, state);
}
case NETWORK_LOCATIONS_UPDATED: {
const { locations } = action;
const networkRuntimes = locations.map(location => {
const [ host, port ] = location.split(":");
return {
id: location,
extra: {
connectionParameters: { host, port: parseInt(port, 10) },
},
isUnknown: false,
name: location,
type: RUNTIMES.NETWORK,
};
});
return Object.assign({}, state, { networkRuntimes });
}
case UNWATCH_RUNTIME_SUCCESS: {
return Object.assign({}, state, { selectedRuntimeId: null });
}
@ -115,32 +97,12 @@ function runtimesReducer(state = RuntimesState(), action) {
return _updateRuntimeById(runtimeId, { runtimeDetails }, state);
}
case USB_RUNTIMES_UPDATED: {
const { runtimes } = action;
const usbRuntimes = runtimes.map(runtime => {
// Retrieve runtimeDetails from existing runtimes.
const existingRuntime = findRuntimeById(runtime.id, state);
const runtimeDetails = existingRuntime ? existingRuntime.runtimeDetails : null;
// Set connectionParameters only for known runtimes.
const socketPath = runtime._socketPath;
const deviceId = runtime.deviceId;
const connectionParameters =
runtime.isUnknown() ? null : { deviceId, socketPath };
return {
id: runtime.id,
extra: {
connectionParameters,
deviceName: runtime.deviceName,
},
isUnknown: runtime.isUnknown(),
name: runtime.shortName,
runtimeDetails,
type: RUNTIMES.USB,
};
case REMOTE_RUNTIMES_UPDATED: {
const { runtimes, runtimeType } = action;
const key = TYPE_TO_RUNTIMES_KEY[runtimeType];
return Object.assign({}, state, {
[key]: runtimes,
});
return Object.assign({}, state, { usbRuntimes });
}
case WATCH_RUNTIME_SUCCESS: {