Bug 1350646: Part 4 - Remove most modules which depend on sdk/addon/*. r=Mossop

MozReview-Commit-ID: C2muN6tC23B

--HG--
extra : source : c68367bb0eb39090b8bb1925916dea24bdbfe6b0
This commit is contained in:
Kris Maglione 2017-08-02 14:04:30 -07:00
Родитель cc4c331174
Коммит 674ad0854d
24 изменённых файлов: 0 добавлений и 6776 удалений

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

@ -21,16 +21,6 @@ EXTRA_JS_MODULES.sdk.system += [
]
modules = [
'dev/debuggee.js',
'dev/frame-script.js',
'dev/panel.js',
'dev/panel/view.js',
'dev/ports.js',
'dev/theme.js',
'dev/theme/hooks.js',
'dev/toolbox.js',
'dev/utils.js',
'dev/volcan.js',
'diffpatcher/diff.js',
'diffpatcher/index.js',
'diffpatcher/patch.js',
@ -73,7 +63,6 @@ modules = [
'sdk/content/loader.js',
'sdk/content/mod.js',
'sdk/content/page-mod.js',
'sdk/content/page-worker.js',
'sdk/content/sandbox.js',
'sdk/content/sandbox/events.js',
'sdk/content/tab-events.js',
@ -106,7 +95,6 @@ modules = [
'sdk/event/dom.js',
'sdk/event/target.js',
'sdk/event/utils.js',
'sdk/frame/hidden-frame.js',
'sdk/frame/utils.js',
'sdk/fs/path.js',
'sdk/hotkeys.js',
@ -139,7 +127,6 @@ modules = [
'sdk/lang/functional/helpers.js',
'sdk/lang/type.js',
'sdk/lang/weak-set.js',
'sdk/loader/cuddlefish.js',
'sdk/loader/sandbox.js',
'sdk/messaging.js',
'sdk/model/core.js',
@ -149,21 +136,11 @@ modules = [
'sdk/output/system.js',
'sdk/page-mod.js',
'sdk/page-mod/match-pattern.js',
'sdk/page-worker.js',
'sdk/panel.js',
'sdk/panel/events.js',
'sdk/panel/utils.js',
'sdk/passwords.js',
'sdk/passwords/utils.js',
'sdk/places/bookmarks.js',
'sdk/places/contract.js',
'sdk/places/events.js',
'sdk/places/favicon.js',
'sdk/places/history.js',
'sdk/places/host/host-bookmarks.js',
'sdk/places/host/host-query.js',
'sdk/places/host/host-tags.js',
'sdk/places/utils.js',
'sdk/platform/xpcom.js',
'sdk/preferences/event-target.js',
'sdk/preferences/native-options.js',

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

@ -1,90 +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";
module.metadata = {
"stability": "experimental"
};
const { Cu } = require("chrome");
const { Class } = require("../sdk/core/heritage");
const { MessagePort, MessageChannel } = require("../sdk/messaging");
const { DevToolsShim } = Cu.import("chrome://devtools-shim/content/DevToolsShim.jsm", {});
const outputs = new WeakMap();
const inputs = new WeakMap();
const targets = new WeakMap();
const transports = new WeakMap();
const inputFor = port => inputs.get(port);
const outputFor = port => outputs.get(port);
const transportFor = port => transports.get(port);
const fromTarget = target => {
const debuggee = new Debuggee();
const { port1, port2 } = new MessageChannel();
inputs.set(debuggee, port1);
outputs.set(debuggee, port2);
targets.set(debuggee, target);
return debuggee;
};
exports.fromTarget = fromTarget;
const Debuggee = Class({
extends: MessagePort.prototype,
close: function() {
const server = transportFor(this);
if (server) {
transports.delete(this);
server.close();
}
outputFor(this).close();
},
start: function() {
const target = targets.get(this);
if (target.isLocalTab) {
// Since a remote protocol connection will be made, let's start the
// DebuggerServer here, once and for all tools.
let transport = DevToolsShim.connectDebuggerServer();
transports.set(this, transport);
}
// TODO: Implement support for remote connections (See Bug 980421)
else {
throw Error("Remote targets are not yet supported");
}
// pipe messages send to the debuggee to an actual
// server via remote debugging protocol transport.
inputFor(this).addEventListener("message", ({data}) =>
transportFor(this).send(data));
// pipe messages received from the remote debugging
// server transport onto the this debuggee.
transportFor(this).hooks = {
onPacket: packet => inputFor(this).postMessage(packet),
onClosed: () => inputFor(this).close()
};
inputFor(this).start();
outputFor(this).start();
},
postMessage: function(data) {
return outputFor(this).postMessage(data);
},
get onmessage() {
return outputFor(this).onmessage;
},
set onmessage(onmessage) {
outputFor(this).onmessage = onmessage;
},
addEventListener: function(...args) {
return outputFor(this).addEventListener(...args);
},
removeEventListener: function(...args) {
return outputFor(this).removeEventListener(...args);
}
});
exports.Debuggee = Debuggee;

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

@ -1,120 +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";
(function({content, sendSyncMessage, addMessageListener, sendAsyncMessage}) {
const Cc = Components.classes;
const Ci = Components.interfaces;
const observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
const channels = new Map();
const handles = new WeakMap();
// Takes remote port handle and creates a local one.
// also set's up a messaging channel between them.
// This is temporary workaround until Bug 914974 is fixed
// and port can be transfered through message manager.
const demarshal = (handle) => {
if (handle.type === "MessagePort") {
if (!channels.has(handle.id)) {
const channel = new content.MessageChannel();
channels.set(handle.id, channel);
handles.set(channel.port1, handle);
channel.port1.onmessage = onOutPort;
}
return channels.get(handle.id).port2;
}
return null;
};
const onOutPort = event => {
const handle = handles.get(event.target);
sendAsyncMessage("sdk/port/message", {
port: handle,
message: event.data
});
};
const onInPort = ({data}) => {
const channel = channels.get(data.port.id);
if (channel)
channel.port1.postMessage(data.message);
};
const onOutEvent = event =>
sendSyncMessage("sdk/event/" + event.type,
{ type: event.type,
data: event.data });
const onInMessage = (message) => {
const {type, data, origin, bubbles, cancelable, ports} = message.data;
const event = new content.MessageEvent(type, {
bubbles: bubbles,
cancelable: cancelable,
data: data,
origin: origin,
target: content,
source: content,
ports: ports.map(demarshal)
});
content.dispatchEvent(event);
};
const onReady = event => {
channels.clear();
};
addMessageListener("sdk/event/message", onInMessage);
addMessageListener("sdk/port/message", onInPort);
const observer = {
handleEvent: ({target, type}) => {
observer.observe(target, type);
},
observe: (document, topic, data) => {
// When frame associated with message manager is removed from document `docShell`
// is set to `null` but observer is still kept alive. At this point accesing
// `content.document` throws "can't access dead object" exceptions. In order to
// avoid leaking observer and logged errors observer is going to be removed when
// `docShell` is set to `null`.
if (!docShell) {
observerService.removeObserver(observer, topic);
}
else if (document === content.document) {
if (topic.endsWith("-document-interactive")) {
sendAsyncMessage("sdk/event/ready", {
type: "ready",
readyState: document.readyState,
uri: document.documentURI
});
}
if (topic.endsWith("-document-loaded")) {
sendAsyncMessage("sdk/event/load", {
type: "load",
readyState: document.readyState,
uri: document.documentURI
});
}
if (topic === "unload") {
channels.clear();
sendAsyncMessage("sdk/event/unload", {
type: "unload",
readyState: "uninitialized",
uri: document.documentURI
});
}
}
}
};
observerService.addObserver(observer, "content-document-interactive");
observerService.addObserver(observer, "content-document-loaded");
observerService.addObserver(observer, "chrome-document-interactive");
observerService.addObserver(observer, "chrome-document-loaded");
addEventListener("unload", observer, false);
})(this);

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

@ -1,259 +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";
module.metadata = {
"stability": "experimental"
};
const { Cu } = require("chrome");
const { Class } = require("../sdk/core/heritage");
const { curry } = require("../sdk/lang/functional");
const { EventTarget } = require("../sdk/event/target");
const { Disposable, setup, dispose } = require("../sdk/core/disposable");
const { emit, off, setListeners } = require("../sdk/event/core");
const { when } = require("../sdk/event/utils");
const { getFrameElement } = require("../sdk/window/utils");
const { contract, validate } = require("../sdk/util/contract");
const { data: { url: resolve }} = require("../sdk/self");
const { identify } = require("../sdk/ui/id");
const { isLocalURL, URL } = require("../sdk/url");
const { encode } = require("../sdk/base64");
const { marshal, demarshal } = require("./ports");
const { fromTarget } = require("./debuggee");
const { removed } = require("../sdk/dom/events");
const { id: addonID } = require("../sdk/self");
const { viewFor } = require("../sdk/view/core");
const { createView } = require("./panel/view");
const OUTER_FRAME_URI = module.uri.replace(/\.js$/, ".html");
const FRAME_SCRIPT = module.uri.replace("/panel.js", "/frame-script.js");
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const HTML_NS = "http://www.w3.org/1999/xhtml";
const makeID = name =>
("dev-panel-" + addonID + "-" + name).
split("/").join("-").
split(".").join("-").
split(" ").join("-").
replace(/[^A-Za-z0-9_\-]/g, "");
// Weak mapping between `Panel` instances and their frame's
// `nsIMessageManager`.
const managers = new WeakMap();
// Return `nsIMessageManager` for the given `Panel` instance.
const managerFor = x => managers.get(x);
// Weak mappinging between iframe's and their owner
// `Panel` instances.
const panels = new WeakMap();
const panelFor = frame => panels.get(frame);
// Weak mapping between panels and debugees they're targeting.
const debuggees = new WeakMap();
const debuggeeFor = panel => debuggees.get(panel);
const frames = new WeakMap();
const frameFor = panel => frames.get(panel);
const setAttributes = (node, attributes) => {
for (var key in attributes)
node.setAttribute(key, attributes[key]);
};
const onStateChange = ({target, data}) => {
const panel = panelFor(target);
panel.readyState = data.readyState;
emit(panel, data.type, { target: panel, type: data.type });
};
// port event listener on the message manager that demarshalls
// and forwards to the actual receiver. This is a workaround
// until Bug 914974 is fixed.
const onPortMessage = ({data, target}) => {
const port = demarshal(target, data.port);
if (port)
port.postMessage(data.message);
};
// When frame is removed from the toolbox destroy panel
// associated with it to release all the resources.
const onFrameRemove = frame => {
panelFor(frame).destroy();
};
const onFrameInited = frame => {
frame.style.visibility = "visible";
}
const inited = frame => new Promise(resolve => {
const { messageManager } = frame.frameLoader;
const listener = message => {
messageManager.removeMessageListener("sdk/event/ready", listener);
resolve(frame);
};
messageManager.addMessageListener("sdk/event/ready", listener);
});
const getTarget = ({target}) => target;
const Panel = Class({
extends: Disposable,
implements: [EventTarget],
get id() {
return makeID(this.name || this.label);
},
readyState: "uninitialized",
ready: function() {
const { readyState } = this;
const isReady = readyState === "complete" ||
readyState === "interactive";
return isReady ? Promise.resolve(this) :
when(this, "ready").then(getTarget);
},
loaded: function() {
const { readyState } = this;
const isLoaded = readyState === "complete";
return isLoaded ? Promise.resolve(this) :
when(this, "load").then(getTarget);
},
unloaded: function() {
const { readyState } = this;
const isUninitialized = readyState === "uninitialized";
return isUninitialized ? Promise.resolve(this) :
when(this, "unload").then(getTarget);
},
postMessage: function(data, ports=[]) {
const manager = managerFor(this);
manager.sendAsyncMessage("sdk/event/message", {
type: "message",
bubbles: false,
cancelable: false,
data: data,
origin: this.url,
ports: ports.map(marshal(manager))
});
}
});
exports.Panel = Panel;
validate.define(Panel, contract({
label: {
is: ["string"],
msg: "The `option.label` must be a provided"
},
tooltip: {
is: ["string", "undefined"],
msg: "The `option.tooltip` must be a string"
},
icon: {
is: ["string"],
map: x => x && resolve(x),
ok: x => isLocalURL(x),
msg: "The `options.icon` must be a valid local URI."
},
url: {
map: x => resolve(x.toString()),
is: ["string"],
ok: x => isLocalURL(x),
msg: "The `options.url` must be a valid local URI."
},
invertIconForLightTheme: {
is: ["boolean", "undefined"],
msg: "The `options.invertIconForLightTheme` must be a boolean."
},
invertIconForDarkTheme: {
is: ["boolean", "undefined"],
msg: "The `options.invertIconForDarkTheme` must be a boolean."
}
}));
setup.define(Panel, (panel, {window, toolbox, url}) => {
// Hack: Given that iframe created by devtools API is no good for us,
// we obtain original iframe and replace it with the one that has
// desired configuration.
const original = getFrameElement(window);
const container = original.parentNode;
original.remove();
const frame = createView(panel, container.ownerDocument);
// Following modifications are a temporary workaround until Bug 1049188
// is fixed.
// Enforce certain iframe customizations regardless of users request.
setAttributes(frame, {
"id": original.id,
"src": url,
"flex": 1,
"forceOwnRefreshDriver": "",
"tooltip": "aHTMLTooltip"
});
frame.style.visibility = "hidden";
frame.classList.add("toolbox-panel-iframe");
// Inject iframe into designated node until add-on author decides
// to inject it elsewhere instead.
if (!frame.parentNode)
container.appendChild(frame);
// associate view with a panel
frames.set(panel, frame);
// associate panel model with a frame view.
panels.set(frame, panel);
const debuggee = fromTarget(toolbox.target);
// associate debuggee with a panel.
debuggees.set(panel, debuggee);
// Setup listeners for the frame message manager.
const { messageManager } = frame.frameLoader;
messageManager.addMessageListener("sdk/event/ready", onStateChange);
messageManager.addMessageListener("sdk/event/load", onStateChange);
messageManager.addMessageListener("sdk/event/unload", onStateChange);
messageManager.addMessageListener("sdk/port/message", onPortMessage);
messageManager.loadFrameScript(FRAME_SCRIPT, false);
managers.set(panel, messageManager);
// destroy panel if frame is removed.
removed(frame).then(onFrameRemove);
// show frame when it is initialized.
inited(frame).then(onFrameInited);
// set listeners if there are ones defined on the prototype.
setListeners(panel, Object.getPrototypeOf(panel));
panel.setup({ debuggee: debuggee });
});
createView.define(Panel, (panel, document) => {
const frame = document.createElement("iframe");
setAttributes(frame, {
"sandbox": "allow-scripts",
// We end up using chrome iframe with forced message manager
// as fixing a swapFrameLoader seemed like a giant task (see
// Bug 1075490).
"type": "chrome",
"forcemessagemanager": true,
"transparent": true,
"seamless": "seamless",
});
return frame;
});
dispose.define(Panel, function(panel) {
debuggeeFor(panel).close();
debuggees.delete(panel);
managers.delete(panel);
frames.delete(panel);
panel.readyState = "destroyed";
panel.dispose();
});
viewFor.define(Panel, frameFor);

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

@ -1,14 +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";
module.metadata = {
"stability": "experimental"
};
const { method } = require("method/core");
const createView = method("dev/panel/view#createView");
exports.createView = createView;

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

@ -1,64 +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";
module.metadata = {
"stability": "experimental"
};
// This module provides `marshal` and `demarshal` functions
// that can be used to send MessagePort's over `nsIFrameMessageManager`
// until Bug 914974 is fixed.
const { add, iterator } = require("../sdk/lang/weak-set");
const { curry } = require("../sdk/lang/functional");
var id = 0;
const ports = new WeakMap();
// Takes `nsIFrameMessageManager` and `MessagePort` instances
// and returns a handle representing given `port`. Messages
// received on given `port` will be forwarded to a message
// manager under `sdk/port/message` and messages like:
// { port: { type: "MessagePort", id: 2}, data: data }
// Where id is an identifier associated with a given `port`
// and `data` is an `event.data` received on port.
const marshal = curry((manager, port) => {
if (!ports.has(port)) {
id = id + 1;
const handle = {type: "MessagePort", id: id};
// Bind id to the given port
ports.set(port, handle);
// Obtain a weak reference to a port.
add(exports, port);
port.onmessage = event => {
manager.sendAsyncMessage("sdk/port/message", {
port: handle,
message: event.data
});
};
return handle;
}
return ports.get(port);
});
exports.marshal = marshal;
// Takes `nsIFrameMessageManager` instance and a handle returned
// `marshal(manager, port)` returning a `port` that was passed
// to it. Note that `port` may be GC-ed in which case returned
// value will be `null`.
const demarshal = curry((manager, {type, id}) => {
if (type === "MessagePort") {
for (let port of iterator(exports)) {
if (id === ports.get(port).id)
return port;
}
}
return null;
});
exports.demarshal = demarshal;

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

@ -1,135 +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";
module.metadata = {
"stability": "experimental"
};
const { Class } = require("../sdk/core/heritage");
const { EventTarget } = require("../sdk/event/target");
const { Disposable, setup, dispose } = require("../sdk/core/disposable");
const { contract, validate } = require("../sdk/util/contract");
const { id: addonID } = require("../sdk/self");
const { onEnable, onDisable } = require("dev/theme/hooks");
const { isString, instanceOf, isFunction } = require("sdk/lang/type");
const { add } = require("sdk/util/array");
const { data } = require("../sdk/self");
const { isLocalURL } = require("../sdk/url");
const makeID = name =>
("dev-theme-" + addonID + (name ? "-" + name : "")).
split(/[ . /]/).join("-").
replace(/[^A-Za-z0-9_\-]/g, "");
const Theme = Class({
extends: Disposable,
implements: [EventTarget],
initialize: function(options) {
this.name = options.name;
this.label = options.label;
this.styles = options.styles;
// Event handlers
this.onEnable = options.onEnable;
this.onDisable = options.onDisable;
},
get id() {
return makeID(this.name || this.label);
},
setup: function() {
// Any initialization steps done at the registration time.
},
getStyles: function() {
if (!this.styles) {
return [];
}
if (isString(this.styles)) {
if (isLocalURL(this.styles)) {
return [data.url(this.styles)];
}
}
let result = [];
for (let style of this.styles) {
if (isString(style)) {
if (isLocalURL(style)) {
style = data.url(style);
}
add(result, style);
} else if (instanceOf(style, Theme)) {
result = result.concat(style.getStyles());
}
}
return result;
},
getClassList: function() {
let result = [];
for (let style of this.styles) {
if (instanceOf(style, Theme)) {
result = result.concat(style.getClassList());
}
}
if (this.name) {
add(result, this.name);
}
return result;
}
});
exports.Theme = Theme;
// Initialization & dispose
setup.define(Theme, (theme) => {
theme.classList = [];
theme.setup();
});
dispose.define(Theme, function(theme) {
theme.dispose();
});
// Validation
validate.define(Theme, contract({
label: {
is: ["string"],
msg: "The `option.label` must be a provided"
},
}));
// Support theme events: apply and unapply the theme.
onEnable.define(Theme, (theme, {window, oldTheme}) => {
if (isFunction(theme.onEnable)) {
theme.onEnable(window, oldTheme);
}
});
onDisable.define(Theme, (theme, {window, newTheme}) => {
if (isFunction(theme.onDisable)) {
theme.onDisable(window, newTheme);
}
});
// Support for built-in themes
const LightTheme = Theme({
name: "theme-light",
styles: "chrome://devtools/skin/light-theme.css",
});
const DarkTheme = Theme({
name: "theme-dark",
styles: "chrome://devtools/skin/dark-theme.css",
});
exports.LightTheme = LightTheme;
exports.DarkTheme = DarkTheme;

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

@ -1,17 +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";
module.metadata = {
"stability": "experimental"
};
const { method } = require("method/core");
const onEnable = method("dev/theme/hooks#onEnable");
const onDisable = method("dev/theme/hooks#onDisable");
exports.onEnable = onEnable;
exports.onDisable = onDisable;

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

@ -1,107 +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";
module.metadata = {
"stability": "experimental"
};
const { Cu, Cc, Ci } = require("chrome");
const { Class } = require("../sdk/core/heritage");
const { Disposable, setup } = require("../sdk/core/disposable");
const { contract, validate } = require("../sdk/util/contract");
const { each, pairs, values } = require("../sdk/util/sequence");
const { onEnable, onDisable } = require("../dev/theme/hooks");
const { DevToolsShim } = Cu.import("chrome://devtools-shim/content/DevToolsShim.jsm", {});
// This is temporary workaround to allow loading of the developer tools client - volcan
// into a toolbox panel, this hack won't be necessary as soon as devtools patch will be
// shipped in nightly, after which it can be removed. Bug 1038517
const registerSDKURI = () => {
const ioService = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);
const resourceHandler = ioService.getProtocolHandler("resource")
.QueryInterface(Ci.nsIResProtocolHandler);
const uri = module.uri.replace("dev/toolbox.js", "");
resourceHandler.setSubstitution("sdk", ioService.newURI(uri));
};
registerSDKURI();
const Tool = Class({
extends: Disposable,
setup: function(params={}) {
const { panels } = validate(this, params);
const { themes } = validate(this, params);
this.panels = panels;
this.themes = themes;
each(([key, Panel]) => {
const { url, label, tooltip, icon, invertIconForLightTheme,
invertIconForDarkTheme } = validate(Panel.prototype);
const { id } = Panel.prototype;
DevToolsShim.registerTool({
id: id,
url: "about:blank",
label: label,
tooltip: tooltip,
icon: icon,
invertIconForLightTheme: invertIconForLightTheme,
invertIconForDarkTheme: invertIconForDarkTheme,
isTargetSupported: target => target.isLocalTab,
build: (window, toolbox) => {
const panel = new Panel();
setup(panel, { window: window,
toolbox: toolbox,
url: url });
return panel.ready();
}
});
}, pairs(panels));
each(([key, theme]) => {
validate(theme);
setup(theme);
DevToolsShim.registerTheme({
id: theme.id,
label: theme.label,
stylesheets: theme.getStyles(),
classList: theme.getClassList(),
onApply: (window, oldTheme) => {
onEnable(theme, { window: window,
oldTheme: oldTheme });
},
onUnapply: (window, newTheme) => {
onDisable(theme, { window: window,
newTheme: newTheme });
}
});
}, pairs(themes));
},
dispose: function() {
each(Panel => DevToolsShim.unregisterTool(Panel.prototype.id),
values(this.panels));
each(Theme => DevToolsShim.unregisterTheme(Theme.prototype.id),
values(this.themes));
}
});
validate.define(Tool, contract({
panels: {
is: ["object", "undefined"]
},
themes: {
is: ["object", "undefined"]
}
}));
exports.Tool = Tool;

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

@ -1,39 +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 { Cu } = require("chrome");
const { DevToolsShim } = Cu.import("chrome://devtools-shim/content/DevToolsShim.jsm", {});
const { getActiveTab } = require("../sdk/tabs/utils");
const { getMostRecentBrowserWindow } = require("../sdk/window/utils");
const targetFor = target => {
target = target || getActiveTab(getMostRecentBrowserWindow());
return DevToolsShim.getTargetForTab(target);
};
const getId = id => ((id.prototype && id.prototype.id) || id.id || id);
const getCurrentPanel = toolbox => toolbox.getCurrentPanel();
exports.getCurrentPanel = getCurrentPanel;
const openToolbox = (id, tab) => {
id = getId(id);
return DevToolsShim.showToolbox(targetFor(tab), id);
};
exports.openToolbox = openToolbox;
const closeToolbox = tab => DevToolsShim.closeToolbox(targetFor(tab));
exports.closeToolbox = closeToolbox;
const getToolbox = tab => DevToolsShim.getToolbox(targetFor(tab));
exports.getToolbox = getToolbox;
const openToolboxPanel = (id, tab) => {
id = getId(id);
return DevToolsShim.showToolbox(targetFor(tab), id).then(getCurrentPanel);
};
exports.openToolboxPanel = openToolboxPanel;

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

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

@ -1,157 +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 { frames } = require("../remote/child");
const { Class } = require("../core/heritage");
const { Disposable } = require('../core/disposable');
lazyRequire(this, "../self", "data");
lazyRequire(this, "../dom/events", "once");
lazyRequire(this, "./utils", "getAttachEventType");
lazyRequire(this, '../util/rules', "Rules");
lazyRequire(this, '../util/uuid', "uuid");
lazyRequire(this, "./worker-child", "WorkerChild");
const { Cc, Ci, Cu } = require("chrome");
const { on: onSystemEvent } = require("../system/events");
const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this, 'appShell',
"@mozilla.org/appshell/appShellService;1",
"nsIAppShellService");
const pages = new Map();
const DOC_INSERTED = "document-element-inserted";
function isValidURL(page, url) {
return !page.rules || page.rules.matchesAny(url);
}
const ChildPage = Class({
implements: [ Disposable ],
setup: function(frame, id, options) {
this.id = id;
this.frame = frame;
this.options = options;
this.webNav = appShell.createWindowlessBrowser(false);
this.docShell.allowJavascript = this.options.allow.script;
// Accessing the browser's window forces the initial about:blank document to
// be created before we start listening for notifications
this.contentWindow;
this.webProgress.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
pages.set(this.id, this);
this.contentURL = options.contentURL;
if (options.include) {
this.rules = Rules();
this.rules.add.apply(this.rules, [].concat(options.include));
}
},
dispose: function() {
pages.delete(this.id);
this.webProgress.removeProgressListener(this);
this.webNav.close();
this.webNav = null;
},
attachWorker: function() {
if (!isValidURL(this, this.contentWindow.location.href))
return;
this.options.id = uuid().toString();
this.options.window = this.contentWindow;
this.frame.port.emit("sdk/frame/connect", this.id, {
id: this.options.id,
url: this.contentWindow.document.documentURIObject.spec
});
new WorkerChild(this.options);
},
get docShell() {
return this.webNav.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDocShell);
},
get webProgress() {
return this.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebProgress);
},
get contentWindow() {
return this.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
},
get contentURL() {
return this.options.contentURL;
},
set contentURL(url) {
this.options.contentURL = url;
url = this.options.contentURL ? data.url(this.options.contentURL) : "about:blank";
this.webNav.loadURI(url, Ci.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
},
onLocationChange: function(progress, request, location, flags) {
// Ignore inner-frame events
if (progress != this.webProgress)
return;
// Ignore events that don't change the document
if (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT)
return;
let event = getAttachEventType(this.options);
// Attaching at the start of the load is handled by the
// document-element-inserted listener.
if (event == DOC_INSERTED)
return;
once(this.contentWindow, event, () => {
this.attachWorker();
}, false);
},
QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener", "nsISupportsWeakReference"])
});
onSystemEvent(DOC_INSERTED, ({type, subject, data}) => {
let page = Array.from(pages.values()).find(p => p.contentWindow.document === subject);
if (!page)
return;
if (getAttachEventType(page.options) == DOC_INSERTED)
page.attachWorker();
}, true);
frames.port.on("sdk/frame/create", (frame, id, options) => {
new ChildPage(frame, id, options);
});
frames.port.on("sdk/frame/set", (frame, id, params) => {
let page = pages.get(id);
if (!page)
return;
if ("allowScript" in params)
page.docShell.allowJavascript = params.allowScript;
if ("contentURL" in params)
page.contentURL = params.contentURL;
});
frames.port.on("sdk/frame/destroy", (frame, id) => {
let page = pages.get(id);
if (!page)
return;
page.destroy();
});

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

@ -1,115 +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";
module.metadata = {
"stability": "experimental"
};
const { Cc, Ci } = require("chrome");
const { Class } = require("../core/heritage");
const { List, addListItem, removeListItem } = require("../util/list");
const { EventTarget } = require("../event/target");
lazyRequire(this, "../event/core", "emit");
lazyRequire(this, "./utils", { "create": "makeFrame" });
lazyRequire(this, "../core/promise", "defer");
const { when: unload } = require("../system/unload");
lazyRequire(this, "../deprecated/api-utils", "validateOptions", "getTypeOf");
lazyRequire(this, "../addon/window", "window");
lazyRequire(this, "../util/array", "fromIterator");
// This cache is used to access friend properties between functions
// without exposing them on the public API.
var cache = new Set();
var elements = new WeakMap();
function contentLoaded(target) {
var deferred = defer();
target.addEventListener("DOMContentLoaded", function DOMContentLoaded(event) {
// "DOMContentLoaded" events from nested frames propagate up to target,
// ignore events unless it's DOMContentLoaded for the given target.
if (event.target === target || event.target === target.contentDocument) {
target.removeEventListener("DOMContentLoaded", DOMContentLoaded);
deferred.resolve(target);
}
});
return deferred.promise;
}
function FrameOptions(options) {
options = options || {}
return validateOptions(options, FrameOptions.validator);
}
FrameOptions.validator = {
onReady: {
is: ["undefined", "function", "array"],
ok: function(v) {
if (getTypeOf(v) === "array") {
// make sure every item is a function
return v.every(item => typeof(item) === "function")
}
return true;
}
},
onUnload: {
is: ["undefined", "function"]
}
};
var HiddenFrame = Class({
extends: EventTarget,
initialize: function initialize(options) {
options = FrameOptions(options);
EventTarget.prototype.initialize.call(this, options);
},
get element() {
return elements.get(this);
},
toString: function toString() {
return "[object Frame]"
}
});
exports.HiddenFrame = HiddenFrame
function addHidenFrame(frame) {
if (!(frame instanceof HiddenFrame))
throw Error("The object to be added must be a HiddenFrame.");
// This instance was already added.
if (cache.has(frame)) return frame;
else cache.add(frame);
let element = makeFrame(window.document, {
nodeName: "iframe",
type: "content",
allowJavascript: true,
allowPlugins: true,
allowAuth: true,
});
elements.set(frame, element);
contentLoaded(element).then(function onFrameReady(element) {
emit(frame, "ready");
}, console.exception);
return frame;
}
exports.add = addHidenFrame
function removeHiddenFrame(frame) {
if (!(frame instanceof HiddenFrame))
throw Error("The object to be removed must be a HiddenFrame.");
if (!cache.has(frame)) return;
// Remove from cache before calling in order to avoid loop
cache.delete(frame);
emit(frame, "unload")
let element = frame.element
if (element) element.remove()
}
exports.remove = removeHiddenFrame;
unload(() => fromIterator(cache).forEach(removeHiddenFrame));

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

@ -1,102 +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";
module.metadata = {
"stability": "unstable"
};
// This module is manually loaded by bootstrap.js in a sandbox and immediatly
// put in module cache so that it is never loaded in any other way.
/* Workarounds to include dependencies in the manifest
require('chrome') // Otherwise CFX will complain about Components
require('toolkit/loader') // Otherwise CFX will stip out loader.js
require('sdk/addon/runner') // Otherwise CFX will stip out addon/runner.js
*/
const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components;
// `loadSandbox` is exposed by bootstrap.js
const loaderURI = module.uri.replace("sdk/loader/cuddlefish.js",
"toolkit/loader.js");
const xulappURI = module.uri.replace("loader/cuddlefish.js",
"system/xul-app.jsm");
// We need to keep a reference to the sandbox in order to unload it in
// bootstrap.js
var loaderSandbox = loadSandbox(loaderURI);
const loaderModule = loaderSandbox.exports;
const { incompatibility } = Cu.import(xulappURI, {}).XulApp;
const { override, load } = loaderModule;
function CuddlefishLoader(options) {
let { manifest } = options;
options = override(options, {
// Put `api-utils/loader` and `api-utils/cuddlefish` loaded as JSM to module
// cache to avoid subsequent loads via `require`.
modules: override({
'toolkit/loader': loaderModule,
'sdk/loader/cuddlefish': exports
}, options.modules),
resolve: function resolve(id, requirer) {
let entry = requirer && requirer in manifest && manifest[requirer];
let uri = null;
// If manifest entry for this requirement is present we follow manifest.
// Note: Standard library modules like 'panel' will be present in
// manifest unless they were moved to platform.
if (entry) {
let requirement = entry.requirements[id];
// If requirer entry is in manifest and it's requirement is not, than
// it has no authority to load since linker was not able to find it.
if (!requirement)
throw Error('Module: ' + requirer + ' has no authority to load: '
+ id, requirer);
uri = requirement;
} else {
// If requirer is off manifest than it's a system module and we allow it
// to go off manifest by resolving a relative path.
uri = loaderModule.resolve(id, requirer);
}
return uri;
},
load: function(loader, module) {
let result;
let error;
// In order to get the module's metadata, we need to load the module.
// if an exception is raised here, it could be that is due to application
// incompatibility. Therefore the exception is stored, and thrown again
// only if the module seems be compatible with the application currently
// running. Otherwise the incompatibility message takes the precedence.
try {
result = load(loader, module);
}
catch (e) {
error = e;
}
error = incompatibility(module) || error;
if (error)
throw error;
return result;
}
});
let loader = loaderModule.Loader(options);
// Hack to allow loading from `toolkit/loader`.
loader.modules[loaderURI] = loaderSandbox;
return loader;
}
exports = override(loaderModule, {
Loader: CuddlefishLoader
});

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

