2014-06-25 09:12:07 +04:00
|
|
|
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2013-08-20 04:22:47 +04:00
|
|
|
/* 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 = ["Home"];
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-12-20 02:51:10 +04:00
|
|
|
Cu.import("resource://gre/modules/SharedPreferences.jsm");
|
2014-02-11 21:16:00 +04:00
|
|
|
Cu.import("resource://gre/modules/Messaging.jsm");
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-03-27 20:25:04 +04:00
|
|
|
// Keep this in sync with the constant defined in PanelAuthCache.java
|
|
|
|
const PREFS_PANEL_AUTH_PREFIX = "home_panels_auth_";
|
|
|
|
|
2014-05-01 01:38:02 +04:00
|
|
|
// Default weight for a banner message.
|
|
|
|
const DEFAULT_WEIGHT = 100;
|
|
|
|
|
2013-08-20 04:22:47 +04:00
|
|
|
// See bug 915424
|
|
|
|
function resolveGeckoURI(aURI) {
|
|
|
|
if (!aURI)
|
|
|
|
throw "Can't resolve an empty uri";
|
|
|
|
|
|
|
|
if (aURI.startsWith("chrome://")) {
|
|
|
|
let registry = Cc['@mozilla.org/chrome/chrome-registry;1'].getService(Ci["nsIChromeRegistry"]);
|
2017-01-09 22:27:26 +03:00
|
|
|
return registry.convertChromeURL(Services.io.newURI(aURI)).spec;
|
2013-08-20 04:22:47 +04:00
|
|
|
} else if (aURI.startsWith("resource://")) {
|
|
|
|
let handler = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
|
2017-01-09 22:27:26 +03:00
|
|
|
return handler.resolveURI(Services.io.newURI(aURI));
|
2013-08-20 04:22:47 +04:00
|
|
|
}
|
|
|
|
return aURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
function BannerMessage(options) {
|
|
|
|
let uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
|
|
|
|
this.id = uuidgen.generateUUID().toString();
|
|
|
|
|
|
|
|
if ("text" in options && options.text != null)
|
|
|
|
this.text = options.text;
|
|
|
|
|
|
|
|
if ("icon" in options && options.icon != null)
|
|
|
|
this.iconURI = resolveGeckoURI(options.icon);
|
|
|
|
|
2013-11-05 02:39:00 +04:00
|
|
|
if ("onshown" in options && typeof options.onshown === "function")
|
|
|
|
this.onshown = options.onshown;
|
|
|
|
|
2013-08-20 04:22:47 +04:00
|
|
|
if ("onclick" in options && typeof options.onclick === "function")
|
|
|
|
this.onclick = options.onclick;
|
2014-02-15 04:51:43 +04:00
|
|
|
|
|
|
|
if ("ondismiss" in options && typeof options.ondismiss === "function")
|
|
|
|
this.ondismiss = options.ondismiss;
|
2014-05-01 01:38:02 +04:00
|
|
|
|
|
|
|
let weight = parseInt(options.weight, 10);
|
|
|
|
this.weight = weight > 0 ? weight : DEFAULT_WEIGHT;
|
2013-08-20 04:22:47 +04:00
|
|
|
}
|
|
|
|
|
2014-04-15 21:03:03 +04:00
|
|
|
// We need this object to have access to the HomeBanner
|
|
|
|
// private members without leaking it outside Home.jsm.
|
2015-09-15 21:19:45 +03:00
|
|
|
var HomeBannerMessageHandlers;
|
2014-04-15 21:03:03 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var HomeBanner = (function () {
|
2014-04-15 21:03:03 +04:00
|
|
|
// Whether there is a "HomeBanner:Get" request we couldn't fulfill.
|
|
|
|
let _pendingRequest = false;
|
|
|
|
|
|
|
|
// Functions used to handle messages sent from Java.
|
|
|
|
HomeBannerMessageHandlers = {
|
|
|
|
"HomeBanner:Get": function handleBannerGet(data) {
|
2014-05-01 01:38:02 +04:00
|
|
|
if (Object.keys(_messages).length > 0) {
|
|
|
|
_sendBannerData();
|
|
|
|
} else {
|
2014-04-15 21:03:03 +04:00
|
|
|
_pendingRequest = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-20 04:22:47 +04:00
|
|
|
// Holds the messages that will rotate through the banner.
|
2014-02-14 20:52:14 +04:00
|
|
|
let _messages = {};
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-05-01 01:38:02 +04:00
|
|
|
// Choose a random message from the set of messages, biasing towards those with higher weight.
|
|
|
|
// Weight logic copied from desktop snippets:
|
|
|
|
// https://github.com/mozilla/snippets-service/blob/7d80edb8b1cddaed075275c2fc7cdf69a10f4003/snippets/base/templates/base/includes/snippet_js.html#L119
|
2014-04-15 21:03:03 +04:00
|
|
|
let _sendBannerData = function() {
|
2014-05-01 01:38:02 +04:00
|
|
|
let totalWeight = 0;
|
|
|
|
for (let key in _messages) {
|
|
|
|
let message = _messages[key];
|
|
|
|
totalWeight += message.weight;
|
|
|
|
message.totalWeight = totalWeight;
|
2014-04-15 21:03:03 +04:00
|
|
|
}
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-05-01 01:38:02 +04:00
|
|
|
let threshold = Math.random() * totalWeight;
|
|
|
|
for (let key in _messages) {
|
|
|
|
let message = _messages[key];
|
|
|
|
if (threshold < message.totalWeight) {
|
2014-09-02 22:59:05 +04:00
|
|
|
Messaging.sendRequest({
|
2014-05-01 01:38:02 +04:00
|
|
|
type: "HomeBanner:Data",
|
|
|
|
id: message.id,
|
|
|
|
text: message.text,
|
|
|
|
iconURI: message.iconURI
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 00:38:27 +04:00
|
|
|
};
|
2013-11-05 02:39:00 +04:00
|
|
|
|
2014-02-21 00:38:27 +04:00
|
|
|
let _handleShown = function(id) {
|
|
|
|
let message = _messages[id];
|
2013-11-05 02:39:00 +04:00
|
|
|
if (message.onshown)
|
|
|
|
message.onshown();
|
2014-02-14 20:52:14 +04:00
|
|
|
};
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
let _handleClick = function(id) {
|
|
|
|
let message = _messages[id];
|
2013-08-20 04:22:47 +04:00
|
|
|
if (message.onclick)
|
|
|
|
message.onclick();
|
2014-02-14 20:52:14 +04:00
|
|
|
};
|
|
|
|
|
2014-02-15 04:51:43 +04:00
|
|
|
let _handleDismiss = function(id) {
|
|
|
|
let message = _messages[id];
|
|
|
|
if (message.ondismiss)
|
|
|
|
message.ondismiss();
|
|
|
|
};
|
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
return Object.freeze({
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
switch(topic) {
|
2014-02-21 00:38:27 +04:00
|
|
|
case "HomeBanner:Shown":
|
|
|
|
_handleShown(data);
|
|
|
|
break;
|
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
case "HomeBanner:Click":
|
|
|
|
_handleClick(data);
|
|
|
|
break;
|
2014-02-15 04:51:43 +04:00
|
|
|
|
|
|
|
case "HomeBanner:Dismiss":
|
|
|
|
_handleDismiss(data);
|
2014-02-21 00:38:27 +04:00
|
|
|
break;
|
2014-02-14 20:52:14 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new banner message to the rotation.
|
|
|
|
*
|
|
|
|
* @return id Unique identifer for the message.
|
|
|
|
*/
|
|
|
|
add: function(options) {
|
|
|
|
let message = new BannerMessage(options);
|
|
|
|
_messages[message.id] = message;
|
|
|
|
|
|
|
|
// If this is the first message we're adding, add
|
|
|
|
// observers to listen for requests from the Java UI.
|
|
|
|
if (Object.keys(_messages).length == 1) {
|
2014-02-21 00:38:27 +04:00
|
|
|
Services.obs.addObserver(this, "HomeBanner:Shown", false);
|
2014-02-14 20:52:14 +04:00
|
|
|
Services.obs.addObserver(this, "HomeBanner:Click", false);
|
2014-02-15 04:51:43 +04:00
|
|
|
Services.obs.addObserver(this, "HomeBanner:Dismiss", false);
|
2014-02-14 20:52:14 +04:00
|
|
|
|
2014-04-15 21:03:03 +04:00
|
|
|
// Send a message to Java if there's a pending "HomeBanner:Get" request.
|
|
|
|
if (_pendingRequest) {
|
|
|
|
_pendingRequest = false;
|
|
|
|
_sendBannerData();
|
|
|
|
}
|
2014-02-14 20:52:14 +04:00
|
|
|
}
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
return message.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a banner message from the rotation.
|
|
|
|
*
|
|
|
|
* @param id The id of the message to remove.
|
|
|
|
*/
|
|
|
|
remove: function(id) {
|
2014-02-22 21:41:31 +04:00
|
|
|
if (!(id in _messages)) {
|
|
|
|
throw "Home.banner: Can't remove message that doesn't exist: id = " + id;
|
|
|
|
}
|
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
delete _messages[id];
|
|
|
|
|
|
|
|
// If there are no more messages, remove the observers.
|
|
|
|
if (Object.keys(_messages).length == 0) {
|
2014-02-21 00:38:27 +04:00
|
|
|
Services.obs.removeObserver(this, "HomeBanner:Shown");
|
2014-02-14 20:52:14 +04:00
|
|
|
Services.obs.removeObserver(this, "HomeBanner:Click");
|
2014-02-15 04:51:43 +04:00
|
|
|
Services.obs.removeObserver(this, "HomeBanner:Dismiss");
|
2014-02-14 20:52:14 +04:00
|
|
|
}
|
2013-08-20 04:22:47 +04:00
|
|
|
}
|
2014-02-14 20:52:14 +04:00
|
|
|
});
|
|
|
|
})();
|
2013-08-20 04:22:47 +04:00
|
|
|
|
2014-04-05 02:55:03 +04:00
|
|
|
// We need this object to have access to the HomePanels
|
2014-02-25 15:28:58 +04:00
|
|
|
// private members without leaking it outside Home.jsm.
|
2015-09-15 21:19:45 +03:00
|
|
|
var HomePanelsMessageHandlers;
|
2014-02-25 15:28:58 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var HomePanels = (function () {
|
2014-04-05 02:55:03 +04:00
|
|
|
// Functions used to handle messages sent from Java.
|
|
|
|
HomePanelsMessageHandlers = {
|
|
|
|
|
|
|
|
"HomePanels:Get": function handlePanelsGet(data) {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
|
|
|
|
let requestId = data.requestId;
|
|
|
|
let ids = data.ids || null;
|
|
|
|
|
|
|
|
let panels = [];
|
|
|
|
for (let id in _registeredPanels) {
|
|
|
|
// Null ids means we want to fetch all available panels
|
|
|
|
if (ids == null || ids.indexOf(id) >= 0) {
|
|
|
|
try {
|
|
|
|
panels.push(_generatePanel(id));
|
|
|
|
} catch(e) {
|
|
|
|
Cu.reportError("Home.panels: Invalid options, panel.id = " + id + ": " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 02:53:58 +03:00
|
|
|
EventDispatcher.instance.sendRequest({
|
2014-04-05 02:55:03 +04:00
|
|
|
type: "HomePanels:Data",
|
|
|
|
panels: panels,
|
|
|
|
requestId: requestId
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
"HomePanels:Authenticate": function handlePanelsAuthenticate(id) {
|
|
|
|
// Generate panel options to get auth handler.
|
|
|
|
let options = _registeredPanels[id]();
|
2014-04-05 02:55:06 +04:00
|
|
|
if (!options.auth) {
|
|
|
|
throw "Home.panels: Invalid auth for panel.id = " + id;
|
2014-04-05 02:55:03 +04:00
|
|
|
}
|
2014-04-05 02:55:06 +04:00
|
|
|
if (!options.auth.authenticate || typeof options.auth.authenticate !== "function") {
|
|
|
|
throw "Home.panels: Invalid auth authenticate function: panel.id = " + this.id;
|
2014-04-05 02:55:03 +04:00
|
|
|
}
|
2014-04-05 02:55:06 +04:00
|
|
|
options.auth.authenticate();
|
2014-04-05 02:55:03 +04:00
|
|
|
},
|
|
|
|
|
2014-04-15 17:51:00 +04:00
|
|
|
"HomePanels:RefreshView": function handlePanelsRefreshView(data) {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
|
|
|
|
let options = _registeredPanels[data.panelId]();
|
|
|
|
let view = options.views[data.viewIndex];
|
|
|
|
|
|
|
|
if (!view) {
|
|
|
|
throw "Home.panels: Invalid view for panel.id = " + data.panelId
|
|
|
|
+ ", view.index = " + data.viewIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!view.onrefresh || typeof view.onrefresh !== "function") {
|
|
|
|
throw "Home.panels: Invalid onrefresh for panel.id = " + data.panelId
|
|
|
|
+ ", view.index = " + data.viewIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
view.onrefresh();
|
|
|
|
},
|
|
|
|
|
2014-04-05 02:55:03 +04:00
|
|
|
"HomePanels:Installed": function handlePanelsInstalled(id) {
|
2014-04-26 03:57:56 +04:00
|
|
|
_assertPanelExists(id);
|
|
|
|
|
2014-04-05 02:55:03 +04:00
|
|
|
let options = _registeredPanels[id]();
|
|
|
|
if (!options.oninstall) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof options.oninstall !== "function") {
|
|
|
|
throw "Home.panels: Invalid oninstall function: panel.id = " + this.id;
|
|
|
|
}
|
|
|
|
options.oninstall();
|
|
|
|
},
|
|
|
|
|
|
|
|
"HomePanels:Uninstalled": function handlePanelsUninstalled(id) {
|
2014-04-26 03:57:56 +04:00
|
|
|
_assertPanelExists(id);
|
|
|
|
|
2014-04-05 02:55:03 +04:00
|
|
|
let options = _registeredPanels[id]();
|
|
|
|
if (!options.onuninstall) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof options.onuninstall !== "function") {
|
|
|
|
throw "Home.panels: Invalid onuninstall function: panel.id = " + this.id;
|
|
|
|
}
|
|
|
|
options.onuninstall();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-24 14:17:05 +04:00
|
|
|
// Holds the current set of registered panels that can be
|
2014-02-25 21:29:49 +04:00
|
|
|
// installed, updated, uninstalled, or unregistered. It maps
|
|
|
|
// panel ids with the functions that dynamically generate
|
|
|
|
// their respective panel options. This is used to retrieve
|
|
|
|
// the current list of available panels in the system.
|
|
|
|
// See HomePanels:Get handler.
|
2014-02-24 14:17:05 +04:00
|
|
|
let _registeredPanels = {};
|
2013-12-20 02:51:10 +04:00
|
|
|
|
2014-02-25 21:29:49 +04:00
|
|
|
// Valid layouts for a panel.
|
|
|
|
let Layout = Object.freeze({
|
|
|
|
FRAME: "frame"
|
|
|
|
});
|
|
|
|
|
|
|
|
// Valid types of views for a dataset.
|
|
|
|
let View = Object.freeze({
|
|
|
|
LIST: "list",
|
|
|
|
GRID: "grid"
|
|
|
|
});
|
|
|
|
|
|
|
|
// Valid item types for a panel view.
|
|
|
|
let Item = Object.freeze({
|
|
|
|
ARTICLE: "article",
|
2015-06-16 14:53:59 +03:00
|
|
|
IMAGE: "image",
|
|
|
|
ICON: "icon"
|
2014-02-25 21:29:49 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Valid item handlers for a panel view.
|
|
|
|
let ItemHandler = Object.freeze({
|
|
|
|
BROWSER: "browser",
|
|
|
|
INTENT: "intent"
|
|
|
|
});
|
|
|
|
|
2014-03-04 02:23:06 +04:00
|
|
|
function Panel(id, options) {
|
|
|
|
this.id = id;
|
|
|
|
this.title = options.title;
|
|
|
|
this.layout = options.layout;
|
|
|
|
this.views = options.views;
|
2015-05-07 03:44:10 +03:00
|
|
|
this.default = !!options.default;
|
2014-02-25 21:29:49 +04:00
|
|
|
|
2014-03-04 02:23:06 +04:00
|
|
|
if (!this.id || !this.title) {
|
2014-02-25 21:29:49 +04:00
|
|
|
throw "Home.panels: Can't create a home panel without an id and title!";
|
|
|
|
}
|
|
|
|
|
2014-03-04 02:23:06 +04:00
|
|
|
if (!this.layout) {
|
2014-02-25 21:40:50 +04:00
|
|
|
// Use FRAME layout by default
|
2014-03-04 02:23:06 +04:00
|
|
|
this.layout = Layout.FRAME;
|
|
|
|
} else if (!_valueExists(Layout, this.layout)) {
|
|
|
|
throw "Home.panels: Invalid layout for panel: panel.id = " + this.id + ", panel.layout =" + this.layout;
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
|
|
|
|
2014-03-04 02:23:06 +04:00
|
|
|
for (let view of this.views) {
|
2014-02-25 21:29:49 +04:00
|
|
|
if (!_valueExists(View, view.type)) {
|
2014-03-04 02:23:06 +04:00
|
|
|
throw "Home.panels: Invalid view type: panel.id = " + this.id + ", view.type = " + view.type;
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!view.itemType) {
|
|
|
|
if (view.type == View.LIST) {
|
|
|
|
// Use ARTICLE item type by default in LIST views
|
|
|
|
view.itemType = Item.ARTICLE;
|
|
|
|
} else if (view.type == View.GRID) {
|
|
|
|
// Use IMAGE item type by default in GRID views
|
|
|
|
view.itemType = Item.IMAGE;
|
|
|
|
}
|
|
|
|
} else if (!_valueExists(Item, view.itemType)) {
|
2014-03-04 02:23:06 +04:00
|
|
|
throw "Home.panels: Invalid item type: panel.id = " + this.id + ", view.itemType = " + view.itemType;
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!view.itemHandler) {
|
|
|
|
// Use BROWSER item handler by default
|
|
|
|
view.itemHandler = ItemHandler.BROWSER;
|
|
|
|
} else if (!_valueExists(ItemHandler, view.itemHandler)) {
|
2014-03-04 02:23:06 +04:00
|
|
|
throw "Home.panels: Invalid item handler: panel.id = " + this.id + ", view.itemHandler = " + view.itemHandler;
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!view.dataset) {
|
2014-03-04 02:23:06 +04:00
|
|
|
throw "Home.panels: No dataset provided for view: panel.id = " + this.id + ", view.type = " + view.type;
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
2014-04-15 17:48:00 +04:00
|
|
|
|
|
|
|
if (view.onrefresh) {
|
|
|
|
view.refreshEnabled = true;
|
|
|
|
}
|
2014-02-25 21:29:49 +04:00
|
|
|
}
|
2014-03-27 20:25:03 +04:00
|
|
|
|
2014-04-05 02:55:06 +04:00
|
|
|
if (options.auth) {
|
|
|
|
if (!options.auth.messageText) {
|
|
|
|
throw "Home.panels: Invalid auth messageText: panel.id = " + this.id;
|
2014-03-27 20:25:03 +04:00
|
|
|
}
|
2014-04-05 02:55:06 +04:00
|
|
|
if (!options.auth.buttonText) {
|
|
|
|
throw "Home.panels: Invalid auth buttonText: panel.id = " + this.id;
|
2014-03-27 20:25:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.authConfig = {
|
2014-04-05 02:55:06 +04:00
|
|
|
messageText: options.auth.messageText,
|
|
|
|
buttonText: options.auth.buttonText
|
2014-03-27 20:25:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Include optional image URL if it is specified.
|
2014-04-05 02:55:06 +04:00
|
|
|
if (options.auth.imageUrl) {
|
|
|
|
this.authConfig.imageUrl = options.auth.imageUrl;
|
2014-03-27 20:25:03 +04:00
|
|
|
}
|
|
|
|
}
|
2015-05-07 03:44:10 +03:00
|
|
|
|
2016-03-18 06:50:07 +03:00
|
|
|
if (options.position >= 0) {
|
2015-05-07 03:44:10 +03:00
|
|
|
this.position = options.position;
|
|
|
|
}
|
2014-03-04 02:23:06 +04:00
|
|
|
}
|
2014-02-25 21:29:49 +04:00
|
|
|
|
2014-03-04 02:23:06 +04:00
|
|
|
let _generatePanel = function(id) {
|
|
|
|
let options = _registeredPanels[id]();
|
|
|
|
return new Panel(id, options);
|
2014-02-14 20:52:14 +04:00
|
|
|
};
|
2014-02-05 18:14:51 +04:00
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
// Helper function used to see if a value is in an object.
|
|
|
|
let _valueExists = function(obj, value) {
|
|
|
|
for (let key in obj) {
|
|
|
|
if (obj[key] == value) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-17 21:27:07 +04:00
|
|
|
}
|
2014-02-14 20:52:14 +04:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-02-21 14:33:59 +04:00
|
|
|
let _assertPanelExists = function(id) {
|
2014-02-24 14:17:05 +04:00
|
|
|
if (!(id in _registeredPanels)) {
|
2014-02-21 14:33:59 +04:00
|
|
|
throw "Home.panels: Panel doesn't exist: id = " + id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-14 20:52:14 +04:00
|
|
|
return Object.freeze({
|
2014-02-25 21:29:49 +04:00
|
|
|
Layout: Layout,
|
|
|
|
View: View,
|
|
|
|
Item: Item,
|
|
|
|
ItemHandler: ItemHandler,
|
2014-01-17 21:27:07 +04:00
|
|
|
|
2014-02-25 21:29:49 +04:00
|
|
|
register: function(id, optionsCallback) {
|
2014-02-21 14:33:59 +04:00
|
|
|
// Bail if the panel already exists
|
2014-02-25 21:29:49 +04:00
|
|
|
if (id in _registeredPanels) {
|
|
|
|
throw "Home.panels: Panel already exists: id = " + id;
|
2014-02-21 14:33:59 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 21:29:49 +04:00
|
|
|
if (!optionsCallback || typeof optionsCallback !== "function") {
|
|
|
|
throw "Home.panels: Panel callback must be a function: id = " + id;
|
2014-01-17 21:27:07 +04:00
|
|
|
}
|
2013-12-20 02:51:10 +04:00
|
|
|
|
2014-02-25 21:29:49 +04:00
|
|
|
_registeredPanels[id] = optionsCallback;
|
2014-02-21 14:33:59 +04:00
|
|
|
},
|
2014-02-05 18:14:51 +04:00
|
|
|
|
2014-02-21 14:33:59 +04:00
|
|
|
unregister: function(id) {
|
|
|
|
_assertPanelExists(id);
|
2014-02-13 15:19:57 +04:00
|
|
|
|
2014-02-24 14:17:05 +04:00
|
|
|
delete _registeredPanels[id];
|
2014-02-21 14:33:59 +04:00
|
|
|
},
|
2014-02-13 15:19:57 +04:00
|
|
|
|
2014-02-21 14:33:59 +04:00
|
|
|
install: function(id) {
|
|
|
|
_assertPanelExists(id);
|
2014-02-13 15:19:57 +04:00
|
|
|
|
2014-09-02 22:59:05 +04:00
|
|
|
Messaging.sendRequest({
|
2014-02-21 14:33:59 +04:00
|
|
|
type: "HomePanels:Install",
|
2014-02-25 21:29:49 +04:00
|
|
|
panel: _generatePanel(id)
|
2014-02-21 14:33:59 +04:00
|
|
|
});
|
2014-02-14 20:52:14 +04:00
|
|
|
},
|
2013-12-20 02:51:10 +04:00
|
|
|
|
2014-02-21 14:33:59 +04:00
|
|
|
uninstall: function(id) {
|
|
|
|
_assertPanelExists(id);
|
2013-12-20 02:51:10 +04:00
|
|
|
|
2014-09-02 22:59:05 +04:00
|
|
|
Messaging.sendRequest({
|
2014-02-21 14:33:58 +04:00
|
|
|
type: "HomePanels:Uninstall",
|
2014-02-21 14:33:59 +04:00
|
|
|
id: id
|
2014-02-14 20:52:14 +04:00
|
|
|
});
|
2014-02-21 14:33:59 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function(id) {
|
|
|
|
_assertPanelExists(id);
|
|
|
|
|
2014-09-02 22:59:05 +04:00
|
|
|
Messaging.sendRequest({
|
2014-02-21 14:33:59 +04:00
|
|
|
type: "HomePanels:Update",
|
2014-02-25 21:29:49 +04:00
|
|
|
panel: _generatePanel(id)
|
2014-02-21 14:33:59 +04:00
|
|
|
});
|
2014-03-27 20:25:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setAuthenticated: function(id, isAuthenticated) {
|
|
|
|
_assertPanelExists(id);
|
|
|
|
|
|
|
|
let authKey = PREFS_PANEL_AUTH_PREFIX + id;
|
2014-05-10 19:39:53 +04:00
|
|
|
let sharedPrefs = SharedPreferences.forProfile();
|
2014-03-27 20:25:04 +04:00
|
|
|
sharedPrefs.setBoolPref(authKey, isAuthenticated);
|
2014-01-17 21:27:07 +04:00
|
|
|
}
|
2014-02-14 20:52:14 +04:00
|
|
|
});
|
|
|
|
})();
|
2013-12-20 02:51:10 +04:00
|
|
|
|
2013-08-20 04:22:47 +04:00
|
|
|
// Public API
|
2014-02-09 06:02:27 +04:00
|
|
|
this.Home = Object.freeze({
|
2013-12-20 02:51:10 +04:00
|
|
|
banner: HomeBanner,
|
2014-01-15 03:08:10 +04:00
|
|
|
panels: HomePanels,
|
|
|
|
|
|
|
|
// Lazy notification observer registered in browser.js
|
|
|
|
observe: function(subject, topic, data) {
|
2014-04-15 21:03:03 +04:00
|
|
|
if (topic in HomeBannerMessageHandlers) {
|
|
|
|
HomeBannerMessageHandlers[topic](data);
|
|
|
|
} else if (topic in HomePanelsMessageHandlers) {
|
2014-04-05 02:55:03 +04:00
|
|
|
HomePanelsMessageHandlers[topic](data);
|
|
|
|
} else {
|
|
|
|
Cu.reportError("Home.observe: message handler not found for topic: " + topic);
|
2014-01-15 03:08:10 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 06:02:27 +04:00
|
|
|
});
|