2014-08-14 08:38:37 +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";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { EventDispatcher } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Messaging.jsm"
|
|
|
|
);
|
2014-08-14 08:38:37 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
|
|
this,
|
|
|
|
"uuidgen",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator"
|
|
|
|
);
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["PageActions"];
|
2014-08-14 08:38:37 +04:00
|
|
|
|
|
|
|
// Copied from browser.js
|
|
|
|
// TODO: We should move this method to a common importable location
|
|
|
|
function resolveGeckoURI(aURI) {
|
|
|
|
if (!aURI) {
|
2019-03-26 11:29:37 +03:00
|
|
|
throw new Error("Can't resolve an empty uri");
|
2019-07-05 11:53:35 +03:00
|
|
|
}
|
2014-08-14 08:38:37 +04:00
|
|
|
|
|
|
|
if (aURI.startsWith("chrome://")) {
|
2017-08-01 18:43:56 +03:00
|
|
|
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;
|
2014-08-14 08:38:37 +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));
|
2014-08-14 08:38:37 +04:00
|
|
|
}
|
|
|
|
return aURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
var PageActions = {
|
|
|
|
_items: {},
|
|
|
|
|
2017-05-11 21:41:33 +03:00
|
|
|
_initialized: false,
|
2014-08-14 08:38:37 +04:00
|
|
|
|
2017-05-11 21:41:33 +03:00
|
|
|
_maybeInitialize: function() {
|
|
|
|
if (!this._initialized && Object.keys(this._items).length) {
|
|
|
|
this._initialized = true;
|
2017-01-26 02:57:31 +03:00
|
|
|
EventDispatcher.instance.registerListener(this, [
|
|
|
|
"PageActions:Clicked",
|
|
|
|
"PageActions:LongClicked",
|
|
|
|
]);
|
2014-08-14 08:38:37 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-11 21:41:33 +03:00
|
|
|
_maybeUninitialize: function() {
|
|
|
|
if (this._initialized && !Object.keys(this._items).length) {
|
|
|
|
this._initialized = false;
|
2017-01-26 02:57:31 +03:00
|
|
|
EventDispatcher.instance.unregisterListener(this, [
|
|
|
|
"PageActions:Clicked",
|
|
|
|
"PageActions:LongClicked",
|
|
|
|
]);
|
2014-08-14 08:38:37 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-01-26 02:57:31 +03:00
|
|
|
onEvent: function(event, data, callback) {
|
|
|
|
let item = this._items[data.id];
|
|
|
|
if (event == "PageActions:Clicked") {
|
2016-04-28 02:06:24 +03:00
|
|
|
if (item.clickCallback) {
|
|
|
|
item.clickCallback();
|
2014-08-14 08:38:37 +04:00
|
|
|
}
|
2017-01-26 02:57:31 +03:00
|
|
|
} else if (event == "PageActions:LongClicked") {
|
2016-04-28 02:06:24 +03:00
|
|
|
if (item.longClickCallback) {
|
|
|
|
item.longClickCallback();
|
2014-08-14 08:38:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-28 02:06:24 +03:00
|
|
|
isShown: function(id) {
|
|
|
|
return !!this._items[id];
|
|
|
|
},
|
|
|
|
|
2016-05-14 04:36:06 +03:00
|
|
|
synthesizeClick: function(id) {
|
|
|
|
let item = this._items[id];
|
|
|
|
if (item && item.clickCallback) {
|
|
|
|
item.clickCallback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-14 08:38:37 +04:00
|
|
|
add: function(aOptions) {
|
2017-10-15 21:50:30 +03:00
|
|
|
let id = aOptions.id || uuidgen.generateUUID().toString();
|
2016-04-28 02:06:24 +03:00
|
|
|
|
2017-01-26 02:57:31 +03:00
|
|
|
EventDispatcher.instance.sendRequest({
|
2014-08-14 08:38:37 +04:00
|
|
|
type: "PageActions:Add",
|
|
|
|
id: id,
|
|
|
|
title: aOptions.title,
|
|
|
|
icon: resolveGeckoURI(aOptions.icon),
|
2017-08-25 11:24:18 +03:00
|
|
|
important: "important" in aOptions ? aOptions.important : false,
|
2018-10-19 15:55:39 +03:00
|
|
|
useTint: "useTint" in aOptions ? aOptions.useTint : false,
|
2014-08-14 08:38:37 +04:00
|
|
|
});
|
|
|
|
|
2016-04-28 02:06:24 +03:00
|
|
|
this._items[id] = {};
|
|
|
|
|
|
|
|
if (aOptions.clickCallback) {
|
|
|
|
this._items[id].clickCallback = aOptions.clickCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptions.longClickCallback) {
|
|
|
|
this._items[id].longClickCallback = aOptions.longClickCallback;
|
|
|
|
}
|
2014-08-14 08:38:37 +04:00
|
|
|
|
2017-05-11 21:41:33 +03:00
|
|
|
this._maybeInitialize();
|
2014-08-14 08:38:37 +04:00
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function(id) {
|
2017-01-26 02:57:31 +03:00
|
|
|
EventDispatcher.instance.sendRequest({
|
2014-08-14 08:38:37 +04:00
|
|
|
type: "PageActions:Remove",
|
2018-10-19 15:55:39 +03:00
|
|
|
id: id,
|
2014-08-14 08:38:37 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
delete this._items[id];
|
2017-05-11 21:41:33 +03:00
|
|
|
this._maybeUninitialize();
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|