@ -1,194 +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";
module.metadata = {
"stability": "stable"
};
const { Class } = require('./core/heritage');
const { ns } = require('./core/namespace');
lazyRequire(this, './event/utils', "pipe", "stripListeners");
const { connect, destroy, WorkerHost } = require('./content/utils');
lazyRequire(this, './content/worker', "Worker");
const { Disposable } = require('./core/disposable');
const { EventTarget } = require('./event/target');
lazyRequire(this, './event/core', "setListeners");
lazyRequire(this, './addon/window', "window");
lazyRequire(this, './frame/utils', { "create": "makeFrame" }, "getDocShell");
const { contract } = require('./util/contract');
const { contract: loaderContract } = require('./content/loader');
lazyRequire(this, './util/rules', "Rules");
const { merge } = require('./util/object');
lazyRequire(this, './util/uuid', "uuid");
const { useRemoteProcesses, remoteRequire, frames } = require("./remote/parent");
remoteRequire("sdk/content/page-worker");
const workers = new WeakMap();
const pages = new Map();
const internal = ns();
let workerFor = (page) => workers.get(page);
let isDisposed = (page) => !pages.has(internal(page).id);
// The frame is used to ensure we have a remote process to load workers in
let remoteFrame = null;
let framePromise = null;
function getFrame() {
if (framePromise)
return framePromise;
framePromise = new Promise(resolve => {
let view = makeFrame(window.document, {
namespaceURI: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
nodeName: "iframe",
type: "content",
remote: useRemoteProcesses,
uri: "about:blank"
});
// Wait for the remote side to connect
let listener = (frame) => {
if (frame.frameElement != view)
return;
frames.off("attach", listener);
remoteFrame = frame;
resolve(frame);
}
frames.on("attach", listener);
});
return framePromise;
}
var pageContract = contract(merge({
allow: {
is: ['object', 'undefined', 'null'],
map: function (allow) { return { script: !allow || allow.script !== false }}
},
onMessage: {
is: ['function', 'undefined']
},
include: {
is: ['string', 'array', 'regexp', 'undefined']
},
contentScriptWhen: {
is: ['string', 'undefined'],
map: (when) => when || "end"
}
}, loaderContract.rules));
function enableScript (page) {
getDocShell(viewFor(page)).allowJavascript = true;
}
function disableScript (page) {
getDocShell(viewFor(page)).allowJavascript = false;
}
function Allow (page) {
return {
get script() {
return internal(page).options.allow.script;
},
set script(value) {
internal(page).options.allow.script = value;
if (isDisposed(page))
return;
remoteFrame.port.emit("sdk/frame/set", internal(page).id, { allowScript: value });
}
};
}
function isValidURL(page, url) {
return !page.rules || page.rules.matchesAny(url);
}
const Page = Class({
implements: [
EventTarget,
Disposable
],
extends: WorkerHost(workerFor),
setup: function Page(options) {
options = pageContract(options);
// Sanitize the options
if ("contentScriptOptions" in options)
options.contentScriptOptions = JSON.stringify(options.contentScriptOptions);
internal(this).id = uuid().toString();
internal(this).options = options;
for (let prop of ['contentScriptFile', 'contentScript', 'contentScriptWhen']) {
this[prop] = options[prop];
}
pages.set(internal(this).id, this);
// Set listeners on the {Page} object itself, not the underlying worker,
// like `onMessage`, as it gets piped
setListeners(this, options);
let worker = new Worker(stripListeners(options));
workers.set(this, worker);
pipe(worker, this);
if (options.include) {
this.rules = Rules();
this.rules.add.apply(this.rules, [].concat(options.include));
}
getFrame().then(frame => {
if (isDisposed(this))
return;
frame.port.emit("sdk/frame/create", internal(this).id, stripListeners(options));
});
},
get allow() { return Allow(this); },
set allow(value) {
if (isDisposed(this))
return;
this.allow.script = pageContract({ allow: value }).allow.script;
},
get contentURL() {
return internal(this).options.contentURL;
},
set contentURL(value) {
if (!isValidURL(this, value))
return;
internal(this).options.contentURL = value;
if (isDisposed(this))
return;
remoteFrame.port.emit("sdk/frame/set", internal(this).id, { contentURL: value });
},
dispose: function () {
if (isDisposed(this))
return;
pages.delete(internal(this).id);
let worker = workerFor(this);
if (worker)
destroy(worker);
remoteFrame.port.emit("sdk/frame/destroy", internal(this).id);
// Destroy the remote frame if all the pages have been destroyed
if (pages.size == 0) {
framePromise = null;
remoteFrame.frameElement.remove();
remoteFrame = null;
}
},
toString: function () { return '[object Page]' }
});
exports.Page = Page;
frames.port.on("sdk/frame/connect", (frame, id, params) => {
let page = pages.get(id);
if (!page)
return;
connect(workerFor(page), frame, params);
});

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

