Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 3d8f29b

Browse files
committed
Bug 871350 - Uplift Add-on SDK integration branch to Firefox
1 parent 3d2f7c2 commit 3d8f29b

16 files changed

Lines changed: 388 additions & 49 deletions

File tree

addon-sdk/source/doc/dev-guide-source/guides/content-scripts/index.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ content of web pages or be notified when the user clicks a link.
1010

1111
The SDK provides several core modules to support this:
1212

13+
**[context-menu](modules/sdk/context-menu.html)**<br>
14+
Add items to the browser's context menu.
15+
1316
**[panel](modules/sdk/panel.html)**<br>
1417
Create a dialog that can host web content.
1518

@@ -19,12 +22,12 @@ Retrieve a page and access its content, without displaying it to the user.
1922
**[page-mod](modules/sdk/page-mod.html)**<br>
2023
Execute scripts in the context of selected web pages.
2124

25+
**[tabs](modules/sdk/tabs.html)**<br>
26+
Manipulate the browser's tabs, including the web content displayed in the tab.
27+
2228
**[widget](modules/sdk/widget.html)**<br>
2329
Host an add-on's user interface, including web content.
2430

25-
**[context-menu](modules/sdk/context-menu.html)**<br>
26-
Add items to the browser's context menu.
27-
2831
Firefox is moving towards a model in which it uses separate
2932
processes to display the UI, handle web content, and execute add-ons. The main
3033
add-on code will run in the add-on process and will not have direct access to

addon-sdk/source/doc/dev-guide-source/guides/content-scripts/using-port.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ user-defined messages.
7979
### Accessing `port` in the Add-on Script ###
8080

