gecko-dev/devtools/client/app-manager/content/device.js
Carsten "Tomcat" Book 08997000eb Backed out 2 changesets (bug 1202902) to recking bug 1202902 to be able to reopen inbound on a CLOSED TREE
Backed out changeset 647025383676 (bug 1202902)
Backed out changeset d70c7fe532c6 (bug 1202902)
2015-10-07 14:03:21 +02:00

235 lines
7.0 KiB
JavaScript

/* 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/. */
var Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
const {gDevTools} = Cu.import("resource:///modules/devtools/client/framework/gDevTools.jsm", {});
const {require} = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
const {TargetFactory} = require("devtools/client/framework/target");
const {ConnectionManager, Connection}
= require("devtools/shared/client/connection-manager");
const {getDeviceFront} = require("devtools/server/actors/device");
const {getTargetForApp, launchApp, closeApp}
= require("devtools/shared/apps/app-actor-front");
const DeviceStore = require("devtools/client/app-manager/device-store");
const WebappsStore = require("devtools/client/app-manager/webapps-store");
const promise = require("devtools/shared/deprecated-sync-thenables");
const DEFAULT_APP_ICON = "chrome://devtools/skin/themes/app-manager/images/default-app-icon.png";
window.addEventListener("message", function(event) {
try {
let message = JSON.parse(event.data);
if (message.name == "connection") {
let cid = parseInt(message.cid);
for (let c of ConnectionManager.connections) {
if (c.uid == cid) {
UI.connection = c;
UI.onNewConnection();
break;
}
}
}
} catch(e) {
Cu.reportError(e);
}
});
window.addEventListener("unload", function onUnload() {
window.removeEventListener("unload", onUnload);
UI.destroy();
});
var UI = {
init: function() {
this.showFooterIfNeeded();
this.setTab("apps");
if (this.connection) {
this.onNewConnection();
} else {
this.hide();
}
},
destroy: function() {
if (this.connection) {
this.connection.off(Connection.Events.STATUS_CHANGED, this._onConnectionStatusChange);
}
if (this.store) {
this.store.destroy();
}
if (this.template) {
this.template.destroy();
}
},
showFooterIfNeeded: function() {
let footer = document.querySelector("#connection-footer");
if (window.parent == window) {
// We're alone. Let's add a footer.
footer.removeAttribute("hidden");
footer.src = "chrome://devtools/content/app-manager/content/connection-footer.xhtml";
} else {
footer.setAttribute("hidden", "true");
}
},
hide: function() {
document.body.classList.add("notconnected");
},
show: function() {
document.body.classList.remove("notconnected");
},
onNewConnection: function() {
this.connection.on(Connection.Events.STATUS_CHANGED, this._onConnectionStatusChange);
this.store = Utils.mergeStores({
"device": new DeviceStore(this.connection),
"apps": new WebappsStore(this.connection),
});
if (this.template) {
this.template.destroy();
}
this.template = new Template(document.body, this.store, Utils.l10n);
this.template.start();
this._onConnectionStatusChange();
},
setWallpaper: function(dataurl) {
document.getElementById("meta").style.backgroundImage = "url(" + dataurl + ")";
},
_onConnectionStatusChange: function() {
if (this.connection.status != Connection.Status.CONNECTED) {
this.hide();
this.listTabsResponse = null;
} else {
this.show();
this.connection.client.listTabs(
response => {
this.listTabsResponse = response;
let front = getDeviceFront(this.connection.client, this.listTabsResponse);
front.getWallpaper().then(longstr => {
longstr.string().then(dataURL => {
longstr.release().then(null, Cu.reportError);
this.setWallpaper(dataURL);
});
});
if (Services.prefs.getBoolPref("devtools.chrome.enabled")) {
let rootButton = document.getElementById("root-actor-debug");
if (response.consoleActor) {
rootButton.removeAttribute("hidden");
} else {
rootButton.setAttribute("hidden", "true");
}
}
let tabsButton = document.querySelector(".tab.browser-tabs");
if (response.tabs.length > 0) {
tabsButton.classList.remove("hidden");
} else {
tabsButton.classList.add("hidden");
}
}
);
}
},
get connected() { return !!this.listTabsResponse; },
setTab: function(name) {
var tab = document.querySelector(".tab.selected");
var panel = document.querySelector(".tabpanel.selected");
if (tab) tab.classList.remove("selected");
if (panel) panel.classList.remove("selected");
var tab = document.querySelector(".tab." + name);
var panel = document.querySelector(".tabpanel." + name);
if (tab) tab.classList.add("selected");
if (panel) panel.classList.add("selected");
},
openToolboxForRootActor: function() {
if (!this.connected) {
return;
}
let options = {
form: this.listTabsResponse,
client: this.connection.client,
chrome: true
};
TargetFactory.forRemoteTab(options).then((target) => {
top.UI.openAndShowToolboxForTarget(target, "Main process", null);
});
},
openToolboxForApp: function(manifest) {
if (!this.connected) {
return;
}
let app = this.store.object.apps.all.filter(a => a.manifestURL == manifest)[0];
getTargetForApp(this.connection.client,
this.listTabsResponse.webappsActor,
manifest).then((target) => {
top.UI.openAndShowToolboxForTarget(target, app.name, app.iconURL);
}, console.error);
},
_getTargetForTab: function (form) {
let options = {
form: form,
client: this.connection.client,
chrome: false
};
let deferred = promise.defer();
return TargetFactory.forRemoteTab(options);
},
openToolboxForTab: function (aNode) {
let index = Array.prototype.indexOf.apply(
aNode.parentNode.parentNode.parentNode.children,
[aNode.parentNode.parentNode]);
this.connection.client.listTabs(
response => {
let tab = response.tabs[index];
this._getTargetForTab(tab).then(target => {
top.UI.openAndShowToolboxForTarget(
target, tab.title, DEFAULT_APP_ICON);
}, console.error);
}
);
},
startApp: function(manifest) {
if (!this.connected) {
return promise.reject();
}
return launchApp(this.connection.client,
this.listTabsResponse.webappsActor,
manifest);
},
stopApp: function(manifest) {
if (!this.connected) {
return promise.reject();
}
return closeApp(this.connection.client,
this.listTabsResponse.webappsActor,
manifest);
},
}
// This must be bound immediately, as it might be used via the message listener
// before UI.init() has been called.
UI._onConnectionStatusChange = UI._onConnectionStatusChange.bind(UI);