@ -1,395 +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";
module.metadata = {
"stability": "unstable",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
/*
* Requiring hosts so they can subscribe to client messages
*/
require('./host/host-bookmarks');
require('./host/host-tags');
require('./host/host-query');
const { Cc, Ci } = require('chrome');
const { Class } = require('../core/heritage');
const { send } = require('../addon/events');
const { defer, reject, all, resolve, promised } = require('../core/promise');
const { EventTarget } = require('../event/target');
const { emit } = require('../event/core');
const { identity, defer:async } = require('../lang/functional');
const { extend, merge } = require('../util/object');
const { fromIterator } = require('../util/array');
const {
constructTree, fetchItem, createQuery,
isRootGroup, createQueryOptions
} = require('./utils');
const {
bookmarkContract, groupContract, separatorContract
} = require('./contract');
const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Ci.nsINavBookmarksService);
/*
* Mapping of uncreated bookmarks with their created
* counterparts
*/
const itemMap = new WeakMap();
/*
* Constant used by nsIHistoryQuery; 1 is a bookmark query
* https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
*/
const BOOKMARK_QUERY = 1;
/*
* Bookmark Item classes
*/
const Bookmark = Class({
extends: [
bookmarkContract.properties(identity)
],
initialize: function initialize (options) {
merge(this, bookmarkContract(extend(defaults, options)));
},
type: 'bookmark',
toString: () => '[object Bookmark]'
});
exports.Bookmark = Bookmark;
const Group = Class({
extends: [
groupContract.properties(identity)
],
initialize: function initialize (options) {
// Don't validate if root group
if (isRootGroup(options))
merge(this, options);
else
merge(this, groupContract(extend(defaults, options)));
},
type: 'group',
toString: () => '[object Group]'
});
exports.Group = Group;
const Separator = Class({
extends: [
separatorContract.properties(identity)
],
initialize: function initialize (options) {
merge(this, separatorContract(extend(defaults, options)));
},
type: 'separator',
toString: () => '[object Separator]'
});
exports.Separator = Separator;
/*
* Functions
*/
function save (items, options) {
items = [].concat(items);
options = options || {};
let emitter = EventTarget();
let results = [];
let errors = [];
let root = constructTree(items);
let cache = new Map();
let isExplicitSave = item => !!~items.indexOf(item);
// `walk` returns an aggregate promise indicating the completion
// of the `commitItem` on each node, not whether or not that
// commit was successful
// Force this to be async, as if a ducktype fails validation,
// the promise implementation will fire an error event, which will
// not trigger the handler as it's not yet bound
//
// Can remove after `Promise.jsm` is implemented in Bug 881047,
// which will guarantee next tick execution
async(() => root.walk(preCommitItem).then(commitComplete))();
function preCommitItem ({value:item}) {
// Do nothing if tree root, default group (unsavable),
// or if it's a dependency and not explicitly saved (in the list
// of items to be saved), and not needed to be saved
if (item === null || // node is the tree root
isRootGroup(item) ||
(getId(item) && !isExplicitSave(item)))
return;
return promised(validate)(item)
.then(() => commitItem(item, options))
.then(data => construct(data, cache))
.then(savedItem => {
// If item was just created, make a map between
// the creation object and created object,
// so we can reference the item that doesn't have an id
if (!getId(item))
saveId(item, savedItem.id);
// Emit both the processed item, and original item
// so a mapping can be understood in handler
emit(emitter, 'data', savedItem, item);
// Push to results iff item was explicitly saved
if (isExplicitSave(item))
results[items.indexOf(item)] = savedItem;
}, reason => {
// Force reason to be a string for consistency
reason = reason + '';
// Emit both the reason, and original item
// so a mapping can be understood in handler
emit(emitter, 'error', reason + '', item);
// Store unsaved item in results list
results[items.indexOf(item)] = item;
errors.push(reason);
});
}
// Called when traversal of the node tree is completed and all
// items have been committed
function commitComplete () {
emit(emitter, 'end', results);
}
return emitter;
}
exports.save = save;
function search (queries, options) {
queries = [].concat(queries);
let emitter = EventTarget();
let cache = new Map();
let queryObjs = queries.map(createQuery.bind(null, BOOKMARK_QUERY));
let optionsObj = createQueryOptions(BOOKMARK_QUERY, options);
// Can remove after `Promise.jsm` is implemented in Bug 881047,
// which will guarantee next tick execution
async(() => {
send('sdk-places-query', { queries: queryObjs, options: optionsObj })
.then(handleQueryResponse);
})();
function handleQueryResponse (data) {
let deferreds = data.map(item => {
return construct(item, cache).then(bookmark => {
emit(emitter, 'data', bookmark);
return bookmark;
}, reason => {
emit(emitter, 'error', reason);
errors.push(reason);
});
});
all(deferreds).then(data => {
emit(emitter, 'end', data);
}, () => emit(emitter, 'end', []));
}
return emitter;
}
exports.search = search;
function remove (items) {
return [].concat(items).map(item => {
item.remove = true;
return item;
});
}
exports.remove = remove;
/*
* Internal Utilities
*/
function commitItem (item, options) {
// Get the item's ID, or getId it's saved version if it exists
let id = getId(item);
let data = normalize(item);
let promise;
data.id = id;
if (!id) {
promise = send('sdk-places-bookmarks-create', data);
} else if (item.remove) {
promise = send('sdk-places-bookmarks-remove', { id: id });
} else {
promise = send('sdk-places-bookmarks-last-updated', {
id: id
}).then(function (updated) {
// If attempting to save an item that is not the
// latest snapshot of a bookmark item, execute
// the resolution function
if (updated !== item.updated && options.resolve)
return fetchItem(id)
.then(options.resolve.bind(null, data));
else
return data;
}).then(send.bind(null, 'sdk-places-bookmarks-save'));
}
return promise;
}
/*
* Turns a bookmark item into a plain object,
* converts `tags` from Set to Array, group instance to an id
*/
function normalize (item) {
let data = merge({}, item);
// Circumvent prototype property of `type`
delete data.type;
data.type = item.type;
data.tags = [];
if (item.tags) {
data.tags = fromIterator(item.tags);
}
data.group = getId(data.group) || exports.UNSORTED.id;
return data;
}
/*
* Takes a data object and constructs a BookmarkItem instance
* of it, recursively generating parent instances as well.
*
* Pass in a `cache` Map to reuse instances of
* bookmark items to reduce overhead;
* The cache object is a map of id to a deferred with a
* promise that resolves to the bookmark item.
*/
function construct (object, cache, forced) {
let item = instantiate(object);
let deferred = defer();
// Item could not be instantiated
if (!item)
return resolve(null);
// Return promise for item if found in the cache,
// and not `forced`. `forced` indicates that this is the construct
// call that should not read from cache, but should actually perform
// the construction, as it was set before several async calls
if (cache.has(item.id) && !forced)
return cache.get(item.id).promise;
else if (cache.has(item.id))
deferred = cache.get(item.id);
else
cache.set(item.id, deferred);
// When parent group is found in cache, use
// the same deferred value
if (item.group && cache.has(item.group)) {
cache.get(item.group).promise.then(group => {
item.group = group;
deferred.resolve(item);
});
// If not in the cache, and a root group, return
// the premade instance
} else if (rootGroups.get(item.group)) {
item.group = rootGroups.get(item.group);
deferred.resolve(item);
// If not in the cache or a root group, fetch the parent
} else {
cache.set(item.group, defer());
fetchItem(item.group).then(group => {
return construct(group, cache, true);
}).then(group => {
item.group = group;
deferred.resolve(item);
}, deferred.reject);
}
return deferred.promise;
}
function instantiate (object) {
if (object.type === 'bookmark')
return Bookmark(object);
if (object.type === 'group')
return Group(object);
if (object.type === 'separator')
return Separator(object);
return null;
}
/**
* Validates a bookmark item; will throw an error if ininvalid,
* to be used with `promised`. As bookmark items check on their class,
* this only checks ducktypes
*/
function validate (object) {
if (!isDuckType(object)) return true;
let contract = object.type === 'bookmark' ? bookmarkContract :
object.type === 'group' ? groupContract :
object.type === 'separator' ? separatorContract :
null;
if (!contract) {
throw Error('No type specified');
}
// If object has a property set, and undefined,
// manually override with default as it'll fail otherwise
let withDefaults = Object.keys(defaults).reduce((obj, prop) => {
if (obj[prop] == null) obj[prop] = defaults[prop];
return obj;
}, extend(object));
contract(withDefaults);
}
function isDuckType (item) {
return !(item instanceof Bookmark) &&
!(item instanceof Group) &&
!(item instanceof Separator);
}
function saveId (unsaved, id) {
itemMap.set(unsaved, id);
}
// Fetches an item's ID from itself, or from the mapped items
function getId (item) {
return typeof item === 'number' ? item :
item ? item.id || itemMap.get(item) :
null;
}
/*
* Set up the default, root groups
*/
var defaultGroupMap = {
MENU: bmsrv.bookmarksMenuFolder,
TOOLBAR: bmsrv.toolbarFolder,
UNSORTED: bmsrv.unfiledBookmarksFolder
};
var rootGroups = new Map();
for (let i in defaultGroupMap) {
let group = Object.freeze(Group({ title: i, id: defaultGroupMap[i] }));
rootGroups.set(defaultGroupMap[i], group);
exports[i] = group;
}
var defaults = {
group: exports.UNSORTED,
index: -1
};

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