8181
In the add-on code, the channel of communication between the add-on and a
82-
particular content script context is encapsulated by the `worker` object. Thus
82+
particular content script context is encapsulated by the
83+
[`worker`](modules/sdk/content/worker.html#Worker) object. Thus
8384
the `port` object for communicating with a content script is a property of the
8485
corresponding `worker` object.
8586

@@ -117,7 +118,8 @@ handle multiple pages, each with its own context in which the content scripts
117118
are executing, so it needs a separate channel (worker) for each page.
118119

119120
So `page-mod` does not integrate the worker API directly: instead, each time a
120-
content script is attached to a page, the worker associated with the page is
121+
content script is attached to a page, the
122+
[worker](modules/sdk/content/worker.html#Worker) associated with the page is
121123
supplied to the page-mod in its `onAttach` function. By supplying a target for
122124
this function in the page-mod's constructor you can register to receive
123125
messages from the content script, and take a reference to the worker so as to

addon-sdk/source/doc/dev-guide-source/guides/content-scripts/using-postmessage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function. Again, `panel` and `page` integrate `worker` directly:
3939
panel.postMessage(addonMessage);
4040

4141
However, for `page-mod` objects you need to listen to the `onAttach` event
42-
and use the worker supplied to that:
42+
and use the [worker](modules/sdk/content/worker.html#Worker) supplied to that:
4343

4444
var pageMod = require('sdk/page-mod').PageMod({
4545
include: ['*'],

addon-sdk/source/doc/dev-guide-source/tutorials/modifying-web-pages-tab.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ message from one side to the other, the sender calls `port.emit()` and
9999
the receiver listens using `port.on()`.
100100

101101
* In the content script, `port` is a property of the global `self` object.
102-
* In the add-on script, `tab-attach()` returns an object containing the
102+
* In the add-on script, `tab.attach()` returns a
103+
[worker](modules/sdk/content/worker.html#Worker) object containing the
103104
`port` property you use to send messages to the content script.
104105

105106
Let's rewrite the example above to pass a message from the add-on to

addon-sdk/source/doc/dev-guide-source/tutorials/modifying-web-pages-url.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ the receiver listens using `port.on()`.
146146

147147
* In the content script, `port` is a property of the global `self` object.
148148
* In the add-on script, you need to listen for the `onAttach` event to get
149-
passed an object that contains `port`.
149+
passed a [worker](modules/sdk/content/worker.html#Worker) object that
150+
contains `port`.
150151

151152
Let's rewrite the example above to pass a message from the add-on to
152153
the content script. The message will contain the new content to insert into

addon-sdk/source/doc/module-source/sdk/content/worker.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
This module is used in the internal implementation of SDK modules
88
which use
99
[content scripts to interact with web content](dev-guide/guides/content-scripts/index.html),
10-
such as the [`panel`](modules/sdk/panel.html) or [`page-mod`](modules/sdk/page-mod.html)
11-
modules.
10+
such as the [`tabs`](modules/sdk/tabs.html), [`panel`](modules/sdk/panel.html),
11+
or [`page-mod`](modules/sdk/page-mod.html) modules.
1212

1313
It exports the `Worker` trait, which enables content
1414
scripts and the add-on code to exchange messages using the
@@ -121,14 +121,27 @@ error that occurs in one of the content scripts.
121121

122122
@argument {Error}
123123
The event listener is passed a single argument which is an
124-
[Error](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error)
124+
[Error](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Error)
125125
object.
126126
</api>
127127

128128
<api name="detach">
129129
@event
130130
This event is emitted when the document associated with this worker is unloaded
131131
or the worker's `destroy()` method is called.
132+
133+
Note that you can't communicate with the content script in response to this
134+
event. If you try, you'll see this error:
135+
136+
<pre>Error: Couldn't find the worker to receive this message.
137+
The script may not be initialized yet, or may already have been unloaded</pre>
138+
139+
You can handle the `detach` event in the content script itself though:
140+
141+
// in content script
142+
self.on("detach", function() {
143+
window.close();
144+
});
132145
</api>
133146

134147
</api>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!-- This Source Code Form is subject to the terms of the Mozilla Public
2+
- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
4+
5+
The `places/favicon` module provides simple helper functions for working with favicons.
6+
7+
<api name="getFavicon">
8+
@function
9+
Takes an `object` that represents a page's URL and returns a promise
10+
that resolves with the favicon URL for that page. The `object` can
11+
be a URL `String` or a [`Tab`](modules/sdk/tabs.html#Tab). The platform service
12+
([mozIAsyncFavicons](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/mozIAsyncFavicons)) retrieves favicon data stored from previously visited sites, and as
13+
such, will only return favicon URLs for visited sites.
14+
15+
let { getFavicon } = require("sdk/places/favicon");
16+
17+
// String example
18+
getFavicon("http://mozilla.org").then(function (url) {
19+
console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico
20+
});
21+
22+
// Tab example
23+
require("sdk/tabs").open({
24+
url: "http://mozilla.org",
25+
onReady: function (tab) {
26+
getFavicon(tab).then(function (url) {
27+
console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico
28+
});
29+
}
30+
});
31+
32+
// An optional callback can be provided to handle
33+
// the promise's resolve and reject states
34+
getFavicon("http://mozilla.org", function (url) {
35+
console.log(url); // http://mozorg.cdn.mozilla.net/media/img/favicon.ico
36+
});
37+
38+
@param object {string, tab}
39+
A value that represents the URL of the page to get the favicon URL from.
40+
Can be a URL `String` or a [`Tab`](modules/sdk/tabs.html#Tab).
41+
42+
@param callback {function}
43+
An optional callback function that will be used in both resolve and
44+
reject cases.
45+
46+
@returns {promise}
47+
A promise that resolves with the favicon URL.
48+
</api>

addon-sdk/source/doc/module-source/sdk/tabs.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ property you can load a new page in the tab:
5555

5656
You can attach a [content script](dev-guide/guides/content-scripts/index.html)
5757
to the page hosted in a tab, and use that to access and manipulate the page's
58-
content:
58+
content (see the
59+
[Modifying the Page Hosted by a Tab](dev-guide/tutorials/modifying-web-pages-tab.html) tutorial):
5960

6061
var tabs = require("sdk/tabs");
6162

@@ -213,6 +214,12 @@ This property can be set to load a different URL in the tab.
213214
@property {string}
214215
The URL of the favicon for the page currently loaded in the tab.
215216
This property is read-only.
217+
218+
<div class="warning">
219+
This property is deprecated.
220+
From version 1.15, use the <a href="modules/sdk/places/favicon.html#getFavicon()">favicon module's <code>getFavicon()</code></a> function instead.
221+
</div>
222+
216223
</api>
217224

218225
<api name="contentType">
@@ -312,8 +319,10 @@ Returns thumbnail data URI of the page currently loaded in this tab.
312319
content script. Optional.
313320

314321
@returns {Worker}
322+
The [Worker](modules/sdk/content/worker.html#Worker) object can be used to
323+
communicate with the content script.
315324
See [Content Scripts guide](dev-guide/guides/content-scripts/index.html)
316-
to learn how to use the `Worker` object to communicate with the content script.
325+
to learn the details.
317326

318327
</api>
319328

addon-sdk/source/lib/sdk/io/data.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,18 @@ module.metadata = {
1010

1111
const { Cc, Ci, Cu } = require("chrome");
1212
const base64 = require("../base64");
13-
const { defer } = require("../core/promise");
14-
const { newURI } = require("../url/utils");
15-
1613
const IOService = Cc["@mozilla.org/network/io-service;1"].
1714
getService(Ci.nsIIOService);
1815

1916
const { deprecateFunction } = require('../util/deprecate');
2017
const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm");
2118
const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
2219
getService(Ci.nsIFaviconService);
23-
const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons);
2420

2521
const PNG_B64 = "data:image/png;base64,";
2622
const DEF_FAVICON_URI = "chrome://mozapps/skin/places/defaultFavicon.png";
2723
let DEF_FAVICON = null;
2824

29-
/**
30-
* Takes URI of the page and returns a promise that resolves
31-
* to the page's favicon URI.
32-
* @param {String} uri
33-
* @param {Function} (callback)
34-
* @returns {Promise}
35-
*/
36-
37-
exports.getFavicon = function getFavicon(uri, callback) {
38-
let pageURI = newURI(uri);
39-
let deferred = defer();
40-
AsyncFavicons.getFaviconURLForPage(pageURI, function (aURI) {
41-
if (aURI && aURI.spec)
42-
deferred.resolve(aURI.spec.toString());
43-
else
44-
deferred.reject(null);
45-
});
46-
if (callback) deferred.promise.then(callback, callback);
47-
return deferred.promise;
48-
};
49-
5025
/**
5126
* Takes URI of the page and returns associated favicon URI.
5227
* If page under passed uri has no favicon then base64 encoded data URI of
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
module.metadata = {
8+
"stability": "unstable"
9+
};
10+
11+
const { Cc, Ci, Cu } = require("chrome");
12+
const { defer, reject } = require("../core/promise");
13+
const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
14+
getService(Ci.nsIFaviconService);
15+
const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons);
16+
const { isValidURI } = require("../url");
17+
const { newURI, getURL } = require("../url/utils");
18+
19+
/**
20+
* Takes an object of several possible types and
21+
* returns a promise that resolves to the page's favicon URI.
22+
* @param {String|Tab} object
23+
* @param {Function} (callback)
24+
* @returns {Promise}
25+
*/
26+
27+
function getFavicon (object, callback) {
28+
let url = getURL(object);
29+
let deferred = defer();
30+
31+
if (url && isValidURI(url)) {
32+
AsyncFavicons.getFaviconURLForPage(newURI(url), function (aURI) {
33+
if (aURI && aURI.spec)
34+
deferred.resolve(aURI.spec.toString());
35+
else
36+
deferred.reject(null);
37+
});
38+
} else {
39+
deferred.reject(null);
40+
}
41+
42+
if (callback) deferred.promise.then(callback, callback);
43+
return deferred.promise;
44+
}
45+
exports.getFavicon = getFavicon;

0 commit comments

Comments
 (0)