Bug 1287010 - Use schema-generated extension, split ext-extension.js r=billm

- This was the last non-schema-generated API in content scripts.

MozReview-Commit-ID: FaIOCHoircf
This commit is contained in:
Rob Wu
2016-08-18 19:19:40 -07:00
parent 0167c8c6a2
commit f600b2cf99
7 changed files with 33 additions and 38 deletions

View File

@@ -179,7 +179,6 @@ var Management = new class extends SchemaAPIManager {
// |contentWindow| is the DOM window the content runs in.
// |uri| is the URI of the content (optional).
// |docShell| is the docshell the content runs in (optional).
// |incognito| is the content running in a private context (default: false).
ExtensionContext = class extends BaseContext {
constructor(extension, params) {
// TODO(robwu): This should be addon_child once all ext- files are split.
@@ -189,7 +188,6 @@ ExtensionContext = class extends BaseContext {
let {type, uri} = params;
this.type = type;
this.uri = uri || extension.baseURI;
this.incognito = params.incognito || false;
if (params.contentWindow) {
this.setContentWindow(params.contentWindow);
@@ -702,9 +700,8 @@ GlobalManager = {
let extension = this.extensionMap.get(id);
let uri = document.documentURIObject;
let incognito = PrivateBrowsingUtils.isContentWindowPrivate(contentWindow);
let context = new ExtensionContext(extension, {type, contentWindow, uri, docShell, incognito});
let context = new ExtensionContext(extension, {type, contentWindow, uri, docShell});
inject(context);
if (type == "background") {
this._initializeBackgroundPage(contentWindow);

View File

@@ -33,8 +33,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "MatchGlobs",
"resource://gre/modules/MatchPattern.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
"resource://gre/modules/MessageChannel.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PromiseUtils",
"resource://gre/modules/PromiseUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Schemas",
@@ -48,7 +46,6 @@ var {
BaseContext,
LocaleData,
Messenger,
injectAPI,
flushJarCache,
getInnerWindowID,
promiseDocumentReady,
@@ -90,25 +87,6 @@ var apiManager = new class extends SchemaAPIManager {
}
};
// This is the fairly simple API that we inject into content
// scripts.
var api = context => {
return {
extension: {
getURL: function(url) {
return context.extension.baseURI.resolve(url);
},
get lastError() {
return context.lastError;
},
inIncognitoContext: PrivateBrowsingUtils.isContentWindowPrivate(context.contentWindow),
},
};
};
// Represents a content script.
function Script(extension, options, deferred = PromiseUtils.defer()) {
this.extension = extension;
@@ -366,19 +344,15 @@ class ExtensionContext extends BaseContext {
// reason. However, we waive here anyway in case that changes.
Cu.waiveXrays(this.sandbox).chrome = this.chromeObj;
let incognito = PrivateBrowsingUtils.isContentWindowPrivate(this.contentWindow);
let localApis = {};
apiManager.generateAPIs(this, localApis);
this.childManager = new ChildAPIManager(this, mm, localApis, {
type: "content_script",
url,
incognito,
});
Schemas.inject(this.chromeObj, this.childManager);
injectAPI(api(this), this.chromeObj);
// This is an iframe with content script API enabled. (See Bug 1214658 for rationale)
if (isExtensionPage) {
Cu.waiveXrays(this.contentWindow).chrome = this.chromeObj;

View File

@@ -0,0 +1,25 @@
"use strict";
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
function extensionApiFactory(context) {
return {
extension: {
getURL(url) {
return context.extension.baseURI.resolve(url);
},
get lastError() {
return context.lastError;
},
get inIncognitoContext() {
return PrivateBrowsingUtils.isContentWindowPrivate(context.contentWindow);
},
},
};
}
extensions.registerSchemaAPI("extension", "addon_child", extensionApiFactory);
extensions.registerSchemaAPI("extension", "content_child", extensionApiFactory);

View File

@@ -4,10 +4,6 @@ extensions.registerSchemaAPI("extension", "addon_parent", context => {
let {extension} = context;
return {
extension: {
getURL: function(url) {
return extension.baseURI.resolve(url);
},
getViews: function(fetchProperties) {
let result = Cu.cloneInto([], context.cloneScope);
@@ -32,13 +28,10 @@ extensions.registerSchemaAPI("extension", "addon_parent", context => {
},
get lastError() {
// TODO(robwu): See comment about lastError in ext-runtime.js
return context.lastError;
},
get inIncognitoContext() {
return context.incognito;
},
isAllowedIncognitoAccess() {
return Promise.resolve(true);
},

View File

@@ -15,6 +15,7 @@ category webextension-scripts storage chrome://extensions/content/ext-storage.js
category webextension-scripts test chrome://extensions/content/ext-test.js
# scripts specific for content process.
category webextension-scripts-content extension chrome://extensions/content/ext-c-extension.js
category webextension-scripts-content i18n chrome://extensions/content/ext-i18n.js
category webextension-scripts-content runtime chrome://extensions/content/ext-c-runtime.js

View File

@@ -18,4 +18,5 @@ toolkit.jar:
content/extensions/ext-extension.js
content/extensions/ext-storage.js
content/extensions/ext-test.js
content/extensions/ext-c-extension.js
content/extensions/ext-c-runtime.js

View File

@@ -5,11 +5,13 @@
[
{
"namespace": "extension",
"restrictions": ["content"],
"description": "The <code>browser.extension</code> API has utilities that can be used by any extension page. It includes support for exchanging messages between an extension and its content scripts or between extensions, as described in detail in $(topic:messaging)[Message Passing].",
"properties": {
"lastError": {
"type": "object",
"optional": true,
"restrictions": ["content"],
"description": "Set for the lifetime of a callback if an ansychronous extension api has resulted in an error. If no error has occured lastError will be <var>undefined</var>.",
"properties": {
"message": { "type": "string", "description": "Description of the error that has taken place." }
@@ -21,6 +23,7 @@
"inIncognitoContext": {
"type": "boolean",
"optional": true,
"restrictions": ["content"],
"description": "True for content scripts running inside incognito tabs, and for extension pages running inside an incognito process. The latter only applies to extensions with 'split' incognito_behavior."
}
},
@@ -36,6 +39,7 @@
{
"name": "getURL",
"type": "function",
"restrictions": ["content"],
"description": "Converts a relative path within an extension install directory to a fully-qualified URL.",
"parameters": [
{