@ -1,73 +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";
module.metadata = {
"stability": "unstable"
};
const { Cc, Ci } = require('chrome');
const { isValidURI, URL } = require('../url');
const { contract } = require('../util/contract');
const { extend } = require('../util/object');
// map of property validations
const validItem = {
id: {
is: ['number', 'undefined', 'null'],
},
group: {
is: ['object', 'number', 'undefined', 'null'],
ok: function (value) {
return value &&
(value.toString && value.toString() === '[object Group]') ||
typeof value === 'number' ||
value.type === 'group';
},
msg: 'The `group` property must be a valid Group object'
},
index: {
is: ['undefined', 'null', 'number'],
map: value => value == null ? -1 : value,
msg: 'The `index` property must be a number.'
},
updated: {
is: ['number', 'undefined']
}
};
const validTitle = {
title: {
is: ['string'],
msg: 'The `title` property must be defined.'
}
};
const validURL = {
url: {
is: ['string'],
ok: isValidURI,
msg: 'The `url` property must be a valid URL.'
}
};
const validTags = {
tags: {
is: ['object'],
ok: tags => tags instanceof Set,
map: function (tags) {
if (Array.isArray(tags))
return new Set(tags);
if (tags == null)
return new Set();
return tags;
},
msg: 'The `tags` property must be a Set, or an array'
}
};
exports.bookmarkContract = contract(
extend(validItem, validTitle, validURL, validTags));
exports.separatorContract = contract(validItem);
exports.groupContract = contract(extend(validItem, validTitle));

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

@ -1,142 +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';
module.metadata = {
'stability': 'experimental',
'engines': {
'Firefox': '*',
"SeaMonkey": '*'
}
};
const { Cc, Ci } = require('chrome');
const { Unknown } = require('../platform/xpcom');
const { Class } = require('../core/heritage');
const { merge } = require('../util/object');
const bookmarkService = Cc['@mozilla.org/browser/nav-bookmarks-service;1']
.getService(Ci.nsINavBookmarksService);
const historyService = Cc['@mozilla.org/browser/nav-history-service;1']
.getService(Ci.nsINavHistoryService);
const { mapBookmarkItemType } = require('./utils');
const { EventTarget } = require('../event/target');
const { emit } = require('../event/core');
const { when } = require('../system/unload');
const emitter = EventTarget();
var HISTORY_ARGS = {
onBeginUpdateBatch: [],
onEndUpdateBatch: [],
onClearHistory: [],
onDeleteURI: ['url'],
onDeleteVisits: ['url', 'visitTime'],
onPageChanged: ['url', 'property', 'value'],
onTitleChanged: ['url', 'title'],
onVisit: [
'url', 'visitId', 'time', 'sessionId', 'referringId', 'transitionType'
]
};
var HISTORY_EVENTS = {
onBeginUpdateBatch: 'history-start-batch',
onEndUpdateBatch: 'history-end-batch',
onClearHistory: 'history-start-clear',
onDeleteURI: 'history-delete-url',
onDeleteVisits: 'history-delete-visits',
onPageChanged: 'history-page-changed',
onTitleChanged: 'history-title-changed',
onVisit: 'history-visit'
};
var BOOKMARK_ARGS = {
onItemAdded: [
'id', 'parentId', 'index', 'type', 'url', 'title', 'dateAdded'
],
onItemChanged: [
'id', 'property', null, 'value', 'lastModified', 'type', 'parentId'
],
onItemMoved: [
'id', 'previousParentId', 'previousIndex', 'currentParentId',
'currentIndex', 'type'
],
onItemRemoved: ['id', 'parentId', 'index', 'type', 'url'],
onItemVisited: ['id', 'visitId', 'time', 'transitionType', 'url', 'parentId']
};
var BOOKMARK_EVENTS = {
onItemAdded: 'bookmark-item-added',
onItemChanged: 'bookmark-item-changed',
onItemMoved: 'bookmark-item-moved',
onItemRemoved: 'bookmark-item-removed',
onItemVisited: 'bookmark-item-visited',
};
function createHandler (type, propNames) {
propNames = propNames || [];
return function (...args) {
let data = propNames.reduce((acc, prop, i) => {
if (prop)
acc[prop] = formatValue(prop, args[i]);
return acc;
}, {});
emit(emitter, 'data', {
type: type,
data: data
});
};
}
/*
* Creates an observer, creating handlers based off of
* the `events` names, and ordering arguments from `propNames` hash
*/
function createObserverInstance (events, propNames) {
let definition = Object.keys(events).reduce((prototype, eventName) => {
prototype[eventName] = createHandler(events[eventName], propNames[eventName]);
return prototype;
}, {});
return Class(merge(definition, { extends: Unknown }))();
}
/*
* Formats `data` based off of the value of `type`
*/
function formatValue (type, data) {
if (type === 'type')
return mapBookmarkItemType(data);
if (type === 'url' && data)
return data.spec;
return data;
}
var historyObserver = createObserverInstance(HISTORY_EVENTS, HISTORY_ARGS);
// Hack alert: we sometimes need to send extra title-changed notifications
// ourselves for backwards compat. Replace the original onVisit callback to
// add on that logic:
historyObserver.realOnVisit = historyObserver.onVisit;
historyObserver.onVisit = function(url, visitId, time, sessionId,
referringId, transitionType, guid, hidden,
visitCount, typed, lastKnownTitle) {
// If this is the first visit we're adding, fire title-changed
// in case anyone cares.
if (visitCount == 1) {
historyObserver.onTitleChanged(url, lastKnownTitle);
}
this.realOnVisit(url, visitId, time, sessionId, referringId, transitionType);
};
historyService.addObserver(historyObserver);
var bookmarkObserver = createObserverInstance(BOOKMARK_EVENTS, BOOKMARK_ARGS);
bookmarkService.addObserver(bookmarkObserver);
when(() => {
historyService.removeObserver(historyObserver);
bookmarkService.removeObserver(bookmarkObserver);
});
exports.events = emitter;

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

@ -1,49 +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";
module.metadata = {
"stability": "unstable",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
const { Cc, Ci, Cu } = require("chrome");
const { defer, reject } = require("../core/promise");
const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
getService(Ci.nsIFaviconService);
const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons);
const { isValidURI } = require("../url");
const { newURI, getURL } = require("../url/utils");
/**
* Takes an object of several possible types and
* returns a promise that resolves to the page's favicon URI.
* @param {String|Tab} object
* @param {Function} (callback)
* @returns {Promise}
*/
function getFavicon (object, callback) {
let url = getURL(object);
let deferred = defer();
if (url && isValidURI(url)) {
AsyncFavicons.getFaviconURLForPage(newURI(url), function (aURI) {
if (aURI && aURI.spec)
deferred.resolve(aURI.spec.toString());
else
deferred.reject(null);
});
} else {
deferred.reject(null);
}
if (callback) deferred.promise.then(callback, callback);
return deferred.promise;
}
exports.getFavicon = getFavicon;

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

@ -1,65 +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";
module.metadata = {
"stability": "unstable",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
/*
* Requiring hosts so they can subscribe to client messages
*/
require('./host/host-bookmarks');
require('./host/host-tags');
require('./host/host-query');
const { Cc, Ci } = require('chrome');
const { Class } = require('../core/heritage');
const { events, send } = require('../addon/events');
const { defer, reject, all } = require('../core/promise');
const { uuid } = require('../util/uuid');
const { flatten } = require('../util/array');
const { has, extend, merge, pick } = require('../util/object');
const { emit } = require('../event/core');
const { defer: async } = require('../lang/functional');
const { EventTarget } = require('../event/target');
const {
urlQueryParser, createQuery, createQueryOptions
} = require('./utils');
/*
* Constant used by nsIHistoryQuery; 0 is a history query
* https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
*/
const HISTORY_QUERY = 0;
var search = function query (queries, options) {
queries = [].concat(queries);
let emitter = EventTarget();
let queryObjs = queries.map(createQuery.bind(null, HISTORY_QUERY));
let optionsObj = createQueryOptions(HISTORY_QUERY, options);
// Can remove after `Promise.jsm` is implemented in Bug 881047,
// which will guarantee next tick execution
async(() => {
send('sdk-places-query', {
query: queryObjs,
options: optionsObj
}).then(results => {
results.map(item => emit(emitter, 'data', item));
emit(emitter, 'end', results);
}, reason => {
emit(emitter, 'error', reason);
emit(emitter, 'end', []);
});
})();
return emitter;
};
exports.search = search;

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

@ -1,238 +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";
module.metadata = {
"stability": "experimental",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
const { Cc, Ci } = require('chrome');
const browserHistory = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsIBrowserHistory);
const asyncHistory = Cc["@mozilla.org/browser/history;1"].
getService(Ci.mozIAsyncHistory);
const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Ci.nsINavBookmarksService);
const taggingService = Cc["@mozilla.org/browser/tagging-service;1"].
getService(Ci.nsITaggingService);
const ios = Cc['@mozilla.org/network/io-service;1'].
getService(Ci.nsIIOService);
const { query } = require('./host-query');
const {
defer, all, resolve, promised, reject
} = require('../../core/promise');
const { request, response } = require('../../addon/host');
const { send } = require('../../addon/events');
const { on, emit } = require('../../event/core');
const { filter } = require('../../event/utils');
const { URL, isValidURI } = require('../../url');
const { newURI } = require('../../url/utils');
const DEFAULT_INDEX = bmsrv.DEFAULT_INDEX;
const UNSORTED_ID = bmsrv.unfiledBookmarksFolder;
const ROOT_FOLDERS = [
bmsrv.unfiledBookmarksFolder, bmsrv.toolbarFolder,
bmsrv.tagsFolder, bmsrv.bookmarksMenuFolder
];
const EVENT_MAP = {
'sdk-places-bookmarks-create': createBookmarkItem,
'sdk-places-bookmarks-save': saveBookmarkItem,
'sdk-places-bookmarks-last-updated': getBookmarkLastUpdated,
'sdk-places-bookmarks-get': getBookmarkItem,
'sdk-places-bookmarks-remove': removeBookmarkItem,
'sdk-places-bookmarks-get-all': getAllBookmarks,
'sdk-places-bookmarks-get-children': getChildren
};
function typeMap (type) {
if (typeof type === 'number') {
if (bmsrv.TYPE_BOOKMARK === type) return 'bookmark';
if (bmsrv.TYPE_FOLDER === type) return 'group';
if (bmsrv.TYPE_SEPARATOR === type) return 'separator';
} else {
if ('bookmark' === type) return bmsrv.TYPE_BOOKMARK;
if ('group' === type) return bmsrv.TYPE_FOLDER;
if ('separator' === type) return bmsrv.TYPE_SEPARATOR;
}
}
function getBookmarkLastUpdated ({id}) {
return resolve(bmsrv.getItemLastModified(id));
}
exports.getBookmarkLastUpdated;
function createBookmarkItem (data) {
let error;
if (data.group == null) data.group = UNSORTED_ID;
if (data.index == null) data.index = DEFAULT_INDEX;
if (data.type === 'group')
data.id = bmsrv.createFolder(
data.group, data.title, data.index
);
else if (data.type === 'separator')
data.id = bmsrv.insertSeparator(
data.group, data.index
);
else
data.id = bmsrv.insertBookmark(
data.group, newURI(data.url), data.index, data.title
);
// In the event where default or no index is provided (-1),
// query the actual index for the response
if (data.index === -1)
data.index = bmsrv.getItemIndex(data.id);
try {
data.updated = bmsrv.getItemLastModified(data.id);
}
catch (e) {
console.exception(e);
}
return tag(data, true).then(() => data);
}
exports.createBookmarkItem = createBookmarkItem;
function saveBookmarkItem (data) {
let id = data.id;
if (!id)
reject('Item is missing id');
let group = bmsrv.getFolderIdForItem(id);
let index = bmsrv.getItemIndex(id);
let type = bmsrv.getItemType(id);
let title = typeMap(type) !== 'separator' ?
bmsrv.getItemTitle(id) :
undefined;
let url = typeMap(type) === 'bookmark' ?
bmsrv.getBookmarkURI(id).spec :
undefined;
if (url != data.url)
bmsrv.changeBookmarkURI(id, newURI(data.url));
else if (typeMap(type) === 'bookmark')
data.url = url;
if (title != data.title)
bmsrv.setItemTitle(id, data.title);
else if (typeMap(type) !== 'separator')
data.title = title;
if (data.group && data.group !== group)
bmsrv.moveItem(id, data.group, data.index || -1);
else if (data.index != null && data.index !== index) {
// We use moveItem here instead of setItemIndex
// so we don't have to manage the indicies of the siblings
bmsrv.moveItem(id, group, data.index);
} else if (data.index == null)
data.index = index;
data.updated = bmsrv.getItemLastModified(data.id);
return tag(data).then(() => data);
}
exports.saveBookmarkItem = saveBookmarkItem;
function removeBookmarkItem (data) {
let id = data.id;
if (!id)
reject('Item is missing id');
bmsrv.removeItem(id);
return resolve(null);
}
exports.removeBookmarkItem = removeBookmarkItem;
function getBookmarkItem (data) {
let id = data.id;
if (!id)
reject('Item is missing id');
let type = bmsrv.getItemType(id);
data.type = typeMap(type);
if (type === bmsrv.TYPE_BOOKMARK || type === bmsrv.TYPE_FOLDER)
data.title = bmsrv.getItemTitle(id);
if (type === bmsrv.TYPE_BOOKMARK) {
data.url = bmsrv.getBookmarkURI(id).spec;
// Should be moved into host-tags as a method
data.tags = taggingService.getTagsForURI(newURI(data.url), {});
}
data.group = bmsrv.getFolderIdForItem(id);
data.index = bmsrv.getItemIndex(id);
data.updated = bmsrv.getItemLastModified(data.id);
return resolve(data);
}
exports.getBookmarkItem = getBookmarkItem;
function getAllBookmarks () {
return query({}, { queryType: 1 }).then(bookmarks =>
all(bookmarks.map(getBookmarkItem)));
}
exports.getAllBookmarks = getAllBookmarks;
function getChildren ({ id }) {
if (typeMap(bmsrv.getItemType(id)) !== 'group') return [];
let ids = [];
for (let i = 0; ids[ids.length - 1] !== -1; i++)
ids.push(bmsrv.getIdForItemAt(id, i));
ids.pop();
return all(ids.map(id => getBookmarkItem({ id: id })));
}
exports.getChildren = getChildren;
/*
* Hook into host
*/
var reqStream = filter(request, (data) => /sdk-places-bookmarks/.test(data.event));
on(reqStream, 'data', ({ event, id, data }) => {
if (!EVENT_MAP[event]) return;
let resData = { id: id, event: event };
promised(EVENT_MAP[event])(data).
then(res => resData.data = res, e => resData.error = e).
then(() => emit(response, 'data', resData));
});
function tag (data, isNew) {
// If a new item, we can skip checking what other tags
// are on the item
if (data.type !== 'bookmark') {
return resolve();
}
else if (!isNew) {
return send('sdk-places-tags-get-tags-by-url', { url: data.url })
.then(tags => {
return send('sdk-places-tags-untag', {
tags: tags.filter(tag => !~data.tags.indexOf(tag)),
url: data.url
});
}).then(() => send('sdk-places-tags-tag', {
url: data.url, tags: data.tags
}));
}
else if (data.tags && data.tags.length) {
return send('sdk-places-tags-tag', { url: data.url, tags: data.tags });
}
else
return resolve();
}

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

@ -1,179 +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";
module.metadata = {
"stability": "experimental",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
const { Cc, Ci } = require('chrome');
const { all } = require('../../core/promise');
const { safeMerge, omit } = require('../../util/object');
const historyService = Cc['@mozilla.org/browser/nav-history-service;1']
.getService(Ci.nsINavHistoryService);
const bookmarksService = Cc['@mozilla.org/browser/nav-bookmarks-service;1']
.getService(Ci.nsINavBookmarksService);
const { request, response } = require('../../addon/host');
const { newURI } = require('../../url/utils');
const { send } = require('../../addon/events');
const { on, emit } = require('../../event/core');
const { filter } = require('../../event/utils');
const ROOT_FOLDERS = [
bookmarksService.unfiledBookmarksFolder, bookmarksService.toolbarFolder,
bookmarksService.bookmarksMenuFolder
];
const EVENT_MAP = {
'sdk-places-query': queryReceiver
};
// Properties that need to be manually
// copied into a nsINavHistoryQuery object
const MANUAL_QUERY_PROPERTIES = [
'uri', 'folder', 'tags', 'url', 'folder'
];
const PLACES_PROPERTIES = [
'uri', 'title', 'accessCount', 'time'
];
function execute (queries, options) {
return new Promise(resolve => {
let root = historyService
.executeQueries(queries, queries.length, options).root;
// Let's extract an eventual uri wildcard, if both domain and uri are set.
// See utils.js::urlQueryParser() for more details.
// In case of multiple queries, we only retain the first found wildcard.
let uriWildcard = queries.reduce((prev, query) => {
if (query.uri && query.domain) {
if (!prev)
prev = query.uri.spec;
query.uri = null;
}
return prev;
}, "");
resolve(collect([], root, uriWildcard));
});
}
function collect (acc, node, uriWildcard) {
node.containerOpen = true;
for (let i = 0; i < node.childCount; i++) {
let child = node.getChild(i);
if (!uriWildcard || child.uri.startsWith(uriWildcard)) {
acc.push(child);
}
if (child.type === child.RESULT_TYPE_FOLDER) {
let container = child.QueryInterface(Ci.nsINavHistoryContainerResultNode);
collect(acc, container, uriWildcard);
}
}
node.containerOpen = false;
return acc;
}
function query (queries, options) {
return new Promise((resolve, reject) => {
queries = queries || [];
options = options || {};
let optionsObj, queryObjs;
optionsObj = historyService.getNewQueryOptions();
queryObjs = [].concat(queries).map(createQuery);
if (!queryObjs.length) {
queryObjs = [historyService.getNewQuery()];
}
safeMerge(optionsObj, options);
/*
* Currently `places:` queries are not supported
*/
optionsObj.excludeQueries = true;
execute(queryObjs, optionsObj).then((results) => {
if (optionsObj.queryType === 0) {
return results.map(normalize);
}
else if (optionsObj.queryType === 1) {
// Formats query results into more standard
// data structures for returning
return all(results.map(({itemId}) =>
send('sdk-places-bookmarks-get', { id: itemId })));
}
}).then(resolve, reject);
});
}
exports.query = query;
function createQuery (query) {
query = query || {};
let queryObj = historyService.getNewQuery();
safeMerge(queryObj, omit(query, MANUAL_QUERY_PROPERTIES));
if (query.tags && Array.isArray(query.tags))
queryObj.tags = query.tags;
if (query.uri || query.url)
queryObj.uri = newURI(query.uri || query.url);
if (query.folder)
queryObj.setFolders([query.folder], 1);
return queryObj;
}
function queryReceiver (message) {
let queries = message.data.queries || message.data.query;
let options = message.data.options;
let resData = {
id: message.id,
event: message.event
};
query(queries, options).then(results => {
resData.data = results;
respond(resData);
}, reason => {
resData.error = reason;
respond(resData);
});
}
/*
* Converts a nsINavHistoryResultNode into a plain object
*
* https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryResultNode
*/
function normalize (historyObj) {
return PLACES_PROPERTIES.reduce((obj, prop) => {
if (prop === 'uri')
obj.url = historyObj.uri;
else if (prop === 'time') {
// Cast from microseconds to milliseconds
obj.time = Math.floor(historyObj.time / 1000)
}
else if (prop === 'accessCount')
obj.visitCount = historyObj[prop];
else
obj[prop] = historyObj[prop];
return obj;
}, {});
}
/*
* Hook into host
*/
var reqStream = filter(request, data => /sdk-places-query/.test(data.event));
on(reqStream, 'data', function (e) {
if (EVENT_MAP[e.event]) EVENT_MAP[e.event](e);
});
function respond (data) {
emit(response, 'data', data);
}

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

@ -1,94 +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";
module.metadata = {
"stability": "experimental",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
const { Cc, Ci } = require('chrome');
const taggingService = Cc["@mozilla.org/browser/tagging-service;1"].
getService(Ci.nsITaggingService);
const ios = Cc['@mozilla.org/network/io-service;1'].
getService(Ci.nsIIOService);
const { URL } = require('../../url');
const { newURI } = require('../../url/utils');
const { request, response } = require('../../addon/host');
const { on, emit } = require('../../event/core');
const { filter } = require('../../event/utils');
const EVENT_MAP = {
'sdk-places-tags-tag': tag,
'sdk-places-tags-untag': untag,
'sdk-places-tags-get-tags-by-url': getTagsByURL,
'sdk-places-tags-get-urls-by-tag': getURLsByTag
};
function tag (message) {
let data = message.data;
let resData = {
id: message.id,
event: message.event
};
if (data.tags && data.tags.length > 0)
resData.data = taggingService.tagURI(newURI(data.url), data.tags);
respond(resData);
}
function untag (message) {
let data = message.data;
let resData = {
id: message.id,
event: message.event
};
if (!data.tags || data.tags.length > 0)
resData.data = taggingService.untagURI(newURI(data.url), data.tags);
respond(resData);
}
function getURLsByTag (message) {
let data = message.data;
let resData = {
id: message.id,
event: message.event
};
resData.data = taggingService
.getURIsForTag(data.tag).map(uri => uri.spec);
respond(resData);
}
function getTagsByURL (message) {
let data = message.data;
let resData = {
id: message.id,
event: message.event
};
resData.data = taggingService.getTagsForURI(newURI(data.url), {});
respond(resData);
}
/*
* Hook into host
*/
var reqStream = filter(request, function (data) {
return /sdk-places-tags/.test(data.event);
});
on(reqStream, 'data', function (e) {
if (EVENT_MAP[e.event]) EVENT_MAP[e.event](e);
});
function respond (data) {
emit(response, 'data', data);
}

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

@ -1,268 +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';
module.metadata = {
"stability": "experimental",
"engines": {
"Firefox": "*",
"SeaMonkey": "*"
}
};
const { Cc, Ci, Cu } = require('chrome');
const { Class } = require('../core/heritage');
const { method } = require('../lang/functional');
const { defer, promised, all } = require('../core/promise');
const { send } = require('../addon/events');
const { EventTarget } = require('../event/target');
const { merge } = require('../util/object');
const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Ci.nsINavBookmarksService);
Cu.importGlobalProperties(["URL"]);
/*
* TreeNodes are used to construct dependency trees
* for BookmarkItems
*/
var TreeNode = Class({
initialize: function (value) {
this.value = value;
this.children = [];
},
add: function (values) {
[].concat(values).forEach(value => {
this.children.push(value instanceof TreeNode ? value : TreeNode(value));
});
},
get length () {
let count = 0;
this.walk(() => count++);
// Do not count the current node
return --count;
},
get: method(get),
walk: method(walk),
toString: () => '[object TreeNode]'
});
exports.TreeNode = TreeNode;
/*
* Descends down from `node` applying `fn` to each in order.
* `fn` can return values or promises -- if promise returned,
* children are not processed until resolved. `fn` is passed
* one argument, the current node, `curr`.
*/
function walk (curr, fn) {
return promised(fn)(curr).then(val => {
return all(curr.children.map(child => walk(child, fn)));
});
}
/*
* Descends from the TreeNode `node`, returning
* the node with value `value` if found or `null`
* otherwise
*/
function get (node, value) {
if (node.value === value) return node;
for (let child of node.children) {
let found = get(child, value);
if (found) return found;
}
return null;
}
/*
* Constructs a tree of bookmark nodes
* returning the root (value: null);
*/
function constructTree (items) {
let root = TreeNode(null);
items.forEach(treeify.bind(null, root));
function treeify (root, item) {
// If node already exists, skip
let node = root.get(item);
if (node) return node;
node = TreeNode(item);
let parentNode = item.group ? treeify(root, item.group) : root;
parentNode.add(node);
return node;
}
return root;
}
exports.constructTree = constructTree;
/*
* Shortcut for converting an id, or an object with an id, into
* an object with corresponding bookmark data
*/
function fetchItem (item) {
return send('sdk-places-bookmarks-get', { id: item.id || item });
}
exports.fetchItem = fetchItem;
/*
* Takes an ID or an object with ID and checks it against
* the root bookmark folders
*/
function isRootGroup (id) {
id = id && id.id;
return ~[bmsrv.bookmarksMenuFolder, bmsrv.toolbarFolder,
bmsrv.unfiledBookmarksFolder
].indexOf(id);
}
exports.isRootGroup = isRootGroup;
/*
* Merges appropriate options into query based off of url
* 4 scenarios:
*
* 'moz.com' // domain: moz.com, domainIsHost: true
* --> 'http://moz.com', 'http://moz.com/thunderbird'
* '*.moz.com' // domain: moz.com, domainIsHost: false
* --> 'http://moz.com', 'http://moz.com/index', 'http://ff.moz.com/test'
* 'http://moz.com' // uri: http://moz.com/
* --> 'http://moz.com/'
* 'http://moz.com/*' // uri: http://moz.com/, domain: moz.com, domainIsHost: true
* --> 'http://moz.com/', 'http://moz.com/thunderbird'
*/
function urlQueryParser (query, url) {
if (!url) return;
if (/^https?:\/\//.test(url)) {
query.uri = url.charAt(url.length - 1) === '/' ? url : url + '/';
if (/\*$/.test(url)) {
// Wildcard searches on URIs are not supported, so try to extract a
// domain and filter the data later.
url = url.replace(/\*$/, '');
try {
query.domain = new URL(url).hostname;
query.domainIsHost = true;
// Unfortunately here we cannot use an expando to store the wildcard,
// cause the query is a wrapped native XPCOM object, so we reuse uri.
// We clearly don't want to query for both uri and domain, thus we'll
// have to handle this in host-query.js::execute()
query.uri = url;
} catch (ex) {
// Cannot extract an host cause it's not a valid uri, the query will
// just return nothing.
}
}
} else {
if (/^\*/.test(url)) {
query.domain = url.replace(/^\*\./, '');
query.domainIsHost = false;
} else {
query.domain = url;
query.domainIsHost = true;
}
}
}
exports.urlQueryParser = urlQueryParser;
/*
* Takes an EventEmitter and returns a promise that
* aggregates results and handles a bulk resolve and reject
*/
function promisedEmitter (emitter) {
let { promise, resolve, reject } = defer();
let errors = [];
emitter.on('error', error => errors.push(error));
emitter.on('end', (items) => {
if (errors.length) reject(errors[0]);
else resolve(items);
});
return promise;
}
exports.promisedEmitter = promisedEmitter;
// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
function createQuery (type, query) {
query = query || {};
let qObj = {
searchTerms: query.query
};
urlQueryParser(qObj, query.url);
// 0 === history
if (type === 0) {
// PRTime used by query is in microseconds, not milliseconds
qObj.beginTime = (query.from || 0) * 1000;
qObj.endTime = (query.to || new Date()) * 1000;
// Set reference time to Epoch
qObj.beginTimeReference = 0;
qObj.endTimeReference = 0;
}
// 1 === bookmarks
else if (type === 1) {
qObj.tags = query.tags;
qObj.folder = query.group && query.group.id;
}
// 2 === unified (not implemented on platform)
else if (type === 2) {
}
return qObj;
}
exports.createQuery = createQuery;
// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
const SORT_MAP = {
title: 1,
date: 3, // sort by visit date
url: 5,
visitCount: 7,
// keywords currently unsupported
// keyword: 9,
dateAdded: 11, // bookmarks only
lastModified: 13 // bookmarks only
};
function createQueryOptions (type, options) {
options = options || {};
let oObj = {};
oObj.sortingMode = SORT_MAP[options.sort] || 0;
if (options.descending && options.sort)
oObj.sortingMode++;
// Resolve to default sort if ineligible based on query type
if (type === 0 && // history
(options.sort === 'dateAdded' || options.sort === 'lastModified'))
oObj.sortingMode = 0;
oObj.maxResults = typeof options.count === 'number' ? options.count : 0;
oObj.queryType = type;
return oObj;
}
exports.createQueryOptions = createQueryOptions;
function mapBookmarkItemType (type) {
if (typeof type === 'number') {
if (bmsrv.TYPE_BOOKMARK === type) return 'bookmark';
if (bmsrv.TYPE_FOLDER === type) return 'group';
if (bmsrv.TYPE_SEPARATOR === type) return 'separator';
} else {
if ('bookmark' === type) return bmsrv.TYPE_BOOKMARK;
if ('group' === type) return bmsrv.TYPE_FOLDER;
if ('separator' === type) return bmsrv.TYPE_SEPARATOR;
}
}
exports.mapBookmarkItemType = mapBookmarkItemType;