summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions
diff options
context:
space:
mode:
Diffstat (limited to 'browser/components/extensions')
-rw-r--r--browser/components/extensions/.eslintrc.js22
-rw-r--r--browser/components/extensions/ext-bookmarks.js374
-rw-r--r--browser/components/extensions/ext-browserAction.js528
-rw-r--r--browser/components/extensions/ext-c-contextMenus.js158
-rw-r--r--browser/components/extensions/ext-c-omnibox.js32
-rw-r--r--browser/components/extensions/ext-c-tabs.js35
-rw-r--r--browser/components/extensions/ext-commands.js259
-rw-r--r--browser/components/extensions/ext-contextMenus.js537
-rw-r--r--browser/components/extensions/ext-desktop-runtime.js26
-rw-r--r--browser/components/extensions/ext-history.js246
-rw-r--r--browser/components/extensions/ext-omnibox.js104
-rw-r--r--browser/components/extensions/ext-pageAction.js287
-rw-r--r--browser/components/extensions/ext-sessions.js92
-rw-r--r--browser/components/extensions/ext-tabs.js1093
-rw-r--r--browser/components/extensions/ext-utils.js1243
-rw-r--r--browser/components/extensions/ext-windows.js231
-rw-r--r--browser/components/extensions/extension-mac-panel.css3
-rw-r--r--browser/components/extensions/extension-mac.css11
-rw-r--r--browser/components/extensions/extension-win-panel.css7
-rw-r--r--browser/components/extensions/extension.css572
-rw-r--r--browser/components/extensions/extension.svg19
-rw-r--r--browser/components/extensions/extensions-browser.manifest31
-rw-r--r--browser/components/extensions/jar.mn29
-rw-r--r--browser/components/extensions/moz.build17
-rw-r--r--browser/components/extensions/schemas/LICENSE27
-rw-r--r--browser/components/extensions/schemas/bookmarks.json568
-rw-r--r--browser/components/extensions/schemas/browser_action.json430
-rw-r--r--browser/components/extensions/schemas/commands.json148
-rw-r--r--browser/components/extensions/schemas/context_menus.json424
-rw-r--r--browser/components/extensions/schemas/context_menus_internal.json78
-rw-r--r--browser/components/extensions/schemas/history.json316
-rw-r--r--browser/components/extensions/schemas/jar.mn16
-rw-r--r--browser/components/extensions/schemas/moz.build7
-rw-r--r--browser/components/extensions/schemas/omnibox.json248
-rw-r--r--browser/components/extensions/schemas/page_action.json234
-rw-r--r--browser/components/extensions/schemas/sessions.json146
-rw-r--r--browser/components/extensions/schemas/tabs.json1295
-rw-r--r--browser/components/extensions/schemas/windows.json508
-rw-r--r--browser/components/extensions/test/browser/.eslintrc.js36
-rw-r--r--browser/components/extensions/test/browser/browser.ini115
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_context.js398
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_disabled.js68
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon.js321
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon_permissions.js210
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_popup.js413
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_popup_resize.js304
-rw-r--r--browser/components/extensions/test/browser/browser_ext_browserAction_simple.js59
-rw-r--r--browser/components/extensions/test/browser/browser_ext_commands_execute_browser_action.js113
-rw-r--r--browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js133
-rw-r--r--browser/components/extensions/test/browser/browser_ext_commands_getAll.js81
-rw-r--r--browser/components/extensions/test/browser/browser_ext_commands_onCommand.js229
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contentscript_connect.js67
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus.js342
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_checkboxes.js96
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js76
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_onclick.js196
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_radioGroups.js100
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_uninstall.js84
-rw-r--r--browser/components/extensions/test/browser/browser_ext_contextMenus_urlPatterns.js254
-rw-r--r--browser/components/extensions/test/browser/browser_ext_currentWindow.js149
-rw-r--r--browser/components/extensions/test/browser/browser_ext_getViews.js198
-rw-r--r--browser/components/extensions/test/browser/browser_ext_incognito_popup.js108
-rw-r--r--browser/components/extensions/test/browser/browser_ext_incognito_views.js121
-rw-r--r--browser/components/extensions/test/browser/browser_ext_lastError.js55
-rw-r--r--browser/components/extensions/test/browser/browser_ext_legacy_extension_context_contentscript.js173
-rw-r--r--browser/components/extensions/test/browser/browser_ext_omnibox.js286
-rw-r--r--browser/components/extensions/test/browser/browser_ext_optionsPage_privileges.js66
-rw-r--r--browser/components/extensions/test/browser/browser_ext_pageAction_context.js178
-rw-r--r--browser/components/extensions/test/browser/browser_ext_pageAction_popup.js238
-rw-r--r--browser/components/extensions/test/browser/browser_ext_pageAction_popup_resize.js169
-rw-r--r--browser/components/extensions/test/browser/browser_ext_pageAction_simple.js60
-rw-r--r--browser/components/extensions/test/browser/browser_ext_pageAction_title.js226
-rw-r--r--browser/components/extensions/test/browser/browser_ext_popup_api_injection.js101
-rw-r--r--browser/components/extensions/test/browser/browser_ext_popup_background.js133
-rw-r--r--browser/components/extensions/test/browser/browser_ext_popup_corners.js98
-rw-r--r--browser/components/extensions/test/browser/browser_ext_popup_sendMessage.js93
-rw-r--r--browser/components/extensions/test/browser/browser_ext_popup_shutdown.js77
-rw-r--r--browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage.js276
-rw-r--r--browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage_uninstall.js101
-rw-r--r--browser/components/extensions/test/browser/browser_ext_runtime_setUninstallURL.js94
-rw-r--r--browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed.js97
-rw-r--r--browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_private.js61
-rw-r--r--browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js96
-rw-r--r--browser/components/extensions/test/browser/browser_ext_sessions_restore.js134
-rw-r--r--browser/components/extensions/test/browser/browser_ext_simple.js57
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tab_runtimeConnect.js74
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_audio.js203
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_captureVisibleTab.js155
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_cookieStoreId.js156
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_create.js166
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_create_invalid_url.js66
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_detectLanguage.js47
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_duplicate.js146
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_events.js280
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js234
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_executeScript_bad.js217
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_executeScript_good.js189
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_executeScript_no_create.js67
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js107
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_getCurrent.js70
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_insertCSS.js86
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_move.js103
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_move_window.js98
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_move_window_multiple.js43
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_move_window_pinned.js42
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_onHighlighted.js126
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_onUpdated.js198
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_query.js224
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_reload.js54
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js58
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_removeCSS.js95
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js227
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_update.js45
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_update_url.js110
-rw-r--r--browser/components/extensions/test/browser/browser_ext_tabs_zoom.js222
-rw-r--r--browser/components/extensions/test/browser/browser_ext_topwindowid.js23
-rw-r--r--browser/components/extensions/test/browser/browser_ext_webNavigation_frameId0.js45
-rw-r--r--browser/components/extensions/test/browser/browser_ext_webNavigation_getFrames.js168
-rw-r--r--browser/components/extensions/test/browser/browser_ext_webNavigation_urlbar_transitions.js251
-rw-r--r--browser/components/extensions/test/browser/browser_ext_webRequest.js95
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows.js33
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_allowScriptsToClose.js61
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_create.js142
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_create_params.js33
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_create_tabId.js140
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_create_url.js84
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_events.js115
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_size.js114
-rw-r--r--browser/components/extensions/test/browser/browser_ext_windows_update.js189
-rw-r--r--browser/components/extensions/test/browser/context.html23
-rw-r--r--browser/components/extensions/test/browser/context_tabs_onUpdated_iframe.html19
-rw-r--r--browser/components/extensions/test/browser/context_tabs_onUpdated_page.html18
-rw-r--r--browser/components/extensions/test/browser/ctxmenu-image.pngbin0 -> 5401 bytes
-rw-r--r--browser/components/extensions/test/browser/file_bypass_cache.sjs11
-rw-r--r--browser/components/extensions/test/browser/file_dummy.html9
-rw-r--r--browser/components/extensions/test/browser/file_iframe_document.html10
-rw-r--r--browser/components/extensions/test/browser/file_iframe_document.sjs41
-rw-r--r--browser/components/extensions/test/browser/file_language_fr_en.html14
-rw-r--r--browser/components/extensions/test/browser/file_language_ja.html10
-rw-r--r--browser/components/extensions/test/browser/file_language_tlh.html12
-rw-r--r--browser/components/extensions/test/browser/file_popup_api_injection_a.html10
-rw-r--r--browser/components/extensions/test/browser/file_popup_api_injection_b.html10
-rw-r--r--browser/components/extensions/test/browser/head.js263
-rw-r--r--browser/components/extensions/test/browser/head_pageAction.js157
-rw-r--r--browser/components/extensions/test/browser/head_sessions.js47
-rw-r--r--browser/components/extensions/test/browser/searchSuggestionEngine.sjs9
-rw-r--r--browser/components/extensions/test/browser/searchSuggestionEngine.xml9
-rw-r--r--browser/components/extensions/test/mochitest/mochitest.ini6
-rw-r--r--browser/components/extensions/test/mochitest/test_ext_all_apis.html75
-rw-r--r--browser/components/extensions/test/xpcshell/.eslintrc.js9
-rw-r--r--browser/components/extensions/test/xpcshell/head.js55
-rw-r--r--browser/components/extensions/test/xpcshell/test_ext_bookmarks.js601
-rw-r--r--browser/components/extensions/test/xpcshell/test_ext_history.js487
-rw-r--r--browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js24
-rw-r--r--browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js61
-rw-r--r--browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js57
-rw-r--r--browser/components/extensions/test/xpcshell/xpcshell.ini11
157 files changed, 25000 insertions, 0 deletions
diff --git a/browser/components/extensions/.eslintrc.js b/browser/components/extensions/.eslintrc.js
new file mode 100644
index 000000000..81a11c4ac
--- /dev/null
+++ b/browser/components/extensions/.eslintrc.js
@@ -0,0 +1,22 @@
+"use strict";
+
+module.exports = { // eslint-disable-line no-undef
+ "extends": "../../../toolkit/components/extensions/.eslintrc.js",
+
+ "globals": {
+ "AllWindowEvents": true,
+ "browserActionFor": true,
+ "currentWindow": true,
+ "EventEmitter": true,
+ "getCookieStoreIdForTab": true,
+ "IconDetails": true,
+ "makeWidgetId": true,
+ "pageActionFor": true,
+ "PanelPopup": true,
+ "TabContext": true,
+ "ViewPopup": true,
+ "WindowEventManager": true,
+ "WindowListManager": true,
+ "WindowManager": true,
+ },
+};
diff --git a/browser/components/extensions/ext-bookmarks.js b/browser/components/extensions/ext-bookmarks.js
new file mode 100644
index 000000000..399f6212d
--- /dev/null
+++ b/browser/components/extensions/ext-bookmarks.js
@@ -0,0 +1,374 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+const {
+ SingletonEventManager,
+} = ExtensionUtils;
+
+XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
+ "resource://devtools/shared/event-emitter.js");
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+ "resource://gre/modules/PlacesUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Task",
+ "resource://gre/modules/Task.jsm");
+
+let listenerCount = 0;
+
+function getTree(rootGuid, onlyChildren) {
+ function convert(node, parent) {
+ let treenode = {
+ id: node.guid,
+ title: node.title || "",
+ index: node.index,
+ dateAdded: node.dateAdded / 1000,
+ };
+
+ if (parent && node.guid != PlacesUtils.bookmarks.rootGuid) {
+ treenode.parentId = parent.guid;
+ }
+
+ if (node.type == PlacesUtils.TYPE_X_MOZ_PLACE) {
+ // This isn't quite correct. Recently Bookmarked ends up here ...
+ treenode.url = node.uri;
+ } else {
+ treenode.dateGroupModified = node.lastModified / 1000;
+
+ if (node.children && !onlyChildren) {
+ treenode.children = node.children.map(child => convert(child, node));
+ }
+ }
+
+ return treenode;
+ }
+
+ return PlacesUtils.promiseBookmarksTree(rootGuid, {
+ excludeItemsCallback: item => {
+ if (item.type == PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR) {
+ return true;
+ }
+ return item.annos &&
+ item.annos.find(a => a.name == PlacesUtils.EXCLUDE_FROM_BACKUP_ANNO);
+ },
+ }).then(root => {
+ if (onlyChildren) {
+ let children = root.children || [];
+ return children.map(child => convert(child, root));
+ }
+ // It seems like the array always just contains the root node.
+ return [convert(root, null)];
+ }).catch(e => Promise.reject({message: e.message}));
+}
+
+function convert(result) {
+ let node = {
+ id: result.guid,
+ title: result.title || "",
+ index: result.index,
+ dateAdded: result.dateAdded.getTime(),
+ };
+
+ if (result.guid != PlacesUtils.bookmarks.rootGuid) {
+ node.parentId = result.parentGuid;
+ }
+
+ if (result.type == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
+ node.url = result.url.href; // Output is always URL object.
+ } else {
+ node.dateGroupModified = result.lastModified.getTime();
+ }
+
+ return node;
+}
+
+let observer = {
+ skipTags: true,
+ skipDescendantsOnItemRemoval: true,
+
+ onBeginUpdateBatch() {},
+ onEndUpdateBatch() {},
+
+ onItemAdded(id, parentId, index, itemType, uri, title, dateAdded, guid, parentGuid, source) {
+ if (itemType == PlacesUtils.bookmarks.TYPE_SEPARATOR) {
+ return;
+ }
+
+ let bookmark = {
+ id: guid,
+ parentId: parentGuid,
+ index,
+ title,
+ dateAdded: dateAdded / 1000,
+ };
+
+ if (itemType == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
+ bookmark.url = uri.spec;
+ } else {
+ bookmark.dateGroupModified = bookmark.dateAdded;
+ }
+
+ this.emit("created", bookmark);
+ },
+
+ onItemVisited() {},
+
+ onItemMoved(id, oldParentId, oldIndex, newParentId, newIndex, itemType, guid, oldParentGuid, newParentGuid, source) {
+ if (itemType == PlacesUtils.bookmarks.TYPE_SEPARATOR) {
+ return;
+ }
+
+ let info = {
+ parentId: newParentGuid,
+ index: newIndex,
+ oldParentId: oldParentGuid,
+ oldIndex,
+ };
+ this.emit("moved", {guid, info});
+ },
+
+ onItemRemoved(id, parentId, index, itemType, uri, guid, parentGuid, source) {
+ if (itemType == PlacesUtils.bookmarks.TYPE_SEPARATOR) {
+ return;
+ }
+
+ let node = {
+ id: guid,
+ parentId: parentGuid,
+ index,
+ };
+
+ if (itemType == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
+ node.url = uri.spec;
+ }
+
+ this.emit("removed", {guid, info: {parentId: parentGuid, index, node}});
+ },
+
+ onItemChanged(id, prop, isAnno, val, lastMod, itemType, parentId, guid, parentGuid, oldVal, source) {
+ if (itemType == PlacesUtils.bookmarks.TYPE_SEPARATOR) {
+ return;
+ }
+
+ let info = {};
+ if (prop == "title") {
+ info.title = val;
+ } else if (prop == "uri") {
+ info.url = val;
+ } else {
+ // Not defined yet.
+ return;
+ }
+
+ this.emit("changed", {guid, info});
+ },
+};
+EventEmitter.decorate(observer);
+
+function decrementListeners() {
+ listenerCount -= 1;
+ if (!listenerCount) {
+ PlacesUtils.bookmarks.removeObserver(observer);
+ }
+}
+
+function incrementListeners() {
+ listenerCount++;
+ if (listenerCount == 1) {
+ PlacesUtils.bookmarks.addObserver(observer, false);
+ }
+}
+
+extensions.registerSchemaAPI("bookmarks", "addon_parent", context => {
+ return {
+ bookmarks: {
+ get: function(idOrIdList) {
+ let list = Array.isArray(idOrIdList) ? idOrIdList : [idOrIdList];
+
+ return Task.spawn(function* () {
+ let bookmarks = [];
+ for (let id of list) {
+ let bookmark = yield PlacesUtils.bookmarks.fetch({guid: id});
+ if (!bookmark) {
+ throw new Error("Bookmark not found");
+ }
+ bookmarks.push(convert(bookmark));
+ }
+ return bookmarks;
+ }).catch(error => Promise.reject({message: error.message}));
+ },
+
+ getChildren: function(id) {
+ // TODO: We should optimize this.
+ return getTree(id, true);
+ },
+
+ getTree: function() {
+ return getTree(PlacesUtils.bookmarks.rootGuid, false);
+ },
+
+ getSubTree: function(id) {
+ return getTree(id, false);
+ },
+
+ search: function(query) {
+ return PlacesUtils.bookmarks.search(query).then(result => result.map(convert));
+ },
+
+ getRecent: function(numberOfItems) {
+ return PlacesUtils.bookmarks.getRecent(numberOfItems).then(result => result.map(convert));
+ },
+
+ create: function(bookmark) {
+ let info = {
+ title: bookmark.title || "",
+ };
+
+ // If url is NULL or missing, it will be a folder.
+ if (bookmark.url !== null) {
+ info.type = PlacesUtils.bookmarks.TYPE_BOOKMARK;
+ info.url = bookmark.url || "";
+ } else {
+ info.type = PlacesUtils.bookmarks.TYPE_FOLDER;
+ }
+
+ if (bookmark.index !== null) {
+ info.index = bookmark.index;
+ }
+
+ if (bookmark.parentId !== null) {
+ info.parentGuid = bookmark.parentId;
+ } else {
+ info.parentGuid = PlacesUtils.bookmarks.unfiledGuid;
+ }
+
+ try {
+ return PlacesUtils.bookmarks.insert(info).then(convert)
+ .catch(error => Promise.reject({message: error.message}));
+ } catch (e) {
+ return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
+ }
+ },
+
+ move: function(id, destination) {
+ let info = {
+ guid: id,
+ };
+
+ if (destination.parentId !== null) {
+ info.parentGuid = destination.parentId;
+ }
+ info.index = (destination.index === null) ?
+ PlacesUtils.bookmarks.DEFAULT_INDEX : destination.index;
+
+ try {
+ return PlacesUtils.bookmarks.update(info).then(convert)
+ .catch(error => Promise.reject({message: error.message}));
+ } catch (e) {
+ return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
+ }
+ },
+
+ update: function(id, changes) {
+ let info = {
+ guid: id,
+ };
+
+ if (changes.title !== null) {
+ info.title = changes.title;
+ }
+ if (changes.url !== null) {
+ info.url = changes.url;
+ }
+
+ try {
+ return PlacesUtils.bookmarks.update(info).then(convert)
+ .catch(error => Promise.reject({message: error.message}));
+ } catch (e) {
+ return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
+ }
+ },
+
+ remove: function(id) {
+ let info = {
+ guid: id,
+ };
+
+ // The API doesn't give you the old bookmark at the moment
+ try {
+ return PlacesUtils.bookmarks.remove(info, {preventRemovalOfNonEmptyFolders: true}).then(result => {})
+ .catch(error => Promise.reject({message: error.message}));
+ } catch (e) {
+ return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
+ }
+ },
+
+ removeTree: function(id) {
+ let info = {
+ guid: id,
+ };
+
+ try {
+ return PlacesUtils.bookmarks.remove(info).then(result => {})
+ .catch(error => Promise.reject({message: error.message}));
+ } catch (e) {
+ return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
+ }
+ },
+
+ onCreated: new SingletonEventManager(context, "bookmarks.onCreated", fire => {
+ let listener = (event, bookmark) => {
+ context.runSafe(fire, bookmark.id, bookmark);
+ };
+
+ observer.on("created", listener);
+ incrementListeners();
+ return () => {
+ observer.off("created", listener);
+ decrementListeners();
+ };
+ }).api(),
+
+ onRemoved: new SingletonEventManager(context, "bookmarks.onRemoved", fire => {
+ let listener = (event, data) => {
+ context.runSafe(fire, data.guid, data.info);
+ };
+
+ observer.on("removed", listener);
+ incrementListeners();
+ return () => {
+ observer.off("removed", listener);
+ decrementListeners();
+ };
+ }).api(),
+
+ onChanged: new SingletonEventManager(context, "bookmarks.onChanged", fire => {
+ let listener = (event, data) => {
+ context.runSafe(fire, data.guid, data.info);
+ };
+
+ observer.on("changed", listener);
+ incrementListeners();
+ return () => {
+ observer.off("changed", listener);
+ decrementListeners();
+ };
+ }).api(),
+
+ onMoved: new SingletonEventManager(context, "bookmarks.onMoved", fire => {
+ let listener = (event, data) => {
+ context.runSafe(fire, data.guid, data.info);
+ };
+
+ observer.on("moved", listener);
+ incrementListeners();
+ return () => {
+ observer.off("moved", listener);
+ decrementListeners();
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-browserAction.js b/browser/components/extensions/ext-browserAction.js
new file mode 100644
index 000000000..97c6fd22c
--- /dev/null
+++ b/browser/components/extensions/ext-browserAction.js
@@ -0,0 +1,528 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
+ "resource:///modules/CustomizableUI.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "clearTimeout",
+ "resource://gre/modules/Timer.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "setTimeout",
+ "resource://gre/modules/Timer.jsm");
+
+XPCOMUtils.defineLazyGetter(this, "colorUtils", () => {
+ return require("devtools/shared/css/color").colorUtils;
+});
+
+Cu.import("resource://devtools/shared/event-emitter.js");
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
+
+var {
+ EventManager,
+ IconDetails,
+} = ExtensionUtils;
+
+const POPUP_PRELOAD_TIMEOUT_MS = 200;
+
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+function isAncestorOrSelf(target, node) {
+ for (; node; node = node.parentNode) {
+ if (node === target) {
+ return true;
+ }
+ }
+ return false;
+}
+
+// WeakMap[Extension -> BrowserAction]
+var browserActionMap = new WeakMap();
+
+// Responsible for the browser_action section of the manifest as well
+// as the associated popup.
+function BrowserAction(options, extension) {
+ this.extension = extension;
+
+ let widgetId = makeWidgetId(extension.id);
+ this.id = `${widgetId}-browser-action`;
+ this.viewId = `PanelUI-webext-${widgetId}-browser-action-view`;
+ this.widget = null;
+
+ this.pendingPopup = null;
+ this.pendingPopupTimeout = null;
+
+ this.tabManager = TabManager.for(extension);
+
+ this.defaults = {
+ enabled: true,
+ title: options.default_title || extension.name,
+ badgeText: "",
+ badgeBackgroundColor: null,
+ icon: IconDetails.normalize({path: options.default_icon}, extension),
+ popup: options.default_popup || "",
+ };
+
+ this.browserStyle = options.browser_style || false;
+ if (options.browser_style === null) {
+ this.extension.logger.warn("Please specify whether you want browser_style " +
+ "or not in your browser_action options.");
+ }
+
+ this.tabContext = new TabContext(tab => Object.create(this.defaults),
+ extension);
+
+ EventEmitter.decorate(this);
+}
+
+BrowserAction.prototype = {
+ build() {
+ let widget = CustomizableUI.createWidget({
+ id: this.id,
+ viewId: this.viewId,
+ type: "view",
+ removable: true,
+ label: this.defaults.title || this.extension.name,
+ tooltiptext: this.defaults.title || "",
+ defaultArea: CustomizableUI.AREA_NAVBAR,
+
+ onBeforeCreated: document => {
+ let view = document.createElementNS(XUL_NS, "panelview");
+ view.id = this.viewId;
+ view.setAttribute("flex", "1");
+
+ document.getElementById("PanelUI-multiView").appendChild(view);
+ },
+
+ onDestroyed: document => {
+ let view = document.getElementById(this.viewId);
+ if (view) {
+ this.clearPopup();
+ CustomizableUI.hidePanelForNode(view);
+ view.remove();
+ }
+ },
+
+ onCreated: node => {
+ node.classList.add("badged-button");
+ node.classList.add("webextension-browser-action");
+ node.setAttribute("constrain-size", "true");
+
+ node.onmousedown = event => this.handleEvent(event);
+
+ this.updateButton(node, this.defaults);
+ },
+
+ onViewShowing: event => {
+ let document = event.target.ownerDocument;
+ let tabbrowser = document.defaultView.gBrowser;
+
+ let tab = tabbrowser.selectedTab;
+ let popupURL = this.getProperty(tab, "popup");
+ this.tabManager.addActiveTabPermission(tab);
+
+ // Popups are shown only if a popup URL is defined; otherwise
+ // a "click" event is dispatched. This is done for compatibility with the
+ // Google Chrome onClicked extension API.
+ if (popupURL) {
+ try {
+ let popup = this.getPopup(document.defaultView, popupURL);
+ event.detail.addBlocker(popup.attach(event.target));
+ } catch (e) {
+ Cu.reportError(e);
+ event.preventDefault();
+ }
+ } else {
+ // This isn't not a hack, but it seems to provide the correct behavior
+ // with the fewest complications.
+ event.preventDefault();
+ this.emit("click");
+ }
+ },
+ });
+
+ this.tabContext.on("tab-select", // eslint-disable-line mozilla/balanced-listeners
+ (evt, tab) => { this.updateWindow(tab.ownerGlobal); });
+
+ this.widget = widget;
+ },
+
+ /**
+ * Triggers this browser action for the given window, with the same effects as
+ * if it were clicked by a user.
+ *
+ * This has no effect if the browser action is disabled for, or not
+ * present in, the given window.
+ */
+ triggerAction: Task.async(function* (window) {
+ let popup = ViewPopup.for(this.extension, window);
+ if (popup) {
+ popup.closePopup();
+ return;
+ }
+
+ let widget = this.widget.forWindow(window);
+ let tab = window.gBrowser.selectedTab;
+
+ if (!widget || !this.getProperty(tab, "enabled")) {
+ return;
+ }
+
+ // Popups are shown only if a popup URL is defined; otherwise
+ // a "click" event is dispatched. This is done for compatibility with the
+ // Google Chrome onClicked extension API.
+ if (this.getProperty(tab, "popup")) {
+ if (this.widget.areaType == CustomizableUI.TYPE_MENU_PANEL) {
+ yield window.PanelUI.show();
+ }
+
+ let event = new window.CustomEvent("command", {bubbles: true, cancelable: true});
+ widget.node.dispatchEvent(event);
+ } else {
+ this.emit("click");
+ }
+ }),
+
+ handleEvent(event) {
+ let button = event.target;
+ let window = button.ownerDocument.defaultView;
+
+ switch (event.type) {
+ case "mousedown":
+ if (event.button == 0) {
+ // Begin pre-loading the browser for the popup, so it's more likely to
+ // be ready by the time we get a complete click.
+ let tab = window.gBrowser.selectedTab;
+ let popupURL = this.getProperty(tab, "popup");
+ let enabled = this.getProperty(tab, "enabled");
+
+ if (popupURL && enabled) {
+ // Add permission for the active tab so it will exist for the popup.
+ // Store the tab to revoke the permission during clearPopup.
+ if (!this.pendingPopup && !this.tabManager.hasActiveTabPermission(tab)) {
+ this.tabManager.addActiveTabPermission(tab);
+ this.tabToRevokeDuringClearPopup = tab;
+ }
+
+ this.pendingPopup = this.getPopup(window, popupURL);
+ window.addEventListener("mouseup", this, true);
+ } else {
+ this.clearPopup();
+ }
+ }
+ break;
+
+ case "mouseup":
+ if (event.button == 0) {
+ this.clearPopupTimeout();
+ // If we have a pending pre-loaded popup, cancel it after we've waited
+ // long enough that we can be relatively certain it won't be opening.
+ if (this.pendingPopup) {
+ let {node} = this.widget.forWindow(window);
+ if (isAncestorOrSelf(node, event.originalTarget)) {
+ this.pendingPopupTimeout = setTimeout(() => this.clearPopup(),
+ POPUP_PRELOAD_TIMEOUT_MS);
+ } else {
+ this.clearPopup();
+ }
+ }
+ }
+ break;
+ }
+ },
+
+ /**
+ * Returns a potentially pre-loaded popup for the given URL in the given
+ * window. If a matching pre-load popup already exists, returns that.
+ * Otherwise, initializes a new one.
+ *
+ * If a pre-load popup exists which does not match, it is destroyed before a
+ * new one is created.
+ *
+ * @param {Window} window
+ * The browser window in which to create the popup.
+ * @param {string} popupURL
+ * The URL to load into the popup.
+ * @returns {ViewPopup}
+ */
+ getPopup(window, popupURL) {
+ this.clearPopupTimeout();
+ let {pendingPopup} = this;
+ this.pendingPopup = null;
+
+ if (pendingPopup) {
+ if (pendingPopup.window === window && pendingPopup.popupURL === popupURL) {
+ return pendingPopup;
+ }
+ pendingPopup.destroy();
+ }
+
+ let fixedWidth = this.widget.areaType == CustomizableUI.TYPE_MENU_PANEL;
+ return new ViewPopup(this.extension, window, popupURL, this.browserStyle, fixedWidth);
+ },
+
+ /**
+ * Clears any pending pre-loaded popup and related timeouts.
+ */
+ clearPopup() {
+ this.clearPopupTimeout();
+ if (this.pendingPopup) {
+ if (this.tabToRevokeDuringClearPopup) {
+ this.tabManager.revokeActiveTabPermission(this.tabToRevokeDuringClearPopup);
+ this.tabToRevokeDuringClearPopup = null;
+ }
+ this.pendingPopup.destroy();
+ this.pendingPopup = null;
+ }
+ },
+
+ /**
+ * Clears any pending timeouts to clear stale, pre-loaded popups.
+ */
+ clearPopupTimeout() {
+ if (this.pendingPopup) {
+ this.pendingPopup.window.removeEventListener("mouseup", this, true);
+ }
+
+ if (this.pendingPopupTimeout) {
+ clearTimeout(this.pendingPopupTimeout);
+ this.pendingPopupTimeout = null;
+ }
+ },
+
+ // Update the toolbar button |node| with the tab context data
+ // in |tabData|.
+ updateButton(node, tabData) {
+ let title = tabData.title || this.extension.name;
+ node.setAttribute("tooltiptext", title);
+ node.setAttribute("label", title);
+
+ if (tabData.badgeText) {
+ node.setAttribute("badge", tabData.badgeText);
+ } else {
+ node.removeAttribute("badge");
+ }
+
+ if (tabData.enabled) {
+ node.removeAttribute("disabled");
+ } else {
+ node.setAttribute("disabled", "true");
+ }
+
+ let badgeNode = node.ownerDocument.getAnonymousElementByAttribute(node,
+ "class", "toolbarbutton-badge");
+ if (badgeNode) {
+ let color = tabData.badgeBackgroundColor;
+ if (color) {
+ color = `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${color[3] / 255})`;
+ }
+ badgeNode.style.backgroundColor = color || "";
+ }
+
+ const LEGACY_CLASS = "toolbarbutton-legacy-addon";
+ node.classList.remove(LEGACY_CLASS);
+
+ let baseSize = 16;
+ let {icon, size} = IconDetails.getPreferredIcon(tabData.icon, this.extension, baseSize);
+
+ // If the best available icon size is not divisible by 16, check if we have
+ // an 18px icon to fall back to, and trim off the padding instead.
+ if (size % 16 && !icon.endsWith(".svg")) {
+ let result = IconDetails.getPreferredIcon(tabData.icon, this.extension, 18);
+
+ if (result.size % 18 == 0) {
+ baseSize = 18;
+ icon = result.icon;
+ node.classList.add(LEGACY_CLASS);
+ }
+ }
+
+ // These URLs should already be properly escaped, but make doubly sure CSS
+ // string escape characters are escaped here, since they could lead to a
+ // sandbox break.
+ let escape = str => str.replace(/[\\\s"]/g, encodeURIComponent);
+
+ let getIcon = size => escape(IconDetails.getPreferredIcon(tabData.icon, this.extension, size).icon);
+
+ node.setAttribute("style", `
+ --webextension-menupanel-image: url("${getIcon(32)}");
+ --webextension-menupanel-image-2x: url("${getIcon(64)}");
+ --webextension-toolbar-image: url("${escape(icon)}");
+ --webextension-toolbar-image-2x: url("${getIcon(baseSize * 2)}");
+ `);
+ },
+
+ // Update the toolbar button for a given window.
+ updateWindow(window) {
+ let widget = this.widget.forWindow(window);
+ if (widget) {
+ let tab = window.gBrowser.selectedTab;
+ this.updateButton(widget.node, this.tabContext.get(tab));
+ }
+ },
+
+ // Update the toolbar button when the extension changes the icon,
+ // title, badge, etc. If it only changes a parameter for a single
+ // tab, |tab| will be that tab. Otherwise it will be null.
+ updateOnChange(tab) {
+ if (tab) {
+ if (tab.selected) {
+ this.updateWindow(tab.ownerGlobal);
+ }
+ } else {
+ for (let window of WindowListManager.browserWindows()) {
+ this.updateWindow(window);
+ }
+ }
+ },
+
+ // tab is allowed to be null.
+ // prop should be one of "icon", "title", "badgeText", "popup", or "badgeBackgroundColor".
+ setProperty(tab, prop, value) {
+ if (tab == null) {
+ this.defaults[prop] = value;
+ } else if (value != null) {
+ this.tabContext.get(tab)[prop] = value;
+ } else {
+ delete this.tabContext.get(tab)[prop];
+ }
+
+ this.updateOnChange(tab);
+ },
+
+ // tab is allowed to be null.
+ // prop should be one of "title", "badgeText", "popup", or "badgeBackgroundColor".
+ getProperty(tab, prop) {
+ if (tab == null) {
+ return this.defaults[prop];
+ }
+ return this.tabContext.get(tab)[prop];
+ },
+
+ shutdown() {
+ this.tabContext.shutdown();
+ CustomizableUI.destroyWidget(this.id);
+ },
+};
+
+BrowserAction.for = (extension) => {
+ return browserActionMap.get(extension);
+};
+
+global.browserActionFor = BrowserAction.for;
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("manifest_browser_action", (type, directive, extension, manifest) => {
+ let browserAction = new BrowserAction(manifest.browser_action, extension);
+ browserAction.build();
+ browserActionMap.set(extension, browserAction);
+});
+
+extensions.on("shutdown", (type, extension) => {
+ if (browserActionMap.has(extension)) {
+ browserActionMap.get(extension).shutdown();
+ browserActionMap.delete(extension);
+ }
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+extensions.registerSchemaAPI("browserAction", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ browserAction: {
+ onClicked: new EventManager(context, "browserAction.onClicked", fire => {
+ let listener = () => {
+ let tab = TabManager.activeTab;
+ fire(TabManager.convert(extension, tab));
+ };
+ BrowserAction.for(extension).on("click", listener);
+ return () => {
+ BrowserAction.for(extension).off("click", listener);
+ };
+ }).api(),
+
+ enable: function(tabId) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : null;
+ BrowserAction.for(extension).setProperty(tab, "enabled", true);
+ },
+
+ disable: function(tabId) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : null;
+ BrowserAction.for(extension).setProperty(tab, "enabled", false);
+ },
+
+ setTitle: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let title = details.title;
+ // Clear the tab-specific title when given a null string.
+ if (tab && title == "") {
+ title = null;
+ }
+ BrowserAction.for(extension).setProperty(tab, "title", title);
+ },
+
+ getTitle: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let title = BrowserAction.for(extension).getProperty(tab, "title");
+ return Promise.resolve(title);
+ },
+
+ setIcon: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let icon = IconDetails.normalize(details, extension, context);
+ BrowserAction.for(extension).setProperty(tab, "icon", icon);
+ },
+
+ setBadgeText: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ BrowserAction.for(extension).setProperty(tab, "badgeText", details.text);
+ },
+
+ getBadgeText: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let text = BrowserAction.for(extension).getProperty(tab, "badgeText");
+ return Promise.resolve(text);
+ },
+
+ setPopup: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ // Note: Chrome resolves arguments to setIcon relative to the calling
+ // context, but resolves arguments to setPopup relative to the extension
+ // root.
+ // For internal consistency, we currently resolve both relative to the
+ // calling context.
+ let url = details.popup && context.uri.resolve(details.popup);
+ BrowserAction.for(extension).setProperty(tab, "popup", url);
+ },
+
+ getPopup: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let popup = BrowserAction.for(extension).getProperty(tab, "popup");
+ return Promise.resolve(popup);
+ },
+
+ setBadgeBackgroundColor: function(details) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+ let color = details.color;
+ if (!Array.isArray(color)) {
+ let col = colorUtils.colorToRGBA(color);
+ color = col && [col.r, col.g, col.b, Math.round(col.a * 255)];
+ }
+ BrowserAction.for(extension).setProperty(tab, "badgeBackgroundColor", color);
+ },
+
+ getBadgeBackgroundColor: function(details, callback) {
+ let tab = details.tabId !== null ? TabManager.getTab(details.tabId, context) : null;
+
+ let color = BrowserAction.for(extension).getProperty(tab, "badgeBackgroundColor");
+ return Promise.resolve(color || [0xd9, 0, 0, 255]);
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-c-contextMenus.js b/browser/components/extensions/ext-c-contextMenus.js
new file mode 100644
index 000000000..9fde90808
--- /dev/null
+++ b/browser/components/extensions/ext-c-contextMenus.js
@@ -0,0 +1,158 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+// If id is not specified for an item we use an integer.
+// This ID need only be unique within a single addon. Since all addon code that
+// can use this API runs in the same process, this local variable suffices.
+var gNextMenuItemID = 0;
+
+// Map[Extension -> Map[string or id, ContextMenusClickPropHandler]]
+var gPropHandlers = new Map();
+
+// The contextMenus API supports an "onclick" attribute in the create/update
+// methods to register a callback. This class manages these onclick properties.
+class ContextMenusClickPropHandler {
+ constructor(context) {
+ this.context = context;
+ // Map[string or integer -> callback]
+ this.onclickMap = new Map();
+ this.dispatchEvent = this.dispatchEvent.bind(this);
+ }
+
+ // A listener on contextMenus.onClicked that forwards the event to the only
+ // listener, if any.
+ dispatchEvent(info, tab) {
+ let onclick = this.onclickMap.get(info.menuItemId);
+ if (onclick) {
+ // No need for runSafe or anything because we are already being run inside
+ // an event handler -- the event is just being forwarded to the actual
+ // handler.
+ onclick(info, tab);
+ }
+ }
+
+ // Sets the `onclick` handler for the given menu item.
+ // The `onclick` function MUST be owned by `this.context`.
+ setListener(id, onclick) {
+ if (this.onclickMap.size === 0) {
+ this.context.childManager.getParentEvent("contextMenus.onClicked").addListener(this.dispatchEvent);
+ this.context.callOnClose(this);
+ }
+ this.onclickMap.set(id, onclick);
+
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ if (!propHandlerMap) {
+ propHandlerMap = new Map();
+ } else {
+ // If the current callback was created in a different context, remove it
+ // from the other context.
+ let propHandler = propHandlerMap.get(id);
+ if (propHandler && propHandler !== this) {
+ propHandler.unsetListener(id);
+ }
+ }
+ propHandlerMap.set(id, this);
+ gPropHandlers.set(this.context.extension, propHandlerMap);
+ }
+
+ // Deletes the `onclick` handler for the given menu item.
+ // The `onclick` function MUST be owned by `this.context`.
+ unsetListener(id) {
+ if (!this.onclickMap.delete(id)) {
+ return;
+ }
+ if (this.onclickMap.size === 0) {
+ this.context.childManager.getParentEvent("contextMenus.onClicked").removeListener(this.dispatchEvent);
+ this.context.forgetOnClose(this);
+ }
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ propHandlerMap.delete(id);
+ if (propHandlerMap.size === 0) {
+ gPropHandlers.delete(this.context.extension);
+ }
+ }
+
+ // Deletes the `onclick` handler for the given menu item, if any, regardless
+ // of the context where it was created.
+ unsetListenerFromAnyContext(id) {
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ let propHandler = propHandlerMap && propHandlerMap.get(id);
+ if (propHandler) {
+ propHandler.unsetListener(id);
+ }
+ }
+
+ // Remove all `onclick` handlers of the extension.
+ deleteAllListenersFromExtension() {
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ if (propHandlerMap) {
+ for (let [id, propHandler] of propHandlerMap) {
+ propHandler.unsetListener(id);
+ }
+ }
+ }
+
+ // Removes all `onclick` handlers from this context.
+ close() {
+ for (let id of this.onclickMap.keys()) {
+ this.unsetListener(id);
+ }
+ }
+}
+
+extensions.registerSchemaAPI("contextMenus", "addon_child", context => {
+ let onClickedProp = new ContextMenusClickPropHandler(context);
+
+ return {
+ contextMenus: {
+ create(createProperties, callback) {
+ if (createProperties.id === null) {
+ createProperties.id = ++gNextMenuItemID;
+ }
+ let {onclick} = createProperties;
+ delete createProperties.onclick;
+ context.childManager.callParentAsyncFunction("contextMenus.createInternal", [
+ createProperties,
+ ]).then(() => {
+ if (onclick) {
+ onClickedProp.setListener(createProperties.id, onclick);
+ }
+ if (callback) {
+ callback();
+ }
+ });
+ return createProperties.id;
+ },
+
+ update(id, updateProperties) {
+ let {onclick} = updateProperties;
+ delete updateProperties.onclick;
+ return context.childManager.callParentAsyncFunction("contextMenus.update", [
+ id,
+ updateProperties,
+ ]).then(() => {
+ if (onclick) {
+ onClickedProp.setListener(id, onclick);
+ } else if (onclick === null) {
+ onClickedProp.unsetListenerFromAnyContext(id);
+ }
+ // else onclick is not set so it should not be changed.
+ });
+ },
+
+ remove(id) {
+ onClickedProp.unsetListenerFromAnyContext(id);
+ return context.childManager.callParentAsyncFunction("contextMenus.remove", [
+ id,
+ ]);
+ },
+
+ removeAll() {
+ onClickedProp.deleteAllListenersFromExtension();
+
+ return context.childManager.callParentAsyncFunction("contextMenus.removeAll", []);
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-c-omnibox.js b/browser/components/extensions/ext-c-omnibox.js
new file mode 100644
index 000000000..3b9b6e2f7
--- /dev/null
+++ b/browser/components/extensions/ext-c-omnibox.js
@@ -0,0 +1,32 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+var {
+ runSafeSyncWithoutClone,
+ SingletonEventManager,
+} = ExtensionUtils;
+
+extensions.registerSchemaAPI("omnibox", "addon_child", context => {
+ return {
+ omnibox: {
+ onInputChanged: new SingletonEventManager(context, "omnibox.onInputChanged", fire => {
+ let listener = (text, id) => {
+ runSafeSyncWithoutClone(fire, text, suggestions => {
+ // TODO: Switch to using callParentFunctionNoReturn once bug 1314903 is fixed.
+ context.childManager.callParentAsyncFunction("omnibox_internal.addSuggestions", [
+ id,
+ suggestions,
+ ]);
+ });
+ };
+ context.childManager.getParentEvent("omnibox_internal.onInputChanged").addListener(listener);
+ return () => {
+ context.childManager.getParentEvent("omnibox_internal.onInputChanged").removeListener(listener);
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-c-tabs.js b/browser/components/extensions/ext-c-tabs.js
new file mode 100644
index 000000000..d5ce9fbf9
--- /dev/null
+++ b/browser/components/extensions/ext-c-tabs.js
@@ -0,0 +1,35 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+extensions.registerSchemaAPI("tabs", "addon_child", context => {
+ return {
+ tabs: {
+ connect: function(tabId, connectInfo) {
+ let name = "";
+ if (connectInfo && connectInfo.name !== null) {
+ name = connectInfo.name;
+ }
+ let recipient = {
+ extensionId: context.extension.id,
+ tabId,
+ };
+ if (connectInfo && connectInfo.frameId !== null) {
+ recipient.frameId = connectInfo.frameId;
+ }
+ return context.messenger.connect(context.messageManager, name, recipient);
+ },
+
+ sendMessage: function(tabId, message, options, responseCallback) {
+ let recipient = {
+ extensionId: context.extension.id,
+ tabId: tabId,
+ };
+ if (options && options.frameId !== null) {
+ recipient.frameId = options.frameId;
+ }
+ return context.messenger.sendMessage(context.messageManager, message, recipient, responseCallback);
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-commands.js b/browser/components/extensions/ext-commands.js
new file mode 100644
index 000000000..416544e86
--- /dev/null
+++ b/browser/components/extensions/ext-commands.js
@@ -0,0 +1,259 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://devtools/shared/event-emitter.js");
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+var {
+ EventManager,
+ PlatformInfo,
+} = ExtensionUtils;
+
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+// WeakMap[Extension -> CommandList]
+var commandsMap = new WeakMap();
+
+function CommandList(manifest, extension) {
+ this.extension = extension;
+ this.id = makeWidgetId(extension.id);
+ this.windowOpenListener = null;
+
+ // Map[{String} commandName -> {Object} commandProperties]
+ this.commands = this.loadCommandsFromManifest(manifest);
+
+ // WeakMap[Window -> <xul:keyset>]
+ this.keysetsMap = new WeakMap();
+
+ this.register();
+ EventEmitter.decorate(this);
+}
+
+CommandList.prototype = {
+ /**
+ * Registers the commands to all open windows and to any which
+ * are later created.
+ */
+ register() {
+ for (let window of WindowListManager.browserWindows()) {
+ this.registerKeysToDocument(window);
+ }
+
+ this.windowOpenListener = (window) => {
+ if (!this.keysetsMap.has(window)) {
+ this.registerKeysToDocument(window);
+ }
+ };
+
+ WindowListManager.addOpenListener(this.windowOpenListener);
+ },
+
+ /**
+ * Unregisters the commands from all open windows and stops commands
+ * from being registered to windows which are later created.
+ */
+ unregister() {
+ for (let window of WindowListManager.browserWindows()) {
+ if (this.keysetsMap.has(window)) {
+ this.keysetsMap.get(window).remove();
+ }
+ }
+
+ WindowListManager.removeOpenListener(this.windowOpenListener);
+ },
+
+ /**
+ * Creates a Map from commands for each command in the manifest.commands object.
+ *
+ * @param {Object} manifest The manifest JSON object.
+ * @returns {Map<string, object>}
+ */
+ loadCommandsFromManifest(manifest) {
+ let commands = new Map();
+ // For Windows, chrome.runtime expects 'win' while chrome.commands
+ // expects 'windows'. We can special case this for now.
+ let os = PlatformInfo.os == "win" ? "windows" : PlatformInfo.os;
+ for (let name of Object.keys(manifest.commands)) {
+ let command = manifest.commands[name];
+ let shortcut = command.suggested_key[os] || command.suggested_key.default;
+ if (shortcut) {
+ commands.set(name, {
+ description: command.description,
+ shortcut: shortcut.replace(/\s+/g, ""),
+ });
+ }
+ }
+ return commands;
+ },
+
+ /**
+ * Registers the commands to a document.
+ * @param {ChromeWindow} window The XUL window to insert the Keyset.
+ */
+ registerKeysToDocument(window) {
+ let doc = window.document;
+ let keyset = doc.createElementNS(XUL_NS, "keyset");
+ keyset.id = `ext-keyset-id-${this.id}`;
+ this.commands.forEach((command, name) => {
+ let keyElement = this.buildKey(doc, name, command.shortcut);
+ keyset.appendChild(keyElement);
+ });
+ doc.documentElement.appendChild(keyset);
+ this.keysetsMap.set(window, keyset);
+ },
+
+ /**
+ * Builds a XUL Key element and attaches an onCommand listener which
+ * emits a command event with the provided name when fired.
+ *
+ * @param {Document} doc The XUL document.
+ * @param {string} name The name of the command.
+ * @param {string} shortcut The shortcut provided in the manifest.
+ * @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/key
+ *
+ * @returns {Document} The newly created Key element.
+ */
+ buildKey(doc, name, shortcut) {
+ let keyElement = this.buildKeyFromShortcut(doc, shortcut);
+
+ // We need to have the attribute "oncommand" for the "command" listener to fire,
+ // and it is currently ignored when set to the empty string.
+ keyElement.setAttribute("oncommand", "//");
+
+ /* eslint-disable mozilla/balanced-listeners */
+ // We remove all references to the key elements when the extension is shutdown,
+ // therefore the listeners for these elements will be garbage collected.
+ keyElement.addEventListener("command", (event) => {
+ if (name == "_execute_page_action") {
+ let win = event.target.ownerDocument.defaultView;
+ pageActionFor(this.extension).triggerAction(win);
+ } else if (name == "_execute_browser_action") {
+ let win = event.target.ownerDocument.defaultView;
+ browserActionFor(this.extension).triggerAction(win);
+ } else {
+ TabManager.for(this.extension)
+ .addActiveTabPermission(TabManager.activeTab);
+ this.emit("command", name);
+ }
+ });
+ /* eslint-enable mozilla/balanced-listeners */
+
+ return keyElement;
+ },
+
+ /**
+ * Builds a XUL Key element from the provided shortcut.
+ *
+ * @param {Document} doc The XUL document.
+ * @param {string} shortcut The shortcut provided in the manifest.
+ *
+ * @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/key
+ * @returns {Document} The newly created Key element.
+ */
+ buildKeyFromShortcut(doc, shortcut) {
+ let keyElement = doc.createElementNS(XUL_NS, "key");
+
+ let parts = shortcut.split("+");
+
+ // The key is always the last element.
+ let chromeKey = parts.pop();
+
+ // The modifiers are the remaining elements.
+ keyElement.setAttribute("modifiers", this.getModifiersAttribute(parts));
+
+ if (/^[A-Z0-9]$/.test(chromeKey)) {
+ // We use the key attribute for all single digits and characters.
+ keyElement.setAttribute("key", chromeKey);
+ } else {
+ keyElement.setAttribute("keycode", this.getKeycodeAttribute(chromeKey));
+ }
+
+ return keyElement;
+ },
+
+ /**
+ * Determines the corresponding XUL keycode from the given chrome key.
+ *
+ * For example:
+ *
+ * input | output
+ * ---------------------------------------
+ * "PageUP" | "VK_PAGE_UP"
+ * "Delete" | "VK_DELETE"
+ *
+ * @param {string} chromeKey The chrome key (e.g. "PageUp", "Space", ...)
+ * @returns {string} The constructed value for the Key's 'keycode' attribute.
+ */
+ getKeycodeAttribute(chromeKey) {
+ return `VK${chromeKey.replace(/([A-Z])/g, "_$&").toUpperCase()}`;
+ },
+
+ /**
+ * Determines the corresponding XUL modifiers from the chrome modifiers.
+ *
+ * For example:
+ *
+ * input | output
+ * ---------------------------------------
+ * ["Ctrl", "Shift"] | "accel shift"
+ * ["MacCtrl"] | "control"
+ *
+ * @param {Array} chromeModifiers The array of chrome modifiers.
+ * @returns {string} The constructed value for the Key's 'modifiers' attribute.
+ */
+ getModifiersAttribute(chromeModifiers) {
+ let modifiersMap = {
+ "Alt": "alt",
+ "Command": "accel",
+ "Ctrl": "accel",
+ "MacCtrl": "control",
+ "Shift": "shift",
+ };
+ return Array.from(chromeModifiers, modifier => {
+ return modifiersMap[modifier];
+ }).join(" ");
+ },
+};
+
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("manifest_commands", (type, directive, extension, manifest) => {
+ commandsMap.set(extension, new CommandList(manifest, extension));
+});
+
+extensions.on("shutdown", (type, extension) => {
+ let commandsList = commandsMap.get(extension);
+ if (commandsList) {
+ commandsList.unregister();
+ commandsMap.delete(extension);
+ }
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+extensions.registerSchemaAPI("commands", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ commands: {
+ getAll() {
+ let commands = commandsMap.get(extension).commands;
+ return Promise.resolve(Array.from(commands, ([name, command]) => {
+ return ({
+ name,
+ description: command.description,
+ shortcut: command.shortcut,
+ });
+ }));
+ },
+ onCommand: new EventManager(context, "commands.onCommand", fire => {
+ let listener = (eventName, commandName) => {
+ fire(commandName);
+ };
+ commandsMap.get(extension).on("command", listener);
+ return () => {
+ commandsMap.get(extension).off("command", listener);
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-contextMenus.js b/browser/components/extensions/ext-contextMenus.js
new file mode 100644
index 000000000..b3bf8aa53
--- /dev/null
+++ b/browser/components/extensions/ext-contextMenus.js
@@ -0,0 +1,537 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+Cu.import("resource://gre/modules/MatchPattern.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+var {
+ EventManager,
+ ExtensionError,
+ IconDetails,
+} = ExtensionUtils;
+
+// Map[Extension -> Map[ID -> MenuItem]]
+// Note: we want to enumerate all the menu items so
+// this cannot be a weak map.
+var gContextMenuMap = new Map();
+
+// Map[Extension -> MenuItem]
+var gRootItems = new Map();
+
+// If id is not specified for an item we use an integer.
+var gNextMenuItemID = 0;
+
+// Used to assign unique names to radio groups.
+var gNextRadioGroupID = 0;
+
+// The max length of a menu item's label.
+var gMaxLabelLength = 64;
+
+// When a new contextMenu is opened, this function is called and
+// we populate the |xulMenu| with all the items from extensions
+// to be displayed. We always clear all the items again when
+// popuphidden fires.
+var gMenuBuilder = {
+ build: function(contextData) {
+ let xulMenu = contextData.menu;
+ xulMenu.addEventListener("popuphidden", this);
+ this.xulMenu = xulMenu;
+ for (let [, root] of gRootItems) {
+ let rootElement = this.buildElementWithChildren(root, contextData);
+ if (!rootElement.firstChild || !rootElement.firstChild.childNodes.length) {
+ // If the root has no visible children, there is no reason to show
+ // the root menu item itself either.
+ continue;
+ }
+ rootElement.setAttribute("ext-type", "top-level-menu");
+ rootElement = this.removeTopLevelMenuIfNeeded(rootElement);
+
+ // Display the extension icon on the root element.
+ if (root.extension.manifest.icons) {
+ let parentWindow = contextData.menu.ownerGlobal;
+ let extension = root.extension;
+
+ let {icon} = IconDetails.getPreferredIcon(extension.manifest.icons, extension,
+ 16 * parentWindow.devicePixelRatio);
+
+ // The extension icons in the manifest are not pre-resolved, since
+ // they're sometimes used by the add-on manager when the extension is
+ // not enabled, and its URLs are not resolvable.
+ let resolvedURL = root.extension.baseURI.resolve(icon);
+
+ if (rootElement.localName == "menu") {
+ rootElement.setAttribute("class", "menu-iconic");
+ } else if (rootElement.localName == "menuitem") {
+ rootElement.setAttribute("class", "menuitem-iconic");
+ }
+ rootElement.setAttribute("image", resolvedURL);
+ }
+
+ xulMenu.appendChild(rootElement);
+ this.itemsToCleanUp.add(rootElement);
+ }
+ },
+
+ buildElementWithChildren(item, contextData) {
+ let element = this.buildSingleElement(item, contextData);
+ let groupName;
+ for (let child of item.children) {
+ if (child.type == "radio" && !child.groupName) {
+ if (!groupName) {
+ groupName = `webext-radio-group-${gNextRadioGroupID++}`;
+ }
+ child.groupName = groupName;
+ } else {
+ groupName = null;
+ }
+
+ if (child.enabledForContext(contextData)) {
+ let childElement = this.buildElementWithChildren(child, contextData);
+ // Here element must be a menu element and its first child
+ // is a menupopup, we have to append its children to this
+ // menupopup.
+ element.firstChild.appendChild(childElement);
+ }
+ }
+
+ return element;
+ },
+
+ removeTopLevelMenuIfNeeded(element) {
+ // If there is only one visible top level element we don't need the
+ // root menu element for the extension.
+ let menuPopup = element.firstChild;
+ if (menuPopup && menuPopup.childNodes.length == 1) {
+ let onlyChild = menuPopup.firstChild;
+ onlyChild.remove();
+ return onlyChild;
+ }
+
+ return element;
+ },
+
+ buildSingleElement(item, contextData) {
+ let doc = contextData.menu.ownerDocument;
+ let element;
+ if (item.children.length > 0) {
+ element = this.createMenuElement(doc, item);
+ } else if (item.type == "separator") {
+ element = doc.createElement("menuseparator");
+ } else {
+ element = doc.createElement("menuitem");
+ }
+
+ return this.customizeElement(element, item, contextData);
+ },
+
+ createMenuElement(doc, item) {
+ let element = doc.createElement("menu");
+ // Menu elements need to have a menupopup child for its menu items.
+ let menupopup = doc.createElement("menupopup");
+ element.appendChild(menupopup);
+ return element;
+ },
+
+ customizeElement(element, item, contextData) {
+ let label = item.title;
+ if (label) {
+ if (contextData.isTextSelected && label.indexOf("%s") > -1) {
+ let selection = contextData.selectionText;
+ // The rendering engine will truncate the title if it's longer than 64 characters.
+ // But if it makes sense let's try truncate selection text only, to handle cases like
+ // 'look up "%s" in MyDictionary' more elegantly.
+ let maxSelectionLength = gMaxLabelLength - label.length + 2;
+ if (maxSelectionLength > 4) {
+ selection = selection.substring(0, maxSelectionLength - 3) + "...";
+ }
+ label = label.replace(/%s/g, selection);
+ }
+
+ element.setAttribute("label", label);
+ }
+
+ if (item.type == "checkbox") {
+ element.setAttribute("type", "checkbox");
+ if (item.checked) {
+ element.setAttribute("checked", "true");
+ }
+ } else if (item.type == "radio") {
+ element.setAttribute("type", "radio");
+ element.setAttribute("name", item.groupName);
+ if (item.checked) {
+ element.setAttribute("checked", "true");
+ }
+ }
+
+ if (!item.enabled) {
+ element.setAttribute("disabled", "true");
+ }
+
+ element.addEventListener("command", event => { // eslint-disable-line mozilla/balanced-listeners
+ if (event.target !== event.currentTarget) {
+ return;
+ }
+ const wasChecked = item.checked;
+ if (item.type == "checkbox") {
+ item.checked = !item.checked;
+ } else if (item.type == "radio") {
+ // Deselect all radio items in the current radio group.
+ for (let child of item.parent.children) {
+ if (child.type == "radio" && child.groupName == item.groupName) {
+ child.checked = false;
+ }
+ }
+ // Select the clicked radio item.
+ item.checked = true;
+ }
+
+ item.tabManager.addActiveTabPermission();
+
+ let tab = item.tabManager.convert(contextData.tab);
+ let info = item.getClickInfo(contextData, wasChecked);
+ item.extension.emit("webext-contextmenu-menuitem-click", info, tab);
+ });
+
+ return element;
+ },
+
+ handleEvent: function(event) {
+ if (this.xulMenu != event.target || event.type != "popuphidden") {
+ return;
+ }
+
+ delete this.xulMenu;
+ let target = event.target;
+ target.removeEventListener("popuphidden", this);
+ for (let item of this.itemsToCleanUp) {
+ item.remove();
+ }
+ this.itemsToCleanUp.clear();
+ },
+
+ itemsToCleanUp: new Set(),
+};
+
+function contextMenuObserver(subject, topic, data) {
+ subject = subject.wrappedJSObject;
+ gMenuBuilder.build(subject);
+}
+
+function getContexts(contextData) {
+ let contexts = new Set(["all"]);
+
+ if (contextData.inFrame) {
+ contexts.add("frame");
+ }
+
+ if (contextData.isTextSelected) {
+ contexts.add("selection");
+ }
+
+ if (contextData.onLink) {
+ contexts.add("link");
+ }
+
+ if (contextData.onEditableArea) {
+ contexts.add("editable");
+ }
+
+ if (contextData.onImage) {
+ contexts.add("image");
+ }
+
+ if (contextData.onVideo) {
+ contexts.add("video");
+ }
+
+ if (contextData.onAudio) {
+ contexts.add("audio");
+ }
+
+ if (contexts.size == 1) {
+ contexts.add("page");
+ }
+
+ return contexts;
+}
+
+function MenuItem(extension, createProperties, isRoot = false) {
+ this.extension = extension;
+ this.children = [];
+ this.parent = null;
+ this.tabManager = TabManager.for(extension);
+
+ this.setDefaults();
+ this.setProps(createProperties);
+ if (!this.hasOwnProperty("_id")) {
+ this.id = gNextMenuItemID++;
+ }
+ // If the item is not the root and has no parent
+ // it must be a child of the root.
+ if (!isRoot && !this.parent) {
+ this.root.addChild(this);
+ }
+}
+
+MenuItem.prototype = {
+ setProps(createProperties) {
+ for (let propName in createProperties) {
+ if (createProperties[propName] === null) {
+ // Omitted optional argument.
+ continue;
+ }
+ this[propName] = createProperties[propName];
+ }
+
+ if (createProperties.documentUrlPatterns != null) {
+ this.documentUrlMatchPattern = new MatchPattern(this.documentUrlPatterns);
+ }
+
+ if (createProperties.targetUrlPatterns != null) {
+ this.targetUrlMatchPattern = new MatchPattern(this.targetUrlPatterns);
+ }
+ },
+
+ setDefaults() {
+ this.setProps({
+ type: "normal",
+ checked: false,
+ contexts: ["all"],
+ enabled: true,
+ });
+ },
+
+ set id(id) {
+ if (this.hasOwnProperty("_id")) {
+ throw new Error("Id of a MenuItem cannot be changed");
+ }
+ let isIdUsed = gContextMenuMap.get(this.extension).has(id);
+ if (isIdUsed) {
+ throw new Error("Id already exists");
+ }
+ this._id = id;
+ },
+
+ get id() {
+ return this._id;
+ },
+
+ ensureValidParentId(parentId) {
+ if (parentId === undefined) {
+ return;
+ }
+ let menuMap = gContextMenuMap.get(this.extension);
+ if (!menuMap.has(parentId)) {
+ throw new Error("Could not find any MenuItem with id: " + parentId);
+ }
+ for (let item = menuMap.get(parentId); item; item = item.parent) {
+ if (item === this) {
+ throw new ExtensionError("MenuItem cannot be an ancestor (or self) of its new parent.");
+ }
+ }
+ },
+
+ set parentId(parentId) {
+ this.ensureValidParentId(parentId);
+
+ if (this.parent) {
+ this.parent.detachChild(this);
+ }
+
+ if (parentId === undefined) {
+ this.root.addChild(this);
+ } else {
+ let menuMap = gContextMenuMap.get(this.extension);
+ menuMap.get(parentId).addChild(this);
+ }
+ },
+
+ get parentId() {
+ return this.parent ? this.parent.id : undefined;
+ },
+
+ addChild(child) {
+ if (child.parent) {
+ throw new Error("Child MenuItem already has a parent.");
+ }
+ this.children.push(child);
+ child.parent = this;
+ },
+
+ detachChild(child) {
+ let idx = this.children.indexOf(child);
+ if (idx < 0) {
+ throw new Error("Child MenuItem not found, it cannot be removed.");
+ }
+ this.children.splice(idx, 1);
+ child.parent = null;
+ },
+
+ get root() {
+ let extension = this.extension;
+ if (!gRootItems.has(extension)) {
+ let root = new MenuItem(extension,
+ {title: extension.name},
+ /* isRoot = */ true);
+ gRootItems.set(extension, root);
+ }
+
+ return gRootItems.get(extension);
+ },
+
+ remove() {
+ if (this.parent) {
+ this.parent.detachChild(this);
+ }
+ let children = this.children.slice(0);
+ for (let child of children) {
+ child.remove();
+ }
+
+ let menuMap = gContextMenuMap.get(this.extension);
+ menuMap.delete(this.id);
+ if (this.root == this) {
+ gRootItems.delete(this.extension);
+ }
+ },
+
+ getClickInfo(contextData, wasChecked) {
+ let mediaType;
+ if (contextData.onVideo) {
+ mediaType = "video";
+ }
+ if (contextData.onAudio) {
+ mediaType = "audio";
+ }
+ if (contextData.onImage) {
+ mediaType = "image";
+ }
+
+ let info = {
+ menuItemId: this.id,
+ editable: contextData.onEditableArea,
+ };
+
+ function setIfDefined(argName, value) {
+ if (value !== undefined) {
+ info[argName] = value;
+ }
+ }
+
+ setIfDefined("parentMenuItemId", this.parentId);
+ setIfDefined("mediaType", mediaType);
+ setIfDefined("linkUrl", contextData.linkUrl);
+ setIfDefined("srcUrl", contextData.srcUrl);
+ setIfDefined("pageUrl", contextData.pageUrl);
+ setIfDefined("frameUrl", contextData.frameUrl);
+ setIfDefined("selectionText", contextData.selectionText);
+
+ if ((this.type === "checkbox") || (this.type === "radio")) {
+ info.checked = this.checked;
+ info.wasChecked = wasChecked;
+ }
+
+ return info;
+ },
+
+ enabledForContext(contextData) {
+ let contexts = getContexts(contextData);
+ if (!this.contexts.some(n => contexts.has(n))) {
+ return false;
+ }
+
+ let docPattern = this.documentUrlMatchPattern;
+ let pageURI = Services.io.newURI(contextData.pageUrl, null, null);
+ if (docPattern && !docPattern.matches(pageURI)) {
+ return false;
+ }
+
+ let targetPattern = this.targetUrlMatchPattern;
+ if (targetPattern) {
+ let targetUrls = [];
+ if (contextData.onImage || contextData.onAudio || contextData.onVideo) {
+ // TODO: double check if srcUrl is always set when we need it
+ targetUrls.push(contextData.srcUrl);
+ }
+ if (contextData.onLink) {
+ targetUrls.push(contextData.linkUrl);
+ }
+ if (!targetUrls.some(targetUrl => targetPattern.matches(NetUtil.newURI(targetUrl)))) {
+ return false;
+ }
+ }
+
+ return true;
+ },
+};
+
+var gExtensionCount = 0;
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("startup", (type, extension) => {
+ gContextMenuMap.set(extension, new Map());
+ if (++gExtensionCount == 1) {
+ Services.obs.addObserver(contextMenuObserver,
+ "on-build-contextmenu",
+ false);
+ }
+});
+
+extensions.on("shutdown", (type, extension) => {
+ gContextMenuMap.delete(extension);
+ gRootItems.delete(extension);
+ if (--gExtensionCount == 0) {
+ Services.obs.removeObserver(contextMenuObserver,
+ "on-build-contextmenu");
+ }
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+extensions.registerSchemaAPI("contextMenus", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ contextMenus: {
+ createInternal: function(createProperties) {
+ // Note that the id is required by the schema. If the addon did not set
+ // it, the implementation of contextMenus.create in the child should
+ // have added it.
+ let menuItem = new MenuItem(extension, createProperties);
+ gContextMenuMap.get(extension).set(menuItem.id, menuItem);
+ },
+
+ update: function(id, updateProperties) {
+ let menuItem = gContextMenuMap.get(extension).get(id);
+ if (menuItem) {
+ menuItem.setProps(updateProperties);
+ }
+ },
+
+ remove: function(id) {
+ let menuItem = gContextMenuMap.get(extension).get(id);
+ if (menuItem) {
+ menuItem.remove();
+ }
+ },
+
+ removeAll: function() {
+ let root = gRootItems.get(extension);
+ if (root) {
+ root.remove();
+ }
+ },
+
+ onClicked: new EventManager(context, "contextMenus.onClicked", fire => {
+ let listener = (event, info, tab) => {
+ fire(info, tab);
+ };
+
+ extension.on("webext-contextmenu-menuitem-click", listener);
+ return () => {
+ extension.off("webext-contextmenu-menuitem-click", listener);
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-desktop-runtime.js b/browser/components/extensions/ext-desktop-runtime.js
new file mode 100644
index 000000000..0fdb45562
--- /dev/null
+++ b/browser/components/extensions/ext-desktop-runtime.js
@@ -0,0 +1,26 @@
+"use strict";
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("uninstall", (msg, extension) => {
+ if (extension.uninstallURL) {
+ let browser = WindowManager.topWindow.gBrowser;
+ browser.addTab(extension.uninstallURL, {relatedToCurrent: true});
+ }
+});
+
+global.openOptionsPage = (extension) => {
+ let window = WindowManager.topWindow;
+ if (!window) {
+ return Promise.reject({message: "No browser window available"});
+ }
+
+ if (extension.manifest.options_ui.open_in_tab) {
+ window.switchToTabHavingURI(extension.manifest.options_ui.page, true);
+ return Promise.resolve();
+ }
+
+ let viewId = `addons://detail/${encodeURIComponent(extension.id)}/preferences`;
+
+ return window.BrowserOpenAddonsMgr(viewId);
+};
+
diff --git a/browser/components/extensions/ext-history.js b/browser/components/extensions/ext-history.js
new file mode 100644
index 000000000..a47df1621
--- /dev/null
+++ b/browser/components/extensions/ext-history.js
@@ -0,0 +1,246 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
+ "resource://devtools/shared/event-emitter.js");
+XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
+ "resource://gre/modules/NetUtil.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+ "resource://gre/modules/PlacesUtils.jsm");
+
+const {
+ normalizeTime,
+ SingletonEventManager,
+} = ExtensionUtils;
+
+let nsINavHistoryService = Ci.nsINavHistoryService;
+const TRANSITION_TO_TRANSITION_TYPES_MAP = new Map([
+ ["link", nsINavHistoryService.TRANSITION_LINK],
+ ["typed", nsINavHistoryService.TRANSITION_TYPED],
+ ["auto_bookmark", nsINavHistoryService.TRANSITION_BOOKMARK],
+ ["auto_subframe", nsINavHistoryService.TRANSITION_EMBED],
+ ["manual_subframe", nsINavHistoryService.TRANSITION_FRAMED_LINK],
+]);
+
+let TRANSITION_TYPE_TO_TRANSITIONS_MAP = new Map();
+for (let [transition, transitionType] of TRANSITION_TO_TRANSITION_TYPES_MAP) {
+ TRANSITION_TYPE_TO_TRANSITIONS_MAP.set(transitionType, transition);
+}
+
+function getTransitionType(transition) {
+ // cannot set a default value for the transition argument as the framework sets it to null
+ transition = transition || "link";
+ let transitionType = TRANSITION_TO_TRANSITION_TYPES_MAP.get(transition);
+ if (!transitionType) {
+ throw new Error(`|${transition}| is not a supported transition for history`);
+ }
+ return transitionType;
+}
+
+function getTransition(transitionType) {
+ return TRANSITION_TYPE_TO_TRANSITIONS_MAP.get(transitionType) || "link";
+}
+
+/*
+ * Converts a nsINavHistoryResultNode into a HistoryItem
+ *
+ * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryResultNode
+ */
+function convertNodeToHistoryItem(node) {
+ return {
+ id: node.pageGuid,
+ url: node.uri,
+ title: node.title,
+ lastVisitTime: PlacesUtils.toDate(node.time).getTime(),
+ visitCount: node.accessCount,
+ };
+}
+
+/*
+ * Converts a nsINavHistoryResultNode into a VisitItem
+ *
+ * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryResultNode
+ */
+function convertNodeToVisitItem(node) {
+ return {
+ id: node.pageGuid,
+ visitId: node.visitId,
+ visitTime: PlacesUtils.toDate(node.time).getTime(),
+ referringVisitId: node.fromVisitId,
+ transition: getTransition(node.visitType),
+ };
+}
+
+/*
+ * Converts a nsINavHistoryContainerResultNode into an array of objects
+ *
+ * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryContainerResultNode
+ */
+function convertNavHistoryContainerResultNode(container, converter) {
+ let results = [];
+ container.containerOpen = true;
+ for (let i = 0; i < container.childCount; i++) {
+ let node = container.getChild(i);
+ results.push(converter(node));
+ }
+ container.containerOpen = false;
+ return results;
+}
+
+var _observer;
+
+function getObserver() {
+ if (!_observer) {
+ _observer = {
+ onDeleteURI: function(uri, guid, reason) {
+ this.emit("visitRemoved", {allHistory: false, urls: [uri.spec]});
+ },
+ onVisit: function(uri, visitId, time, sessionId, referringId, transitionType, guid, hidden, visitCount, typed) {
+ let data = {
+ id: guid,
+ url: uri.spec,
+ title: "",
+ lastVisitTime: time / 1000, // time from Places is microseconds,
+ visitCount,
+ typedCount: typed,
+ };
+ this.emit("visited", data);
+ },
+ onBeginUpdateBatch: function() {},
+ onEndUpdateBatch: function() {},
+ onTitleChanged: function() {},
+ onClearHistory: function() {
+ this.emit("visitRemoved", {allHistory: true, urls: []});
+ },
+ onPageChanged: function() {},
+ onFrecencyChanged: function() {},
+ onManyFrecenciesChanged: function() {},
+ onDeleteVisits: function(uri, time, guid, reason) {
+ this.emit("visitRemoved", {allHistory: false, urls: [uri.spec]});
+ },
+ };
+ EventEmitter.decorate(_observer);
+ PlacesUtils.history.addObserver(_observer, false);
+ }
+ return _observer;
+}
+
+extensions.registerSchemaAPI("history", "addon_parent", context => {
+ return {
+ history: {
+ addUrl: function(details) {
+ let transition, date;
+ try {
+ transition = getTransitionType(details.transition);
+ } catch (error) {
+ return Promise.reject({message: error.message});
+ }
+ if (details.visitTime) {
+ date = normalizeTime(details.visitTime);
+ }
+ let pageInfo = {
+ title: details.title,
+ url: details.url,
+ visits: [
+ {
+ transition,
+ date,
+ },
+ ],
+ };
+ try {
+ return PlacesUtils.history.insert(pageInfo).then(() => undefined);
+ } catch (error) {
+ return Promise.reject({message: error.message});
+ }
+ },
+
+ deleteAll: function() {
+ return PlacesUtils.history.clear();
+ },
+
+ deleteRange: function(filter) {
+ let newFilter = {
+ beginDate: normalizeTime(filter.startTime),
+ endDate: normalizeTime(filter.endTime),
+ };
+ // History.removeVisitsByFilter returns a boolean, but our API should return nothing
+ return PlacesUtils.history.removeVisitsByFilter(newFilter).then(() => undefined);
+ },
+
+ deleteUrl: function(details) {
+ let url = details.url;
+ // History.remove returns a boolean, but our API should return nothing
+ return PlacesUtils.history.remove(url).then(() => undefined);
+ },
+
+ search: function(query) {
+ let beginTime = (query.startTime == null) ?
+ PlacesUtils.toPRTime(Date.now() - 24 * 60 * 60 * 1000) :
+ PlacesUtils.toPRTime(normalizeTime(query.startTime));
+ let endTime = (query.endTime == null) ?
+ Number.MAX_VALUE :
+ PlacesUtils.toPRTime(normalizeTime(query.endTime));
+ if (beginTime > endTime) {
+ return Promise.reject({message: "The startTime cannot be after the endTime"});
+ }
+
+ let options = PlacesUtils.history.getNewQueryOptions();
+ options.sortingMode = options.SORT_BY_DATE_DESCENDING;
+ options.maxResults = query.maxResults || 100;
+
+ let historyQuery = PlacesUtils.history.getNewQuery();
+ historyQuery.searchTerms = query.text;
+ historyQuery.beginTime = beginTime;
+ historyQuery.endTime = endTime;
+ let queryResult = PlacesUtils.history.executeQuery(historyQuery, options).root;
+ let results = convertNavHistoryContainerResultNode(queryResult, convertNodeToHistoryItem);
+ return Promise.resolve(results);
+ },
+
+ getVisits: function(details) {
+ let url = details.url;
+ if (!url) {
+ return Promise.reject({message: "A URL must be provided for getVisits"});
+ }
+
+ let options = PlacesUtils.history.getNewQueryOptions();
+ options.sortingMode = options.SORT_BY_DATE_DESCENDING;
+ options.resultType = options.RESULTS_AS_VISIT;
+
+ let historyQuery = PlacesUtils.history.getNewQuery();
+ historyQuery.uri = NetUtil.newURI(url);
+ let queryResult = PlacesUtils.history.executeQuery(historyQuery, options).root;
+ let results = convertNavHistoryContainerResultNode(queryResult, convertNodeToVisitItem);
+ return Promise.resolve(results);
+ },
+
+ onVisited: new SingletonEventManager(context, "history.onVisited", fire => {
+ let listener = (event, data) => {
+ context.runSafe(fire, data);
+ };
+
+ getObserver().on("visited", listener);
+ return () => {
+ getObserver().off("visited", listener);
+ };
+ }).api(),
+
+ onVisitRemoved: new SingletonEventManager(context, "history.onVisitRemoved", fire => {
+ let listener = (event, data) => {
+ context.runSafe(fire, data);
+ };
+
+ getObserver().on("visitRemoved", listener);
+ return () => {
+ getObserver().off("visitRemoved", listener);
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-omnibox.js b/browser/components/extensions/ext-omnibox.js
new file mode 100644
index 000000000..9b2f60ca4
--- /dev/null
+++ b/browser/components/extensions/ext-omnibox.js
@@ -0,0 +1,104 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "ExtensionSearchHandler",
+ "resource://gre/modules/ExtensionSearchHandler.jsm");
+var {
+ SingletonEventManager,
+} = ExtensionUtils;
+
+// WeakMap[extension -> keyword]
+let gKeywordMap = new WeakMap();
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("manifest_omnibox", (type, directive, extension, manifest) => {
+ let keyword = manifest.omnibox.keyword;
+ try {
+ // This will throw if the keyword is already registered.
+ ExtensionSearchHandler.registerKeyword(keyword, extension);
+ gKeywordMap.set(extension, keyword);
+ } catch (e) {
+ extension.manifestError(e.message);
+ }
+});
+
+extensions.on("shutdown", (type, extension) => {
+ let keyword = gKeywordMap.get(extension);
+ if (keyword) {
+ ExtensionSearchHandler.unregisterKeyword(keyword);
+ gKeywordMap.delete(extension);
+ }
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+extensions.registerSchemaAPI("omnibox", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ omnibox: {
+ setDefaultSuggestion(suggestion) {
+ let keyword = gKeywordMap.get(extension);
+ try {
+ // This will throw if the keyword failed to register.
+ ExtensionSearchHandler.setDefaultSuggestion(keyword, suggestion);
+ } catch (e) {
+ return Promise.reject(e.message);
+ }
+ },
+
+ onInputStarted: new SingletonEventManager(context, "omnibox.onInputStarted", fire => {
+ let listener = (eventName) => {
+ fire();
+ };
+ extension.on(ExtensionSearchHandler.MSG_INPUT_STARTED, listener);
+ return () => {
+ extension.off(ExtensionSearchHandler.MSG_INPUT_STARTED, listener);
+ };
+ }).api(),
+
+ onInputCancelled: new SingletonEventManager(context, "omnibox.onInputCancelled", fire => {
+ let listener = (eventName) => {
+ fire();
+ };
+ extension.on(ExtensionSearchHandler.MSG_INPUT_CANCELLED, listener);
+ return () => {
+ extension.off(ExtensionSearchHandler.MSG_INPUT_CANCELLED, listener);
+ };
+ }).api(),
+
+ onInputEntered: new SingletonEventManager(context, "omnibox.onInputEntered", fire => {
+ let listener = (eventName, text, disposition) => {
+ fire(text, disposition);
+ };
+ extension.on(ExtensionSearchHandler.MSG_INPUT_ENTERED, listener);
+ return () => {
+ extension.off(ExtensionSearchHandler.MSG_INPUT_ENTERED, listener);
+ };
+ }).api(),
+ },
+
+ omnibox_internal: {
+ addSuggestions(id, suggestions) {
+ let keyword = gKeywordMap.get(extension);
+ try {
+ ExtensionSearchHandler.addSuggestions(keyword, id, suggestions);
+ } catch (e) {
+ // Silently fail because the extension developer can not know for sure if the user
+ // has already invalidated the callback when asynchronously providing suggestions.
+ }
+ },
+
+ onInputChanged: new SingletonEventManager(context, "omnibox_internal.onInputChanged", fire => {
+ let listener = (eventName, text, id) => {
+ fire(text, id);
+ };
+ extension.on(ExtensionSearchHandler.MSG_INPUT_CHANGED, listener);
+ return () => {
+ extension.off(ExtensionSearchHandler.MSG_INPUT_CHANGED, listener);
+ };
+ }).api(),
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-pageAction.js b/browser/components/extensions/ext-pageAction.js
new file mode 100644
index 000000000..153f05d7a
--- /dev/null
+++ b/browser/components/extensions/ext-pageAction.js
@@ -0,0 +1,287 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/Task.jsm");
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+var {
+ EventManager,
+ IconDetails,
+} = ExtensionUtils;
+
+// WeakMap[Extension -> PageAction]
+var pageActionMap = new WeakMap();
+
+// Handles URL bar icons, including the |page_action| manifest entry
+// and associated API.
+function PageAction(options, extension) {
+ this.extension = extension;
+ this.id = makeWidgetId(extension.id) + "-page-action";
+
+ this.tabManager = TabManager.for(extension);
+
+ this.defaults = {
+ show: false,
+ title: options.default_title || extension.name,
+ icon: IconDetails.normalize({path: options.default_icon}, extension),
+ popup: options.default_popup || "",
+ };
+
+ this.browserStyle = options.browser_style || false;
+ if (options.browser_style === null) {
+ this.extension.logger.warn("Please specify whether you want browser_style " +
+ "or not in your page_action options.");
+ }
+
+ this.tabContext = new TabContext(tab => Object.create(this.defaults),
+ extension);
+
+ this.tabContext.on("location-change", this.handleLocationChange.bind(this)); // eslint-disable-line mozilla/balanced-listeners
+
+ // WeakMap[ChromeWindow -> <xul:image>]
+ this.buttons = new WeakMap();
+
+ EventEmitter.decorate(this);
+}
+
+PageAction.prototype = {
+ // Returns the value of the property |prop| for the given tab, where
+ // |prop| is one of "show", "title", "icon", "popup".
+ getProperty(tab, prop) {
+ return this.tabContext.get(tab)[prop];
+ },
+
+ // Sets the value of the property |prop| for the given tab to the
+ // given value, symmetrically to |getProperty|.
+ //
+ // If |tab| is currently selected, updates the page action button to
+ // reflect the new value.
+ setProperty(tab, prop, value) {
+ if (value != null) {
+ this.tabContext.get(tab)[prop] = value;
+ } else {
+ delete this.tabContext.get(tab)[prop];
+ }
+
+ if (tab.selected) {
+ this.updateButton(tab.ownerGlobal);
+ }
+ },
+
+ // Updates the page action button in the given window to reflect the
+ // properties of the currently selected tab:
+ //
+ // Updates "tooltiptext" and "aria-label" to match "title" property.
+ // Updates "image" to match the "icon" property.
+ // Shows or hides the icon, based on the "show" property.
+ updateButton(window) {
+ let tabData = this.tabContext.get(window.gBrowser.selectedTab);
+
+ if (!(tabData.show || this.buttons.has(window))) {
+ // Don't bother creating a button for a window until it actually
+ // needs to be shown.
+ return;
+ }
+
+ let button = this.getButton(window);
+
+ if (tabData.show) {
+ // Update the title and icon only if the button is visible.
+
+ let title = tabData.title || this.extension.name;
+ button.setAttribute("tooltiptext", title);
+ button.setAttribute("aria-label", title);
+
+ // These URLs should already be properly escaped, but make doubly sure CSS
+ // string escape characters are escaped here, since they could lead to a
+ // sandbox break.
+ let escape = str => str.replace(/[\\\s"]/g, encodeURIComponent);
+
+ let getIcon = size => escape(IconDetails.getPreferredIcon(tabData.icon, this.extension, size).icon);
+
+ button.setAttribute("style", `
+ --webextension-urlbar-image: url("${getIcon(16)}");
+ --webextension-urlbar-image-2x: url("${getIcon(32)}");
+ `);
+
+ button.classList.add("webextension-page-action");
+ }
+
+ button.hidden = !tabData.show;
+ },
+
+ // Create an |image| node and add it to the |urlbar-icons|
+ // container in the given window.
+ addButton(window) {
+ let document = window.document;
+
+ let button = document.createElement("image");
+ button.id = this.id;
+ button.setAttribute("class", "urlbar-icon");
+
+ button.addEventListener("click", event => { // eslint-disable-line mozilla/balanced-listeners
+ if (event.button == 0) {
+ this.handleClick(window);
+ }
+ });
+
+ document.getElementById("urlbar-icons").appendChild(button);
+
+ return button;
+ },
+
+ // Returns the page action button for the given window, creating it if
+ // it doesn't already exist.
+ getButton(window) {
+ if (!this.buttons.has(window)) {
+ let button = this.addButton(window);
+ this.buttons.set(window, button);
+ }
+
+ return this.buttons.get(window);
+ },
+
+ /**
+ * Triggers this page action for the given window, with the same effects as
+ * if it were clicked by a user.
+ *
+ * This has no effect if the page action is hidden for the selected tab.
+ *
+ * @param {Window} window
+ */
+ triggerAction(window) {
+ let pageAction = pageActionMap.get(this.extension);
+ if (pageAction.getProperty(window.gBrowser.selectedTab, "show")) {
+ pageAction.handleClick(window);
+ }
+ },
+
+ // Handles a click event on the page action button for the given
+ // window.
+ // If the page action has a |popup| property, a panel is opened to
+ // that URL. Otherwise, a "click" event is emitted, and dispatched to
+ // the any click listeners in the add-on.
+ handleClick(window) {
+ let tab = window.gBrowser.selectedTab;
+ let popupURL = this.tabContext.get(tab).popup;
+
+ this.tabManager.addActiveTabPermission(tab);
+
+ // If the widget has a popup URL defined, we open a popup, but do not
+ // dispatch a click event to the extension.
+ // If it has no popup URL defined, we dispatch a click event, but do not
+ // open a popup.
+ if (popupURL) {
+ new PanelPopup(this.extension, this.getButton(window), popupURL,
+ this.browserStyle);
+ } else {
+ this.emit("click", tab);
+ }
+ },
+
+ handleLocationChange(eventType, tab, fromBrowse) {
+ if (fromBrowse) {
+ this.tabContext.clear(tab);
+ }
+ this.updateButton(tab.ownerGlobal);
+ },
+
+ shutdown() {
+ this.tabContext.shutdown();
+
+ for (let window of WindowListManager.browserWindows()) {
+ if (this.buttons.has(window)) {
+ this.buttons.get(window).remove();
+ }
+ }
+ },
+};
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("manifest_page_action", (type, directive, extension, manifest) => {
+ let pageAction = new PageAction(manifest.page_action, extension);
+ pageActionMap.set(extension, pageAction);
+});
+
+extensions.on("shutdown", (type, extension) => {
+ if (pageActionMap.has(extension)) {
+ pageActionMap.get(extension).shutdown();
+ pageActionMap.delete(extension);
+ }
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+PageAction.for = extension => {
+ return pageActionMap.get(extension);
+};
+
+global.pageActionFor = PageAction.for;
+
+extensions.registerSchemaAPI("pageAction", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ pageAction: {
+ onClicked: new EventManager(context, "pageAction.onClicked", fire => {
+ let listener = (evt, tab) => {
+ fire(TabManager.convert(extension, tab));
+ };
+ let pageAction = PageAction.for(extension);
+
+ pageAction.on("click", listener);
+ return () => {
+ pageAction.off("click", listener);
+ };
+ }).api(),
+
+ show(tabId) {
+ let tab = TabManager.getTab(tabId, context);
+ PageAction.for(extension).setProperty(tab, "show", true);
+ },
+
+ hide(tabId) {
+ let tab = TabManager.getTab(tabId, context);
+ PageAction.for(extension).setProperty(tab, "show", false);
+ },
+
+ setTitle(details) {
+ let tab = TabManager.getTab(details.tabId, context);
+
+ // Clear the tab-specific title when given a null string.
+ PageAction.for(extension).setProperty(tab, "title", details.title || null);
+ },
+
+ getTitle(details) {
+ let tab = TabManager.getTab(details.tabId, context);
+
+ let title = PageAction.for(extension).getProperty(tab, "title");
+ return Promise.resolve(title);
+ },
+
+ setIcon(details) {
+ let tab = TabManager.getTab(details.tabId, context);
+
+ let icon = IconDetails.normalize(details, extension, context);
+ PageAction.for(extension).setProperty(tab, "icon", icon);
+ },
+
+ setPopup(details) {
+ let tab = TabManager.getTab(details.tabId, context);
+
+ // Note: Chrome resolves arguments to setIcon relative to the calling
+ // context, but resolves arguments to setPopup relative to the extension
+ // root.
+ // For internal consistency, we currently resolve both relative to the
+ // calling context.
+ let url = details.popup && context.uri.resolve(details.popup);
+ PageAction.for(extension).setProperty(tab, "popup", url);
+ },
+
+ getPopup(details) {
+ let tab = TabManager.getTab(details.tabId, context);
+
+ let popup = PageAction.for(extension).getProperty(tab, "popup");
+ return Promise.resolve(popup);
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-sessions.js b/browser/components/extensions/ext-sessions.js
new file mode 100644
index 000000000..4c13a1ac3
--- /dev/null
+++ b/browser/components/extensions/ext-sessions.js
@@ -0,0 +1,92 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+var {
+ promiseObserved,
+} = ExtensionUtils;
+
+XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
+ "resource:///modules/sessionstore/SessionStore.jsm");
+
+function getRecentlyClosed(maxResults, extension) {
+ let recentlyClosed = [];
+
+ // Get closed windows
+ let closedWindowData = SessionStore.getClosedWindowData(false);
+ for (let window of closedWindowData) {
+ recentlyClosed.push({
+ lastModified: window.closedAt,
+ window: WindowManager.convertFromSessionStoreClosedData(window, extension)});
+ }
+
+ // Get closed tabs
+ for (let window of WindowListManager.browserWindows()) {
+ let closedTabData = SessionStore.getClosedTabData(window, false);
+ for (let tab of closedTabData) {
+ recentlyClosed.push({
+ lastModified: tab.closedAt,
+ tab: TabManager.for(extension).convertFromSessionStoreClosedData(tab, window)});
+ }
+ }
+
+ // Sort windows and tabs
+ recentlyClosed.sort((a, b) => b.lastModified - a.lastModified);
+ return recentlyClosed.slice(0, maxResults);
+}
+
+function createSession(restored, extension, sessionId) {
+ if (!restored) {
+ return Promise.reject({message: `Could not restore object using sessionId ${sessionId}.`});
+ }
+ let sessionObj = {lastModified: Date.now()};
+ if (restored instanceof Ci.nsIDOMChromeWindow) {
+ return promiseObserved("sessionstore-single-window-restored", subject => subject == restored).then(() => {
+ sessionObj.window = WindowManager.convert(extension, restored, {populate: true});
+ return Promise.resolve([sessionObj]);
+ });
+ }
+ sessionObj.tab = TabManager.for(extension).convert(restored);
+ return Promise.resolve([sessionObj]);
+}
+
+extensions.registerSchemaAPI("sessions", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ sessions: {
+ getRecentlyClosed: function(filter) {
+ let maxResults = filter.maxResults == undefined ? this.MAX_SESSION_RESULTS : filter.maxResults;
+ return Promise.resolve(getRecentlyClosed(maxResults, extension));
+ },
+ restore: function(sessionId) {
+ let session, closedId;
+ if (sessionId) {
+ closedId = sessionId;
+ session = SessionStore.undoCloseById(closedId);
+ } else if (SessionStore.lastClosedObjectType == "window") {
+ // If the most recently closed object is a window, just undo closing the most recent window.
+ session = SessionStore.undoCloseWindow(0);
+ } else {
+ // It is a tab, and we cannot call SessionStore.undoCloseTab without a window,
+ // so we must find the tab in which case we can just use its closedId.
+ let recentlyClosedTabs = [];
+ for (let window of WindowListManager.browserWindows()) {
+ let closedTabData = SessionStore.getClosedTabData(window, false);
+ for (let tab of closedTabData) {
+ recentlyClosedTabs.push(tab);
+ }
+ }
+
+ // Sort the tabs.
+ recentlyClosedTabs.sort((a, b) => b.closedAt - a.closedAt);
+
+ // Use the closedId of the most recently closed tab to restore it.
+ closedId = recentlyClosedTabs[0].closedId;
+ session = SessionStore.undoCloseById(closedId);
+ }
+ return createSession(session, extension, closedId);
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/ext-tabs.js b/browser/components/extensions/ext-tabs.js
new file mode 100644
index 000000000..bb575aaab
--- /dev/null
+++ b/browser/components/extensions/ext-tabs.js
@@ -0,0 +1,1093 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
+ "@mozilla.org/browser/aboutnewtab-service;1",
+ "nsIAboutNewTabService");
+
+XPCOMUtils.defineLazyModuleGetter(this, "MatchPattern",
+ "resource://gre/modules/MatchPattern.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
+ "resource://gre/modules/PrivateBrowsingUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PromiseUtils",
+ "resource://gre/modules/PromiseUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+ "resource://gre/modules/Services.jsm");
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+
+var {
+ EventManager,
+ ignoreEvent,
+} = ExtensionUtils;
+
+// This function is pretty tightly tied to Extension.jsm.
+// Its job is to fill in the |tab| property of the sender.
+function getSender(extension, target, sender) {
+ if ("tabId" in sender) {
+ // The message came from an ExtensionContext. In that case, it should
+ // include a tabId property (which is filled in by the page-open
+ // listener below).
+ let tab = TabManager.getTab(sender.tabId, null, null);
+ delete sender.tabId;
+ if (tab) {
+ sender.tab = TabManager.convert(extension, tab);
+ return;
+ }
+ }
+ if (target instanceof Ci.nsIDOMXULElement) {
+ // If the message was sent from a content script to a <browser> element,
+ // then we can just get the `tab` from `target`.
+ let tabbrowser = target.ownerGlobal.gBrowser;
+ if (tabbrowser) {
+ let tab = tabbrowser.getTabForBrowser(target);
+
+ // `tab` can be `undefined`, e.g. for extension popups. This condition is
+ // reached if `getSender` is called for a popup without a valid `tabId`.
+ if (tab) {
+ sender.tab = TabManager.convert(extension, tab);
+ }
+ }
+ }
+}
+
+// Used by Extension.jsm
+global.tabGetSender = getSender;
+
+/* eslint-disable mozilla/balanced-listeners */
+
+extensions.on("page-shutdown", (type, context) => {
+ if (context.viewType == "tab") {
+ if (context.extension.id !== context.xulBrowser.contentPrincipal.addonId) {
+ // Only close extension tabs.
+ // This check prevents about:addons from closing when it contains a
+ // WebExtension as an embedded inline options page.
+ return;
+ }
+ let {gBrowser} = context.xulBrowser.ownerGlobal;
+ if (gBrowser) {
+ let tab = gBrowser.getTabForBrowser(context.xulBrowser);
+ if (tab) {
+ gBrowser.removeTab(tab);
+ }
+ }
+ }
+});
+
+extensions.on("fill-browser-data", (type, browser, data) => {
+ data.tabId = browser ? TabManager.getBrowserId(browser) : -1;
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+global.currentWindow = function(context) {
+ let {xulWindow} = context;
+ if (xulWindow && context.viewType != "background") {
+ return xulWindow;
+ }
+ return WindowManager.topWindow;
+};
+
+let tabListener = {
+ init() {
+ if (this.initialized) {
+ return;
+ }
+
+ this.adoptedTabs = new WeakMap();
+
+ this.handleWindowOpen = this.handleWindowOpen.bind(this);
+ this.handleWindowClose = this.handleWindowClose.bind(this);
+
+ AllWindowEvents.addListener("TabClose", this);
+ AllWindowEvents.addListener("TabOpen", this);
+ WindowListManager.addOpenListener(this.handleWindowOpen);
+ WindowListManager.addCloseListener(this.handleWindowClose);
+
+ EventEmitter.decorate(this);
+
+ this.initialized = true;
+ },
+
+ handleEvent(event) {
+ switch (event.type) {
+ case "TabOpen":
+ if (event.detail.adoptedTab) {
+ this.adoptedTabs.set(event.detail.adoptedTab, event.target);
+ }
+
+ // We need to delay sending this event until the next tick, since the
+ // tab does not have its final index when the TabOpen event is dispatched.
+ Promise.resolve().then(() => {
+ if (event.detail.adoptedTab) {
+ this.emitAttached(event.originalTarget);
+ } else {
+ this.emitCreated(event.originalTarget);
+ }
+ });
+ break;
+
+ case "TabClose":
+ let tab = event.originalTarget;
+
+ if (event.detail.adoptedBy) {
+ this.emitDetached(tab, event.detail.adoptedBy);
+ } else {
+ this.emitRemoved(tab, false);
+ }
+ break;
+ }
+ },
+
+ handleWindowOpen(window) {
+ if (window.arguments[0] instanceof window.XULElement) {
+ // If the first window argument is a XUL element, it means the
+ // window is about to adopt a tab from another window to replace its
+ // initial tab.
+ //
+ // Note that this event handler depends on running before the
+ // delayed startup code in browser.js, which is currently triggered
+ // by the first MozAfterPaint event. That code handles finally
+ // adopting the tab, and clears it from the arguments list in the
+ // process, so if we run later than it, we're too late.
+ let tab = window.arguments[0];
+ this.adoptedTabs.set(tab, window.gBrowser.tabs[0]);
+
+ // We need to be sure to fire this event after the onDetached event
+ // for the original tab.
+ let listener = (event, details) => {
+ if (details.tab == tab) {
+ this.off("tab-detached", listener);
+
+ Promise.resolve().then(() => {
+ this.emitAttached(details.adoptedBy);
+ });
+ }
+ };
+
+ this.on("tab-detached", listener);
+ } else {
+ for (let tab of window.gBrowser.tabs) {
+ this.emitCreated(tab);
+ }
+ }
+ },
+
+ handleWindowClose(window) {
+ for (let tab of window.gBrowser.tabs) {
+ if (this.adoptedTabs.has(tab)) {
+ this.emitDetached(tab, this.adoptedTabs.get(tab));
+ } else {
+ this.emitRemoved(tab, true);
+ }
+ }
+ },
+
+ emitAttached(tab) {
+ let newWindowId = WindowManager.getId(tab.ownerGlobal);
+ let tabId = TabManager.getId(tab);
+
+ this.emit("tab-attached", {tab, tabId, newWindowId, newPosition: tab._tPos});
+ },
+
+ emitDetached(tab, adoptedBy) {
+ let oldWindowId = WindowManager.getId(tab.ownerGlobal);
+ let tabId = TabManager.getId(tab);
+
+ this.emit("tab-detached", {tab, adoptedBy, tabId, oldWindowId, oldPosition: tab._tPos});
+ },
+
+ emitCreated(tab) {
+ this.emit("tab-created", {tab});
+ },
+
+ emitRemoved(tab, isWindowClosing) {
+ let windowId = WindowManager.getId(tab.ownerGlobal);
+ let tabId = TabManager.getId(tab);
+
+ // When addons run in-process, `window.close()` is synchronous. Most other
+ // addon-invoked calls are asynchronous since they go through a proxy
+ // context via the message manager. This includes event registrations such
+ // as `tabs.onRemoved.addListener`.
+ // So, even if `window.close()` were to be called (in-process) after calling
+ // `tabs.onRemoved.addListener`, then the tab would be closed before the
+ // event listener is registered. To make sure that the event listener is
+ // notified, we dispatch `tabs.onRemoved` asynchronously.
+ Services.tm.mainThread.dispatch(() => {
+ this.emit("tab-removed", {tab, tabId, windowId, isWindowClosing});
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ tabReadyInitialized: false,
+ tabReadyPromises: new WeakMap(),
+ initializingTabs: new WeakSet(),
+
+ initTabReady() {
+ if (!this.tabReadyInitialized) {
+ AllWindowEvents.addListener("progress", this);
+
+ this.tabReadyInitialized = true;
+ }
+ },
+
+ onLocationChange(browser, webProgress, request, locationURI, flags) {
+ if (webProgress.isTopLevel) {
+ let gBrowser = browser.ownerGlobal.gBrowser;
+ let tab = gBrowser.getTabForBrowser(browser);
+
+ // Now we are certain that the first page in the tab was loaded.
+ this.initializingTabs.delete(tab);
+
+ // browser.innerWindowID is now set, resolve the promises if any.
+ let deferred = this.tabReadyPromises.get(tab);
+ if (deferred) {
+ deferred.resolve(tab);
+ this.tabReadyPromises.delete(tab);
+ }
+ }
+ },
+
+ /**
+ * Returns a promise that resolves when the tab is ready.
+ * Tabs created via the `tabs.create` method are "ready" once the location
+ * changes to the requested URL. Other tabs are assumed to be ready once their
+ * inner window ID is known.
+ *
+ * @param {XULElement} tab The <tab> element.
+ * @returns {Promise} Resolves with the given tab once ready.
+ */
+ awaitTabReady(tab) {
+ let deferred = this.tabReadyPromises.get(tab);
+ if (!deferred) {
+ deferred = PromiseUtils.defer();
+ if (!this.initializingTabs.has(tab) && tab.linkedBrowser.innerWindowID) {
+ deferred.resolve(tab);
+ } else {
+ this.initTabReady();
+ this.tabReadyPromises.set(tab, deferred);
+ }
+ }
+ return deferred.promise;
+ },
+};
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("startup", () => {
+ tabListener.init();
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+extensions.registerSchemaAPI("tabs", "addon_parent", context => {
+ let {extension} = context;
+ let self = {
+ tabs: {
+ onActivated: new WindowEventManager(context, "tabs.onActivated", "TabSelect", (fire, event) => {
+ let tab = event.originalTarget;
+ let tabId = TabManager.getId(tab);
+ let windowId = WindowManager.getId(tab.ownerGlobal);
+ fire({tabId, windowId});
+ }).api(),
+
+ onCreated: new EventManager(context, "tabs.onCreated", fire => {
+ let listener = (eventName, event) => {
+ fire(TabManager.convert(extension, event.tab));
+ };
+
+ tabListener.on("tab-created", listener);
+ return () => {
+ tabListener.off("tab-created", listener);
+ };
+ }).api(),
+
+ /**
+ * Since multiple tabs currently can't be highlighted, onHighlighted
+ * essentially acts an alias for self.tabs.onActivated but returns
+ * the tabId in an array to match the API.
+ * @see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/onHighlighted
+ */
+ onHighlighted: new WindowEventManager(context, "tabs.onHighlighted", "TabSelect", (fire, event) => {
+ let tab = event.originalTarget;
+ let tabIds = [TabManager.getId(tab)];
+ let windowId = WindowManager.getId(tab.ownerGlobal);
+ fire({tabIds, windowId});
+ }).api(),
+
+ onAttached: new EventManager(context, "tabs.onAttached", fire => {
+ let listener = (eventName, event) => {
+ fire(event.tabId, {newWindowId: event.newWindowId, newPosition: event.newPosition});
+ };
+
+ tabListener.on("tab-attached", listener);
+ return () => {
+ tabListener.off("tab-attached", listener);
+ };
+ }).api(),
+
+ onDetached: new EventManager(context, "tabs.onDetached", fire => {
+ let listener = (eventName, event) => {
+ fire(event.tabId, {oldWindowId: event.oldWindowId, oldPosition: event.oldPosition});
+ };
+
+ tabListener.on("tab-detached", listener);
+ return () => {
+ tabListener.off("tab-detached", listener);
+ };
+ }).api(),
+
+ onRemoved: new EventManager(context, "tabs.onRemoved", fire => {
+ let listener = (eventName, event) => {
+ fire(event.tabId, {windowId: event.windowId, isWindowClosing: event.isWindowClosing});
+ };
+
+ tabListener.on("tab-removed", listener);
+ return () => {
+ tabListener.off("tab-removed", listener);
+ };
+ }).api(),
+
+ onReplaced: ignoreEvent(context, "tabs.onReplaced"),
+
+ onMoved: new EventManager(context, "tabs.onMoved", fire => {
+ // There are certain circumstances where we need to ignore a move event.
+ //
+ // Namely, the first time the tab is moved after it's created, we need
+ // to report the final position as the initial position in the tab's
+ // onAttached or onCreated event. This is because most tabs are inserted
+ // in a temporary location and then moved after the TabOpen event fires,
+ // which generates a TabOpen event followed by a TabMove event, which
+ // does not match the contract of our API.
+ let ignoreNextMove = new WeakSet();
+
+ let openListener = event => {
+ ignoreNextMove.add(event.target);
+ // Remove the tab from the set on the next tick, since it will already
+ // have been moved by then.
+ Promise.resolve().then(() => {
+ ignoreNextMove.delete(event.target);
+ });
+ };
+
+ let moveListener = event => {
+ let tab = event.originalTarget;
+
+ if (ignoreNextMove.has(tab)) {
+ ignoreNextMove.delete(tab);
+ return;
+ }
+
+ fire(TabManager.getId(tab), {
+ windowId: WindowManager.getId(tab.ownerGlobal),
+ fromIndex: event.detail,
+ toIndex: tab._tPos,
+ });
+ };
+
+ AllWindowEvents.addListener("TabMove", moveListener);
+ AllWindowEvents.addListener("TabOpen", openListener);
+ return () => {
+ AllWindowEvents.removeListener("TabMove", moveListener);
+ AllWindowEvents.removeListener("TabOpen", openListener);
+ };
+ }).api(),
+
+ onUpdated: new EventManager(context, "tabs.onUpdated", fire => {
+ function sanitize(extension, changeInfo) {
+ let result = {};
+ let nonempty = false;
+ for (let prop in changeInfo) {
+ if ((prop != "favIconUrl" && prop != "url") || extension.hasPermission("tabs")) {
+ nonempty = true;
+ result[prop] = changeInfo[prop];
+ }
+ }
+ return [nonempty, result];
+ }
+
+ let fireForBrowser = (browser, changed) => {
+ let [needed, changeInfo] = sanitize(extension, changed);
+ if (needed) {
+ let gBrowser = browser.ownerGlobal.gBrowser;
+ let tabElem = gBrowser.getTabForBrowser(browser);
+
+ let tab = TabManager.convert(extension, tabElem);
+ fire(tab.id, changeInfo, tab);
+ }
+ };
+
+ let listener = event => {
+ let needed = [];
+ if (event.type == "TabAttrModified") {
+ let changed = event.detail.changed;
+ if (changed.includes("image")) {
+ needed.push("favIconUrl");
+ }
+ if (changed.includes("muted")) {
+ needed.push("mutedInfo");
+ }
+ if (changed.includes("soundplaying")) {
+ needed.push("audible");
+ }
+ } else if (event.type == "TabPinned") {
+ needed.push("pinned");
+ } else if (event.type == "TabUnpinned") {
+ needed.push("pinned");
+ }
+
+ if (needed.length && !extension.hasPermission("tabs")) {
+ needed = needed.filter(attr => attr != "url" && attr != "favIconUrl");
+ }
+
+ if (needed.length) {
+ let tab = TabManager.convert(extension, event.originalTarget);
+
+ let changeInfo = {};
+ for (let prop of needed) {
+ changeInfo[prop] = tab[prop];
+ }
+ fire(tab.id, changeInfo, tab);
+ }
+ };
+ let progressListener = {
+ onStateChange(browser, webProgress, request, stateFlags, statusCode) {
+ if (!webProgress.isTopLevel) {
+ return;
+ }
+
+ let status;
+ if (stateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW) {
+ if (stateFlags & Ci.nsIWebProgressListener.STATE_START) {
+ status = "loading";
+ } else if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
+ status = "complete";
+ }
+ } else if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
+ statusCode == Cr.NS_BINDING_ABORTED) {
+ status = "complete";
+ }
+
+ fireForBrowser(browser, {status});
+ },
+
+ onLocationChange(browser, webProgress, request, locationURI, flags) {
+ if (!webProgress.isTopLevel) {
+ return;
+ }
+
+ fireForBrowser(browser, {
+ status: webProgress.isLoadingDocument ? "loading" : "complete",
+ url: locationURI.spec,
+ });
+ },
+ };
+
+ AllWindowEvents.addListener("progress", progressListener);
+ AllWindowEvents.addListener("TabAttrModified", listener);
+ AllWindowEvents.addListener("TabPinned", listener);
+ AllWindowEvents.addListener("TabUnpinned", listener);
+
+ return () => {
+ AllWindowEvents.removeListener("progress", progressListener);
+ AllWindowEvents.removeListener("TabAttrModified", listener);
+ AllWindowEvents.removeListener("TabPinned", listener);
+ AllWindowEvents.removeListener("TabUnpinned", listener);
+ };
+ }).api(),
+
+ create: function(createProperties) {
+ return new Promise((resolve, reject) => {
+ let window = createProperties.windowId !== null ?
+ WindowManager.getWindow(createProperties.windowId, context) :
+ WindowManager.topWindow;
+ if (!window.gBrowser) {
+ let obs = (finishedWindow, topic, data) => {
+ if (finishedWindow != window) {
+ return;
+ }
+ Services.obs.removeObserver(obs, "browser-delayed-startup-finished");
+ resolve(window);
+ };
+ Services.obs.addObserver(obs, "browser-delayed-startup-finished", false);
+ } else {
+ resolve(window);
+ }
+ }).then(window => {
+ let url;
+
+ if (createProperties.url !== null) {
+ url = context.uri.resolve(createProperties.url);
+
+ if (!context.checkLoadURL(url, {dontReportErrors: true})) {
+ return Promise.reject({message: `Illegal URL: ${url}`});
+ }
+ }
+
+ if (createProperties.cookieStoreId && !extension.hasPermission("cookies")) {
+ return Promise.reject({message: `No permission for cookieStoreId: ${createProperties.cookieStoreId}`});
+ }
+
+ let options = {};
+ if (createProperties.cookieStoreId) {
+ if (!global.isValidCookieStoreId(createProperties.cookieStoreId)) {
+ return Promise.reject({message: `Illegal cookieStoreId: ${createProperties.cookieStoreId}`});
+ }
+
+ let privateWindow = PrivateBrowsingUtils.isBrowserPrivate(window.gBrowser);
+ if (privateWindow && !global.isPrivateCookieStoreId(createProperties.cookieStoreId)) {
+ return Promise.reject({message: `Illegal to set non-private cookieStorageId in a private window`});
+ }
+
+ if (!privateWindow && global.isPrivateCookieStoreId(createProperties.cookieStoreId)) {
+ return Promise.reject({message: `Illegal to set private cookieStorageId in a non-private window`});
+ }
+
+ if (global.isContainerCookieStoreId(createProperties.cookieStoreId)) {
+ let containerId = global.getContainerForCookieStoreId(createProperties.cookieStoreId);
+ if (!containerId) {
+ return Promise.reject({message: `No cookie store exists with ID ${createProperties.cookieStoreId}`});
+ }
+
+ options.userContextId = containerId;
+ }
+ }
+
+ tabListener.initTabReady();
+ let tab = window.gBrowser.addTab(url || window.BROWSER_NEW_TAB_URL, options);
+
+ let active = true;
+ if (createProperties.active !== null) {
+ active = createProperties.active;
+ }
+ if (active) {
+ window.gBrowser.selectedTab = tab;
+ }
+
+ if (createProperties.index !== null) {
+ window.gBrowser.moveTabTo(tab, createProperties.index);
+ }
+
+ if (createProperties.pinned) {
+ window.gBrowser.pinTab(tab);
+ }
+
+ if (createProperties.url && !createProperties.url.startsWith("about:")) {
+ // We can't wait for a location change event for about:newtab,
+ // since it may be pre-rendered, in which case its initial
+ // location change event has already fired.
+
+ // Mark the tab as initializing, so that operations like
+ // `executeScript` wait until the requested URL is loaded in
+ // the tab before dispatching messages to the inner window
+ // that contains the URL we're attempting to load.
+ tabListener.initializingTabs.add(tab);
+ }
+
+ return TabManager.convert(extension, tab);
+ });
+ },
+
+ remove: function(tabs) {
+ if (!Array.isArray(tabs)) {
+ tabs = [tabs];
+ }
+
+ for (let tabId of tabs) {
+ let tab = TabManager.getTab(tabId, context);
+ tab.ownerGlobal.gBrowser.removeTab(tab);
+ }
+
+ return Promise.resolve();
+ },
+
+ update: function(tabId, updateProperties) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let tabbrowser = tab.ownerGlobal.gBrowser;
+
+ if (updateProperties.url !== null) {
+ let url = context.uri.resolve(updateProperties.url);
+
+ if (!context.checkLoadURL(url, {dontReportErrors: true})) {
+ return Promise.reject({message: `Illegal URL: ${url}`});
+ }
+
+ tab.linkedBrowser.loadURI(url);
+ }
+
+ if (updateProperties.active !== null) {
+ if (updateProperties.active) {
+ tabbrowser.selectedTab = tab;
+ } else {
+ // Not sure what to do here? Which tab should we select?
+ }
+ }
+ if (updateProperties.muted !== null) {
+ if (tab.muted != updateProperties.muted) {
+ tab.toggleMuteAudio(extension.uuid);
+ }
+ }
+ if (updateProperties.pinned !== null) {
+ if (updateProperties.pinned) {
+ tabbrowser.pinTab(tab);
+ } else {
+ tabbrowser.unpinTab(tab);
+ }
+ }
+ // FIXME: highlighted/selected, openerTabId
+
+ return Promise.resolve(TabManager.convert(extension, tab));
+ },
+
+ reload: function(tabId, reloadProperties) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
+ if (reloadProperties && reloadProperties.bypassCache) {
+ flags |= Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
+ }
+ tab.linkedBrowser.reloadWithFlags(flags);
+
+ return Promise.resolve();
+ },
+
+ get: function(tabId) {
+ let tab = TabManager.getTab(tabId, context);
+
+ return Promise.resolve(TabManager.convert(extension, tab));
+ },
+
+ getCurrent() {
+ let tab;
+ if (context.tabId) {
+ tab = TabManager.convert(extension, TabManager.getTab(context.tabId, context));
+ }
+ return Promise.resolve(tab);
+ },
+
+ query: function(queryInfo) {
+ let pattern = null;
+ if (queryInfo.url !== null) {
+ if (!extension.hasPermission("tabs")) {
+ return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" parameter'});
+ }
+
+ pattern = new MatchPattern(queryInfo.url);
+ }
+
+ function matches(tab) {
+ let props = ["active", "pinned", "highlighted", "status", "title", "index"];
+ for (let prop of props) {
+ if (queryInfo[prop] !== null && queryInfo[prop] != tab[prop]) {
+ return false;
+ }
+ }
+
+ if (queryInfo.audible !== null) {
+ if (queryInfo.audible != tab.audible) {
+ return false;
+ }
+ }
+
+ if (queryInfo.muted !== null) {
+ if (queryInfo.muted != tab.mutedInfo.muted) {
+ return false;
+ }
+ }
+
+ if (queryInfo.cookieStoreId !== null &&
+ tab.cookieStoreId != queryInfo.cookieStoreId) {
+ return false;
+ }
+
+ if (pattern && !pattern.matches(Services.io.newURI(tab.url, null, null))) {
+ return false;
+ }
+
+ return true;
+ }
+
+ let result = [];
+ for (let window of WindowListManager.browserWindows()) {
+ let lastFocused = window === WindowManager.topWindow;
+ if (queryInfo.lastFocusedWindow !== null && queryInfo.lastFocusedWindow !== lastFocused) {
+ continue;
+ }
+
+ let windowType = WindowManager.windowType(window);
+ if (queryInfo.windowType !== null && queryInfo.windowType !== windowType) {
+ continue;
+ }
+
+ if (queryInfo.windowId !== null) {
+ if (queryInfo.windowId === WindowManager.WINDOW_ID_CURRENT) {
+ if (currentWindow(context) !== window) {
+ continue;
+ }
+ } else if (queryInfo.windowId !== WindowManager.getId(window)) {
+ continue;
+ }
+ }
+
+ if (queryInfo.currentWindow !== null) {
+ let eq = window === currentWindow(context);
+ if (queryInfo.currentWindow != eq) {
+ continue;
+ }
+ }
+
+ let tabs = TabManager.for(extension).getTabs(window);
+ for (let tab of tabs) {
+ if (matches(tab)) {
+ result.push(tab);
+ }
+ }
+ }
+ return Promise.resolve(result);
+ },
+
+ captureVisibleTab: function(windowId, options) {
+ if (!extension.hasPermission("<all_urls>")) {
+ return Promise.reject({message: "The <all_urls> permission is required to use the captureVisibleTab API"});
+ }
+
+ let window = windowId == null ?
+ WindowManager.topWindow :
+ WindowManager.getWindow(windowId, context);
+
+ let tab = window.gBrowser.selectedTab;
+ return tabListener.awaitTabReady(tab).then(() => {
+ let browser = tab.linkedBrowser;
+ let recipient = {
+ innerWindowID: browser.innerWindowID,
+ };
+
+ if (!options) {
+ options = {};
+ }
+ if (options.format == null) {
+ options.format = "png";
+ }
+ if (options.quality == null) {
+ options.quality = 92;
+ }
+
+ let message = {
+ options,
+ width: browser.clientWidth,
+ height: browser.clientHeight,
+ };
+
+ return context.sendMessage(browser.messageManager, "Extension:Capture",
+ message, {recipient});
+ });
+ },
+
+ detectLanguage: function(tabId) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ return tabListener.awaitTabReady(tab).then(() => {
+ let browser = tab.linkedBrowser;
+ let recipient = {innerWindowID: browser.innerWindowID};
+
+ return context.sendMessage(browser.messageManager, "Extension:DetectLanguage",
+ {}, {recipient});
+ });
+ },
+
+ // Used to executeScript, insertCSS and removeCSS.
+ _execute: function(tabId, details, kind, method) {
+ let tab = tabId !== null ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let options = {
+ js: [],
+ css: [],
+ remove_css: method == "removeCSS",
+ };
+
+ // We require a `code` or a `file` property, but we can't accept both.
+ if ((details.code === null) == (details.file === null)) {
+ return Promise.reject({message: `${method} requires either a 'code' or a 'file' property, but not both`});
+ }
+
+ if (details.frameId !== null && details.allFrames) {
+ return Promise.reject({message: `'frameId' and 'allFrames' are mutually exclusive`});
+ }
+
+ if (TabManager.for(extension).hasActiveTabPermission(tab)) {
+ // If we have the "activeTab" permission for this tab, ignore
+ // the host whitelist.
+ options.matchesHost = ["<all_urls>"];
+ } else {
+ options.matchesHost = extension.whiteListedHosts.serialize();
+ }
+
+ if (details.code !== null) {
+ options[kind + "Code"] = details.code;
+ }
+ if (details.file !== null) {
+ let url = context.uri.resolve(details.file);
+ if (!extension.isExtensionURL(url)) {
+ return Promise.reject({message: "Files to be injected must be within the extension"});
+ }
+ options[kind].push(url);
+ }
+ if (details.allFrames) {
+ options.all_frames = details.allFrames;
+ }
+ if (details.frameId !== null) {
+ options.frame_id = details.frameId;
+ }
+ if (details.matchAboutBlank) {
+ options.match_about_blank = details.matchAboutBlank;
+ }
+ if (details.runAt !== null) {
+ options.run_at = details.runAt;
+ } else {
+ options.run_at = "document_idle";
+ }
+
+ return tabListener.awaitTabReady(tab).then(() => {
+ let browser = tab.linkedBrowser;
+ let recipient = {
+ innerWindowID: browser.innerWindowID,
+ };
+
+ return context.sendMessage(browser.messageManager, "Extension:Execute", {options}, {recipient});
+ });
+ },
+
+ executeScript: function(tabId, details) {
+ return self.tabs._execute(tabId, details, "js", "executeScript");
+ },
+
+ insertCSS: function(tabId, details) {
+ return self.tabs._execute(tabId, details, "css", "insertCSS").then(() => {});
+ },
+
+ removeCSS: function(tabId, details) {
+ return self.tabs._execute(tabId, details, "css", "removeCSS").then(() => {});
+ },
+
+ move: function(tabIds, moveProperties) {
+ let index = moveProperties.index;
+ let tabsMoved = [];
+ if (!Array.isArray(tabIds)) {
+ tabIds = [tabIds];
+ }
+
+ let destinationWindow = null;
+ if (moveProperties.windowId !== null) {
+ destinationWindow = WindowManager.getWindow(moveProperties.windowId, context);
+ // Fail on an invalid window.
+ if (!destinationWindow) {
+ return Promise.reject({message: `Invalid window ID: ${moveProperties.windowId}`});
+ }
+ }
+
+ /*
+ Indexes are maintained on a per window basis so that a call to
+ move([tabA, tabB], {index: 0})
+ -> tabA to 0, tabB to 1 if tabA and tabB are in the same window
+ move([tabA, tabB], {index: 0})
+ -> tabA to 0, tabB to 0 if tabA and tabB are in different windows
+ */
+ let indexMap = new Map();
+
+ let tabs = tabIds.map(tabId => TabManager.getTab(tabId, context));
+ for (let tab of tabs) {
+ // If the window is not specified, use the window from the tab.
+ let window = destinationWindow || tab.ownerGlobal;
+ let gBrowser = window.gBrowser;
+
+ let insertionPoint = indexMap.get(window) || index;
+ // If the index is -1 it should go to the end of the tabs.
+ if (insertionPoint == -1) {
+ insertionPoint = gBrowser.tabs.length;
+ }
+
+ // We can only move pinned tabs to a point within, or just after,
+ // the current set of pinned tabs. Unpinned tabs, likewise, can only
+ // be moved to a position after the current set of pinned tabs.
+ // Attempts to move a tab to an illegal position are ignored.
+ let numPinned = gBrowser._numPinnedTabs;
+ let ok = tab.pinned ? insertionPoint <= numPinned : insertionPoint >= numPinned;
+ if (!ok) {
+ continue;
+ }
+
+ indexMap.set(window, insertionPoint + 1);
+
+ if (tab.ownerGlobal != window) {
+ // If the window we are moving the tab in is different, then move the tab
+ // to the new window.
+ tab = gBrowser.adoptTab(tab, insertionPoint, false);
+ } else {
+ // If the window we are moving is the same, just move the tab.
+ gBrowser.moveTabTo(tab, insertionPoint);
+ }
+ tabsMoved.push(tab);
+ }
+
+ return Promise.resolve(tabsMoved.map(tab => TabManager.convert(extension, tab)));
+ },
+
+ duplicate: function(tabId) {
+ let tab = TabManager.getTab(tabId, context);
+
+ let gBrowser = tab.ownerGlobal.gBrowser;
+ let newTab = gBrowser.duplicateTab(tab);
+
+ return new Promise(resolve => {
+ // We need to use SSTabRestoring because any attributes set before
+ // are ignored. SSTabRestored is too late and results in a jump in
+ // the UI. See http://bit.ly/session-store-api for more information.
+ newTab.addEventListener("SSTabRestoring", function listener() {
+ // As the tab is restoring, move it to the correct position.
+ newTab.removeEventListener("SSTabRestoring", listener);
+ // Pinned tabs that are duplicated are inserted
+ // after the existing pinned tab and pinned.
+ if (tab.pinned) {
+ gBrowser.pinTab(newTab);
+ }
+ gBrowser.moveTabTo(newTab, tab._tPos + 1);
+ });
+
+ newTab.addEventListener("SSTabRestored", function listener() {
+ // Once it has been restored, select it and return the promise.
+ newTab.removeEventListener("SSTabRestored", listener);
+ gBrowser.selectedTab = newTab;
+ return resolve(TabManager.convert(extension, newTab));
+ });
+ });
+ },
+
+ getZoom(tabId) {
+ let tab = tabId ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let {ZoomManager} = tab.ownerGlobal;
+ let zoom = ZoomManager.getZoomForBrowser(tab.linkedBrowser);
+
+ return Promise.resolve(zoom);
+ },
+
+ setZoom(tabId, zoom) {
+ let tab = tabId ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let {FullZoom, ZoomManager} = tab.ownerGlobal;
+
+ if (zoom === 0) {
+ // A value of zero means use the default zoom factor.
+ return FullZoom.reset(tab.linkedBrowser);
+ } else if (zoom >= ZoomManager.MIN && zoom <= ZoomManager.MAX) {
+ FullZoom.setZoom(zoom, tab.linkedBrowser);
+ } else {
+ return Promise.reject({
+ message: `Zoom value ${zoom} out of range (must be between ${ZoomManager.MIN} and ${ZoomManager.MAX})`,
+ });
+ }
+
+ return Promise.resolve();
+ },
+
+ _getZoomSettings(tabId) {
+ let tab = tabId ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let {FullZoom} = tab.ownerGlobal;
+
+ return {
+ mode: "automatic",
+ scope: FullZoom.siteSpecific ? "per-origin" : "per-tab",
+ defaultZoomFactor: 1,
+ };
+ },
+
+ getZoomSettings(tabId) {
+ return Promise.resolve(this._getZoomSettings(tabId));
+ },
+
+ setZoomSettings(tabId, settings) {
+ let tab = tabId ? TabManager.getTab(tabId, context) : TabManager.activeTab;
+
+ let currentSettings = this._getZoomSettings(tab.id);
+
+ if (!Object.keys(settings).every(key => settings[key] === currentSettings[key])) {
+ return Promise.reject(`Unsupported zoom settings: ${JSON.stringify(settings)}`);
+ }
+ return Promise.resolve();
+ },
+
+ onZoomChange: new EventManager(context, "tabs.onZoomChange", fire => {
+ let getZoomLevel = browser => {
+ let {ZoomManager} = browser.ownerGlobal;
+
+ return ZoomManager.getZoomForBrowser(browser);
+ };
+
+ // Stores the last known zoom level for each tab's browser.
+ // WeakMap[<browser> -> number]
+ let zoomLevels = new WeakMap();
+
+ // Store the zoom level for all existing tabs.
+ for (let window of WindowListManager.browserWindows()) {
+ for (let tab of window.gBrowser.tabs) {
+ let browser = tab.linkedBrowser;
+ zoomLevels.set(browser, getZoomLevel(browser));
+ }
+ }
+
+ let tabCreated = (eventName, event) => {
+ let browser = event.tab.linkedBrowser;
+ zoomLevels.set(browser, getZoomLevel(browser));
+ };
+
+
+ let zoomListener = event => {
+ let browser = event.originalTarget;
+
+ // For non-remote browsers, this event is dispatched on the document
+ // rather than on the <browser>.
+ if (browser instanceof Ci.nsIDOMDocument) {
+ browser = browser.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell)
+ .chromeEventHandler;
+ }
+
+ let {gBrowser} = browser.ownerGlobal;
+ let tab = gBrowser.getTabForBrowser(browser);
+ if (!tab) {
+ // We only care about zoom events in the top-level browser of a tab.
+ return;
+ }
+
+ let oldZoomFactor = zoomLevels.get(browser);
+ let newZoomFactor = getZoomLevel(browser);
+
+ if (oldZoomFactor != newZoomFactor) {
+ zoomLevels.set(browser, newZoomFactor);
+
+ let tabId = TabManager.getId(tab);
+ fire({
+ tabId,
+ oldZoomFactor,
+ newZoomFactor,
+ zoomSettings: self.tabs._getZoomSettings(tabId),
+ });
+ }
+ };
+
+ tabListener.on("tab-attached", tabCreated);
+ tabListener.on("tab-created", tabCreated);
+
+ AllWindowEvents.addListener("FullZoomChange", zoomListener);
+ AllWindowEvents.addListener("TextZoomChange", zoomListener);
+ return () => {
+ tabListener.off("tab-attached", tabCreated);
+ tabListener.off("tab-created", tabCreated);
+
+ AllWindowEvents.removeListener("FullZoomChange", zoomListener);
+ AllWindowEvents.removeListener("TextZoomChange", zoomListener);
+ };
+ }).api(),
+ },
+ };
+ return self;
+});
diff --git a/browser/components/extensions/ext-utils.js b/browser/components/extensions/ext-utils.js
new file mode 100644
index 000000000..57c38a339
--- /dev/null
+++ b/browser/components/extensions/ext-utils.js
@@ -0,0 +1,1243 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
+ "resource:///modules/CustomizableUI.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
+ "resource://gre/modules/NetUtil.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
+ "resource://gre/modules/PrivateBrowsingUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Task",
+ "resource://gre/modules/Task.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "setTimeout",
+ "resource://gre/modules/Timer.jsm");
+
+XPCOMUtils.defineLazyServiceGetter(this, "styleSheetService",
+ "@mozilla.org/content/style-sheet-service;1",
+ "nsIStyleSheetService");
+
+XPCOMUtils.defineLazyGetter(this, "colorUtils", () => {
+ return require("devtools/shared/css/color").colorUtils;
+});
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+Cu.import("resource://gre/modules/AppConstants.jsm");
+
+const POPUP_LOAD_TIMEOUT_MS = 200;
+
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+var {
+ DefaultWeakMap,
+ EventManager,
+ promiseEvent,
+} = ExtensionUtils;
+
+// This file provides some useful code for the |tabs| and |windows|
+// modules. All of the code is installed on |global|, which is a scope
+// shared among the different ext-*.js scripts.
+
+global.makeWidgetId = id => {
+ id = id.toLowerCase();
+ // FIXME: This allows for collisions.
+ return id.replace(/[^a-z0-9_-]/g, "_");
+};
+
+function promisePopupShown(popup) {
+ return new Promise(resolve => {
+ if (popup.state == "open") {
+ resolve();
+ } else {
+ popup.addEventListener("popupshown", function onPopupShown(event) {
+ popup.removeEventListener("popupshown", onPopupShown);
+ resolve();
+ });
+ }
+ });
+}
+
+XPCOMUtils.defineLazyGetter(this, "popupStylesheets", () => {
+ let stylesheets = ["chrome://browser/content/extension.css"];
+
+ if (AppConstants.platform === "macosx") {
+ stylesheets.push("chrome://browser/content/extension-mac.css");
+ }
+ return stylesheets;
+});
+
+XPCOMUtils.defineLazyGetter(this, "standaloneStylesheets", () => {
+ let stylesheets = [];
+
+ if (AppConstants.platform === "macosx") {
+ stylesheets.push("chrome://browser/content/extension-mac-panel.css");
+ }
+ if (AppConstants.platform === "win") {
+ stylesheets.push("chrome://browser/content/extension-win-panel.css");
+ }
+ return stylesheets;
+});
+
+class BasePopup {
+ constructor(extension, viewNode, popupURL, browserStyle, fixedWidth = false) {
+ this.extension = extension;
+ this.popupURL = popupURL;
+ this.viewNode = viewNode;
+ this.browserStyle = browserStyle;
+ this.window = viewNode.ownerGlobal;
+ this.destroyed = false;
+ this.fixedWidth = fixedWidth;
+
+ extension.callOnClose(this);
+
+ this.contentReady = new Promise(resolve => {
+ this._resolveContentReady = resolve;
+ });
+
+ this.viewNode.addEventListener(this.DESTROY_EVENT, this);
+
+ let doc = viewNode.ownerDocument;
+ let arrowContent = doc.getAnonymousElementByAttribute(this.panel, "class", "panel-arrowcontent");
+ this.borderColor = doc.defaultView.getComputedStyle(arrowContent).borderTopColor;
+
+ this.browser = null;
+ this.browserLoaded = new Promise((resolve, reject) => {
+ this.browserLoadedDeferred = {resolve, reject};
+ });
+ this.browserReady = this.createBrowser(viewNode, popupURL);
+
+ BasePopup.instances.get(this.window).set(extension, this);
+ }
+
+ static for(extension, window) {
+ return BasePopup.instances.get(window).get(extension);
+ }
+
+ close() {
+ this.closePopup();
+ }
+
+ destroy() {
+ this.extension.forgetOnClose(this);
+
+ this.destroyed = true;
+ this.browserLoadedDeferred.reject(new Error("Popup destroyed"));
+ return this.browserReady.then(() => {
+ this.destroyBrowser(this.browser);
+ this.browser.remove();
+
+ this.viewNode.removeEventListener(this.DESTROY_EVENT, this);
+ this.viewNode.style.maxHeight = "";
+
+ if (this.panel) {
+ this.panel.style.removeProperty("--arrowpanel-background");
+ this.panel.style.removeProperty("--panel-arrow-image-vertical");
+ }
+
+ BasePopup.instances.get(this.window).delete(this.extension);
+
+ this.browser = null;
+ this.viewNode = null;
+ });
+ }
+
+ destroyBrowser(browser) {
+ let mm = browser.messageManager;
+ // If the browser has already been removed from the document, because the
+ // popup was closed externally, there will be no message manager here.
+ if (mm) {
+ mm.removeMessageListener("DOMTitleChanged", this);
+ mm.removeMessageListener("Extension:BrowserBackgroundChanged", this);
+ mm.removeMessageListener("Extension:BrowserContentLoaded", this);
+ mm.removeMessageListener("Extension:BrowserResized", this);
+ mm.removeMessageListener("Extension:DOMWindowClose", this);
+ }
+ }
+
+ // Returns the name of the event fired on `viewNode` when the popup is being
+ // destroyed. This must be implemented by every subclass.
+ get DESTROY_EVENT() {
+ throw new Error("Not implemented");
+ }
+
+ get STYLESHEETS() {
+ let sheets = [];
+
+ if (this.browserStyle) {
+ sheets.push(...popupStylesheets);
+ }
+ if (!this.fixedWidth) {
+ sheets.push(...standaloneStylesheets);
+ }
+
+ return sheets;
+ }
+
+ get panel() {
+ let panel = this.viewNode;
+ while (panel && panel.localName != "panel") {
+ panel = panel.parentNode;
+ }
+ return panel;
+ }
+
+ receiveMessage({name, data}) {
+ switch (name) {
+ case "DOMTitleChanged":
+ this.viewNode.setAttribute("aria-label", this.browser.contentTitle);
+ break;
+
+ case "Extension:BrowserBackgroundChanged":
+ this.setBackground(data.background);
+ break;
+
+ case "Extension:BrowserContentLoaded":
+ this.browserLoadedDeferred.resolve();
+ break;
+
+ case "Extension:BrowserResized":
+ this._resolveContentReady();
+ if (this.ignoreResizes) {
+ this.dimensions = data;
+ } else {
+ this.resizeBrowser(data);
+ }
+ break;
+
+ case "Extension:DOMWindowClose":
+ this.closePopup();
+ break;
+ }
+ }
+
+ handleEvent(event) {
+ switch (event.type) {
+ case this.DESTROY_EVENT:
+ this.destroy();
+ break;
+ }
+ }
+
+ createBrowser(viewNode, popupURL = null) {
+ let document = viewNode.ownerDocument;
+ this.browser = document.createElementNS(XUL_NS, "browser");
+ this.browser.setAttribute("type", "content");
+ this.browser.setAttribute("disableglobalhistory", "true");
+ this.browser.setAttribute("transparent", "true");
+ this.browser.setAttribute("class", "webextension-popup-browser");
+ this.browser.setAttribute("tooltip", "aHTMLTooltip");
+
+ // We only need flex sizing for the sake of the slide-in sub-views of the
+ // main menu panel, so that the browser occupies the full width of the view,
+ // and also takes up any extra height that's available to it.
+ this.browser.setAttribute("flex", "1");
+
+ // Note: When using noautohide panels, the popup manager will add width and
+ // height attributes to the panel, breaking our resize code, if the browser
+ // starts out smaller than 30px by 10px. This isn't an issue now, but it
+ // will be if and when we popup debugging.
+
+ viewNode.appendChild(this.browser);
+
+ extensions.emit("extension-browser-inserted", this.browser);
+ let windowId = WindowManager.getId(this.browser.ownerGlobal);
+ this.browser.messageManager.sendAsyncMessage("Extension:InitExtensionView", {
+ viewType: "popup",
+ windowId,
+ });
+ // TODO(robwu): Rework this to use the Extension:ExtensionViewLoaded message
+ // to detect loads and so on. And definitely move this content logic inside
+ // a file in the child process.
+
+ let initBrowser = browser => {
+ let mm = browser.messageManager;
+ mm.addMessageListener("DOMTitleChanged", this);
+ mm.addMessageListener("Extension:BrowserBackgroundChanged", this);
+ mm.addMessageListener("Extension:BrowserContentLoaded", this);
+ mm.addMessageListener("Extension:BrowserResized", this);
+ mm.addMessageListener("Extension:DOMWindowClose", this, true);
+ };
+
+ if (!popupURL) {
+ initBrowser(this.browser);
+ return this.browser;
+ }
+
+ return promiseEvent(this.browser, "load").then(() => {
+ initBrowser(this.browser);
+
+ let mm = this.browser.messageManager;
+
+ mm.loadFrameScript(
+ "chrome://extensions/content/ext-browser-content.js", false);
+
+ mm.sendAsyncMessage("Extension:InitBrowser", {
+ allowScriptsToClose: true,
+ fixedWidth: this.fixedWidth,
+ maxWidth: 800,
+ maxHeight: 600,
+ stylesheets: this.STYLESHEETS,
+ });
+
+ this.browser.setAttribute("src", popupURL);
+ });
+ }
+
+ resizeBrowser({width, height, detail}) {
+ if (this.fixedWidth) {
+ // Figure out how much extra space we have on the side of the panel
+ // opposite the arrow.
+ let side = this.panel.getAttribute("side") == "top" ? "bottom" : "top";
+ let maxHeight = this.viewHeight + this.extraHeight[side];
+
+ height = Math.min(height, maxHeight);
+ this.browser.style.height = `${height}px`;
+
+ // Set a maximum height on the <panelview> element to our preferred
+ // maximum height, so that the PanelUI resizing code can make an accurate
+ // calculation. If we don't do this, the flex sizing logic will prevent us
+ // from ever reporting a preferred size smaller than the height currently
+ // available to us in the panel.
+ height = Math.max(height, this.viewHeight);
+ this.viewNode.style.maxHeight = `${height}px`;
+ } else {
+ this.browser.style.width = `${width}px`;
+ this.browser.style.height = `${height}px`;
+ }
+
+ let event = new this.window.CustomEvent("WebExtPopupResized", {detail});
+ this.browser.dispatchEvent(event);
+ }
+
+ setBackground(background) {
+ let panelBackground = "";
+ let panelArrow = "";
+
+ if (background) {
+ let borderColor = this.borderColor || background;
+
+ panelBackground = background;
+ panelArrow = `url("data:image/svg+xml,${encodeURIComponent(`<?xml version="1.0" encoding="UTF-8"?>
+ <svg xmlns="http://www.w3.org/2000/svg" width="20" height="10">
+ <path d="M 0,10 L 10,0 20,10 z" fill="${borderColor}"/>
+ <path d="M 1,10 L 10,1 19,10 z" fill="${background}"/>
+ </svg>
+ `)}")`;
+ }
+
+ this.panel.style.setProperty("--arrowpanel-background", panelBackground);
+ this.panel.style.setProperty("--panel-arrow-image-vertical", panelArrow);
+ this.background = background;
+ }
+}
+
+/**
+ * A map of active popups for a given browser window.
+ *
+ * WeakMap[window -> WeakMap[Extension -> BasePopup]]
+ */
+BasePopup.instances = new DefaultWeakMap(() => new WeakMap());
+
+class PanelPopup extends BasePopup {
+ constructor(extension, imageNode, popupURL, browserStyle) {
+ let document = imageNode.ownerDocument;
+
+ let panel = document.createElement("panel");
+ panel.setAttribute("id", makeWidgetId(extension.id) + "-panel");
+ panel.setAttribute("class", "browser-extension-panel");
+ panel.setAttribute("tabspecific", "true");
+ panel.setAttribute("type", "arrow");
+ panel.setAttribute("role", "group");
+
+ document.getElementById("mainPopupSet").appendChild(panel);
+
+ super(extension, panel, popupURL, browserStyle);
+
+ this.contentReady.then(() => {
+ panel.openPopup(imageNode, "bottomcenter topright", 0, 0, false, false);
+
+ let event = new this.window.CustomEvent("WebExtPopupLoaded", {
+ bubbles: true,
+ detail: {extension},
+ });
+ this.browser.dispatchEvent(event);
+ });
+ }
+
+ get DESTROY_EVENT() {
+ return "popuphidden";
+ }
+
+ destroy() {
+ super.destroy();
+ this.viewNode.remove();
+ }
+
+ closePopup() {
+ promisePopupShown(this.viewNode).then(() => {
+ // Make sure we're not already destroyed.
+ if (this.viewNode) {
+ this.viewNode.hidePopup();
+ }
+ });
+ }
+}
+
+class ViewPopup extends BasePopup {
+ constructor(extension, window, popupURL, browserStyle, fixedWidth) {
+ let document = window.document;
+
+ // Create a temporary panel to hold the browser while it pre-loads its
+ // content. This panel will never be shown, but the browser's docShell will
+ // be swapped with the browser in the real panel when it's ready.
+ let panel = document.createElement("panel");
+ panel.setAttribute("type", "arrow");
+ document.getElementById("mainPopupSet").appendChild(panel);
+
+ super(extension, panel, popupURL, browserStyle, fixedWidth);
+
+ this.ignoreResizes = true;
+
+ this.attached = false;
+ this.tempPanel = panel;
+
+ this.browser.classList.add("webextension-preload-browser");
+ }
+
+ /**
+ * Attaches the pre-loaded browser to the given view node, and reserves a
+ * promise which resolves when the browser is ready.
+ *
+ * @param {Element} viewNode
+ * The node to attach the browser to.
+ * @returns {Promise<boolean>}
+ * Resolves when the browser is ready. Resolves to `false` if the
+ * browser was destroyed before it was fully loaded, and the popup
+ * should be closed, or `true` otherwise.
+ */
+ attach(viewNode) {
+ return Task.spawn(function* () {
+ this.viewNode = viewNode;
+ this.viewNode.addEventListener(this.DESTROY_EVENT, this);
+
+ // Wait until the browser element is fully initialized, and give it at least
+ // a short grace period to finish loading its initial content, if necessary.
+ //
+ // In practice, the browser that was created by the mousdown handler should
+ // nearly always be ready by this point.
+ yield Promise.all([
+ this.browserReady,
+ Promise.race([
+ // This promise may be rejected if the popup calls window.close()
+ // before it has fully loaded.
+ this.browserLoaded.catch(() => {}),
+ new Promise(resolve => setTimeout(resolve, POPUP_LOAD_TIMEOUT_MS)),
+ ]),
+ ]);
+
+ if (!this.destroyed && !this.panel) {
+ this.destroy();
+ }
+
+ if (this.destroyed) {
+ return false;
+ }
+
+ this.attached = true;
+
+ // Store the initial height of the view, so that we never resize menu panel
+ // sub-views smaller than the initial height of the menu.
+ this.viewHeight = this.viewNode.boxObject.height;
+
+ // Calculate the extra height available on the screen above and below the
+ // menu panel. Use that to calculate the how much the sub-view may grow.
+ let popupRect = this.panel.getBoundingClientRect();
+
+ this.setBackground(this.background);
+
+ let win = this.window;
+ let popupBottom = win.mozInnerScreenY + popupRect.bottom;
+ let popupTop = win.mozInnerScreenY + popupRect.top;
+
+ let screenBottom = win.screen.availTop + win.screen.availHeight;
+ this.extraHeight = {
+ bottom: Math.max(0, screenBottom - popupBottom),
+ top: Math.max(0, popupTop - win.screen.availTop),
+ };
+
+ // Create a new browser in the real popup.
+ let browser = this.browser;
+ this.createBrowser(this.viewNode);
+
+ this.browser.swapDocShells(browser);
+ this.destroyBrowser(browser);
+
+ this.ignoreResizes = false;
+ if (this.dimensions) {
+ this.resizeBrowser(this.dimensions);
+ }
+
+ this.tempPanel.remove();
+ this.tempPanel = null;
+
+ let event = new this.window.CustomEvent("WebExtPopupLoaded", {
+ bubbles: true,
+ detail: {extension: this.extension},
+ });
+ this.browser.dispatchEvent(event);
+
+ return true;
+ }.bind(this));
+ }
+
+ destroy() {
+ return super.destroy().then(() => {
+ if (this.tempPanel) {
+ this.tempPanel.remove();
+ this.tempPanel = null;
+ }
+ });
+ }
+
+ get DESTROY_EVENT() {
+ return "ViewHiding";
+ }
+
+ closePopup() {
+ if (this.attached) {
+ CustomizableUI.hidePanelForNode(this.viewNode);
+ } else {
+ this.destroy();
+ }
+ }
+}
+
+Object.assign(global, {PanelPopup, ViewPopup});
+
+// Manages tab-specific context data, and dispatching tab select events
+// across all windows.
+global.TabContext = function TabContext(getDefaults, extension) {
+ this.extension = extension;
+ this.getDefaults = getDefaults;
+
+ this.tabData = new WeakMap();
+ this.lastLocation = new WeakMap();
+
+ AllWindowEvents.addListener("progress", this);
+ AllWindowEvents.addListener("TabSelect", this);
+
+ EventEmitter.decorate(this);
+};
+
+TabContext.prototype = {
+ get(tab) {
+ if (!this.tabData.has(tab)) {
+ this.tabData.set(tab, this.getDefaults(tab));
+ }
+
+ return this.tabData.get(tab);
+ },
+
+ clear(tab) {
+ this.tabData.delete(tab);
+ },
+
+ handleEvent(event) {
+ if (event.type == "TabSelect") {
+ let tab = event.target;
+ this.emit("tab-select", tab);
+ this.emit("location-change", tab);
+ }
+ },
+
+ onStateChange(browser, webProgress, request, stateFlags, statusCode) {
+ let flags = Ci.nsIWebProgressListener;
+
+ if (!(~stateFlags & (flags.STATE_IS_WINDOW | flags.STATE_START) ||
+ this.lastLocation.has(browser))) {
+ this.lastLocation.set(browser, request.URI);
+ }
+ },
+
+ onLocationChange(browser, webProgress, request, locationURI, flags) {
+ let gBrowser = browser.ownerGlobal.gBrowser;
+ let lastLocation = this.lastLocation.get(browser);
+ if (browser === gBrowser.selectedBrowser &&
+ !(lastLocation && lastLocation.equalsExceptRef(browser.currentURI))) {
+ let tab = gBrowser.getTabForBrowser(browser);
+ this.emit("location-change", tab, true);
+ }
+ this.lastLocation.set(browser, browser.currentURI);
+ },
+
+ shutdown() {
+ AllWindowEvents.removeListener("progress", this);
+ AllWindowEvents.removeListener("TabSelect", this);
+ },
+};
+
+// Manages tab mappings and permissions for a specific extension.
+function ExtensionTabManager(extension) {
+ this.extension = extension;
+
+ // A mapping of tab objects to the inner window ID the extension currently has
+ // the active tab permission for. The active permission for a given tab is
+ // valid only for the inner window that was active when the permission was
+ // granted. If the tab navigates, the inner window ID changes, and the
+ // permission automatically becomes stale.
+ //
+ // WeakMap[tab => inner-window-id<int>]
+ this.hasTabPermissionFor = new WeakMap();
+}
+
+ExtensionTabManager.prototype = {
+ addActiveTabPermission(tab = TabManager.activeTab) {
+ if (this.extension.hasPermission("activeTab")) {
+ // Note that, unlike Chrome, we don't currently clear this permission with
+ // the tab navigates. If the inner window is revived from BFCache before
+ // we've granted this permission to a new inner window, the extension
+ // maintains its permissions for it.
+ this.hasTabPermissionFor.set(tab, tab.linkedBrowser.innerWindowID);
+ }
+ },
+
+ revokeActiveTabPermission(tab = TabManager.activeTab) {
+ this.hasTabPermissionFor.delete(tab);
+ },
+
+ // Returns true if the extension has the "activeTab" permission for this tab.
+ // This is somewhat more permissive than the generic "tabs" permission, as
+ // checked by |hasTabPermission|, in that it also allows programmatic script
+ // injection without an explicit host permission.
+ hasActiveTabPermission(tab) {
+ // This check is redundant with addTabPermission, but cheap.
+ if (this.extension.hasPermission("activeTab")) {
+ return (this.hasTabPermissionFor.has(tab) &&
+ this.hasTabPermissionFor.get(tab) === tab.linkedBrowser.innerWindowID);
+ }
+ return false;
+ },
+
+ hasTabPermission(tab) {
+ return this.extension.hasPermission("tabs") || this.hasActiveTabPermission(tab);
+ },
+
+ convert(tab) {
+ let window = tab.ownerGlobal;
+ let browser = tab.linkedBrowser;
+
+ let mutedInfo = {muted: tab.muted};
+ if (tab.muteReason === null) {
+ mutedInfo.reason = "user";
+ } else if (tab.muteReason) {
+ mutedInfo.reason = "extension";
+ mutedInfo.extensionId = tab.muteReason;
+ }
+
+ let result = {
+ id: TabManager.getId(tab),
+ index: tab._tPos,
+ windowId: WindowManager.getId(window),
+ selected: tab.selected,
+ highlighted: tab.selected,
+ active: tab.selected,
+ pinned: tab.pinned,
+ status: TabManager.getStatus(tab),
+ incognito: WindowManager.isBrowserPrivate(browser),
+ width: browser.frameLoader.lazyWidth || browser.clientWidth,
+ height: browser.frameLoader.lazyHeight || browser.clientHeight,
+ audible: tab.soundPlaying,
+ mutedInfo,
+ };
+ if (this.extension.hasPermission("cookies")) {
+ result.cookieStoreId = getCookieStoreIdForTab(result, tab);
+ }
+
+ if (this.hasTabPermission(tab)) {
+ result.url = browser.currentURI.spec;
+ let title = browser.contentTitle || tab.label;
+ if (title) {
+ result.title = title;
+ }
+ let icon = window.gBrowser.getIcon(tab);
+ if (icon) {
+ result.favIconUrl = icon;
+ }
+ }
+
+ return result;
+ },
+
+ // Converts tabs returned from SessionStore.getClosedTabData and
+ // SessionStore.getClosedWindowData into API tab objects
+ convertFromSessionStoreClosedData(tab, window) {
+ let result = {
+ sessionId: String(tab.closedId),
+ index: tab.pos ? tab.pos : 0,
+ windowId: WindowManager.getId(window),
+ selected: false,
+ highlighted: false,
+ active: false,
+ pinned: false,
+ incognito: Boolean(tab.state && tab.state.isPrivate),
+ };
+
+ if (this.hasTabPermission(tab)) {
+ let entries = tab.state ? tab.state.entries : tab.entries;
+ result.url = entries[0].url;
+ result.title = entries[0].title;
+ if (tab.image) {
+ result.favIconUrl = tab.image;
+ }
+ }
+
+ return result;
+ },
+
+ getTabs(window) {
+ return Array.from(window.gBrowser.tabs)
+ .filter(tab => !tab.closing)
+ .map(tab => this.convert(tab));
+ },
+};
+
+// Sends the tab and windowId upon request. This is primarily used to support
+// the synchronous `browser.extension.getViews` API.
+let onGetTabAndWindowId = {
+ receiveMessage({name, target, sync}) {
+ let {gBrowser} = target.ownerGlobal;
+ let tab = gBrowser && gBrowser.getTabForBrowser(target);
+ if (tab) {
+ let reply = {
+ tabId: TabManager.getId(tab),
+ windowId: WindowManager.getId(tab.ownerGlobal),
+ };
+ if (sync) {
+ return reply;
+ }
+ target.messageManager.sendAsyncMessage("Extension:SetTabAndWindowId", reply);
+ }
+ },
+};
+/* eslint-disable mozilla/balanced-listeners */
+Services.mm.addMessageListener("Extension:GetTabAndWindowId", onGetTabAndWindowId);
+/* eslint-enable mozilla/balanced-listeners */
+
+
+// Manages global mappings between XUL tabs and extension tab IDs.
+global.TabManager = {
+ _tabs: new WeakMap(),
+ _nextId: 1,
+ _initialized: false,
+
+ // We begin listening for TabOpen and TabClose events once we've started
+ // assigning IDs to tabs, so that we can remap the IDs of tabs which are moved
+ // between windows.
+ initListener() {
+ if (this._initialized) {
+ return;
+ }
+
+ AllWindowEvents.addListener("TabOpen", this);
+ AllWindowEvents.addListener("TabClose", this);
+ WindowListManager.addOpenListener(this.handleWindowOpen.bind(this));
+
+ this._initialized = true;
+ },
+
+ handleEvent(event) {
+ if (event.type == "TabOpen") {
+ let {adoptedTab} = event.detail;
+ if (adoptedTab) {
+ // This tab is being created to adopt a tab from a different window.
+ // Copy the ID from the old tab to the new.
+ let tab = event.target;
+ this._tabs.set(tab, this.getId(adoptedTab));
+
+ tab.linkedBrowser.messageManager.sendAsyncMessage("Extension:SetTabAndWindowId", {
+ windowId: WindowManager.getId(tab.ownerGlobal),
+ });
+ }
+ } else if (event.type == "TabClose") {
+ let {adoptedBy} = event.detail;
+ if (adoptedBy) {
+ // This tab is being closed because it was adopted by a new window.
+ // Copy its ID to the new tab, in case it was created as the first tab
+ // of a new window, and did not have an `adoptedTab` detail when it was
+ // opened.
+ this._tabs.set(adoptedBy, this.getId(event.target));
+
+ adoptedBy.linkedBrowser.messageManager.sendAsyncMessage("Extension:SetTabAndWindowId", {
+ windowId: WindowManager.getId(adoptedBy),
+ });
+ }
+ }
+ },
+
+ handleWindowOpen(window) {
+ if (window.arguments && window.arguments[0] instanceof window.XULElement) {
+ // If the first window argument is a XUL element, it means the
+ // window is about to adopt a tab from another window to replace its
+ // initial tab.
+ let adoptedTab = window.arguments[0];
+
+ this._tabs.set(window.gBrowser.tabs[0], this.getId(adoptedTab));
+ }
+ },
+
+ getId(tab) {
+ if (this._tabs.has(tab)) {
+ return this._tabs.get(tab);
+ }
+ this.initListener();
+
+ let id = this._nextId++;
+ this._tabs.set(tab, id);
+ return id;
+ },
+
+ getBrowserId(browser) {
+ let gBrowser = browser.ownerGlobal.gBrowser;
+ // Some non-browser windows have gBrowser but not
+ // getTabForBrowser!
+ if (gBrowser && gBrowser.getTabForBrowser) {
+ let tab = gBrowser.getTabForBrowser(browser);
+ if (tab) {
+ return this.getId(tab);
+ }
+ }
+ return -1;
+ },
+
+ /**
+ * Returns the XUL <tab> element associated with the given tab ID. If no tab
+ * with the given ID exists, and no default value is provided, an error is
+ * raised, belonging to the scope of the given context.
+ *
+ * @param {integer} tabId
+ * The ID of the tab to retrieve.
+ * @param {ExtensionContext} context
+ * The context of the caller.
+ * This value may be omitted if `default_` is not `undefined`.
+ * @param {*} default_
+ * The value to return if no tab exists with the given ID.
+ * @returns {Element<tab>}
+ * A XUL <tab> element.
+ */
+ getTab(tabId, context, default_ = undefined) {
+ // FIXME: Speed this up without leaking memory somehow.
+ for (let window of WindowListManager.browserWindows()) {
+ if (!window.gBrowser) {
+ continue;
+ }
+ for (let tab of window.gBrowser.tabs) {
+ if (this.getId(tab) == tabId) {
+ return tab;
+ }
+ }
+ }
+ if (default_ !== undefined) {
+ return default_;
+ }
+ throw new context.cloneScope.Error(`Invalid tab ID: ${tabId}`);
+ },
+
+ get activeTab() {
+ let window = WindowManager.topWindow;
+ if (window && window.gBrowser) {
+ return window.gBrowser.selectedTab;
+ }
+ return null;
+ },
+
+ getStatus(tab) {
+ return tab.getAttribute("busy") == "true" ? "loading" : "complete";
+ },
+
+ convert(extension, tab) {
+ return TabManager.for(extension).convert(tab);
+ },
+};
+
+// WeakMap[Extension -> ExtensionTabManager]
+let tabManagers = new WeakMap();
+
+// Returns the extension-specific tab manager for the given extension, or
+// creates one if it doesn't already exist.
+TabManager.for = function(extension) {
+ if (!tabManagers.has(extension)) {
+ tabManagers.set(extension, new ExtensionTabManager(extension));
+ }
+ return tabManagers.get(extension);
+};
+
+/* eslint-disable mozilla/balanced-listeners */
+extensions.on("shutdown", (type, extension) => {
+ tabManagers.delete(extension);
+});
+/* eslint-enable mozilla/balanced-listeners */
+
+function memoize(fn) {
+ let weakMap = new DefaultWeakMap(fn);
+ return weakMap.get.bind(weakMap);
+}
+
+// Manages mapping between XUL windows and extension window IDs.
+global.WindowManager = {
+ _windows: new WeakMap(),
+ _nextId: 0,
+
+ // Note: These must match the values in windows.json.
+ WINDOW_ID_NONE: -1,
+ WINDOW_ID_CURRENT: -2,
+
+ get topWindow() {
+ return Services.wm.getMostRecentWindow("navigator:browser");
+ },
+
+ windowType(window) {
+ // TODO: Make this work.
+
+ let {chromeFlags} = window.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell)
+ .treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIXULWindow);
+
+ if (chromeFlags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_DIALOG) {
+ return "popup";
+ }
+
+ return "normal";
+ },
+
+ updateGeometry(window, options) {
+ if (options.left !== null || options.top !== null) {
+ let left = options.left !== null ? options.left : window.screenX;
+ let top = options.top !== null ? options.top : window.screenY;
+ window.moveTo(left, top);
+ }
+
+ if (options.width !== null || options.height !== null) {
+ let width = options.width !== null ? options.width : window.outerWidth;
+ let height = options.height !== null ? options.height : window.outerHeight;
+ window.resizeTo(width, height);
+ }
+ },
+
+ isBrowserPrivate: memoize(browser => {
+ return PrivateBrowsingUtils.isBrowserPrivate(browser);
+ }),
+
+ getId(window) {
+ if (this._windows.has(window)) {
+ return this._windows.get(window);
+ }
+ let id = this._nextId++;
+ this._windows.set(window, id);
+ return id;
+ },
+
+ getWindow(id, context) {
+ if (id == this.WINDOW_ID_CURRENT) {
+ return currentWindow(context);
+ }
+
+ for (let window of WindowListManager.browserWindows(true)) {
+ if (this.getId(window) == id) {
+ return window;
+ }
+ }
+ return null;
+ },
+
+ getState(window) {
+ const STATES = {
+ [window.STATE_MAXIMIZED]: "maximized",
+ [window.STATE_MINIMIZED]: "minimized",
+ [window.STATE_NORMAL]: "normal",
+ };
+ let state = STATES[window.windowState];
+ if (window.fullScreen) {
+ state = "fullscreen";
+ }
+ return state;
+ },
+
+ setState(window, state) {
+ if (state != "fullscreen" && window.fullScreen) {
+ window.fullScreen = false;
+ }
+
+ switch (state) {
+ case "maximized":
+ window.maximize();
+ break;
+
+ case "minimized":
+ case "docked":
+ window.minimize();
+ break;
+
+ case "normal":
+ // Restore sometimes returns the window to its previous state, rather
+ // than to the "normal" state, so it may need to be called anywhere from
+ // zero to two times.
+ window.restore();
+ if (window.windowState != window.STATE_NORMAL) {
+ window.restore();
+ }
+ if (window.windowState != window.STATE_NORMAL) {
+ // And on OS-X, where normal vs. maximized is basically a heuristic,
+ // we need to cheat.
+ window.sizeToContent();
+ }
+ break;
+
+ case "fullscreen":
+ window.fullScreen = true;
+ break;
+
+ default:
+ throw new Error(`Unexpected window state: ${state}`);
+ }
+ },
+
+ convert(extension, window, getInfo) {
+ let xulWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell)
+ .treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIXULWindow);
+
+ let result = {
+ id: this.getId(window),
+ focused: window.document.hasFocus(),
+ top: window.screenY,
+ left: window.screenX,
+ width: window.outerWidth,
+ height: window.outerHeight,
+ incognito: PrivateBrowsingUtils.isWindowPrivate(window),
+ type: this.windowType(window),
+ state: this.getState(window),
+ alwaysOnTop: xulWindow.zLevel >= Ci.nsIXULWindow.raisedZ,
+ };
+
+ if (getInfo && getInfo.populate) {
+ result.tabs = TabManager.for(extension).getTabs(window);
+ }
+
+ return result;
+ },
+
+ // Converts windows returned from SessionStore.getClosedWindowData
+ // into API window objects
+ convertFromSessionStoreClosedData(window, extension) {
+ let result = {
+ sessionId: String(window.closedId),
+ focused: false,
+ incognito: false,
+ type: "normal", // this is always "normal" for a closed window
+ state: this.getState(window),
+ alwaysOnTop: false,
+ };
+
+ if (window.tabs.length) {
+ result.tabs = [];
+ window.tabs.forEach((tab, index) => {
+ result.tabs.push(TabManager.for(extension).convertFromSessionStoreClosedData(tab, window, index));
+ });
+ }
+
+ return result;
+ },
+};
+
+// Manages listeners for window opening and closing. A window is
+// considered open when the "load" event fires on it. A window is
+// closed when a "domwindowclosed" notification fires for it.
+global.WindowListManager = {
+ _openListeners: new Set(),
+ _closeListeners: new Set(),
+
+ // Returns an iterator for all browser windows. Unless |includeIncomplete| is
+ // true, only fully-loaded windows are returned.
+ * browserWindows(includeIncomplete = false) {
+ // The window type parameter is only available once the window's document
+ // element has been created. This means that, when looking for incomplete
+ // browser windows, we need to ignore the type entirely for windows which
+ // haven't finished loading, since we would otherwise skip browser windows
+ // in their early loading stages.
+ // This is particularly important given that the "domwindowcreated" event
+ // fires for browser windows when they're in that in-between state, and just
+ // before we register our own "domwindowcreated" listener.
+
+ let e = Services.wm.getEnumerator("");
+ while (e.hasMoreElements()) {
+ let window = e.getNext();
+
+ let ok = includeIncomplete;
+ if (window.document.readyState == "complete") {
+ ok = window.document.documentElement.getAttribute("windowtype") == "navigator:browser";
+ }
+
+ if (ok) {
+ yield window;
+ }
+ }
+ },
+
+ addOpenListener(listener) {
+ if (this._openListeners.size == 0 && this._closeListeners.size == 0) {
+ Services.ww.registerNotification(this);
+ }
+ this._openListeners.add(listener);
+
+ for (let window of this.browserWindows(true)) {
+ if (window.document.readyState != "complete") {
+ window.addEventListener("load", this);
+ }
+ }
+ },
+
+ removeOpenListener(listener) {
+ this._openListeners.delete(listener);
+ if (this._openListeners.size == 0 && this._closeListeners.size == 0) {
+ Services.ww.unregisterNotification(this);
+ }
+ },
+
+ addCloseListener(listener) {
+ if (this._openListeners.size == 0 && this._closeListeners.size == 0) {
+ Services.ww.registerNotification(this);
+ }
+ this._closeListeners.add(listener);
+ },
+
+ removeCloseListener(listener) {
+ this._closeListeners.delete(listener);
+ if (this._openListeners.size == 0 && this._closeListeners.size == 0) {
+ Services.ww.unregisterNotification(this);
+ }
+ },
+
+ handleEvent(event) {
+ event.currentTarget.removeEventListener(event.type, this);
+ let window = event.target.defaultView;
+ if (window.document.documentElement.getAttribute("windowtype") != "navigator:browser") {
+ return;
+ }
+
+ for (let listener of this._openListeners) {
+ listener(window);
+ }
+ },
+
+ observe(window, topic, data) {
+ if (topic == "domwindowclosed") {
+ if (window.document.documentElement.getAttribute("windowtype") != "navigator:browser") {
+ return;
+ }
+
+ window.removeEventListener("load", this);
+ for (let listener of this._closeListeners) {
+ listener(window);
+ }
+ } else {
+ window.addEventListener("load", this);
+ }
+ },
+};
+
+// Provides a facility to listen for DOM events across all XUL windows.
+global.AllWindowEvents = {
+ _listeners: new Map(),
+
+ // If |type| is a normal event type, invoke |listener| each time
+ // that event fires in any open window. If |type| is "progress", add
+ // a web progress listener that covers all open windows.
+ addListener(type, listener) {
+ if (type == "domwindowopened") {
+ return WindowListManager.addOpenListener(listener);
+ } else if (type == "domwindowclosed") {
+ return WindowListManager.addCloseListener(listener);
+ }
+
+ if (this._listeners.size == 0) {
+ WindowListManager.addOpenListener(this.openListener);
+ }
+
+ if (!this._listeners.has(type)) {
+ this._listeners.set(type, new Set());
+ }
+ let list = this._listeners.get(type);
+ list.add(listener);
+
+ // Register listener on all existing windows.
+ for (let window of WindowListManager.browserWindows()) {
+ this.addWindowListener(window, type, listener);
+ }
+ },
+
+ removeListener(eventType, listener) {
+ if (eventType == "domwindowopened") {
+ return WindowListManager.removeOpenListener(listener);
+ } else if (eventType == "domwindowclosed") {
+ return WindowListManager.removeCloseListener(listener);
+ }
+
+ let listeners = this._listeners.get(eventType);
+ listeners.delete(listener);
+ if (listeners.size == 0) {
+ this._listeners.delete(eventType);
+ if (this._listeners.size == 0) {
+ WindowListManager.removeOpenListener(this.openListener);
+ }
+ }
+
+ // Unregister listener from all existing windows.
+ let useCapture = eventType === "focus" || eventType === "blur";
+ for (let window of WindowListManager.browserWindows()) {
+ if (eventType == "progress") {
+ window.gBrowser.removeTabsProgressListener(listener);
+ } else {
+ window.removeEventListener(eventType, listener, useCapture);
+ }
+ }
+ },
+
+ /* eslint-disable mozilla/balanced-listeners */
+ addWindowListener(window, eventType, listener) {
+ let useCapture = eventType === "focus" || eventType === "blur";
+
+ if (eventType == "progress") {
+ window.gBrowser.addTabsProgressListener(listener);
+ } else {
+ window.addEventListener(eventType, listener, useCapture);
+ }
+ },
+ /* eslint-enable mozilla/balanced-listeners */
+
+ // Runs whenever the "load" event fires for a new window.
+ openListener(window) {
+ for (let [eventType, listeners] of AllWindowEvents._listeners) {
+ for (let listener of listeners) {
+ this.addWindowListener(window, eventType, listener);
+ }
+ }
+ },
+};
+
+AllWindowEvents.openListener = AllWindowEvents.openListener.bind(AllWindowEvents);
+
+// Subclass of EventManager where we just need to call
+// add/removeEventListener on each XUL window.
+global.WindowEventManager = function(context, name, event, listener) {
+ EventManager.call(this, context, name, fire => {
+ let listener2 = (...args) => listener(fire, ...args);
+ AllWindowEvents.addListener(event, listener2);
+ return () => {
+ AllWindowEvents.removeListener(event, listener2);
+ };
+ });
+};
+
+WindowEventManager.prototype = Object.create(EventManager.prototype);
diff --git a/browser/components/extensions/ext-windows.js b/browser/components/extensions/ext-windows.js
new file mode 100644
index 000000000..5956ae15b
--- /dev/null
+++ b/browser/components/extensions/ext-windows.js
@@ -0,0 +1,231 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
+ "@mozilla.org/browser/aboutnewtab-service;1",
+ "nsIAboutNewTabService");
+XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
+ "resource://gre/modules/AppConstants.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
+ "resource://gre/modules/PrivateBrowsingUtils.jsm");
+
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
+var {
+ EventManager,
+ promiseObserved,
+} = ExtensionUtils;
+
+function onXULFrameLoaderCreated({target}) {
+ target.messageManager.sendAsyncMessage("AllowScriptsToClose", {});
+}
+
+extensions.registerSchemaAPI("windows", "addon_parent", context => {
+ let {extension} = context;
+ return {
+ windows: {
+ onCreated:
+ new WindowEventManager(context, "windows.onCreated", "domwindowopened", (fire, window) => {
+ fire(WindowManager.convert(extension, window));
+ }).api(),
+
+ onRemoved:
+ new WindowEventManager(context, "windows.onRemoved", "domwindowclosed", (fire, window) => {
+ fire(WindowManager.getId(window));
+ }).api(),
+
+ onFocusChanged: new EventManager(context, "windows.onFocusChanged", fire => {
+ // Keep track of the last windowId used to fire an onFocusChanged event
+ let lastOnFocusChangedWindowId;
+
+ let listener = event => {
+ // Wait a tick to avoid firing a superfluous WINDOW_ID_NONE
+ // event when switching focus between two Firefox windows.
+ Promise.resolve().then(() => {
+ let window = Services.focus.activeWindow;
+ let windowId = window ? WindowManager.getId(window) : WindowManager.WINDOW_ID_NONE;
+ if (windowId !== lastOnFocusChangedWindowId) {
+ fire(windowId);
+ lastOnFocusChangedWindowId = windowId;
+ }
+ });
+ };
+ AllWindowEvents.addListener("focus", listener);
+ AllWindowEvents.addListener("blur", listener);
+ return () => {
+ AllWindowEvents.removeListener("focus", listener);
+ AllWindowEvents.removeListener("blur", listener);
+ };
+ }).api(),
+
+ get: function(windowId, getInfo) {
+ let window = WindowManager.getWindow(windowId, context);
+ return Promise.resolve(WindowManager.convert(extension, window, getInfo));
+ },
+
+ getCurrent: function(getInfo) {
+ let window = currentWindow(context);
+ return Promise.resolve(WindowManager.convert(extension, window, getInfo));
+ },
+
+ getLastFocused: function(getInfo) {
+ let window = WindowManager.topWindow;
+ return Promise.resolve(WindowManager.convert(extension, window, getInfo));
+ },
+
+ getAll: function(getInfo) {
+ let windows = Array.from(WindowListManager.browserWindows(),
+ window => WindowManager.convert(extension, window, getInfo));
+ return Promise.resolve(windows);
+ },
+
+ create: function(createData) {
+ let needResize = (createData.left !== null || createData.top !== null ||
+ createData.width !== null || createData.height !== null);
+
+ if (needResize) {
+ if (createData.state !== null && createData.state != "normal") {
+ return Promise.reject({message: `"state": "${createData.state}" may not be combined with "left", "top", "width", or "height"`});
+ }
+ createData.state = "normal";
+ }
+
+ function mkstr(s) {
+ let result = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ result.data = s;
+ return result;
+ }
+
+ let args = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
+
+ if (createData.tabId !== null) {
+ if (createData.url !== null) {
+ return Promise.reject({message: "`tabId` may not be used in conjunction with `url`"});
+ }
+
+ if (createData.allowScriptsToClose) {
+ return Promise.reject({message: "`tabId` may not be used in conjunction with `allowScriptsToClose`"});
+ }
+
+ let tab = TabManager.getTab(createData.tabId, context);
+
+ // Private browsing tabs can only be moved to private browsing
+ // windows.
+ let incognito = PrivateBrowsingUtils.isBrowserPrivate(tab.linkedBrowser);
+ if (createData.incognito !== null && createData.incognito != incognito) {
+ return Promise.reject({message: "`incognito` property must match the incognito state of tab"});
+ }
+ createData.incognito = incognito;
+
+ args.appendElement(tab, /* weak = */ false);
+ } else if (createData.url !== null) {
+ if (Array.isArray(createData.url)) {
+ let array = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
+ for (let url of createData.url) {
+ array.appendElement(mkstr(url), /* weak = */ false);
+ }
+ args.appendElement(array, /* weak = */ false);
+ } else {
+ args.appendElement(mkstr(createData.url), /* weak = */ false);
+ }
+ } else {
+ args.appendElement(mkstr(aboutNewTabService.newTabURL), /* weak = */ false);
+ }
+
+ let features = ["chrome"];
+
+ if (createData.type === null || createData.type == "normal") {
+ features.push("dialog=no", "all");
+ } else {
+ // All other types create "popup"-type windows by default.
+ features.push("dialog", "resizable", "minimizable", "centerscreen", "titlebar", "close");
+ }
+
+ if (createData.incognito !== null) {
+ if (createData.incognito) {
+ features.push("private");
+ } else {
+ features.push("non-private");
+ }
+ }
+
+ let {allowScriptsToClose, url} = createData;
+ if (allowScriptsToClose === null) {
+ allowScriptsToClose = typeof url === "string" && url.startsWith("moz-extension://");
+ }
+
+ let window = Services.ww.openWindow(null, "chrome://browser/content/browser.xul", "_blank",
+ features.join(","), args);
+
+ WindowManager.updateGeometry(window, createData);
+
+ // TODO: focused, type
+
+ return new Promise(resolve => {
+ window.addEventListener("load", function listener() {
+ window.removeEventListener("load", listener);
+ if (["maximized", "normal"].includes(createData.state)) {
+ window.document.documentElement.setAttribute("sizemode", createData.state);
+ }
+ resolve(promiseObserved("browser-delayed-startup-finished", win => win == window));
+ });
+ }).then(() => {
+ // Some states only work after delayed-startup-finished
+ if (["minimized", "fullscreen", "docked"].includes(createData.state)) {
+ WindowManager.setState(window, createData.state);
+ }
+ if (allowScriptsToClose) {
+ for (let {linkedBrowser} of window.gBrowser.tabs) {
+ onXULFrameLoaderCreated({target: linkedBrowser});
+ linkedBrowser.addEventListener( // eslint-disable-line mozilla/balanced-listeners
+ "XULFrameLoaderCreated", onXULFrameLoaderCreated);
+ }
+ }
+ return WindowManager.convert(extension, window, {populate: true});
+ });
+ },
+
+ update: function(windowId, updateInfo) {
+ if (updateInfo.state !== null && updateInfo.state != "normal") {
+ if (updateInfo.left !== null || updateInfo.top !== null ||
+ updateInfo.width !== null || updateInfo.height !== null) {
+ return Promise.reject({message: `"state": "${updateInfo.state}" may not be combined with "left", "top", "width", or "height"`});
+ }
+ }
+
+ let window = WindowManager.getWindow(windowId, context);
+ if (updateInfo.focused) {
+ Services.focus.activeWindow = window;
+ }
+
+ if (updateInfo.state !== null) {
+ WindowManager.setState(window, updateInfo.state);
+ }
+
+ if (updateInfo.drawAttention) {
+ // Bug 1257497 - Firefox can't cancel attention actions.
+ window.getAttention();
+ }
+
+ WindowManager.updateGeometry(window, updateInfo);
+
+ // TODO: All the other properties, focused=false...
+
+ return Promise.resolve(WindowManager.convert(extension, window));
+ },
+
+ remove: function(windowId) {
+ let window = WindowManager.getWindow(windowId, context);
+ window.close();
+
+ return new Promise(resolve => {
+ let listener = () => {
+ AllWindowEvents.removeListener("domwindowclosed", listener);
+ resolve();
+ };
+ AllWindowEvents.addListener("domwindowclosed", listener);
+ });
+ },
+ },
+ };
+});
diff --git a/browser/components/extensions/extension-mac-panel.css b/browser/components/extensions/extension-mac-panel.css
new file mode 100644
index 000000000..2e9ed6bdb
--- /dev/null
+++ b/browser/components/extensions/extension-mac-panel.css
@@ -0,0 +1,3 @@
+body {
+ border-radius: 3.5px;
+}
diff --git a/browser/components/extensions/extension-mac.css b/browser/components/extensions/extension-mac.css
new file mode 100644
index 000000000..49cd3b359
--- /dev/null
+++ b/browser/components/extensions/extension-mac.css
@@ -0,0 +1,11 @@
+button,
+select,
+input[type="checkbox"] + label::before {
+ border-radius: 4px;
+}
+
+.panel-section-footer {
+ border-bottom-left-radius: 4px;
+ border-bottom-right-radius: 4px;
+ overflow: hidden;
+}
diff --git a/browser/components/extensions/extension-win-panel.css b/browser/components/extensions/extension-win-panel.css
new file mode 100644
index 000000000..ddafe3ea5
--- /dev/null
+++ b/browser/components/extensions/extension-win-panel.css
@@ -0,0 +1,7 @@
+@media (-moz-os-version: windows-xp),
+ (-moz-os-version: windows-vista),
+ (-moz-os-version: windows-win7) {
+ body {
+ border-radius: 4px;
+ }
+}
diff --git a/browser/components/extensions/extension.css b/browser/components/extensions/extension.css
new file mode 100644
index 000000000..6b59033e3
--- /dev/null
+++ b/browser/components/extensions/extension.css
@@ -0,0 +1,572 @@
+/* stylelint-disable property-no-vendor-prefix */
+/* stylelint-disable property-no-vendor-prefix */
+/* Base */
+button,
+select,
+option,
+input {
+ -moz-appearance: none;
+}
+
+/* Variables */
+html,
+body {
+ background: transparent;
+ box-sizing: border-box;
+ color: #222426;
+ cursor: default;
+ display: flex;
+ flex-direction: column;
+ font: caption;
+ margin: 0;
+ padding: 0;
+ -moz-user-select: none;
+}
+
+body * {
+ box-sizing: border-box;
+ text-align: start;
+}
+
+/* stylelint-disable property-no-vendor-prefix */
+/* Buttons */
+button,
+select {
+ background-color: #fbfbfb;
+ border: 1px solid #b1b1b1;
+ box-shadow: 0 0 0 0 transparent;
+ font: caption;
+ height: 24px;
+ outline: 0 !important;
+ padding: 0 8px 0;
+ transition-duration: 250ms;
+ transition-property: box-shadow, border;
+}
+
+select {
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8cGF0aCBkPSJNOCwxMkwzLDcsNCw2bDQsNCw0LTQsMSwxWiIgZmlsbD0iIzZBNkE2QSIgLz4KPC9zdmc+Cg==);
+ background-position: calc(100% - 4px) center;
+ background-repeat: no-repeat;
+ padding-inline-end: 24px;
+ text-overflow: ellipsis;
+}
+
+label {
+ font: caption;
+}
+
+button::-moz-focus-inner {
+ border: 0;
+ outline: 0;
+}
+
+/* Dropdowns */
+select {
+ background-color: #fbfbfb;
+ border: 1px solid #b1b1b1;
+ box-shadow: 0 0 0 0 transparent;
+ font: caption;
+ height: 24px;
+ outline: 0 !important;
+ padding: 0 8px 0;
+ transition-duration: 250ms;
+ transition-property: box-shadow, border;
+}
+
+select {
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8cGF0aCBkPSJNOCwxMkwzLDcsNCw2bDQsNCw0LTQsMSwxWiIgZmlsbD0iIzZBNkE2QSIgLz4KPC9zdmc+Cg==);
+ background-position: calc(100% - 4px) center;
+ background-repeat: no-repeat;
+ padding-inline-end: 24px;
+ text-overflow: ellipsis;
+}
+
+select:-moz-focusring {
+ color: transparent;
+ text-shadow: 0 0 0 #000;
+}
+
+select:-moz-focusring * {
+ color: #000;
+ text-shadow: none;
+}
+
+button.hover,
+select.hover {
+ background-color: #ebebeb;
+ border: 1px solid #b1b1b1;
+}
+
+button.pressed,
+select.pressed {
+ background-color: #d4d4d4;
+ border: 1px solid #858585;
+}
+
+button.disabled,
+select.disabled {
+ color: #999;
+ opacity: .5;
+}
+
+button.focused,
+select.focused {
+ border-color: #fff;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+button.default {
+ background-color: #0996f8;
+ border-color: #0670cc;
+ color: #fff;
+}
+
+button.default.hover {
+ background-color: #0670cc;
+ border-color: #005bab;
+}
+
+button.default.pressed {
+ background-color: #005bab;
+ border-color: #004480;
+}
+
+button.default.focused {
+ border-color: #fff;
+}
+
+/* Radio Buttons */
+.radioItem {
+ margin-bottom: 6px;
+ text-align: left;
+}
+
+input[type="radio"] {
+ display: none;
+}
+
+input[type="radio"] + label {
+ -moz-user-select: none;
+}
+
+input[type="radio"] + label::before {
+ background-color: #fff;
+ background-position: center;
+ border: 1px solid #b1b1b1;
+ border-radius: 50%;
+ content: "";
+ display: inline-block;
+ height: 16px;
+ margin-right: 6px;
+ vertical-align: text-top;
+ width: 16px;
+}
+
+input[type="radio"]:hover + label::before,
+.radioItem.hover input[type="radio"]:not(active) + label::before {
+ background-color: #fbfbfb;
+ border-color: #b1b1b1;
+}
+
+input[type="radio"]:hover:active + label::before,
+.radioItem.pressed input[type="radio"]:not(active) + label::before {
+ background-color: #ebebeb;
+ border-color: #858585;
+}
+
+input[type="radio"]:checked + label::before {
+ background-color: #0996f8;
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8Y2lyY2xlIGN4PSI4IiBjeT0iOCIgcj0iNCIgZmlsbD0iI2ZmZiIgLz4KPC9zdmc+Cg==);
+ border-color: #0670cc;
+}
+
+input[type="radio"]:checked:hover + label::before,
+.radioItem.hover input[type="radio"]:checked:not(active) + label::before {
+ background-color: #0670cc;
+ border-color: #005bab;
+}
+
+input[type="radio"]:checked:hover:active + label::before,
+.radioItem.pressed input[type="radio"]:checked:not(active) + label::before {
+ background-color: #005bab;
+ border-color: #004480;
+}
+
+.radioItem.disabled input[type="radio"] + label,
+.radioItem.disabled input[type="radio"]:hover + label,
+.radioItem.disabled input[type="radio"]:hover:active + label {
+ color: #999;
+ opacity: .5;
+}
+
+.radioItem.focused input[type="radio"] + label::before {
+ border-color: #0996f8;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+.radioItem.focused input[type="radio"]:checked + label::before {
+ border-color: #fff;
+}
+
+/* Checkboxes */
+.checkboxItem {
+ margin-bottom: 6px;
+ text-align: left;
+}
+
+input[type="checkbox"] {
+ display: none;
+}
+
+input[type="checkbox"] + label {
+ -moz-user-select: none;
+}
+
+input[type="checkbox"] + label::before {
+ background-color: #fff;
+ background-position: center;
+ border: 1px solid #b1b1b1;
+ content: "";
+ display: inline-block;
+ height: 16px;
+ margin-right: 6px;
+ vertical-align: text-top;
+ width: 16px;
+}
+
+input[type="checkbox"]:hover + label::before,
+.checkboxItem.hover input[type="checkbox"]:not(active) + label::before {
+ background-color: #fbfbfb;
+ border-color: #b1b1b1;
+}
+
+input[type="checkbox"]:hover:active + label::before,
+.checkboxItem.pressed input[type="checkbox"]:not(active) + label::before {
+ background-color: #ebebeb;
+ border-color: #858585;
+}
+
+input[type="checkbox"]:checked + label::before {
+ background-color: #0996f8;
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8cGF0aCBkPSJNNy43LDEyLjkgQzcuNCwxMy4zIDYuOCwxMy40IDYuNCwxMyBMMy4yLDkuOCBDMi44LDkuNCAyLjgsOC42IDMuMiw4LjIgQzMuNiw3LjggNC40LDcuOCA0LjgsOC4yIEw2LjksMTAuMyBMMTEuMSw0LjQgQzExLjUsMy45IDEyLjIsMy44IDEyLjcsNC4xIEMxMy4yLDQuNSAxMy4zLDUuMiAxMyw1LjcgTDcuNywxMi45IEw3LjcsMTIuOSBaIiBmaWxsPSIjZmZmIiAvPgo8L3N2Zz4K);
+ border-color: #0670cc;
+}
+
+input[type="checkbox"]:checked:hover + label::before,
+.checkboxItem.hover input[type="checkbox"]:checked:not(active) + label::before {
+ background-color: #0670cc;
+ border-color: #005bab;
+}
+
+input[type="checkbox"]:checked:hover:active + label::before,
+.checkboxItem.pressed input[type="checkbox"]:checked:not(active) + label::before {
+ background-color: #005bab;
+ border-color: #004480;
+}
+
+.checkboxItem.disabled input[type="checkbox"] + label,
+.checkboxItem.disabled input[type="checkbox"]:hover + label,
+.checkboxItem.disabled input[type="checkbox"]:hover:active + label {
+ color: #999;
+ opacity: .5;
+}
+
+.checkboxItem.focused input[type="checkbox"] + label::before {
+ border-color: #0996f8;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+.checkboxItem.focused input[type="checkbox"]:checked + label::before {
+ border-color: #fff;
+}
+
+/* Expander Button */
+button.expander {
+ background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8cGF0aCBkPSJNOCwxMkwzLDcsNCw2bDQsNCw0LTQsMSwxWiIgZmlsbD0iIzZBNkE2QSIgLz4KPC9zdmc+Cg==);
+ background-position: center;
+ background-repeat: no-repeat;
+ height: 24px;
+ padding: 0;
+ width: 24px;
+}
+
+/* Interactive States */
+button:hover:not(.pressed):not(.disabled):not(.focused),
+select:hover:not(.pressed):not(.disabled):not(.focused) {
+ background-color: #ebebeb;
+ border: 1px solid #b1b1b1;
+}
+
+button:hover:active:not(.hover):not(.disabled):not(.focused),
+select:hover:active:not(.hover):not(.disabled):not(.focused) {
+ background-color: #d4d4d4;
+ border: 1px solid #858585;
+}
+
+button.default:hover:not(.pressed):not(.disabled):not(.focused) {
+ background-color: #0670cc;
+ border-color: #005bab;
+}
+
+button.default:hover:active:not(.hover):not(.disabled):not(.focused) {
+ background-color: #005bab;
+ border-color: #004480;
+}
+
+button:focus:not(.disabled) {
+ border-color: #fff !important;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+/* Fields */
+input[type="text"],
+textarea {
+ background-color: #fff;
+ border: 1px solid #b1b1b1;
+ box-shadow: 0 0 0 0 rgba(97, 181, 255, 0);
+ font: caption;
+ padding: 0 6px 0;
+ transition-duration: 250ms;
+ transition-property: box-shadow;
+}
+
+input[type="text"] {
+ height: 24px;
+}
+
+input[type="text"].hover,
+textarea.hover {
+ border: 1px solid #858585;
+}
+
+input[type="text"].disabled,
+textarea.disabled {
+ color: #999;
+ opacity: .5;
+}
+
+input[type="text"].focused,
+textarea.focused {
+ border-color: #0996f8;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+/* Interactive States */
+input[type="text"]:not(disabled):hover,
+textarea:not(disabled):hover {
+ border: 1px solid #858585;
+}
+
+input[type="text"]:focus,
+input[type="text"]:focus:hover,
+textarea:focus,
+textarea:focus:hover {
+ border-color: #0996f8;
+ box-shadow: 0 0 0 2px rgba(97, 181, 255, 0.75);
+}
+
+/* stylelint-disable property-no-vendor-prefix */
+.panel-section {
+ display: flex;
+ flex-direction: row;
+}
+
+.panel-section-separator {
+ background-color: rgba(0, 0, 0, 0.15);
+ min-height: 1px;
+}
+
+/* Panel Section - Header */
+.panel-section-header {
+ border-bottom: 1px solid rgba(0, 0, 0, 0.15);
+ padding: 16px;
+}
+
+.panel-section-header > .icon-section-header {
+ background-position: center center;
+ background-repeat: no-repeat;
+ height: 32px;
+ margin-right: 16px;
+ position: relative;
+ width: 32px;
+}
+
+.panel-section-header > .text-section-header {
+ align-self: center;
+ font-size: 1.385em;
+ font-weight: lighter;
+}
+
+/* Panel Section - List */
+.panel-section-list {
+ flex-direction: column;
+ padding: 4px 0;
+}
+
+.panel-list-item {
+ align-items: center;
+ display: flex;
+ flex-direction: row;
+ height: 24px;
+ padding: 0 16px;
+}
+
+.panel-list-item:not(.disabled):hover {
+ background-color: rgba(0, 0, 0, 0.06);
+ border-bottom: 1px solid rgba(0, 0, 0, 0.1);
+ border-top: 1px solid rgba(0, 0, 0, 0.1);
+}
+
+.panel-list-item:not(.disabled):hover:active {
+ background-color: rgba(0, 0, 0, 0.1);
+}
+
+.panel-list-item.disabled {
+ color: #999;
+}
+
+.panel-list-item > .icon {
+ flex-grow: 0;
+ flex-shrink: 0;
+}
+
+.panel-list-item > .text {
+ flex-grow: 10;
+}
+
+.panel-list-item > .text-shortcut {
+ color: #808080;
+ font-family: "Lucida Grande", caption;
+ font-size: .847em;
+ justify-content: flex-end;
+}
+
+.panel-section-list .panel-section-separator {
+ margin: 4px 0;
+}
+
+/* Panel Section - Form Elements */
+.panel-section-formElements {
+ display: flex;
+ flex-direction: column;
+ padding: 16px;
+}
+
+.panel-formElements-item {
+ align-items: center;
+ display: flex;
+ flex-direction: row;
+ margin-bottom: 12px;
+}
+
+.panel-formElements-item:last-child {
+ margin-bottom: 0;
+}
+
+.panel-formElements-item label {
+ flex-shrink: 0;
+ margin-right: 6px;
+ text-align: right;
+}
+
+.panel-formElements-item input[type="text"],
+.panel-formElements-item select {
+ flex-grow: 1;
+}
+
+/* Panel Section - Footer */
+.panel-section-footer {
+ background-color: rgba(0, 0, 0, 0.06);
+ border-top: 1px solid rgba(0, 0, 0, 0.15);
+ color: #1a1a1a;
+ display: flex;
+ flex-direction: row;
+ height: 41px;
+ margin-top: -1px;
+ padding: 0;
+}
+
+.panel-section-footer-button {
+ flex: 1 1 auto;
+ height: 100%;
+ margin: 0 -1px;
+ padding: 12px;
+ text-align: center;
+}
+
+.panel-section-footer-button > .text-shortcut {
+ color: #808080;
+ font-family: "Lucida Grande", caption;
+ font-size: .847em;
+}
+
+.panel-section-footer-button:hover {
+ background-color: rgba(0, 0, 0, 0.06);
+}
+
+.panel-section-footer-button:hover:active {
+ background-color: rgba(0, 0, 0, 0.1);
+}
+
+.panel-section-footer-button.default {
+ background-color: #0996f8;
+ box-shadow: 0 1px 0 #0670cc inset;
+ color: #fff;
+}
+
+.panel-section-footer-button.default:hover {
+ background-color: #0670cc;
+ box-shadow: 0 1px 0 #005bab inset;
+}
+
+.panel-section-footer-button.default:hover:active {
+ background-color: #005bab;
+ box-shadow: 0 1px 0 #004480 inset;
+}
+
+.panel-section-footer-separator {
+ background-color: rgba(0, 0, 0, 0.1);
+ width: 1px;
+ z-index: 99;
+}
+
+/* Panel Section - Tabs */
+.panel-section-tabs {
+ color: #1a1a1a;
+ display: flex;
+ flex-direction: row;
+ height: 41px;
+ margin-bottom: -1px;
+ padding: 0;
+}
+
+.panel-section-tabs-button {
+ flex: 1 1 auto;
+ height: 100%;
+ margin: 0 -1px;
+ padding: 12px;
+ text-align: center;
+}
+
+.panel-section-tabs-button:hover {
+ background-color: rgba(0, 0, 0, 0.06);
+}
+
+.panel-section-tabs-button:hover:active {
+ background-color: rgba(0, 0, 0, 0.1);
+}
+
+.panel-section-tabs-button.selected {
+ box-shadow: 0 -1px 0 #0670cc inset, 0 -4px 0 #0996f8 inset;
+ color: #0996f8;
+}
+
+.panel-section-tabs-button.selected:hover {
+ color: #0670cc;
+}
+
+.panel-section-tabs-separator {
+ background-color: rgba(0, 0, 0, 0.1);
+ width: 1px;
+ z-index: 99;
+}
diff --git a/browser/components/extensions/extension.svg b/browser/components/extensions/extension.svg
new file mode 100644
index 000000000..a16455253
--- /dev/null
+++ b/browser/components/extensions/extension.svg
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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/. -->
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
+ width="64" height="64" viewBox="0 0 64 64">
+ <defs>
+ <style>
+ .style-puzzle-piece {
+ fill: url('#gradient-linear-puzzle-piece');
+ }
+ </style>
+ <linearGradient id="gradient-linear-puzzle-piece" x1="0%" y1="0%" x2="0%" y2="100%">
+ <stop offset="0%" stop-color="#66cc52" stop-opacity="1"/>
+ <stop offset="100%" stop-color="#60bf4c" stop-opacity="1"/>
+ </linearGradient>
+ </defs>
+ <path class="style-puzzle-piece" d="M42,62c2.2,0,4-1.8,4-4l0-14.2c0,0,0.4-3.7,2.8-3.7c2.4,0,2.2,3.9,6.7,3.9c2.3,0,6.2-1.2,6.2-8.2 c0-7-3.9-7.9-6.2-7.9c-4.5,0-4.3,3.7-6.7,3.7c-2.4,0-2.8-3.8-2.8-3.8V22c0-2.2-1.8-4-4-4H31.5c0,0-3.4-0.6-3.4-3 c0-2.4,3.8-2.6,3.8-7.1c0-2.3-1.3-5.9-8.3-5.9s-8,3.6-8,5.9c0,4.5,3.4,4.7,3.4,7.1c0,2.4-3.4,3-3.4,3H6c-2.2,0-4,1.8-4,4l0,7.8 c0,0-0.4,6,4.4,6c3.1,0,3.2-4.1,7.3-4.1c2,0,4,1.9,4,6c0,4.2-2,6.3-4,6.3c-4,0-4.2-4.1-7.3-4.1c-4.8,0-4.4,5.8-4.4,5.8L2,58 c0,2.2,1.8,4,4,4H19c0,0,6.3,0.4,6.3-4.4c0-3.1-4-3.6-4-7.7c0-2,2.2-4.5,6.4-4.5c4.2,0,6.6,2.5,6.6,4.5c0,4-3.9,4.6-3.9,7.7 c0,4.9,6.3,4.4,6.3,4.4H42z"/>
+</svg>
diff --git a/browser/components/extensions/extensions-browser.manifest b/browser/components/extensions/extensions-browser.manifest
new file mode 100644
index 000000000..ed5cca813
--- /dev/null
+++ b/browser/components/extensions/extensions-browser.manifest
@@ -0,0 +1,31 @@
+# scripts
+category webextension-scripts bookmarks chrome://browser/content/ext-bookmarks.js
+category webextension-scripts browserAction chrome://browser/content/ext-browserAction.js
+category webextension-scripts commands chrome://browser/content/ext-commands.js
+category webextension-scripts contextMenus chrome://browser/content/ext-contextMenus.js
+category webextension-scripts desktop-runtime chrome://browser/content/ext-desktop-runtime.js
+category webextension-scripts history chrome://browser/content/ext-history.js
+category webextension-scripts omnibox chrome://browser/content/ext-omnibox.js
+category webextension-scripts pageAction chrome://browser/content/ext-pageAction.js
+category webextension-scripts sessions chrome://browser/content/ext-sessions.js
+category webextension-scripts tabs chrome://browser/content/ext-tabs.js
+category webextension-scripts utils chrome://browser/content/ext-utils.js
+category webextension-scripts windows chrome://browser/content/ext-windows.js
+
+# scripts that must run in the same process as addon code.
+category webextension-scripts-addon contextMenus chrome://browser/content/ext-c-contextMenus.js
+category webextension-scripts-addon omnibox chrome://browser/content/ext-c-omnibox.js
+category webextension-scripts-addon tabs chrome://browser/content/ext-c-tabs.js
+
+# schemas
+category webextension-schemas bookmarks chrome://browser/content/schemas/bookmarks.json
+category webextension-schemas browser_action chrome://browser/content/schemas/browser_action.json
+category webextension-schemas commands chrome://browser/content/schemas/commands.json
+category webextension-schemas context_menus chrome://browser/content/schemas/context_menus.json
+category webextension-schemas context_menus_internal chrome://browser/content/schemas/context_menus_internal.json
+category webextension-schemas history chrome://browser/content/schemas/history.json
+category webextension-schemas omnibox chrome://browser/content/schemas/omnibox.json
+category webextension-schemas page_action chrome://browser/content/schemas/page_action.json
+category webextension-schemas sessions chrome://browser/content/schemas/sessions.json
+category webextension-schemas tabs chrome://browser/content/schemas/tabs.json
+category webextension-schemas windows chrome://browser/content/schemas/windows.json
diff --git a/browser/components/extensions/jar.mn b/browser/components/extensions/jar.mn
new file mode 100644
index 000000000..a7b506ec4
--- /dev/null
+++ b/browser/components/extensions/jar.mn
@@ -0,0 +1,29 @@
+# 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/.
+
+browser.jar:
+ content/browser/extension.css
+#ifdef XP_MACOSX
+ content/browser/extension-mac.css
+ content/browser/extension-mac-panel.css
+#endif
+#ifdef XP_WIN
+ content/browser/extension-win-panel.css
+#endif
+ content/browser/extension.svg
+ content/browser/ext-bookmarks.js
+ content/browser/ext-browserAction.js
+ content/browser/ext-commands.js
+ content/browser/ext-contextMenus.js
+ content/browser/ext-desktop-runtime.js
+ content/browser/ext-history.js
+ content/browser/ext-omnibox.js
+ content/browser/ext-pageAction.js
+ content/browser/ext-sessions.js
+ content/browser/ext-tabs.js
+ content/browser/ext-utils.js
+ content/browser/ext-windows.js
+ content/browser/ext-c-contextMenus.js
+ content/browser/ext-c-omnibox.js
+ content/browser/ext-c-tabs.js
diff --git a/browser/components/extensions/moz.build b/browser/components/extensions/moz.build
new file mode 100644
index 000000000..5b3654c3a
--- /dev/null
+++ b/browser/components/extensions/moz.build
@@ -0,0 +1,17 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+JAR_MANIFESTS += ['jar.mn']
+
+EXTRA_COMPONENTS += [
+ 'extensions-browser.manifest',
+]
+
+DIRS += ['schemas']
+
+BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
+MOCHITEST_MANIFESTS += ['test/mochitest/mochitest.ini']
+XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
diff --git a/browser/components/extensions/schemas/LICENSE b/browser/components/extensions/schemas/LICENSE
new file mode 100644
index 000000000..9314092fd
--- /dev/null
+++ b/browser/components/extensions/schemas/LICENSE
@@ -0,0 +1,27 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/browser/components/extensions/schemas/bookmarks.json b/browser/components/extensions/schemas/bookmarks.json
new file mode 100644
index 000000000..fb74c633e
--- /dev/null
+++ b/browser/components/extensions/schemas/bookmarks.json
@@ -0,0 +1,568 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "Permission",
+ "choices": [{
+ "type": "string",
+ "enum": [
+ "bookmarks"
+ ]
+ }]
+ }
+ ]
+ },
+ {
+ "namespace": "bookmarks",
+ "description": "Use the <code>browser.bookmarks</code> API to create, organize, and otherwise manipulate bookmarks. Also see $(topic:override)[Override Pages], which you can use to create a custom Bookmark Manager page.",
+ "permissions": ["bookmarks"],
+ "types": [
+ {
+ "id": "BookmarkTreeNodeUnmodifiable",
+ "type": "string",
+ "enum": ["managed"],
+ "description": "Indicates the reason why this node is unmodifiable. The <var>managed</var> value indicates that this node was configured by the system administrator or by the custodian of a supervised user. Omitted if the node can be modified by the user and the extension (default)."
+ },
+ {
+ "id": "BookmarkTreeNode",
+ "type": "object",
+ "description": "A node (either a bookmark or a folder) in the bookmark tree. Child nodes are ordered within their parent folder.",
+ "properties": {
+ "id": {
+ "type": "string",
+ "description": "The unique identifier for the node. IDs are unique within the current profile, and they remain valid even after the browser is restarted."
+ },
+ "parentId": {
+ "type": "string",
+ "optional": true,
+ "description": "The <code>id</code> of the parent folder. Omitted for the root node."
+ },
+ "index": {
+ "type": "integer",
+ "optional": true,
+ "description": "The 0-based position of this node within its parent folder."
+ },
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL navigated to when a user clicks the bookmark. Omitted for folders."
+ },
+ "title": {
+ "type": "string",
+ "description": "The text displayed for the node."
+ },
+ "dateAdded": {
+ "type": "number",
+ "optional": true,
+ "description": "When this node was created, in milliseconds since the epoch (<code>new Date(dateAdded)</code>)."
+ },
+ "dateGroupModified": {
+ "type": "number",
+ "optional": true,
+ "description": "When the contents of this folder last changed, in milliseconds since the epoch."
+ },
+ "unmodifiable": {
+ "$ref": "BookmarkTreeNodeUnmodifiable",
+ "optional": true,
+ "description": "Indicates the reason why this node is unmodifiable. The <var>managed</var> value indicates that this node was configured by the system administrator or by the custodian of a supervised user. Omitted if the node can be modified by the user and the extension (default)."
+ },
+ "children": {
+ "type": "array",
+ "optional": true,
+ "items": { "$ref": "BookmarkTreeNode" },
+ "description": "An ordered list of children of this node."
+ }
+ }
+ },
+ {
+ "id": "CreateDetails",
+ "description": "Object passed to the create() function.",
+ "type": "object",
+ "properties": {
+ "parentId": {
+ "type": "string",
+ "optional": true,
+ "description": "Defaults to the Other Bookmarks folder."
+ },
+ "index": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true
+ },
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
+ }
+ }
+ ],
+ "functions": [
+ {
+ "name": "get",
+ "type": "function",
+ "description": "Retrieves the specified BookmarkTreeNode(s).",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "idOrIdList",
+ "description": "A single string-valued id, or an array of string-valued ids",
+ "choices": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "minItems": 1
+ }
+ ]
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getChildren",
+ "type": "function",
+ "description": "Retrieves the children of the specified BookmarkTreeNode id.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode"}
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getRecent",
+ "type": "function",
+ "description": "Retrieves the recently added bookmarks.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "minimum": 1,
+ "name": "numberOfItems",
+ "description": "The maximum number of items to return."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getTree",
+ "type": "function",
+ "description": "Retrieves the entire Bookmarks hierarchy.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getSubTree",
+ "type": "function",
+ "description": "Retrieves part of the Bookmarks hierarchy, starting at the specified node.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id",
+ "description": "The ID of the root of the subtree to retrieve."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "search",
+ "type": "function",
+ "description": "Searches for BookmarkTreeNodes matching the given query. Queries specified with an object produce BookmarkTreeNodes matching all specified properties.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "query",
+ "description": "Either a string of words and quoted phrases that are matched against bookmark URLs and titles, or an object. If an object, the properties <code>query</code>, <code>url</code>, and <code>title</code> may be specified and bookmarks matching all specified properties will be produced.",
+ "choices": [
+ {
+ "type": "string",
+ "description": "A string of words and quoted phrases that are matched against bookmark URLs and titles."
+ },
+ {
+ "type": "object",
+ "description": "An object specifying properties and values to match when searching. Produces bookmarks matching all properties.",
+ "properties": {
+ "query": {
+ "type": "string",
+ "optional": true,
+ "description": "A string of words and quoted phrases that are matched against bookmark URLs and titles."
+ },
+ "url": {
+ "type": "string",
+ "format": "url",
+ "optional": true,
+ "description": "The URL of the bookmark; matches verbatim. Note that folders have no URL."
+ },
+ "title": {
+ "type": "string",
+ "optional": true,
+ "description": "The title of the bookmark; matches verbatim."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": { "$ref": "BookmarkTreeNode" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "create",
+ "type": "function",
+ "description": "Creates a bookmark or folder under the specified parentId. If url is NULL or missing, it will be a folder.",
+ "async": "callback",
+ "parameters": [
+ {
+ "$ref": "CreateDetails",
+ "name": "bookmark"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "move",
+ "type": "function",
+ "description": "Moves the specified BookmarkTreeNode to the provided location.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "destination",
+ "properties": {
+ "parentId": {
+ "type": "string",
+ "optional": true
+ },
+ "index": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "update",
+ "type": "function",
+ "description": "Updates the properties of a bookmark or folder. Specify only the properties that you want to change; unspecified properties will be left unchanged. <b>Note:</b> Currently, only 'title' and 'url' are supported.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "changes",
+ "properties": {
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "result",
+ "$ref": "BookmarkTreeNode"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "remove",
+ "type": "function",
+ "description": "Removes a bookmark or an empty bookmark folder.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "removeTree",
+ "type": "function",
+ "description": "Recursively removes a bookmark folder.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "import",
+ "unsupported": true,
+ "type": "function",
+ "description": "Imports bookmarks from an html bookmark file",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "export",
+ "unsupported": true,
+ "type": "function",
+ "description": "Exports bookmarks to an html bookmark file",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onCreated",
+ "type": "function",
+ "description": "Fired when a bookmark or folder is created.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "$ref": "BookmarkTreeNode",
+ "name": "bookmark"
+ }
+ ]
+ },
+ {
+ "name": "onRemoved",
+ "type": "function",
+ "description": "Fired when a bookmark or folder is removed. When a folder is removed recursively, a single notification is fired for the folder, and none for its contents.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "removeInfo",
+ "properties": {
+ "parentId": { "type": "string" },
+ "index": { "type": "integer" },
+ "node": { "$ref": "BookmarkTreeNode" }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onChanged",
+ "type": "function",
+ "description": "Fired when a bookmark or folder changes. <b>Note:</b> Currently, only title and url changes trigger this.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "changeInfo",
+ "properties": {
+ "title": { "type": "string" },
+ "url": {
+ "type": "string",
+ "optional": true
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onMoved",
+ "type": "function",
+ "description": "Fired when a bookmark or folder is moved to a different parent folder.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "moveInfo",
+ "properties": {
+ "parentId": { "type": "string" },
+ "index": { "type": "integer" },
+ "oldParentId": { "type": "string" },
+ "oldIndex": { "type": "integer" }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onChildrenReordered",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fired when the children of a folder have changed their order due to the order being sorted in the UI. This is not called as a result of a move().",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "id"
+ },
+ {
+ "type": "object",
+ "name": "reorderInfo",
+ "properties": {
+ "childIds": {
+ "type": "array",
+ "items": { "type": "string" }
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onImportBegan",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fired when a bookmark import session is begun. Expensive observers should ignore onCreated updates until onImportEnded is fired. Observers should still handle other notifications immediately.",
+ "parameters": []
+ },
+ {
+ "name": "onImportEnded",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fired when a bookmark import session is ended.",
+ "parameters": []
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/browser_action.json b/browser/components/extensions/schemas/browser_action.json
new file mode 100644
index 000000000..1a7da956a
--- /dev/null
+++ b/browser/components/extensions/schemas/browser_action.json
@@ -0,0 +1,430 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "WebExtensionManifest",
+ "properties": {
+ "browser_action": {
+ "type": "object",
+ "additionalProperties": { "$ref": "UnrecognizedProperty" },
+ "properties": {
+ "default_title": {
+ "type": "string",
+ "optional": true,
+ "preprocess": "localize"
+ },
+ "default_icon": {
+ "$ref": "IconPath",
+ "optional": true
+ },
+ "default_popup": {
+ "type": "string",
+ "format": "relativeUrl",
+ "optional": true,
+ "preprocess": "localize"
+ },
+ "browser_style": {
+ "type": "boolean",
+ "optional": true
+ }
+ },
+ "optional": true
+ }
+ }
+ }
+ ]
+ },
+ {
+ "namespace": "browserAction",
+ "description": "Use browser actions to put icons in the main browser toolbar, to the right of the address bar. In addition to its icon, a browser action can also have a tooltip, a badge, and a popup.",
+ "permissions": ["manifest:browser_action"],
+ "types": [
+ {
+ "id": "ColorArray",
+ "type": "array",
+ "items": {
+ "type": "integer",
+ "minimum": 0,
+ "maximum": 255
+ },
+ "minItems": 4,
+ "maxItems": 4
+ },
+ {
+ "id": "ImageDataType",
+ "type": "object",
+ "isInstanceOf": "ImageData",
+ "additionalProperties": { "type": "any" },
+ "postprocess": "convertImageDataToURL",
+ "description": "Pixel data for an image. Must be an ImageData object (for example, from a <code>canvas</code> element)."
+ }
+ ],
+ "functions": [
+ {
+ "name": "setTitle",
+ "type": "function",
+ "description": "Sets the title of the browser action. This shows up in the tooltip.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "title": {
+ "type": "string",
+ "description": "The string the browser action should display when moused over."
+ },
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getTitle",
+ "type": "function",
+ "description": "Gets the title of the browser action.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Specify the tab to get the title from. If no tab is specified, the non-tab-specific title is returned."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "setIcon",
+ "type": "function",
+ "description": "Sets the icon for the browser action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the <b>path</b> or the <b>imageData</b> property must be specified.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "imageData": {
+ "choices": [
+ { "$ref": "ImageDataType" },
+ {
+ "type": "object",
+ "additionalProperties": {"$ref": "ImageDataType"}
+ }
+ ],
+ "optional": true,
+ "description": "Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals <code>scale</code>, then image with size <code>scale</code> * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'19': foo}'"
+ },
+ "path": {
+ "choices": [
+ { "type": "string" },
+ {
+ "type": "object",
+ "additionalProperties": {"type": "string"}
+ }
+ ],
+ "optional": true,
+ "description": "Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals <code>scale</code>, then image with size <code>scale</code> * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.imageData = {'19': foo}'"
+ },
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "setPopup",
+ "type": "function",
+ "description": "Sets the html document to be opened as a popup when the user clicks on the browser action's icon.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 0,
+ "description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
+ },
+ "popup": {
+ "type": "string",
+ "description": "The html file to show in a popup. If set to the empty string (''), no popup is shown."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getPopup",
+ "type": "function",
+ "description": "Gets the html document set as the popup for this browser action.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Specify the tab to get the popup from. If no tab is specified, the non-tab-specific popup is returned."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "setBadgeText",
+ "type": "function",
+ "description": "Sets the badge text for the browser action. The badge is displayed on top of the icon.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "text": {
+ "type": "string",
+ "description": "Any number of characters can be passed, but only about four can fit in the space."
+ },
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getBadgeText",
+ "type": "function",
+ "description": "Gets the badge text of the browser action. If no tab is specified, the non-tab-specific badge text is returned.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Specify the tab to get the badge text from. If no tab is specified, the non-tab-specific badge text is returned."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "setBadgeBackgroundColor",
+ "type": "function",
+ "description": "Sets the background color for the badge.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "color": {
+ "description": "An array of four integers in the range [0,255] that make up the RGBA color of the badge. For example, opaque red is <code>[255, 0, 0, 255]</code>. Can also be a string with a CSS value, with opaque red being <code>#FF0000</code> or <code>#F00</code>.",
+ "choices": [
+ {"type": "string"},
+ {"$ref": "ColorArray"}
+ ]
+ },
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Limits the change to when a particular tab is selected. Automatically resets when the tab is closed."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getBadgeBackgroundColor",
+ "type": "function",
+ "description": "Gets the background color of the browser action.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "optional": true,
+ "description": "Specify the tab to get the badge background color from. If no tab is specified, the non-tab-specific badge background color is returned."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "$ref": "ColorArray"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "enable",
+ "type": "function",
+ "description": "Enables the browser action for a tab. By default, browser actions are enabled.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "optional": true,
+ "name": "tabId",
+ "minimum": 0,
+ "description": "The id of the tab for which you want to modify the browser action."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "disable",
+ "type": "function",
+ "description": "Disables the browser action for a tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "optional": true,
+ "name": "tabId",
+ "minimum": 0,
+ "description": "The id of the tab for which you want to modify the browser action."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "openPopup",
+ "type": "function",
+ "description": "Opens the extension popup window in the active window but does not grant tab permissions.",
+ "unsupported": true,
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "popupView",
+ "type": "object",
+ "optional": true,
+ "description": "JavaScript 'window' object for the popup window if it was succesfully opened.",
+ "additionalProperties": { "type": "any" }
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onClicked",
+ "type": "function",
+ "description": "Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup.",
+ "parameters": [
+ {
+ "name": "tab",
+ "$ref": "tabs.Tab"
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/commands.json b/browser/components/extensions/schemas/commands.json
new file mode 100644
index 000000000..a1632088e
--- /dev/null
+++ b/browser/components/extensions/schemas/commands.json
@@ -0,0 +1,148 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "id": "KeyName",
+ "choices": [
+ {
+ "type": "string",
+ "pattern": "^\\s*(Alt|Ctrl|Command|MacCtrl)\\s*\\+\\s*(Shift\\s*\\+\\s*)?([A-Z0-9]|Comma|Period|Home|End|PageUp|PageDown|Space|Insert|Delete|Up|Down|Left|Right)\\s*$"
+ },
+ {
+ "type": "string",
+ "pattern": "^(MediaNextTrack|MediaPlayPause|MediaPrevTrack|MediaStop)$"
+ }
+ ]
+ },
+ {
+ "$extend": "WebExtensionManifest",
+ "properties": {
+ "commands": {
+ "type": "object",
+ "optional": true,
+ "additionalProperties": {
+ "type": "object",
+ "additionalProperties": { "$ref": "UnrecognizedProperty" },
+ "properties": {
+ "suggested_key": {
+ "type": "object",
+ "optional": true,
+ "properties": {
+ "default": {
+ "$ref": "KeyName",
+ "optional": true
+ },
+ "mac": {
+ "$ref": "KeyName",
+ "optional": true
+ },
+ "linux": {
+ "$ref": "KeyName",
+ "optional": true
+ },
+ "windows": {
+ "$ref": "KeyName",
+ "optional": true
+ },
+ "chromeos": {
+ "type": "string",
+ "optional": true
+ },
+ "android": {
+ "type": "string",
+ "optional": true
+ },
+ "ios": {
+ "type": "string",
+ "optional": true
+ },
+ "additionalProperties": {
+ "type": "string",
+ "deprecated": "Unknown platform name",
+ "optional": true
+ }
+ }
+ },
+ "description": {
+ "type": "string",
+ "optional": true
+ }
+ }
+ }
+ }
+ }
+ }
+ ]
+ },
+ {
+ "namespace": "commands",
+ "description": "Use the commands API to add keyboard shortcuts that trigger actions in your extension, for example, an action to open the browser action or send a command to the xtension.",
+ "permissions": ["manifest:commands"],
+ "types": [
+ {
+ "id": "Command",
+ "type": "object",
+ "properties": {
+ "name": {
+ "type": "string",
+ "optional": true,
+ "description": "The name of the Extension Command"
+ },
+ "description": {
+ "type": "string",
+ "optional": true,
+ "description": "The Extension Command description"
+ },
+ "shortcut": {
+ "type": "string",
+ "optional": true,
+ "description": "The shortcut active for this command, or blank if not active."
+ }
+ }
+ }
+ ],
+ "events": [
+ {
+ "name": "onCommand",
+ "description": "Fired when a registered command is activated using a keyboard shortcut.",
+ "type": "function",
+ "parameters": [
+ {
+ "name": "command",
+ "type": "string"
+ }
+ ]
+ }
+ ],
+ "functions": [
+ {
+ "name": "getAll",
+ "type": "function",
+ "async": "callback",
+ "description": "Returns all the registered extension commands for this extension and their shortcut (if active).",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "commands",
+ "type": "array",
+ "items": {
+ "$ref": "Command"
+ }
+ }
+ ],
+ "description": "Called to return the registered commands."
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/context_menus.json b/browser/components/extensions/schemas/context_menus.json
new file mode 100644
index 000000000..b31af51f3
--- /dev/null
+++ b/browser/components/extensions/schemas/context_menus.json
@@ -0,0 +1,424 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "Permission",
+ "choices": [{
+ "type": "string",
+ "enum": [
+ "contextMenus"
+ ]
+ }]
+ }
+ ]
+ },
+ {
+ "namespace": "contextMenus",
+ "description": "Use the <code>browser.contextMenus</code> API to add items to the browser's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
+ "permissions": ["contextMenus"],
+ "properties": {
+ "ACTION_MENU_TOP_LEVEL_LIMIT": {
+ "value": 6,
+ "description": "The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored."
+ }
+ },
+ "types": [
+ {
+ "id": "ContextType",
+ "type": "string",
+ "enum": ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", "launcher", "browser_action", "page_action"],
+ "description": "The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking on the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu."
+ },
+ {
+ "id": "ItemType",
+ "type": "string",
+ "enum": ["normal", "checkbox", "radio", "separator"],
+ "description": "The type of menu item."
+ },
+ {
+ "id": "OnClickData",
+ "type": "object",
+ "description": "Information sent when a context menu item is clicked.",
+ "properties": {
+ "menuItemId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "description": "The ID of the menu item that was clicked."
+ },
+ "parentMenuItemId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "optional": true,
+ "description": "The parent ID, if any, for the item clicked."
+ },
+ "mediaType": {
+ "type": "string",
+ "optional": true,
+ "description": "One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements."
+ },
+ "linkUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "If the element is a link, the URL it points to."
+ },
+ "srcUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "Will be present for elements with a 'src' URL."
+ },
+ "pageUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu."
+ },
+ "frameUrl": {
+ "type": "string",
+ "optional": true,
+ "description": " The URL of the frame of the element where the context menu was clicked, if it was in a frame."
+ },
+ "selectionText": {
+ "type": "string",
+ "optional": true,
+ "description": "The text for the context selection, if any."
+ },
+ "editable": {
+ "type": "boolean",
+ "description": "A flag indicating whether the element is editable (text input, textarea, etc.)."
+ },
+ "wasChecked": {
+ "type": "boolean",
+ "optional": true,
+ "description": "A flag indicating the state of a checkbox or radio item before it was clicked."
+ },
+ "checked": {
+ "type": "boolean",
+ "optional": true,
+ "description": "A flag indicating the state of a checkbox or radio item after it is clicked."
+ }
+ }
+ }
+ ],
+ "functions": [
+ {
+ "name": "create",
+ "type": "function",
+ "description": "Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in $(ref:runtime.lastError)).",
+ "returns": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "description": "The ID of the newly created item."
+ },
+ "parameters": [
+ {
+ "type": "object",
+ "name": "createProperties",
+ "properties": {
+ "type": {
+ "$ref": "ItemType",
+ "optional": true,
+ "description": "The type of menu item. Defaults to 'normal' if not specified."
+ },
+ "id": {
+ "type": "string",
+ "optional": true,
+ "description": "The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension."
+ },
+ "title": {
+ "type": "string",
+ "optional": true,
+ "description": "The text to be displayed in the item; this is <em>required</em> unless <code>type</code> is 'separator'. When the context is 'selection', you can use <code>%s</code> within the string to show the selected text. For example, if this parameter's value is \"Translate '%s' to Pig Latin\" and the user selects the word \"cool\", the context menu item for the selection is \"Translate 'cool' to Pig Latin\"."
+ },
+ "checked": {
+ "type": "boolean",
+ "optional": true,
+ "description": "The initial state of a checkbox or radio item: true for selected and false for unselected. Only one radio item can be selected at a time in a given group of radio items."
+ },
+ "contexts": {
+ "type": "array",
+ "items": {
+ "$ref": "ContextType"
+ },
+ "minItems": 1,
+ "optional": true,
+ "description": "List of contexts this menu item will appear in. Defaults to ['page'] if not specified."
+ },
+ "onclick": {
+ "type": "function",
+ "optional": true,
+ "description": "A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for $(ref:contextMenus.onClicked).",
+ "parameters": [
+ {
+ "name": "info",
+ "$ref": "contextMenusInternal.OnClickData",
+ "description": "Information about the item clicked and the context where the click happened."
+ },
+ {
+ "name": "tab",
+ "$ref": "tabs.Tab",
+ "description": "The details of the tab where the click took place. Note: this parameter only present for extensions."
+ }
+ ]
+ },
+ "parentId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "optional": true,
+ "description": "The ID of a parent menu item; this makes the item a child of a previously added item."
+ },
+ "documentUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true,
+ "description": "Lets you restrict the item to apply only to documents whose URL matches one of the given patterns. (This applies to frames as well.) For details on the format of a pattern, see $(topic:match_patterns)[Match Patterns]."
+ },
+ "targetUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true,
+ "description": "Similar to documentUrlPatterns, but lets you filter based on the src attribute of img/audio/video tags and the href of anchor tags."
+ },
+ "enabled": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether this context menu item is enabled or disabled. Defaults to true."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called when the item has been created in the browser. If there were any problems creating the item, details will be available in $(ref:runtime.lastError).",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "createInternal",
+ "type": "function",
+ "allowedContexts": ["addon_parent_only"],
+ "async": "callback",
+ "description": "Identical to contextMenus.create, except: the 'id' field is required and allows an integer, 'onclick' is not allowed, and the method is async (and the return value is not a menu item ID).",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "createProperties",
+ "properties": {
+ "type": {
+ "$ref": "ItemType",
+ "optional": true
+ },
+ "id": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ]
+ },
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "checked": {
+ "type": "boolean",
+ "optional": true
+ },
+ "contexts": {
+ "type": "array",
+ "items": {
+ "$ref": "ContextType"
+ },
+ "minItems": 1,
+ "optional": true
+ },
+ "parentId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "optional": true
+ },
+ "documentUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true
+ },
+ "targetUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true
+ },
+ "enabled": {
+ "type": "boolean",
+ "optional": true
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "update",
+ "type": "function",
+ "description": "Updates a previously created context menu item.",
+ "async": "callback",
+ "parameters": [
+ {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "name": "id",
+ "description": "The ID of the item to update."
+ },
+ {
+ "type": "object",
+ "name": "updateProperties",
+ "description": "The properties to update. Accepts the same values as the create function.",
+ "properties": {
+ "type": {
+ "$ref": "ItemType",
+ "optional": true
+ },
+ "title": {
+ "type": "string",
+ "optional": true
+ },
+ "checked": {
+ "type": "boolean",
+ "optional": true
+ },
+ "contexts": {
+ "type": "array",
+ "items": {
+ "$ref": "ContextType"
+ },
+ "minItems": 1,
+ "optional": true
+ },
+ "onclick": {
+ "type": "function",
+ "optional": "omit-key-if-missing",
+ "parameters": [
+ {
+ "name": "info",
+ "$ref": "contextMenusInternal.OnClickData"
+ },
+ {
+ "name": "tab",
+ "$ref": "tabs.Tab",
+ "description": "The details of the tab where the click took place. Note: this parameter only present for extensions."
+ }
+ ]
+ },
+ "parentId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "optional": true,
+ "description": "Note: You cannot change an item to be a child of one of its own descendants."
+ },
+ "documentUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true
+ },
+ "targetUrlPatterns": {
+ "type": "array",
+ "items": {"type": "string"},
+ "optional": true
+ },
+ "enabled": {
+ "type": "boolean",
+ "optional": true
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [],
+ "description": "Called when the context menu has been updated."
+ }
+ ]
+ },
+ {
+ "name": "remove",
+ "type": "function",
+ "description": "Removes a context menu item.",
+ "async": "callback",
+ "parameters": [
+ {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "name": "menuItemId",
+ "description": "The ID of the context menu item to remove."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [],
+ "description": "Called when the context menu has been removed."
+ }
+ ]
+ },
+ {
+ "name": "removeAll",
+ "type": "function",
+ "description": "Removes all context menu items added by this extension.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [],
+ "description": "Called when removal is complete."
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onClicked",
+ "type": "function",
+ "description": "Fired when a context menu item is clicked.",
+ "parameters": [
+ {
+ "name": "info",
+ "$ref": "OnClickData",
+ "description": "Information about the item clicked and the context where the click happened."
+ },
+ {
+ "name": "tab",
+ "$ref": "tabs.Tab",
+ "description": "The details of the tab where the click took place. If the click did not take place in a tab, this parameter will be missing.",
+ "optional": true
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/context_menus_internal.json b/browser/components/extensions/schemas/context_menus_internal.json
new file mode 100644
index 000000000..c3cb7aff0
--- /dev/null
+++ b/browser/components/extensions/schemas/context_menus_internal.json
@@ -0,0 +1,78 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "contextMenusInternal",
+ "description": "Use the <code>browser.contextMenus</code> API to add items to the browser's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
+ "types": [
+ {
+ "id": "OnClickData",
+ "type": "object",
+ "description": "Information sent when a context menu item is clicked.",
+ "properties": {
+ "menuItemId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "description": "The ID of the menu item that was clicked."
+ },
+ "parentMenuItemId": {
+ "choices": [
+ { "type": "integer" },
+ { "type": "string" }
+ ],
+ "optional": true,
+ "description": "The parent ID, if any, for the item clicked."
+ },
+ "mediaType": {
+ "type": "string",
+ "optional": true,
+ "description": "One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements."
+ },
+ "linkUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "If the element is a link, the URL it points to."
+ },
+ "srcUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "Will be present for elements with a 'src' URL."
+ },
+ "pageUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu."
+ },
+ "frameUrl": {
+ "type": "string",
+ "optional": true,
+ "description": " The URL of the frame of the element where the context menu was clicked, if it was in a frame."
+ },
+ "selectionText": {
+ "type": "string",
+ "optional": true,
+ "description": "The text for the context selection, if any."
+ },
+ "editable": {
+ "type": "boolean",
+ "description": "A flag indicating whether the element is editable (text input, textarea, etc.)."
+ },
+ "wasChecked": {
+ "type": "boolean",
+ "optional": true,
+ "description": "A flag indicating the state of a checkbox or radio item before it was clicked."
+ },
+ "checked": {
+ "type": "boolean",
+ "optional": true,
+ "description": "A flag indicating the state of a checkbox or radio item after it is clicked."
+ }
+ }
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/history.json b/browser/components/extensions/schemas/history.json
new file mode 100644
index 000000000..e05569e38
--- /dev/null
+++ b/browser/components/extensions/schemas/history.json
@@ -0,0 +1,316 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "Permission",
+ "choices": [{
+ "type": "string",
+ "enum": [
+ "history"
+ ]
+ }]
+ }
+ ]
+ },
+ {
+ "namespace": "history",
+ "description": "Use the <code>browser.history</code> API to interact with the browser's record of visited pages. You can add, remove, and query for URLs in the browser's history. To override the history page with your own version, see $(topic:override)[Override Pages].",
+ "permissions": ["history"],
+ "types": [
+ {
+ "id": "TransitionType",
+ "type": "string",
+ "enum": ["link", "typed", "auto_bookmark", "auto_subframe", "manual_subframe", "generated", "auto_toplevel", "form_submit", "reload", "keyword", "keyword_generated"],
+ "description": "The $(topic:transition-types)[transition type] for this visit from its referrer."
+ },
+ {
+ "id": "HistoryItem",
+ "type": "object",
+ "description": "An object encapsulating one result of a history query.",
+ "properties": {
+ "id": {
+ "type": "string",
+ "description": "The unique identifier for the item."
+ },
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL navigated to by a user."
+ },
+ "title": {
+ "type": "string",
+ "optional": true,
+ "description": "The title of the page when it was last loaded."
+ },
+ "lastVisitTime": {
+ "type": "number",
+ "optional": true,
+ "description": "When this page was last loaded, represented in milliseconds since the epoch."
+ },
+ "visitCount": {
+ "type": "integer",
+ "optional": true,
+ "description": "The number of times the user has navigated to this page."
+ },
+ "typedCount": {
+ "type": "integer",
+ "optional": true,
+ "description": "The number of times the user has navigated to this page by typing in the address."
+ }
+ }
+ },
+ {
+ "id": "VisitItem",
+ "type": "object",
+ "description": "An object encapsulating one visit to a URL.",
+ "properties": {
+ "id": {
+ "type": "string",
+ "description": "The unique identifier for the item."
+ },
+ "visitId": {
+ "type": "string",
+ "description": "The unique identifier for this visit."
+ },
+ "visitTime": {
+ "type": "number",
+ "optional": true,
+ "description": "When this visit occurred, represented in milliseconds since the epoch."
+ },
+ "referringVisitId": {
+ "type": "string",
+ "description": "The visit ID of the referrer."
+ },
+ "transition": {
+ "$ref": "TransitionType",
+ "description": "The $(topic:transition-types)[transition type] for this visit from its referrer."
+ }
+ }
+ }
+ ],
+ "functions": [
+ {
+ "name": "search",
+ "type": "function",
+ "description": "Searches the history for the last visit time of each page matching the query.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "query",
+ "type": "object",
+ "properties": {
+ "text": {
+ "type": "string",
+ "description": "A free-text query to the history service. Leave empty to retrieve all pages."
+ },
+ "startTime": {
+ "$ref": "extensionTypes.Date",
+ "optional": true,
+ "description": "Limit results to those visited after this date. If not specified, this defaults to 24 hours in the past."
+ },
+ "endTime": {
+ "$ref": "extensionTypes.Date",
+ "optional": true,
+ "description": "Limit results to those visited before this date."
+ },
+ "maxResults": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 1,
+ "description": "The maximum number of results to retrieve. Defaults to 100."
+ }
+ }
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": {
+ "$ref": "HistoryItem"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getVisits",
+ "type": "function",
+ "description": "Retrieves information about visits to a URL.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "url": {
+ "type": "string",
+ "description": "The URL for which to retrieve visit information. It must be in the format as returned from a call to history.search."
+ }
+ }
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": [
+ {
+ "name": "results",
+ "type": "array",
+ "items": {
+ "$ref": "VisitItem"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "addUrl",
+ "type": "function",
+ "description": "Adds a URL to the history with a default visitTime of the current time and a default $(topic:transition-types)[transition type] of \"link\".",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "url": {
+ "type": "string",
+ "description": "The URL to add. Must be a valid URL that can be added to history."
+ },
+ "title": {
+ "type": "string",
+ "optional": true,
+ "description": "The title of the page."
+ },
+ "transition": {
+ "$ref": "TransitionType",
+ "optional": true,
+ "description": "The $(topic:transition-types)[transition type] for this visit from its referrer."
+ },
+ "visitTime": {
+ "$ref": "extensionTypes.Date",
+ "optional": true,
+ "description": "The date when this visit occurred."
+ }
+ }
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "deleteUrl",
+ "type": "function",
+ "description": "Removes all occurrences of the given URL from the history.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "url": {
+ "type": "string",
+ "description": "The URL to remove."
+ }
+ }
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "deleteRange",
+ "type": "function",
+ "description": "Removes all items within the specified date range from the history. Pages will not be removed from the history unless all visits fall within the range.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "range",
+ "type": "object",
+ "properties": {
+ "startTime": {
+ "$ref": "extensionTypes.Date",
+ "description": "Items added to history after this date."
+ },
+ "endTime": {
+ "$ref": "extensionTypes.Date",
+ "description": "Items added to history before this date."
+ }
+ }
+ },
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "deleteAll",
+ "type": "function",
+ "description": "Deletes all items from the history.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "callback",
+ "type": "function",
+ "parameters": []
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onVisited",
+ "type": "function",
+ "description": "Fired when a URL is visited, providing the HistoryItem data for that URL. This event fires before the page has loaded.",
+ "parameters": [
+ {
+ "name": "result",
+ "$ref": "HistoryItem"
+ }
+ ]
+ },
+ {
+ "name": "onVisitRemoved",
+ "type": "function",
+ "description": "Fired when one or more URLs are removed from the history service. When all visits have been removed the URL is purged from history.",
+ "parameters": [
+ {
+ "name": "removed",
+ "type": "object",
+ "properties": {
+ "allHistory": {
+ "type": "boolean",
+ "description": "True if all history was removed. If true, then urls will be empty."
+ },
+ "urls": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/jar.mn b/browser/components/extensions/schemas/jar.mn
new file mode 100644
index 000000000..c9fc9a808
--- /dev/null
+++ b/browser/components/extensions/schemas/jar.mn
@@ -0,0 +1,16 @@
+# 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/.
+
+browser.jar:
+ content/browser/schemas/bookmarks.json
+ content/browser/schemas/browser_action.json
+ content/browser/schemas/commands.json
+ content/browser/schemas/context_menus.json
+ content/browser/schemas/context_menus_internal.json
+ content/browser/schemas/history.json
+ content/browser/schemas/omnibox.json
+ content/browser/schemas/page_action.json
+ content/browser/schemas/sessions.json
+ content/browser/schemas/tabs.json
+ content/browser/schemas/windows.json
diff --git a/browser/components/extensions/schemas/moz.build b/browser/components/extensions/schemas/moz.build
new file mode 100644
index 000000000..aac3a838c
--- /dev/null
+++ b/browser/components/extensions/schemas/moz.build
@@ -0,0 +1,7 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+JAR_MANIFESTS += ['jar.mn']
diff --git a/browser/components/extensions/schemas/omnibox.json b/browser/components/extensions/schemas/omnibox.json
new file mode 100644
index 000000000..34428fab7
--- /dev/null
+++ b/browser/components/extensions/schemas/omnibox.json
@@ -0,0 +1,248 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "WebExtensionManifest",
+ "properties": {
+ "omnibox": {
+ "type": "object",
+ "additionalProperties": { "$ref": "UnrecognizedProperty" },
+ "properties": {
+ "keyword": {
+ "type": "string",
+ "pattern": "^[^?\\s:]([^\\s:]*[^/\\s:])?$"
+ }
+ },
+ "optional": true
+ }
+ }
+ }
+ ]
+ },
+ {
+ "namespace": "omnibox",
+ "description": "The omnibox API allows you to register a keyword with Firefox's address bar.",
+ "permissions": ["manifest:omnibox"],
+ "types": [
+ {
+ "id": "DescriptionStyleType",
+ "type": "string",
+ "description": "The style type.",
+ "enum": ["url", "match", "dim"]
+ },
+ {
+ "id": "OnInputEnteredDisposition",
+ "type": "string",
+ "enum": ["currentTab", "newForegroundTab", "newBackgroundTab"],
+ "description": "The window disposition for the omnibox query. This is the recommended context to display results. For example, if the omnibox command is to navigate to a certain URL, a disposition of 'newForegroundTab' means the navigation should take place in a new selected tab."
+ },
+ {
+ "id": "SuggestResult",
+ "type": "object",
+ "description": "A suggest result.",
+ "properties": {
+ "content": {
+ "type": "string",
+ "minLength": 1,
+ "description": "The text that is put into the URL bar, and that is sent to the extension when the user chooses this entry."
+ },
+ "description": {
+ "type": "string",
+ "minLength": 1,
+ "description": "The text that is displayed in the URL dropdown. Can contain XML-style markup for styling. The supported tags are 'url' (for a literal URL), 'match' (for highlighting text that matched what the user's query), and 'dim' (for dim helper text). The styles can be nested, eg. <dim><match>dimmed match</match></dim>. You must escape the five predefined entities to display them as text: stackoverflow.com/a/1091953/89484 "
+ },
+ "descriptionStyles": {
+ "optional": true,
+ "unsupported": true,
+ "type": "array",
+ "description": "An array of style ranges for the description, as provided by the extension.",
+ "items": {
+ "type": "object",
+ "description": "The style ranges for the description, as provided by the extension.",
+ "properties": {
+ "offset": { "type": "integer" },
+ "type": { "description": "The style type", "$ref": "DescriptionStyleType"},
+ "length": { "type": "integer", "optional": true }
+ }
+ }
+ },
+ "descriptionStylesRaw": {
+ "optional": true,
+ "unsupported": true,
+ "type": "array",
+ "description": "An array of style ranges for the description, as provided by ToValue().",
+ "items": {
+ "type": "object",
+ "description": "The style ranges for the description, as provided by ToValue().",
+ "properties": {
+ "offset": { "type": "integer" },
+ "type": { "type": "integer" }
+ }
+ }
+ }
+ }
+ },
+ {
+ "id": "DefaultSuggestResult",
+ "type": "object",
+ "description": "A suggest result.",
+ "properties": {
+ "description": {
+ "type": "string",
+ "minLength": 1,
+ "description": "The text that is displayed in the URL dropdown."
+ },
+ "descriptionStyles": {
+ "optional": true,
+ "unsupported": true,
+ "type": "array",
+ "description": "An array of style ranges for the description, as provided by the extension.",
+ "items": {
+ "type": "object",
+ "description": "The style ranges for the description, as provided by the extension.",
+ "properties": {
+ "offset": { "type": "integer" },
+ "type": { "description": "The style type", "$ref": "DescriptionStyleType"},
+ "length": { "type": "integer", "optional": true }
+ }
+ }
+ },
+ "descriptionStylesRaw": {
+ "optional": true,
+ "unsupported": true,
+ "type": "array",
+ "description": "An array of style ranges for the description, as provided by ToValue().",
+ "items": {
+ "type": "object",
+ "description": "The style ranges for the description, as provided by ToValue().",
+ "properties": {
+ "offset": { "type": "integer" },
+ "type": { "type": "integer" }
+ }
+ }
+ }
+ }
+ }
+ ],
+ "functions": [
+ {
+ "name": "setDefaultSuggestion",
+ "type": "function",
+ "description": "Sets the description and styling for the default suggestion. The default suggestion is the text that is displayed in the first suggestion row underneath the URL bar.",
+ "parameters": [
+ {
+ "name": "suggestion",
+ "$ref": "DefaultSuggestResult",
+ "description": "A partial SuggestResult object, without the 'content' parameter."
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onInputStarted",
+ "type": "function",
+ "description": "User has started a keyword input session by typing the extension's keyword. This is guaranteed to be sent exactly once per input session, and before any onInputChanged events.",
+ "parameters": []
+ },
+ {
+ "name": "onInputChanged",
+ "type": "function",
+ "description": "User has changed what is typed into the omnibox.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "text"
+ },
+ {
+ "name": "suggest",
+ "type": "function",
+ "description": "A callback passed to the onInputChanged event used for sending suggestions back to the browser.",
+ "parameters": [
+ {
+ "name": "suggestResults",
+ "type": "array",
+ "description": "Array of suggest results",
+ "items": {
+ "$ref": "SuggestResult"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "onInputEntered",
+ "type": "function",
+ "description": "User has accepted what is typed into the omnibox.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "text"
+ },
+ {
+ "name": "disposition",
+ "$ref": "OnInputEnteredDisposition"
+ }
+ ]
+ },
+ {
+ "name": "onInputCancelled",
+ "type": "function",
+ "description": "User has ended the keyword input session without accepting the input.",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "namespace": "omnibox_internal",
+ "description": "The internal namespace used by the omnibox API.",
+ "defaultContexts": ["addon_parent_only"],
+ "functions": [
+ {
+ "name": "addSuggestions",
+ "type": "function",
+ "async": "callback",
+ "description": "Internal function used by omnibox.onInputChanged for adding search suggestions",
+ "parameters": [
+ {
+ "name": "id",
+ "type": "integer",
+ "description": "The ID of the callback received by onInputChangedInternal"
+ },
+ {
+ "name": "suggestResults",
+ "type": "array",
+ "description": "Array of suggest results",
+ "items": {
+ "$ref": "omnibox.SuggestResult"
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onInputChanged",
+ "type": "function",
+ "description": "Identical to omnibox.onInputChanged except no 'suggest' callback is provided.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "text"
+ }
+ ]
+ }
+ ]
+ }
+] \ No newline at end of file
diff --git a/browser/components/extensions/schemas/page_action.json b/browser/components/extensions/schemas/page_action.json
new file mode 100644
index 000000000..f4f9ee8db
--- /dev/null
+++ b/browser/components/extensions/schemas/page_action.json
@@ -0,0 +1,234 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "WebExtensionManifest",
+ "properties": {
+ "page_action": {
+ "type": "object",
+ "additionalProperties": { "$ref": "UnrecognizedProperty" },
+ "properties": {
+ "default_title": {
+ "type": "string",
+ "optional": true,
+ "preprocess": "localize"
+ },
+ "default_icon": {
+ "$ref": "IconPath",
+ "optional": true
+ },
+ "default_popup": {
+ "type": "string",
+ "format": "relativeUrl",
+ "optional": true,
+ "preprocess": "localize"
+ },
+ "browser_style": {
+ "type": "boolean",
+ "optional": true
+ }
+ },
+ "optional": true
+ }
+ }
+ }
+ ]
+ },
+ {
+ "namespace": "pageAction",
+ "description": "Use the <code>browser.pageAction</code> API to put icons inside the address bar. Page actions represent actions that can be taken on the current page, but that aren't applicable to all pages.",
+ "permissions": ["manifest:page_action"],
+ "types": [
+ {
+ "id": "ImageDataType",
+ "type": "object",
+ "isInstanceOf": "ImageData",
+ "additionalProperties": { "type": "any" },
+ "postprocess": "convertImageDataToURL",
+ "description": "Pixel data for an image. Must be an ImageData object (for example, from a <code>canvas</code> element)."
+ }
+ ],
+ "functions": [
+ {
+ "name": "show",
+ "type": "function",
+ "async": "callback",
+ "description": "Shows the page action. The page action is shown whenever the tab is selected.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "hide",
+ "type": "function",
+ "async": "callback",
+ "description": "Hides the page action.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "setTitle",
+ "type": "function",
+ "description": "Sets the title of the page action. This is displayed in a tooltip over the page action.",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {"type": "integer", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
+ "title": {"type": "string", "description": "The tooltip string."}
+ }
+ }
+ ]
+ },
+ {
+ "name": "getTitle",
+ "type": "function",
+ "description": "Gets the title of the page action.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "description": "Specify the tab to get the title from."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "setIcon",
+ "type": "function",
+ "description": "Sets the icon for the page action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the <b>path</b> or the <b>imageData</b> property must be specified.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {"type": "integer", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
+ "imageData": {
+ "choices": [
+ { "$ref": "ImageDataType" },
+ {
+ "type": "object",
+ "additionalProperties": {"$ref": "ImageDataType"}
+ }
+ ],
+ "optional": true,
+ "description": "Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals <code>scale</code>, then image with size <code>scale</code> * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'19': foo}'"
+ },
+ "path": {
+ "choices": [
+ { "type": "string" },
+ {
+ "type": "object",
+ "additionalProperties": {"type": "string"}
+ }
+ ],
+ "optional": true,
+ "description": "Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals <code>scale</code>, then image with size <code>scale</code> * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.imageData = {'19': foo}'"
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "setPopup",
+ "type": "function",
+ "description": "Sets the html document to be opened as a popup when the user clicks on the page action's icon.",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {"type": "integer", "minimum": 0, "description": "The id of the tab for which you want to modify the page action."},
+ "popup": {
+ "type": "string",
+ "description": "The html file to show in a popup. If set to the empty string (''), no popup is shown."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "getPopup",
+ "type": "function",
+ "description": "Gets the html document set as the popup for this page action.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "details",
+ "type": "object",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "description": "Specify the tab to get the popup from."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onClicked",
+ "type": "function",
+ "description": "Fired when a page action icon is clicked. This event will not fire if the page action has a popup.",
+ "parameters": [
+ {
+ "name": "tab",
+ "$ref": "tabs.Tab"
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/sessions.json b/browser/components/extensions/schemas/sessions.json
new file mode 100644
index 000000000..690bb8ebc
--- /dev/null
+++ b/browser/components/extensions/schemas/sessions.json
@@ -0,0 +1,146 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "Permission",
+ "choices": [{
+ "type": "string",
+ "enum": [
+ "sessions"
+ ]
+ }]
+ }
+ ]
+ },
+ {
+ "namespace": "sessions",
+ "description": "Use the <code>chrome.sessions</code> API to query and restore tabs and windows from a browsing session.",
+ "permissions": ["sessions"],
+ "types": [
+ {
+ "id": "Filter",
+ "type": "object",
+ "properties": {
+ "maxResults": {
+ "type": "integer",
+ "minimum": 0,
+ "maximum": 25,
+ "optional": true,
+ "description": "The maximum number of entries to be fetched in the requested list. Omit this parameter to fetch the maximum number of entries ($(ref:sessions.MAX_SESSION_RESULTS))."
+ }
+ }
+ },
+ {
+ "id": "Session",
+ "type": "object",
+ "properties": {
+ "lastModified": {"type": "integer", "description": "The time when the window or tab was closed or modified, represented in milliseconds since the epoch."},
+ "tab": {"$ref": "tabs.Tab", "optional": true, "description": "The $(ref:tabs.Tab), if this entry describes a tab. Either this or $(ref:sessions.Session.window) will be set."},
+ "window": {"$ref": "windows.Window", "optional": true, "description": "The $(ref:windows.Window), if this entry describes a window. Either this or $(ref:sessions.Session.tab) will be set."}
+ }
+ },
+ {
+ "id": "Device",
+ "type": "object",
+ "properties": {
+ "info": {"type": "string"},
+ "deviceName": {"type": "string", "description": "The name of the foreign device."},
+ "sessions": {"type": "array", "items": {"$ref": "Session"}, "description": "A list of open window sessions for the foreign device, sorted from most recently to least recently modified session."}
+ }
+ }
+ ],
+ "functions": [
+ {
+ "name": "getRecentlyClosed",
+ "type": "function",
+ "description": "Gets the list of recently closed tabs and/or windows.",
+ "async": "callback",
+ "parameters": [
+ {
+ "$ref": "Filter",
+ "name": "filter",
+ "optional": true,
+ "default": {}
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "sessions", "type": "array", "items": { "$ref": "Session" }, "description": "The list of closed entries in reverse order that they were closed (the most recently closed tab or window will be at index <code>0</code>). The entries may contain either tabs or windows."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getDevices",
+ "unsupported": true,
+ "type": "function",
+ "description": "Retrieves all devices with synced sessions.",
+ "async": "callback",
+ "parameters": [
+ {
+ "$ref": "Filter",
+ "name": "filter",
+ "optional": true
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "devices", "type": "array", "items": { "$ref": "Device" }, "description": "The list of $(ref:sessions.Device) objects for each synced session, sorted in order from device with most recently modified session to device with least recently modified session. $(ref:tabs.Tab) objects are sorted by recency in the $(ref:windows.Window) of the $(ref:sessions.Session) objects."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "restore",
+ "type": "function",
+ "description": "Reopens a $(ref:windows.Window) or $(ref:tabs.Tab), with an optional callback to run when the entry has been restored.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "sessionId",
+ "optional": true,
+ "description": "The $(ref:windows.Window.sessionId), or $(ref:tabs.Tab.sessionId) to restore. If this parameter is not specified, the most recently closed session is restored."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "$ref": "Session",
+ "name": "restoredSession",
+ "description": "A $(ref:sessions.Session) containing the restored $(ref:windows.Window) or $(ref:tabs.Tab) object."
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onChanged",
+ "unsupported": true,
+ "description": "Fired when recently closed tabs and/or windows are changed. This event does not monitor synced sessions changes.",
+ "type": "function"
+ }
+ ],
+ "properties": {
+ "MAX_SESSION_RESULTS": {
+ "value": 25,
+ "description": "The maximum number of $(ref:sessions.Session) that will be included in a requested list."
+ }
+ }
+ }
+]
diff --git a/browser/components/extensions/schemas/tabs.json b/browser/components/extensions/schemas/tabs.json
new file mode 100644
index 000000000..23ce33a4b
--- /dev/null
+++ b/browser/components/extensions/schemas/tabs.json
@@ -0,0 +1,1295 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "manifest",
+ "types": [
+ {
+ "$extend": "Permission",
+ "choices": [{
+ "type": "string",
+ "enum": [
+ "activeTab",
+ "tabs"
+ ]
+ }]
+ }
+ ]
+ },
+ {
+ "namespace": "tabs",
+ "description": "Use the <code>browser.tabs</code> API to interact with the browser's tab system. You can use this API to create, modify, and rearrange tabs in the browser.",
+ "types": [
+ { "id": "MutedInfoReason",
+ "type": "string",
+ "description": "An event that caused a muted state change.",
+ "enum": [
+ {"name": "user", "description": "A user input action has set/overridden the muted state."},
+ {"name": "capture", "description": "Tab capture started, forcing a muted state change."},
+ {"name": "extension", "description": "An extension, identified by the extensionId field, set the muted state."}
+ ]
+ },
+ {
+ "id": "MutedInfo",
+ "type": "object",
+ "description": "Tab muted state and the reason for the last state change.",
+ "properties": {
+ "muted": {
+ "type": "boolean",
+ "description": "Whether the tab is prevented from playing sound (but hasn't necessarily recently produced sound). Equivalent to whether the muted audio indicator is showing."
+ },
+ "reason": {
+ "$ref": "MutedInfoReason",
+ "optional": true,
+ "description": "The reason the tab was muted or unmuted. Not set if the tab's mute state has never been changed."
+ },
+ "extensionId": {
+ "type": "string",
+ "optional": true,
+ "description": "The ID of the extension that changed the muted state. Not set if an extension was not the reason the muted state last changed."
+ }
+ }
+ },
+ {
+ "id": "Tab",
+ "type": "object",
+ "properties": {
+ "id": {"type": "integer", "minimum": -1, "optional": true, "description": "The ID of the tab. Tab IDs are unique within a browser session. Under some circumstances a Tab may not be assigned an ID, for example when querying foreign tabs using the $(ref:sessions) API, in which case a session ID may be present. Tab ID can also be set to $(ref:tabs.TAB_ID_NONE) for apps and devtools windows."},
+ "index": {"type": "integer", "minimum": -1, "description": "The zero-based index of the tab within its window."},
+ "windowId": {"type": "integer", "minimum": 0, "description": "The ID of the window the tab is contained within."},
+ "openerTabId": {"unsupported": true, "type": "integer", "minimum": 0, "optional": true, "description": "The ID of the tab that opened this tab, if any. This property is only present if the opener tab still exists."},
+ "selected": {"type": "boolean", "description": "Whether the tab is selected.", "deprecated": "Please use $(ref:tabs.Tab.highlighted).", "unsupported": true},
+ "highlighted": {"type": "boolean", "description": "Whether the tab is highlighted."},
+ "active": {"type": "boolean", "description": "Whether the tab is active in its window. (Does not necessarily mean the window is focused.)"},
+ "pinned": {"type": "boolean", "description": "Whether the tab is pinned."},
+ "audible": {"type": "boolean", "optional": true, "description": "Whether the tab has produced sound over the past couple of seconds (but it might not be heard if also muted). Equivalent to whether the speaker audio indicator is showing."},
+ "mutedInfo": {"$ref": "MutedInfo", "optional": true, "description": "Current tab muted state and the reason for the last state change."},
+ "url": {"type": "string", "optional": true, "permissions": ["tabs"], "description": "The URL the tab is displaying. This property is only present if the extension's manifest includes the <code>\"tabs\"</code> permission."},
+ "title": {"type": "string", "optional": true, "permissions": ["tabs"], "description": "The title of the tab. This property is only present if the extension's manifest includes the <code>\"tabs\"</code> permission."},
+ "favIconUrl": {"type": "string", "optional": true, "permissions": ["tabs"], "description": "The URL of the tab's favicon. This property is only present if the extension's manifest includes the <code>\"tabs\"</code> permission. It may also be an empty string if the tab is loading."},
+ "status": {"type": "string", "optional": true, "description": "Either <em>loading</em> or <em>complete</em>."},
+ "incognito": {"type": "boolean", "description": "Whether the tab is in an incognito window."},
+ "width": {"type": "integer", "optional": true, "description": "The width of the tab in pixels."},
+ "height": {"type": "integer", "optional": true, "description": "The height of the tab in pixels."},
+ "sessionId": {"unsupported": true, "type": "string", "optional": true, "description": "The session ID used to uniquely identify a Tab obtained from the $(ref:sessions) API."},
+ "cookieStoreId": {"type": "string", "description": "The CookieStoreId used for the tab."}
+ }
+ },
+ {
+ "id": "ZoomSettingsMode",
+ "type": "string",
+ "description": "Defines how zoom changes are handled, i.e. which entity is responsible for the actual scaling of the page; defaults to <code>automatic</code>.",
+ "enum": [
+ {
+ "name": "automatic",
+ "description": "Zoom changes are handled automatically by the browser."
+ },
+ {
+ "name": "manual",
+ "description": "Overrides the automatic handling of zoom changes. The <code>onZoomChange</code> event will still be dispatched, and it is the responsibility of the extension to listen for this event and manually scale the page. This mode does not support <code>per-origin</code> zooming, and will thus ignore the <code>scope</code> zoom setting and assume <code>per-tab</code>."
+ },
+ {
+ "name": "disabled",
+ "description": "Disables all zooming in the tab. The tab will revert to the default zoom level, and all attempted zoom changes will be ignored."
+ }
+ ]
+ },
+ {
+ "id": "ZoomSettingsScope",
+ "type": "string",
+ "description": "Defines whether zoom changes will persist for the page's origin, or only take effect in this tab; defaults to <code>per-origin</code> when in <code>automatic</code> mode, and <code>per-tab</code> otherwise.",
+ "enum": [
+ {
+ "name": "per-origin",
+ "description": "Zoom changes will persist in the zoomed page's origin, i.e. all other tabs navigated to that same origin will be zoomed as well. Moreover, <code>per-origin</code> zoom changes are saved with the origin, meaning that when navigating to other pages in the same origin, they will all be zoomed to the same zoom factor. The <code>per-origin</code> scope is only available in the <code>automatic</code> mode."
+ },
+ {
+ "name": "per-tab",
+ "description": "Zoom changes only take effect in this tab, and zoom changes in other tabs will not affect the zooming of this tab. Also, <code>per-tab</code> zoom changes are reset on navigation; navigating a tab will always load pages with their <code>per-origin</code> zoom factors."
+ }
+ ]
+ },
+ {
+ "id": "ZoomSettings",
+ "type": "object",
+ "description": "Defines how zoom changes in a tab are handled and at what scope.",
+ "properties": {
+ "mode": {
+ "$ref": "ZoomSettingsMode",
+ "description": "Defines how zoom changes are handled, i.e. which entity is responsible for the actual scaling of the page; defaults to <code>automatic</code>.",
+ "optional": true
+ },
+ "scope": {
+ "$ref": "ZoomSettingsScope",
+ "description": "Defines whether zoom changes will persist for the page's origin, or only take effect in this tab; defaults to <code>per-origin</code> when in <code>automatic</code> mode, and <code>per-tab</code> otherwise.",
+ "optional": true
+ },
+ "defaultZoomFactor": {
+ "type": "number",
+ "optional": true,
+ "description": "Used to return the default zoom level for the current tab in calls to tabs.getZoomSettings."
+ }
+ }
+ },
+ {
+ "id": "TabStatus",
+ "type": "string",
+ "enum": ["loading", "complete"],
+ "description": "Whether the tabs have completed loading."
+ },
+ {
+ "id": "WindowType",
+ "type": "string",
+ "enum": ["normal", "popup", "panel", "app", "devtools"],
+ "description": "The type of window."
+ }
+ ],
+ "properties": {
+ "TAB_ID_NONE": {
+ "value": -1,
+ "description": "An ID which represents the absence of a browser tab."
+ }
+ },
+ "functions": [
+ {
+ "name": "get",
+ "type": "function",
+ "description": "Retrieves details about the specified tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {"name": "tab", "$ref": "Tab"}
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getCurrent",
+ "type": "function",
+ "description": "Gets the tab that this script call is being made from. May be undefined if called from a non-tab context (for example: a background page or popup view).",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "tab",
+ "$ref": "Tab",
+ "optional": true
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "connect",
+ "type": "function",
+ "description": "Connects to the content script(s) in the specified tab. The $(ref:runtime.onConnect) event is fired in each content script running in the specified tab for the current extension. For more details, see $(topic:messaging)[Content Script Messaging].",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0
+ },
+ {
+ "type": "object",
+ "name": "connectInfo",
+ "properties": {
+ "name": { "type": "string", "optional": true, "description": "Will be passed into onConnect for content scripts that are listening for the connection event." },
+ "frameId": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 0,
+ "description": "Open a port to a specific $(topic:frame_ids)[frame] identified by <code>frameId</code> instead of all frames in the tab."
+ }
+ },
+ "optional": true
+ }
+ ],
+ "returns": {
+ "$ref": "runtime.Port",
+ "description": "A port that can be used to communicate with the content scripts running in the specified tab. The port's $(ref:runtime.Port) event is fired if the tab closes or does not exist. "
+ }
+ },
+ {
+ "name": "sendRequest",
+ "deprecated": "Please use $(ref:runtime.sendMessage).",
+ "unsupported": true,
+ "type": "function",
+ "description": "Sends a single request to the content script(s) in the specified tab, with an optional callback to run when a response is sent back. The $(ref:extension.onRequest) event is fired in each content script running in the specified tab for the current extension.",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0
+ },
+ {
+ "type": "any",
+ "name": "request"
+ },
+ {
+ "type": "function",
+ "name": "responseCallback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "response",
+ "type": "any",
+ "description": "The JSON response object sent by the handler of the request. If an error occurs while connecting to the specified tab, the callback will be called with no arguments and $(ref:runtime.lastError) will be set to the error message."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "sendMessage",
+ "type": "function",
+ "description": "Sends a single message to the content script(s) in the specified tab, with an optional callback to run when a response is sent back. The $(ref:runtime.onMessage) event is fired in each content script running in the specified tab for the current extension.",
+ "async": "responseCallback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0
+ },
+ {
+ "type": "any",
+ "name": "message"
+ },
+ {
+ "type": "object",
+ "name": "options",
+ "properties": {
+ "frameId": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 0,
+ "description": "Send a message to a specific $(topic:frame_ids)[frame] identified by <code>frameId</code> instead of all frames in the tab."
+ }
+ },
+ "optional": true
+ },
+ {
+ "type": "function",
+ "name": "responseCallback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "response",
+ "type": "any",
+ "description": "The JSON response object sent by the handler of the message. If an error occurs while connecting to the specified tab, the callback will be called with no arguments and $(ref:runtime.lastError) will be set to the error message."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getSelected",
+ "deprecated": "Please use $(ref:tabs.query) <code>{active: true}</code>.",
+ "unsupported": true,
+ "type": "function",
+ "description": "Gets the tab that is selected in the specified window.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -2,
+ "optional": true,
+ "description": "Defaults to the $(topic:current-window)[current window]."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {"name": "tab", "$ref": "Tab"}
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getAllInWindow",
+ "deprecated": "Please use $(ref:tabs.query) <code>{windowId: windowId}</code>.",
+ "unsupported": true,
+ "type": "function",
+ "description": "Gets details about all tabs in the specified window.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -2,
+ "optional": true,
+ "description": "Defaults to the $(topic:current-window)[current window]."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {"name": "tabs", "type": "array", "items": { "$ref": "Tab" } }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "create",
+ "type": "function",
+ "description": "Creates a new tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "createProperties",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": -2,
+ "optional": true,
+ "description": "The window to create the new tab in. Defaults to the $(topic:current-window)[current window]."
+ },
+ "index": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The position the tab should take in the window. The provided value will be clamped to between zero and the number of tabs in the window."
+ },
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "The URL to navigate the tab to initially. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page."
+ },
+ "active": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should become the active tab in the window. Does not affect whether the window is focused (see $(ref:windows.update)). Defaults to <var>true</var>."
+ },
+ "selected": {
+ "deprecated": "Please use <em>active</em>.",
+ "unsupported": true,
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should become the selected tab in the window. Defaults to <var>true</var>"
+ },
+ "pinned": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should be pinned. Defaults to <var>false</var>"
+ },
+ "openerTabId": {
+ "unsupported": true,
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab that opened this tab. If specified, the opener tab must be in the same window as the newly created tab."
+ },
+ "cookieStoreId": {
+ "type": "string",
+ "optional": true,
+ "description": "The CookieStoreId for the tab that opened this tab."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "tab",
+ "$ref": "Tab",
+ "description": "Details about the created tab. Will contain the ID of the new tab."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "duplicate",
+ "type": "function",
+ "description": "Duplicates a tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "description": "The ID of the tab which is to be duplicated."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "tab",
+ "optional": true,
+ "description": "Details about the duplicated tab. The $(ref:tabs.Tab) object doesn't contain <code>url</code>, <code>title</code> and <code>favIconUrl</code> if the <code>\"tabs\"</code> permission has not been requested.",
+ "$ref": "Tab"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "query",
+ "type": "function",
+ "description": "Gets all tabs that have the specified properties, or all tabs if no properties are specified.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "queryInfo",
+ "properties": {
+ "active": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are active in their windows."
+ },
+ "pinned": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are pinned."
+ },
+ "audible": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are audible."
+ },
+ "muted": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are muted."
+ },
+ "highlighted": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are highlighted."
+ },
+ "currentWindow": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are in the $(topic:current-window)[current window]."
+ },
+ "lastFocusedWindow": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tabs are in the last focused window."
+ },
+ "status": {
+ "$ref": "TabStatus",
+ "optional": true,
+ "description": "Whether the tabs have completed loading."
+ },
+ "title": {
+ "type": "string",
+ "optional": true,
+ "description": "Match page titles against a pattern."
+ },
+ "url": {
+ "choices": [
+ {"type": "string"},
+ {"type": "array", "items": {"type": "string"}}
+ ],
+ "optional": true,
+ "description": "Match tabs against one or more $(topic:match_patterns)[URL patterns]. Note that fragment identifiers are not matched."
+ },
+ "windowId": {
+ "type": "integer",
+ "optional": true,
+ "minimum": -2,
+ "description": "The ID of the parent window, or $(ref:windows.WINDOW_ID_CURRENT) for the $(topic:current-window)[current window]."
+ },
+ "windowType": {
+ "$ref": "WindowType",
+ "optional": true,
+ "description": "The type of window the tabs are in."
+ },
+ "index": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 0,
+ "description": "The position of the tabs within their windows."
+ },
+ "cookieStoreId": {
+ "type": "string",
+ "optional": true,
+ "description": "The CookieStoreId used for the tab."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "result",
+ "type": "array",
+ "items": {
+ "$ref": "Tab"
+ }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "highlight",
+ "type": "function",
+ "description": "Highlights the given tabs.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "highlightInfo",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "optional": true,
+ "description": "The window that contains the tabs.",
+ "minimum": -2
+ },
+ "tabs": {
+ "description": "One or more tab indices to highlight.",
+ "choices": [
+ {"type": "array", "items": {"type": "integer", "minimum": 0}},
+ {"type": "integer"}
+ ]
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "windows.Window",
+ "description": "Contains details about the window whose tabs were highlighted."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "update",
+ "type": "function",
+ "description": "Modifies the properties of a tab. Properties that are not specified in <var>updateProperties</var> are not modified.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "Defaults to the selected tab of the $(topic:current-window)[current window]."
+ },
+ {
+ "type": "object",
+ "name": "updateProperties",
+ "properties": {
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "A URL to navigate the tab to."
+ },
+ "active": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should be active. Does not affect whether the window is focused (see $(ref:windows.update))."
+ },
+ "highlighted": {
+ "unsupported": true,
+ "type": "boolean",
+ "optional": true,
+ "description": "Adds or removes the tab from the current selection."
+ },
+ "selected": {
+ "unsupported": true,
+ "deprecated": "Please use <em>highlighted</em>.",
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should be selected."
+ },
+ "pinned": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should be pinned."
+ },
+ "muted": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the tab should be muted."
+ },
+ "openerTabId": {
+ "unsupported": true,
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab that opened this tab. If specified, the opener tab must be in the same window as this tab."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "tab",
+ "$ref": "Tab",
+ "optional": true,
+ "description": "Details about the updated tab. The $(ref:tabs.Tab) object doesn't contain <code>url</code>, <code>title</code> and <code>favIconUrl</code> if the <code>\"tabs\"</code> permission has not been requested."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "move",
+ "type": "function",
+ "description": "Moves one or more tabs to a new position within its window, or to a new window. Note that tabs can only be moved to and from normal (window.type === \"normal\") windows.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "tabIds",
+ "description": "The tab or list of tabs to move.",
+ "choices": [
+ {"type": "integer", "minimum": 0},
+ {"type": "array", "items": {"type": "integer", "minimum": 0}}
+ ]
+ },
+ {
+ "type": "object",
+ "name": "moveProperties",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": -2,
+ "optional": true,
+ "description": "Defaults to the window the tab is currently in."
+ },
+ "index": {
+ "type": "integer",
+ "minimum": -1,
+ "description": "The position to move the window to. -1 will place the tab at the end of the window."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "tabs",
+ "description": "Details about the moved tabs.",
+ "choices": [
+ {"$ref": "Tab"},
+ {"type": "array", "items": {"$ref": "Tab"}}
+ ]
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "reload",
+ "type": "function",
+ "description": "Reload a tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab to reload; defaults to the selected tab of the current window."
+ },
+ {
+ "type": "object",
+ "name": "reloadProperties",
+ "optional": true,
+ "properties": {
+ "bypassCache": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether using any local cache. Default is false."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "remove",
+ "type": "function",
+ "description": "Closes one or more tabs.",
+ "async": "callback",
+ "parameters": [
+ {
+ "name": "tabIds",
+ "description": "The tab or list of tabs to close.",
+ "choices": [
+ {"type": "integer", "minimum": 0},
+ {"type": "array", "items": {"type": "integer", "minimum": 0}}
+ ]
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "detectLanguage",
+ "type": "function",
+ "description": "Detects the primary language of the content in a tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "Defaults to the active tab of the $(topic:current-window)[current window]."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "language",
+ "description": "An ISO language code such as <code>en</code> or <code>fr</code>. For a complete list of languages supported by this method, see <a href='http://src.chromium.org/viewvc/chrome/trunk/src/third_party/cld/languages/internal/languages.cc'>kLanguageInfoTable</a>. The 2nd to 4th columns will be checked and the first non-NULL value will be returned except for Simplified Chinese for which zh-CN will be returned. For an unknown language, <code>und</code> will be returned."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "captureVisibleTab",
+ "type": "function",
+ "description": "Captures the visible area of the currently active tab in the specified window. You must have $(topic:declare_permissions)[&lt;all_urls&gt;] permission to use this method.",
+ "permissions": ["<all_urls>"],
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -2,
+ "optional": true,
+ "description": "The target window. Defaults to the $(topic:current-window)[current window]."
+ },
+ {
+ "$ref": "extensionTypes.ImageDetails",
+ "name": "options",
+ "optional": true
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "dataUrl",
+ "description": "A data URL which encodes an image of the visible area of the captured tab. May be assigned to the 'src' property of an HTML Image element for display."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "executeScript",
+ "type": "function",
+ "description": "Injects JavaScript code into a page. For details, see the $(topic:content_scripts)[programmatic injection] section of the content scripts doc.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab in which to run the script; defaults to the active tab of the current window."
+ },
+ {
+ "$ref": "extensionTypes.InjectDetails",
+ "name": "details",
+ "description": "Details of the script to run."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called after all the JavaScript has been executed.",
+ "parameters": [
+ {
+ "name": "result",
+ "optional": true,
+ "type": "array",
+ "items": {"type": "any"},
+ "description": "The result of the script in every injected frame."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "insertCSS",
+ "type": "function",
+ "description": "Injects CSS into a page. For details, see the $(topic:content_scripts)[programmatic injection] section of the content scripts doc.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab in which to insert the CSS; defaults to the active tab of the current window."
+ },
+ {
+ "$ref": "extensionTypes.InjectDetails",
+ "name": "details",
+ "description": "Details of the CSS text to insert."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called when all the CSS has been inserted.",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "removeCSS",
+ "type": "function",
+ "description": "Removes injected CSS from a page. For details, see the $(topic:content_scripts)[programmatic injection] section of the content scripts doc.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab from which to remove the injected CSS; defaults to the active tab of the current window."
+ },
+ {
+ "$ref": "extensionTypes.InjectDetails",
+ "name": "details",
+ "description": "Details of the CSS text to remove."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called when all the CSS has been removed.",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "setZoom",
+ "type": "function",
+ "description": "Zooms a specified tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab to zoom; defaults to the active tab of the current window."
+ },
+ {
+ "type": "number",
+ "name": "zoomFactor",
+ "description": "The new zoom factor. Use a value of 0 here to set the tab to its current default zoom factor. Values greater than zero specify a (possibly non-default) zoom factor for the tab."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called after the zoom factor has been changed.",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getZoom",
+ "type": "function",
+ "description": "Gets the current zoom factor of a specified tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "optional": true,
+ "description": "The ID of the tab to get the current zoom factor from; defaults to the active tab of the current window."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "description": "Called with the tab's current zoom factor after it has been fetched.",
+ "parameters": [
+ {
+ "type": "number",
+ "name": "zoomFactor",
+ "description": "The tab's current zoom factor."
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "setZoomSettings",
+ "type": "function",
+ "description": "Sets the zoom settings for a specified tab, which define how zoom changes are handled. These settings are reset to defaults upon navigating the tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "optional": true,
+ "minimum": 0,
+ "description": "The ID of the tab to change the zoom settings for; defaults to the active tab of the current window."
+ },
+ {
+ "$ref": "ZoomSettings",
+ "name": "zoomSettings",
+ "description": "Defines how zoom changes are handled and at what scope."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "description": "Called after the zoom settings have been changed.",
+ "parameters": []
+ }
+ ]
+ },
+ {
+ "name": "getZoomSettings",
+ "type": "function",
+ "description": "Gets the current zoom settings of a specified tab.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "optional": true,
+ "minimum": 0,
+ "description": "The ID of the tab to get the current zoom settings from; defaults to the active tab of the current window."
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "description": "Called with the tab's current zoom settings.",
+ "parameters": [
+ {
+ "$ref": "ZoomSettings",
+ "name": "zoomSettings",
+ "description": "The tab's current zoom settings."
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onCreated",
+ "type": "function",
+ "description": "Fired when a tab is created. Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set.",
+ "parameters": [
+ {
+ "$ref": "Tab",
+ "name": "tab",
+ "description": "Details of the tab that was created."
+ }
+ ]
+ },
+ {
+ "name": "onUpdated",
+ "type": "function",
+ "description": "Fired when a tab is updated.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0},
+ {
+ "type": "object",
+ "name": "changeInfo",
+ "description": "Lists the changes to the state of the tab that was updated.",
+ "properties": {
+ "status": {
+ "type": "string",
+ "optional": true,
+ "description": "The status of the tab. Can be either <em>loading</em> or <em>complete</em>."
+ },
+ "url": {
+ "type": "string",
+ "optional": true,
+ "description": "The tab's URL if it has changed."
+ },
+ "pinned": {
+ "type": "boolean",
+ "optional": true,
+ "description": "The tab's new pinned state."
+ },
+ "audible": {
+ "type": "boolean",
+ "optional": true,
+ "description": "The tab's new audible state."
+ },
+ "mutedInfo": {
+ "$ref": "MutedInfo",
+ "optional": true,
+ "description": "The tab's new muted state and the reason for the change."
+ },
+ "favIconUrl": {
+ "type": "string",
+ "optional": true,
+ "description": "The tab's new favicon URL."
+ }
+ }
+ },
+ {
+ "$ref": "Tab",
+ "name": "tab",
+ "description": "Gives the state of the tab that was updated."
+ }
+ ]
+ },
+ {
+ "name": "onMoved",
+ "type": "function",
+ "description": "Fired when a tab is moved within a window. Only one move event is fired, representing the tab the user directly moved. Move events are not fired for the other tabs that must move in response. This event is not fired when a tab is moved between windows. For that, see $(ref:tabs.onDetached).",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0},
+ {
+ "type": "object",
+ "name": "moveInfo",
+ "properties": {
+ "windowId": {"type": "integer", "minimum": 0},
+ "fromIndex": {"type": "integer", "minimum": 0},
+ "toIndex": {"type": "integer", "minimum": 0}
+ }
+ }
+ ]
+ },
+ {
+ "name": "onSelectionChanged",
+ "deprecated": "Please use $(ref:tabs.onActivated).",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fires when the selected tab in a window changes.",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "description": "The ID of the tab that has become active."
+ },
+ {
+ "type": "object",
+ "name": "selectInfo",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The ID of the window the selected tab changed inside of."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onActiveChanged",
+ "deprecated": "Please use $(ref:tabs.onActivated).",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fires when the selected tab in a window changes. Note that the tab's URL may not be set at the time this event fired, but you can listen to $(ref:tabs.onUpdated) events to be notified when a URL is set.",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "tabId",
+ "minimum": 0,
+ "description": "The ID of the tab that has become active."
+ },
+ {
+ "type": "object",
+ "name": "selectInfo",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The ID of the window the selected tab changed inside of."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onActivated",
+ "type": "function",
+ "description": "Fires when the active tab in a window changes. Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set.",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "activeInfo",
+ "properties": {
+ "tabId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The ID of the tab that has become active."
+ },
+ "windowId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The ID of the window the active tab changed inside of."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onHighlightChanged",
+ "deprecated": "Please use $(ref:tabs.onHighlighted).",
+ "unsupported": true,
+ "type": "function",
+ "description": "Fired when the highlighted or selected tabs in a window changes.",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "selectInfo",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The window whose tabs changed."
+ },
+ "tabIds": {
+ "type": "array",
+ "items": {"type": "integer", "minimum": 0},
+ "description": "All highlighted tabs in the window."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onHighlighted",
+ "type": "function",
+ "description": "Fired when the highlighted or selected tabs in a window changes.",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "highlightInfo",
+ "properties": {
+ "windowId": {
+ "type": "integer",
+ "minimum": 0,
+ "description": "The window whose tabs changed."
+ },
+ "tabIds": {
+ "type": "array",
+ "items": {"type": "integer", "minimum": 0},
+ "description": "All highlighted tabs in the window."
+ }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onDetached",
+ "type": "function",
+ "description": "Fired when a tab is detached from a window, for example because it is being moved between windows.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0},
+ {
+ "type": "object",
+ "name": "detachInfo",
+ "properties": {
+ "oldWindowId": {"type": "integer", "minimum": 0},
+ "oldPosition": {"type": "integer", "minimum": 0}
+ }
+ }
+ ]
+ },
+ {
+ "name": "onAttached",
+ "type": "function",
+ "description": "Fired when a tab is attached to a window, for example because it was moved between windows.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0},
+ {
+ "type": "object",
+ "name": "attachInfo",
+ "properties": {
+ "newWindowId": {"type": "integer", "minimum": 0},
+ "newPosition": {"type": "integer", "minimum": 0}
+ }
+ }
+ ]
+ },
+ {
+ "name": "onRemoved",
+ "type": "function",
+ "description": "Fired when a tab is closed.",
+ "parameters": [
+ {"type": "integer", "name": "tabId", "minimum": 0},
+ {
+ "type": "object",
+ "name": "removeInfo",
+ "properties": {
+ "windowId": {"type": "integer", "minimum": 0, "description": "The window whose tab is closed." },
+ "isWindowClosing": {"type": "boolean", "description": "True when the tab is being closed because its window is being closed." }
+ }
+ }
+ ]
+ },
+ {
+ "name": "onReplaced",
+ "type": "function",
+ "description": "Fired when a tab is replaced with another tab due to prerendering or instant.",
+ "parameters": [
+ {"type": "integer", "name": "addedTabId", "minimum": 0},
+ {"type": "integer", "name": "removedTabId", "minimum": 0}
+ ]
+ },
+ {
+ "name": "onZoomChange",
+ "type": "function",
+ "description": "Fired when a tab is zoomed.",
+ "parameters": [{
+ "type": "object",
+ "name": "ZoomChangeInfo",
+ "properties": {
+ "tabId": {"type": "integer", "minimum": 0},
+ "oldZoomFactor": {"type": "number"},
+ "newZoomFactor": {"type": "number"},
+ "zoomSettings": {"$ref": "ZoomSettings"}
+ }
+ }]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/schemas/windows.json b/browser/components/extensions/schemas/windows.json
new file mode 100644
index 000000000..8453358b5
--- /dev/null
+++ b/browser/components/extensions/schemas/windows.json
@@ -0,0 +1,508 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+[
+ {
+ "namespace": "windows",
+ "description": "Use the <code>browser.windows</code> API to interact with browser windows. You can use this API to create, modify, and rearrange windows in the browser.",
+ "types": [
+ {
+ "id": "WindowType",
+ "type": "string",
+ "description": "The type of browser window this is. Under some circumstances a Window may not be assigned type property, for example when querying closed windows from the $(ref:sessions) API.",
+ "enum": ["normal", "popup", "panel", "app", "devtools"]
+ },
+ {
+ "id": "WindowState",
+ "type": "string",
+ "description": "The state of this browser window. Under some circumstances a Window may not be assigned state property, for example when querying closed windows from the $(ref:sessions) API.",
+ "enum": ["normal", "minimized", "maximized", "fullscreen", "docked"]
+ },
+ {
+ "id": "Window",
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "integer",
+ "optional": true,
+ "minimum": 0,
+ "description": "The ID of the window. Window IDs are unique within a browser session. Under some circumstances a Window may not be assigned an ID, for example when querying windows using the $(ref:sessions) API, in which case a session ID may be present."
+ },
+ "focused": {
+ "type": "boolean",
+ "description": "Whether the window is currently the focused window."
+ },
+ "top": {
+ "type": "integer",
+ "optional": true,
+ "description": "The offset of the window from the top edge of the screen in pixels. Under some circumstances a Window may not be assigned top property, for example when querying closed windows from the $(ref:sessions) API."
+ },
+ "left": {
+ "type": "integer",
+ "optional": true,
+ "description": "The offset of the window from the left edge of the screen in pixels. Under some circumstances a Window may not be assigned left property, for example when querying closed windows from the $(ref:sessions) API."
+ },
+ "width": {
+ "type": "integer",
+ "optional": true,
+ "description": "The width of the window, including the frame, in pixels. Under some circumstances a Window may not be assigned width property, for example when querying closed windows from the $(ref:sessions) API."
+ },
+ "height": {
+ "type": "integer",
+ "optional": true,
+ "description": "The height of the window, including the frame, in pixels. Under some circumstances a Window may not be assigned height property, for example when querying closed windows from the $(ref:sessions) API."
+ },
+ "tabs": {
+ "type": "array",
+ "items": { "$ref": "tabs.Tab" },
+ "optional": true,
+ "description": "Array of $(ref:tabs.Tab) objects representing the current tabs in the window."
+ },
+ "incognito": {
+ "type": "boolean",
+ "description": "Whether the window is incognito."
+ },
+ "type": {
+ "$ref": "WindowType",
+ "optional": true,
+ "description": "The type of browser window this is."
+ },
+ "state": {
+ "$ref": "WindowState",
+ "optional": true,
+ "description": "The state of this browser window."
+ },
+ "alwaysOnTop": {
+ "type": "boolean",
+ "description": "Whether the window is set to be always on top."
+ },
+ "sessionId": {
+ "type": "string",
+ "optional": true,
+ "description": "The session ID used to uniquely identify a Window obtained from the $(ref:sessions) API."
+ }
+ }
+ },
+ {
+ "id": "CreateType",
+ "type": "string",
+ "description": "Specifies what type of browser window to create. The 'panel' and 'detached_panel' types create a popup unless the '--enable-panels' flag is set.",
+ "enum": ["normal", "popup", "panel", "detached_panel"]
+ }
+ ],
+ "properties": {
+ "WINDOW_ID_NONE": {
+ "value": -1,
+ "description": "The windowId value that represents the absence of a browser window."
+ },
+ "WINDOW_ID_CURRENT": {
+ "value": -2,
+ "description": "The windowId value that represents the $(topic:current-window)[current window]."
+ }
+ },
+ "functions": [
+ {
+ "name": "get",
+ "type": "function",
+ "description": "Gets details about a window.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -2
+ },
+ {
+ "type": "object",
+ "name": "getInfo",
+ "optional": true,
+ "description": "",
+ "properties": {
+ "populate": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, the $(ref:windows.Window) object will have a <var>tabs</var> property that contains a list of the $(ref:tabs.Tab) objects. The <code>Tab</code> objects only contain the <code>url</code>, <code>title</code> and <code>favIconUrl</code> properties if the extension's manifest file includes the <code>\"tabs\"</code> permission."
+ },
+ "windowTypes": {
+ "type": "array",
+ "items": {
+ "$ref": "WindowType"
+ },
+ "optional": true,
+ "description": "If set, the $(ref:windows.Window) returned will be filtered based on its type. If unset the default filter is set to <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "Window"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getCurrent",
+ "type": "function",
+ "description": "Gets the $(topic:current-window)[current window].",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "getInfo",
+ "optional": true,
+ "description": "",
+ "properties": {
+ "populate": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, the $(ref:windows.Window) object will have a <var>tabs</var> property that contains a list of the $(ref:tabs.Tab) objects. The <code>Tab</code> objects only contain the <code>url</code>, <code>title</code> and <code>favIconUrl</code> properties if the extension's manifest file includes the <code>\"tabs\"</code> permission."
+ },
+ "windowTypes": {
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "optional": true,
+ "description": "If set, the $(ref:windows.Window) returned will be filtered based on its type. If unset the default filter is set to <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "Window"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getLastFocused",
+ "type": "function",
+ "description": "Gets the window that was most recently focused &mdash; typically the window 'on top'.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "getInfo",
+ "optional": true,
+ "description": "",
+ "properties": {
+ "populate": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, the $(ref:windows.Window) object will have a <var>tabs</var> property that contains a list of the $(ref:tabs.Tab) objects. The <code>Tab</code> objects only contain the <code>url</code>, <code>title</code> and <code>favIconUrl</code> properties if the extension's manifest file includes the <code>\"tabs\"</code> permission."
+ },
+ "windowTypes": {
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "optional": true,
+ "description": "If set, the $(ref:windows.Window) returned will be filtered based on its type. If unset the default filter is set to <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "Window"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "getAll",
+ "type": "function",
+ "description": "Gets all windows.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "getInfo",
+ "optional": true,
+ "description": "",
+ "properties": {
+ "populate": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, each $(ref:windows.Window) object will have a <var>tabs</var> property that contains a list of the $(ref:tabs.Tab) objects for that window. The <code>Tab</code> objects only contain the <code>url</code>, <code>title</code> and <code>favIconUrl</code> properties if the extension's manifest file includes the <code>\"tabs\"</code> permission."
+ },
+ "windowTypes": {
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "optional": true,
+ "description": "If set, the $(ref:windows.Window) returned will be filtered based on its type. If unset the default filter is set to <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "parameters": [
+ {
+ "name": "windows",
+ "type": "array",
+ "items": { "$ref": "Window" }
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "create",
+ "type": "function",
+ "description": "Creates (opens) a new browser with any optional sizing, position or default URL provided.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "object",
+ "name": "createData",
+ "optional": true,
+ "default": {},
+ "properties": {
+ "url": {
+ "description": "A URL or array of URLs to open as tabs in the window. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.",
+ "optional": true,
+ "choices": [
+ { "type": "string", "format": "relativeUrl" },
+ {
+ "type": "array",
+ "items": { "type": "string", "format": "relativeUrl" }
+ }
+ ]
+ },
+ "tabId": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The id of the tab for which you want to adopt to the new window."
+ },
+ "left": {
+ "type": "integer",
+ "optional": true,
+ "description": "The number of pixels to position the new window from the left edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels."
+ },
+ "top": {
+ "type": "integer",
+ "optional": true,
+ "description": "The number of pixels to position the new window from the top edge of the screen. If not specified, the new window is offset naturally from the last focused window. This value is ignored for panels."
+ },
+ "width": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The width in pixels of the new window, including the frame. If not specified defaults to a natural width."
+ },
+ "height": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The height in pixels of the new window, including the frame. If not specified defaults to a natural height."
+ },
+ "focused": {
+ "unsupported": true,
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, opens an active window. If false, opens an inactive window."
+ },
+ "incognito": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Whether the new window should be an incognito window."
+ },
+ "type": {
+ "$ref": "CreateType",
+ "optional": true,
+ "description": "Specifies what type of browser window to create. The 'panel' and 'detached_panel' types create a popup unless the '--enable-panels' flag is set."
+ },
+ "state": {
+ "$ref": "WindowState",
+ "optional": true,
+ "description": "The initial state of the window. The 'minimized', 'maximized' and 'fullscreen' states cannot be combined with 'left', 'top', 'width' or 'height'."
+ },
+ "allowScriptsToClose": {
+ "type": "boolean",
+ "optional": true,
+ "description": "Allow scripts to close the window."
+ }
+ },
+ "optional": true
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "Window",
+ "description": "Contains details about the created window.",
+ "optional": true
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "update",
+ "type": "function",
+ "description": "Updates the properties of a window. Specify only the properties that you want to change; unspecified properties will be left unchanged.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -2
+ },
+ {
+ "type": "object",
+ "name": "updateInfo",
+ "properties": {
+ "left": {
+ "type": "integer",
+ "optional": true,
+ "description": "The offset from the left edge of the screen to move the window to in pixels. This value is ignored for panels."
+ },
+ "top": {
+ "type": "integer",
+ "optional": true,
+ "description": "The offset from the top edge of the screen to move the window to in pixels. This value is ignored for panels."
+ },
+ "width": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The width to resize the window to in pixels. This value is ignored for panels."
+ },
+ "height": {
+ "type": "integer",
+ "minimum": 0,
+ "optional": true,
+ "description": "The height to resize the window to in pixels. This value is ignored for panels."
+ },
+ "focused": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, brings the window to the front. If false, brings the next window in the z-order to the front."
+ },
+ "drawAttention": {
+ "type": "boolean",
+ "optional": true,
+ "description": "If true, causes the window to be displayed in a manner that draws the user's attention to the window, without changing the focused window. The effect lasts until the user changes focus to the window. This option has no effect if the window already has focus. Set to false to cancel a previous draw attention request."
+ },
+ "state": {
+ "$ref": "WindowState",
+ "optional": true,
+ "description": "The new state of the window. The 'minimized', 'maximized' and 'fullscreen' states cannot be combined with 'left', 'top', 'width' or 'height'."
+ }
+ }
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": [
+ {
+ "name": "window",
+ "$ref": "Window"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "name": "remove",
+ "type": "function",
+ "description": "Removes (closes) a window, and all the tabs inside it.",
+ "async": "callback",
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": 0
+ },
+ {
+ "type": "function",
+ "name": "callback",
+ "optional": true,
+ "parameters": []
+ }
+ ]
+ }
+ ],
+ "events": [
+ {
+ "name": "onCreated",
+ "type": "function",
+ "description": "Fired when a window is created.",
+ "filters": [
+ {
+ "name": "windowTypes",
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "description": "Conditions that the window's type being created must satisfy. By default it will satisfy <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ ],
+ "parameters": [
+ {
+ "$ref": "Window",
+ "name": "window",
+ "description": "Details of the window that was created."
+ }
+ ]
+ },
+ {
+ "name": "onRemoved",
+ "type": "function",
+ "description": "Fired when a window is removed (closed).",
+ "filters": [
+ {
+ "name": "windowTypes",
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "description": "Conditions that the window's type being removed must satisfy. By default it will satisfy <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ ],
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": 0,
+ "description": "ID of the removed window."
+ }
+ ]
+ },
+ {
+ "name": "onFocusChanged",
+ "type": "function",
+ "description": "Fired when the currently focused window changes. Will be $(ref:windows.WINDOW_ID_NONE) if all browser windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE will always be sent immediately preceding a switch from one browser window to another.",
+ "filters": [
+ {
+ "name": "windowTypes",
+ "type": "array",
+ "items": { "$ref": "WindowType" },
+ "description": "Conditions that the window's type being removed must satisfy. By default it will satisfy <code>['app', 'normal', 'panel', 'popup']</code>, with <code>'app'</code> and <code>'panel'</code> window types limited to the extension's own windows."
+ }
+ ],
+ "parameters": [
+ {
+ "type": "integer",
+ "name": "windowId",
+ "minimum": -1,
+ "description": "ID of the newly focused window."
+ }
+ ]
+ }
+ ]
+ }
+]
diff --git a/browser/components/extensions/test/browser/.eslintrc.js b/browser/components/extensions/test/browser/.eslintrc.js
new file mode 100644
index 000000000..0e673ecb9
--- /dev/null
+++ b/browser/components/extensions/test/browser/.eslintrc.js
@@ -0,0 +1,36 @@
+"use strict";
+
+module.exports = { // eslint-disable-line no-undef
+ "extends": "../../../../../testing/mochitest/browser.eslintrc.js",
+
+ "env": {
+ "webextensions": true,
+ },
+
+ "globals": {
+ "NetUtil": true,
+ "XPCOMUtils": true,
+ "Task": true,
+
+ // Browser window globals.
+ "PanelUI": false,
+
+ // Test harness globals
+ "ExtensionTestUtils": false,
+ "TestUtils": false,
+
+ "clickBrowserAction": true,
+ "clickPageAction": true,
+ "closeContextMenu": true,
+ "closeExtensionContextMenu": true,
+ "focusWindow": true,
+ "makeWidgetId": true,
+ "openContextMenu": true,
+ "openExtensionContextMenu": true,
+ "CustomizableUI": true,
+ },
+
+ "rules": {
+ "no-shadow": 0,
+ },
+};
diff --git a/browser/components/extensions/test/browser/browser.ini b/browser/components/extensions/test/browser/browser.ini
new file mode 100644
index 000000000..1e894dcb5
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser.ini
@@ -0,0 +1,115 @@
+[DEFAULT]
+support-files =
+ head.js
+ head_pageAction.js
+ head_sessions.js
+ context.html
+ ctxmenu-image.png
+ context_tabs_onUpdated_page.html
+ context_tabs_onUpdated_iframe.html
+ file_popup_api_injection_a.html
+ file_popup_api_injection_b.html
+ file_iframe_document.html
+ file_iframe_document.sjs
+ file_bypass_cache.sjs
+ file_language_fr_en.html
+ file_language_ja.html
+ file_language_tlh.html
+ file_dummy.html
+ searchSuggestionEngine.xml
+ searchSuggestionEngine.sjs
+ ../../../../../toolkit/components/extensions/test/mochitest/head_webrequest.js
+tags = webextensions
+
+
+[browser_ext_browserAction_context.js]
+[browser_ext_browserAction_disabled.js]
+[browser_ext_browserAction_pageAction_icon.js]
+[browser_ext_browserAction_pageAction_icon_permissions.js]
+[browser_ext_browserAction_popup.js]
+[browser_ext_browserAction_popup_resize.js]
+[browser_ext_browserAction_simple.js]
+[browser_ext_commands_execute_browser_action.js]
+[browser_ext_commands_execute_page_action.js]
+[browser_ext_commands_getAll.js]
+[browser_ext_commands_onCommand.js]
+[browser_ext_contentscript_connect.js]
+[browser_ext_contextMenus.js]
+[browser_ext_contextMenus_checkboxes.js]
+[browser_ext_contextMenus_icons.js]
+[browser_ext_contextMenus_onclick.js]
+[browser_ext_contextMenus_radioGroups.js]
+[browser_ext_contextMenus_uninstall.js]
+[browser_ext_contextMenus_urlPatterns.js]
+[browser_ext_currentWindow.js]
+[browser_ext_getViews.js]
+[browser_ext_incognito_popup.js]
+[browser_ext_incognito_views.js]
+[browser_ext_lastError.js]
+[browser_ext_legacy_extension_context_contentscript.js]
+[browser_ext_omnibox.js]
+[browser_ext_optionsPage_privileges.js]
+[browser_ext_pageAction_context.js]
+[browser_ext_pageAction_popup.js]
+[browser_ext_pageAction_popup_resize.js]
+[browser_ext_pageAction_simple.js]
+[browser_ext_pageAction_title.js]
+[browser_ext_popup_api_injection.js]
+[browser_ext_popup_background.js]
+[browser_ext_popup_corners.js]
+[browser_ext_popup_sendMessage.js]
+[browser_ext_popup_shutdown.js]
+[browser_ext_runtime_openOptionsPage.js]
+[browser_ext_runtime_openOptionsPage_uninstall.js]
+[browser_ext_runtime_setUninstallURL.js]
+[browser_ext_sessions_getRecentlyClosed.js]
+[browser_ext_sessions_getRecentlyClosed_private.js]
+[browser_ext_sessions_getRecentlyClosed_tabs.js]
+[browser_ext_sessions_restore.js]
+[browser_ext_simple.js]
+[browser_ext_tab_runtimeConnect.js]
+[browser_ext_tabs_audio.js]
+[browser_ext_tabs_captureVisibleTab.js]
+[browser_ext_tabs_create.js]
+[browser_ext_tabs_create_invalid_url.js]
+[browser_ext_tabs_detectLanguage.js]
+[browser_ext_tabs_duplicate.js]
+[browser_ext_tabs_events.js]
+[browser_ext_tabs_executeScript.js]
+[browser_ext_tabs_executeScript_good.js]
+[browser_ext_tabs_executeScript_bad.js]
+[browser_ext_tabs_executeScript_runAt.js]
+[browser_ext_tabs_getCurrent.js]
+[browser_ext_tabs_insertCSS.js]
+[browser_ext_tabs_removeCSS.js]
+[browser_ext_tabs_move.js]
+[browser_ext_tabs_move_window.js]
+[browser_ext_tabs_move_window_multiple.js]
+[browser_ext_tabs_move_window_pinned.js]
+[browser_ext_tabs_onHighlighted.js]
+[browser_ext_tabs_onUpdated.js]
+[browser_ext_tabs_query.js]
+[browser_ext_tabs_reload.js]
+[browser_ext_tabs_reload_bypass_cache.js]
+[browser_ext_tabs_sendMessage.js]
+[browser_ext_tabs_cookieStoreId.js]
+[browser_ext_tabs_update.js]
+[browser_ext_tabs_zoom.js]
+[browser_ext_tabs_update_url.js]
+[browser_ext_topwindowid.js]
+[browser_ext_webNavigation_frameId0.js]
+[browser_ext_webNavigation_getFrames.js]
+[browser_ext_webNavigation_urlbar_transitions.js]
+[browser_ext_webRequest.js]
+[browser_ext_windows.js]
+[browser_ext_windows_allowScriptsToClose.js]
+[browser_ext_windows_create.js]
+tags = fullscreen
+[browser_ext_windows_create_params.js]
+[browser_ext_windows_create_tabId.js]
+[browser_ext_windows_create_url.js]
+[browser_ext_windows_events.js]
+[browser_ext_windows_size.js]
+skip-if = os == 'mac' # Fails when windows are randomly opened in fullscreen mode
+[browser_ext_windows_update.js]
+tags = fullscreen
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_context.js b/browser/components/extensions/test/browser/browser_ext_browserAction_context.js
new file mode 100644
index 000000000..8a26dbb3c
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_context.js
@@ -0,0 +1,398 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* runTests(options) {
+ async function background(getTests) {
+ async function checkDetails(expecting, tabId) {
+ let title = await browser.browserAction.getTitle({tabId});
+ browser.test.assertEq(expecting.title, title,
+ "expected value from getTitle");
+
+ let popup = await browser.browserAction.getPopup({tabId});
+ browser.test.assertEq(expecting.popup, popup,
+ "expected value from getPopup");
+
+ let badge = await browser.browserAction.getBadgeText({tabId});
+ browser.test.assertEq(expecting.badge, badge,
+ "expected value from getBadge");
+
+ let badgeBackgroundColor = await browser.browserAction.getBadgeBackgroundColor({tabId});
+ browser.test.assertEq(String(expecting.badgeBackgroundColor),
+ String(badgeBackgroundColor),
+ "expected value from getBadgeBackgroundColor");
+ }
+
+ let expectDefaults = expecting => {
+ return checkDetails(expecting);
+ };
+
+ let tabs = [];
+ let tests = getTests(tabs, expectDefaults);
+
+ {
+ let tabId = 0xdeadbeef;
+ let calls = [
+ () => browser.browserAction.enable(tabId),
+ () => browser.browserAction.disable(tabId),
+ () => browser.browserAction.setTitle({tabId, title: "foo"}),
+ () => browser.browserAction.setIcon({tabId, path: "foo.png"}),
+ () => browser.browserAction.setPopup({tabId, popup: "foo.html"}),
+ () => browser.browserAction.setBadgeText({tabId, text: "foo"}),
+ () => browser.browserAction.setBadgeBackgroundColor({tabId, color: [0xff, 0, 0, 0xff]}),
+ ];
+
+ for (let call of calls) {
+ await browser.test.assertRejects(
+ new Promise(resolve => resolve(call())),
+ RegExp(`Invalid tab ID: ${tabId}`),
+ "Expected invalid tab ID error");
+ }
+ }
+
+ // Runs the next test in the `tests` array, checks the results,
+ // and passes control back to the outer test scope.
+ function nextTest() {
+ let test = tests.shift();
+
+ test(async expecting => {
+ // Check that the API returns the expected values, and then
+ // run the next test.
+ let tabs = await browser.tabs.query({active: true, currentWindow: true});
+ await checkDetails(expecting, tabs[0].id);
+
+ // Check that the actual icon has the expected values, then
+ // run the next test.
+ browser.test.sendMessage("nextTest", expecting, tests.length);
+ });
+ }
+
+ browser.test.onMessage.addListener((msg) => {
+ if (msg != "runNextTest") {
+ browser.test.fail("Expecting 'runNextTest' message");
+ }
+
+ nextTest();
+ });
+
+ browser.tabs.query({active: true, currentWindow: true}, resultTabs => {
+ tabs[0] = resultTabs[0].id;
+
+ nextTest();
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: options.manifest,
+
+ files: options.files || {},
+
+ background: `(${background})(${options.getTests})`,
+ });
+
+ let browserActionId;
+ function checkDetails(details) {
+ if (!browserActionId) {
+ browserActionId = `${makeWidgetId(extension.id)}-browser-action`;
+ }
+
+ let button = document.getElementById(browserActionId);
+
+ ok(button, "button exists");
+
+ let title = details.title || options.manifest.name;
+
+ is(getListStyleImage(button), details.icon, "icon URL is correct");
+ is(button.getAttribute("tooltiptext"), title, "image title is correct");
+ is(button.getAttribute("label"), title, "image label is correct");
+ is(button.getAttribute("badge"), details.badge, "badge text is correct");
+ is(button.getAttribute("disabled") == "true", Boolean(details.disabled), "disabled state is correct");
+
+ if (details.badge && details.badgeBackgroundColor) {
+ let badge = button.ownerDocument.getAnonymousElementByAttribute(
+ button, "class", "toolbarbutton-badge");
+
+ let badgeColor = window.getComputedStyle(badge).backgroundColor;
+ let color = details.badgeBackgroundColor;
+ let expectedColor = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
+
+ is(badgeColor, expectedColor, "badge color is correct");
+ }
+
+
+ // TODO: Popup URL.
+ }
+
+ let awaitFinish = new Promise(resolve => {
+ extension.onMessage("nextTest", (expecting, testsRemaining) => {
+ checkDetails(expecting);
+
+ if (testsRemaining) {
+ extension.sendMessage("runNextTest");
+ } else {
+ resolve();
+ }
+ });
+ });
+
+ yield extension.startup();
+
+ yield awaitFinish;
+
+ yield extension.unload();
+}
+
+add_task(function* testTabSwitchContext() {
+ yield runTests({
+ manifest: {
+ "browser_action": {
+ "default_icon": "default.png",
+ "default_popup": "__MSG_popup__",
+ "default_title": "Default __MSG_title__",
+ },
+
+ "default_locale": "en",
+
+ "permissions": ["tabs"],
+ },
+
+ "files": {
+ "_locales/en/messages.json": {
+ "popup": {
+ "message": "default.html",
+ "description": "Popup",
+ },
+
+ "title": {
+ "message": "Title",
+ "description": "Title",
+ },
+ },
+
+ "default.png": imageBuffer,
+ "default-2.png": imageBuffer,
+ "1.png": imageBuffer,
+ "2.png": imageBuffer,
+ },
+
+ getTests(tabs, expectDefaults) {
+ const DEFAULT_BADGE_COLOR = [0xd9, 0, 0, 255];
+
+ let details = [
+ {"icon": browser.runtime.getURL("default.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default Title",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR},
+ {"icon": browser.runtime.getURL("1.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default Title",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR},
+ {"icon": browser.runtime.getURL("2.png"),
+ "popup": browser.runtime.getURL("2.html"),
+ "title": "Title 2",
+ "badge": "2",
+ "badgeBackgroundColor": [0xff, 0, 0, 0xff],
+ "disabled": true},
+ {"icon": browser.runtime.getURL("1.png"),
+ "popup": browser.runtime.getURL("default-2.html"),
+ "title": "Default Title 2",
+ "badge": "d2",
+ "badgeBackgroundColor": [0, 0xff, 0, 0xff],
+ "disabled": true},
+ {"icon": browser.runtime.getURL("1.png"),
+ "popup": browser.runtime.getURL("default-2.html"),
+ "title": "Default Title 2",
+ "badge": "d2",
+ "badgeBackgroundColor": [0, 0xff, 0, 0xff],
+ "disabled": false},
+ {"icon": browser.runtime.getURL("default-2.png"),
+ "popup": browser.runtime.getURL("default-2.html"),
+ "title": "Default Title 2",
+ "badge": "d2",
+ "badgeBackgroundColor": [0, 0xff, 0, 0xff]},
+ ];
+
+ return [
+ async expect => {
+ browser.test.log("Initial state, expect default properties.");
+
+ await expectDefaults(details[0]);
+ expect(details[0]);
+ },
+ async expect => {
+ browser.test.log("Change the icon in the current tab. Expect default properties excluding the icon.");
+ browser.browserAction.setIcon({tabId: tabs[0], path: "1.png"});
+
+ await expectDefaults(details[0]);
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Create a new tab. Expect default properties.");
+ let tab = await browser.tabs.create({active: true, url: "about:blank?0"});
+ tabs.push(tab.id);
+
+ await expectDefaults(details[0]);
+ expect(details[0]);
+ },
+ async expect => {
+ browser.test.log("Change properties. Expect new properties.");
+ let tabId = tabs[1];
+ browser.browserAction.setIcon({tabId, path: "2.png"});
+ browser.browserAction.setPopup({tabId, popup: "2.html"});
+ browser.browserAction.setTitle({tabId, title: "Title 2"});
+ browser.browserAction.setBadgeText({tabId, text: "2"});
+ browser.browserAction.setBadgeBackgroundColor({tabId, color: "#ff0000"});
+ browser.browserAction.disable(tabId);
+
+ await expectDefaults(details[0]);
+ expect(details[2]);
+ },
+ expect => {
+ browser.test.log("Navigate to a new page. Expect no changes.");
+
+ // TODO: This listener should not be necessary, but the |tabs.update|
+ // callback currently fires too early in e10s windows.
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed) {
+ if (tabId == tabs[1] && changed.url) {
+ browser.tabs.onUpdated.removeListener(listener);
+ expect(details[2]);
+ }
+ });
+
+ browser.tabs.update(tabs[1], {url: "about:blank?1"});
+ },
+ async expect => {
+ browser.test.log("Switch back to the first tab. Expect previously set properties.");
+ await browser.tabs.update(tabs[0], {active: true});
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Change default values, expect those changes reflected.");
+ browser.browserAction.setIcon({path: "default-2.png"});
+ browser.browserAction.setPopup({popup: "default-2.html"});
+ browser.browserAction.setTitle({title: "Default Title 2"});
+ browser.browserAction.setBadgeText({text: "d2"});
+ browser.browserAction.setBadgeBackgroundColor({color: [0, 0xff, 0, 0xff]});
+ browser.browserAction.disable();
+
+ await expectDefaults(details[3]);
+ expect(details[3]);
+ },
+ async expect => {
+ browser.test.log("Re-enable by default. Expect enabled.");
+ browser.browserAction.enable();
+
+ await expectDefaults(details[4]);
+ expect(details[4]);
+ },
+ async expect => {
+ browser.test.log("Switch back to tab 2. Expect former value, unaffected by changes to defaults in previous step.");
+ await browser.tabs.update(tabs[1], {active: true});
+
+ await expectDefaults(details[3]);
+ expect(details[2]);
+ },
+ async expect => {
+ browser.test.log("Delete tab, switch back to tab 1. Expect previous results again.");
+ await browser.tabs.remove(tabs[1]);
+ expect(details[4]);
+ },
+ async expect => {
+ browser.test.log("Create a new tab. Expect new default properties.");
+ let tab = await browser.tabs.create({active: true, url: "about:blank?2"});
+ tabs.push(tab.id);
+ expect(details[5]);
+ },
+ async expect => {
+ browser.test.log("Delete tab.");
+ await browser.tabs.remove(tabs[2]);
+ expect(details[4]);
+ },
+ ];
+ },
+ });
+});
+
+add_task(function* testDefaultTitle() {
+ yield runTests({
+ manifest: {
+ "name": "Foo Extension",
+
+ "browser_action": {
+ "default_icon": "icon.png",
+ },
+
+ "permissions": ["tabs"],
+ },
+
+ files: {
+ "icon.png": imageBuffer,
+ },
+
+ getTests(tabs, expectDefaults) {
+ const DEFAULT_BADGE_COLOR = [0xd9, 0, 0, 255];
+
+ let details = [
+ {"title": "Foo Extension",
+ "popup": "",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR,
+ "icon": browser.runtime.getURL("icon.png")},
+ {"title": "Foo Title",
+ "popup": "",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR,
+ "icon": browser.runtime.getURL("icon.png")},
+ {"title": "Bar Title",
+ "popup": "",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR,
+ "icon": browser.runtime.getURL("icon.png")},
+ {"title": "",
+ "popup": "",
+ "badge": "",
+ "badgeBackgroundColor": DEFAULT_BADGE_COLOR,
+ "icon": browser.runtime.getURL("icon.png")},
+ ];
+
+ return [
+ async expect => {
+ browser.test.log("Initial state. Expect extension title as default title.");
+
+ await expectDefaults(details[0]);
+ expect(details[0]);
+ },
+ async expect => {
+ browser.test.log("Change the title. Expect new title.");
+ browser.browserAction.setTitle({tabId: tabs[0], title: "Foo Title"});
+
+ await expectDefaults(details[0]);
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Change the default. Expect same properties.");
+ browser.browserAction.setTitle({title: "Bar Title"});
+
+ await expectDefaults(details[2]);
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Clear the title. Expect new default title.");
+ browser.browserAction.setTitle({tabId: tabs[0], title: ""});
+
+ await expectDefaults(details[2]);
+ expect(details[2]);
+ },
+ async expect => {
+ browser.test.log("Set default title to null string. Expect null string from API, extension title in UI.");
+ browser.browserAction.setTitle({title: ""});
+
+ await expectDefaults(details[3]);
+ expect(details[3]);
+ },
+ ];
+ },
+ });
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_disabled.js b/browser/components/extensions/test/browser/browser_ext_browserAction_disabled.js
new file mode 100644
index 000000000..c0b9c1a1d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_disabled.js
@@ -0,0 +1,68 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testDisabled() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {},
+ },
+
+ background: function() {
+ let clicked = false;
+
+ browser.browserAction.onClicked.addListener(() => {
+ browser.test.log("Got click event");
+ clicked = true;
+ });
+
+ browser.test.onMessage.addListener((msg, expectClick) => {
+ if (msg == "enable") {
+ browser.test.log("enable browserAction");
+ browser.browserAction.enable();
+ } else if (msg == "disable") {
+ browser.test.log("disable browserAction");
+ browser.browserAction.disable();
+ } else if (msg == "check-clicked") {
+ browser.test.assertEq(expectClick, clicked, "got click event?");
+ clicked = false;
+ } else {
+ browser.test.fail("Unexpected message");
+ }
+
+ browser.test.sendMessage("next-test");
+ });
+
+ browser.test.sendMessage("ready");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ yield clickBrowserAction(extension);
+ yield new Promise(resolve => setTimeout(resolve, 0));
+
+ extension.sendMessage("check-clicked", true);
+ yield extension.awaitMessage("next-test");
+
+ extension.sendMessage("disable");
+ yield extension.awaitMessage("next-test");
+
+ yield clickBrowserAction(extension);
+ yield new Promise(resolve => setTimeout(resolve, 0));
+
+ extension.sendMessage("check-clicked", false);
+ yield extension.awaitMessage("next-test");
+
+ extension.sendMessage("enable");
+ yield extension.awaitMessage("next-test");
+
+ yield clickBrowserAction(extension);
+ yield new Promise(resolve => setTimeout(resolve, 0));
+
+ extension.sendMessage("check-clicked", true);
+ yield extension.awaitMessage("next-test");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon.js b/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon.js
new file mode 100644
index 000000000..9665d6832
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon.js
@@ -0,0 +1,321 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+// Test that various combinations of icon details specs, for both paths
+// and ImageData objects, result in the correct image being displayed in
+// all display resolutions.
+add_task(function* testDetailsObjects() {
+ function background() {
+ function getImageData(color) {
+ let canvas = document.createElement("canvas");
+ canvas.width = 2;
+ canvas.height = 2;
+ let canvasContext = canvas.getContext("2d");
+
+ canvasContext.clearRect(0, 0, canvas.width, canvas.height);
+ canvasContext.fillStyle = color;
+ canvasContext.fillRect(0, 0, 1, 1);
+
+ return {
+ url: canvas.toDataURL("image/png"),
+ imageData: canvasContext.getImageData(0, 0, canvas.width, canvas.height),
+ };
+ }
+
+ let imageData = {
+ red: getImageData("red"),
+ green: getImageData("green"),
+ };
+
+ /* eslint-disable comma-dangle, indent */
+ let iconDetails = [
+ // Only paths.
+ {details: {"path": "a.png"},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a.png")}},
+ {details: {"path": "/a.png"},
+ resolutions: {
+ "1": browser.runtime.getURL("a.png"),
+ "2": browser.runtime.getURL("a.png")}},
+ {details: {"path": {"19": "a.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a.png")}},
+ {details: {"path": {"38": "a.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a.png")}},
+ {details: {"path": {"19": "a.png", "38": "a-x2.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a-x2.png")}},
+
+ // Test that CSS strings are escaped properly.
+ {details: {"path": 'a.png#" \\'},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png#%22%20%5C"),
+ "2": browser.runtime.getURL("data/a.png#%22%20%5C")}},
+
+ // Only ImageData objects.
+ {details: {"imageData": imageData.red.imageData},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": imageData.red.url}},
+ {details: {"imageData": {"19": imageData.red.imageData}},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": imageData.red.url}},
+ {details: {"imageData": {"38": imageData.red.imageData}},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": imageData.red.url}},
+ {details: {"imageData": {
+ "19": imageData.red.imageData,
+ "38": imageData.green.imageData}},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": imageData.green.url}},
+
+ // Mixed path and imageData objects.
+ //
+ // The behavior is currently undefined if both |path| and
+ // |imageData| specify icons of the same size.
+ {details: {
+ "path": {"19": "a.png"},
+ "imageData": {"38": imageData.red.imageData}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": imageData.red.url}},
+ {details: {
+ "path": {"38": "a.png"},
+ "imageData": {"19": imageData.red.imageData}},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": browser.runtime.getURL("data/a.png")}},
+
+ // A path or ImageData object by itself is treated as a 19px icon.
+ {details: {
+ "path": "a.png",
+ "imageData": {"38": imageData.red.imageData}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": imageData.red.url}},
+ {details: {
+ "path": {"38": "a.png"},
+ "imageData": imageData.red.imageData},
+ resolutions: {
+ "1": imageData.red.url,
+ "2": browser.runtime.getURL("data/a.png")}},
+
+ // Various resolutions
+ {details: {"path": {"18": "a.png", "36": "a-x2.png"}},
+ legacy: true,
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a-x2.png")}},
+ {details: {"path": {"16": "a.png", "30": "a-x2.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/a.png"),
+ "2": browser.runtime.getURL("data/a-x2.png")}},
+ {details: {"path": {"16": "16.png", "100": "100.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/16.png"),
+ "2": browser.runtime.getURL("data/100.png")}},
+ {details: {"path": {"2": "2.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/2.png"),
+ "2": browser.runtime.getURL("data/2.png")}},
+ {details: {"path": {
+ "16": "16.svg",
+ "18": "18.svg"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/16.svg"),
+ "2": browser.runtime.getURL("data/18.svg")}},
+ {details: {"path": {
+ "6": "6.png",
+ "18": "18.png",
+ "36": "36.png",
+ "48": "48.png",
+ "128": "128.png"}},
+ legacy: true,
+ resolutions: {
+ "1": browser.runtime.getURL("data/18.png"),
+ "2": browser.runtime.getURL("data/36.png")},
+ menuResolutions: {
+ "1": browser.runtime.getURL("data/36.png"),
+ "2": browser.runtime.getURL("data/128.png")}},
+ {details: {"path": {
+ "16": "16.png",
+ "18": "18.png",
+ "32": "32.png",
+ "48": "48.png",
+ "64": "64.png",
+ "128": "128.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/16.png"),
+ "2": browser.runtime.getURL("data/32.png")},
+ menuResolutions: {
+ "1": browser.runtime.getURL("data/32.png"),
+ "2": browser.runtime.getURL("data/64.png")}},
+ {details: {"path": {
+ "18": "18.png",
+ "32": "32.png",
+ "48": "48.png",
+ "128": "128.png"}},
+ resolutions: {
+ "1": browser.runtime.getURL("data/32.png"),
+ "2": browser.runtime.getURL("data/32.png")}},
+ ];
+
+ // Allow serializing ImageData objects for logging.
+ ImageData.prototype.toJSON = () => "<ImageData>";
+
+ let tabId;
+
+ browser.test.onMessage.addListener((msg, test) => {
+ if (msg != "setIcon") {
+ browser.test.fail("expecting 'setIcon' message");
+ }
+
+ let details = iconDetails[test.index];
+
+ let detailString = JSON.stringify(details);
+ browser.test.log(`Setting browerAction/pageAction to ${detailString} expecting URLs ${JSON.stringify(details.resolutions)}`);
+
+ Promise.all([
+ browser.browserAction.setIcon(Object.assign({tabId}, details.details)),
+ browser.pageAction.setIcon(Object.assign({tabId}, details.details)),
+ ]).then(() => {
+ browser.test.sendMessage("iconSet");
+ });
+ });
+
+ // Generate a list of tests and resolutions to send back to the test
+ // context.
+ //
+ // This process is a bit convoluted, because the outer test context needs
+ // to handle checking the button nodes and changing the screen resolution,
+ // but it can't pass us icon definitions with ImageData objects. This
+ // shouldn't be a problem, since structured clones should handle ImageData
+ // objects without issue. Unfortunately, |cloneInto| implements a slightly
+ // different algorithm than we use in web APIs, and does not handle them
+ // correctly.
+ let tests = [];
+ for (let [idx, icon] of iconDetails.entries()) {
+ tests.push({
+ index: idx,
+ legacy: !!icon.legacy,
+ menuResolutions: icon.menuResolutions,
+ resolutions: icon.resolutions,
+ });
+ }
+
+ // Sort by resolution, so we don't needlessly switch back and forth
+ // between each test.
+ tests.sort(test => test.resolution);
+
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ tabId = tabs[0].id;
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("ready", tests);
+ });
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {},
+ "page_action": {},
+ "background": {
+ "page": "data/background.html",
+ }
+ },
+
+ files: {
+ "data/background.html": `<script src="background.js"></script>`,
+ "data/background.js": background,
+
+ "data/16.svg": imageBuffer,
+ "data/18.svg": imageBuffer,
+
+ "data/16.png": imageBuffer,
+ "data/18.png": imageBuffer,
+ "data/32.png": imageBuffer,
+ "data/36.png": imageBuffer,
+ "data/48.png": imageBuffer,
+ "data/64.png": imageBuffer,
+ "data/128.png": imageBuffer,
+
+ "a.png": imageBuffer,
+ "data/2.png": imageBuffer,
+ "data/100.png": imageBuffer,
+ "data/a.png": imageBuffer,
+ "data/a-x2.png": imageBuffer,
+ },
+ });
+
+ const RESOLUTION_PREF = "layout.css.devPixelsPerPx";
+
+ yield extension.startup();
+
+ let pageActionId = `${makeWidgetId(extension.id)}-page-action`;
+ let browserActionWidget = getBrowserActionWidget(extension);
+
+ let tests = yield extension.awaitMessage("ready");
+ for (let test of tests) {
+ extension.sendMessage("setIcon", test);
+ yield extension.awaitMessage("iconSet");
+
+ let browserActionButton = browserActionWidget.forWindow(window).node;
+ let pageActionImage = document.getElementById(pageActionId);
+
+
+ // Test icon sizes in the toolbar/urlbar.
+ for (let resolution of Object.keys(test.resolutions)) {
+ yield SpecialPowers.pushPrefEnv({set: [[RESOLUTION_PREF, resolution]]});
+
+ is(window.devicePixelRatio, +resolution, "window has the required resolution");
+
+ let imageURL = test.resolutions[resolution];
+ is(getListStyleImage(browserActionButton), imageURL, `browser action has the correct image at ${resolution}x resolution`);
+ is(getListStyleImage(pageActionImage), imageURL, `page action has the correct image at ${resolution}x resolution`);
+
+ let isLegacy = browserActionButton.classList.contains("toolbarbutton-legacy-addon");
+ is(isLegacy, test.legacy, "Legacy class should be present?");
+
+ yield SpecialPowers.popPrefEnv();
+ }
+
+ if (!test.menuResolutions) {
+ continue;
+ }
+
+
+ // Test icon sizes in the menu panel.
+ CustomizableUI.addWidgetToArea(browserActionWidget.id,
+ CustomizableUI.AREA_PANEL);
+
+ yield showBrowserAction(extension);
+ browserActionButton = browserActionWidget.forWindow(window).node;
+
+ for (let resolution of Object.keys(test.menuResolutions)) {
+ yield SpecialPowers.pushPrefEnv({set: [[RESOLUTION_PREF, resolution]]});
+
+ is(window.devicePixelRatio, +resolution, "window has the required resolution");
+
+ let imageURL = test.menuResolutions[resolution];
+ is(getListStyleImage(browserActionButton), imageURL, `browser action has the correct menu image at ${resolution}x resolution`);
+
+ yield SpecialPowers.popPrefEnv();
+ }
+
+ yield closeBrowserAction(extension);
+
+ CustomizableUI.addWidgetToArea(browserActionWidget.id,
+ CustomizableUI.AREA_NAVBAR);
+ }
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon_permissions.js b/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon_permissions.js
new file mode 100644
index 000000000..110746cae
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon_permissions.js
@@ -0,0 +1,210 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+// Test that an error is thrown when providing invalid icon sizes
+add_task(function* testInvalidIconSizes() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {},
+ "page_action": {},
+ },
+
+ background: function() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ let tabId = tabs[0].id;
+
+ let promises = [];
+ for (let api of ["pageAction", "browserAction"]) {
+ // helper function to run setIcon and check if it fails
+ let assertSetIconThrows = function(detail, error, message) {
+ detail.tabId = tabId;
+ promises.push(
+ browser.test.assertRejects(
+ browser[api].setIcon(detail),
+ /must be an integer/,
+ "setIcon with invalid icon size"));
+ };
+
+ let imageData = new ImageData(1, 1);
+
+ // test invalid icon size inputs
+ for (let type of ["path", "imageData"]) {
+ let img = type == "imageData" ? imageData : "test.png";
+
+ assertSetIconThrows({[type]: {"abcdef": img}});
+ assertSetIconThrows({[type]: {"48px": img}});
+ assertSetIconThrows({[type]: {"20.5": img}});
+ assertSetIconThrows({[type]: {"5.0": img}});
+ assertSetIconThrows({[type]: {"-300": img}});
+ assertSetIconThrows({[type]: {"abc": img, "5": img}});
+ }
+
+ assertSetIconThrows({imageData: {"abcdef": imageData}, path: {"5": "test.png"}});
+ assertSetIconThrows({path: {"abcdef": "test.png"}, imageData: {"5": imageData}});
+ }
+
+ Promise.all(promises).then(() => {
+ browser.test.notifyPass("setIcon with invalid icon size");
+ });
+ });
+ },
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitFinish("setIcon with invalid icon size")]);
+
+ yield extension.unload();
+});
+
+
+// Test that default icon details in the manifest.json file are handled
+// correctly.
+add_task(function* testDefaultDetails() {
+ // TODO: Test localized variants.
+ let icons = [
+ "foo/bar.png",
+ "/foo/bar.png",
+ {"19": "foo/bar.png"},
+ {"38": "foo/bar.png"},
+ ];
+
+ if (window.devicePixelRatio > 1) {
+ icons.push({"19": "baz/quux.png", "38": "foo/bar.png"});
+ } else {
+ icons.push({"19": "foo/bar.png", "38": "baz/quux@2x.png"});
+ }
+
+ let expectedURL = new RegExp(String.raw`^moz-extension://[^/]+/foo/bar\.png$`);
+
+ for (let icon of icons) {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {"default_icon": icon},
+ "page_action": {"default_icon": icon},
+ },
+
+ background: function() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ let tabId = tabs[0].id;
+
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("ready");
+ });
+ });
+ },
+
+ files: {
+ "foo/bar.png": imageBuffer,
+ "baz/quux.png": imageBuffer,
+ "baz/quux@2x.png": imageBuffer,
+ },
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("ready")]);
+
+ let browserActionId = makeWidgetId(extension.id) + "-browser-action";
+ let pageActionId = makeWidgetId(extension.id) + "-page-action";
+
+ let browserActionButton = document.getElementById(browserActionId);
+ let image = getListStyleImage(browserActionButton);
+
+ ok(expectedURL.test(image), `browser action image ${image} matches ${expectedURL}`);
+
+ let pageActionImage = document.getElementById(pageActionId);
+ image = getListStyleImage(pageActionImage);
+
+ ok(expectedURL.test(image), `page action image ${image} matches ${expectedURL}`);
+
+ yield extension.unload();
+
+ let node = document.getElementById(pageActionId);
+ is(node, null, "pageAction image removed from document");
+ }
+});
+
+
+// Check that attempts to load a privileged URL as an icon image fail.
+add_task(function* testSecureURLsDenied() {
+ // Test URLs passed to setIcon.
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {},
+ "page_action": {},
+ },
+
+ background: function() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ let tabId = tabs[0].id;
+
+ let urls = ["chrome://browser/content/browser.xul",
+ "javascript:true"];
+
+ let promises = [];
+ for (let url of urls) {
+ for (let api of ["pageAction", "browserAction"]) {
+ promises.push(
+ browser.test.assertRejects(
+ browser[api].setIcon({tabId, path: url}),
+ /Illegal URL/,
+ `Load of '${url}' should fail.`));
+ }
+ }
+
+ Promise.all(promises).then(() => {
+ browser.test.notifyPass("setIcon security tests");
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("setIcon security tests");
+ yield extension.unload();
+});
+
+
+add_task(function* testSecureManifestURLsDenied() {
+ // Test URLs included in the manifest.
+
+ let urls = ["chrome://browser/content/browser.xul",
+ "javascript:true"];
+
+ let apis = ["browser_action", "page_action"];
+
+ for (let url of urls) {
+ for (let api of apis) {
+ info(`TEST ${api} icon url: ${url}`);
+
+ let matchURLForbidden = url => ({
+ message: new RegExp(`match the format "strictRelativeUrl"`),
+ });
+
+ let messages = [matchURLForbidden(url)];
+
+ let waitForConsole = new Promise(resolve => {
+ // Not necessary in browser-chrome tests, but monitorConsole gripes
+ // if we don't call it.
+ SimpleTest.waitForExplicitFinish();
+
+ SimpleTest.monitorConsole(resolve, messages);
+ });
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ [api]: {
+ "default_icon": url,
+ },
+ },
+ });
+
+ yield Assert.rejects(extension.startup(),
+ null,
+ "Manifest rejected");
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+ }
+ }
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_popup.js b/browser/components/extensions/test/browser/browser_ext_browserAction_popup.js
new file mode 100644
index 000000000..9f04b3c11
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_popup.js
@@ -0,0 +1,413 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function getBrowserAction(extension) {
+ const {GlobalManager, Management: {global: {browserActionFor}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let ext = GlobalManager.extensionMap.get(extension.id);
+ return browserActionFor(ext);
+}
+
+let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head><body>${url}</body></html>`;
+
+function* testInArea(area) {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "background": {
+ "page": "data/background.html",
+ },
+ "browser_action": {
+ "default_popup": "popup-a.html",
+ "browser_style": true,
+ },
+ },
+
+ files: {
+ "popup-a.html": scriptPage("popup-a.js"),
+ "popup-a.js": function() {
+ window.onload = () => {
+ let color = window.getComputedStyle(document.body).color;
+ browser.test.assertEq("rgb(34, 36, 38)", color);
+ browser.runtime.sendMessage("from-popup-a");
+ };
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "close-popup-using-window.close") {
+ window.close();
+ }
+ });
+ },
+
+ "data/popup-b.html": scriptPage("popup-b.js"),
+ "data/popup-b.js": function() {
+ window.onload = () => {
+ browser.runtime.sendMessage("from-popup-b");
+ };
+ },
+
+ "data/popup-c.html": scriptPage("popup-c.js"),
+ "data/popup-c.js": function() {
+ // Close the popup before the document is fully-loaded to make sure that
+ // we handle this case sanely.
+ browser.runtime.sendMessage("from-popup-c");
+ window.close();
+ },
+
+ "data/background.html": scriptPage("background.js"),
+
+ "data/background.js": function() {
+ let sendClick;
+ let tests = [
+ () => {
+ browser.test.log(`Click browser action, expect popup "a".`);
+ sendClick({expectEvent: false, expectPopup: "a"});
+ },
+ () => {
+ browser.test.log(`Click browser action again, expect popup "a".`);
+ sendClick({expectEvent: false, expectPopup: "a"});
+ },
+ () => {
+ browser.test.log(`Call triggerAction, expect popup "a" again. Leave popup open.`);
+ sendClick({expectEvent: false, expectPopup: "a", closePopup: false}, "trigger-action");
+ },
+ () => {
+ browser.test.log(`Call triggerAction again. Expect remaining popup closed.`);
+ sendClick({expectEvent: false, expectPopup: null}, "trigger-action");
+ browser.test.sendMessage("next-test", {waitUntilClosed: true});
+ },
+ () => {
+ browser.test.log(`Call triggerAction again. Expect popup "a" again.`);
+ sendClick({expectEvent: false, expectPopup: "a"}, "trigger-action");
+ },
+ () => {
+ browser.test.log(`Set popup to "c" and click browser action. Expect popup "c".`);
+ browser.browserAction.setPopup({popup: "popup-c.html"});
+ sendClick({expectEvent: false, expectPopup: "c", closePopup: false});
+ },
+ () => {
+ browser.test.log(`Set popup to "b" and click browser action. Expect popup "b".`);
+ browser.browserAction.setPopup({popup: "popup-b.html"});
+ sendClick({expectEvent: false, expectPopup: "b"});
+ },
+ () => {
+ browser.test.log(`Click browser action again, expect popup "b".`);
+ sendClick({expectEvent: false, expectPopup: "b"});
+ },
+ () => {
+ browser.test.log(`Clear popup URL. Click browser action. Expect click event.`);
+ browser.browserAction.setPopup({popup: ""});
+ sendClick({expectEvent: true, expectPopup: null});
+ },
+ () => {
+ browser.test.log(`Click browser action again. Expect another click event.`);
+ sendClick({expectEvent: true, expectPopup: null});
+ },
+ () => {
+ browser.test.log(`Call triggerAction. Expect click event.`);
+ sendClick({expectEvent: true, expectPopup: null}, "trigger-action");
+ },
+ () => {
+ browser.test.log(`Set popup to "a" and click browser action. Expect popup "a", and leave open.`);
+ browser.browserAction.setPopup({popup: "/popup-a.html"});
+ sendClick({expectEvent: false, expectPopup: "a", closePopup: false});
+ },
+ () => {
+ browser.test.log(`Tell popup "a" to call window.close(). Expect popup closed.`);
+ browser.test.sendMessage("next-test", {closePopupUsingWindow: true});
+ },
+ ];
+
+ let expect = {};
+ sendClick = ({expectEvent, expectPopup, runNextTest, waitUntilClosed, closePopup}, message = "send-click") => {
+ if (closePopup == undefined) {
+ closePopup = true;
+ }
+
+ expect = {event: expectEvent, popup: expectPopup, runNextTest, waitUntilClosed, closePopup};
+ browser.test.sendMessage(message);
+ };
+
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "close-popup-using-window.close") {
+ return;
+ } else if (expect.popup) {
+ browser.test.assertEq(msg, `from-popup-${expect.popup}`,
+ "expected popup opened");
+ } else {
+ browser.test.fail(`unexpected popup: ${msg}`);
+ }
+
+ expect.popup = null;
+ browser.test.sendMessage("next-test", expect);
+ });
+
+ browser.browserAction.onClicked.addListener(() => {
+ if (expect.event) {
+ browser.test.succeed("expected click event received");
+ } else {
+ browser.test.fail("unexpected click event");
+ }
+
+ expect.event = false;
+ browser.test.sendMessage("next-test", expect);
+ });
+
+ browser.test.onMessage.addListener((msg) => {
+ if (msg == "close-popup-using-window.close") {
+ browser.runtime.sendMessage("close-popup-using-window.close");
+ return;
+ }
+
+ if (msg != "next-test") {
+ browser.test.fail("Expecting 'next-test' message");
+ }
+
+ if (tests.length) {
+ let test = tests.shift();
+ test();
+ } else {
+ browser.test.notifyPass("browseraction-tests-done");
+ }
+ });
+
+ browser.test.sendMessage("next-test");
+ },
+ },
+ });
+
+ extension.onMessage("send-click", () => {
+ clickBrowserAction(extension);
+ });
+
+ extension.onMessage("trigger-action", () => {
+ getBrowserAction(extension).triggerAction(window);
+ });
+
+ let widget;
+ extension.onMessage("next-test", Task.async(function* (expecting = {}) {
+ if (!widget) {
+ widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, area);
+ }
+ if (expecting.waitUntilClosed) {
+ let panel = getBrowserActionPopup(extension);
+ if (panel && panel.state != "closed") {
+ yield promisePopupHidden(panel);
+ }
+ } else if (expecting.closePopupUsingWindow) {
+ let panel = getBrowserActionPopup(extension);
+ ok(panel, "Expect panel to exist");
+ yield promisePopupShown(panel);
+
+ extension.sendMessage("close-popup-using-window.close");
+
+ yield promisePopupHidden(panel);
+ ok(true, "Panel is closed");
+ } else if (expecting.closePopup) {
+ yield closeBrowserAction(extension);
+ }
+
+ extension.sendMessage("next-test");
+ }));
+
+ yield Promise.all([extension.startup(), extension.awaitFinish("browseraction-tests-done")]);
+
+ yield extension.unload();
+
+ let view = document.getElementById(widget.viewId);
+ is(view, null, "browserAction view removed from document");
+}
+
+add_task(function* testBrowserActionInToolbar() {
+ yield testInArea(CustomizableUI.AREA_NAVBAR);
+});
+
+add_task(function* testBrowserActionInPanel() {
+ yield testInArea(CustomizableUI.AREA_PANEL);
+});
+
+add_task(function* testBrowserActionClickCanceled() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ "permissions": ["activeTab"],
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html><html><head><meta charset="utf-8"></head></html>`,
+ },
+ });
+
+ yield extension.startup();
+
+ const {GlobalManager, Management: {global: {browserActionFor}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let ext = GlobalManager.extensionMap.get(extension.id);
+ let browserAction = browserActionFor(ext);
+
+ let widget = getBrowserActionWidget(extension).forWindow(window);
+ let tab = window.gBrowser.selectedTab;
+
+ // Test canceled click.
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mousedown", button: 0}, window);
+
+ isnot(browserAction.pendingPopup, null, "Have pending popup");
+ is(browserAction.pendingPopup.window, window, "Have pending popup for the correct window");
+
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ is(browserAction.tabToRevokeDuringClearPopup, tab, "Tab to revoke was saved");
+ is(browserAction.tabManager.hasActiveTabPermission(tab), true, "Active tab was granted permission");
+
+ EventUtils.synthesizeMouseAtCenter(document.documentElement, {type: "mouseup", button: 0}, window);
+
+ is(browserAction.pendingPopup, null, "Pending popup was cleared");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ is(browserAction.tabToRevokeDuringClearPopup, null, "Tab to revoke was removed");
+ is(browserAction.tabManager.hasActiveTabPermission(tab), false, "Permission was revoked from tab");
+
+ // Test completed click.
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mousedown", button: 0}, window);
+
+ isnot(browserAction.pendingPopup, null, "Have pending popup");
+ is(browserAction.pendingPopup.window, window, "Have pending popup for the correct window");
+
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ // We need to do these tests during the mouseup event cycle, since the click
+ // and command events will be dispatched immediately after mouseup, and void
+ // the results.
+ let mouseUpPromise = BrowserTestUtils.waitForEvent(widget.node, "mouseup", false, event => {
+ isnot(browserAction.pendingPopup, null, "Pending popup was not cleared");
+ isnot(browserAction.pendingPopupTimeout, null, "Have a pending popup timeout");
+ return true;
+ });
+
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mouseup", button: 0}, window);
+
+ yield mouseUpPromise;
+
+ is(browserAction.pendingPopup, null, "Pending popup was cleared");
+ is(browserAction.pendingPopupTimeout, null, "Pending popup timeout was cleared");
+
+ yield promisePopupShown(getBrowserActionPopup(extension));
+ yield closeBrowserAction(extension);
+
+ yield extension.unload();
+});
+
+add_task(function* testBrowserActionDisabled() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ },
+
+ background() {
+ browser.browserAction.disable();
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html><html><head><meta charset="utf-8"><script src="popup.js"></script></head></html>`,
+ "popup.js"() {
+ browser.test.fail("Should not get here");
+ },
+ },
+ });
+
+ yield extension.startup();
+
+ const {GlobalManager, Management: {global: {browserActionFor}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let ext = GlobalManager.extensionMap.get(extension.id);
+ let browserAction = browserActionFor(ext);
+
+ let widget = getBrowserActionWidget(extension).forWindow(window);
+
+ // Test canceled click.
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mousedown", button: 0}, window);
+
+ is(browserAction.pendingPopup, null, "Have no pending popup");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ EventUtils.synthesizeMouseAtCenter(document.documentElement, {type: "mouseup", button: 0}, window);
+
+ is(browserAction.pendingPopup, null, "Have no pending popup");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+
+ // Test completed click.
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mousedown", button: 0}, window);
+
+ is(browserAction.pendingPopup, null, "Have no pending popup");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ // We need to do these tests during the mouseup event cycle, since the click
+ // and command events will be dispatched immediately after mouseup, and void
+ // the results.
+ let mouseUpPromise = BrowserTestUtils.waitForEvent(widget.node, "mouseup", false, event => {
+ is(browserAction.pendingPopup, null, "Have no pending popup");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+ return true;
+ });
+
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mouseup", button: 0}, window);
+
+ yield mouseUpPromise;
+
+ is(browserAction.pendingPopup, null, "Have no pending popup");
+ is(browserAction.pendingPopupTimeout, null, "Have no pending popup timeout");
+
+ // Give the popup a chance to load and trigger a failure, if it was
+ // erroneously opened.
+ yield new Promise(resolve => setTimeout(resolve, 250));
+
+ yield extension.unload();
+});
+
+add_task(function* testBrowserActionTabPopulation() {
+ // Note: This test relates to https://bugzilla.mozilla.org/show_bug.cgi?id=1310019
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ "permissions": ["activeTab"],
+ },
+
+ files: {
+ "popup.html": scriptPage("popup.js"),
+ "popup.js": function() {
+ browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
+ browser.test.assertEq("mochitest index /",
+ tabs[0].title,
+ "Tab has the expected title on first click");
+ browser.test.sendMessage("tabTitle");
+ });
+ },
+ },
+ });
+
+ let win = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "http://example.com/");
+ yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+
+ yield extension.startup();
+
+ let widget = getBrowserActionWidget(extension).forWindow(win);
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mousedown", button: 0}, win);
+
+ yield extension.awaitMessage("tabTitle");
+
+ EventUtils.synthesizeMouseAtCenter(widget.node, {type: "mouseup", button: 0}, win);
+
+ yield extension.unload();
+ yield BrowserTestUtils.closeWindow(win);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_popup_resize.js b/browser/components/extensions/test/browser/browser_ext_browserAction_popup_resize.js
new file mode 100644
index 000000000..6c19b17f1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_popup_resize.js
@@ -0,0 +1,304 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* openPanel(extension, win = window, awaitLoad = false) {
+ clickBrowserAction(extension, win);
+
+ return yield awaitExtensionPanel(extension, win, awaitLoad);
+}
+
+add_task(function* testBrowserActionPopupResize() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ },
+
+ files: {
+ "popup.html": '<!DOCTYPE html><html><head><meta charset="utf-8"></head></html>',
+ },
+ });
+
+ yield extension.startup();
+
+ let browser = yield openPanel(extension);
+
+ function* checkSize(expected) {
+ let dims = yield promiseContentDimensions(browser);
+
+ is(dims.window.innerHeight, expected, `Panel window should be ${expected}px tall`);
+ is(dims.body.clientHeight, dims.body.scrollHeight,
+ "Panel body should be tall enough to fit its contents");
+
+ // Tolerate if it is 1px too wide, as that may happen with the current resizing method.
+ ok(Math.abs(dims.window.innerWidth - expected) <= 1, `Panel window should be ${expected}px wide`);
+ is(dims.body.clientWidth, dims.body.scrollWidth,
+ "Panel body should be wide enough to fit its contents");
+ }
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+ function setSize(size) {
+ content.document.body.style.height = `${size}px`;
+ content.document.body.style.width = `${size}px`;
+ }
+ /* eslint-enable mozilla/no-cpows-in-tests */
+
+ let sizes = [
+ 200,
+ 400,
+ 300,
+ ];
+
+ for (let size of sizes) {
+ yield alterContent(browser, setSize, size);
+ yield checkSize(size);
+ }
+
+ yield closeBrowserAction(extension);
+ yield extension.unload();
+});
+
+function* testPopupSize(standardsMode, browserWin = window, arrowSide = "top") {
+ let docType = standardsMode ? "<!DOCTYPE html>" : "";
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+ },
+
+ files: {
+ "popup.html": `${docType}
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <style type="text/css">
+ body > span {
+ display: inline-block;
+ width: 10px;
+ height: 150px;
+ border: 2px solid black;
+ }
+ .big > span {
+ width: 300px;
+ height: 100px;
+ }
+ .bigger > span {
+ width: 150px;
+ height: 150px;
+ }
+ .huge > span {
+ height: ${2 * screen.height}px;
+ }
+ </style>
+ </head>
+ <body>
+ <span></span>
+ <span></span>
+ <span></span>
+ <span></span>
+ </body>
+ </html>`,
+ },
+ });
+
+ yield extension.startup();
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+
+ if (arrowSide == "top") {
+ // Test the standalone panel for a toolbar button.
+ let browser = yield openPanel(extension, browserWin, true);
+
+ let dims = yield promiseContentDimensions(browser);
+
+ is(dims.isStandards, standardsMode, "Document has the expected compat mode");
+
+ let {innerWidth, innerHeight} = dims.window;
+
+ dims = yield alterContent(browser, () => {
+ content.document.body.classList.add("bigger");
+ });
+
+ let win = dims.window;
+ is(win.innerHeight, innerHeight, "Window height should not change");
+ ok(win.innerWidth > innerWidth, `Window width should increase (${win.innerWidth} > ${innerWidth})`);
+
+
+ dims = yield alterContent(browser, () => {
+ content.document.body.classList.remove("bigger");
+ });
+
+ win = dims.window;
+ is(win.innerHeight, innerHeight, "Window height should not change");
+
+ // The getContentSize calculation is not always reliable to single-pixel
+ // precision.
+ ok(Math.abs(win.innerWidth - innerWidth) <= 1,
+ `Window width should return to approximately its original value (${win.innerWidth} ~= ${innerWidth})`);
+
+ yield closeBrowserAction(extension, browserWin);
+ }
+
+
+ // Test the PanelUI panel for a menu panel button.
+ let widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, CustomizableUI.AREA_PANEL);
+
+ let browser = yield openPanel(extension, browserWin);
+
+ let {panel} = browserWin.PanelUI;
+ let origPanelRect = panel.getBoundingClientRect();
+
+ // Check that the panel is still positioned as expected.
+ let checkPanelPosition = () => {
+ is(panel.getAttribute("side"), arrowSide, "Panel arrow is positioned as expected");
+
+ let panelRect = panel.getBoundingClientRect();
+ if (arrowSide == "top") {
+ ok(panelRect.top, origPanelRect.top, "Panel has not moved downwards");
+ ok(panelRect.bottom >= origPanelRect.bottom, `Panel has not shrunk from original size (${panelRect.bottom} >= ${origPanelRect.bottom})`);
+
+ let screenBottom = browserWin.screen.availTop + browserWin.screen.availHeight;
+ let panelBottom = browserWin.mozInnerScreenY + panelRect.bottom;
+ ok(panelBottom <= screenBottom, `Bottom of popup should be on-screen. (${panelBottom} <= ${screenBottom})`);
+ } else {
+ ok(panelRect.bottom, origPanelRect.bottom, "Panel has not moved upwards");
+ ok(panelRect.top <= origPanelRect.top, `Panel has not shrunk from original size (${panelRect.top} <= ${origPanelRect.top})`);
+
+ let panelTop = browserWin.mozInnerScreenY + panelRect.top;
+ ok(panelTop >= browserWin.screen.availTop, `Top of popup should be on-screen. (${panelTop} >= ${browserWin.screen.availTop})`);
+ }
+ };
+
+ yield awaitBrowserLoaded(browser);
+
+ // Wait long enough to make sure the initial resize debouncing timer has
+ // expired.
+ yield new Promise(resolve => setTimeout(resolve, 100));
+
+ let dims = yield promiseContentDimensions(browser);
+
+ is(dims.isStandards, standardsMode, "Document has the expected compat mode");
+
+ // If the browser's preferred height is smaller than the initial height of the
+ // panel, then it will still take up the full available vertical space. Even
+ // so, we need to check that we've gotten the preferred height calculation
+ // correct, so check that explicitly.
+ let getHeight = () => parseFloat(browser.style.height);
+
+ let {innerWidth, innerHeight} = dims.window;
+ let height = getHeight();
+
+
+ let setClass = className => {
+ content.document.body.className = className;
+ };
+
+ info("Increase body children's width. " +
+ "Expect them to wrap, and the frame to grow vertically rather than widen.");
+
+ dims = yield alterContent(browser, setClass, "big");
+ let win = dims.window;
+
+ ok(getHeight() > height, `Browser height should increase (${getHeight()} > ${height})`);
+
+ is(win.innerWidth, innerWidth, "Window width should not change");
+ ok(win.innerHeight >= innerHeight, `Window height should increase (${win.innerHeight} >= ${innerHeight})`);
+ is(win.scrollMaxY, 0, "Document should not be vertically scrollable");
+
+ checkPanelPosition();
+
+
+ info("Increase body children's width and height. " +
+ "Expect them to wrap, and the frame to grow vertically rather than widen.");
+
+ dims = yield alterContent(browser, setClass, "bigger");
+ win = dims.window;
+
+ ok(getHeight() > height, `Browser height should increase (${getHeight()} > ${height})`);
+
+ is(win.innerWidth, innerWidth, "Window width should not change");
+ ok(win.innerHeight >= innerHeight, `Window height should increase (${win.innerHeight} >= ${innerHeight})`);
+ is(win.scrollMaxY, 0, "Document should not be vertically scrollable");
+
+ checkPanelPosition();
+
+
+ info("Increase body height beyond the height of the screen. " +
+ "Expect the panel to grow to accommodate, but not larger than the height of the screen.");
+
+ dims = yield alterContent(browser, setClass, "huge");
+ win = dims.window;
+
+ ok(getHeight() > height, `Browser height should increase (${getHeight()} > ${height})`);
+
+ is(win.innerWidth, innerWidth, "Window width should not change");
+ ok(win.innerHeight > innerHeight, `Window height should increase (${win.innerHeight} > ${innerHeight})`);
+ ok(win.innerHeight < screen.height, `Window height be less than the screen height (${win.innerHeight} < ${screen.height})`);
+ ok(win.scrollMaxY > 0, `Document should be vertically scrollable (${win.scrollMaxY} > 0)`);
+
+ checkPanelPosition();
+
+
+ info("Restore original styling. Expect original dimensions.");
+ dims = yield alterContent(browser, setClass, "");
+ win = dims.window;
+
+ is(getHeight(), height, "Browser height should return to its original value");
+
+ is(win.innerWidth, innerWidth, "Window width should not change");
+ is(win.innerHeight, innerHeight, "Window height should return to its original value");
+ is(win.scrollMaxY, 0, "Document should not be vertically scrollable");
+
+ checkPanelPosition();
+
+ yield closeBrowserAction(extension, browserWin);
+
+ yield extension.unload();
+}
+
+add_task(function* testBrowserActionMenuResizeStandards() {
+ yield testPopupSize(true);
+});
+
+add_task(function* testBrowserActionMenuResizeQuirks() {
+ yield testPopupSize(false);
+});
+
+// Test that we still make reasonable maximum size calculations when the window
+// is close enough to the bottom of the screen that the menu panel opens above,
+// rather than below, its button.
+add_task(function* testBrowserActionMenuResizeBottomArrow() {
+ const WIDTH = 800;
+ const HEIGHT = 300;
+
+ let left = screen.availLeft + screen.availWidth - WIDTH;
+ let top = screen.availTop + screen.availHeight - HEIGHT;
+
+ let win = yield BrowserTestUtils.openNewBrowserWindow();
+
+ win.resizeTo(WIDTH, HEIGHT);
+
+ // Sometimes we run into problems on Linux with resizing being asynchronous
+ // and window managers not allowing us to move the window so that any part of
+ // it is off-screen, so we need to try more than once.
+ for (let i = 0; i < 20; i++) {
+ win.moveTo(left, top);
+
+ if (win.screenX == left && win.screenY == top) {
+ break;
+ }
+
+ yield new Promise(resolve => setTimeout(resolve, 100));
+ }
+
+ yield testPopupSize(true, win, "bottom");
+
+ yield BrowserTestUtils.closeWindow(win);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_browserAction_simple.js b/browser/components/extensions/test/browser/browser_ext_browserAction_simple.js
new file mode 100644
index 000000000..e83010958
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_simple.js
@@ -0,0 +1,59 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "unrecognized_property": "with-a-random-value",
+ },
+ },
+
+ files: {
+ "popup.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="popup.js"></script>
+ </body></html>
+ `,
+
+ "popup.js": function() {
+ window.onload = () => {
+ browser.runtime.sendMessage("from-popup");
+ };
+ },
+ },
+
+ background: function() {
+ browser.runtime.onMessage.addListener(msg => {
+ browser.test.assertEq(msg, "from-popup", "correct message received");
+ browser.test.sendMessage("popup");
+ });
+ },
+ });
+
+ SimpleTest.waitForExplicitFinish();
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{
+ message: /Reading manifest: Error processing browser_action.unrecognized_property: An unexpected property was found/,
+ }]);
+ });
+
+ yield extension.startup();
+
+ // Do this a few times to make sure the pop-up is reloaded each time.
+ for (let i = 0; i < 3; i++) {
+ clickBrowserAction(extension);
+
+ yield extension.awaitMessage("popup");
+
+ closeBrowserAction(extension);
+ }
+
+ yield extension.unload();
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_commands_execute_browser_action.js b/browser/components/extensions/test/browser/browser_ext_commands_execute_browser_action.js
new file mode 100644
index 000000000..f97a735d4
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_commands_execute_browser_action.js
@@ -0,0 +1,113 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* testExecuteBrowserActionWithOptions(options = {}) {
+ let extensionOptions = {};
+
+ extensionOptions.manifest = {
+ "commands": {
+ "_execute_browser_action": {
+ "suggested_key": {
+ "default": "Alt+Shift+J",
+ },
+ },
+ },
+ "browser_action": {
+ "browser_style": true,
+ },
+ };
+
+ if (options.withPopup) {
+ extensionOptions.manifest.browser_action.default_popup = "popup.html";
+
+ extensionOptions.files = {
+ "popup.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <script src="popup.js"></script>
+ </head>
+ </html>
+ `,
+
+ "popup.js": function() {
+ browser.runtime.sendMessage("from-browser-action-popup");
+ },
+ };
+ }
+
+ extensionOptions.background = () => {
+ browser.test.onMessage.addListener((message, withPopup) => {
+ browser.commands.onCommand.addListener((commandName) => {
+ if (commandName == "_execute_browser_action") {
+ browser.test.fail("The onCommand listener should never fire for _execute_browser_action.");
+ }
+ });
+
+ browser.browserAction.onClicked.addListener(() => {
+ if (withPopup) {
+ browser.test.fail("The onClick listener should never fire if the browserAction has a popup.");
+ browser.test.notifyFail("execute-browser-action-on-clicked-fired");
+ } else {
+ browser.test.notifyPass("execute-browser-action-on-clicked-fired");
+ }
+ });
+
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "from-browser-action-popup") {
+ browser.test.notifyPass("execute-browser-action-popup-opened");
+ }
+ });
+
+ browser.test.sendMessage("send-keys");
+ });
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionOptions);
+
+ extension.onMessage("send-keys", () => {
+ EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+ });
+
+ yield extension.startup();
+
+ if (options.inArea) {
+ let widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, options.inArea);
+ }
+
+ extension.sendMessage("withPopup", options.withPopup);
+
+ if (options.withPopup) {
+ yield extension.awaitFinish("execute-browser-action-popup-opened");
+ yield closeBrowserAction(extension);
+ } else {
+ yield extension.awaitFinish("execute-browser-action-on-clicked-fired");
+ }
+ yield extension.unload();
+}
+
+add_task(function* test_execute_browser_action_with_popup() {
+ yield testExecuteBrowserActionWithOptions({
+ withPopup: true,
+ });
+});
+
+add_task(function* test_execute_browser_action_without_popup() {
+ yield testExecuteBrowserActionWithOptions();
+});
+
+add_task(function* test_execute_browser_action_in_hamburger_menu_with_popup() {
+ yield testExecuteBrowserActionWithOptions({
+ withPopup: true,
+ inArea: CustomizableUI.AREA_PANEL,
+ });
+});
+
+add_task(function* test_execute_browser_action_in_hamburger_menu_without_popup() {
+ yield testExecuteBrowserActionWithOptions({
+ inArea: CustomizableUI.AREA_PANEL,
+ });
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js b/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
new file mode 100644
index 000000000..83684493e
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
@@ -0,0 +1,133 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_execute_page_action_without_popup() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "commands": {
+ "_execute_page_action": {
+ "suggested_key": {
+ "default": "Alt+Shift+J",
+ },
+ },
+ "send-keys-command": {
+ "suggested_key": {
+ "default": "Alt+Shift+3",
+ },
+ },
+ },
+ "page_action": {},
+ },
+
+ background: function() {
+ let isShown = false;
+
+ browser.commands.onCommand.addListener((commandName) => {
+ if (commandName == "_execute_page_action") {
+ browser.test.fail(`The onCommand listener should never fire for ${commandName}.`);
+ } else if (commandName == "send-keys-command") {
+ if (!isShown) {
+ isShown = true;
+ browser.tabs.query({currentWindow: true, active: true}, tabs => {
+ tabs.forEach(tab => {
+ browser.pageAction.show(tab.id);
+ });
+ browser.test.sendMessage("send-keys");
+ });
+ }
+ }
+ });
+
+ browser.pageAction.onClicked.addListener(() => {
+ browser.test.assertTrue(isShown, "The onClicked event should fire if the page action is shown.");
+ browser.test.notifyPass("page-action-without-popup");
+ });
+
+ browser.test.sendMessage("send-keys");
+ },
+ });
+
+ extension.onMessage("send-keys", () => {
+ EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+ EventUtils.synthesizeKey("3", {altKey: true, shiftKey: true});
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("page-action-without-popup");
+ yield extension.unload();
+});
+
+add_task(function* test_execute_page_action_with_popup() {
+ let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head><body>Test Popup</body></html>`;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "commands": {
+ "_execute_page_action": {
+ "suggested_key": {
+ "default": "Alt+Shift+J",
+ },
+ },
+ "send-keys-command": {
+ "suggested_key": {
+ "default": "Alt+Shift+3",
+ },
+ },
+ },
+ "page_action": {
+ "default_popup": "popup.html",
+ },
+ },
+
+ files: {
+ "popup.html": scriptPage("popup.js"),
+ "popup.js": function() {
+ browser.runtime.sendMessage("popup-opened");
+ },
+ },
+
+ background: function() {
+ let isShown = false;
+
+ browser.commands.onCommand.addListener((message) => {
+ if (message == "_execute_page_action") {
+ browser.test.fail(`The onCommand listener should never fire for ${message}.`);
+ }
+
+ if (message == "send-keys-command") {
+ if (!isShown) {
+ isShown = true;
+ browser.tabs.query({currentWindow: true, active: true}, tabs => {
+ tabs.forEach(tab => {
+ browser.pageAction.show(tab.id);
+ });
+ browser.test.sendMessage("send-keys");
+ });
+ }
+ }
+ });
+
+ browser.pageAction.onClicked.addListener(() => {
+ browser.test.fail(`The onClicked listener should never fire when the pageAction has a popup.`);
+ });
+
+ browser.runtime.onMessage.addListener(msg => {
+ browser.test.assertEq(msg, "popup-opened", "expected popup opened");
+ browser.test.assertTrue(isShown, "The onClicked event should fire if the page action is shown.");
+ browser.test.notifyPass("page-action-with-popup");
+ });
+
+ browser.test.sendMessage("send-keys");
+ },
+ });
+
+ extension.onMessage("send-keys", () => {
+ EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+ EventUtils.synthesizeKey("3", {altKey: true, shiftKey: true});
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("page-action-with-popup");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_commands_getAll.js b/browser/components/extensions/test/browser/browser_ext_commands_getAll.js
new file mode 100644
index 000000000..5885e8aee
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_commands_getAll.js
@@ -0,0 +1,81 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+var {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm");
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "name": "Commands Extension",
+ "commands": {
+ "with-desciption": {
+ "suggested_key": {
+ "default": "Ctrl+Shift+Y",
+ },
+ "description": "should have a description",
+ },
+ "without-description": {
+ "suggested_key": {
+ "default": "Ctrl+Shift+D",
+ },
+ },
+ "with-platform-info": {
+ "suggested_key": {
+ "mac": "Ctrl+Shift+M",
+ "linux": "Ctrl+Shift+L",
+ "windows": "Ctrl+Shift+W",
+ "android": "Ctrl+Shift+A",
+ },
+ },
+ },
+ },
+
+ background: function() {
+ browser.test.onMessage.addListener((message, additionalScope) => {
+ browser.commands.getAll((commands) => {
+ let errorMessage = "getAll should return an array of commands";
+ browser.test.assertEq(commands.length, 3, errorMessage);
+
+ let command = commands.find(c => c.name == "with-desciption");
+
+ errorMessage = "The description should match what is provided in the manifest";
+ browser.test.assertEq("should have a description", command.description, errorMessage);
+
+ errorMessage = "The shortcut should match the default shortcut provided in the manifest";
+ browser.test.assertEq("Ctrl+Shift+Y", command.shortcut, errorMessage);
+
+ command = commands.find(c => c.name == "without-description");
+
+ errorMessage = "The description should be empty when it is not provided";
+ browser.test.assertEq(null, command.description, errorMessage);
+
+ errorMessage = "The shortcut should match the default shortcut provided in the manifest";
+ browser.test.assertEq("Ctrl+Shift+D", command.shortcut, errorMessage);
+
+ let platformKeys = {
+ macosx: "M",
+ linux: "L",
+ win: "W",
+ android: "A",
+ };
+
+ command = commands.find(c => c.name == "with-platform-info");
+ let platformKey = platformKeys[additionalScope.platform];
+ let shortcut = `Ctrl+Shift+${platformKey}`;
+ errorMessage = `The shortcut should match the one provided in the manifest for OS='${additionalScope.platform}'`;
+ browser.test.assertEq(shortcut, command.shortcut, errorMessage);
+
+ browser.test.notifyPass("commands");
+ });
+ });
+ browser.test.sendMessage("ready");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+ extension.sendMessage("additional-scope", {platform: AppConstants.platform});
+ yield extension.awaitFinish("commands");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js b/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
new file mode 100644
index 000000000..dd959dcec
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
@@ -0,0 +1,229 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/AppConstants.jsm");
+
+add_task(function* test_user_defined_commands() {
+ const testCommands = [
+ // Ctrl Shortcuts
+ {
+ name: "toggle-ctrl-a",
+ shortcut: "Ctrl+A",
+ key: "A",
+ modifiers: {
+ accelKey: true,
+ },
+ },
+ {
+ name: "toggle-ctrl-up",
+ shortcut: "Ctrl+Up",
+ key: "VK_UP",
+ modifiers: {
+ accelKey: true,
+ },
+ },
+ // Alt Shortcuts
+ {
+ name: "toggle-alt-a",
+ shortcut: "Alt+A",
+ key: "A",
+ modifiers: {
+ altKey: true,
+ },
+ },
+ {
+ name: "toggle-alt-down",
+ shortcut: "Alt+Down",
+ key: "VK_DOWN",
+ modifiers: {
+ altKey: true,
+ },
+ },
+ // Mac Shortcuts
+ {
+ name: "toggle-command-shift-page-up",
+ shortcutMac: "Command+Shift+PageUp",
+ key: "VK_PAGE_UP",
+ modifiers: {
+ accelKey: true,
+ shiftKey: true,
+ },
+ },
+ {
+ name: "toggle-mac-control-shift+period",
+ shortcut: "Ctrl+Shift+Period",
+ shortcutMac: "MacCtrl+Shift+Period",
+ key: "VK_PERIOD",
+ modifiers: {
+ ctrlKey: true,
+ shiftKey: true,
+ },
+ },
+ // Ctrl+Shift Shortcuts
+ {
+ name: "toggle-ctrl-shift-left",
+ shortcut: "Ctrl+Shift+Left",
+ key: "VK_LEFT",
+ modifiers: {
+ accelKey: true,
+ shiftKey: true,
+ },
+ },
+ // Alt+Shift Shortcuts
+ {
+ name: "toggle-alt-shift-1",
+ shortcut: "Alt+Shift+1",
+ key: "1",
+ modifiers: {
+ altKey: true,
+ shiftKey: true,
+ },
+ },
+ {
+ name: "toggle-alt-shift-a",
+ shortcut: "Alt+Shift+A",
+ key: "A",
+ modifiers: {
+ altKey: true,
+ shiftKey: true,
+ },
+ },
+ {
+ name: "toggle-alt-shift-right",
+ shortcut: "Alt+Shift+Right",
+ key: "VK_RIGHT",
+ modifiers: {
+ altKey: true,
+ shiftKey: true,
+ },
+ },
+ // Misc Shortcuts
+ {
+ name: "valid-command-with-unrecognized-property-name",
+ shortcut: "Alt+Shift+3",
+ key: "3",
+ modifiers: {
+ altKey: true,
+ shiftKey: true,
+ },
+ unrecognized_property: "with-a-random-value",
+ },
+ {
+ name: "spaces-in-shortcut-name",
+ shortcut: " Alt + Shift + 2 ",
+ key: "2",
+ modifiers: {
+ altKey: true,
+ shiftKey: true,
+ },
+ },
+ ];
+
+ // Create a window before the extension is loaded.
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.loadURI(win1.gBrowser.selectedBrowser, "about:robots");
+ yield BrowserTestUtils.browserLoaded(win1.gBrowser.selectedBrowser);
+
+ let commands = {};
+ let isMac = AppConstants.platform == "macosx";
+ let totalMacOnlyCommands = 0;
+
+ for (let testCommand of testCommands) {
+ let command = {
+ suggested_key: {},
+ };
+
+ if (testCommand.shortcut) {
+ command.suggested_key.default = testCommand.shortcut;
+ }
+
+ if (testCommand.shortcutMac) {
+ command.suggested_key.mac = testCommand.shortcutMac;
+ }
+
+ if (testCommand.shortcutMac && !testCommand.shortcut) {
+ totalMacOnlyCommands++;
+ }
+
+ if (testCommand.unrecognized_property) {
+ command.unrecognized_property = testCommand.unrecognized_property;
+ }
+
+ commands[testCommand.name] = command;
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "commands": commands,
+ },
+
+ background: function() {
+ browser.commands.onCommand.addListener(commandName => {
+ browser.test.sendMessage("oncommand", commandName);
+ });
+ browser.test.sendMessage("ready");
+ },
+ });
+
+ SimpleTest.waitForExplicitFinish();
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{
+ message: /Reading manifest: Error processing commands.*.unrecognized_property: An unexpected property was found/,
+ }]);
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ function* runTest(window) {
+ for (let testCommand of testCommands) {
+ if (testCommand.shortcutMac && !testCommand.shortcut && !isMac) {
+ continue;
+ }
+ EventUtils.synthesizeKey(testCommand.key, testCommand.modifiers, window);
+ let message = yield extension.awaitMessage("oncommand");
+ is(message, testCommand.name, `Expected onCommand listener to fire with the correct name: ${testCommand.name}`);
+ }
+ }
+
+ // Create another window after the extension is loaded.
+ let win2 = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.loadURI(win2.gBrowser.selectedBrowser, "about:robots");
+ yield BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);
+
+ let totalTestCommands = Object.keys(testCommands).length;
+ let expectedCommandsRegistered = isMac ? totalTestCommands : totalTestCommands - totalMacOnlyCommands;
+
+ // Confirm the keysets have been added to both windows.
+ let keysetID = `ext-keyset-id-${makeWidgetId(extension.id)}`;
+ let keyset = win1.document.getElementById(keysetID);
+ ok(keyset != null, "Expected keyset to exist");
+ is(keyset.childNodes.length, expectedCommandsRegistered, "Expected keyset to have the correct number of children");
+
+ keyset = win2.document.getElementById(keysetID);
+ ok(keyset != null, "Expected keyset to exist");
+ is(keyset.childNodes.length, expectedCommandsRegistered, "Expected keyset to have the correct number of children");
+
+ // Confirm that the commands are registered to both windows.
+ yield focusWindow(win1);
+ yield runTest(win1);
+
+ yield focusWindow(win2);
+ yield runTest(win2);
+
+ yield extension.unload();
+
+ // Confirm that the keysets have been removed from both windows after the extension is unloaded.
+ keyset = win1.document.getElementById(keysetID);
+ is(keyset, null, "Expected keyset to be removed from the window");
+
+ keyset = win2.document.getElementById(keysetID);
+ is(keyset, null, "Expected keyset to be removed from the window");
+
+ yield BrowserTestUtils.closeWindow(win1);
+ yield BrowserTestUtils.closeWindow(win2);
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contentscript_connect.js b/browser/components/extensions/test/browser/browser_ext_contentscript_connect.js
new file mode 100644
index 000000000..8b2d9badf
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contentscript_connect.js
@@ -0,0 +1,67 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/"],
+ },
+
+ background: function() {
+ let ports_received = 0;
+ let port_messages_received = 0;
+
+ browser.runtime.onConnect.addListener((port) => {
+ browser.test.assertTrue(!!port, "port1 received");
+
+ ports_received++;
+ browser.test.assertEq(1, ports_received, "1 port received");
+
+ port.onMessage.addListener((msg, msgPort) => {
+ browser.test.assertEq("port message", msg, "listener1 port message received");
+ browser.test.assertEq(port, msgPort, "onMessage should receive port as second argument");
+
+ port_messages_received++;
+ browser.test.assertEq(1, port_messages_received, "1 port message received");
+ });
+ });
+ browser.runtime.onConnect.addListener((port) => {
+ browser.test.assertTrue(!!port, "port2 received");
+
+ ports_received++;
+ browser.test.assertEq(2, ports_received, "2 ports received");
+
+ port.onMessage.addListener((msg, msgPort) => {
+ browser.test.assertEq("port message", msg, "listener2 port message received");
+ browser.test.assertEq(port, msgPort, "onMessage should receive port as second argument");
+
+ port_messages_received++;
+ browser.test.assertEq(2, port_messages_received, "2 port messages received");
+
+ browser.test.notifyPass("contentscript_connect.pass");
+ });
+ });
+
+ browser.tabs.executeScript({file: "script.js"}).catch(e => {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("contentscript_connect.pass");
+ });
+ },
+
+ files: {
+ "script.js": function() {
+ let port = browser.runtime.connect();
+ port.postMessage("port message");
+ },
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contentscript_connect.pass");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus.js b/browser/components/extensions/test/browser/browser_ext_contextMenus.js
new file mode 100644
index 000000000..fa1483b20
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus.js
@@ -0,0 +1,342 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+const PAGE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: function() {
+ browser.contextMenus.create({
+ id: "clickme-image",
+ title: "Click me!",
+ contexts: ["image"],
+ });
+ browser.contextMenus.create({
+ id: "clickme-page",
+ title: "Click me!",
+ contexts: ["page"],
+ });
+ browser.test.notifyPass();
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish();
+
+ let contentAreaContextMenu = yield openContextMenu("#img1");
+ let item = contentAreaContextMenu.getElementsByAttribute("label", "Click me!");
+ is(item.length, 1, "contextMenu item for image was found");
+ yield closeContextMenu();
+
+ contentAreaContextMenu = yield openContextMenu("body");
+ item = contentAreaContextMenu.getElementsByAttribute("label", "Click me!");
+ is(item.length, 1, "contextMenu item for page was found");
+ yield closeContextMenu();
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+});
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: async function() {
+ // A generic onclick callback function.
+ function genericOnClick(info, tab) {
+ browser.test.sendMessage("onclick", {info, tab});
+ }
+
+ browser.contextMenus.onClicked.addListener((info, tab) => {
+ browser.test.sendMessage("browser.contextMenus.onClicked", {info, tab});
+ });
+
+ browser.contextMenus.create({
+ contexts: ["all"],
+ type: "separator",
+ });
+
+ let contexts = ["page", "selection", "image", "editable"];
+ for (let i = 0; i < contexts.length; i++) {
+ let context = contexts[i];
+ let title = context;
+ browser.contextMenus.create({
+ title: title,
+ contexts: [context],
+ id: "ext-" + context,
+ onclick: genericOnClick,
+ });
+ if (context == "selection") {
+ browser.contextMenus.update("ext-selection", {
+ title: "selection is: '%s'",
+ onclick: (info, tab) => {
+ browser.contextMenus.removeAll();
+ genericOnClick(info, tab);
+ },
+ });
+ }
+ }
+
+ let parent = browser.contextMenus.create({
+ title: "parent",
+ });
+ browser.contextMenus.create({
+ title: "child1",
+ parentId: parent,
+ onclick: genericOnClick,
+ });
+ let child2 = browser.contextMenus.create({
+ title: "child2",
+ parentId: parent,
+ onclick: genericOnClick,
+ });
+
+ let parentToDel = browser.contextMenus.create({
+ title: "parentToDel",
+ });
+ browser.contextMenus.create({
+ title: "child1",
+ parentId: parentToDel,
+ onclick: genericOnClick,
+ });
+ browser.contextMenus.create({
+ title: "child2",
+ parentId: parentToDel,
+ onclick: genericOnClick,
+ });
+ browser.contextMenus.remove(parentToDel);
+
+ browser.contextMenus.create({
+ title: "Without onclick property",
+ id: "ext-without-onclick",
+ });
+
+ await browser.test.assertRejects(
+ browser.contextMenus.update(parent, {parentId: child2}),
+ /cannot be an ancestor/,
+ "Should not be able to reparent an item as descendent of itself");
+
+ browser.test.notifyPass("contextmenus");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus");
+
+ let expectedClickInfo = {
+ menuItemId: "ext-image",
+ mediaType: "image",
+ srcUrl: "http://mochi.test:8888/browser/browser/components/extensions/test/browser/ctxmenu-image.png",
+ pageUrl: PAGE,
+ editable: false,
+ };
+
+ function checkClickInfo(result) {
+ for (let i of Object.keys(expectedClickInfo)) {
+ is(result.info[i], expectedClickInfo[i],
+ "click info " + i + " expected to be: " + expectedClickInfo[i] + " but was: " + result.info[i]);
+ }
+ is(expectedClickInfo.pageSrc, result.tab.url, "click info page source is the right tab");
+ }
+
+ let extensionMenuRoot = yield openExtensionContextMenu();
+
+ // Check some menu items
+ let items = extensionMenuRoot.getElementsByAttribute("label", "image");
+ is(items.length, 1, "contextMenu item for image was found (context=image)");
+ let image = items[0];
+
+ items = extensionMenuRoot.getElementsByAttribute("label", "selection-edited");
+ is(items.length, 0, "contextMenu item for selection was not found (context=image)");
+
+ items = extensionMenuRoot.getElementsByAttribute("label", "parentToDel");
+ is(items.length, 0, "contextMenu item for removed parent was not found (context=image)");
+
+ items = extensionMenuRoot.getElementsByAttribute("label", "parent");
+ is(items.length, 1, "contextMenu item for parent was found (context=image)");
+
+ is(items[0].childNodes[0].childNodes.length, 2, "child items for parent were found (context=image)");
+
+ // Click on ext-image item and check the click results
+ yield closeExtensionContextMenu(image);
+
+ let result = yield extension.awaitMessage("onclick");
+ checkClickInfo(result);
+ result = yield extension.awaitMessage("browser.contextMenus.onClicked");
+ checkClickInfo(result);
+
+
+ // Test "editable" context and OnClick data property.
+ extensionMenuRoot = yield openExtensionContextMenu("#edit-me");
+
+ // Check some menu items.
+ items = extensionMenuRoot.getElementsByAttribute("label", "editable");
+ is(items.length, 1, "contextMenu item for text input element was found (context=editable)");
+ let editable = items[0];
+
+ // Click on ext-editable item and check the click results.
+ yield closeExtensionContextMenu(editable);
+
+ expectedClickInfo = {
+ menuItemId: "ext-editable",
+ pageUrl: PAGE,
+ editable: true,
+ };
+
+ result = yield extension.awaitMessage("onclick");
+ checkClickInfo(result);
+ result = yield extension.awaitMessage("browser.contextMenus.onClicked");
+ checkClickInfo(result);
+
+
+ // Select some text
+ yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* (arg) {
+ let doc = content.document;
+ let range = doc.createRange();
+ let selection = content.getSelection();
+ selection.removeAllRanges();
+ let textNode = doc.getElementById("img1").previousSibling;
+ range.setStart(textNode, 0);
+ range.setEnd(textNode, 100);
+ selection.addRange(range);
+ });
+
+ // Bring up context menu again
+ extensionMenuRoot = yield openExtensionContextMenu();
+
+ // Check some menu items
+ items = extensionMenuRoot.getElementsByAttribute("label", "Without onclick property");
+ is(items.length, 1, "contextMenu item was found (context=page)");
+
+ yield closeExtensionContextMenu(items[0]);
+
+ expectedClickInfo = {
+ menuItemId: "ext-without-onclick",
+ pageUrl: PAGE,
+ };
+
+ result = yield extension.awaitMessage("browser.contextMenus.onClicked");
+ checkClickInfo(result);
+
+ // Bring up context menu again
+ extensionMenuRoot = yield openExtensionContextMenu();
+
+ // Check some menu items
+ items = extensionMenuRoot.getElementsByAttribute("label", "selection is: 'just some text 123456789012345678901234567890...'");
+ is(items.length, 1, "contextMenu item for selection was found (context=selection)");
+ let selectionItem = items[0];
+
+ items = extensionMenuRoot.getElementsByAttribute("label", "selection");
+ is(items.length, 0, "contextMenu item label update worked (context=selection)");
+
+ yield closeExtensionContextMenu(selectionItem);
+
+ expectedClickInfo = {
+ menuItemId: "ext-selection",
+ pageUrl: PAGE,
+ selectionText: "just some text 1234567890123456789012345678901234567890123456789012345678901234567890123456789012",
+ };
+
+ result = yield extension.awaitMessage("onclick");
+ checkClickInfo(result);
+ result = yield extension.awaitMessage("browser.contextMenus.onClicked");
+ checkClickInfo(result);
+
+ let contentAreaContextMenu = yield openContextMenu("#img1");
+ items = contentAreaContextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+ is(items.length, 0, "top level item was not found (after removeAll()");
+ yield closeContextMenu();
+
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
+
+add_task(function* testRemoveAllWithTwoExtensions() {
+ const tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
+ const manifest = {permissions: ["contextMenus"]};
+
+ const first = ExtensionTestUtils.loadExtension({manifest, background() {
+ browser.contextMenus.create({title: "alpha", contexts: ["all"]});
+
+ browser.contextMenus.onClicked.addListener(() => {
+ browser.contextMenus.removeAll();
+ });
+ browser.test.onMessage.addListener(msg => {
+ if (msg == "ping") {
+ browser.test.sendMessage("pong-alpha");
+ return;
+ }
+ browser.contextMenus.create({title: "gamma", contexts: ["all"]});
+ });
+ }});
+
+ const second = ExtensionTestUtils.loadExtension({manifest, background() {
+ browser.contextMenus.create({title: "beta", contexts: ["all"]});
+
+ browser.contextMenus.onClicked.addListener(() => {
+ browser.contextMenus.removeAll();
+ });
+
+ browser.test.onMessage.addListener(() => {
+ browser.test.sendMessage("pong-beta");
+ });
+ }});
+
+ yield first.startup();
+ yield second.startup();
+
+ function* confirmMenuItems(...items) {
+ // Round-trip to extension to make sure that the context menu state has been
+ // updated by the async contextMenus.create / contextMenus.removeAll calls.
+ first.sendMessage("ping");
+ second.sendMessage("ping");
+ yield first.awaitMessage("pong-alpha");
+ yield second.awaitMessage("pong-beta");
+
+ const menu = yield openContextMenu();
+ for (const id of ["alpha", "beta", "gamma"]) {
+ const expected = items.includes(id);
+ const found = menu.getElementsByAttribute("label", id);
+ is(found.length, expected, `menu item ${id} ${expected ? "" : "not "}found`);
+ }
+ // Return the first menu item, we need to click it.
+ return menu.getElementsByAttribute("label", items[0])[0];
+ }
+
+ // Confirm alpha, beta exist; click alpha to remove it.
+ const alpha = yield confirmMenuItems("alpha", "beta");
+ yield closeExtensionContextMenu(alpha);
+
+ // Confirm only beta exists.
+ yield confirmMenuItems("beta");
+ yield closeContextMenu();
+
+ // Create gamma, confirm, click.
+ first.sendMessage("create");
+ const beta = yield confirmMenuItems("beta", "gamma");
+ yield closeExtensionContextMenu(beta);
+
+ // Confirm only gamma is left.
+ yield confirmMenuItems("gamma");
+ yield closeContextMenu();
+
+ yield first.unload();
+ yield second.unload();
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_checkboxes.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_checkboxes.js
new file mode 100644
index 000000000..a3fa9d32c
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_checkboxes.js
@@ -0,0 +1,96 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: function() {
+ // Report onClickData info back.
+ browser.contextMenus.onClicked.addListener(info => {
+ browser.test.sendMessage("contextmenus-click", info);
+ });
+
+ browser.contextMenus.create({
+ title: "Checkbox",
+ type: "checkbox",
+ });
+
+ browser.contextMenus.create({
+ type: "separator",
+ });
+
+ browser.contextMenus.create({
+ title: "Checkbox",
+ type: "checkbox",
+ checked: true,
+ });
+
+ browser.contextMenus.create({
+ title: "Checkbox",
+ type: "checkbox",
+ });
+
+ browser.test.notifyPass("contextmenus-checkboxes");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-checkboxes");
+
+ function confirmCheckboxStates(extensionMenuRoot, expectedStates) {
+ let checkboxItems = extensionMenuRoot.getElementsByAttribute("type", "checkbox");
+
+ is(checkboxItems.length, 3, "there should be 3 checkbox items in the context menu");
+
+ is(checkboxItems[0].hasAttribute("checked"), expectedStates[0], `checkbox item 1 has state (checked=${expectedStates[0]})`);
+ is(checkboxItems[1].hasAttribute("checked"), expectedStates[1], `checkbox item 2 has state (checked=${expectedStates[1]})`);
+ is(checkboxItems[2].hasAttribute("checked"), expectedStates[2], `checkbox item 3 has state (checked=${expectedStates[2]})`);
+
+ return extensionMenuRoot.getElementsByAttribute("type", "checkbox");
+ }
+
+ function confirmOnClickData(onClickData, id, was, checked) {
+ is(onClickData.wasChecked, was, `checkbox item ${id} was ${was ? "" : "not "}checked before the click`);
+ is(onClickData.checked, checked, `checkbox item ${id} is ${checked ? "" : "not "}checked after the click`);
+ }
+
+ let extensionMenuRoot = yield openExtensionContextMenu();
+ let items = confirmCheckboxStates(extensionMenuRoot, [false, true, false]);
+ yield closeExtensionContextMenu(items[0]);
+
+ let result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 1, false, true);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmCheckboxStates(extensionMenuRoot, [true, true, false]);
+ yield closeExtensionContextMenu(items[2]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 3, false, true);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmCheckboxStates(extensionMenuRoot, [true, true, true]);
+ yield closeExtensionContextMenu(items[0]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 1, true, false);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmCheckboxStates(extensionMenuRoot, [false, true, true]);
+ yield closeExtensionContextMenu(items[2]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 3, true, false);
+
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js
new file mode 100644
index 000000000..a3d31bd19
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js
@@ -0,0 +1,76 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ let encodedImageData = "iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAC4klEQVRYhdWXLWzbQBSADQtDAwsHC1tUhUxqfL67lk2tdn+OJg0ODU0rLByqgqINBY6tmlbn7LMTJ5FaFVVBk1G0oUGjG2jT2Y7jxmmcbU/6iJ+f36fz+e5sGP9riCGm9hB37RG+scd4Yo/wsDXCZyIE2xuXsce4bY+wXkAsQtzYmExrfFgvkJkRbkzo1ehoxx5iXcgI/9iYUGt8WH9MqDXEcmNChmEYrRCf2SHWeYgQx3x0tLNRIeKQLTtEFyJEep4NTuhk8BC+yMrwEE3+iozo42d8gK7FAOkMsRiiN8QhW2ttSK5QTfRRV4QoymVeJMvPvDp7gCZigD613MN6yRFA3SWarow9QB9LCfG+NeF9qCtjAKOSQjCqVKhfVsiHEQ+grgx/lRGqUihAc1uL8EFD+KCRO+GrF4J61phcoRoPoEzkYhZYpykh5sMb7kOdIeY+jHKur4QI4Feh4AFX1nVeLxrAvQchGsBz5ls6wa2QdwcvIcE2863bTH79KOvsz/uUYJsp+J0pSzNlDckVqqVGUAF+n6uS7txcOl6wot4JVy70ufDLy4pWLUQVPE81pRI0mGe9oxLMHSeohHvMs/STUNaUK6vDPCvOyxMFDx4achehRDJmHnydnkPww5OFfLxrGIZBFDyYl4LpMzlTQFIP6AQx86w2UeYBccFpJrcKv5L9eGDtUAU6RIELqsB74uynjy/UBRF1gS5BTFxwQT1wTiXoUg9MH7m/3NZRRoi5IJytUbMgzv4Wc832+oQkiKgEehmyMkkpKsFkQV11QsRJL5rJYBLItQgRaUZEmnoZXsomz3vGiWw+I9KMF9SVFOqZEemZekli1jN3U/UOqhHHvC6oWWGElhfSpGdOk6+O9prdwvtLj5BjRsQxdRnot+Zeifpy/2/0stktKTRNLmbk0mwXyl8253fyojj+8rxOHNAhjjm5n0/5OOCGOKBzkrMO0Z75lvSAzKlrF32Z/3z8BqLAn+yMV7VhAAAAAElFTkSuQmCC";
+ let decodedImageData = atob(encodedImageData);
+ const IMAGE_ARRAYBUFFER = Uint8Array.from(decodedImageData, byte => byte.charCodeAt(0)).buffer;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ "icons": {
+ "18": "extension.png",
+ },
+ },
+
+ files: {
+ "extension.png": IMAGE_ARRAYBUFFER,
+ },
+
+ background: function() {
+ let menuitemId = browser.contextMenus.create({
+ title: "child-to-delete",
+ onclick: () => {
+ browser.contextMenus.remove(menuitemId);
+ },
+ });
+
+ browser.contextMenus.create({
+ title: "child",
+ });
+
+ browser.test.onMessage.addListener(() => {
+ browser.test.sendMessage("pong");
+ });
+ browser.test.notifyPass("contextmenus-icons");
+ },
+ });
+
+ let confirmContextMenuIcon = (rootElement) => {
+ let expectedURL = new RegExp(String.raw`^moz-extension://[^/]+/extension\.png$`);
+ let imageUrl = rootElement.getAttribute("image");
+ ok(expectedURL.test(imageUrl), "The context menu should display the extension icon next to the root element");
+ };
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-icons");
+
+ let extensionMenu = yield openExtensionContextMenu();
+
+ let contextMenu = document.getElementById("contentAreaContextMenu");
+ let topLevelMenuItem = contextMenu.getElementsByAttribute("ext-type", "top-level-menu")[0];
+ confirmContextMenuIcon(topLevelMenuItem);
+
+ let childToDelete = extensionMenu.getElementsByAttribute("label", "child-to-delete")[0];
+ yield closeExtensionContextMenu(childToDelete);
+ // Now perform a roundtrip to the extension process to make sure that the
+ // click event has had a chance to fire.
+ extension.sendMessage("ping");
+ yield extension.awaitMessage("pong");
+
+ yield openExtensionContextMenu();
+
+ contextMenu = document.getElementById("contentAreaContextMenu");
+ topLevelMenuItem = contextMenu.getElementsByAttribute("label", "child")[0];
+
+ confirmContextMenuIcon(topLevelMenuItem);
+ yield closeContextMenu();
+
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_onclick.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_onclick.js
new file mode 100644
index 000000000..96453863d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_onclick.js
@@ -0,0 +1,196 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+// Loaded both as a background script and a tab page.
+function testScript() {
+ let page = location.pathname.includes("tab.html") ? "tab" : "background";
+ let clickCounts = {
+ old: 0,
+ new: 0,
+ };
+ browser.contextMenus.onClicked.addListener(() => {
+ // Async to give other onclick handlers a chance to fire.
+ setTimeout(() => {
+ browser.test.sendMessage("onClicked-fired", page);
+ });
+ });
+ browser.test.onMessage.addListener((toPage, msg) => {
+ if (toPage !== page) {
+ return;
+ }
+ browser.test.log(`Received ${msg} for ${toPage}`);
+ if (msg == "get-click-counts") {
+ browser.test.sendMessage("click-counts", clickCounts);
+ } else if (msg == "clear-click-counts") {
+ clickCounts.old = clickCounts.new = 0;
+ browser.test.sendMessage("next");
+ } else if (msg == "create-with-onclick") {
+ browser.contextMenus.create({
+ id: "iden",
+ title: "tifier",
+ onclick() {
+ ++clickCounts.old;
+ browser.test.log(`onclick fired for original onclick property in ${page}`);
+ },
+ }, () => browser.test.sendMessage("next"));
+ } else if (msg == "create-without-onclick") {
+ browser.contextMenus.create({
+ id: "iden",
+ title: "tifier",
+ }, () => browser.test.sendMessage("next"));
+ } else if (msg == "update-without-onclick") {
+ browser.contextMenus.update("iden", {
+ enabled: true, // Already enabled, so this does nothing.
+ }, () => browser.test.sendMessage("next"));
+ } else if (msg == "update-with-onclick") {
+ browser.contextMenus.update("iden", {
+ onclick() {
+ ++clickCounts.new;
+ browser.test.log(`onclick fired for updated onclick property in ${page}`);
+ },
+ }, () => browser.test.sendMessage("next"));
+ } else if (msg == "remove") {
+ browser.contextMenus.remove("iden", () => browser.test.sendMessage("next"));
+ } else if (msg == "removeAll") {
+ browser.contextMenus.removeAll(() => browser.test.sendMessage("next"));
+ }
+ });
+
+ if (page == "background") {
+ browser.test.log("Opening tab.html");
+ browser.tabs.create({
+ url: "tab.html",
+ active: false, // To not interfere with the context menu tests.
+ });
+ } else {
+ // Sanity check - the pages must be in the same process.
+ let pages = browser.extension.getViews();
+ browser.test.assertTrue(pages.includes(window),
+ "Expected this tab to be an extension view");
+ pages = pages.filter(w => w !== window);
+ browser.test.assertEq(pages[0], browser.extension.getBackgroundPage(),
+ "Expected the other page to be a background page");
+ browser.test.sendMessage("tab.html ready");
+ }
+}
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+ background: testScript,
+ files: {
+ "tab.html": `<!DOCTYPE html><meta charset="utf-8"><script src="tab.js"></script>`,
+ "tab.js": testScript,
+ },
+ });
+ yield extension.startup();
+ yield extension.awaitMessage("tab.html ready");
+
+ function* clickContextMenu() {
+ // Using openContextMenu instead of openExtensionContextMenu because the
+ // test extension has only one context menu item.
+ let extensionMenuRoot = yield openContextMenu();
+ let items = extensionMenuRoot.getElementsByAttribute("label", "tifier");
+ is(items.length, 1, "Expected one context menu item");
+ yield closeExtensionContextMenu(items[0]);
+ // One of them is "tab", the other is "background".
+ info(`onClicked from: ${yield extension.awaitMessage("onClicked-fired")}`);
+ info(`onClicked from: ${yield extension.awaitMessage("onClicked-fired")}`);
+ }
+
+ function* getCounts(page) {
+ extension.sendMessage(page, "get-click-counts");
+ return yield extension.awaitMessage("click-counts");
+ }
+ function* resetCounts() {
+ extension.sendMessage("tab", "clear-click-counts");
+ extension.sendMessage("background", "clear-click-counts");
+ yield extension.awaitMessage("next");
+ yield extension.awaitMessage("next");
+ }
+
+ // During this test, at most one "onclick" attribute is expected at any time.
+ for (let pageOne of ["background", "tab"]) {
+ for (let pageTwo of ["background", "tab"]) {
+ info(`Testing with menu created by ${pageOne} and updated by ${pageTwo}`);
+ extension.sendMessage(pageOne, "create-with-onclick");
+ yield extension.awaitMessage("next");
+
+ // Test that update without onclick attribute does not clear the existing
+ // onclick handler.
+ extension.sendMessage(pageTwo, "update-without-onclick");
+ yield extension.awaitMessage("next");
+ yield clickContextMenu();
+ let clickCounts = yield getCounts(pageOne);
+ is(clickCounts.old, 1, `Original onclick should still be present in ${pageOne}`);
+ is(clickCounts.new, 0, `Not expecting any new handlers in ${pageOne}`);
+ if (pageOne !== pageTwo) {
+ clickCounts = yield getCounts(pageTwo);
+ is(clickCounts.old, 0, `Not expecting any handlers in ${pageTwo}`);
+ is(clickCounts.new, 0, `Not expecting any new handlers in ${pageTwo}`);
+ }
+ yield resetCounts();
+
+ // Test that update with onclick handler in a different page clears the
+ // existing handler and activates the new onclick handler.
+ extension.sendMessage(pageTwo, "update-with-onclick");
+ yield extension.awaitMessage("next");
+ yield clickContextMenu();
+ clickCounts = yield getCounts(pageOne);
+ is(clickCounts.old, 0, `Original onclick should be gone from ${pageOne}`);
+ if (pageOne !== pageTwo) {
+ is(clickCounts.new, 0, `Still not expecting new handlers in ${pageOne}`);
+ }
+ clickCounts = yield getCounts(pageTwo);
+ if (pageOne !== pageTwo) {
+ is(clickCounts.old, 0, `Not expecting an old onclick in ${pageTwo}`);
+ }
+ is(clickCounts.new, 1, `New onclick should be triggered in ${pageTwo}`);
+ yield resetCounts();
+
+ // Test that updating the handler (different again from the last `update`
+ // call, but the same as the `create` call) clears the existing handler
+ // and activates the new onclick handler.
+ extension.sendMessage(pageOne, "update-with-onclick");
+ yield extension.awaitMessage("next");
+ yield clickContextMenu();
+ clickCounts = yield getCounts(pageOne);
+ is(clickCounts.new, 1, `onclick should be triggered in ${pageOne}`);
+ if (pageOne !== pageTwo) {
+ clickCounts = yield getCounts(pageTwo);
+ is(clickCounts.new, 0, `onclick should be gone from ${pageTwo}`);
+ }
+ yield resetCounts();
+
+ // Test that removing the context menu and recreating it with the same ID
+ // (in a different context) does not leave behind any onclick handlers.
+ extension.sendMessage(pageTwo, "remove");
+ yield extension.awaitMessage("next");
+ extension.sendMessage(pageTwo, "create-without-onclick");
+ yield extension.awaitMessage("next");
+ yield clickContextMenu();
+ clickCounts = yield getCounts(pageOne);
+ is(clickCounts.new, 0, `Did not expect any click handlers in ${pageOne}`);
+ if (pageOne !== pageTwo) {
+ clickCounts = yield getCounts(pageTwo);
+ is(clickCounts.new, 0, `Did not expect any click handlers in ${pageTwo}`);
+ }
+ yield resetCounts();
+
+ // Remove context menu for the next iteration of the test. And just to get
+ // more coverage, let's use removeAll instead of remove.
+ extension.sendMessage(pageOne, "removeAll");
+ yield extension.awaitMessage("next");
+ }
+ }
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_radioGroups.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_radioGroups.js
new file mode 100644
index 000000000..3c5fa584b
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_radioGroups.js
@@ -0,0 +1,100 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: function() {
+ // Report onClickData info back.
+ browser.contextMenus.onClicked.addListener(info => {
+ browser.test.sendMessage("contextmenus-click", info);
+ });
+
+ browser.contextMenus.create({
+ title: "radio-group-1",
+ type: "radio",
+ checked: true,
+ });
+
+ browser.contextMenus.create({
+ type: "separator",
+ });
+
+ browser.contextMenus.create({
+ title: "radio-group-2",
+ type: "radio",
+ });
+
+ browser.contextMenus.create({
+ title: "radio-group-2",
+ type: "radio",
+ });
+
+ browser.test.notifyPass("contextmenus-radio-groups");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-radio-groups");
+
+ function confirmRadioGroupStates(extensionMenuRoot, expectedStates) {
+ let radioItems = extensionMenuRoot.getElementsByAttribute("type", "radio");
+ let radioGroup1 = extensionMenuRoot.getElementsByAttribute("label", "radio-group-1");
+ let radioGroup2 = extensionMenuRoot.getElementsByAttribute("label", "radio-group-2");
+
+ is(radioItems.length, 3, "there should be 3 radio items in the context menu");
+ is(radioGroup1.length, 1, "the first radio group should only have 1 radio item");
+ is(radioGroup2.length, 2, "the second radio group should only have 2 radio items");
+
+ is(radioGroup1[0].hasAttribute("checked"), expectedStates[0], `radio item 1 has state (checked=${expectedStates[0]})`);
+ is(radioGroup2[0].hasAttribute("checked"), expectedStates[1], `radio item 2 has state (checked=${expectedStates[1]})`);
+ is(radioGroup2[1].hasAttribute("checked"), expectedStates[2], `radio item 3 has state (checked=${expectedStates[2]})`);
+
+ return extensionMenuRoot.getElementsByAttribute("type", "radio");
+ }
+
+ function confirmOnClickData(onClickData, id, was, checked) {
+ is(onClickData.wasChecked, was, `radio item ${id} was ${was ? "" : "not "}checked before the click`);
+ is(onClickData.checked, checked, `radio item ${id} is ${checked ? "" : "not "}checked after the click`);
+ }
+
+ let extensionMenuRoot = yield openExtensionContextMenu();
+ let items = confirmRadioGroupStates(extensionMenuRoot, [true, false, false]);
+ yield closeExtensionContextMenu(items[1]);
+
+ let result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 2, false, true);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmRadioGroupStates(extensionMenuRoot, [true, true, false]);
+ yield closeExtensionContextMenu(items[2]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 3, false, true);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmRadioGroupStates(extensionMenuRoot, [true, false, true]);
+ yield closeExtensionContextMenu(items[0]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 1, true, true);
+
+ extensionMenuRoot = yield openExtensionContextMenu();
+ items = confirmRadioGroupStates(extensionMenuRoot, [true, false, true]);
+ yield closeExtensionContextMenu(items[0]);
+
+ result = yield extension.awaitMessage("contextmenus-click");
+ confirmOnClickData(result, 1, true, true);
+
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_uninstall.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_uninstall.js
new file mode 100644
index 000000000..fdf06d656
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_uninstall.js
@@ -0,0 +1,84 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ // Install an extension.
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: function() {
+ browser.contextMenus.create({title: "a"});
+ browser.contextMenus.create({title: "b"});
+ browser.test.notifyPass("contextmenus-icons");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-icons");
+
+ // Open the context menu.
+ let contextMenu = yield openContextMenu("#img1");
+
+ // Confirm that the extension menu item exists.
+ let topLevelExtensionMenuItems = contextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+ is(topLevelExtensionMenuItems.length, 1, "the top level extension menu item exists");
+
+ yield closeContextMenu();
+
+ // Uninstall the extension.
+ yield extension.unload();
+
+ // Open the context menu.
+ contextMenu = yield openContextMenu("#img1");
+
+ // Confirm that the extension menu item has been removed.
+ topLevelExtensionMenuItems = contextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+ is(topLevelExtensionMenuItems.length, 0, "no top level extension menu items should exist");
+
+ yield closeContextMenu();
+
+ // Install a new extension.
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+ background: function() {
+ browser.contextMenus.create({title: "c"});
+ browser.contextMenus.create({title: "d"});
+ browser.test.notifyPass("contextmenus-uninstall-second-extension");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-uninstall-second-extension");
+
+ // Open the context menu.
+ contextMenu = yield openContextMenu("#img1");
+
+ // Confirm that only the new extension menu item is in the context menu.
+ topLevelExtensionMenuItems = contextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+ is(topLevelExtensionMenuItems.length, 1, "only one top level extension menu item should exist");
+
+ // Close the context menu.
+ yield closeContextMenu();
+
+ // Uninstall the extension.
+ yield extension.unload();
+
+ // Open the context menu.
+ contextMenu = yield openContextMenu("#img1");
+
+ // Confirm that no extension menu items exist.
+ topLevelExtensionMenuItems = contextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+ is(topLevelExtensionMenuItems.length, 0, "no top level extension menu items should exist");
+
+ yield closeContextMenu();
+
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_contextMenus_urlPatterns.js b/browser/components/extensions/test/browser/browser_ext_contextMenus_urlPatterns.js
new file mode 100644
index 000000000..7849b8778
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_urlPatterns.js
@@ -0,0 +1,254 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser,
+ "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["contextMenus"],
+ },
+
+ background: function() {
+ // Test menu items using targetUrlPatterns.
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternMatches-contextAll",
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png", "*://*/*some-link"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternMatches-contextImage",
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternMatches-contextLink",
+ targetUrlPatterns: ["*://*/*some-link"],
+ contexts: ["link"],
+ });
+
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternDoesNotMatch-contextAll",
+ targetUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternDoesNotMatch-contextImage",
+ targetUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "targetUrlPatterns-patternDoesNotMatch-contextLink",
+ targetUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["link"],
+ });
+
+ // Test menu items using documentUrlPatterns.
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-contextAll",
+ documentUrlPatterns: ["*://*/*context.html"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-contextImage",
+ documentUrlPatterns: ["*://*/*context.html", "http://*/url-that-does-not-match"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-contextLink",
+ documentUrlPatterns: ["*://*/*context.html", "*://*/does-not-match"],
+ contexts: ["link"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-contextAll",
+ documentUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-contextImage",
+ documentUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-contextLink",
+ documentUrlPatterns: ["*://*/does-not-match"],
+ contexts: ["link"],
+ });
+
+ // Test menu items using both targetUrlPatterns and documentUrlPatterns.
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextAll",
+ documentUrlPatterns: ["*://*/*context.html"],
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextAll",
+ documentUrlPatterns: ["*://does-not-match"],
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextAll",
+ documentUrlPatterns: ["*://*/*context.html"],
+ targetUrlPatterns: ["*://does-not-match"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextAll",
+ documentUrlPatterns: ["*://does-not-match"],
+ targetUrlPatterns: ["*://does-not-match"],
+ contexts: ["all"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextImage",
+ documentUrlPatterns: ["*://*/*context.html"],
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextImage",
+ documentUrlPatterns: ["*://does-not-match"],
+ targetUrlPatterns: ["*://*/*ctxmenu-image.png"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextImage",
+ documentUrlPatterns: ["*://*/*context.html"],
+ targetUrlPatterns: ["*://does-not-match"],
+ contexts: ["image"],
+ });
+
+ browser.contextMenus.create({
+ title: "documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextImage",
+ documentUrlPatterns: ["*://does-not-match"],
+ targetUrlPatterns: ["*://does-not-match"],
+ contexts: ["image"],
+ });
+
+ browser.test.notifyPass("contextmenus-urlPatterns");
+ },
+ });
+
+ function* confirmContextMenuItems(menu, expected) {
+ for (let [label, shouldShow] of expected) {
+ let items = menu.getElementsByAttribute("label", label);
+ if (shouldShow) {
+ is(items.length, 1, `The menu item for label ${label} was correctly shown`);
+ } else {
+ is(items.length, 0, `The menu item for label ${label} was correctly not shown`);
+ }
+ }
+ }
+
+ yield extension.startup();
+ yield extension.awaitFinish("contextmenus-urlPatterns");
+
+ let extensionContextMenu = yield openExtensionContextMenu("#img1");
+ let expected = [
+ ["targetUrlPatterns-patternMatches-contextAll", true],
+ ["targetUrlPatterns-patternMatches-contextImage", true],
+ ["targetUrlPatterns-patternMatches-contextLink", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-contextAll", true],
+ ["documentUrlPatterns-patternMatches-contextImage", true],
+ ["documentUrlPatterns-patternMatches-contextLink", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextAll", true],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextAll", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextImage", true],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextImage", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ];
+ yield confirmContextMenuItems(extensionContextMenu, expected);
+ yield closeContextMenu();
+
+ let contextMenu = yield openContextMenu("body");
+ expected = [
+ ["targetUrlPatterns-patternMatches-contextAll", false],
+ ["targetUrlPatterns-patternMatches-contextImage", false],
+ ["targetUrlPatterns-patternMatches-contextLink", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-contextAll", true],
+ ["documentUrlPatterns-patternMatches-contextImage", false],
+ ["documentUrlPatterns-patternMatches-contextLink", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextAll", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternMatches-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternMatches-contextImage", false],
+ ["documentUrlPatterns-patternMatches-targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ];
+ yield confirmContextMenuItems(contextMenu, expected);
+ yield closeContextMenu();
+
+ contextMenu = yield openContextMenu("#link1");
+ expected = [
+ ["targetUrlPatterns-patternMatches-contextAll", true],
+ ["targetUrlPatterns-patternMatches-contextImage", false],
+ ["targetUrlPatterns-patternMatches-contextLink", true],
+ ["targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-contextAll", true],
+ ["documentUrlPatterns-patternMatches-contextImage", false],
+ ["documentUrlPatterns-patternMatches-contextLink", true],
+ ["documentUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ];
+ yield confirmContextMenuItems(contextMenu, expected);
+ yield closeContextMenu();
+
+ contextMenu = yield openContextMenu("#img-wrapped-in-link");
+ expected = [
+ ["targetUrlPatterns-patternMatches-contextAll", true],
+ ["targetUrlPatterns-patternMatches-contextImage", true],
+ ["targetUrlPatterns-patternMatches-contextLink", true],
+ ["targetUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["targetUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ["documentUrlPatterns-patternMatches-contextAll", true],
+ ["documentUrlPatterns-patternMatches-contextImage", true],
+ ["documentUrlPatterns-patternMatches-contextLink", true],
+ ["documentUrlPatterns-patternDoesNotMatch-contextAll", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextImage", false],
+ ["documentUrlPatterns-patternDoesNotMatch-contextLink", false],
+ ];
+ yield confirmContextMenuItems(contextMenu, expected);
+ yield closeContextMenu();
+
+ yield extension.unload();
+ yield BrowserTestUtils.removeTab(tab1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_currentWindow.js b/browser/components/extensions/test/browser/browser_ext_currentWindow.js
new file mode 100644
index 000000000..11660bf4d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_currentWindow.js
@@ -0,0 +1,149 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function genericChecker() {
+ let kind = "background";
+ let path = window.location.pathname;
+ if (path.includes("/popup.html")) {
+ kind = "popup";
+ } else if (path.includes("/page.html")) {
+ kind = "page";
+ }
+
+ browser.test.onMessage.addListener((msg, ...args) => {
+ if (msg == kind + "-check-current1") {
+ browser.tabs.query({
+ currentWindow: true,
+ }, function(tabs) {
+ browser.test.sendMessage("result", tabs[0].windowId);
+ });
+ } else if (msg == kind + "-check-current2") {
+ browser.tabs.query({
+ windowId: browser.windows.WINDOW_ID_CURRENT,
+ }, function(tabs) {
+ browser.test.sendMessage("result", tabs[0].windowId);
+ });
+ } else if (msg == kind + "-check-current3") {
+ browser.windows.getCurrent(function(window) {
+ browser.test.sendMessage("result", window.id);
+ });
+ } else if (msg == kind + "-open-page") {
+ browser.tabs.create({windowId: args[0], url: browser.runtime.getURL("page.html")});
+ } else if (msg == kind + "-close-page") {
+ browser.tabs.query({
+ windowId: args[0],
+ }, tabs => {
+ let tab = tabs.find(tab => tab.url.includes("/page.html"));
+ browser.tabs.remove(tab.id, () => {
+ browser.test.sendMessage("closed");
+ });
+ });
+ }
+ });
+ browser.test.sendMessage(kind + "-ready");
+}
+
+add_task(function* () {
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+ let win2 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ yield focusWindow(win2);
+
+ yield BrowserTestUtils.loadURI(win1.gBrowser.selectedBrowser, "about:robots");
+ yield BrowserTestUtils.browserLoaded(win1.gBrowser.selectedBrowser);
+
+ yield BrowserTestUtils.loadURI(win2.gBrowser.selectedBrowser, "about:config");
+ yield BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "browser_action": {
+ "default_popup": "popup.html",
+ },
+ },
+
+ files: {
+ "page.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="page.js"></script>
+ </body></html>
+ `,
+
+ "page.js": genericChecker,
+
+ "popup.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="popup.js"></script>
+ </body></html>
+ `,
+
+ "popup.js": genericChecker,
+ },
+
+ background: genericChecker,
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("background-ready")]);
+
+ let {Management: {global: {WindowManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let winId1 = WindowManager.getId(win1);
+ let winId2 = WindowManager.getId(win2);
+
+ function* checkWindow(kind, winId, name) {
+ extension.sendMessage(kind + "-check-current1");
+ is((yield extension.awaitMessage("result")), winId, `${name} is on top (check 1) [${kind}]`);
+ extension.sendMessage(kind + "-check-current2");
+ is((yield extension.awaitMessage("result")), winId, `${name} is on top (check 2) [${kind}]`);
+ extension.sendMessage(kind + "-check-current3");
+ is((yield extension.awaitMessage("result")), winId, `${name} is on top (check 3) [${kind}]`);
+ }
+
+ yield focusWindow(win1);
+ yield checkWindow("background", winId1, "win1");
+ yield focusWindow(win2);
+ yield checkWindow("background", winId2, "win2");
+
+ function* triggerPopup(win, callback) {
+ yield clickBrowserAction(extension, win);
+ yield awaitExtensionPanel(extension, win);
+
+ yield extension.awaitMessage("popup-ready");
+
+ yield callback();
+
+ closeBrowserAction(extension, win);
+ }
+
+ // Set focus to some other window.
+ yield focusWindow(window);
+
+ yield triggerPopup(win1, function* () {
+ yield checkWindow("popup", winId1, "win1");
+ });
+
+ yield triggerPopup(win2, function* () {
+ yield checkWindow("popup", winId2, "win2");
+ });
+
+ function* triggerPage(winId, name) {
+ extension.sendMessage("background-open-page", winId);
+ yield extension.awaitMessage("page-ready");
+ yield checkWindow("page", winId, name);
+ extension.sendMessage("background-close-page", winId);
+ yield extension.awaitMessage("closed");
+ }
+
+ yield triggerPage(winId1, "win1");
+ yield triggerPage(winId2, "win2");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(win1);
+ yield BrowserTestUtils.closeWindow(win2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_getViews.js b/browser/components/extensions/test/browser/browser_ext_getViews.js
new file mode 100644
index 000000000..684e19ac5
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_getViews.js
@@ -0,0 +1,198 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function genericChecker() {
+ let kind = "background";
+ let path = window.location.pathname;
+ if (path.indexOf("popup") != -1) {
+ kind = "popup";
+ } else if (path.indexOf("tab") != -1) {
+ kind = "tab";
+ }
+ window.kind = kind;
+
+ browser.test.onMessage.addListener((msg, ...args) => {
+ if (msg == kind + "-check-views") {
+ let windowId = args[0];
+ let counts = {
+ "background": 0,
+ "tab": 0,
+ "popup": 0,
+ "kind": 0,
+ "window": 0,
+ };
+ if (Number.isInteger(windowId)) {
+ counts.window = browser.extension.getViews({windowId: windowId}).length;
+ }
+ if (kind !== "background") {
+ counts.kind = browser.extension.getViews({type: kind}).length;
+ }
+ let views = browser.extension.getViews();
+ let background;
+ for (let i = 0; i < views.length; i++) {
+ let view = views[i];
+ browser.test.assertTrue(view.kind in counts, "view type is valid");
+ counts[view.kind]++;
+ if (view.kind == "background") {
+ browser.test.assertTrue(view === browser.extension.getBackgroundPage(),
+ "background page is correct");
+ background = view;
+ }
+ }
+ if (background) {
+ browser.runtime.getBackgroundPage().then(view => {
+ browser.test.assertEq(background, view, "runtime.getBackgroundPage() is correct");
+ browser.test.sendMessage("counts", counts);
+ });
+ } else {
+ browser.test.sendMessage("counts", counts);
+ }
+ } else if (msg == kind + "-open-tab") {
+ browser.tabs.create({windowId: args[0], url: browser.runtime.getURL("tab.html")});
+ } else if (msg == kind + "-close-tab") {
+ browser.tabs.query({
+ windowId: args[0],
+ }, tabs => {
+ let tab = tabs.find(tab => tab.url.indexOf("tab.html") != -1);
+ browser.tabs.remove(tab.id, () => {
+ browser.test.sendMessage("closed");
+ });
+ });
+ }
+ });
+ browser.test.sendMessage(kind + "-ready");
+}
+
+add_task(function* () {
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+ let win2 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "browser_action": {
+ "default_popup": "popup.html",
+ },
+ },
+
+ files: {
+ "tab.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="tab.js"></script>
+ </body></html>
+ `,
+
+ "tab.js": genericChecker,
+
+ "popup.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="popup.js"></script>
+ </body></html>
+ `,
+
+ "popup.js": genericChecker,
+ },
+
+ background: genericChecker,
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("background-ready")]);
+
+ info("started");
+
+ let {Management: {global: {WindowManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let winId1 = WindowManager.getId(win1);
+ let winId2 = WindowManager.getId(win2);
+
+ function* openTab(winId) {
+ extension.sendMessage("background-open-tab", winId);
+ yield extension.awaitMessage("tab-ready");
+ }
+
+ function* checkViews(kind, tabCount, popupCount, kindCount, windowId = undefined, windowCount = 0) {
+ extension.sendMessage(kind + "-check-views", windowId);
+ let counts = yield extension.awaitMessage("counts");
+ is(counts.background, 1, "background count correct");
+ is(counts.tab, tabCount, "tab count correct");
+ is(counts.popup, popupCount, "popup count correct");
+ is(counts.kind, kindCount, "count for type correct");
+ is(counts.window, windowCount, "count for window correct");
+ }
+
+ yield checkViews("background", 0, 0, 0);
+
+ yield openTab(winId1);
+
+ yield checkViews("background", 1, 0, 0, winId1, 1);
+ yield checkViews("tab", 1, 0, 1);
+
+ yield openTab(winId2);
+
+ yield checkViews("background", 2, 0, 0, winId2, 1);
+
+ function* triggerPopup(win, callback) {
+ yield clickBrowserAction(extension, win);
+ yield awaitExtensionPanel(extension, win);
+
+ yield extension.awaitMessage("popup-ready");
+
+ yield callback();
+
+ closeBrowserAction(extension, win);
+ }
+
+ // The popup occasionally closes prematurely if we open it immediately here.
+ // I'm not sure what causes it to close (it's something internal, and seems to
+ // be focus-related, but it's not caused by JS calling hidePopup), but even a
+ // short timeout seems to consistently fix it.
+ yield new Promise(resolve => win1.setTimeout(resolve, 10));
+
+ yield triggerPopup(win1, function* () {
+ yield checkViews("background", 2, 1, 0, winId1, 2);
+ yield checkViews("popup", 2, 1, 1);
+ });
+
+ yield triggerPopup(win2, function* () {
+ yield checkViews("background", 2, 1, 0, winId2, 2);
+ yield checkViews("popup", 2, 1, 1);
+ });
+
+ info("checking counts after popups");
+
+ yield checkViews("background", 2, 0, 0, winId1, 1);
+
+ info("closing one tab");
+
+ extension.sendMessage("background-close-tab", winId1);
+ yield extension.awaitMessage("closed");
+
+ info("one tab closed, one remains");
+
+ yield checkViews("background", 1, 0, 0);
+
+ info("opening win1 popup");
+
+ yield triggerPopup(win1, function* () {
+ yield checkViews("background", 1, 1, 0);
+ yield checkViews("tab", 1, 1, 1);
+ yield checkViews("popup", 1, 1, 1);
+ });
+
+ info("opening win2 popup");
+
+ yield triggerPopup(win2, function* () {
+ yield checkViews("background", 1, 1, 0);
+ yield checkViews("tab", 1, 1, 1);
+ yield checkViews("popup", 1, 1, 1);
+ });
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(win1);
+ yield BrowserTestUtils.closeWindow(win2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_incognito_popup.js b/browser/components/extensions/test/browser/browser_ext_incognito_popup.js
new file mode 100644
index 000000000..174b2179d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_incognito_popup.js
@@ -0,0 +1,108 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testIncognitoPopup() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ "browser_action": {
+ "default_popup": "popup.html",
+ },
+ "page_action": {
+ "default_popup": "popup.html",
+ },
+ },
+
+ background: async function() {
+ let resolveMessage;
+ browser.runtime.onMessage.addListener(msg => {
+ if (resolveMessage && msg.message == "popup-details") {
+ resolveMessage(msg);
+ }
+ });
+
+ let awaitPopup = windowId => {
+ return new Promise(resolve => {
+ resolveMessage = resolve;
+ }).then(msg => {
+ browser.test.assertEq(windowId, msg.windowId, "Got popup message from correct window");
+ return msg;
+ });
+ };
+
+ let testWindow = async window => {
+ let [tab] = await browser.tabs.query({active: true, windowId: window.id});
+
+ await browser.pageAction.show(tab.id);
+ browser.test.sendMessage("click-pageAction");
+
+ let msg = await awaitPopup(window.id);
+ browser.test.assertEq(window.incognito, msg.incognito, "Correct incognito status in pageAction popup");
+
+ browser.test.sendMessage("click-browserAction");
+
+ msg = await awaitPopup(window.id);
+ browser.test.assertEq(window.incognito, msg.incognito, "Correct incognito status in browserAction popup");
+ };
+
+ const URL = "http://example.com/incognito";
+ let windowReady = new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed, tab) {
+ if (changed.status == "complete" && tab.url == URL) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+
+ try {
+ {
+ let window = await browser.windows.getCurrent();
+
+ await testWindow(window);
+ }
+
+ {
+ let window = await browser.windows.create({incognito: true, url: URL});
+ await windowReady;
+
+ await testWindow(window);
+
+ await browser.windows.remove(window.id);
+ }
+
+ browser.test.notifyPass("incognito");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("incognito");
+ }
+ },
+
+ files: {
+ "popup.html": '<html><head><meta charset="utf-8"><script src="popup.js"></script></head></html>',
+
+ "popup.js": async function() {
+ let win = await browser.windows.getCurrent();
+ browser.runtime.sendMessage({
+ message: "popup-details",
+ windowId: win.id,
+ incognito: browser.extension.inIncognitoContext,
+ });
+ window.close();
+ },
+ },
+ });
+
+ extension.onMessage("click-browserAction", () => {
+ clickBrowserAction(extension, Services.wm.getMostRecentWindow("navigator:browser"));
+ });
+
+ extension.onMessage("click-pageAction", () => {
+ clickPageAction(extension, Services.wm.getMostRecentWindow("navigator:browser"));
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("incognito");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_incognito_views.js b/browser/components/extensions/test/browser/browser_ext_incognito_views.js
new file mode 100644
index 000000000..4865b2d4f
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_incognito_views.js
@@ -0,0 +1,121 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testIncognitoViews() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ "browser_action": {
+ "default_popup": "popup.html",
+ },
+ },
+
+ background: async function() {
+ window.isBackgroundPage = true;
+
+ let resolveMessage;
+ browser.runtime.onMessage.addListener(msg => {
+ if (resolveMessage && msg.message == "popup-details") {
+ resolveMessage(msg);
+ }
+ });
+
+ let awaitPopup = windowId => {
+ return new Promise(resolve => {
+ resolveMessage = resolve;
+ }).then(msg => {
+ browser.test.assertEq(windowId, msg.windowId, "Got popup message from correct window");
+ return msg;
+ });
+ };
+
+ let testWindow = async window => {
+ browser.test.sendMessage("click-browserAction");
+
+ let msg = await awaitPopup(window.id);
+ browser.test.assertEq(window.incognito, msg.incognito, "Correct incognito status in browserAction popup");
+ };
+
+ const URL = "http://example.com/incognito";
+ let windowReady = new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed, tab) {
+ if (changed.status == "complete" && tab.url == URL) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+
+ try {
+ {
+ let window = await browser.windows.getCurrent();
+
+ await testWindow(window);
+ }
+
+ {
+ let window = await browser.windows.create({incognito: true, url: URL});
+ await windowReady;
+
+ await testWindow(window);
+
+ await browser.windows.remove(window.id);
+ }
+
+ browser.test.notifyPass("incognito-views");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("incognito-views");
+ }
+ },
+
+ files: {
+ "popup.html": '<html><head><meta charset="utf-8"><script src="popup.js"></script></head></html>',
+
+ "popup.js": async function() {
+ let views = browser.extension.getViews();
+
+ if (browser.extension.inIncognitoContext) {
+ let bgPage = browser.extension.getBackgroundPage();
+ browser.test.assertEq(null, bgPage, "Should not be able to access background page in incognito context");
+
+ bgPage = await browser.runtime.getBackgroundPage();
+ browser.test.assertEq(null, bgPage, "Should not be able to access background page in incognito context");
+
+ browser.test.assertEq(1, views.length, "Should only see one view in incognito popup");
+ browser.test.assertEq(window, views[0], "This window should be the only view");
+ } else {
+ let bgPage = browser.extension.getBackgroundPage();
+ browser.test.assertEq(true, bgPage.isBackgroundPage,
+ "Should be able to access background page in non-incognito context");
+
+ bgPage = await browser.runtime.getBackgroundPage();
+ browser.test.assertEq(true, bgPage.isBackgroundPage,
+ "Should be able to access background page in non-incognito context");
+
+ browser.test.assertEq(2, views.length, "Should only two views in non-incognito popup");
+ browser.test.assertEq(bgPage, views[0], "The background page should be the first view");
+ browser.test.assertEq(window, views[1], "This window should be the second view");
+ }
+
+ let win = await browser.windows.getCurrent();
+ browser.runtime.sendMessage({
+ message: "popup-details",
+ windowId: win.id,
+ incognito: browser.extension.inIncognitoContext,
+ });
+
+ window.close();
+ },
+ },
+ });
+
+ extension.onMessage("click-browserAction", () => {
+ clickBrowserAction(extension, Services.wm.getMostRecentWindow("navigator:browser"));
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("incognito-views");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_lastError.js b/browser/components/extensions/test/browser/browser_ext_lastError.js
new file mode 100644
index 000000000..499e709aa
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_lastError.js
@@ -0,0 +1,55 @@
+"use strict";
+
+function* sendMessage(options) {
+ function background(options) {
+ browser.runtime.sendMessage(result => {
+ browser.test.assertEq(undefined, result, "Argument value");
+ if (options.checkLastError) {
+ let lastError = browser[options.checkLastError].lastError;
+ browser.test.assertEq("runtime.sendMessage's message argument is missing",
+ lastError && lastError.message,
+ "lastError value");
+ }
+ browser.test.sendMessage("done");
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: `(${background})(${JSON.stringify(options)})`,
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("done");
+
+ yield extension.unload();
+}
+
+add_task(function* testLastError() {
+ // Not necessary in browser-chrome tests, but monitorConsole gripes
+ // if we don't call it.
+ SimpleTest.waitForExplicitFinish();
+
+ // Check that we have no unexpected console messages when lastError is
+ // checked.
+ for (let api of ["extension", "runtime"]) {
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{message: /message argument is missing/, forbid: true}]);
+ });
+
+ yield sendMessage({checkLastError: api});
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+ }
+
+ // Check that we do have a console message when lastError is not checked.
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{message: /Unchecked lastError value: Error: runtime.sendMessage's message argument is missing/}]);
+ });
+
+ yield sendMessage({});
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_legacy_extension_context_contentscript.js b/browser/components/extensions/test/browser/browser_ext_legacy_extension_context_contentscript.js
new file mode 100644
index 000000000..01557a745
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_legacy_extension_context_contentscript.js
@@ -0,0 +1,173 @@
+"use strict";
+
+const {
+ LegacyExtensionContext,
+} = Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm", {});
+
+function promiseAddonStartup(extension) {
+ const {Management} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ return new Promise((resolve) => {
+ let listener = (evt, extensionInstance) => {
+ Management.off("startup", listener);
+ resolve(extensionInstance);
+ };
+ Management.on("startup", listener);
+ });
+}
+
+/**
+ * This test case ensures that the LegacyExtensionContext can receive a connection
+ * from a content script and that the received port contains the expected sender
+ * tab info.
+ */
+add_task(function* test_legacy_extension_context_contentscript_connection() {
+ function backgroundScript() {
+ // Extract the assigned uuid from the background page url and send it
+ // in a test message.
+ let uuid = window.location.hostname;
+
+ browser.test.onMessage.addListener(async msg => {
+ if (msg == "open-test-tab") {
+ let tab = await browser.tabs.create({url: "http://example.com/"});
+ browser.test.sendMessage("get-expected-sender-info",
+ {uuid, tab});
+ } else if (msg == "close-current-tab") {
+ try {
+ let [tab] = await browser.tabs.query({active: true});
+ await browser.tabs.remove(tab.id);
+ browser.test.sendMessage("current-tab-closed", true);
+ } catch (e) {
+ browser.test.sendMessage("current-tab-closed", false);
+ }
+ }
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ function contentScript() {
+ browser.runtime.sendMessage("webextension -> legacy_extension message", (reply) => {
+ browser.test.assertEq("legacy_extension -> webextension reply", reply,
+ "Got the expected reply from the LegacyExtensionContext");
+ browser.test.sendMessage("got-reply-message");
+ });
+
+ let port = browser.runtime.connect();
+
+ port.onMessage.addListener(msg => {
+ browser.test.assertEq("legacy_extension -> webextension port message", msg,
+ "Got the expected message from the LegacyExtensionContext");
+ port.postMessage("webextension -> legacy_extension port message");
+ });
+ }
+
+ let extensionData = {
+ background: `new ${backgroundScript}`,
+ manifest: {
+ content_scripts: [
+ {
+ matches: ["http://example.com/*"],
+ js: ["content-script.js"],
+ },
+ ],
+ },
+ files: {
+ "content-script.js": `new ${contentScript}`,
+ },
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+
+ let waitForExtensionReady = extension.awaitMessage("ready");
+
+ let waitForExtensionInstance = promiseAddonStartup(extension);
+
+ extension.startup();
+
+ let extensionInstance = yield waitForExtensionInstance;
+
+ // Connect to the target extension.id as an external context
+ // using the given custom sender info.
+ let legacyContext = new LegacyExtensionContext(extensionInstance);
+
+ let waitConnectPort = new Promise(resolve => {
+ let {browser} = legacyContext.api;
+ browser.runtime.onConnect.addListener(port => {
+ resolve(port);
+ });
+ });
+
+ let waitMessage = new Promise(resolve => {
+ let {browser} = legacyContext.api;
+ browser.runtime.onMessage.addListener((singleMsg, msgSender, sendReply) => {
+ sendReply("legacy_extension -> webextension reply");
+ resolve({singleMsg, msgSender});
+ });
+ });
+
+ is(legacyContext.envType, "legacy_extension",
+ "LegacyExtensionContext instance has the expected type");
+
+ ok(legacyContext.api, "Got the API object");
+
+ yield waitForExtensionReady;
+
+ extension.sendMessage("open-test-tab");
+
+ let {uuid, tab} = yield extension.awaitMessage("get-expected-sender-info");
+
+ let {singleMsg, msgSender} = yield waitMessage;
+ is(singleMsg, "webextension -> legacy_extension message",
+ "Got the expected message");
+ ok(msgSender, "Got a message sender object");
+
+ is(msgSender.id, uuid, "The sender has the expected id property");
+ is(msgSender.url, "http://example.com/", "The sender has the expected url property");
+ ok(msgSender.tab, "The sender has a tab property");
+ is(msgSender.tab.id, tab.id, "The port sender has the expected tab.id");
+
+ // Wait confirmation that the reply has been received.
+ yield extension.awaitMessage("got-reply-message");
+
+ let port = yield waitConnectPort;
+
+ ok(port, "Got the Port API object");
+ ok(port.sender, "The port has a sender property");
+
+ is(port.sender.id, uuid, "The port sender has an id property");
+ is(port.sender.url, "http://example.com/", "The port sender has the expected url property");
+ ok(port.sender.tab, "The port sender has a tab property");
+ is(port.sender.tab.id, tab.id, "The port sender has the expected tab.id");
+
+ let waitPortMessage = new Promise(resolve => {
+ port.onMessage.addListener((msg) => {
+ resolve(msg);
+ });
+ });
+
+ port.postMessage("legacy_extension -> webextension port message");
+
+ let msg = yield waitPortMessage;
+
+ is(msg, "webextension -> legacy_extension port message",
+ "LegacyExtensionContext received the expected message from the webextension");
+
+ let waitForDisconnect = new Promise(resolve => {
+ port.onDisconnect.addListener(resolve);
+ });
+
+ let waitForTestDone = extension.awaitMessage("current-tab-closed");
+
+ extension.sendMessage("close-current-tab");
+
+ yield waitForDisconnect;
+
+ info("Got the disconnect event on tab closed");
+
+ let success = yield waitForTestDone;
+
+ ok(success, "Test completed successfully");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_omnibox.js b/browser/components/extensions/test/browser/browser_ext_omnibox.js
new file mode 100644
index 000000000..98d3573c5
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_omnibox.js
@@ -0,0 +1,286 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* setup() {
+ const SUGGEST_URLBAR_PREF = "browser.urlbar.suggest.searches";
+ Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, false);
+
+ registerCleanupFunction(() => {
+ Services.prefs.clearUserPref(SUGGEST_URLBAR_PREF);
+ });
+}
+
+add_task(function* () {
+ let keyword = "test";
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "omnibox": {
+ "keyword": keyword,
+ },
+ },
+
+ background: function() {
+ browser.omnibox.onInputStarted.addListener(() => {
+ browser.test.sendMessage("on-input-started-fired");
+ });
+
+ let synchronous = true;
+ let suggestions = null;
+ let suggestCallback = null;
+
+ browser.omnibox.onInputChanged.addListener((text, suggest) => {
+ if (synchronous && suggestions) {
+ suggest(suggestions);
+ } else {
+ suggestCallback = suggest;
+ }
+ browser.test.sendMessage("on-input-changed-fired", {text});
+ });
+
+ browser.omnibox.onInputCancelled.addListener(() => {
+ browser.test.sendMessage("on-input-cancelled-fired");
+ });
+
+ browser.omnibox.onInputEntered.addListener((text, disposition) => {
+ browser.test.sendMessage("on-input-entered-fired", {text, disposition});
+ });
+
+ browser.test.onMessage.addListener((msg, data) => {
+ switch (msg) {
+ case "set-suggestions":
+ suggestions = data.suggestions;
+ browser.test.sendMessage("suggestions-set");
+ break;
+ case "set-default-suggestion":
+ browser.omnibox.setDefaultSuggestion(data.suggestion);
+ browser.test.sendMessage("default-suggestion-set");
+ break;
+ case "set-synchronous":
+ synchronous = data.synchronous;
+ break;
+ case "test-multiple-suggest-calls":
+ suggestions.forEach(suggestion => suggestCallback([suggestion]));
+ browser.test.sendMessage("test-ready");
+ break;
+ case "test-suggestions-after-delay":
+ Promise.resolve().then(() => {
+ suggestCallback(suggestions);
+ browser.test.sendMessage("test-ready");
+ });
+ break;
+ }
+ });
+ },
+ });
+
+ function* expectEvent(event, expected = {}) {
+ let actual = yield extension.awaitMessage(event);
+ if (expected.text) {
+ is(actual.text, expected.text,
+ `Expected "${event}" to have fired with text: "${expected.text}".`);
+ }
+ if (expected.disposition) {
+ is(actual.disposition, expected.disposition,
+ `Expected "${event}" to have fired with disposition: "${expected.disposition}".`);
+ }
+ }
+
+ function* startInputSession() {
+ gURLBar.focus();
+ gURLBar.value = keyword;
+ EventUtils.synthesizeKey(" ", {});
+ yield expectEvent("on-input-started-fired");
+ EventUtils.synthesizeKey("t", {});
+ yield expectEvent("on-input-changed-fired", {text: "t"});
+ return "t";
+ }
+
+ function* testInputEvents() {
+ gURLBar.focus();
+
+ // Start an input session by typing in <keyword><space>.
+ for (let letter of keyword) {
+ EventUtils.synthesizeKey(letter, {});
+ }
+ EventUtils.synthesizeKey(" ", {});
+ yield expectEvent("on-input-started-fired");
+
+ // We should expect input changed events now that the keyword is active.
+ EventUtils.synthesizeKey("b", {});
+ yield expectEvent("on-input-changed-fired", {text: "b"});
+
+ EventUtils.synthesizeKey("c", {});
+ yield expectEvent("on-input-changed-fired", {text: "bc"});
+
+ EventUtils.synthesizeKey("VK_BACK_SPACE", {});
+ yield expectEvent("on-input-changed-fired", {text: "b"});
+
+ // Even though the input is <keyword><space> We should not expect an
+ // input started event to fire since the keyword is active.
+ EventUtils.synthesizeKey("VK_BACK_SPACE", {});
+ yield expectEvent("on-input-changed-fired", {text: ""});
+
+ // Make the keyword inactive by hitting backspace.
+ EventUtils.synthesizeKey("VK_BACK_SPACE", {});
+ yield expectEvent("on-input-cancelled-fired");
+
+ // Activate the keyword by typing a space.
+ // Expect onInputStarted to fire.
+ EventUtils.synthesizeKey(" ", {});
+ yield expectEvent("on-input-started-fired");
+
+ // onInputChanged should fire even if a space is entered.
+ EventUtils.synthesizeKey(" ", {});
+ yield expectEvent("on-input-changed-fired", {text: " "});
+
+ // The active session should cancel if the input blurs.
+ gURLBar.blur();
+ yield expectEvent("on-input-cancelled-fired");
+ }
+
+ function* testHeuristicResult(expectedText, setDefaultSuggestion) {
+ if (setDefaultSuggestion) {
+ extension.sendMessage("set-default-suggestion", {
+ suggestion: {
+ description: expectedText,
+ },
+ });
+ yield extension.awaitMessage("default-suggestion-set");
+ }
+
+ let text = yield startInputSession();
+
+ let item = gURLBar.popup.richlistbox.children[0];
+
+ is(item.getAttribute("title"), expectedText,
+ `Expected heuristic result to have title: "${expectedText}".`);
+
+ is(item.getAttribute("displayurl"), `${keyword} ${text}`,
+ `Expected heuristic result to have displayurl: "${keyword} ${text}".`);
+
+ EventUtils.synthesizeMouseAtCenter(item, {});
+
+ yield expectEvent("on-input-entered-fired", {
+ text,
+ disposition: "currentTab",
+ });
+ }
+
+ function* testDisposition(suggestionIndex, expectedDisposition, expectedText) {
+ yield startInputSession();
+
+ // Select the suggestion.
+ for (let i = 0; i < suggestionIndex; i++) {
+ EventUtils.synthesizeKey("VK_DOWN", {});
+ }
+
+ let item = gURLBar.popup.richlistbox.children[suggestionIndex];
+ if (expectedDisposition == "currentTab") {
+ EventUtils.synthesizeMouseAtCenter(item, {});
+ } else if (expectedDisposition == "newForegroundTab") {
+ EventUtils.synthesizeMouseAtCenter(item, {accelKey: true});
+ } else if (expectedDisposition == "newBackgroundTab") {
+ EventUtils.synthesizeMouseAtCenter(item, {shiftKey: true, accelKey: true});
+ }
+
+ yield expectEvent("on-input-entered-fired", {
+ text: expectedText,
+ disposition: expectedDisposition,
+ });
+ }
+
+ function* testSuggestions(info) {
+ extension.sendMessage("set-synchronous", {synchronous: false});
+
+ function expectSuggestion({content, description}, index) {
+ let item = gURLBar.popup.richlistbox.children[index + 1]; // Skip the heuristic result.
+
+ ok(!!item, "Expected item to exist");
+ is(item.getAttribute("title"), description,
+ `Expected suggestion to have title: "${description}".`);
+
+ is(item.getAttribute("displayurl"), `${keyword} ${content}`,
+ `Expected suggestion to have displayurl: "${keyword} ${content}".`);
+ }
+
+ let text = yield startInputSession();
+
+ extension.sendMessage(info.test);
+ yield extension.awaitMessage("test-ready");
+
+ info.suggestions.forEach(expectSuggestion);
+
+ EventUtils.synthesizeMouseAtCenter(gURLBar.popup.richlistbox.children[0], {});
+ yield expectEvent("on-input-entered-fired", {
+ text,
+ disposition: "currentTab",
+ });
+ }
+
+ yield setup();
+ yield extension.startup();
+
+ yield testInputEvents();
+
+ // Test the heuristic result with default suggestions.
+ yield testHeuristicResult("Generated extension", false /* setDefaultSuggestion */);
+ yield testHeuristicResult("hello world", true /* setDefaultSuggestion */);
+ yield testHeuristicResult("foo bar", true /* setDefaultSuggestion */);
+
+ let suggestions = [
+ {content: "a", description: "select a"},
+ {content: "b", description: "select b"},
+ {content: "c", description: "select c"},
+ ];
+
+ extension.sendMessage("set-suggestions", {suggestions});
+ yield extension.awaitMessage("suggestions-set");
+
+ // Test each suggestion and search disposition.
+ yield testDisposition(1, "currentTab", suggestions[0].content);
+ yield testDisposition(2, "newForegroundTab", suggestions[1].content);
+ yield testDisposition(3, "newBackgroundTab", suggestions[2].content);
+
+ extension.sendMessage("set-suggestions", {suggestions});
+ yield extension.awaitMessage("suggestions-set");
+
+ // Test adding suggestions asynchronously.
+ yield testSuggestions({
+ test: "test-multiple-suggest-calls",
+ skipHeuristic: true,
+ suggestions,
+ });
+ yield testSuggestions({
+ test: "test-suggestions-after-delay",
+ skipHeuristic: true,
+ suggestions,
+ });
+
+ // Start monitoring the console.
+ SimpleTest.waitForExplicitFinish();
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{
+ message: new RegExp(`The keyword provided is already registered: "${keyword}"`),
+ }]);
+ });
+
+ // Try registering another extension with the same keyword
+ let extension2 = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "omnibox": {
+ "keyword": keyword,
+ },
+ },
+ });
+
+ yield extension2.startup();
+
+ // Stop monitoring the console and confirm the correct errors are logged.
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+
+ yield extension2.unload();
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_optionsPage_privileges.js b/browser/components/extensions/test/browser/browser_ext_optionsPage_privileges.js
new file mode 100644
index 000000000..3e7342dd1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_optionsPage_privileges.js
@@ -0,0 +1,66 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_tab_options_privileges() {
+ function backgroundScript() {
+ browser.runtime.onMessage.addListener(({msgName, tabId}) => {
+ if (msgName == "removeTabId") {
+ browser.tabs.remove(tabId).then(() => {
+ browser.test.notifyPass("options-ui-privileges");
+ }).catch(error => {
+ browser.test.log(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("options-ui-privileges");
+ });
+ }
+ });
+ browser.runtime.openOptionsPage();
+ }
+
+ async function optionsScript() {
+ try {
+ let [tab] = await browser.tabs.query({url: "http://example.com/"});
+ browser.test.assertEq("http://example.com/", tab.url, "Got the expect tab");
+
+ tab = await browser.tabs.getCurrent();
+ browser.runtime.sendMessage({msgName: "removeTabId", tabId: tab.id});
+ } catch (error) {
+ browser.test.log(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("options-ui-privileges");
+ }
+ }
+
+ const ID = "options_privileges@tests.mozilla.org";
+ let extension = ExtensionTestUtils.loadExtension({
+ useAddonManager: "temporary",
+
+ manifest: {
+ applications: {gecko: {id: ID}},
+ "permissions": ["tabs"],
+ "options_ui": {
+ "page": "options.html",
+ },
+ },
+ files: {
+ "options.html": `<!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <script src="options.js" type="text/javascript"></script>
+ </head>
+ </html>`,
+ "options.js": optionsScript,
+ },
+ background: backgroundScript,
+ });
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("options-ui-privileges");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_pageAction_context.js b/browser/components/extensions/test/browser/browser_ext_pageAction_context.js
new file mode 100644
index 000000000..2c2a4cd2f
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_pageAction_context.js
@@ -0,0 +1,178 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* global runTests */
+
+Services.scriptloader.loadSubScript(new URL("head_pageAction.js", gTestPath).href,
+ this);
+
+add_task(function* testTabSwitchContext() {
+ yield runTests({
+ manifest: {
+ "name": "Foo Extension",
+
+ "page_action": {
+ "default_icon": "default.png",
+ "default_popup": "__MSG_popup__",
+ "default_title": "Default __MSG_title__ \u263a",
+ },
+
+ "default_locale": "en",
+
+ "permissions": ["tabs"],
+ },
+
+ "files": {
+ "_locales/en/messages.json": {
+ "popup": {
+ "message": "default.html",
+ "description": "Popup",
+ },
+
+ "title": {
+ "message": "Title",
+ "description": "Title",
+ },
+ },
+
+ "_locales/es_ES/messages.json": {
+ "popup": {
+ "message": "default.html",
+ "description": "Popup",
+ },
+
+ "title": {
+ "message": "T\u00edtulo",
+ "description": "Title",
+ },
+ },
+
+ "default.png": imageBuffer,
+ "1.png": imageBuffer,
+ "2.png": imageBuffer,
+ },
+
+ getTests(tabs) {
+ let details = [
+ {"icon": browser.runtime.getURL("default.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ {"icon": browser.runtime.getURL("1.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ {"icon": browser.runtime.getURL("2.png"),
+ "popup": browser.runtime.getURL("2.html"),
+ "title": "Title 2"},
+ {"icon": browser.runtime.getURL("2.png"),
+ "popup": browser.runtime.getURL("2.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ ];
+
+ let promiseTabLoad = details => {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed) {
+ if (tabId == details.id && changed.url == details.url) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ };
+
+ return [
+ expect => {
+ browser.test.log("Initial state. No icon visible.");
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Show the icon on the first tab, expect default properties.");
+ await browser.pageAction.show(tabs[0]);
+ expect(details[0]);
+ },
+ expect => {
+ browser.test.log("Change the icon. Expect default properties excluding the icon.");
+ browser.pageAction.setIcon({tabId: tabs[0], path: "1.png"});
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Create a new tab. No icon visible.");
+ let tab = await browser.tabs.create({active: true, url: "about:blank?0"});
+ tabs.push(tab.id);
+ expect(null);
+ },
+ expect => {
+ browser.test.log("Await tab load. No icon visible.");
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Change properties. Expect new properties.");
+ let tabId = tabs[1];
+ await browser.pageAction.show(tabId);
+
+ browser.pageAction.setIcon({tabId, path: "2.png"});
+ browser.pageAction.setPopup({tabId, popup: "2.html"});
+ browser.pageAction.setTitle({tabId, title: "Title 2"});
+
+ expect(details[2]);
+ },
+ async expect => {
+ browser.test.log("Change the hash. Expect same properties.");
+
+ let promise = promiseTabLoad({id: tabs[1], url: "about:blank?0#ref"});
+ browser.tabs.update(tabs[1], {url: "about:blank?0#ref"});
+ await promise;
+
+ expect(details[2]);
+ },
+ expect => {
+ browser.test.log("Clear the title. Expect default title.");
+ browser.pageAction.setTitle({tabId: tabs[1], title: ""});
+
+ expect(details[3]);
+ },
+ async expect => {
+ browser.test.log("Navigate to a new page. Expect icon hidden.");
+
+ // TODO: This listener should not be necessary, but the |tabs.update|
+ // callback currently fires too early in e10s windows.
+ let promise = promiseTabLoad({id: tabs[1], url: "about:blank?1"});
+
+ browser.tabs.update(tabs[1], {url: "about:blank?1"});
+
+ await promise;
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Show the icon. Expect default properties again.");
+
+ await browser.pageAction.show(tabs[1]);
+ expect(details[0]);
+ },
+ async expect => {
+ browser.test.log("Switch back to the first tab. Expect previously set properties.");
+ await browser.tabs.update(tabs[0], {active: true});
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Hide the icon on tab 2. Switch back, expect hidden.");
+ await browser.pageAction.hide(tabs[1]);
+
+ await browser.tabs.update(tabs[1], {active: true});
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Switch back to tab 1. Expect previous results again.");
+ await browser.tabs.remove(tabs[1]);
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Hide the icon. Expect hidden.");
+
+ await browser.pageAction.hide(tabs[0]);
+ expect(null);
+ },
+ ];
+ },
+ });
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_pageAction_popup.js b/browser/components/extensions/test/browser/browser_ext_pageAction_popup.js
new file mode 100644
index 000000000..83defdd68
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_pageAction_popup.js
@@ -0,0 +1,238 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testPageActionPopup() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head></html>`;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "background": {
+ "page": "data/background.html",
+ },
+ "page_action": {
+ "default_popup": "popup-a.html",
+ },
+ },
+
+ files: {
+ "popup-a.html": scriptPage("popup-a.js"),
+ "popup-a.js": function() {
+ window.onload = () => {
+ let background = window.getComputedStyle(document.body).backgroundColor;
+ browser.test.assertEq("transparent", background);
+ browser.runtime.sendMessage("from-popup-a");
+ };
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "close-popup") {
+ window.close();
+ }
+ });
+ },
+
+ "data/popup-b.html": scriptPage("popup-b.js"),
+ "data/popup-b.js": function() {
+ browser.runtime.sendMessage("from-popup-b");
+ },
+
+ "data/background.html": scriptPage("background.js"),
+
+ "data/background.js": async function() {
+ let tabId;
+
+ let sendClick;
+ let tests = [
+ () => {
+ sendClick({expectEvent: false, expectPopup: "a"});
+ },
+ () => {
+ sendClick({expectEvent: false, expectPopup: "a"});
+ },
+ () => {
+ browser.pageAction.setPopup({tabId, popup: "popup-b.html"});
+ sendClick({expectEvent: false, expectPopup: "b"});
+ },
+ () => {
+ sendClick({expectEvent: false, expectPopup: "b"});
+ },
+ () => {
+ browser.pageAction.setPopup({tabId, popup: ""});
+ sendClick({expectEvent: true, expectPopup: null});
+ },
+ () => {
+ sendClick({expectEvent: true, expectPopup: null});
+ },
+ () => {
+ browser.pageAction.setPopup({tabId, popup: "/popup-a.html"});
+ sendClick({expectEvent: false, expectPopup: "a", runNextTest: true});
+ },
+ () => {
+ browser.test.sendMessage("next-test", {expectClosed: true});
+ },
+ () => {
+ sendClick({expectEvent: false, expectPopup: "a", runNextTest: true});
+ },
+ () => {
+ browser.test.sendMessage("next-test", {closeOnTabSwitch: true});
+ },
+ ];
+
+ let expect = {};
+ sendClick = ({expectEvent, expectPopup, runNextTest}) => {
+ expect = {event: expectEvent, popup: expectPopup, runNextTest};
+ browser.test.sendMessage("send-click");
+ };
+
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "close-popup") {
+ return;
+ } else if (expect.popup) {
+ browser.test.assertEq(msg, `from-popup-${expect.popup}`,
+ "expected popup opened");
+ } else {
+ browser.test.fail(`unexpected popup: ${msg}`);
+ }
+
+ expect.popup = null;
+ if (expect.runNextTest) {
+ expect.runNextTest = false;
+ tests.shift()();
+ } else {
+ browser.test.sendMessage("next-test");
+ }
+ });
+
+ browser.pageAction.onClicked.addListener(() => {
+ if (expect.event) {
+ browser.test.succeed("expected click event received");
+ } else {
+ browser.test.fail("unexpected click event");
+ }
+
+ expect.event = false;
+ browser.test.sendMessage("next-test");
+ });
+
+ browser.test.onMessage.addListener(msg => {
+ if (msg == "close-popup") {
+ browser.runtime.sendMessage("close-popup");
+ return;
+ }
+
+ if (msg != "next-test") {
+ browser.test.fail("Expecting 'next-test' message");
+ }
+
+ if (tests.length) {
+ let test = tests.shift();
+ test();
+ } else {
+ browser.test.notifyPass("pageaction-tests-done");
+ }
+ });
+
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ tabId = tab.id;
+
+ await browser.pageAction.show(tabId);
+ browser.test.sendMessage("next-test");
+ },
+ },
+ });
+
+ extension.onMessage("send-click", () => {
+ clickPageAction(extension);
+ });
+
+ let pageActionId, panelId;
+ extension.onMessage("next-test", Task.async(function* (expecting = {}) {
+ pageActionId = `${makeWidgetId(extension.id)}-page-action`;
+ panelId = `${makeWidgetId(extension.id)}-panel`;
+ let panel = document.getElementById(panelId);
+ if (expecting.expectClosed) {
+ ok(panel, "Expect panel to exist");
+ yield promisePopupShown(panel);
+
+ extension.sendMessage("close-popup");
+
+ yield promisePopupHidden(panel);
+ ok(true, `Panel is closed`);
+ } else if (expecting.closeOnTabSwitch) {
+ ok(panel, "Expect panel to exist");
+ yield promisePopupShown(panel);
+
+ let oldTab = gBrowser.selectedTab;
+ ok(oldTab != gBrowser.tabs[0], "Should have an inactive tab to switch to");
+
+ let hiddenPromise = promisePopupHidden(panel);
+
+ gBrowser.selectedTab = gBrowser.tabs[0];
+ yield hiddenPromise;
+ info("Panel closed");
+
+ gBrowser.selectedTab = oldTab;
+ } else if (panel) {
+ yield promisePopupShown(panel);
+ panel.hidePopup();
+ }
+
+ if (panel) {
+ panel = document.getElementById(panelId);
+ is(panel, null, "panel successfully removed from document after hiding");
+ }
+
+ extension.sendMessage("next-test");
+ }));
+
+
+ yield extension.startup();
+ yield extension.awaitFinish("pageaction-tests-done");
+
+ yield extension.unload();
+
+ let node = document.getElementById(pageActionId);
+ is(node, null, "pageAction image removed from document");
+
+ let panel = document.getElementById(panelId);
+ is(panel, null, "pageAction panel removed from document");
+
+ yield BrowserTestUtils.removeTab(tab);
+});
+
+
+add_task(function* testPageActionSecurity() {
+ const URL = "chrome://browser/content/browser.xul";
+
+ let apis = ["browser_action", "page_action"];
+
+ for (let api of apis) {
+ info(`TEST ${api} icon url: ${URL}`);
+
+ let messages = [/Access to restricted URI denied/];
+
+ let waitForConsole = new Promise(resolve => {
+ // Not necessary in browser-chrome tests, but monitorConsole gripes
+ // if we don't call it.
+ SimpleTest.waitForExplicitFinish();
+
+ SimpleTest.monitorConsole(resolve, messages);
+ });
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ [api]: {"default_popup": URL},
+ },
+ });
+
+ yield Assert.rejects(extension.startup(),
+ null,
+ "Manifest rejected");
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+ }
+});
+
+add_task(forceGC);
diff --git a/browser/components/extensions/test/browser/browser_ext_pageAction_popup_resize.js b/browser/components/extensions/test/browser/browser_ext_pageAction_popup_resize.js
new file mode 100644
index 000000000..98c4c3488
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_pageAction_popup_resize.js
@@ -0,0 +1,169 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+let delay = ms => new Promise(resolve => {
+ setTimeout(resolve, ms);
+});
+
+add_task(function* testPageActionPopupResize() {
+ let browser;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ },
+ background: function() {
+ /* global browser */
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ const tabId = tabs[0].id;
+
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("action-shown");
+ });
+ });
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html><html><head><meta charset="utf-8"></head><body><div></div></body></html>`,
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("action-shown");
+
+ clickPageAction(extension, window);
+
+ browser = yield awaitExtensionPanel(extension);
+
+ function* checkSize(expected) {
+ let dims = yield promiseContentDimensions(browser);
+ let {body, root} = dims;
+
+ is(dims.window.innerHeight, expected, `Panel window should be ${expected}px tall`);
+ is(body.clientHeight, body.scrollHeight,
+ "Panel body should be tall enough to fit its contents");
+ is(root.clientHeight, root.scrollHeight,
+ "Panel root should be tall enough to fit its contents");
+
+ // Tolerate if it is 1px too wide, as that may happen with the current resizing method.
+ ok(Math.abs(dims.window.innerWidth - expected) <= 1, `Panel window should be ${expected}px wide`);
+ is(body.clientWidth, body.scrollWidth,
+ "Panel body should be wide enough to fit its contents");
+ }
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+ function setSize(size) {
+ let elem = content.document.body.firstChild;
+ elem.style.height = `${size}px`;
+ elem.style.width = `${size}px`;
+ }
+ /* eslint-enable mozilla/no-cpows-in-tests */
+
+ let sizes = [
+ 200,
+ 400,
+ 300,
+ ];
+
+ for (let size of sizes) {
+ yield alterContent(browser, setSize, size);
+ yield checkSize(size);
+ }
+
+ yield alterContent(browser, setSize, 1400);
+
+ let dims = yield promiseContentDimensions(browser);
+ let {body, root} = dims;
+
+ if (AppConstants.platform == "win") {
+ while (dims.window.innerWidth < 800) {
+ yield delay(50);
+ dims = yield promiseContentDimensions(browser);
+ }
+ }
+
+ is(dims.window.innerWidth, 800, "Panel window width");
+ ok(body.clientWidth <= 800, `Panel body width ${body.clientWidth} is less than 800`);
+ is(body.scrollWidth, 1400, "Panel body scroll width");
+
+ is(dims.window.innerHeight, 600, "Panel window height");
+ ok(root.clientHeight <= 600, `Panel root height (${root.clientHeight}px) is less than 600px`);
+ is(root.scrollHeight, 1400, "Panel root scroll height");
+
+ yield extension.unload();
+});
+
+add_task(function* testPageActionPopupReflow() {
+ let browser;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ },
+ background: function() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ const tabId = tabs[0].id;
+
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("action-shown");
+ });
+ });
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html><html><head><meta charset="utf-8"></head>
+ <body>
+ The quick mauve fox jumps over the opalescent toad, with its glowing
+ eyes, and its vantablack mouth, and its bottomless chasm where you
+ would hope to find a heart, that looks straight into the deepest
+ pits of hell. The fox shivers, and cowers, and tries to run, but
+ the toad is utterly without pity. It turns, ever so slightly...
+ </body>
+ </html>`,
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("action-shown");
+
+ clickPageAction(extension, window);
+
+ browser = yield awaitExtensionPanel(extension);
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+ function setSize(size) {
+ content.document.body.style.fontSize = `${size}px`;
+ }
+ /* eslint-enable mozilla/no-cpows-in-tests */
+
+ let dims = yield alterContent(browser, setSize, 18);
+
+ if (AppConstants.platform == "win") {
+ while (dims.window.innerWidth < 800) {
+ yield delay(50);
+ dims = yield promiseContentDimensions(browser);
+ }
+ }
+
+ is(dims.window.innerWidth, 800, "Panel window should be 800px wide");
+ is(dims.body.clientWidth, 800, "Panel body should be 800px wide");
+ is(dims.body.clientWidth, dims.body.scrollWidth,
+ "Panel body should be wide enough to fit its contents");
+
+ ok(dims.window.innerHeight > 36,
+ `Panel window height (${dims.window.innerHeight}px) should be taller than two lines of text.`);
+
+ is(dims.body.clientHeight, dims.body.scrollHeight,
+ "Panel body should be tall enough to fit its contents");
+ is(dims.root.clientHeight, dims.root.scrollHeight,
+ "Panel root should be tall enough to fit its contents");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_pageAction_simple.js b/browser/components/extensions/test/browser/browser_ext_pageAction_simple.js
new file mode 100644
index 000000000..d1d173801
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_pageAction_simple.js
@@ -0,0 +1,60 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "page_action": {
+ "default_popup": "popup.html",
+ "unrecognized_property": "with-a-random-value",
+ },
+ },
+
+ files: {
+ "popup.html": `
+ <!DOCTYPE html>
+ <html><body>
+ <script src="popup.js"></script>
+ </body></html>
+ `,
+
+ "popup.js": function() {
+ browser.runtime.sendMessage("from-popup");
+ },
+ },
+
+ background: function() {
+ browser.runtime.onMessage.addListener(msg => {
+ browser.test.assertEq(msg, "from-popup", "correct message received");
+ browser.test.sendMessage("popup");
+ });
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ let tabId = tabs[0].id;
+
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("page-action-shown");
+ });
+ });
+ },
+ });
+
+ SimpleTest.waitForExplicitFinish();
+ let waitForConsole = new Promise(resolve => {
+ SimpleTest.monitorConsole(resolve, [{
+ message: /Reading manifest: Error processing page_action.unrecognized_property: An unexpected property was found/,
+ }]);
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("page-action-shown");
+
+ clickPageAction(extension);
+
+ yield extension.awaitMessage("popup");
+
+ yield extension.unload();
+
+ SimpleTest.endMonitorConsole();
+ yield waitForConsole;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_pageAction_title.js b/browser/components/extensions/test/browser/browser_ext_pageAction_title.js
new file mode 100644
index 000000000..793cd4499
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_pageAction_title.js
@@ -0,0 +1,226 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* global runTests */
+
+Services.scriptloader.loadSubScript(new URL("head_pageAction.js", gTestPath).href,
+ this);
+
+add_task(function* testTabSwitchContext() {
+ yield runTests({
+ manifest: {
+ "name": "Foo Extension",
+
+ "page_action": {
+ "default_icon": "default.png",
+ "default_popup": "__MSG_popup__",
+ "default_title": "Default __MSG_title__ \u263a",
+ },
+
+ "default_locale": "en",
+
+ "permissions": ["tabs"],
+ },
+
+ "files": {
+ "_locales/en/messages.json": {
+ "popup": {
+ "message": "default.html",
+ "description": "Popup",
+ },
+
+ "title": {
+ "message": "Title",
+ "description": "Title",
+ },
+ },
+
+ "_locales/es_ES/messages.json": {
+ "popup": {
+ "message": "default.html",
+ "description": "Popup",
+ },
+
+ "title": {
+ "message": "T\u00edtulo",
+ "description": "Title",
+ },
+ },
+
+ "default.png": imageBuffer,
+ "1.png": imageBuffer,
+ "2.png": imageBuffer,
+ },
+
+ getTests(tabs) {
+ let details = [
+ {"icon": browser.runtime.getURL("default.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ {"icon": browser.runtime.getURL("1.png"),
+ "popup": browser.runtime.getURL("default.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ {"icon": browser.runtime.getURL("2.png"),
+ "popup": browser.runtime.getURL("2.html"),
+ "title": "Title 2"},
+ {"icon": browser.runtime.getURL("2.png"),
+ "popup": browser.runtime.getURL("2.html"),
+ "title": "Default T\u00edtulo \u263a"},
+ ];
+
+ let promiseTabLoad = details => {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed) {
+ if (tabId == details.id && changed.url == details.url) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ };
+ return [
+ expect => {
+ browser.test.log("Initial state. No icon visible.");
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Show the icon on the first tab, expect default properties.");
+ await browser.pageAction.show(tabs[0]);
+ expect(details[0]);
+ },
+ expect => {
+ browser.test.log("Change the icon. Expect default properties excluding the icon.");
+ browser.pageAction.setIcon({tabId: tabs[0], path: "1.png"});
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Create a new tab. No icon visible.");
+ let tab = await browser.tabs.create({active: true, url: "about:blank?0"});
+ tabs.push(tab.id);
+ expect(null);
+ },
+ expect => {
+ browser.test.log("Await tab load. No icon visible.");
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Change properties. Expect new properties.");
+ let tabId = tabs[1];
+
+ await browser.pageAction.show(tabId);
+ browser.pageAction.setIcon({tabId, path: "2.png"});
+ browser.pageAction.setPopup({tabId, popup: "2.html"});
+ browser.pageAction.setTitle({tabId, title: "Title 2"});
+
+ expect(details[2]);
+ },
+ async expect => {
+ browser.test.log("Change the hash. Expect same properties.");
+
+ let promise = promiseTabLoad({id: tabs[1], url: "about:blank?0#ref"});
+
+ browser.tabs.update(tabs[1], {url: "about:blank?0#ref"});
+
+ await promise;
+ expect(details[2]);
+ },
+ expect => {
+ browser.test.log("Clear the title. Expect default title.");
+ browser.pageAction.setTitle({tabId: tabs[1], title: ""});
+
+ expect(details[3]);
+ },
+ async expect => {
+ browser.test.log("Navigate to a new page. Expect icon hidden.");
+
+ // TODO: This listener should not be necessary, but the |tabs.update|
+ // callback currently fires too early in e10s windows.
+ let promise = promiseTabLoad({id: tabs[1], url: "about:blank?1"});
+
+ browser.tabs.update(tabs[1], {url: "about:blank?1"});
+
+ await promise;
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Show the icon. Expect default properties again.");
+ await browser.pageAction.show(tabs[1]);
+ expect(details[0]);
+ },
+ async expect => {
+ browser.test.log("Switch back to the first tab. Expect previously set properties.");
+ await browser.tabs.update(tabs[0], {active: true});
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Hide the icon on tab 2. Switch back, expect hidden.");
+ await browser.pageAction.hide(tabs[1]);
+ await browser.tabs.update(tabs[1], {active: true});
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Switch back to tab 1. Expect previous results again.");
+ await browser.tabs.remove(tabs[1]);
+ expect(details[1]);
+ },
+ async expect => {
+ browser.test.log("Hide the icon. Expect hidden.");
+ await browser.pageAction.hide(tabs[0]);
+ expect(null);
+ },
+ ];
+ },
+ });
+});
+
+add_task(function* testDefaultTitle() {
+ yield runTests({
+ manifest: {
+ "name": "Foo Extension",
+
+ "page_action": {
+ "default_icon": "icon.png",
+ },
+
+ "permissions": ["tabs"],
+ },
+
+ files: {
+ "icon.png": imageBuffer,
+ },
+
+ getTests(tabs) {
+ let details = [
+ {"title": "Foo Extension",
+ "popup": "",
+ "icon": browser.runtime.getURL("icon.png")},
+ {"title": "Foo Title",
+ "popup": "",
+ "icon": browser.runtime.getURL("icon.png")},
+ ];
+
+ return [
+ expect => {
+ browser.test.log("Initial state. No icon visible.");
+ expect(null);
+ },
+ async expect => {
+ browser.test.log("Show the icon on the first tab, expect extension title as default title.");
+ await browser.pageAction.show(tabs[0]);
+ expect(details[0]);
+ },
+ expect => {
+ browser.test.log("Change the title. Expect new title.");
+ browser.pageAction.setTitle({tabId: tabs[0], title: "Foo Title"});
+ expect(details[1]);
+ },
+ expect => {
+ browser.test.log("Clear the title. Expect extension title.");
+ browser.pageAction.setTitle({tabId: tabs[0], title: ""});
+ expect(details[0]);
+ },
+ ];
+ },
+ });
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_popup_api_injection.js b/browser/components/extensions/test/browser/browser_ext_popup_api_injection.js
new file mode 100644
index 000000000..6f8a541a9
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_api_injection.js
@@ -0,0 +1,101 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testPageActionPopup() {
+ const BASE = "http://example.com/browser/browser/components/extensions/test/browser";
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": `${BASE}/file_popup_api_injection_a.html`,
+ },
+ "page_action": {
+ "default_popup": `${BASE}/file_popup_api_injection_b.html`,
+ },
+ },
+
+ files: {
+ "popup-a.html": `<html><head><meta charset="utf-8">
+ <script type="application/javascript" src="popup-a.js"></script></head></html>`,
+ "popup-a.js": 'browser.test.sendMessage("from-popup-a");',
+
+ "popup-b.html": `<html><head><meta charset="utf-8">
+ <script type="application/javascript" src="popup-b.js"></script></head></html>`,
+ "popup-b.js": 'browser.test.sendMessage("from-popup-b");',
+ },
+
+ background: function() {
+ let tabId;
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ tabId = tabs[0].id;
+ browser.pageAction.show(tabId).then(() => {
+ browser.test.sendMessage("ready");
+ });
+ });
+
+ browser.test.onMessage.addListener(() => {
+ browser.browserAction.setPopup({popup: "/popup-a.html"});
+ browser.pageAction.setPopup({tabId, popup: "popup-b.html"});
+
+ browser.test.sendMessage("ok");
+ });
+ },
+ });
+
+ let promiseConsoleMessage = pattern => new Promise(resolve => {
+ Services.console.registerListener(function listener(msg) {
+ if (pattern.test(msg.message)) {
+ resolve(msg.message);
+ Services.console.unregisterListener(listener);
+ }
+ });
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+
+ // Check that unprivileged documents don't get the API.
+ // BrowserAction:
+ let awaitMessage = promiseConsoleMessage(/WebExt Privilege Escalation: BrowserAction/);
+ SimpleTest.expectUncaughtException();
+ yield clickBrowserAction(extension);
+ yield promisePopupShown(getBrowserActionPopup(extension));
+
+ let message = yield awaitMessage;
+ ok(message.includes("WebExt Privilege Escalation: BrowserAction: typeof(browser) = undefined"),
+ `No BrowserAction API injection`);
+
+ yield closeBrowserAction(extension);
+
+ // PageAction
+ awaitMessage = promiseConsoleMessage(/WebExt Privilege Escalation: PageAction/);
+ SimpleTest.expectUncaughtException();
+ yield clickPageAction(extension);
+
+ message = yield awaitMessage;
+ ok(message.includes("WebExt Privilege Escalation: PageAction: typeof(browser) = undefined"),
+ `No PageAction API injection: ${message}`);
+
+ yield closePageAction(extension);
+
+ SimpleTest.expectUncaughtException(false);
+
+
+ // Check that privileged documents *do* get the API.
+ extension.sendMessage("next");
+ yield extension.awaitMessage("ok");
+
+
+ yield clickBrowserAction(extension);
+ yield extension.awaitMessage("from-popup-a");
+ yield promisePopupShown(getBrowserActionPopup(extension));
+ yield closeBrowserAction(extension);
+
+ yield clickPageAction(extension);
+ yield extension.awaitMessage("from-popup-b");
+ yield closePageAction(extension);
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_popup_background.js b/browser/components/extensions/test/browser/browser_ext_popup_background.js
new file mode 100644
index 000000000..8b310c674
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_background.js
@@ -0,0 +1,133 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testPopupBackground() {
+ let extension = ExtensionTestUtils.loadExtension({
+ background() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ browser.pageAction.show(tabs[0].id);
+ });
+ },
+
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body style="width: 100px; height: 100px; background-color: green;">
+ </body>
+ </html>`,
+ },
+ });
+
+ yield extension.startup();
+
+ function* testPanel(browser, standAlone) {
+ let panel = getPanelForNode(browser);
+ let arrowContent = document.getAnonymousElementByAttribute(panel, "class", "panel-arrowcontent");
+ let arrow = document.getAnonymousElementByAttribute(panel, "anonid", "arrow");
+
+ let borderColor = getComputedStyle(arrowContent).borderTopColor;
+
+ let checkArrow = (background = null) => {
+ let image = getComputedStyle(arrow).listStyleImage;
+
+ if (background == null || !standAlone) {
+ ok(image.startsWith('url("chrome://'), `We should have the built-in background image (got: ${image})`);
+ return;
+ }
+
+ if (AppConstants.platform == "mac") {
+ // Panels have a drop shadow rather than a border on OS-X, so we extend
+ // the background color through the border area instead.
+ borderColor = background;
+ }
+
+ image = decodeURIComponent(image);
+ let borderIndex = image.indexOf(`fill="${borderColor}"`);
+ let backgroundIndex = image.lastIndexOf(`fill="${background}"`);
+
+ ok(borderIndex >= 0, `Have border fill (index=${borderIndex})`);
+ ok(backgroundIndex >= 0, `Have background fill (index=${backgroundIndex})`);
+ is(getComputedStyle(arrowContent).backgroundColor, background, "Arrow content should have correct background");
+ isnot(borderIndex, backgroundIndex, "Border and background fills are separate elements");
+ };
+
+ function getBackground(browser) {
+ return ContentTask.spawn(browser, null, function* () {
+ return content.getComputedStyle(content.document.body)
+ .backgroundColor;
+ });
+ }
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+ let setBackground = color => {
+ content.document.body.style.backgroundColor = color;
+ };
+ /* eslint-enable mozilla/no-cpows-in-tests */
+
+ yield new Promise(resolve => setTimeout(resolve, 100));
+
+ info("Test that initial background color is applied");
+
+ checkArrow(yield getBackground(browser));
+
+ info("Test that dynamically-changed background color is applied");
+
+ yield alterContent(browser, setBackground, "black");
+
+ checkArrow(yield getBackground(browser));
+
+ info("Test that non-opaque background color results in default styling");
+
+ yield alterContent(browser, setBackground, "rgba(1, 2, 3, .9)");
+
+ checkArrow(null);
+ }
+
+ {
+ info("Test stand-alone browserAction popup");
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser, true);
+ yield closeBrowserAction(extension);
+ }
+
+ {
+ info("Test menu panel browserAction popup");
+
+ let widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, CustomizableUI.AREA_PANEL);
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser, false);
+ yield closeBrowserAction(extension);
+ }
+
+ {
+ info("Test pageAction popup");
+
+ clickPageAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser, true);
+ yield closePageAction(extension);
+ }
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_popup_corners.js b/browser/components/extensions/test/browser/browser_ext_popup_corners.js
new file mode 100644
index 000000000..52985ee46
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_corners.js
@@ -0,0 +1,98 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testPopupBorderRadius() {
+ let extension = ExtensionTestUtils.loadExtension({
+ background() {
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ browser.pageAction.show(tabs[0].id);
+ });
+ },
+
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html>
+ <html>
+ <head><meta charset="utf-8"></head>
+ <body style="width: 100px; height: 100px;"></body>
+ </html>`,
+ },
+ });
+
+ yield extension.startup();
+
+ function* testPanel(browser, standAlone = true) {
+ let panel = getPanelForNode(browser);
+ let arrowContent = document.getAnonymousElementByAttribute(panel, "class", "panel-arrowcontent");
+
+ let panelStyle = getComputedStyle(arrowContent);
+
+ let viewNode = browser.parentNode === panel ? browser : browser.parentNode;
+ let viewStyle = getComputedStyle(viewNode);
+
+ let props = ["borderTopLeftRadius", "borderTopRightRadius",
+ "borderBottomRightRadius", "borderBottomLeftRadius"];
+
+ /* eslint-disable mozilla/no-cpows-in-tests */
+ let bodyStyle = yield ContentTask.spawn(browser, props, function* (props) {
+ let bodyStyle = content.getComputedStyle(content.document.body);
+
+ return new Map(props.map(prop => [prop, bodyStyle[prop]]));
+ });
+ /* eslint-enable mozilla/no-cpows-in-tests */
+
+ for (let prop of props) {
+ if (standAlone) {
+ is(viewStyle[prop], panelStyle[prop], `Panel and view ${prop} should be the same`);
+ is(bodyStyle.get(prop), panelStyle[prop], `Panel and body ${prop} should be the same`);
+ } else {
+ is(viewStyle[prop], "0px", `View node ${prop} should be 0px`);
+ is(bodyStyle.get(prop), "0px", `Body node ${prop} should be 0px`);
+ }
+ }
+ }
+
+ {
+ info("Test stand-alone browserAction popup");
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser);
+ yield closeBrowserAction(extension);
+ }
+
+ {
+ info("Test menu panel browserAction popup");
+
+ let widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, CustomizableUI.AREA_PANEL);
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser, false);
+ yield closeBrowserAction(extension);
+ }
+
+ {
+ info("Test pageAction popup");
+
+ clickPageAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ yield testPanel(browser);
+ yield closePageAction(extension);
+ }
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_popup_sendMessage.js b/browser/components/extensions/test/browser/browser_ext_popup_sendMessage.js
new file mode 100644
index 000000000..472ee7bbd
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_sendMessage.js
@@ -0,0 +1,93 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_popup_sendMessage_reply() {
+ let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head><body>${url}</body></html>`;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": true,
+ },
+ },
+
+ files: {
+ "popup.html": scriptPage("popup.js"),
+ "popup.js": async function() {
+ browser.runtime.onMessage.addListener(async msg => {
+ if (msg == "popup-ping") {
+ return Promise.resolve("popup-pong");
+ }
+ });
+
+ let response = await browser.runtime.sendMessage("background-ping");
+ browser.test.sendMessage("background-ping-response", response);
+ },
+ },
+
+ async background() {
+ browser.runtime.onMessage.addListener(async msg => {
+ if (msg == "background-ping") {
+ let response = await browser.runtime.sendMessage("popup-ping");
+
+ browser.test.sendMessage("popup-ping-response", response);
+
+ await new Promise(resolve => {
+ // Wait long enough that we're relatively sure the docShells have
+ // been swapped. Note that this value is fairly arbitrary. The load
+ // event that triggers the swap should happen almost immediately
+ // after the message is sent. The extra quarter of a second gives us
+ // enough leeway that we can expect to respond after the swap in the
+ // vast majority of cases.
+ setTimeout(resolve, 250);
+ });
+
+ return "background-pong";
+ }
+ });
+
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+
+ await browser.pageAction.show(tab.id);
+
+ browser.test.sendMessage("page-action-ready");
+ },
+ });
+
+ yield extension.startup();
+
+ {
+ clickBrowserAction(extension);
+
+ let pong = yield extension.awaitMessage("background-ping-response");
+ is(pong, "background-pong", "Got pong");
+
+ pong = yield extension.awaitMessage("popup-ping-response");
+ is(pong, "popup-pong", "Got pong");
+
+ yield closeBrowserAction(extension);
+ }
+
+ yield extension.awaitMessage("page-action-ready");
+
+ {
+ clickPageAction(extension);
+
+ let pong = yield extension.awaitMessage("background-ping-response");
+ is(pong, "background-pong", "Got pong");
+
+ pong = yield extension.awaitMessage("popup-ping-response");
+ is(pong, "popup-pong", "Got pong");
+
+ yield closePageAction(extension);
+ }
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_popup_shutdown.js b/browser/components/extensions/test/browser/browser_ext_popup_shutdown.js
new file mode 100644
index 000000000..66d37e857
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_shutdown.js
@@ -0,0 +1,77 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+let getExtension = () => {
+ return ExtensionTestUtils.loadExtension({
+ background: async function() {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ await browser.pageAction.show(tab.id);
+ browser.test.sendMessage("pageAction ready");
+ },
+
+ manifest: {
+ "browser_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+
+ "page_action": {
+ "default_popup": "popup.html",
+ "browser_style": false,
+ },
+ },
+
+ files: {
+ "popup.html": `<!DOCTYPE html>
+ <html><head><meta charset="utf-8"></head></html>`,
+ },
+ });
+};
+
+add_task(function* testStandaloneBrowserAction() {
+ info("Test stand-alone browserAction popup");
+
+ let extension = getExtension();
+ yield extension.startup();
+ yield extension.awaitMessage("pageAction ready");
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ let panel = getPanelForNode(browser);
+
+ yield extension.unload();
+
+ is(panel.parentNode, null, "Panel should be removed from the document");
+});
+
+add_task(function* testMenuPanelBrowserAction() {
+ let extension = getExtension();
+ yield extension.startup();
+ yield extension.awaitMessage("pageAction ready");
+
+ let widget = getBrowserActionWidget(extension);
+ CustomizableUI.addWidgetToArea(widget.id, CustomizableUI.AREA_PANEL);
+
+ clickBrowserAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ let panel = getPanelForNode(browser);
+
+ yield extension.unload();
+
+ is(panel.state, "closed", "Panel should be closed");
+});
+
+add_task(function* testPageAction() {
+ let extension = getExtension();
+ yield extension.startup();
+ yield extension.awaitMessage("pageAction ready");
+
+ clickPageAction(extension);
+ let browser = yield awaitExtensionPanel(extension);
+ let panel = getPanelForNode(browser);
+
+ yield extension.unload();
+
+ is(panel.parentNode, null, "Panel should be removed from the document");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage.js b/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage.js
new file mode 100644
index 000000000..1631ececa
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage.js
@@ -0,0 +1,276 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+requestLongerTimeout(2);
+
+function add_tasks(task) {
+ add_task(task.bind(null, {embedded: false}));
+
+ add_task(task.bind(null, {embedded: true}));
+}
+
+function* loadExtension(options) {
+ let extension = ExtensionTestUtils.loadExtension({
+ useAddonManager: "temporary",
+
+ embedded: options.embedded,
+
+ manifest: Object.assign({
+ "permissions": ["tabs"],
+ }, options.manifest),
+
+ files: {
+ "options.html": `<!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <script src="options.js" type="text/javascript"></script>
+ </head>
+ </html>`,
+
+ "options.js": function() {
+ window.iAmOption = true;
+ browser.runtime.sendMessage("options.html");
+ browser.runtime.onMessage.addListener((msg, sender, respond) => {
+ if (msg == "ping") {
+ respond("pong");
+ }
+ });
+ },
+ },
+
+ background: options.background,
+ });
+
+ yield extension.startup();
+
+ return extension;
+}
+
+add_tasks(function* test_inline_options(extraOptions) {
+ info(`Test options opened inline (${JSON.stringify(extraOptions)})`);
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ let extension = yield loadExtension(Object.assign({}, extraOptions, {
+ manifest: {
+ applications: {gecko: {id: "inline_options@tests.mozilla.org"}},
+ "options_ui": {
+ "page": "options.html",
+ },
+ },
+
+ background: async function() {
+ let _optionsPromise;
+ let awaitOptions = () => {
+ browser.test.assertFalse(_optionsPromise, "Should not be awaiting options already");
+
+ return new Promise(resolve => {
+ _optionsPromise = {resolve};
+ });
+ };
+
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ if (msg == "options.html") {
+ if (_optionsPromise) {
+ _optionsPromise.resolve(sender.tab);
+ _optionsPromise = null;
+ } else {
+ browser.test.fail("Saw unexpected options page load");
+ }
+ }
+ });
+
+ try {
+ let [firstTab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ browser.test.log("Open options page. Expect fresh load.");
+
+ let [, optionsTab] = await Promise.all([
+ browser.runtime.openOptionsPage(),
+ awaitOptions(),
+ ]);
+
+ browser.test.assertEq("about:addons", optionsTab.url, "Tab contains AddonManager");
+ browser.test.assertTrue(optionsTab.active, "Tab is active");
+ browser.test.assertTrue(optionsTab.id != firstTab.id, "Tab is a new tab");
+
+ browser.test.assertEq(0, browser.extension.getViews({type: "popup"}).length, "viewType is not popup");
+ browser.test.assertEq(1, browser.extension.getViews({type: "tab"}).length, "viewType is tab");
+ browser.test.assertEq(1, browser.extension.getViews({windowId: optionsTab.windowId}).length, "windowId matches");
+
+ let views = browser.extension.getViews();
+ browser.test.assertEq(2, views.length, "Expected the options page and the background page");
+ browser.test.assertTrue(views.includes(window), "One of the views is the background page");
+ browser.test.assertTrue(views.some(w => w.iAmOption), "One of the views is the options page");
+
+ browser.test.log("Switch tabs.");
+ await browser.tabs.update(firstTab.id, {active: true});
+
+ browser.test.log("Open options page again. Expect tab re-selected, no new load.");
+
+ await browser.runtime.openOptionsPage();
+ let [tab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ browser.test.assertEq(optionsTab.id, tab.id, "Tab is the same as the previous options tab");
+ browser.test.assertEq("about:addons", tab.url, "Tab contains AddonManager");
+
+ browser.test.log("Ping options page.");
+ let pong = await browser.runtime.sendMessage("ping");
+ browser.test.assertEq("pong", pong, "Got pong.");
+
+ browser.test.log("Remove options tab.");
+ await browser.tabs.remove(optionsTab.id);
+
+ browser.test.log("Open options page again. Expect fresh load.");
+ [, tab] = await Promise.all([
+ browser.runtime.openOptionsPage(),
+ awaitOptions(),
+ ]);
+ browser.test.assertEq("about:addons", tab.url, "Tab contains AddonManager");
+ browser.test.assertTrue(tab.active, "Tab is active");
+ browser.test.assertTrue(tab.id != optionsTab.id, "Tab is a new tab");
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("options-ui");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("options-ui");
+ }
+ },
+ }));
+
+ yield extension.awaitFinish("options-ui");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
+
+add_tasks(function* test_tab_options(extraOptions) {
+ info(`Test options opened in a tab (${JSON.stringify(extraOptions)})`);
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ let extension = yield loadExtension(Object.assign({}, extraOptions, {
+ manifest: {
+ applications: {gecko: {id: "tab_options@tests.mozilla.org"}},
+ "options_ui": {
+ "page": "options.html",
+ "open_in_tab": true,
+ },
+ },
+
+ background: async function() {
+ let _optionsPromise;
+ let awaitOptions = () => {
+ browser.test.assertFalse(_optionsPromise, "Should not be awaiting options already");
+
+ return new Promise(resolve => {
+ _optionsPromise = {resolve};
+ });
+ };
+
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ if (msg == "options.html") {
+ if (_optionsPromise) {
+ _optionsPromise.resolve(sender.tab);
+ _optionsPromise = null;
+ } else {
+ browser.test.fail("Saw unexpected options page load");
+ }
+ }
+ });
+
+ let optionsURL = browser.extension.getURL("options.html");
+
+ try {
+ let [firstTab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ browser.test.log("Open options page. Expect fresh load.");
+ let [, optionsTab] = await Promise.all([
+ browser.runtime.openOptionsPage(),
+ awaitOptions(),
+ ]);
+ browser.test.assertEq(optionsURL, optionsTab.url, "Tab contains options.html");
+ browser.test.assertTrue(optionsTab.active, "Tab is active");
+ browser.test.assertTrue(optionsTab.id != firstTab.id, "Tab is a new tab");
+
+ browser.test.assertEq(0, browser.extension.getViews({type: "popup"}).length, "viewType is not popup");
+ browser.test.assertEq(1, browser.extension.getViews({type: "tab"}).length, "viewType is tab");
+ browser.test.assertEq(1, browser.extension.getViews({windowId: optionsTab.windowId}).length, "windowId matches");
+
+ let views = browser.extension.getViews();
+ browser.test.assertEq(2, views.length, "Expected the options page and the background page");
+ browser.test.assertTrue(views.includes(window), "One of the views is the background page");
+ browser.test.assertTrue(views.some(w => w.iAmOption), "One of the views is the options page");
+
+ browser.test.log("Switch tabs.");
+ await browser.tabs.update(firstTab.id, {active: true});
+
+ browser.test.log("Open options page again. Expect tab re-selected, no new load.");
+
+ await browser.runtime.openOptionsPage();
+ let [tab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ browser.test.assertEq(optionsTab.id, tab.id, "Tab is the same as the previous options tab");
+ browser.test.assertEq(optionsURL, tab.url, "Tab contains options.html");
+
+ // Unfortunately, we can't currently do this, since onMessage doesn't
+ // currently support responses when there are multiple listeners.
+ //
+ // browser.test.log("Ping options page.");
+ // return new Promise(resolve => browser.runtime.sendMessage("ping", resolve));
+
+ browser.test.log("Remove options tab.");
+ await browser.tabs.remove(optionsTab.id);
+
+ browser.test.log("Open options page again. Expect fresh load.");
+ [, tab] = await Promise.all([
+ browser.runtime.openOptionsPage(),
+ awaitOptions(),
+ ]);
+ browser.test.assertEq(optionsURL, tab.url, "Tab contains options.html");
+ browser.test.assertTrue(tab.active, "Tab is active");
+ browser.test.assertTrue(tab.id != optionsTab.id, "Tab is a new tab");
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("options-ui-tab");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("options-ui-tab");
+ }
+ },
+ }));
+
+ yield extension.awaitFinish("options-ui-tab");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
+
+add_tasks(function* test_options_no_manifest(extraOptions) {
+ info(`Test with no manifest key (${JSON.stringify(extraOptions)})`);
+
+ let extension = yield loadExtension(Object.assign({}, extraOptions, {
+ manifest: {
+ applications: {gecko: {id: "no_options@tests.mozilla.org"}},
+ },
+
+ async background() {
+ browser.test.log("Try to open options page when not specified in the manifest.");
+
+ await browser.test.assertRejects(
+ browser.runtime.openOptionsPage(),
+ /No `options_ui` declared/,
+ "Expected error from openOptionsPage()");
+
+ browser.test.notifyPass("options-no-manifest");
+ },
+ }));
+
+ yield extension.awaitFinish("options-no-manifest");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage_uninstall.js b/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage_uninstall.js
new file mode 100644
index 000000000..0c123b70e
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_runtime_openOptionsPage_uninstall.js
@@ -0,0 +1,101 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* loadExtension(options) {
+ let extension = ExtensionTestUtils.loadExtension({
+ useAddonManager: "temporary",
+
+ manifest: Object.assign({
+ "permissions": ["tabs"],
+ }, options.manifest),
+
+ files: {
+ "options.html": `<!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <script src="options.js" type="text/javascript"></script>
+ </head>
+ </html>`,
+
+ "options.js": function() {
+ browser.runtime.sendMessage("options.html");
+ browser.runtime.onMessage.addListener((msg, sender, respond) => {
+ if (msg == "ping") {
+ respond("pong");
+ }
+ });
+ },
+ },
+
+ background: options.background,
+ });
+
+ yield extension.startup();
+
+ return extension;
+}
+
+add_task(function* test_inline_options_uninstall() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ let extension = yield loadExtension({
+ manifest: {
+ applications: {gecko: {id: "inline_options_uninstall@tests.mozilla.org"}},
+ "options_ui": {
+ "page": "options.html",
+ },
+ },
+
+ background: async function() {
+ let _optionsPromise;
+ let awaitOptions = () => {
+ browser.test.assertFalse(_optionsPromise, "Should not be awaiting options already");
+
+ return new Promise(resolve => {
+ _optionsPromise = {resolve};
+ });
+ };
+
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ if (msg == "options.html") {
+ if (_optionsPromise) {
+ _optionsPromise.resolve(sender.tab);
+ _optionsPromise = null;
+ } else {
+ browser.test.fail("Saw unexpected options page load");
+ }
+ }
+ });
+
+ try {
+ let [firstTab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ browser.test.log("Open options page. Expect fresh load.");
+ let [, tab] = await Promise.all([
+ browser.runtime.openOptionsPage(),
+ awaitOptions(),
+ ]);
+
+ browser.test.assertEq("about:addons", tab.url, "Tab contains AddonManager");
+ browser.test.assertTrue(tab.active, "Tab is active");
+ browser.test.assertTrue(tab.id != firstTab.id, "Tab is a new tab");
+
+ browser.test.sendMessage("options-ui-open");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ }
+ },
+ });
+
+ yield extension.awaitMessage("options-ui-open");
+ yield extension.unload();
+
+ is(gBrowser.selectedBrowser.currentURI.spec, "about:addons",
+ "Add-on manager tab should still be open");
+
+ yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_runtime_setUninstallURL.js b/browser/components/extensions/test/browser/browser_ext_runtime_setUninstallURL.js
new file mode 100644
index 000000000..1c7ef4969
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_runtime_setUninstallURL.js
@@ -0,0 +1,94 @@
+"use strict";
+
+let {AddonManager} = Components.utils.import("resource://gre/modules/AddonManager.jsm", {});
+let {Extension} = Components.utils.import("resource://gre/modules/Extension.jsm", {});
+
+function* makeAndInstallXPI(id, backgroundScript, loadedURL) {
+ let xpi = Extension.generateXPI({
+ manifest: {applications: {gecko: {id}}},
+ background: backgroundScript,
+ });
+ SimpleTest.registerCleanupFunction(function cleanupXPI() {
+ Services.obs.notifyObservers(xpi, "flush-cache-entry", null);
+ xpi.remove(false);
+ });
+
+ let loadPromise = BrowserTestUtils.waitForNewTab(gBrowser, loadedURL);
+
+
+ info(`installing ${xpi.path}`);
+ let addon = yield AddonManager.installTemporaryAddon(xpi);
+ info("installed");
+
+ // A WebExtension is started asynchronously, we have our test extension
+ // open a new tab to signal that the background script has executed.
+ let loadTab = yield loadPromise;
+ yield BrowserTestUtils.removeTab(loadTab);
+
+ return addon;
+}
+
+
+add_task(function* test_setuninstallurl_badargs() {
+ async function background() {
+ await browser.test.assertRejects(
+ browser.runtime.setUninstallURL("this is not a url"),
+ /Invalid URL/,
+ "setUninstallURL with an invalid URL should fail");
+
+ await browser.test.assertRejects(
+ browser.runtime.setUninstallURL("file:///etc/passwd"),
+ /must have the scheme http or https/,
+ "setUninstallURL with an illegal URL should fail");
+
+ browser.test.notifyPass("setUninstallURL bad params");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background,
+ });
+ yield extension.startup();
+ yield extension.awaitFinish();
+ yield extension.unload();
+});
+
+// Test the documented behavior of setUninstallURL() that passing an
+// empty string is equivalent to not setting an uninstall URL
+// (i.e., no new tab is opened upon uninstall)
+add_task(function* test_setuninstall_empty_url() {
+ async function backgroundScript() {
+ await browser.runtime.setUninstallURL("");
+ browser.tabs.create({url: "http://example.com/addon_loaded"});
+ }
+
+ let addon = yield makeAndInstallXPI("test_uinstallurl2@tests.mozilla.org",
+ backgroundScript,
+ "http://example.com/addon_loaded");
+
+ addon.uninstall(true);
+ info("uninstalled");
+
+ // no need to explicitly check for the absence of a new tab,
+ // BrowserTestUtils will eventually complain if one is opened.
+});
+
+add_task(function* test_setuninstallurl() {
+ async function backgroundScript() {
+ await browser.runtime.setUninstallURL("http://example.com/addon_uninstalled");
+ browser.tabs.create({url: "http://example.com/addon_loaded"});
+ }
+
+ let addon = yield makeAndInstallXPI("test_uinstallurl@tests.mozilla.org",
+ backgroundScript,
+ "http://example.com/addon_loaded");
+
+ // look for a new tab with the uninstall url.
+ let uninstallPromise = BrowserTestUtils.waitForNewTab(gBrowser, "http://example.com/addon_uninstalled");
+
+ addon.uninstall(true);
+ info("uninstalled");
+
+ let uninstalledTab = yield uninstallPromise;
+ isnot(uninstalledTab, null, "opened tab with uninstall url");
+ yield BrowserTestUtils.removeTab(uninstalledTab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed.js b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed.js
new file mode 100644
index 000000000..413f7bde6
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed.js
@@ -0,0 +1,97 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* globals recordInitialTimestamps, onlyNewItemsFilter, checkRecentlyClosed */
+
+requestLongerTimeout(2);
+
+Services.scriptloader.loadSubScript(new URL("head_sessions.js", gTestPath).href,
+ this);
+
+add_task(function* test_sessions_get_recently_closed() {
+ function* openAndCloseWindow(url = "http://example.com", tabUrls) {
+ let win = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, url);
+ yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+ if (tabUrls) {
+ for (let url of tabUrls) {
+ yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
+ }
+ }
+ yield BrowserTestUtils.closeWindow(win);
+ }
+
+ function background() {
+ Promise.all([
+ browser.sessions.getRecentlyClosed(),
+ browser.tabs.query({active: true, currentWindow: true}),
+ ]).then(([recentlyClosed, tabs]) => {
+ browser.test.sendMessage("initialData", {recentlyClosed, currentWindowId: tabs[0].windowId});
+ });
+
+ browser.test.onMessage.addListener((msg, filter) => {
+ if (msg == "check-sessions") {
+ browser.sessions.getRecentlyClosed(filter).then(recentlyClosed => {
+ browser.test.sendMessage("recentlyClosed", recentlyClosed);
+ });
+ }
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["sessions", "tabs"],
+ },
+ background,
+ });
+
+ // Open and close a window that will be ignored, to prove that we are removing previous entries
+ yield openAndCloseWindow();
+
+ yield extension.startup();
+
+ let {recentlyClosed, currentWindowId} = yield extension.awaitMessage("initialData");
+ recordInitialTimestamps(recentlyClosed.map(item => item.lastModified));
+
+ yield openAndCloseWindow();
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ checkRecentlyClosed(recentlyClosed.filter(onlyNewItemsFilter), 1, currentWindowId);
+
+ yield openAndCloseWindow("about:config", ["about:robots", "about:mozilla"]);
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ // Check for multiple tabs in most recently closed window
+ is(recentlyClosed[0].window.tabs.length, 3, "most recently closed window has the expected number of tabs");
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com");
+ yield BrowserTestUtils.removeTab(tab);
+
+ tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com");
+ yield BrowserTestUtils.removeTab(tab);
+
+ yield openAndCloseWindow();
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ let finalResult = recentlyClosed.filter(onlyNewItemsFilter);
+ checkRecentlyClosed(finalResult, 5, currentWindowId);
+
+ isnot(finalResult[0].window, undefined, "first item is a window");
+ is(finalResult[0].tab, undefined, "first item is not a tab");
+ isnot(finalResult[1].tab, undefined, "second item is a tab");
+ is(finalResult[1].window, undefined, "second item is not a window");
+ isnot(finalResult[2].tab, undefined, "third item is a tab");
+ is(finalResult[2].window, undefined, "third item is not a window");
+ isnot(finalResult[3].window, undefined, "fourth item is a window");
+ is(finalResult[3].tab, undefined, "fourth item is not a tab");
+ isnot(finalResult[4].window, undefined, "fifth item is a window");
+ is(finalResult[4].tab, undefined, "fifth item is not a tab");
+
+ // test with filter
+ extension.sendMessage("check-sessions", {maxResults: 2});
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ checkRecentlyClosed(recentlyClosed.filter(onlyNewItemsFilter), 2, currentWindowId);
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_private.js b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_private.js
new file mode 100644
index 000000000..217c8e130
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_private.js
@@ -0,0 +1,61 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* globals recordInitialTimestamps, onlyNewItemsFilter, checkRecentlyClosed */
+
+SimpleTest.requestCompleteLog();
+
+Services.scriptloader.loadSubScript(new URL("head_sessions.js", gTestPath).href,
+ this);
+
+add_task(function* test_sessions_get_recently_closed_private() {
+ function background() {
+ browser.test.onMessage.addListener((msg, filter) => {
+ if (msg == "check-sessions") {
+ browser.sessions.getRecentlyClosed(filter).then(recentlyClosed => {
+ browser.test.sendMessage("recentlyClosed", recentlyClosed);
+ });
+ }
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["sessions", "tabs"],
+ },
+ background,
+ });
+
+ // Open a private browsing window.
+ let privateWin = yield BrowserTestUtils.openNewBrowserWindow({private: true});
+
+ yield extension.startup();
+
+ let {Management: {global: {WindowManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+ let privateWinId = WindowManager.getId(privateWin);
+
+ extension.sendMessage("check-sessions");
+ let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ recordInitialTimestamps(recentlyClosed.map(item => item.lastModified));
+
+ // Open and close two tabs in the private window
+ let tab = yield BrowserTestUtils.openNewForegroundTab(privateWin.gBrowser, "http://example.com");
+ yield BrowserTestUtils.removeTab(tab);
+
+ tab = yield BrowserTestUtils.openNewForegroundTab(privateWin.gBrowser, "http://example.com");
+ yield BrowserTestUtils.removeTab(tab);
+
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ checkRecentlyClosed(recentlyClosed.filter(onlyNewItemsFilter), 2, privateWinId, true);
+
+ // Close the private window.
+ yield BrowserTestUtils.closeWindow(privateWin);
+
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ is(recentlyClosed.filter(onlyNewItemsFilter).length, 0, "the closed private window info was not found in recently closed data");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js
new file mode 100644
index 000000000..ae0daff9a
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js
@@ -0,0 +1,96 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function expectedTabInfo(tab, window) {
+ let browser = tab.linkedBrowser;
+ return {
+ url: browser.currentURI.spec,
+ title: browser.contentTitle,
+ favIconUrl: window.gBrowser.getIcon(tab),
+ };
+}
+
+function checkTabInfo(expected, actual) {
+ for (let prop in expected) {
+ is(actual[prop], expected[prop], `Expected value found for ${prop} of tab object.`);
+ }
+}
+
+add_task(async function test_sessions_get_recently_closed_tabs() {
+ async function background() {
+ browser.test.onMessage.addListener(async msg => {
+ if (msg == "check-sessions") {
+ let recentlyClosed = await browser.sessions.getRecentlyClosed();
+ browser.test.sendMessage("recentlyClosed", recentlyClosed);
+ }
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["sessions", "tabs"],
+ },
+ background,
+ });
+
+ let win = await BrowserTestUtils.openNewBrowserWindow();
+ await BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "about:addons");
+ await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+ let expectedTabs = [];
+ let tab = win.gBrowser.selectedTab;
+ expectedTabs.push(expectedTabInfo(tab, win));
+
+ for (let url of ["about:robots", "about:mozilla"]) {
+ tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
+ expectedTabs.push(expectedTabInfo(tab, win));
+ }
+
+ await extension.startup();
+
+ // Test with a closed tab.
+ await BrowserTestUtils.removeTab(tab);
+
+ extension.sendMessage("check-sessions");
+ let recentlyClosed = await extension.awaitMessage("recentlyClosed");
+ let tabInfo = recentlyClosed[0].tab;
+ let expectedTab = expectedTabs.pop();
+ checkTabInfo(expectedTab, tabInfo);
+
+ // Test with a closed window containing tabs.
+ await BrowserTestUtils.closeWindow(win);
+
+ extension.sendMessage("check-sessions");
+ recentlyClosed = await extension.awaitMessage("recentlyClosed");
+ let tabInfos = recentlyClosed[0].window.tabs;
+ is(tabInfos.length, 2, "Expected number of tabs in closed window.");
+ for (let x = 0; x < tabInfos.length; x++) {
+ checkTabInfo(expectedTabs[x], tabInfos[x]);
+ }
+
+ await extension.unload();
+
+ // Test without tabs permission.
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["sessions"],
+ },
+ background,
+ });
+
+ await extension.startup();
+
+ extension.sendMessage("check-sessions");
+ recentlyClosed = await extension.awaitMessage("recentlyClosed");
+ tabInfos = recentlyClosed[0].window.tabs;
+ is(tabInfos.length, 2, "Expected number of tabs in closed window.");
+ for (let tabInfo of tabInfos) {
+ for (let prop in expectedTabs[0]) {
+ is(undefined,
+ tabInfo[prop],
+ `${prop} of tab object is undefined without tabs permission.`);
+ }
+ }
+
+ await extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_sessions_restore.js b/browser/components/extensions/test/browser/browser_ext_sessions_restore.js
new file mode 100644
index 000000000..6f1c6cf9a
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_restore.js
@@ -0,0 +1,134 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+SimpleTest.requestCompleteLog();
+
+XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
+ "resource:///modules/sessionstore/SessionStore.jsm");
+
+add_task(function* test_sessions_restore() {
+ function background() {
+ browser.test.onMessage.addListener((msg, data) => {
+ if (msg == "check-sessions") {
+ browser.sessions.getRecentlyClosed().then(recentlyClosed => {
+ browser.test.sendMessage("recentlyClosed", recentlyClosed);
+ });
+ } else if (msg == "restore") {
+ browser.sessions.restore(data).then(sessions => {
+ browser.test.sendMessage("restored", sessions);
+ });
+ } else if (msg == "restore-reject") {
+ browser.sessions.restore("not-a-valid-session-id").then(
+ sessions => {
+ browser.test.fail("restore rejected with an invalid sessionId");
+ },
+ error => {
+ browser.test.assertTrue(
+ error.message.includes("Could not restore object using sessionId not-a-valid-session-id."));
+ browser.test.sendMessage("restore-rejected");
+ }
+ );
+ }
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["sessions", "tabs"],
+ },
+ background,
+ });
+
+ yield extension.startup();
+
+ let {Management: {global: {WindowManager, TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ function checkLocalTab(tab, expectedUrl) {
+ let realTab = TabManager.getTab(tab.id);
+ let tabState = JSON.parse(SessionStore.getTabState(realTab));
+ is(tabState.entries[0].url, expectedUrl, "restored tab has the expected url");
+ }
+
+ let win = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "about:config");
+ yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+ for (let url of ["about:robots", "about:mozilla"]) {
+ yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);
+ }
+ yield BrowserTestUtils.closeWindow(win);
+
+ extension.sendMessage("check-sessions");
+ let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+
+ // Check that our expected window is the most recently closed.
+ is(recentlyClosed[0].window.tabs.length, 3, "most recently closed window has the expected number of tabs");
+
+ // Restore the window.
+ extension.sendMessage("restore");
+ let restored = yield extension.awaitMessage("restored");
+
+ is(restored.length, 1, "restore returned the expected number of sessions");
+ is(restored[0].window.tabs.length, 3, "restore returned a window with the expected number of tabs");
+ checkLocalTab(restored[0].window.tabs[0], "about:config");
+ checkLocalTab(restored[0].window.tabs[1], "about:robots");
+ checkLocalTab(restored[0].window.tabs[2], "about:mozilla");
+
+ // Close the window again.
+ let window = WindowManager.getWindow(restored[0].window.id);
+ yield BrowserTestUtils.closeWindow(window);
+
+ // Restore the window using the sessionId.
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ extension.sendMessage("restore", recentlyClosed[0].window.sessionId);
+ restored = yield extension.awaitMessage("restored");
+
+ is(restored.length, 1, "restore returned the expected number of sessions");
+ is(restored[0].window.tabs.length, 3, "restore returned a window with the expected number of tabs");
+ checkLocalTab(restored[0].window.tabs[0], "about:config");
+ checkLocalTab(restored[0].window.tabs[1], "about:robots");
+ checkLocalTab(restored[0].window.tabs[2], "about:mozilla");
+
+ // Close the window again.
+ window = WindowManager.getWindow(restored[0].window.id);
+ yield BrowserTestUtils.closeWindow(window);
+
+ // Open and close a tab.
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+ yield BrowserTestUtils.removeTab(tab);
+
+ // Restore the most recently closed item.
+ extension.sendMessage("restore");
+ restored = yield extension.awaitMessage("restored");
+
+ is(restored.length, 1, "restore returned the expected number of sessions");
+ tab = restored[0].tab;
+ ok(tab, "restore returned a tab");
+ checkLocalTab(tab, "about:robots");
+
+ // Close the tab again.
+ let realTab = TabManager.getTab(tab.id);
+ yield BrowserTestUtils.removeTab(realTab);
+
+ // Restore the tab using the sessionId.
+ extension.sendMessage("check-sessions");
+ recentlyClosed = yield extension.awaitMessage("recentlyClosed");
+ extension.sendMessage("restore", recentlyClosed[0].tab.sessionId);
+ restored = yield extension.awaitMessage("restored");
+
+ is(restored.length, 1, "restore returned the expected number of sessions");
+ tab = restored[0].tab;
+ ok(tab, "restore returned a tab");
+ checkLocalTab(tab, "about:robots");
+
+ // Close the tab again.
+ realTab = TabManager.getTab(tab.id);
+ yield BrowserTestUtils.removeTab(realTab);
+
+ // Try to restore something with an invalid sessionId.
+ extension.sendMessage("restore-reject");
+ restored = yield extension.awaitMessage("restore-rejected");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_simple.js b/browser/components/extensions/test/browser/browser_ext_simple.js
new file mode 100644
index 000000000..ffa00c9db
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_simple.js
@@ -0,0 +1,57 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_simple() {
+ let extensionData = {
+ manifest: {
+ "name": "Simple extension test",
+ "version": "1.0",
+ "manifest_version": 2,
+ "description": "",
+ },
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+ info("load complete");
+ yield extension.startup();
+ info("startup complete");
+ yield extension.unload();
+ info("extension unloaded successfully");
+});
+
+add_task(function* test_background() {
+ function backgroundScript() {
+ browser.test.log("running background script");
+
+ browser.test.onMessage.addListener((x, y) => {
+ browser.test.assertEq(x, 10, "x is 10");
+ browser.test.assertEq(y, 20, "y is 20");
+
+ browser.test.notifyPass("background test passed");
+ });
+
+ browser.test.sendMessage("running", 1);
+ }
+
+ let extensionData = {
+ background: "(" + backgroundScript.toString() + ")()",
+ manifest: {
+ "name": "Simple extension test",
+ "version": "1.0",
+ "manifest_version": 2,
+ "description": "",
+ },
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+ info("load complete");
+ let [, x] = yield Promise.all([extension.startup(), extension.awaitMessage("running")]);
+ is(x, 1, "got correct value from extension");
+ info("startup complete");
+ extension.sendMessage(10, 20);
+ yield extension.awaitFinish();
+ info("test complete");
+ yield extension.unload();
+ info("extension unloaded successfully");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tab_runtimeConnect.js b/browser/components/extensions/test/browser/browser_ext_tab_runtimeConnect.js
new file mode 100644
index 000000000..a5541a002
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tab_runtimeConnect.js
@@ -0,0 +1,74 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ let messages_received = [];
+
+ let tabId;
+
+ browser.runtime.onConnect.addListener((port) => {
+ browser.test.assertTrue(!!port, "tab to background port received");
+ browser.test.assertEq("tab-connection-name", port.name, "port name should be defined and equal to connectInfo.name");
+ browser.test.assertTrue(!!port.sender.tab, "port.sender.tab should be defined");
+ browser.test.assertEq(tabId, port.sender.tab.id, "port.sender.tab.id should be equal to the expected tabId");
+
+ port.onMessage.addListener((msg) => {
+ messages_received.push(msg);
+
+ if (messages_received.length == 1) {
+ browser.test.assertEq("tab to background port message", msg, "'tab to background' port message received");
+ port.postMessage("background to tab port message");
+ }
+
+ if (messages_received.length == 2) {
+ browser.test.assertTrue(!!msg.tabReceived, "'background to tab' reply port message received");
+ browser.test.assertEq("background to tab port message", msg.tabReceived, "reply port content contains the message received");
+
+ browser.test.notifyPass("tabRuntimeConnect.pass");
+ }
+ });
+ });
+
+ browser.tabs.create({url: "tab.html"},
+ (tab) => { tabId = tab.id; });
+ },
+
+ files: {
+ "tab.js": function() {
+ let port = browser.runtime.connect({name: "tab-connection-name"});
+ port.postMessage("tab to background port message");
+ port.onMessage.addListener((msg) => {
+ port.postMessage({tabReceived: msg});
+ });
+ },
+ "tab.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>test tab extension page</title>
+ <meta charset="utf-8">
+ <script src="tab.js" async></script>
+ </head>
+ <body>
+ <h1>test tab extension page</h1>
+ </body>
+ </html>
+ `,
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabRuntimeConnect.pass");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_audio.js b/browser/components/extensions/test/browser/browser_ext_tabs_audio.js
new file mode 100644
index 000000000..f9f6956d4
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_audio.js
@@ -0,0 +1,203 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank?1");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank?2");
+
+ gBrowser.selectedTab = tab1;
+
+ async function background() {
+ function promiseUpdated(tabId, attr) {
+ return new Promise(resolve => {
+ let onUpdated = (tabId_, changeInfo, tab) => {
+ if (tabId == tabId_ && attr in changeInfo) {
+ browser.tabs.onUpdated.removeListener(onUpdated);
+
+ resolve({changeInfo, tab});
+ }
+ };
+ browser.tabs.onUpdated.addListener(onUpdated);
+ });
+ }
+
+ let deferred = {};
+ browser.test.onMessage.addListener((message, tabId, result) => {
+ if (message == "change-tab-done" && deferred[tabId]) {
+ deferred[tabId].resolve(result);
+ }
+ });
+
+ function changeTab(tabId, attr, on) {
+ return new Promise((resolve, reject) => {
+ deferred[tabId] = {resolve, reject};
+ browser.test.sendMessage("change-tab", tabId, attr, on);
+ });
+ }
+
+
+ try {
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+ browser.test.assertEq(tabs.length, 3, "We have three tabs");
+
+ for (let tab of tabs) {
+ // Note: We want to check that these are actual boolean values, not
+ // just that they evaluate as false.
+ browser.test.assertEq(false, tab.mutedInfo.muted, "Tab is not muted");
+ browser.test.assertEq(undefined, tab.mutedInfo.reason, "Tab has no muted info reason");
+ browser.test.assertEq(false, tab.audible, "Tab is not audible");
+ }
+
+ let windowId = tabs[0].windowId;
+ let tabIds = [tabs[1].id, tabs[2].id];
+
+ browser.test.log("Test initial queries for muted and audible return no tabs");
+ let silent = await browser.tabs.query({windowId, audible: false});
+ let audible = await browser.tabs.query({windowId, audible: true});
+ let muted = await browser.tabs.query({windowId, muted: true});
+ let nonMuted = await browser.tabs.query({windowId, muted: false});
+
+ browser.test.assertEq(3, silent.length, "Three silent tabs");
+ browser.test.assertEq(0, audible.length, "No audible tabs");
+
+ browser.test.assertEq(0, muted.length, "No muted tabs");
+ browser.test.assertEq(3, nonMuted.length, "Three non-muted tabs");
+
+ browser.test.log("Toggle muted and audible externally on one tab each, and check results");
+ [muted, audible] = await Promise.all([
+ promiseUpdated(tabIds[0], "mutedInfo"),
+ promiseUpdated(tabIds[1], "audible"),
+ changeTab(tabIds[0], "muted", true),
+ changeTab(tabIds[1], "audible", true),
+ ]);
+
+ for (let obj of [muted.changeInfo, muted.tab]) {
+ browser.test.assertEq(true, obj.mutedInfo.muted, "Tab is muted");
+ browser.test.assertEq("user", obj.mutedInfo.reason, "Tab was muted by the user");
+ }
+
+ browser.test.assertEq(true, audible.changeInfo.audible, "Tab audible state changed");
+ browser.test.assertEq(true, audible.tab.audible, "Tab is audible");
+
+ browser.test.log("Re-check queries. Expect one audible and one muted tab");
+ silent = await browser.tabs.query({windowId, audible: false});
+ audible = await browser.tabs.query({windowId, audible: true});
+ muted = await browser.tabs.query({windowId, muted: true});
+ nonMuted = await browser.tabs.query({windowId, muted: false});
+
+ browser.test.assertEq(2, silent.length, "Two silent tabs");
+ browser.test.assertEq(1, audible.length, "One audible tab");
+
+ browser.test.assertEq(1, muted.length, "One muted tab");
+ browser.test.assertEq(2, nonMuted.length, "Two non-muted tabs");
+
+ browser.test.assertEq(true, muted[0].mutedInfo.muted, "Tab is muted");
+ browser.test.assertEq("user", muted[0].mutedInfo.reason, "Tab was muted by the user");
+
+ browser.test.assertEq(true, audible[0].audible, "Tab is audible");
+
+ browser.test.log("Toggle muted internally on two tabs, and check results");
+ [nonMuted, muted] = await Promise.all([
+ promiseUpdated(tabIds[0], "mutedInfo"),
+ promiseUpdated(tabIds[1], "mutedInfo"),
+ browser.tabs.update(tabIds[0], {muted: false}),
+ browser.tabs.update(tabIds[1], {muted: true}),
+ ]);
+
+ for (let obj of [nonMuted.changeInfo, nonMuted.tab]) {
+ browser.test.assertEq(false, obj.mutedInfo.muted, "Tab is not muted");
+ }
+ for (let obj of [muted.changeInfo, muted.tab]) {
+ browser.test.assertEq(true, obj.mutedInfo.muted, "Tab is muted");
+ }
+
+ for (let obj of [nonMuted.changeInfo, nonMuted.tab, muted.changeInfo, muted.tab]) {
+ browser.test.assertEq("extension", obj.mutedInfo.reason, "Mute state changed by extension");
+
+ // FIXME: browser.runtime.id is currently broken.
+ browser.test.assertEq(browser.i18n.getMessage("@@extension_id"),
+ obj.mutedInfo.extensionId,
+ "Mute state changed by extension");
+ }
+
+ browser.test.log("Test that mutedInfo is preserved by sessionstore");
+ let tab = await changeTab(tabIds[1], "duplicate").then(browser.tabs.get);
+
+ browser.test.assertEq(true, tab.mutedInfo.muted, "Tab is muted");
+
+ browser.test.assertEq("extension", tab.mutedInfo.reason, "Mute state changed by extension");
+
+ // FIXME: browser.runtime.id is currently broken.
+ browser.test.assertEq(browser.i18n.getMessage("@@extension_id"),
+ tab.mutedInfo.extensionId,
+ "Mute state changed by extension");
+
+ browser.test.log("Unmute externally, and check results");
+ [nonMuted] = await Promise.all([
+ promiseUpdated(tabIds[1], "mutedInfo"),
+ changeTab(tabIds[1], "muted", false),
+ browser.tabs.remove(tab.id),
+ ]);
+
+ for (let obj of [nonMuted.changeInfo, nonMuted.tab]) {
+ browser.test.assertEq(false, obj.mutedInfo.muted, "Tab is not muted");
+ browser.test.assertEq("user", obj.mutedInfo.reason, "Mute state changed by user");
+ }
+
+ browser.test.notifyPass("tab-audio");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("tab-audio");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ extension.onMessage("change-tab", (tabId, attr, on) => {
+ let {Management: {global: {TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let tab = TabManager.getTab(tabId);
+
+ if (attr == "muted") {
+ // Ideally we'd simulate a click on the tab audio icon for this, but the
+ // handler relies on CSS :hover states, which are complicated and fragile
+ // to simulate.
+ if (tab.muted != on) {
+ tab.toggleMuteAudio();
+ }
+ } else if (attr == "audible") {
+ let browser = tab.linkedBrowser;
+ if (on) {
+ browser.audioPlaybackStarted();
+ } else {
+ browser.audioPlaybackStopped();
+ }
+ } else if (attr == "duplicate") {
+ // This is a bit of a hack. It won't be necessary once we have
+ // `tabs.duplicate`.
+ let newTab = gBrowser.duplicateTab(tab);
+ BrowserTestUtils.waitForEvent(newTab, "SSTabRestored", () => true).then(() => {
+ extension.sendMessage("change-tab-done", tabId, TabManager.getId(newTab));
+ });
+ return;
+ }
+
+ extension.sendMessage("change-tab-done", tabId);
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("tab-audio");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_captureVisibleTab.js b/browser/components/extensions/test/browser/browser_ext_tabs_captureVisibleTab.js
new file mode 100644
index 000000000..1491a19ab
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_captureVisibleTab.js
@@ -0,0 +1,155 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* runTest(options) {
+ options.neutral = [0xaa, 0xaa, 0xaa];
+
+ let html = `
+ <!DOCTYPE html>
+ <html lang="en">
+ <head><meta charset="UTF-8"></head>
+ <body style="background-color: rgb(${options.color})">
+ <!-- Fill most of the image with a neutral color to test edge-to-edge scaling. -->
+ <div style="position: absolute;
+ left: 2px;
+ right: 2px;
+ top: 2px;
+ bottom: 2px;
+ background: rgb(${options.neutral});"></div>
+ </body>
+ </html>
+ `;
+
+ let url = `data:text/html,${encodeURIComponent(html)}`;
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, url, true);
+
+ tab.linkedBrowser.fullZoom = options.fullZoom;
+
+ async function background(options) {
+ browser.test.log(`Test color ${options.color} at fullZoom=${options.fullZoom}`);
+
+ try {
+ let [tab] = await browser.tabs.query({currentWindow: true, active: true});
+
+ let [jpeg, png, ...pngs] = await Promise.all([
+ browser.tabs.captureVisibleTab(tab.windowId, {format: "jpeg", quality: 95}),
+ browser.tabs.captureVisibleTab(tab.windowId, {format: "png", quality: 95}),
+ browser.tabs.captureVisibleTab(tab.windowId, {quality: 95}),
+ browser.tabs.captureVisibleTab(tab.windowId),
+ ]);
+
+ browser.test.assertTrue(pngs.every(url => url == png), "All PNGs are identical");
+
+ browser.test.assertTrue(jpeg.startsWith("data:image/jpeg;base64,"), "jpeg is JPEG");
+ browser.test.assertTrue(png.startsWith("data:image/png;base64,"), "png is PNG");
+
+ let promises = [jpeg, png].map(url => new Promise(resolve => {
+ let img = new Image();
+ img.src = url;
+ img.onload = () => resolve(img);
+ }));
+
+ [jpeg, png] = await Promise.all(promises);
+ let tabDims = `${tab.width}\u00d7${tab.height}`;
+
+ let images = {jpeg, png};
+ for (let format of Object.keys(images)) {
+ let img = images[format];
+
+ let dims = `${img.width}\u00d7${img.height}`;
+ browser.test.assertEq(tabDims, dims, `${format} dimensions are correct`);
+
+ let canvas = document.createElement("canvas");
+ canvas.width = img.width;
+ canvas.height = img.height;
+ canvas.mozOpaque = true;
+
+ let ctx = canvas.getContext("2d");
+ ctx.drawImage(img, 0, 0);
+
+ // Check the colors of the first and last pixels of the image, to make
+ // sure we capture the entire frame, and scale it correctly.
+ let coords = [
+ {x: 0, y: 0,
+ color: options.color},
+ {x: img.width - 1,
+ y: img.height - 1,
+ color: options.color},
+ {x: img.width / 2 | 0,
+ y: img.height / 2 | 0,
+ color: options.neutral},
+ ];
+
+ for (let {x, y, color} of coords) {
+ let imageData = ctx.getImageData(x, y, 1, 1).data;
+
+ if (format == "png") {
+ browser.test.assertEq(`rgba(${color},255)`, `rgba(${[...imageData]})`, `${format} image color is correct at (${x}, ${y})`);
+ } else {
+ // Allow for some deviation in JPEG version due to lossy compression.
+ const SLOP = 3;
+
+ browser.test.log(`Testing ${format} image color at (${x}, ${y}), have rgba(${[...imageData]}), expecting approx. rgba(${color},255)`);
+
+ browser.test.assertTrue(Math.abs(color[0] - imageData[0]) <= SLOP, `${format} image color.red is correct at (${x}, ${y})`);
+ browser.test.assertTrue(Math.abs(color[1] - imageData[1]) <= SLOP, `${format} image color.green is correct at (${x}, ${y})`);
+ browser.test.assertTrue(Math.abs(color[2] - imageData[2]) <= SLOP, `${format} image color.blue is correct at (${x}, ${y})`);
+ browser.test.assertEq(255, imageData[3], `${format} image color.alpha is correct at (${x}, ${y})`);
+ }
+ }
+ }
+
+ browser.test.notifyPass("captureVisibleTab");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("captureVisibleTab");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["<all_urls>"],
+ },
+
+ background: `(${background})(${JSON.stringify(options)})`,
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("captureVisibleTab");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+}
+
+add_task(function* testCaptureVisibleTab() {
+ yield runTest({color: [0, 0, 0], fullZoom: 1});
+
+ yield runTest({color: [0, 0, 0], fullZoom: 2});
+
+ yield runTest({color: [0, 0, 0], fullZoom: 0.5});
+
+ yield runTest({color: [255, 255, 255], fullZoom: 1});
+});
+
+add_task(function* testCaptureVisibleTabPermissions() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background() {
+ browser.test.assertFalse("captureVisibleTab" in browser.tabs,
+ 'Extension without "<all_tabs>" permission should not have access to captureVisibleTab');
+ browser.test.notifyPass("captureVisibleTabPermissions");
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("captureVisibleTabPermissions");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_cookieStoreId.js b/browser/components/extensions/test/browser/browser_ext_tabs_cookieStoreId.js
new file mode 100644
index 000000000..dc0647e3c
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_cookieStoreId.js
@@ -0,0 +1,156 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* setup() {
+ // make sure userContext is enabled.
+ return SpecialPowers.pushPrefEnv({"set": [
+ ["privacy.userContext.enabled", true],
+ ]});
+});
+
+add_task(function* () {
+ info("Start testing tabs.create with cookieStoreId");
+
+ let testCases = [
+ // No private window
+ {privateTab: false, cookieStoreId: null, success: true, expectedCookieStoreId: "firefox-default"},
+ {privateTab: false, cookieStoreId: "firefox-default", success: true, expectedCookieStoreId: "firefox-default"},
+ {privateTab: false, cookieStoreId: "firefox-container-1", success: true, expectedCookieStoreId: "firefox-container-1"},
+ {privateTab: false, cookieStoreId: "firefox-container-2", success: true, expectedCookieStoreId: "firefox-container-2"},
+ {privateTab: false, cookieStoreId: "firefox-container-42", failure: "exist"},
+ {privateTab: false, cookieStoreId: "firefox-private", failure: "defaultToPrivate"},
+ {privateTab: false, cookieStoreId: "wow", failure: "illegal"},
+
+ // Private window
+ {privateTab: true, cookieStoreId: null, success: true, expectedCookieStoreId: "firefox-private"},
+ {privateTab: true, cookieStoreId: "firefox-private", success: true, expectedCookieStoreId: "firefox-private"},
+ {privateTab: true, cookieStoreId: "firefox-default", failure: "privateToDefault"},
+ {privateTab: true, cookieStoreId: "firefox-container-1", failure: "privateToDefault"},
+ {privateTab: true, cookieStoreId: "wow", failure: "illegal"},
+ ];
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs", "cookies"],
+ },
+
+ background: function() {
+ function testTab(data, tab) {
+ browser.test.assertTrue(data.success, "we want a success");
+ browser.test.assertTrue(!!tab, "we have a tab");
+ browser.test.assertEq(data.expectedCookieStoreId, tab.cookieStoreId, "tab should have the correct cookieStoreId");
+ }
+
+ async function runTest(data) {
+ try {
+ // Tab Creation
+ let tab;
+ try {
+ tab = await browser.tabs.create({
+ windowId: data.privateTab ? this.privateWindowId : this.defaultWindowId,
+ cookieStoreId: data.cookieStoreId,
+ });
+
+ browser.test.assertTrue(!data.failure, "we want a success");
+ } catch (error) {
+ browser.test.assertTrue(!!data.failure, "we want a failure");
+
+ if (data.failure == "illegal") {
+ browser.test.assertTrue(/Illegal cookieStoreId/.test(error.message),
+ "runtime.lastError should report the expected error message");
+ } else if (data.failure == "defaultToPrivate") {
+ browser.test.assertTrue("Illegal to set private cookieStorageId in a non private window",
+ error.message,
+ "runtime.lastError should report the expected error message");
+ } else if (data.failure == "privateToDefault") {
+ browser.test.assertTrue("Illegal to set non private cookieStorageId in a private window",
+ error.message,
+ "runtime.lastError should report the expected error message");
+ } else if (data.failure == "exist") {
+ browser.test.assertTrue(/No cookie store exists/.test(error.message),
+ "runtime.lastError should report the expected error message");
+ } else {
+ browser.test.fail("The test is broken");
+ }
+
+ browser.test.sendMessage("test-done");
+ return;
+ }
+
+ // Tests for tab creation
+ testTab(data, tab);
+
+ {
+ // Tests for tab querying
+ let [tab] = await browser.tabs.query({
+ windowId: data.privateTab ? this.privateWindowId : this.defaultWindowId,
+ cookieStoreId: data.cookieStoreId,
+ });
+
+ browser.test.assertTrue(tab != undefined, "Tab found!");
+ testTab(data, tab);
+ }
+
+ let stores = await browser.cookies.getAllCookieStores();
+
+ let store = stores.find(store => store.id === tab.cookieStoreId);
+ browser.test.assertTrue(!!store, "We have a store for this tab.");
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.sendMessage("test-done");
+ } catch (e) {
+ browser.test.fail("An exception has been thrown");
+ }
+ }
+
+ async function initialize() {
+ let win = await browser.windows.create({incognito: true});
+ this.privateWindowId = win.id;
+
+ win = await browser.windows.create({incognito: false});
+ this.defaultWindowId = win.id;
+
+ browser.test.sendMessage("ready");
+ }
+
+ async function shutdown() {
+ await browser.windows.remove(this.privateWindowId);
+ await browser.windows.remove(this.defaultWindowId);
+ browser.test.sendMessage("gone");
+ }
+
+ // Waiting for messages
+ browser.test.onMessage.addListener((msg, data) => {
+ if (msg == "be-ready") {
+ initialize();
+ } else if (msg == "test") {
+ runTest(data);
+ } else {
+ browser.test.assertTrue("finish", msg, "Shutting down");
+ shutdown();
+ }
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ info("Tests must be ready...");
+ extension.sendMessage("be-ready");
+ yield extension.awaitMessage("ready");
+ info("Tests are ready to run!");
+
+ for (let test of testCases) {
+ info(`test tab.create with cookieStoreId: "${test.cookieStoreId}"`);
+ extension.sendMessage("test", test);
+ yield extension.awaitMessage("test-done");
+ }
+
+ info("Waiting for shutting down...");
+ extension.sendMessage("finish");
+ yield extension.awaitMessage("gone");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_create.js b/browser/components/extensions/test/browser/browser_ext_tabs_create.js
new file mode 100644
index 000000000..8bc5a68a2
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_create.js
@@ -0,0 +1,166 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+ gBrowser.selectedTab = tab;
+
+ // TODO: Multiple windows.
+
+ // Using pre-loaded new tab pages interferes with onUpdated events.
+ // It probably shouldn't.
+ SpecialPowers.setBoolPref("browser.newtab.preload", false);
+ registerCleanupFunction(() => {
+ SpecialPowers.clearUserPref("browser.newtab.preload");
+ });
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "background": {"page": "bg/background.html"},
+ },
+
+ files: {
+ "bg/blank.html": `<html><head><meta charset="utf-8"></head></html>`,
+
+ "bg/background.html": `<html><head>
+ <meta charset="utf-8">
+ <script src="background.js"></script>
+ </head></html>`,
+
+ "bg/background.js": function() {
+ let activeTab;
+ let activeWindow;
+
+ function runTests() {
+ const DEFAULTS = {
+ index: 2,
+ windowId: activeWindow,
+ active: true,
+ pinned: false,
+ url: "about:newtab",
+ };
+
+ let tests = [
+ {
+ create: {url: "http://example.com/"},
+ result: {url: "http://example.com/"},
+ },
+ {
+ create: {url: "blank.html"},
+ result: {url: browser.runtime.getURL("bg/blank.html")},
+ },
+ {
+ create: {},
+ result: {url: "about:newtab"},
+ },
+ {
+ create: {active: false},
+ result: {active: false},
+ },
+ {
+ create: {active: true},
+ result: {active: true},
+ },
+ {
+ create: {pinned: true},
+ result: {pinned: true, index: 0},
+ },
+ {
+ create: {pinned: true, active: true},
+ result: {pinned: true, active: true, index: 0},
+ },
+ {
+ create: {pinned: true, active: false},
+ result: {pinned: true, active: false, index: 0},
+ },
+ {
+ create: {index: 1},
+ result: {index: 1},
+ },
+ {
+ create: {index: 1, active: false},
+ result: {index: 1, active: false},
+ },
+ {
+ create: {windowId: activeWindow},
+ result: {windowId: activeWindow},
+ },
+ ];
+
+ async function nextTest() {
+ if (!tests.length) {
+ browser.test.notifyPass("tabs.create");
+ return;
+ }
+
+ let test = tests.shift();
+ let expected = Object.assign({}, DEFAULTS, test.result);
+
+ browser.test.log(`Testing tabs.create(${JSON.stringify(test.create)}), expecting ${JSON.stringify(test.result)}`);
+
+ let updatedPromise = new Promise(resolve => {
+ let onUpdated = (changedTabId, changed) => {
+ if (changed.url) {
+ browser.tabs.onUpdated.removeListener(onUpdated);
+ resolve({tabId: changedTabId, url: changed.url});
+ }
+ };
+ browser.tabs.onUpdated.addListener(onUpdated);
+ });
+
+ let createdPromise = new Promise(resolve => {
+ let onCreated = tab => {
+ browser.test.assertTrue("id" in tab, `Expected tabs.onCreated callback to receive tab object`);
+ resolve();
+ };
+ browser.tabs.onCreated.addListener(onCreated);
+ });
+
+ let [tab] = await Promise.all([
+ browser.tabs.create(test.create),
+ createdPromise,
+ ]);
+ let tabId = tab.id;
+
+ for (let key of Object.keys(expected)) {
+ if (key === "url") {
+ // FIXME: This doesn't get updated until later in the load cycle.
+ continue;
+ }
+
+ browser.test.assertEq(expected[key], tab[key], `Expected value for tab.${key}`);
+ }
+
+ let updated = await updatedPromise;
+ browser.test.assertEq(tabId, updated.tabId, `Expected value for tab.id`);
+ browser.test.assertEq(expected.url, updated.url, `Expected value for tab.url`);
+
+ await browser.tabs.remove(tabId);
+ await browser.tabs.update(activeTab, {active: true});
+
+ nextTest();
+ }
+
+ nextTest();
+ }
+
+ browser.tabs.query({active: true, currentWindow: true}, tabs => {
+ activeTab = tabs[0].id;
+ activeWindow = tabs[0].windowId;
+
+ runTests();
+ });
+ },
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.create");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
+
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_create_invalid_url.js b/browser/components/extensions/test/browser/browser_ext_tabs_create_invalid_url.js
new file mode 100644
index 000000000..49938bf22
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_create_invalid_url.js
@@ -0,0 +1,66 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* testTabsCreateInvalidURL(tabsCreateURL) {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.test.sendMessage("ready");
+ browser.test.onMessage.addListener((msg, tabsCreateURL) => {
+ browser.tabs.create({url: tabsCreateURL}, (tab) => {
+ browser.test.assertEq(undefined, tab, "on error tab should be undefined");
+ browser.test.assertTrue(/Illegal URL/.test(browser.runtime.lastError.message),
+ "runtime.lastError should report the expected error message");
+
+ // Remove the opened tab is any.
+ if (tab) {
+ browser.tabs.remove(tab.id);
+ }
+ browser.test.sendMessage("done");
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("ready");
+
+ info(`test tab.create on invalid URL "${tabsCreateURL}"`);
+
+ extension.sendMessage("start", tabsCreateURL);
+ yield extension.awaitMessage("done");
+
+ yield extension.unload();
+}
+
+add_task(function* () {
+ info("Start testing tabs.create on invalid URLs");
+
+ let dataURLPage = `data:text/html,
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body>
+ <h1>data url page</h1>
+ </body>
+ </html>`;
+
+ let testCases = [
+ {tabsCreateURL: "about:addons"},
+ {tabsCreateURL: "javascript:console.log('tabs.update execute javascript')"},
+ {tabsCreateURL: dataURLPage},
+ ];
+
+ for (let {tabsCreateURL} of testCases) {
+ yield* testTabsCreateInvalidURL(tabsCreateURL);
+ }
+
+ info("done");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_detectLanguage.js b/browser/components/extensions/test/browser/browser_ext_tabs_detectLanguage.js
new file mode 100644
index 000000000..f28606001
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_detectLanguage.js
@@ -0,0 +1,47 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testDetectLanguage() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: async function() {
+ const BASE_PATH = "browser/browser/components/extensions/test/browser";
+
+ function loadTab(url) {
+ return browser.tabs.create({url});
+ }
+
+ try {
+ let tab = await loadTab(`http://example.co.jp/${BASE_PATH}/file_language_ja.html`);
+ let lang = await browser.tabs.detectLanguage(tab.id);
+ browser.test.assertEq("ja", lang, "Japanese document should be detected as Japanese");
+ await browser.tabs.remove(tab.id);
+
+ tab = await loadTab(`http://example.co.jp/${BASE_PATH}/file_language_fr_en.html`);
+ lang = await browser.tabs.detectLanguage(tab.id);
+ browser.test.assertEq("fr", lang, "French/English document should be detected as primarily French");
+ await browser.tabs.remove(tab.id);
+
+ tab = await loadTab(`http://example.co.jp/${BASE_PATH}/file_language_tlh.html`);
+ lang = await browser.tabs.detectLanguage(tab.id);
+ browser.test.assertEq("und", lang, "Klingon document should not be detected, should return 'und'");
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("detectLanguage");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("detectLanguage");
+ }
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("detectLanguage");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_duplicate.js b/browser/components/extensions/test/browser/browser_ext_tabs_duplicate.js
new file mode 100644
index 000000000..c4b0ffd2d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_duplicate.js
@@ -0,0 +1,146 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testDuplicateTab() {
+ yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ lastFocusedWindow: true,
+ }, function(tabs) {
+ let source = tabs[1];
+ // By moving it 0, we check that the new tab is created next
+ // to the existing one.
+ browser.tabs.move(source.id, {index: 0}, () => {
+ browser.tabs.duplicate(source.id, (tab) => {
+ browser.test.assertEq("http://example.net/", tab.url);
+ // Should be the second tab, next to the one duplicated.
+ browser.test.assertEq(1, tab.index);
+ // Should be selected by default.
+ browser.test.assertTrue(tab.selected);
+ browser.test.notifyPass("tabs.duplicate");
+ });
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.duplicate");
+ yield extension.unload();
+
+ while (gBrowser.tabs[0].linkedBrowser.currentURI.spec === "http://example.net/") {
+ yield BrowserTestUtils.removeTab(gBrowser.tabs[0]);
+ }
+});
+
+add_task(function* testDuplicateTabLazily() {
+ async function background() {
+ let tabLoadComplete = new Promise(resolve => {
+ browser.test.onMessage.addListener((message, tabId, result) => {
+ if (message == "duplicate-tab-done") {
+ resolve(tabId);
+ }
+ });
+ });
+
+ function awaitLoad(tabId) {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
+ if (tabId == tabId_ && changed.status == "complete") {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ }
+
+ try {
+ let url = "http://example.com/browser/browser/components/extensions/test/browser/file_dummy.html";
+ let tab = await browser.tabs.create({url});
+ let startTabId = tab.id;
+
+ await awaitLoad(startTabId);
+ browser.test.sendMessage("duplicate-tab", startTabId);
+
+ let unloadedTabId = await tabLoadComplete;
+ let loadedtab = await browser.tabs.get(startTabId);
+ browser.test.assertEq("Dummy test page", loadedtab.title, "Title should be returned for loaded pages");
+ browser.test.assertEq("complete", loadedtab.status, "Tab status should be complete for loaded pages");
+
+ let unloadedtab = await browser.tabs.get(unloadedTabId);
+ browser.test.assertEq("Dummy test page", unloadedtab.title, "Title should be returned after page has been unloaded");
+
+ await browser.tabs.remove([tab.id, unloadedTabId]);
+ browser.test.notifyPass("tabs.hasCorrectTabTitle");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("tabs.hasCorrectTabTitle");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ extension.onMessage("duplicate-tab", tabId => {
+ let {Management: {global: {TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let tab = TabManager.getTab(tabId);
+ // This is a bit of a hack to load a tab in the background.
+ let newTab = gBrowser.duplicateTab(tab, false);
+
+ BrowserTestUtils.waitForEvent(newTab, "SSTabRestored", () => true).then(() => {
+ extension.sendMessage("duplicate-tab-done", TabManager.getId(newTab));
+ });
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.hasCorrectTabTitle");
+ yield extension.unload();
+});
+
+add_task(function* testDuplicatePinnedTab() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+ gBrowser.pinTab(tab);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ lastFocusedWindow: true,
+ }, function(tabs) {
+ // Duplicate the pinned tab, example.net.
+ browser.tabs.duplicate(tabs[0].id, (tab) => {
+ browser.test.assertEq("http://example.net/", tab.url);
+ // Should be the second tab, next to the one duplicated.
+ browser.test.assertEq(1, tab.index);
+ // Should be pinned.
+ browser.test.assertTrue(tab.pinned);
+ browser.test.notifyPass("tabs.duplicate.pinned");
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.duplicate.pinned");
+ yield extension.unload();
+
+ while (gBrowser.tabs[0].linkedBrowser.currentURI.spec === "http://example.net/") {
+ yield BrowserTestUtils.removeTab(gBrowser.tabs[0]);
+ }
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_events.js b/browser/components/extensions/test/browser/browser_ext_tabs_events.js
new file mode 100644
index 000000000..75dea40fd
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_events.js
@@ -0,0 +1,280 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testTabEvents() {
+ async function background() {
+ let events = [];
+ browser.tabs.onCreated.addListener(tab => {
+ events.push({type: "onCreated", tab});
+ });
+
+ browser.tabs.onAttached.addListener((tabId, info) => {
+ events.push(Object.assign({type: "onAttached", tabId}, info));
+ });
+
+ browser.tabs.onDetached.addListener((tabId, info) => {
+ events.push(Object.assign({type: "onDetached", tabId}, info));
+ });
+
+ browser.tabs.onRemoved.addListener((tabId, info) => {
+ events.push(Object.assign({type: "onRemoved", tabId}, info));
+ });
+
+ browser.tabs.onMoved.addListener((tabId, info) => {
+ events.push(Object.assign({type: "onMoved", tabId}, info));
+ });
+
+ async function expectEvents(names) {
+ browser.test.log(`Expecting events: ${names.join(", ")}`);
+
+ await new Promise(resolve => setTimeout(resolve, 0));
+
+ browser.test.assertEq(names.length, events.length, "Got expected number of events");
+ for (let [i, name] of names.entries()) {
+ browser.test.assertEq(name, i in events && events[i].type,
+ `Got expected ${name} event`);
+ }
+ return events.splice(0);
+ }
+
+ try {
+ browser.test.log("Create second browser window");
+
+ let windows = await Promise.all([
+ browser.windows.getCurrent(),
+ browser.windows.create({url: "about:blank"}),
+ ]);
+
+ let windowId = windows[0].id;
+ let otherWindowId = windows[1].id;
+
+ let [created] = await expectEvents(["onCreated"]);
+ let initialTab = created.tab;
+
+
+ browser.test.log("Create tab in window 1");
+ let tab = await browser.tabs.create({windowId, index: 0, url: "about:blank"});
+ let oldIndex = tab.index;
+ browser.test.assertEq(0, oldIndex, "Tab has the expected index");
+
+ [created] = await expectEvents(["onCreated"]);
+ browser.test.assertEq(tab.id, created.tab.id, "Got expected tab ID");
+ browser.test.assertEq(oldIndex, created.tab.index, "Got expected tab index");
+
+
+ browser.test.log("Move tab to window 2");
+ await browser.tabs.move([tab.id], {windowId: otherWindowId, index: 0});
+
+ let [detached, attached] = await expectEvents(["onDetached", "onAttached"]);
+ browser.test.assertEq(oldIndex, detached.oldPosition, "Expected old index");
+ browser.test.assertEq(windowId, detached.oldWindowId, "Expected old window ID");
+
+ browser.test.assertEq(0, attached.newPosition, "Expected new index");
+ browser.test.assertEq(otherWindowId, attached.newWindowId, "Expected new window ID");
+
+
+ browser.test.log("Move tab within the same window");
+ let [moved] = await browser.tabs.move([tab.id], {index: 1});
+ browser.test.assertEq(1, moved.index, "Expected new index");
+
+ [moved] = await expectEvents(["onMoved"]);
+ browser.test.assertEq(tab.id, moved.tabId, "Expected tab ID");
+ browser.test.assertEq(0, moved.fromIndex, "Expected old index");
+ browser.test.assertEq(1, moved.toIndex, "Expected new index");
+ browser.test.assertEq(otherWindowId, moved.windowId, "Expected window ID");
+
+
+ browser.test.log("Remove tab");
+ await browser.tabs.remove(tab.id);
+ let [removed] = await expectEvents(["onRemoved"]);
+
+ browser.test.assertEq(tab.id, removed.tabId, "Expected removed tab ID");
+ browser.test.assertEq(otherWindowId, removed.windowId, "Expected removed tab window ID");
+ // Note: We want to test for the actual boolean value false here.
+ browser.test.assertEq(false, removed.isWindowClosing, "Expected isWindowClosing value");
+
+
+ browser.test.log("Close second window");
+ await browser.windows.remove(otherWindowId);
+ [removed] = await expectEvents(["onRemoved"]);
+ browser.test.assertEq(initialTab.id, removed.tabId, "Expected removed tab ID");
+ browser.test.assertEq(otherWindowId, removed.windowId, "Expected removed tab window ID");
+ browser.test.assertEq(true, removed.isWindowClosing, "Expected isWindowClosing value");
+
+
+ browser.test.log("Create additional tab in window 1");
+ tab = await browser.tabs.create({windowId, url: "about:blank"});
+ await expectEvents(["onCreated"]);
+
+
+ browser.test.log("Create a new window, adopting the new tab");
+ // We have to explicitly wait for the event here, since its timing is
+ // not predictable.
+ let promiseAttached = new Promise(resolve => {
+ browser.tabs.onAttached.addListener(function listener(tabId) {
+ browser.tabs.onAttached.removeListener(listener);
+ resolve();
+ });
+ });
+
+ let [window] = await Promise.all([
+ browser.windows.create({tabId: tab.id}),
+ promiseAttached,
+ ]);
+
+ [detached, attached] = await expectEvents(["onDetached", "onAttached"]);
+
+ browser.test.assertEq(tab.id, detached.tabId, "Expected onDetached tab ID");
+
+ browser.test.assertEq(tab.id, attached.tabId, "Expected onAttached tab ID");
+ browser.test.assertEq(0, attached.newPosition, "Expected onAttached new index");
+ browser.test.assertEq(window.id, attached.newWindowId,
+ "Expected onAttached new window id");
+
+ browser.test.log("Close the new window");
+ await browser.windows.remove(window.id);
+
+ browser.test.notifyPass("tabs-events");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("tabs-events");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs-events");
+ yield extension.unload();
+});
+
+add_task(function* testTabEventsSize() {
+ function background() {
+ function sendSizeMessages(tab, type) {
+ browser.test.sendMessage(`${type}-dims`, {width: tab.width, height: tab.height});
+ }
+
+ browser.tabs.onCreated.addListener(tab => {
+ sendSizeMessages(tab, "on-created");
+ });
+
+ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
+ if (changeInfo.status == "complete") {
+ sendSizeMessages(tab, "on-updated");
+ }
+ });
+
+ browser.test.onMessage.addListener(async (msg, arg) => {
+ if (msg === "create-tab") {
+ let tab = await browser.tabs.create({url: "http://example.com/"});
+ sendSizeMessages(tab, "create");
+ browser.test.sendMessage("created-tab-id", tab.id);
+ } else if (msg === "update-tab") {
+ let tab = await browser.tabs.update(arg, {url: "http://example.org/"});
+ sendSizeMessages(tab, "update");
+ } else if (msg === "remove-tab") {
+ browser.tabs.remove(arg);
+ browser.test.sendMessage("tab-removed");
+ }
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+ background,
+ });
+
+ const RESOLUTION_PREF = "layout.css.devPixelsPerPx";
+ registerCleanupFunction(() => {
+ SpecialPowers.clearUserPref(RESOLUTION_PREF);
+ });
+
+ function checkDimensions(dims, type) {
+ is(dims.width, gBrowser.selectedBrowser.clientWidth, `tab from ${type} reports expected width`);
+ is(dims.height, gBrowser.selectedBrowser.clientHeight, `tab from ${type} reports expected height`);
+ }
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("ready")]);
+
+ for (let resolution of [2, 1]) {
+ SpecialPowers.setCharPref(RESOLUTION_PREF, String(resolution));
+ is(window.devicePixelRatio, resolution, "window has the required resolution");
+
+ extension.sendMessage("create-tab");
+ let tabId = yield extension.awaitMessage("created-tab-id");
+
+ checkDimensions(yield extension.awaitMessage("create-dims"), "create");
+ checkDimensions(yield extension.awaitMessage("on-created-dims"), "onCreated");
+ checkDimensions(yield extension.awaitMessage("on-updated-dims"), "onUpdated");
+
+ extension.sendMessage("update-tab", tabId);
+
+ checkDimensions(yield extension.awaitMessage("update-dims"), "update");
+ checkDimensions(yield extension.awaitMessage("on-updated-dims"), "onUpdated");
+
+ extension.sendMessage("remove-tab", tabId);
+ yield extension.awaitMessage("tab-removed");
+ }
+
+ yield extension.unload();
+ SpecialPowers.clearUserPref(RESOLUTION_PREF);
+});
+
+add_task(function* testTabRemovalEvent() {
+ async function background() {
+ function awaitLoad(tabId) {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
+ if (tabId == tabId_ && changed.status == "complete") {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ }
+
+ chrome.tabs.onRemoved.addListener((tabId, info) => {
+ browser.test.log("Make sure the removed tab is not available in the tabs.query callback.");
+ chrome.tabs.query({}, tabs => {
+ for (let tab of tabs) {
+ browser.test.assertTrue(tab.id != tabId, "Tab query should not include removed tabId");
+ }
+ browser.test.notifyPass("tabs-events");
+ });
+ });
+
+ try {
+ let url = "http://example.com/browser/browser/components/extensions/test/browser/context.html";
+ let tab = await browser.tabs.create({url: url});
+ await awaitLoad(tab.id);
+
+ await browser.tabs.remove(tab.id);
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("tabs-events");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs-events");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js
new file mode 100644
index 000000000..5a15f2e39
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript.js
@@ -0,0 +1,234 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testExecuteScript() {
+ let {MessageChannel} = Cu.import("resource://gre/modules/MessageChannel.jsm", {});
+
+ function countMM(messageManagerMap) {
+ let count = 0;
+ // List of permanent message managers in the main process. We should not
+ // count them in the test because MessageChannel unsubscribes when the
+ // message manager closes, which never happens to these, of course.
+ let globalMMs = [
+ Services.mm,
+ Services.ppmm,
+ Services.ppmm.getChildAt(0),
+ ];
+ for (let mm of messageManagerMap.keys()) {
+ // Sanity check: mm is a message manager.
+ try {
+ mm.QueryInterface(Ci.nsIMessageSender);
+ } catch (e) {
+ mm.QueryInterface(Ci.nsIMessageBroadcaster);
+ }
+ if (!globalMMs.includes(mm)) {
+ ++count;
+ }
+ }
+ return count;
+ }
+
+ let messageManagersSize = countMM(MessageChannel.messageManagers);
+ let responseManagersSize = countMM(MessageChannel.responseManagers);
+
+ const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
+ const URL = BASE + "file_iframe_document.html";
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, URL, true);
+
+ async function background() {
+ try {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ let frames = await browser.webNavigation.getAllFrames({tabId: tab.id});
+
+ browser.test.log(`FRAMES: ${frames[1].frameId} ${JSON.stringify(frames)}\n`);
+ await Promise.all([
+ browser.tabs.executeScript({
+ code: "42",
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected one callback result");
+ browser.test.assertEq(42, result[0], "Expected callback result");
+ }),
+
+ browser.tabs.executeScript({
+ file: "script.js",
+ code: "42",
+ }).then(result => {
+ browser.test.fail("Expected not to be able to execute a script with both file and code");
+ }, error => {
+ browser.test.assertTrue(/a 'code' or a 'file' property, but not both/.test(error.message),
+ "Got expected error");
+ }),
+
+ browser.tabs.executeScript({
+ file: "script.js",
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected one callback result");
+ browser.test.assertEq(undefined, result[0], "Expected callback result");
+ }),
+
+ browser.tabs.executeScript({
+ file: "script2.js",
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected one callback result");
+ browser.test.assertEq(27, result[0], "Expected callback result");
+ }),
+
+ browser.tabs.executeScript({
+ code: "location.href;",
+ allFrames: true,
+ }).then(result => {
+ browser.test.assertTrue(Array.isArray(result), "Result is an array");
+
+ browser.test.assertEq(2, result.length, "Result has correct length");
+
+ browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
+ browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
+ }),
+
+ browser.tabs.executeScript({
+ code: "location.href;",
+ runAt: "document_end",
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected callback result");
+ browser.test.assertEq("string", typeof result[0], "Result is a string");
+
+ browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "Result is correct");
+ }),
+
+ browser.tabs.executeScript({
+ code: "window",
+ }).then(result => {
+ browser.test.fail("Expected error when returning non-structured-clonable object");
+ }, error => {
+ browser.test.assertEq("Script returned non-structured-clonable data",
+ error.message, "Got expected error");
+ }),
+
+ browser.tabs.executeScript({
+ code: "Promise.resolve(window)",
+ }).then(result => {
+ browser.test.fail("Expected error when returning non-structured-clonable object");
+ }, error => {
+ browser.test.assertEq("Script returned non-structured-clonable data",
+ error.message, "Got expected error");
+ }),
+
+ browser.tabs.executeScript({
+ frameId: Number.MAX_SAFE_INTEGER,
+ code: "42",
+ }).then(result => {
+ browser.test.fail("Expected error when specifying invalid frame ID");
+ }, error => {
+ let details = {
+ frame_id: Number.MAX_SAFE_INTEGER,
+ matchesHost: ["http://mochi.test/", "http://example.com/"],
+ };
+ browser.test.assertEq(`No window matching ${JSON.stringify(details)}`,
+ error.message, "Got expected error");
+ }),
+
+ browser.tabs.create({url: "http://example.net/", active: false}).then(async tab => {
+ await browser.tabs.executeScript(tab.id, {
+ code: "42",
+ }).then(result => {
+ browser.test.fail("Expected error when trying to execute on invalid domain");
+ }, error => {
+ let details = {
+ matchesHost: ["http://mochi.test/", "http://example.com/"],
+ };
+ browser.test.assertEq(`No window matching ${JSON.stringify(details)}`,
+ error.message, "Got expected error");
+ });
+
+ await browser.tabs.remove(tab.id);
+ }),
+
+ browser.tabs.executeScript({
+ code: "Promise.resolve(42)",
+ }).then(result => {
+ browser.test.assertEq(42, result[0], "Got expected promise resolution value as result");
+ }),
+
+ browser.tabs.executeScript({
+ code: "location.href;",
+ runAt: "document_end",
+ allFrames: true,
+ }).then(result => {
+ browser.test.assertTrue(Array.isArray(result), "Result is an array");
+
+ browser.test.assertEq(2, result.length, "Result has correct length");
+
+ browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), "First result is correct");
+ browser.test.assertEq("http://mochi.test:8888/", result[1], "Second result is correct");
+ }),
+
+ browser.tabs.executeScript({
+ code: "location.href;",
+ frameId: frames[0].frameId,
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected one result");
+ browser.test.assertTrue(/\/file_iframe_document\.html$/.test(result[0]), `Result for frameId[0] is correct: ${result[0]}`);
+ }),
+
+ browser.tabs.executeScript({
+ code: "location.href;",
+ frameId: frames[1].frameId,
+ }).then(result => {
+ browser.test.assertEq(1, result.length, "Expected one result");
+ browser.test.assertEq("http://mochi.test:8888/", result[0], "Result for frameId[1] is correct");
+ }),
+
+ browser.tabs.create({url: "http://example.com/"}).then(async tab => {
+ let result = await browser.tabs.executeScript(tab.id, {code: "location.href"});
+
+ browser.test.assertEq("http://example.com/", result[0], "Script executed correctly in new tab");
+
+ await browser.tabs.remove(tab.id);
+ }),
+
+ new Promise(resolve => {
+ browser.runtime.onMessage.addListener(message => {
+ browser.test.assertEq("script ran", message, "Expected runtime message");
+ resolve();
+ });
+ }),
+ ]);
+
+ browser.test.notifyPass("executeScript");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("executeScript");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/", "http://example.com/", "webNavigation"],
+ },
+
+ background,
+
+ files: {
+ "script.js": function() {
+ browser.runtime.sendMessage("script ran");
+ },
+
+ "script2.js": "27",
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("executeScript");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+
+ // Make sure that we're not holding on to references to closed message
+ // managers.
+ is(countMM(MessageChannel.messageManagers), messageManagersSize, "Message manager count");
+ is(countMM(MessageChannel.responseManagers), responseManagersSize, "Response manager count");
+ is(MessageChannel.pendingResponses.size, 0, "Pending response count");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_bad.js b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_bad.js
new file mode 100644
index 000000000..d11354ead
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_bad.js
@@ -0,0 +1,217 @@
+"use strict";
+
+// This is a pretty terrible hack, but it's the best we can do until we
+// support |executeScript| callbacks and |lastError|.
+function* testHasNoPermission(params) {
+ let contentSetup = params.contentSetup || (() => Promise.resolve());
+
+ async function background(contentSetup) {
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ browser.test.assertEq(msg, "second script ran", "second script ran");
+ browser.test.notifyPass("executeScript");
+ });
+
+ browser.test.onMessage.addListener(msg => {
+ browser.test.assertEq(msg, "execute-script");
+
+ browser.tabs.query({currentWindow: true}, tabs => {
+ browser.tabs.executeScript({
+ file: "script.js",
+ });
+
+ // Execute a script we know we have permissions for in the
+ // second tab, in the hopes that it will execute after the
+ // first one. This has intermittent failure written all over
+ // it, but it's just about the best we can do until we
+ // support callbacks for executeScript.
+ browser.tabs.executeScript(tabs[1].id, {
+ file: "second-script.js",
+ });
+ });
+ });
+
+ await contentSetup();
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: params.manifest,
+
+ background: `(${background})(${contentSetup})`,
+
+ files: {
+ "script.js": function() {
+ browser.runtime.sendMessage("first script ran");
+ },
+
+ "second-script.js": function() {
+ browser.runtime.sendMessage("second script ran");
+ },
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ if (params.setup) {
+ yield params.setup(extension);
+ }
+
+ extension.sendMessage("execute-script");
+
+ yield extension.awaitFinish("executeScript");
+ yield extension.unload();
+}
+
+add_task(function* testBadPermissions() {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/");
+
+ info("Test no special permissions");
+ yield testHasNoPermission({
+ manifest: {"permissions": ["http://example.com/"]},
+ });
+
+ info("Test tabs permissions");
+ yield testHasNoPermission({
+ manifest: {"permissions": ["http://example.com/", "tabs"]},
+ });
+
+ info("Test no special permissions, commands, key press");
+ yield testHasNoPermission({
+ manifest: {
+ "permissions": ["http://example.com/"],
+ "commands": {
+ "test-tabs-executeScript": {
+ "suggested_key": {
+ "default": "Alt+Shift+K",
+ },
+ },
+ },
+ },
+ contentSetup() {
+ browser.commands.onCommand.addListener(function(command) {
+ if (command == "test-tabs-executeScript") {
+ browser.test.sendMessage("tabs-command-key-pressed");
+ }
+ });
+ return Promise.resolve();
+ },
+ setup: function* (extension) {
+ yield EventUtils.synthesizeKey("k", {altKey: true, shiftKey: true});
+ yield extension.awaitMessage("tabs-command-key-pressed");
+ },
+ });
+
+ info("Test active tab, commands, no key press");
+ yield testHasNoPermission({
+ manifest: {
+ "permissions": ["http://example.com/", "activeTab"],
+ "commands": {
+ "test-tabs-executeScript": {
+ "suggested_key": {
+ "default": "Alt+Shift+K",
+ },
+ },
+ },
+ },
+ });
+
+ info("Test active tab, browser action, no click");
+ yield testHasNoPermission({
+ manifest: {
+ "permissions": ["http://example.com/", "activeTab"],
+ "browser_action": {},
+ },
+ });
+
+ info("Test active tab, page action, no click");
+ yield testHasNoPermission({
+ manifest: {
+ "permissions": ["http://example.com/", "activeTab"],
+ "page_action": {},
+ },
+ async contentSetup() {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ await browser.pageAction.show(tab.id);
+ },
+ });
+
+ yield BrowserTestUtils.removeTab(tab2);
+ yield BrowserTestUtils.removeTab(tab1);
+});
+
+add_task(function* testBadURL() {
+ async function background() {
+ let promises = [
+ new Promise(resolve => {
+ browser.tabs.executeScript({
+ file: "http://example.com/script.js",
+ }, result => {
+ browser.test.assertEq(undefined, result, "Result value");
+
+ browser.test.assertTrue(browser.extension.lastError instanceof Error,
+ "runtime.lastError is Error");
+
+ browser.test.assertTrue(browser.runtime.lastError instanceof Error,
+ "runtime.lastError is Error");
+
+ browser.test.assertEq(
+ "Files to be injected must be within the extension",
+ browser.extension.lastError && browser.extension.lastError.message,
+ "extension.lastError value");
+
+ browser.test.assertEq(
+ "Files to be injected must be within the extension",
+ browser.runtime.lastError && browser.runtime.lastError.message,
+ "runtime.lastError value");
+
+ resolve();
+ });
+ }),
+
+ browser.tabs.executeScript({
+ file: "http://example.com/script.js",
+ }).catch(error => {
+ browser.test.assertTrue(error instanceof Error, "Error is Error");
+
+ browser.test.assertEq(null, browser.extension.lastError,
+ "extension.lastError value");
+
+ browser.test.assertEq(null, browser.runtime.lastError,
+ "runtime.lastError value");
+
+ browser.test.assertEq(
+ "Files to be injected must be within the extension",
+ error && error.message,
+ "error value");
+ }),
+ ];
+
+ await Promise.all(promises);
+
+ browser.test.notifyPass("executeScript-lastError");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["<all_urls>"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("executeScript-lastError");
+
+ yield extension.unload();
+});
+
+// TODO: Test that |executeScript| fails if the tab has navigated to a
+// new page, and no longer matches our expected state. This involves
+// intentionally trying to trigger a race condition, and is probably not
+// even worth attempting until we have proper |executeScript| callbacks.
+
+add_task(forceGC);
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_good.js b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_good.js
new file mode 100644
index 000000000..cf4721310
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_good.js
@@ -0,0 +1,189 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+requestLongerTimeout(2);
+
+function* testHasPermission(params) {
+ let contentSetup = params.contentSetup || (() => Promise.resolve());
+
+ async function background(contentSetup) {
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ browser.test.assertEq(msg, "script ran", "script ran");
+ browser.test.notifyPass("executeScript");
+ });
+
+ browser.test.onMessage.addListener(msg => {
+ browser.test.assertEq(msg, "execute-script");
+
+ browser.tabs.executeScript({
+ file: "script.js",
+ });
+ });
+
+ await contentSetup();
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: params.manifest,
+
+ background: `(${background})(${contentSetup})`,
+
+ files: {
+ "script.js": function() {
+ browser.runtime.sendMessage("script ran");
+ },
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ if (params.setup) {
+ yield params.setup(extension);
+ }
+
+ extension.sendMessage("execute-script");
+
+ yield extension.awaitFinish("executeScript");
+
+ if (params.tearDown) {
+ yield params.tearDown(extension);
+ }
+
+ yield extension.unload();
+}
+
+add_task(function* testGoodPermissions() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/", true);
+
+ info("Test explicit host permission");
+ yield testHasPermission({
+ manifest: {"permissions": ["http://mochi.test/"]},
+ });
+
+ info("Test explicit host subdomain permission");
+ yield testHasPermission({
+ manifest: {"permissions": ["http://*.mochi.test/"]},
+ });
+
+ info("Test explicit <all_urls> permission");
+ yield testHasPermission({
+ manifest: {"permissions": ["<all_urls>"]},
+ });
+
+ info("Test activeTab permission with a command key press");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab"],
+ "commands": {
+ "test-tabs-executeScript": {
+ "suggested_key": {
+ "default": "Alt+Shift+K",
+ },
+ },
+ },
+ },
+ contentSetup() {
+ browser.commands.onCommand.addListener(function(command) {
+ if (command == "test-tabs-executeScript") {
+ browser.test.sendMessage("tabs-command-key-pressed");
+ }
+ });
+ return Promise.resolve();
+ },
+ setup: function* (extension) {
+ yield EventUtils.synthesizeKey("k", {altKey: true, shiftKey: true});
+ yield extension.awaitMessage("tabs-command-key-pressed");
+ },
+ });
+
+ info("Test activeTab permission with a browser action click");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab"],
+ "browser_action": {},
+ },
+ contentSetup() {
+ browser.browserAction.onClicked.addListener(() => {
+ browser.test.log("Clicked.");
+ });
+ return Promise.resolve();
+ },
+ setup: clickBrowserAction,
+ tearDown: closeBrowserAction,
+ });
+
+ info("Test activeTab permission with a page action click");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab"],
+ "page_action": {},
+ },
+ contentSetup: async () => {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ await browser.pageAction.show(tab.id);
+ },
+ setup: clickPageAction,
+ tearDown: closePageAction,
+ });
+
+ info("Test activeTab permission with a browser action w/popup click");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab"],
+ "browser_action": {"default_popup": "_blank.html"},
+ },
+ setup: async extension => {
+ await clickBrowserAction(extension);
+ return awaitExtensionPanel(extension, window, "_blank.html");
+ },
+ tearDown: closeBrowserAction,
+ });
+
+ info("Test activeTab permission with a page action w/popup click");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab"],
+ "page_action": {"default_popup": "_blank.html"},
+ },
+ contentSetup: async () => {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ await browser.pageAction.show(tab.id);
+ },
+ setup: clickPageAction,
+ tearDown: closePageAction,
+ });
+
+ info("Test activeTab permission with a context menu click");
+ yield testHasPermission({
+ manifest: {
+ "permissions": ["activeTab", "contextMenus"],
+ },
+ contentSetup() {
+ browser.contextMenus.create({title: "activeTab", contexts: ["all"]});
+ return Promise.resolve();
+ },
+ setup: function* (extension) {
+ let contextMenu = document.getElementById("contentAreaContextMenu");
+ let awaitPopupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
+ let awaitPopupHidden = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
+
+ yield BrowserTestUtils.synthesizeMouseAtCenter("a[href]", {type: "contextmenu", button: 2},
+ gBrowser.selectedBrowser);
+ yield awaitPopupShown;
+
+ let item = contextMenu.querySelector("[label=activeTab]");
+
+ yield EventUtils.synthesizeMouseAtCenter(item, {}, window);
+
+ yield awaitPopupHidden;
+ },
+ });
+
+ yield BrowserTestUtils.removeTab(tab);
+});
+
+add_task(forceGC);
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_no_create.js b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_no_create.js
new file mode 100644
index 000000000..7b2ffe175
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_no_create.js
@@ -0,0 +1,67 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testExecuteScriptAtOnUpdated() {
+ const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
+ const URL = BASE + "file_iframe_document.html";
+ // This is a regression test for bug 1325830.
+ // The bug (executeScript not completing any more) occurred when executeScript
+ // was called early at the onUpdated event, unless the tabs.create method is
+ // called. So this test does not use tabs.create to open new tabs.
+ // Note that if this test is run together with other tests that do call
+ // tabs.create, then this test case does not properly test the conditions of
+ // the regression any more. To verify that the regression has been resolved,
+ // this test must be run in isolation.
+
+ function background() {
+ // Using variables to prevent listeners from running more than once, instead
+ // of removing the listener. This is to minimize any IPC, since the bug that
+ // is being tested is sensitive to timing.
+ let ignore = false;
+ let url;
+ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
+ if (changeInfo.status === "loading" && tab.url === url && !ignore) {
+ ignore = true;
+ browser.tabs.executeScript(tabId, {
+ code: "document.URL",
+ }).then(results => {
+ browser.test.assertEq(url, results[0], "Content script should run");
+ browser.test.notifyPass("executeScript-at-onUpdated");
+ }, error => {
+ browser.test.fail(`Unexpected error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("executeScript-at-onUpdated");
+ });
+ // (running this log call after executeScript to minimize IPC between
+ // onUpdated and executeScript.)
+ browser.test.log(`Found expected navigation to ${url}`);
+ } else {
+ // The bug occurs when executeScript is called before a tab is
+ // initialized.
+ browser.tabs.executeScript(tabId, {code: ""});
+ }
+ });
+ browser.test.onMessage.addListener(testUrl => {
+ url = testUrl;
+ browser.test.sendMessage("open-test-tab");
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/", "tabs"],
+ },
+ background,
+ });
+
+ yield extension.startup();
+ extension.sendMessage(URL);
+ yield extension.awaitMessage("open-test-tab");
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, URL, true);
+
+ yield extension.awaitFinish("executeScript-at-onUpdated");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js
new file mode 100644
index 000000000..a4c0ed6f1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_runAt.js
@@ -0,0 +1,107 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/**
+ * These tests ensure that the runAt argument to tabs.executeScript delays
+ * script execution until the document has reached the correct state.
+ *
+ * Since tests of this nature are especially race-prone, it relies on a
+ * server-JS script to delay the completion of our test page's load cycle long
+ * enough for us to attempt to load our scripts in the earlies phase we support.
+ *
+ * And since we can't actually rely on that timing, it retries any attempts that
+ * fail to load as early as expected, but don't load at any illegal time.
+ */
+
+add_task(function* testExecuteScript() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank", true);
+
+ async function background() {
+ let tab;
+
+ const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
+ const URL = BASE + "file_iframe_document.sjs";
+
+ const MAX_TRIES = 10;
+
+ try {
+ [tab] = await browser.tabs.query({active: true, currentWindow: true});
+
+ let success = false;
+ for (let tries = 0; !success && tries < MAX_TRIES; tries++) {
+ let url = `${URL}?r=${Math.random()}`;
+
+ let loadingPromise = new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changed, tab_) {
+ if (tabId == tab.id && changed.status == "loading" && tab_.url == url) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+
+ // TODO: Test allFrames and frameId.
+
+ await browser.tabs.update({url});
+ await loadingPromise;
+
+ let states = await Promise.all([
+ // Send the executeScript requests in the reverse order that we expect
+ // them to execute in, to avoid them passing only because of timing
+ // races.
+ browser.tabs.executeScript({
+ code: "document.readyState",
+ runAt: "document_idle",
+ }),
+ browser.tabs.executeScript({
+ code: "document.readyState",
+ runAt: "document_end",
+ }),
+ browser.tabs.executeScript({
+ code: "document.readyState",
+ runAt: "document_start",
+ }),
+ ].reverse());
+
+ browser.test.log(`Got states: ${states}`);
+
+ // Make sure that none of our scripts executed earlier than expected,
+ // regardless of retries.
+ browser.test.assertTrue(states[1] == "interactive" || states[1] == "complete",
+ `document_end state is valid: ${states[1]}`);
+ browser.test.assertTrue(states[2] == "complete",
+ `document_idle state is valid: ${states[2]}`);
+
+ // If we have the earliest valid states for each script, we're done.
+ // Otherwise, try again.
+ success = (states[0] == "loading" &&
+ states[1] == "interactive" &&
+ states[2] == "complete");
+ }
+
+ browser.test.assertTrue(success, "Got the earliest expected states at least once");
+
+ browser.test.notifyPass("executeScript-runAt");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("executeScript-runAt");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/", "tabs"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("executeScript-runAt");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_getCurrent.js b/browser/components/extensions/test/browser/browser_ext_tabs_getCurrent.js
new file mode 100644
index 000000000..b67d935cb
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_getCurrent.js
@@ -0,0 +1,70 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "browser_action": {"default_popup": "popup.html"},
+ },
+
+ files: {
+ "tab.js": function() {
+ let url = document.location.href;
+
+ browser.tabs.getCurrent(currentTab => {
+ browser.test.assertEq(currentTab.url, url, "getCurrent in non-active background tab");
+
+ // Activate the tab.
+ browser.tabs.onActivated.addListener(function listener({tabId}) {
+ if (tabId == currentTab.id) {
+ browser.tabs.onActivated.removeListener(listener);
+
+ browser.tabs.getCurrent(currentTab => {
+ browser.test.assertEq(currentTab.id, tabId, "in active background tab");
+ browser.test.assertEq(currentTab.url, url, "getCurrent in non-active background tab");
+
+ browser.test.sendMessage("tab-finished");
+ });
+ }
+ });
+ browser.tabs.update(currentTab.id, {active: true});
+ });
+ },
+
+ "popup.js": function() {
+ browser.tabs.getCurrent(tab => {
+ browser.test.assertEq(tab, undefined, "getCurrent in popup script");
+ browser.test.sendMessage("popup-finished");
+ });
+ },
+
+ "tab.html": `<head><meta charset="utf-8"><script src="tab.js"></script></head>`,
+ "popup.html": `<head><meta charset="utf-8"><script src="popup.js"></script></head>`,
+ },
+
+ background: function() {
+ browser.tabs.getCurrent(tab => {
+ browser.test.assertEq(tab, undefined, "getCurrent in background script");
+ browser.test.sendMessage("background-finished");
+ });
+
+ browser.tabs.create({url: "tab.html", active: false});
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("background-finished");
+ yield extension.awaitMessage("tab-finished");
+
+ clickBrowserAction(extension);
+ yield awaitExtensionPanel(extension);
+ yield extension.awaitMessage("popup-finished");
+ yield closeBrowserAction(extension);
+
+ // The extension tab is automatically closed when the extension unloads.
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_insertCSS.js b/browser/components/extensions/test/browser/browser_ext_tabs_insertCSS.js
new file mode 100644
index 000000000..a8e172d94
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_insertCSS.js
@@ -0,0 +1,86 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testExecuteScript() {
+ let {MessageChannel} = Cu.import("resource://gre/modules/MessageChannel.jsm", {});
+
+ let messageManagersSize = MessageChannel.messageManagers.size;
+ let responseManagersSize = MessageChannel.responseManagers.size;
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/", true);
+
+ async function background() {
+ let tasks = [
+ {
+ background: "transparent",
+ foreground: "rgb(0, 113, 4)",
+ promise: () => {
+ return browser.tabs.insertCSS({
+ file: "file2.css",
+ });
+ },
+ },
+ {
+ background: "rgb(42, 42, 42)",
+ foreground: "rgb(0, 113, 4)",
+ promise: () => {
+ return browser.tabs.insertCSS({
+ code: "* { background: rgb(42, 42, 42) }",
+ });
+ },
+ },
+ ];
+
+ function checkCSS() {
+ let computedStyle = window.getComputedStyle(document.body);
+ return [computedStyle.backgroundColor, computedStyle.color];
+ }
+
+ try {
+ for (let {promise, background, foreground} of tasks) {
+ let result = await promise();
+
+ browser.test.assertEq(undefined, result, "Expected callback result");
+
+ [result] = await browser.tabs.executeScript({
+ code: `(${checkCSS})()`,
+ });
+
+ browser.test.assertEq(background, result[0], "Expected background color");
+ browser.test.assertEq(foreground, result[1], "Expected foreground color");
+ }
+
+ browser.test.notifyPass("insertCSS");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFailure("insertCSS");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/"],
+ },
+
+ background,
+
+ files: {
+ "file2.css": "* { color: rgb(0, 113, 4) }",
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("insertCSS");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+
+ // Make sure that we're not holding on to references to closed message
+ // managers.
+ is(MessageChannel.messageManagers.size, messageManagersSize, "Message manager count");
+ is(MessageChannel.responseManagers.size, responseManagersSize, "Response manager count");
+ is(MessageChannel.pendingResponses.size, 0, "Pending response count");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_move.js b/browser/components/extensions/test/browser/browser_ext_tabs_move.js
new file mode 100644
index 000000000..917cdc146
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_move.js
@@ -0,0 +1,103 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:config");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: async function() {
+ let [tab] = await browser.tabs.query({lastFocusedWindow: true});
+
+ browser.tabs.move(tab.id, {index: 0});
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+
+ browser.test.assertEq(tabs[0].url, tab.url, "should be first tab");
+ browser.test.notifyPass("tabs.move.single");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.single");
+ yield extension.unload();
+
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: async function() {
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+
+ tabs.sort(function(a, b) { return a.url > b.url; });
+
+ browser.tabs.move(tabs.map(tab => tab.id), {index: 0});
+
+ tabs = await browser.tabs.query({lastFocusedWindow: true});
+
+ browser.test.assertEq(tabs[0].url, "about:blank", "should be first tab");
+ browser.test.assertEq(tabs[1].url, "about:config", "should be second tab");
+ browser.test.assertEq(tabs[2].url, "about:robots", "should be third tab");
+
+ browser.test.notifyPass("tabs.move.multiple");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.multiple");
+ yield extension.unload();
+
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ async background() {
+ let [, tab] = await browser.tabs.query({lastFocusedWindow: true});
+
+ // Assuming that tab.id of 12345 does not exist.
+ await browser.test.assertRejects(
+ browser.tabs.move([tab.id, 12345], {index: 0}),
+ /Invalid tab/,
+ "Should receive invalid tab error");
+
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+ browser.test.assertEq(tabs[1].url, tab.url, "should be second tab");
+ browser.test.notifyPass("tabs.move.invalid");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.invalid");
+ yield extension.unload();
+
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: async function() {
+ let [tab] = await browser.tabs.query({lastFocusedWindow: true});
+ browser.tabs.move(tab.id, {index: -1});
+
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+
+ browser.test.assertEq(tabs[2].url, tab.url, "should be last tab");
+ browser.test.notifyPass("tabs.move.last");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.last");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_move_window.js b/browser/components/extensions/test/browser/browser_ext_tabs_move_window.js
new file mode 100644
index 000000000..f3bce364a
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_move_window.js
@@ -0,0 +1,98 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+ let window1 = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.openNewForegroundTab(window1.gBrowser, "http://example.com/");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ async background() {
+ let tabs = await browser.tabs.query({url: "<all_urls>"});
+ let destination = tabs[0];
+ let source = tabs[1]; // skip over about:blank in window1
+
+ // Assuming that this windowId does not exist.
+ await browser.test.assertRejects(
+ browser.tabs.move(source.id, {windowId: 123144576, index: 0}),
+ /Invalid window/,
+ "Should receive invalid window error");
+
+ browser.tabs.move(source.id, {windowId: destination.windowId, index: 0});
+
+ tabs = await browser.tabs.query({url: "<all_urls>"});
+ browser.test.assertEq(tabs[0].url, "http://example.com/");
+ browser.test.assertEq(tabs[0].windowId, destination.windowId);
+ browser.test.notifyPass("tabs.move.window");
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.window");
+ yield extension.unload();
+
+ for (let tab of window.gBrowser.tabs) {
+ yield BrowserTestUtils.removeTab(tab);
+ }
+ yield BrowserTestUtils.closeWindow(window1);
+});
+
+add_task(function* test_currentWindowAfterTabMoved() {
+ const files = {
+ "current.html": "<meta charset=utf-8><script src=current.js></script>",
+ "current.js": function() {
+ browser.test.onMessage.addListener(msg => {
+ if (msg === "current") {
+ browser.windows.getCurrent(win => {
+ browser.test.sendMessage("id", win.id);
+ });
+ }
+ });
+ browser.test.sendMessage("ready");
+ },
+ };
+
+ async function background() {
+ let tabId;
+
+ const url = browser.extension.getURL("current.html");
+
+ browser.test.onMessage.addListener(async msg => {
+ if (msg === "move") {
+ await browser.windows.create({tabId});
+ browser.test.sendMessage("moved");
+ } else if (msg === "close") {
+ await browser.tabs.remove(tabId);
+ browser.test.sendMessage("done");
+ }
+ });
+
+ let tab = await browser.tabs.create({url});
+ tabId = tab.id;
+ }
+
+ const extension = ExtensionTestUtils.loadExtension({files, background});
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ extension.sendMessage("current");
+ const first = yield extension.awaitMessage("id");
+
+ extension.sendMessage("move");
+ yield extension.awaitMessage("moved");
+
+ extension.sendMessage("current");
+ const second = yield extension.awaitMessage("id");
+
+ isnot(first, second, "current window id is different after moving the tab");
+
+ extension.sendMessage("close");
+ yield extension.awaitMessage("done");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_move_window_multiple.js b/browser/components/extensions/test/browser/browser_ext_tabs_move_window_multiple.js
new file mode 100644
index 000000000..dacd547f2
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_move_window_multiple.js
@@ -0,0 +1,43 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let window1 = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.openNewForegroundTab(window.gBrowser, "http://example.net/");
+ yield BrowserTestUtils.openNewForegroundTab(window.gBrowser, "http://example.com/");
+ yield BrowserTestUtils.openNewForegroundTab(window1.gBrowser, "http://example.net/");
+ yield BrowserTestUtils.openNewForegroundTab(window1.gBrowser, "http://example.com/");
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query(
+ {url: "<all_urls>"},
+ tabs => {
+ let move1 = tabs[1];
+ let move3 = tabs[3];
+ browser.tabs.move([move1.id, move3.id], {index: 0});
+ browser.tabs.query(
+ {url: "<all_urls>"},
+ tabs => {
+ browser.test.assertEq(tabs[0].url, move1.url);
+ browser.test.assertEq(tabs[2].url, move3.url);
+ browser.test.notifyPass("tabs.move.multiple");
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.multiple");
+ yield extension.unload();
+
+ for (let tab of window.gBrowser.tabs) {
+ yield BrowserTestUtils.removeTab(tab);
+ }
+ yield BrowserTestUtils.closeWindow(window1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_move_window_pinned.js b/browser/components/extensions/test/browser/browser_ext_tabs_move_window_pinned.js
new file mode 100644
index 000000000..c592dc56d
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_move_window_pinned.js
@@ -0,0 +1,42 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+ let window1 = yield BrowserTestUtils.openNewBrowserWindow();
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(window1.gBrowser, "http://example.com/");
+ window1.gBrowser.pinTab(tab1);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query(
+ {url: "<all_urls>"},
+ tabs => {
+ let destination = tabs[0];
+ let source = tabs[1]; // remember, pinning moves it to the left.
+ browser.tabs.move(source.id, {windowId: destination.windowId, index: 0});
+
+ browser.tabs.query(
+ {url: "<all_urls>"},
+ tabs => {
+ browser.test.assertEq(true, tabs[0].pinned);
+ browser.test.notifyPass("tabs.move.pin");
+ });
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.move.pin");
+ yield extension.unload();
+
+ for (let tab of window.gBrowser.tabs) {
+ yield BrowserTestUtils.removeTab(tab);
+ }
+ yield BrowserTestUtils.closeWindow(window1);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_onHighlighted.js b/browser/components/extensions/test/browser/browser_ext_tabs_onHighlighted.js
new file mode 100644
index 000000000..9cc2554d6
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_onHighlighted.js
@@ -0,0 +1,126 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testTabEvents() {
+ async function background() {
+ /** The list of active tab ID's */
+ let tabIds = [];
+
+ /**
+ * Stores the events that fire for each tab.
+ *
+ * events {
+ * tabId1: [event1, event2, ...],
+ * tabId2: [event1, event2, ...],
+ * }
+ */
+ let events = {};
+
+ browser.tabs.onActivated.addListener((info) => {
+ if (info.tabId in events) {
+ events[info.tabId].push("onActivated");
+ } else {
+ events[info.tabId] = ["onActivated"];
+ }
+ });
+
+ browser.tabs.onHighlighted.addListener((info) => {
+ if (info.tabIds[0] in events) {
+ events[info.tabIds[0]].push("onHighlighted");
+ } else {
+ events[info.tabIds[0]] = ["onHighlighted"];
+ }
+ });
+
+ /**
+ * Asserts that the expected events are fired for the tab with id = tabId.
+ * The events associated to the specified tab are removed after this check is made.
+ *
+ * @param {number} tabId
+ * @param {Array<string>} expectedEvents
+ */
+ async function expectEvents(tabId, expectedEvents) {
+ browser.test.log(`Expecting events: ${expectedEvents.join(", ")}`);
+
+ await new Promise(resolve => setTimeout(resolve, 0));
+
+ browser.test.assertEq(expectedEvents.length, events[tabId].length,
+ `Got expected number of events for ${tabId}`);
+
+ for (let [i, name] of expectedEvents.entries()) {
+ browser.test.assertEq(name, i in events[tabId] && events[tabId][i],
+ `Got expected ${name} event`);
+ }
+ delete events[tabId];
+ }
+
+ /**
+ * Opens a new tab and asserts that the correct events are fired.
+ *
+ * @param {number} windowId
+ */
+ async function openTab(windowId) {
+ let tab = await browser.tabs.create({windowId});
+
+ tabIds.push(tab.id);
+ browser.test.log(`Opened tab ${tab.id}`);
+
+ await expectEvents(tab.id, [
+ "onActivated",
+ "onHighlighted",
+ ]);
+ }
+
+ /**
+ * Highlights an existing tab and asserts that the correct events are fired.
+ *
+ * @param {number} tabId
+ */
+ async function highlightTab(tabId) {
+ browser.test.log(`Highlighting tab ${tabId}`);
+ let tab = await browser.tabs.update(tabId, {active: true});
+
+ browser.test.assertEq(tab.id, tabId, `Tab ${tab.id} highlighted`);
+
+ await expectEvents(tab.id, [
+ "onActivated",
+ "onHighlighted",
+ ]);
+ }
+
+ /**
+ * The main entry point to the tests.
+ */
+ let tabs = await browser.tabs.query({active: true, currentWindow: true});
+
+ let activeWindow = tabs[0].windowId;
+ await Promise.all([
+ openTab(activeWindow),
+ openTab(activeWindow),
+ openTab(activeWindow),
+ ]);
+
+ await Promise.all([
+ highlightTab(tabIds[0]),
+ highlightTab(tabIds[1]),
+ highlightTab(tabIds[2]),
+ ]);
+
+ await Promise.all(tabIds.map(id => browser.tabs.remove(id)));
+
+ browser.test.notifyPass("tabs.highlight");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.highlight");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_onUpdated.js b/browser/components/extensions/test/browser/browser_ext_tabs_onUpdated.js
new file mode 100644
index 000000000..2c26bbd16
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_onUpdated.js
@@ -0,0 +1,198 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+requestLongerTimeout(2);
+
+add_task(function* () {
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ yield focusWindow(win1);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ "content_scripts": [{
+ "matches": ["http://mochi.test/*/context_tabs_onUpdated_page.html"],
+ "js": ["content-script.js"],
+ "run_at": "document_start",
+ }],
+ },
+
+ background: function() {
+ let pageURL = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html";
+
+ let expectedSequence = [
+ {status: "loading"},
+ {status: "loading", url: pageURL},
+ {status: "complete"},
+ ];
+ let collectedSequence = [];
+
+ browser.tabs.onUpdated.addListener(function(tabId, updatedInfo) {
+ // onUpdated also fires with updatedInfo.faviconUrl, so explicitly
+ // check for updatedInfo.status before recording the event.
+ if ("status" in updatedInfo) {
+ collectedSequence.push(updatedInfo);
+ }
+ });
+
+ browser.runtime.onMessage.addListener(function() {
+ if (collectedSequence.length !== expectedSequence.length) {
+ browser.test.assertEq(
+ JSON.stringify(expectedSequence),
+ JSON.stringify(collectedSequence),
+ "got unexpected number of updateInfo data"
+ );
+ } else {
+ for (let i = 0; i < expectedSequence.length; i++) {
+ browser.test.assertEq(
+ expectedSequence[i].status,
+ collectedSequence[i].status,
+ "check updatedInfo status"
+ );
+ if (expectedSequence[i].url || collectedSequence[i].url) {
+ browser.test.assertEq(
+ expectedSequence[i].url,
+ collectedSequence[i].url,
+ "check updatedInfo url"
+ );
+ }
+ }
+ }
+
+ browser.test.notifyPass("tabs.onUpdated");
+ });
+
+ browser.tabs.create({url: pageURL});
+ },
+ files: {
+ "content-script.js": `
+ window.addEventListener("message", function(evt) {
+ if (evt.data == "frame-updated") {
+ browser.runtime.sendMessage("load-completed");
+ }
+ }, true);
+ `,
+ },
+ });
+
+ yield Promise.all([
+ extension.startup(),
+ extension.awaitFinish("tabs.onUpdated"),
+ ]);
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(win1);
+});
+
+function* do_test_update(background, withPermissions = true) {
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ yield focusWindow(win1);
+
+ let manifest = {};
+ if (withPermissions) {
+ manifest.permissions = ["tabs"];
+ }
+ let extension = ExtensionTestUtils.loadExtension({manifest, background});
+
+ yield Promise.all([
+ yield extension.startup(),
+ yield extension.awaitFinish("finish"),
+ ]);
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(win1);
+}
+
+add_task(function* test_pinned() {
+ yield do_test_update(function background() {
+ // Create a new tab for testing update.
+ browser.tabs.create({}, function(tab) {
+ browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
+ // Check callback
+ browser.test.assertEq(tabId, tab.id, "Check tab id");
+ browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
+ if ("pinned" in changeInfo) {
+ browser.test.assertTrue(changeInfo.pinned, "Check changeInfo.pinned");
+ browser.tabs.onUpdated.removeListener(onUpdated);
+ // Remove created tab.
+ browser.tabs.remove(tabId);
+ browser.test.notifyPass("finish");
+ return;
+ }
+ });
+ browser.tabs.update(tab.id, {pinned: true});
+ });
+ });
+});
+
+add_task(function* test_unpinned() {
+ yield do_test_update(function background() {
+ // Create a new tab for testing update.
+ browser.tabs.create({pinned: true}, function(tab) {
+ browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
+ // Check callback
+ browser.test.assertEq(tabId, tab.id, "Check tab id");
+ browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
+ if ("pinned" in changeInfo) {
+ browser.test.assertFalse(changeInfo.pinned, "Check changeInfo.pinned");
+ browser.tabs.onUpdated.removeListener(onUpdated);
+ // Remove created tab.
+ browser.tabs.remove(tabId);
+ browser.test.notifyPass("finish");
+ return;
+ }
+ });
+ browser.tabs.update(tab.id, {pinned: false});
+ });
+ });
+});
+
+add_task(function* test_url() {
+ yield do_test_update(function background() {
+ // Create a new tab for testing update.
+ browser.tabs.create({}, function(tab) {
+ browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
+ // Check callback
+ browser.test.assertEq(tabId, tab.id, "Check tab id");
+ browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
+ if ("url" in changeInfo) {
+ browser.test.assertEq("about:blank", changeInfo.url,
+ "Check changeInfo.url");
+ browser.tabs.onUpdated.removeListener(onUpdated);
+ // Remove created tab.
+ browser.tabs.remove(tabId);
+ browser.test.notifyPass("finish");
+ return;
+ }
+ });
+ browser.tabs.update(tab.id, {url: "about:blank"});
+ });
+ });
+});
+
+add_task(function* test_without_tabs_permission() {
+ yield do_test_update(function background() {
+ browser.tabs.create({url: "about:blank"}, function(tab) {
+ browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
+ if (tabId == tab.id) {
+ browser.test.assertFalse("url" in changeInfo, "url should not be included without tabs permission");
+ browser.test.assertFalse("favIconUrl" in changeInfo, "favIconUrl should not be included without tabs permission");
+
+ if (changeInfo.status == "complete") {
+ browser.tabs.onUpdated.removeListener(onUpdated);
+ browser.tabs.remove(tabId);
+ browser.test.notifyPass("finish");
+ }
+ }
+ });
+ browser.tabs.reload(tab.id);
+ });
+ }, false /* withPermissions */);
+});
+
+add_task(forceGC);
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_query.js b/browser/components/extensions/test/browser/browser_ext_tabs_query.js
new file mode 100644
index 000000000..7804d1454
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_query.js
@@ -0,0 +1,224 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+requestLongerTimeout(2);
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:config");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ lastFocusedWindow: true,
+ }, function(tabs) {
+ browser.test.assertEq(tabs.length, 3, "should have three tabs");
+
+ tabs.sort((tab1, tab2) => tab1.index - tab2.index);
+
+ browser.test.assertEq(tabs[0].url, "about:blank", "first tab blank");
+ tabs.shift();
+
+ browser.test.assertTrue(tabs[0].active, "tab 0 active");
+ browser.test.assertFalse(tabs[1].active, "tab 1 inactive");
+
+ browser.test.assertFalse(tabs[0].pinned, "tab 0 unpinned");
+ browser.test.assertFalse(tabs[1].pinned, "tab 1 unpinned");
+
+ browser.test.assertEq(tabs[0].url, "about:robots", "tab 0 url correct");
+ browser.test.assertEq(tabs[1].url, "about:config", "tab 1 url correct");
+
+ browser.test.assertEq(tabs[0].status, "complete", "tab 0 status correct");
+ browser.test.assertEq(tabs[1].status, "complete", "tab 1 status correct");
+
+ browser.test.assertEq(tabs[0].title, "Gort! Klaatu barada nikto!", "tab 0 title correct");
+
+ browser.test.notifyPass("tabs.query");
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.query");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+
+ tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+ tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+ let tab3 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://test1.example.org/MochiKit/");
+
+ // test simple queries
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ url: "<all_urls>",
+ }, function(tabs) {
+ browser.test.assertEq(tabs.length, 3, "should have three tabs");
+
+ tabs.sort((tab1, tab2) => tab1.index - tab2.index);
+
+ browser.test.assertEq(tabs[0].url, "http://example.com/", "tab 0 url correct");
+ browser.test.assertEq(tabs[1].url, "http://example.net/", "tab 1 url correct");
+ browser.test.assertEq(tabs[2].url, "http://test1.example.org/MochiKit/", "tab 2 url correct");
+
+ browser.test.notifyPass("tabs.query");
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.query");
+ yield extension.unload();
+
+ // match pattern
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ url: "http://*/MochiKit*",
+ }, function(tabs) {
+ browser.test.assertEq(tabs.length, 1, "should have one tab");
+
+ browser.test.assertEq(tabs[0].url, "http://test1.example.org/MochiKit/", "tab 0 url correct");
+
+ browser.test.notifyPass("tabs.query");
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.query");
+ yield extension.unload();
+
+ // match array of patterns
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ url: ["http://*/MochiKit*", "http://*.com/*"],
+ }, function(tabs) {
+ browser.test.assertEq(tabs.length, 2, "should have two tabs");
+
+ tabs.sort((tab1, tab2) => tab1.index - tab2.index);
+
+ browser.test.assertEq(tabs[0].url, "http://example.com/", "tab 0 url correct");
+ browser.test.assertEq(tabs[1].url, "http://test1.example.org/MochiKit/", "tab 1 url correct");
+
+ browser.test.notifyPass("tabs.query");
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.query");
+ yield extension.unload();
+
+ // test width and height
+ extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.test.onMessage.addListener(async msg => {
+ let tabs = await browser.tabs.query({active: true});
+
+ browser.test.assertEq(tabs.length, 1, "should have one tab");
+ browser.test.sendMessage("dims", {width: tabs[0].width, height: tabs[0].height});
+ });
+ browser.test.sendMessage("ready");
+ },
+ });
+
+ const RESOLUTION_PREF = "layout.css.devPixelsPerPx";
+ registerCleanupFunction(() => {
+ SpecialPowers.clearUserPref(RESOLUTION_PREF);
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("ready")]);
+
+ for (let resolution of [2, 1]) {
+ SpecialPowers.setCharPref(RESOLUTION_PREF, String(resolution));
+ is(window.devicePixelRatio, resolution, "window has the required resolution");
+
+ let {clientHeight, clientWidth} = gBrowser.selectedBrowser;
+
+ extension.sendMessage("check-size");
+ let dims = yield extension.awaitMessage("dims");
+ is(dims.width, clientWidth, "tab reports expected width");
+ is(dims.height, clientHeight, "tab reports expected height");
+ }
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+ yield BrowserTestUtils.removeTab(tab3);
+ SpecialPowers.clearUserPref(RESOLUTION_PREF);
+});
+
+add_task(function* testQueryPermissions() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": [],
+ },
+
+ async background() {
+ try {
+ let tabs = await browser.tabs.query({currentWindow: true, active: true});
+ browser.test.assertEq(tabs.length, 1, "Expect query to return tabs");
+ browser.test.notifyPass("queryPermissions");
+ } catch (e) {
+ browser.test.notifyFail("queryPermissions");
+ }
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("queryPermissions");
+
+ yield extension.unload();
+});
+
+add_task(function* testQueryWithURLPermissions() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": [],
+ },
+
+ async background() {
+ await browser.test.assertRejects(
+ browser.tabs.query({"url": "http://www.bbc.com/"}),
+ 'The "tabs" permission is required to use the query API with the "url" parameter',
+ "Expected tabs.query with 'url' to fail with permissions error message");
+
+ browser.test.notifyPass("queryWithURLPermissions");
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("queryWithURLPermissions");
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_reload.js b/browser/components/extensions/test/browser/browser_ext_tabs_reload.js
new file mode 100644
index 000000000..99b2d426b
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_reload.js
@@ -0,0 +1,54 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ files: {
+ "tab.js": function() {
+ browser.runtime.sendMessage("tab-loaded");
+ },
+ "tab.html":
+ `<head>
+ <meta charset="utf-8">
+ <script src="tab.js"></script>
+ </head>`,
+ },
+
+ async background() {
+ let tabLoadedCount = 0;
+
+ let tab = await browser.tabs.create({url: "tab.html", active: true});
+
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "tab-loaded") {
+ tabLoadedCount++;
+
+ if (tabLoadedCount == 1) {
+ // Reload the tab once passing no arguments.
+ return browser.tabs.reload();
+ }
+
+ if (tabLoadedCount == 2) {
+ // Reload the tab again with explicit arguments.
+ return browser.tabs.reload(tab.id, {
+ bypassCache: false,
+ });
+ }
+
+ if (tabLoadedCount == 3) {
+ browser.test.notifyPass("tabs.reload");
+ }
+ }
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.reload");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js b/browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js
new file mode 100644
index 000000000..648361724
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_reload_bypass_cache.js
@@ -0,0 +1,58 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs", "<all_urls>"],
+ },
+
+ async background() {
+ const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
+ const URL = BASE + "file_bypass_cache.sjs";
+
+ function awaitLoad(tabId) {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
+ if (tabId == tabId_ && changed.status == "complete" && tab.url == URL) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ }
+
+ try {
+ let tab = await browser.tabs.create({url: URL});
+ await awaitLoad(tab.id);
+
+ await browser.tabs.reload(tab.id, {bypassCache: false});
+ await awaitLoad(tab.id);
+
+ let [textContent] = await browser.tabs.executeScript(tab.id, {code: "document.body.textContent"});
+ browser.test.assertEq("", textContent, "`textContent` should be empty when bypassCache=false");
+
+ await browser.tabs.reload(tab.id, {bypassCache: true});
+ await awaitLoad(tab.id);
+
+ [textContent] = await browser.tabs.executeScript(tab.id, {code: "document.body.textContent"});
+
+ let [pragma, cacheControl] = textContent.split(":");
+ browser.test.assertEq("no-cache", pragma, "`pragma` should be set to `no-cache` when bypassCache is true");
+ browser.test.assertEq("no-cache", cacheControl, "`cacheControl` should be set to `no-cache` when bypassCache is true");
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("tabs.reload_bypass_cache");
+ } catch (error) {
+ browser.test.fail(`${error} :: ${error.stack}`);
+ browser.test.notifyFail("tabs.reload_bypass_cache");
+ }
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("tabs.reload_bypass_cache");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_removeCSS.js b/browser/components/extensions/test/browser/browser_ext_tabs_removeCSS.js
new file mode 100644
index 000000000..e0eadab64
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_removeCSS.js
@@ -0,0 +1,95 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testExecuteScript() {
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://mochi.test:8888/", true);
+
+ async function background() {
+ let tasks = [
+ // Insert CSS file.
+ {
+ background: "transparent",
+ foreground: "rgb(0, 113, 4)",
+ promise: () => {
+ return browser.tabs.insertCSS({
+ file: "file2.css",
+ });
+ },
+ },
+ // Insert CSS code.
+ {
+ background: "rgb(42, 42, 42)",
+ foreground: "rgb(0, 113, 4)",
+ promise: () => {
+ return browser.tabs.insertCSS({
+ code: "* { background: rgb(42, 42, 42) }",
+ });
+ },
+ },
+ // Remove CSS code again.
+ {
+ background: "transparent",
+ foreground: "rgb(0, 113, 4)",
+ promise: () => {
+ return browser.tabs.removeCSS({
+ code: "* { background: rgb(42, 42, 42) }",
+ });
+ },
+ },
+ // Remove CSS file again.
+ {
+ background: "transparent",
+ foreground: "rgb(0, 0, 0)",
+ promise: () => {
+ return browser.tabs.removeCSS({
+ file: "file2.css",
+ });
+ },
+ },
+ ];
+
+ function checkCSS() {
+ let computedStyle = window.getComputedStyle(document.body);
+ return [computedStyle.backgroundColor, computedStyle.color];
+ }
+
+ try {
+ for (let {promise, background, foreground} of tasks) {
+ let result = await promise();
+ browser.test.assertEq(undefined, result, "Expected callback result");
+
+ [result] = await browser.tabs.executeScript({
+ code: `(${checkCSS})()`,
+ });
+ browser.test.assertEq(background, result[0], "Expected background color");
+ browser.test.assertEq(foreground, result[1], "Expected foreground color");
+ }
+
+ browser.test.notifyPass("removeCSS");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFailure("removeCSS");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["http://mochi.test/"],
+ },
+
+ background,
+
+ files: {
+ "file2.css": "* { color: rgb(0, 113, 4) }",
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("removeCSS");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js b/browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js
new file mode 100644
index 000000000..64e97afb1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js
@@ -0,0 +1,227 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* tabsSendMessageReply() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "content_scripts": [{
+ "matches": ["http://example.com/"],
+ "js": ["content-script.js"],
+ "run_at": "document_start",
+ }],
+ },
+
+ background: async function() {
+ let firstTab;
+ let promiseResponse = new Promise(resolve => {
+ browser.runtime.onMessage.addListener((msg, sender, respond) => {
+ if (msg == "content-script-ready") {
+ let tabId = sender.tab.id;
+
+ Promise.all([
+ promiseResponse,
+
+ browser.tabs.sendMessage(tabId, "respond-now"),
+ browser.tabs.sendMessage(tabId, "respond-now-2"),
+ new Promise(resolve => browser.tabs.sendMessage(tabId, "respond-soon", resolve)),
+ browser.tabs.sendMessage(tabId, "respond-promise"),
+ browser.tabs.sendMessage(tabId, "respond-never"),
+ new Promise(resolve => {
+ browser.runtime.sendMessage("respond-never", response => { resolve(response); });
+ }),
+
+ browser.tabs.sendMessage(tabId, "respond-error").catch(error => Promise.resolve({error})),
+ browser.tabs.sendMessage(tabId, "throw-error").catch(error => Promise.resolve({error})),
+
+ browser.tabs.sendMessage(firstTab, "no-listener").catch(error => Promise.resolve({error})),
+ ]).then(([response, respondNow, respondNow2, respondSoon, respondPromise, respondNever, respondNever2, respondError, throwError, noListener]) => {
+ browser.test.assertEq("expected-response", response, "Content script got the expected response");
+
+ browser.test.assertEq("respond-now", respondNow, "Got the expected immediate response");
+ browser.test.assertEq("respond-now-2", respondNow2, "Got the expected immediate response from the second listener");
+ browser.test.assertEq("respond-soon", respondSoon, "Got the expected delayed response");
+ browser.test.assertEq("respond-promise", respondPromise, "Got the expected promise response");
+ browser.test.assertEq(undefined, respondNever, "Got the expected no-response resolution");
+ browser.test.assertEq(undefined, respondNever2, "Got the expected no-response resolution");
+
+ browser.test.assertEq("respond-error", respondError.error.message, "Got the expected error response");
+ browser.test.assertEq("throw-error", throwError.error.message, "Got the expected thrown error response");
+
+ browser.test.assertEq("Could not establish connection. Receiving end does not exist.",
+ noListener.error.message,
+ "Got the expected no listener response");
+
+ return browser.tabs.remove(tabId);
+ }).then(() => {
+ browser.test.notifyPass("sendMessage");
+ });
+
+ return Promise.resolve("expected-response");
+ } else if (msg[0] == "got-response") {
+ resolve(msg[1]);
+ }
+ });
+ });
+
+ let tabs = await browser.tabs.query({currentWindow: true, active: true});
+ firstTab = tabs[0].id;
+ browser.tabs.create({url: "http://example.com/"});
+ },
+
+ files: {
+ "content-script.js": async function() {
+ browser.runtime.onMessage.addListener((msg, sender, respond) => {
+ if (msg == "respond-now") {
+ respond(msg);
+ } else if (msg == "respond-soon") {
+ setTimeout(() => { respond(msg); }, 0);
+ return true;
+ } else if (msg == "respond-promise") {
+ return Promise.resolve(msg);
+ } else if (msg == "respond-never") {
+ return;
+ } else if (msg == "respond-error") {
+ return Promise.reject(new Error(msg));
+ } else if (msg == "throw-error") {
+ throw new Error(msg);
+ }
+ });
+
+ browser.runtime.onMessage.addListener((msg, sender, respond) => {
+ if (msg == "respond-now") {
+ respond("hello");
+ } else if (msg == "respond-now-2") {
+ respond(msg);
+ }
+ });
+
+ let response = await browser.runtime.sendMessage("content-script-ready");
+ browser.runtime.sendMessage(["got-response", response]);
+ },
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("sendMessage");
+
+ yield extension.unload();
+});
+
+
+add_task(function* tabsSendHidden() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+
+ "content_scripts": [{
+ "matches": ["http://example.com/content*"],
+ "js": ["content-script.js"],
+ "run_at": "document_start",
+ }],
+ },
+
+ background: async function() {
+ let resolveContent;
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ if (msg[0] == "content-ready") {
+ resolveContent(msg[1]);
+ }
+ });
+
+ let awaitContent = url => {
+ return new Promise(resolve => {
+ resolveContent = resolve;
+ }).then(result => {
+ browser.test.assertEq(url, result, "Expected content script URL");
+ });
+ };
+
+ try {
+ const URL1 = "http://example.com/content1.html";
+ const URL2 = "http://example.com/content2.html";
+
+ let tab = await browser.tabs.create({url: URL1});
+ await awaitContent(URL1);
+
+ let url = await browser.tabs.sendMessage(tab.id, URL1);
+ browser.test.assertEq(URL1, url, "Should get response from expected content window");
+
+ await browser.tabs.update(tab.id, {url: URL2});
+ await awaitContent(URL2);
+
+ url = await browser.tabs.sendMessage(tab.id, URL2);
+ browser.test.assertEq(URL2, url, "Should get response from expected content window");
+
+ // Repeat once just to be sure the first message was processed by all
+ // listeners before we exit the test.
+ url = await browser.tabs.sendMessage(tab.id, URL2);
+ browser.test.assertEq(URL2, url, "Should get response from expected content window");
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("contentscript-bfcache-window");
+ } catch (error) {
+ browser.test.fail(`Error: ${error} :: ${error.stack}`);
+ browser.test.notifyFail("contentscript-bfcache-window");
+ }
+ },
+
+ files: {
+ "content-script.js": function() {
+ // Store this in a local variable to make sure we don't touch any
+ // properties of the possibly-hidden content window.
+ let href = window.location.href;
+
+ browser.runtime.onMessage.addListener((msg, sender) => {
+ browser.test.assertEq(href, msg, "Should be in the expected content window");
+
+ return Promise.resolve(href);
+ });
+
+ browser.runtime.sendMessage(["content-ready", href]);
+ },
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("contentscript-bfcache-window");
+
+ yield extension.unload();
+});
+
+
+add_task(function* tabsSendMessageNoExceptionOnNonExistentTab() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ async background() {
+ let url = "http://example.com/mochitest/browser/browser/components/extensions/test/browser/file_dummy.html";
+ let tab = await browser.tabs.create({url});
+
+ try {
+ browser.tabs.sendMessage(tab.id, "message");
+ browser.tabs.sendMessage(tab.id + 100, "message");
+ } catch (e) {
+ browser.test.fail("no exception should be raised on tabs.sendMessage to nonexistent tabs");
+ }
+
+ await browser.tabs.remove(tab.id);
+
+ browser.test.notifyPass("tabs.sendMessage");
+ },
+ });
+
+ yield Promise.all([
+ extension.startup(),
+ extension.awaitFinish("tabs.sendMessage"),
+ ]);
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_update.js b/browser/components/extensions/test/browser/browser_ext_tabs_update.js
new file mode 100644
index 000000000..8e56a746c
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_update.js
@@ -0,0 +1,45 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:config");
+
+ gBrowser.selectedTab = tab1;
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background: function() {
+ browser.tabs.query({
+ lastFocusedWindow: true,
+ }, function(tabs) {
+ browser.test.assertEq(tabs.length, 3, "should have three tabs");
+
+ tabs.sort((tab1, tab2) => tab1.index - tab2.index);
+
+ browser.test.assertEq(tabs[0].url, "about:blank", "first tab blank");
+ tabs.shift();
+
+ browser.test.assertTrue(tabs[0].active, "tab 0 active");
+ browser.test.assertFalse(tabs[1].active, "tab 1 inactive");
+
+ browser.tabs.update(tabs[1].id, {active: true}, function() {
+ browser.test.sendMessage("check");
+ });
+ });
+ },
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("check")]);
+
+ ok(gBrowser.selectedTab == tab2, "correct tab selected");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_update_url.js b/browser/components/extensions/test/browser/browser_ext_tabs_update_url.js
new file mode 100644
index 000000000..b43855fb1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_update_url.js
@@ -0,0 +1,110 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected) {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ files: {
+ "tab.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body>
+ <h1>tab page</h1>
+ </body>
+ </html>
+ `.trim(),
+ },
+ background: function() {
+ browser.test.sendMessage("ready", browser.runtime.getURL("tab.html"));
+
+ browser.test.onMessage.addListener(async (msg, tabsUpdateURL, isErrorExpected) => {
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+
+ try {
+ let tab = await browser.tabs.update(tabs[1].id, {url: tabsUpdateURL});
+
+ browser.test.assertFalse(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should be rejected`);
+ browser.test.assertTrue(tab, "on success the tab should be defined");
+ } catch (error) {
+ browser.test.assertTrue(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should not be rejected`);
+ browser.test.assertTrue(/^Illegal URL/.test(error.message),
+ "tabs.update should be rejected with the expected error message");
+ }
+
+ browser.test.sendMessage("done");
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ let mozExtTabURL = yield extension.awaitMessage("ready");
+
+ if (tabsUpdateURL == "self") {
+ tabsUpdateURL = mozExtTabURL;
+ }
+
+ info(`tab.update URL "${tabsUpdateURL}" on tab with URL "${existentTabURL}"`);
+
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, existentTabURL);
+
+ extension.sendMessage("start", tabsUpdateURL, isErrorExpected);
+ yield extension.awaitMessage("done");
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield extension.unload();
+}
+
+add_task(function* () {
+ info("Start testing tabs.update on javascript URLs");
+
+ let dataURLPage = `data:text/html,
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body>
+ <h1>data url page</h1>
+ </body>
+ </html>`;
+
+ let checkList = [
+ {
+ tabsUpdateURL: "http://example.net",
+ isErrorExpected: false,
+ },
+ {
+ tabsUpdateURL: "self",
+ isErrorExpected: false,
+ },
+ {
+ tabsUpdateURL: "about:addons",
+ isErrorExpected: true,
+ },
+ {
+ tabsUpdateURL: "javascript:console.log('tabs.update execute javascript')",
+ isErrorExpected: true,
+ },
+ {
+ tabsUpdateURL: dataURLPage,
+ isErrorExpected: true,
+ },
+ ];
+
+ let testCases = checkList
+ .map((check) => Object.assign({}, check, {existentTabURL: "about:blank"}));
+
+ for (let {existentTabURL, tabsUpdateURL, isErrorExpected} of testCases) {
+ yield* testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected);
+ }
+
+ info("done");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_tabs_zoom.js b/browser/components/extensions/test/browser/browser_ext_tabs_zoom.js
new file mode 100644
index 000000000..c2e54d3ea
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_zoom.js
@@ -0,0 +1,222 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+const SITE_SPECIFIC_PREF = "browser.zoom.siteSpecific";
+
+add_task(function* () {
+ let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+ let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
+
+ gBrowser.selectedTab = tab1;
+
+ async function background() {
+ function promiseUpdated(tabId, attr) {
+ return new Promise(resolve => {
+ let onUpdated = (tabId_, changeInfo, tab) => {
+ if (tabId == tabId_ && attr in changeInfo) {
+ browser.tabs.onUpdated.removeListener(onUpdated);
+
+ resolve({changeInfo, tab});
+ }
+ };
+ browser.tabs.onUpdated.addListener(onUpdated);
+ });
+ }
+
+ let deferred = {};
+ browser.test.onMessage.addListener((message, msg, result) => {
+ if (message == "msg-done" && deferred[msg]) {
+ deferred[msg].resolve(result);
+ }
+ });
+
+ let _id = 0;
+ function msg(...args) {
+ return new Promise((resolve, reject) => {
+ let id = ++_id;
+ deferred[id] = {resolve, reject};
+ browser.test.sendMessage("msg", id, ...args);
+ });
+ }
+
+
+ let zoomEvents = [];
+ let eventPromises = [];
+ browser.tabs.onZoomChange.addListener(info => {
+ zoomEvents.push(info);
+ if (eventPromises.length) {
+ eventPromises.shift().resolve();
+ }
+ });
+
+ let awaitZoom = async (tabId, newValue) => {
+ let listener;
+
+ await new Promise(async resolve => {
+ listener = info => {
+ if (info.tabId == tabId && info.newZoomFactor == newValue) {
+ resolve();
+ }
+ };
+ browser.tabs.onZoomChange.addListener(listener);
+
+ let zoomFactor = await browser.tabs.getZoom(tabId);
+ if (zoomFactor == newValue) {
+ resolve();
+ }
+ });
+
+ browser.tabs.onZoomChange.removeListener(listener);
+ };
+
+ let checkZoom = async (tabId, newValue, oldValue = null) => {
+ let awaitEvent;
+ if (oldValue != null && !zoomEvents.length) {
+ awaitEvent = new Promise(resolve => {
+ eventPromises.push({resolve});
+ });
+ }
+
+ let [apiZoom, realZoom] = await Promise.all([
+ browser.tabs.getZoom(tabId),
+ msg("get-zoom", tabId),
+ awaitEvent,
+ ]);
+
+ browser.test.assertEq(newValue, apiZoom, `Got expected zoom value from API`);
+ browser.test.assertEq(newValue, realZoom, `Got expected zoom value from parent`);
+
+ if (oldValue != null) {
+ let event = zoomEvents.shift();
+ browser.test.assertEq(tabId, event.tabId, `Got expected zoom event tab ID`);
+ browser.test.assertEq(newValue, event.newZoomFactor, `Got expected zoom event zoom factor`);
+ browser.test.assertEq(oldValue, event.oldZoomFactor, `Got expected zoom event old zoom factor`);
+
+ browser.test.assertEq(3, Object.keys(event.zoomSettings).length, `Zoom settings should have 3 keys`);
+ browser.test.assertEq("automatic", event.zoomSettings.mode, `Mode should be "automatic"`);
+ browser.test.assertEq("per-origin", event.zoomSettings.scope, `Scope should be "per-origin"`);
+ browser.test.assertEq(1, event.zoomSettings.defaultZoomFactor, `Default zoom should be 1`);
+ }
+ };
+
+ try {
+ let tabs = await browser.tabs.query({lastFocusedWindow: true});
+ browser.test.assertEq(tabs.length, 3, "We have three tabs");
+
+ let tabIds = [tabs[1].id, tabs[2].id];
+ await checkZoom(tabIds[0], 1);
+
+ await browser.tabs.setZoom(tabIds[0], 2);
+ await checkZoom(tabIds[0], 2, 1);
+
+ let zoomSettings = await browser.tabs.getZoomSettings(tabIds[0]);
+ browser.test.assertEq(3, Object.keys(zoomSettings).length, `Zoom settings should have 3 keys`);
+ browser.test.assertEq("automatic", zoomSettings.mode, `Mode should be "automatic"`);
+ browser.test.assertEq("per-origin", zoomSettings.scope, `Scope should be "per-origin"`);
+ browser.test.assertEq(1, zoomSettings.defaultZoomFactor, `Default zoom should be 1`);
+
+
+ browser.test.log(`Switch to tab 2`);
+ await browser.tabs.update(tabIds[1], {active: true});
+ await checkZoom(tabIds[1], 1);
+
+
+ browser.test.log(`Navigate tab 2 to origin of tab 1`);
+ browser.tabs.update(tabIds[1], {url: "http://example.com"});
+ await promiseUpdated(tabIds[1], "url");
+ await checkZoom(tabIds[1], 2, 1);
+
+
+ browser.test.log(`Update zoom in tab 2, expect changes in both tabs`);
+ await browser.tabs.setZoom(tabIds[1], 1.5);
+ await checkZoom(tabIds[1], 1.5, 2);
+
+
+ browser.test.log(`Switch to tab 1, expect asynchronous zoom change just after the switch`);
+ await Promise.all([
+ awaitZoom(tabIds[0], 1.5),
+ browser.tabs.update(tabIds[0], {active: true}),
+ ]);
+ await checkZoom(tabIds[0], 1.5, 2);
+
+
+ browser.test.log("Set zoom to 0, expect it set to 1");
+ await browser.tabs.setZoom(tabIds[0], 0);
+ await checkZoom(tabIds[0], 1, 1.5);
+
+
+ browser.test.log("Change zoom externally, expect changes reflected");
+ await msg("enlarge");
+ await checkZoom(tabIds[0], 1.1, 1);
+
+ await Promise.all([
+ browser.tabs.setZoom(tabIds[0], 0),
+ browser.tabs.setZoom(tabIds[1], 0),
+ ]);
+ await Promise.all([
+ checkZoom(tabIds[0], 1, 1.1),
+ checkZoom(tabIds[1], 1, 1.5),
+ ]);
+
+
+ browser.test.log("Check that invalid zoom values throw an error");
+ await browser.test.assertRejects(
+ browser.tabs.setZoom(tabIds[0], 42),
+ /Zoom value 42 out of range/,
+ "Expected an out of range error");
+
+ browser.test.log("Disable site-specific zoom, expect correct scope");
+ await msg("site-specific", false);
+ zoomSettings = await browser.tabs.getZoomSettings(tabIds[0]);
+
+ browser.test.assertEq("per-tab", zoomSettings.scope, `Scope should be "per-tab"`);
+ await msg("site-specific", null);
+
+ browser.test.notifyPass("tab-zoom");
+ } catch (e) {
+ browser.test.fail(`Error: ${e} :: ${e.stack}`);
+ browser.test.notifyFail("tab-zoom");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ extension.onMessage("msg", (id, msg, ...args) => {
+ let {Management: {global: {TabManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let resp;
+ if (msg == "get-zoom") {
+ let tab = TabManager.getTab(args[0]);
+ resp = ZoomManager.getZoomForBrowser(tab.linkedBrowser);
+ } else if (msg == "set-zoom") {
+ let tab = TabManager.getTab(args[0]);
+ ZoomManager.setZoomForBrowser(tab.linkedBrowser);
+ } else if (msg == "enlarge") {
+ FullZoom.enlarge();
+ } else if (msg == "site-specific") {
+ if (args[0] == null) {
+ SpecialPowers.clearUserPref(SITE_SPECIFIC_PREF);
+ } else {
+ SpecialPowers.setBoolPref(SITE_SPECIFIC_PREF, args[0]);
+ }
+ }
+
+ extension.sendMessage("msg-done", id, resp);
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitFinish("tab-zoom");
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab1);
+ yield BrowserTestUtils.removeTab(tab2);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_topwindowid.js b/browser/components/extensions/test/browser/browser_ext_topwindowid.js
new file mode 100644
index 000000000..9176ac946
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_topwindowid.js
@@ -0,0 +1,23 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_topwindowid_cleanup() {
+ let {Frames} = Cu.import("resource://gre/modules/ExtensionManagement.jsm", {});
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com/");
+
+ let {outerWindowID, messageManager} = tab.linkedBrowser;
+
+ ok(Frames.topWindowIds.has(outerWindowID), "Outer window ID is registered");
+
+ let awaitDisconnect = TestUtils.topicObserved("message-manager-disconnect",
+ subject => subject === messageManager);
+
+ yield BrowserTestUtils.removeTab(tab);
+
+ yield awaitDisconnect;
+
+ ok(!Frames.topWindowIds.has(outerWindowID), "Outer window ID is no longer registered");
+});
+
diff --git a/browser/components/extensions/test/browser/browser_ext_webNavigation_frameId0.js b/browser/components/extensions/test/browser/browser_ext_webNavigation_frameId0.js
new file mode 100644
index 000000000..0058ca065
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_frameId0.js
@@ -0,0 +1,45 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* webNavigation_getFrameId_of_existing_main_frame() {
+ // Whether the frame ID in the extension API is 0 is determined by a map that
+ // is maintained by |Frames| in ExtensionManagement.jsm. This map is filled
+ // using data from content processes. But if ExtensionManagement.jsm is not
+ // imported, then the "Extension:TopWindowID" message gets lost.
+ // As a result, if the state is not synchronized again, the webNavigation API
+ // will mistakenly report a non-zero frame ID for top-level frames.
+ //
+ // If you want to be absolutely sure that the frame ID is correct, don't open
+ // tabs before starting an extension, or explicitly load the module in the
+ // main process:
+ // Cu.import("resource://gre/modules/ExtensionManagement.jsm", {});
+ //
+ // Or simply run the test again.
+ const BASE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
+ const DUMMY_URL = BASE + "file_dummy.html";
+ let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, DUMMY_URL, true);
+
+ async function background(DUMMY_URL) {
+ let tabs = await browser.tabs.query({active: true, currentWindow: true});
+ let frames = await browser.webNavigation.getAllFrames({tabId: tabs[0].id});
+ browser.test.assertEq(1, frames.length, "The dummy page has one frame");
+ browser.test.assertEq(0, frames[0].frameId, "Main frame's ID must be 0");
+ browser.test.assertEq(DUMMY_URL, frames[0].url, "Main frame URL must match");
+ browser.test.notifyPass("frameId checked");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["webNavigation"],
+ },
+
+ background: `(${background})(${JSON.stringify(DUMMY_URL)});`,
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("frameId checked");
+ yield extension.unload();
+
+ yield BrowserTestUtils.removeTab(tab);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_webNavigation_getFrames.js b/browser/components/extensions/test/browser/browser_ext_webNavigation_getFrames.js
new file mode 100644
index 000000000..6b4a597ad
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_getFrames.js
@@ -0,0 +1,168 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testWebNavigationGetNonExistentTab() {
+ let extension = ExtensionTestUtils.loadExtension({
+ background: async function() {
+ // There is no "tabId = 0" because the id assigned by TabManager (defined in ext-utils.js)
+ // starts from 1.
+ await browser.test.assertRejects(
+ browser.webNavigation.getAllFrames({tabId: 0}),
+ "Invalid tab ID: 0",
+ "getAllFrames rejected Promise should pass the expected error");
+
+ // There is no "tabId = 0" because the id assigned by TabManager (defined in ext-utils.js)
+ // starts from 1, processId is currently marked as optional and it is ignored.
+ await browser.test.assertRejects(
+ browser.webNavigation.getFrame({tabId: 0, frameId: 15, processId: 20}),
+ "Invalid tab ID: 0",
+ "getFrame rejected Promise should pass the expected error");
+
+ browser.test.sendMessage("getNonExistentTab.done");
+ },
+ manifest: {
+ permissions: ["webNavigation"],
+ },
+ });
+ info("load complete");
+
+ yield extension.startup();
+ info("startup complete");
+
+ yield extension.awaitMessage("getNonExistentTab.done");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
+
+add_task(function* testWebNavigationFrames() {
+ let extension = ExtensionTestUtils.loadExtension({
+ background: async function() {
+ let tabId;
+ let collectedDetails = [];
+
+ browser.webNavigation.onCompleted.addListener(async details => {
+ collectedDetails.push(details);
+
+ if (details.frameId !== 0) {
+ // wait for the top level iframe to be complete
+ return;
+ }
+
+ let getAllFramesDetails = await browser.webNavigation.getAllFrames({tabId});
+
+ let getFramePromises = getAllFramesDetails.map(({frameId}) => {
+ // processId is currently marked as optional and it is ignored.
+ return browser.webNavigation.getFrame({tabId, frameId, processId: 0});
+ });
+
+ let getFrameResults = await Promise.all(getFramePromises);
+ browser.test.sendMessage("webNavigationFrames.done", {
+ collectedDetails, getAllFramesDetails, getFrameResults,
+ });
+
+ // Pick a random frameId.
+ let nonExistentFrameId = Math.floor(Math.random() * 10000);
+
+ // Increment the picked random nonExistentFrameId until it doesn't exists.
+ while (getAllFramesDetails.filter((details) => details.frameId == nonExistentFrameId).length > 0) {
+ nonExistentFrameId += 1;
+ }
+
+ // Check that getFrame Promise is rejected with the expected error message on nonexistent frameId.
+ await browser.test.assertRejects(
+ browser.webNavigation.getFrame({tabId, frameId: nonExistentFrameId, processId: 20}),
+ `No frame found with frameId: ${nonExistentFrameId}`,
+ "getFrame promise should be rejected with the expected error message on unexistent frameId");
+
+ await browser.tabs.remove(tabId);
+ browser.test.sendMessage("webNavigationFrames.done");
+ });
+
+ let tab = await browser.tabs.create({url: "tab.html"});
+ tabId = tab.id;
+ },
+ manifest: {
+ permissions: ["webNavigation", "tabs"],
+ },
+ files: {
+ "tab.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body>
+ <iframe src="subframe.html"></iframe>
+ <iframe src="subframe.html"></iframe>
+ </body>
+ </html>
+ `,
+ "subframe.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ </html>
+ `,
+ },
+ });
+ info("load complete");
+
+ yield extension.startup();
+ info("startup complete");
+
+ let {
+ collectedDetails,
+ getAllFramesDetails,
+ getFrameResults,
+ } = yield extension.awaitMessage("webNavigationFrames.done");
+
+ is(getAllFramesDetails.length, 3, "expected number of frames found");
+ is(getAllFramesDetails.length, collectedDetails.length,
+ "number of frames found should equal the number onCompleted events collected");
+
+ is(getAllFramesDetails[0].frameId, 0, "the root frame has the expected frameId");
+ is(getAllFramesDetails[0].parentFrameId, -1, "the root frame has the expected parentFrameId");
+
+ // ordered by frameId
+ let sortByFrameId = (el1, el2) => {
+ let val1 = el1 ? el1.frameId : -1;
+ let val2 = el2 ? el2.frameId : -1;
+ return val1 - val2;
+ };
+
+ collectedDetails = collectedDetails.sort(sortByFrameId);
+ getAllFramesDetails = getAllFramesDetails.sort(sortByFrameId);
+ getFrameResults = getFrameResults.sort(sortByFrameId);
+
+ info("check frame details content");
+
+ is(getFrameResults.length, getAllFramesDetails.length,
+ "getFrame and getAllFrames should return the same number of results");
+
+ Assert.deepEqual(getFrameResults, getAllFramesDetails,
+ "getFrame and getAllFrames should return the same results");
+
+ info(`check frame details collected and retrieved with getAllFrames`);
+
+ for (let [i, collected] of collectedDetails.entries()) {
+ let getAllFramesDetail = getAllFramesDetails[i];
+
+ is(getAllFramesDetail.frameId, collected.frameId, "frameId");
+ is(getAllFramesDetail.parentFrameId, collected.parentFrameId, "parentFrameId");
+ is(getAllFramesDetail.tabId, collected.tabId, "tabId");
+
+ // This can be uncommented once Bug 1246125 has been fixed
+ // is(getAllFramesDetail.url, collected.url, "url");
+ }
+
+ info("frame details content checked");
+
+ yield extension.awaitMessage("webNavigationFrames.done");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_webNavigation_urlbar_transitions.js b/browser/components/extensions/test/browser/browser_ext_webNavigation_urlbar_transitions.js
new file mode 100644
index 000000000..f2ea0d901
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_urlbar_transitions.js
@@ -0,0 +1,251 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+ "resource://gre/modules/PlacesUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
+ "resource://testing-common/PlacesTestUtils.jsm");
+
+const SUGGEST_URLBAR_PREF = "browser.urlbar.suggest.searches";
+const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
+
+function* promiseAutocompleteResultPopup(inputText) {
+ gURLBar.focus();
+ gURLBar.value = inputText;
+ gURLBar.controller.startSearch(inputText);
+ yield promisePopupShown(gURLBar.popup);
+ yield BrowserTestUtils.waitForCondition(() => {
+ return gURLBar.controller.searchStatus >=
+ Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH;
+ });
+}
+
+function* addBookmark(bookmark) {
+ if (bookmark.keyword) {
+ yield PlacesUtils.keywords.insert({
+ keyword: bookmark.keyword,
+ url: bookmark.url,
+ });
+ }
+
+ yield PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
+ url: bookmark.url,
+ title: bookmark.title,
+ });
+
+ registerCleanupFunction(function* () {
+ yield PlacesUtils.bookmarks.eraseEverything();
+ });
+}
+
+function addSearchEngine(basename) {
+ return new Promise((resolve, reject) => {
+ info("Waiting for engine to be added: " + basename);
+ let url = getRootDirectory(gTestPath) + basename;
+ Services.search.addEngine(url, null, "", false, {
+ onSuccess: (engine) => {
+ info(`Search engine added: ${basename}`);
+ registerCleanupFunction(() => Services.search.removeEngine(engine));
+ resolve(engine);
+ },
+ onError: (errCode) => {
+ ok(false, `addEngine failed with error code ${errCode}`);
+ reject();
+ },
+ });
+ });
+}
+
+function* prepareSearchEngine() {
+ let oldCurrentEngine = Services.search.currentEngine;
+ Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, true);
+ let engine = yield addSearchEngine(TEST_ENGINE_BASENAME);
+ Services.search.currentEngine = engine;
+
+ registerCleanupFunction(function* () {
+ Services.prefs.clearUserPref(SUGGEST_URLBAR_PREF);
+ Services.search.currentEngine = oldCurrentEngine;
+
+ // Make sure the popup is closed for the next test.
+ gURLBar.blur();
+ gURLBar.popup.selectedIndex = -1;
+ gURLBar.popup.hidePopup();
+ ok(!gURLBar.popup.popupOpen, "popup should be closed");
+
+ // Clicking suggestions causes visits to search results pages, so clear that
+ // history now.
+ yield PlacesTestUtils.clearHistory();
+ });
+}
+
+add_task(function* test_webnavigation_urlbar_typed_transitions() {
+ function backgroundScript() {
+ browser.webNavigation.onCommitted.addListener((msg) => {
+ browser.test.assertEq("http://example.com/?q=typed", msg.url,
+ "Got the expected url");
+ // assert from_address_bar transition qualifier
+ browser.test.assertTrue(msg.transitionQualifiers &&
+ msg.transitionQualifiers.includes("from_address_bar"),
+ "Got the expected from_address_bar transitionQualifier");
+ browser.test.assertEq("typed", msg.transitionType,
+ "Got the expected transitionType");
+ browser.test.notifyPass("webNavigation.from_address_bar.typed");
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: backgroundScript,
+ manifest: {
+ permissions: ["webNavigation"],
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("ready");
+
+ gURLBar.focus();
+ gURLBar.textValue = "http://example.com/?q=typed";
+
+ EventUtils.synthesizeKey("VK_RETURN", {altKey: true});
+
+ yield extension.awaitFinish("webNavigation.from_address_bar.typed");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
+
+add_task(function* test_webnavigation_urlbar_bookmark_transitions() {
+ function backgroundScript() {
+ browser.webNavigation.onCommitted.addListener((msg) => {
+ browser.test.assertEq("http://example.com/?q=bookmark", msg.url,
+ "Got the expected url");
+
+ // assert from_address_bar transition qualifier
+ browser.test.assertTrue(msg.transitionQualifiers &&
+ msg.transitionQualifiers.includes("from_address_bar"),
+ "Got the expected from_address_bar transitionQualifier");
+ browser.test.assertEq("auto_bookmark", msg.transitionType,
+ "Got the expected transitionType");
+ browser.test.notifyPass("webNavigation.from_address_bar.auto_bookmark");
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: backgroundScript,
+ manifest: {
+ permissions: ["webNavigation"],
+ },
+ });
+
+ yield addBookmark({
+ title: "Bookmark To Click",
+ url: "http://example.com/?q=bookmark",
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("ready");
+
+ yield promiseAutocompleteResultPopup("Bookmark To Click");
+
+ let item = gURLBar.popup.richlistbox.getItemAtIndex(1);
+ item.click();
+ yield extension.awaitFinish("webNavigation.from_address_bar.auto_bookmark");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
+
+add_task(function* test_webnavigation_urlbar_keyword_transition() {
+ function backgroundScript() {
+ browser.webNavigation.onCommitted.addListener((msg) => {
+ browser.test.assertEq(`http://example.com/?q=search`, msg.url,
+ "Got the expected url");
+
+ // assert from_address_bar transition qualifier
+ browser.test.assertTrue(msg.transitionQualifiers &&
+ msg.transitionQualifiers.includes("from_address_bar"),
+ "Got the expected from_address_bar transitionQualifier");
+ browser.test.assertEq("keyword", msg.transitionType,
+ "Got the expected transitionType");
+ browser.test.notifyPass("webNavigation.from_address_bar.keyword");
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: backgroundScript,
+ manifest: {
+ permissions: ["webNavigation"],
+ },
+ });
+
+ yield addBookmark({
+ title: "Test Keyword",
+ url: "http://example.com/?q=%s",
+ keyword: "testkw",
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("ready");
+
+ yield promiseAutocompleteResultPopup("testkw search");
+
+ let item = gURLBar.popup.richlistbox.getItemAtIndex(0);
+ item.click();
+
+ yield extension.awaitFinish("webNavigation.from_address_bar.keyword");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
+
+add_task(function* test_webnavigation_urlbar_search_transitions() {
+ function backgroundScript() {
+ browser.webNavigation.onCommitted.addListener((msg) => {
+ browser.test.assertEq("http://mochi.test:8888/", msg.url,
+ "Got the expected url");
+
+ // assert from_address_bar transition qualifier
+ browser.test.assertTrue(msg.transitionQualifiers &&
+ msg.transitionQualifiers.includes("from_address_bar"),
+ "Got the expected from_address_bar transitionQualifier");
+ browser.test.assertEq("generated", msg.transitionType,
+ "Got the expected 'generated' transitionType");
+ browser.test.notifyPass("webNavigation.from_address_bar.generated");
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: backgroundScript,
+ manifest: {
+ permissions: ["webNavigation"],
+ },
+ });
+
+ yield extension.startup();
+
+ yield extension.awaitMessage("ready");
+
+ yield prepareSearchEngine();
+ yield promiseAutocompleteResultPopup("foo");
+
+ let item = gURLBar.popup.richlistbox.getItemAtIndex(0);
+ item.click();
+
+ yield extension.awaitFinish("webNavigation.from_address_bar.generated");
+
+ yield extension.unload();
+ info("extension unloaded");
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_webRequest.js b/browser/components/extensions/test/browser/browser_ext_webRequest.js
new file mode 100644
index 000000000..ab9f58480
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_webRequest.js
@@ -0,0 +1,95 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+/* globals makeExtension */
+"use strict";
+
+Services.scriptloader.loadSubScript(new URL("head_webrequest.js", gTestPath).href,
+ this);
+
+Cu.import("resource:///modules/HiddenFrame.jsm", this);
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+function createHiddenBrowser(url) {
+ let frame = new HiddenFrame();
+ return new Promise(resolve =>
+ frame.get().then(subframe => {
+ let doc = subframe.document;
+ let browser = doc.createElementNS(XUL_NS, "browser");
+ browser.setAttribute("type", "content");
+ browser.setAttribute("disableglobalhistory", "true");
+ browser.setAttribute("src", url);
+
+ doc.documentElement.appendChild(browser);
+ resolve({frame: frame, browser: browser});
+ }));
+}
+
+let extension;
+let dummy = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/file_dummy.html";
+
+add_task(function* setup() {
+ // SelfSupport has a tendency to fire when running this test alone, without
+ // a good way to turn it off we just set the url to ""
+ yield SpecialPowers.pushPrefEnv({
+ set: [["browser.selfsupport.url", ""]],
+ });
+ extension = makeExtension();
+ yield extension.startup();
+});
+
+add_task(function* test_newWindow() {
+ let expect = {
+ "file_dummy.html": {
+ type: "main_frame",
+ },
+ };
+ // NOTE: When running solo, favicon will be loaded at some point during
+ // the tests in this file, so all tests ignore it. When running with
+ // other tests in this directory, favicon gets loaded at some point before
+ // we run, and we never see the request, thus it cannot be handled as part
+ // of expect above.
+ extension.sendMessage("set-expected", {expect, ignore: ["favicon.ico"]});
+ yield extension.awaitMessage("continue");
+
+ let openedWindow = yield BrowserTestUtils.openNewBrowserWindow();
+ yield BrowserTestUtils.openNewForegroundTab(openedWindow.gBrowser, dummy + "?newWindow");
+
+ yield extension.awaitMessage("done");
+ yield BrowserTestUtils.closeWindow(openedWindow);
+});
+
+add_task(function* test_newTab() {
+ // again, in this window
+ let expect = {
+ "file_dummy.html": {
+ type: "main_frame",
+ },
+ };
+ extension.sendMessage("set-expected", {expect, ignore: ["favicon.ico"]});
+ yield extension.awaitMessage("continue");
+ let tab = yield BrowserTestUtils.openNewForegroundTab(window.gBrowser, dummy + "?newTab");
+
+ yield extension.awaitMessage("done");
+ yield BrowserTestUtils.removeTab(tab);
+});
+
+add_task(function* test_subframe() {
+ let expect = {
+ "file_dummy.html": {
+ type: "main_frame",
+ },
+ };
+ // test a content subframe attached to hidden window
+ extension.sendMessage("set-expected", {expect, ignore: ["favicon.ico"]});
+ yield extension.awaitMessage("continue");
+ let frameInfo = yield createHiddenBrowser(dummy + "?subframe");
+ yield extension.awaitMessage("done");
+ // cleanup
+ frameInfo.browser.remove();
+ frameInfo.frame.destroy();
+});
+
+add_task(function* teardown() {
+ yield extension.unload();
+});
+
diff --git a/browser/components/extensions/test/browser/browser_ext_windows.js b/browser/components/extensions/test/browser/browser_ext_windows.js
new file mode 100644
index 000000000..d3dd6ecdb
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows.js
@@ -0,0 +1,33 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ let raisedWin = Services.ww.openWindow(
+ null, Services.prefs.getCharPref("browser.chromeURL"), "_blank",
+ "chrome,dialog=no,all,alwaysRaised", null);
+
+ yield TestUtils.topicObserved("browser-delayed-startup-finished",
+ subject => subject == raisedWin);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: function() {
+ browser.windows.getAll((wins) => {
+ browser.test.assertEq(wins.length, 2, "Expect two windows");
+
+ browser.test.assertEq(false, wins[0].alwaysOnTop,
+ "Expect first window not to be always on top");
+ browser.test.assertEq(true, wins[1].alwaysOnTop,
+ "Expect first window to be always on top");
+
+ browser.test.notifyPass("alwaysOnTop");
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("alwaysOnTop");
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(raisedWin);
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_allowScriptsToClose.js b/browser/components/extensions/test/browser/browser_ext_windows_allowScriptsToClose.js
new file mode 100644
index 000000000..13f8b2eee
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_allowScriptsToClose.js
@@ -0,0 +1,61 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+// Tests allowScriptsToClose option
+add_task(function* test_allowScriptsToClose() {
+ const files = {
+ "dummy.html": "<meta charset=utf-8><script src=close.js></script>",
+ "close.js": function() {
+ window.close();
+ if (!window.closed) {
+ browser.test.sendMessage("close-failed");
+ }
+ },
+ };
+
+ function background() {
+ browser.test.onMessage.addListener((msg, options) => {
+ function listener(_, {status}, {url}) {
+ if (status == "complete" && url == options.url) {
+ browser.tabs.onUpdated.removeListener(listener);
+ browser.tabs.executeScript({file: "close.js"});
+ }
+ }
+ options.url = browser.runtime.getURL(options.url);
+ browser.windows.create(options);
+ if (msg === "create+execute") {
+ browser.tabs.onUpdated.addListener(listener);
+ }
+ });
+ browser.test.notifyPass();
+ }
+
+ const example = "http://example.com/";
+ const manifest = {permissions: ["tabs", example]};
+
+ const extension = ExtensionTestUtils.loadExtension({files, background, manifest});
+ yield SpecialPowers.pushPrefEnv({set: [["dom.allow_scripts_to_close_windows", false]]});
+
+ yield extension.startup();
+ yield extension.awaitFinish();
+
+ extension.sendMessage("create", {url: "dummy.html"});
+ let win = yield BrowserTestUtils.waitForNewWindow();
+ yield BrowserTestUtils.windowClosed(win);
+ info("script allowed to close the window");
+
+ extension.sendMessage("create+execute", {url: example});
+ win = yield BrowserTestUtils.waitForNewWindow();
+ yield extension.awaitMessage("close-failed");
+ info("script prevented from closing the window");
+ win.close();
+
+ extension.sendMessage("create+execute", {url: example, allowScriptsToClose: true});
+ win = yield BrowserTestUtils.waitForNewWindow();
+ yield BrowserTestUtils.windowClosed(win);
+ info("script allowed to close the window");
+
+ yield SpecialPowers.popPrefEnv();
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_create.js b/browser/components/extensions/test/browser/browser_ext_windows_create.js
new file mode 100644
index 000000000..f209c9836
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_create.js
@@ -0,0 +1,142 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+requestLongerTimeout(2);
+
+add_task(function* testWindowCreate() {
+ let extension = ExtensionTestUtils.loadExtension({
+ async background() {
+ let _checkWindowPromise;
+ browser.test.onMessage.addListener(msg => {
+ if (msg == "checked-window") {
+ _checkWindowPromise.resolve();
+ _checkWindowPromise = null;
+ }
+ });
+
+ let os;
+
+ function checkWindow(expected) {
+ return new Promise(resolve => {
+ _checkWindowPromise = {resolve};
+ browser.test.sendMessage("check-window", expected);
+ });
+ }
+
+ async function createWindow(params, expected, keep = false) {
+ let window = await browser.windows.create(...params);
+ // params is null when testing create without createData
+ params = params[0] || {};
+
+ for (let key of Object.keys(params)) {
+ if (key == "state" && os == "mac" && params.state == "normal") {
+ // OS-X doesn't have a hard distinction between "normal" and
+ // "maximized" states.
+ browser.test.assertTrue(window.state == "normal" || window.state == "maximized",
+ `Expected window.state (currently ${window.state}) to be "normal" but will accept "maximized"`);
+ } else {
+ browser.test.assertEq(params[key], window[key], `Got expected value for window.${key}`);
+ }
+ }
+
+ browser.test.assertEq(1, window.tabs.length, "tabs property got populated");
+
+ await checkWindow(expected);
+ if (keep) {
+ return window;
+ }
+
+ if (params.state == "fullscreen" && os == "win") {
+ // FIXME: Closing a fullscreen window causes a window leak in
+ // Windows tests.
+ await browser.windows.update(window.id, {state: "normal"});
+ }
+ await browser.windows.remove(window.id);
+ }
+
+ try {
+ ({os} = await browser.runtime.getPlatformInfo());
+
+ // Set the current window to state: "normal" because the test is failing on Windows
+ // where the current window is maximized.
+ let currentWindow = await browser.windows.getCurrent();
+ await browser.windows.update(currentWindow.id, {state: "normal"});
+
+ await createWindow([], {state: "STATE_NORMAL"});
+ await createWindow([{state: "maximized"}], {state: "STATE_MAXIMIZED"});
+ await createWindow([{state: "minimized"}], {state: "STATE_MINIMIZED"});
+ await createWindow([{state: "normal"}], {state: "STATE_NORMAL", hiddenChrome: []});
+ await createWindow([{state: "fullscreen"}], {state: "STATE_FULLSCREEN"});
+
+ let window = await createWindow(
+ [{type: "popup"}],
+ {hiddenChrome: ["menubar", "toolbar", "location", "directories", "status", "extrachrome"],
+ chromeFlags: ["CHROME_OPENAS_DIALOG"]},
+ true);
+
+ let tabs = await browser.tabs.query({windowType: "popup", active: true});
+
+ browser.test.assertEq(1, tabs.length, "Expected only one popup");
+ browser.test.assertEq(window.id, tabs[0].windowId, "Expected new window to be returned in query");
+
+ await browser.windows.remove(window.id);
+
+ browser.test.notifyPass("window-create");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-create");
+ }
+ },
+ });
+
+ let latestWindow;
+ let windowListener = (window, topic) => {
+ if (topic == "domwindowopened") {
+ latestWindow = window;
+ }
+ };
+ Services.ww.registerNotification(windowListener);
+
+ extension.onMessage("check-window", expected => {
+ if (expected.state != null) {
+ let {windowState} = latestWindow;
+ if (latestWindow.fullScreen) {
+ windowState = latestWindow.STATE_FULLSCREEN;
+ }
+
+ if (expected.state == "STATE_NORMAL" && AppConstants.platform == "macosx") {
+ ok(windowState == window.STATE_NORMAL || windowState == window.STATE_MAXIMIZED,
+ `Expected windowState (currently ${windowState}) to be STATE_NORMAL but will accept STATE_MAXIMIZED`);
+ } else {
+ is(windowState, window[expected.state],
+ `Expected window state to be ${expected.state}`);
+ }
+ }
+ if (expected.hiddenChrome) {
+ let chromeHidden = latestWindow.document.documentElement.getAttribute("chromehidden");
+ is(chromeHidden.trim().split(/\s+/).sort().join(" "),
+ expected.hiddenChrome.sort().join(" "),
+ "Got expected hidden chrome");
+ }
+ if (expected.chromeFlags) {
+ let {chromeFlags} = latestWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell)
+ .treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIXULWindow);
+ for (let flag of expected.chromeFlags) {
+ ok(chromeFlags & Ci.nsIWebBrowserChrome[flag],
+ `Expected window to have the ${flag} flag`);
+ }
+ }
+
+ extension.sendMessage("checked-window");
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-create");
+ yield extension.unload();
+
+ Services.ww.unregisterNotification(windowListener);
+ latestWindow = null;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_create_params.js b/browser/components/extensions/test/browser/browser_ext_windows_create_params.js
new file mode 100644
index 000000000..c54d94e05
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_create_params.js
@@ -0,0 +1,33 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+
+// Tests that incompatible parameters can't be used together.
+add_task(function* testWindowCreateParams() {
+ let extension = ExtensionTestUtils.loadExtension({
+ async background() {
+ try {
+ for (let state of ["minimized", "maximized", "fullscreen"]) {
+ for (let param of ["left", "top", "width", "height"]) {
+ let expected = `"state": "${state}" may not be combined with "left", "top", "width", or "height"`;
+
+ await browser.test.assertRejects(
+ browser.windows.create({state, [param]: 100}),
+ RegExp(expected),
+ `Got expected error from create(${param}=100)`);
+ }
+ }
+
+ browser.test.notifyPass("window-create-params");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-create-params");
+ }
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-create-params");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_create_tabId.js b/browser/components/extensions/test/browser/browser_ext_windows_create_tabId.js
new file mode 100644
index 000000000..52ffaea8b
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_create_tabId.js
@@ -0,0 +1,140 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testWindowCreate() {
+ async function background() {
+ let promiseTabAttached = () => {
+ return new Promise(resolve => {
+ browser.tabs.onAttached.addListener(function listener() {
+ browser.tabs.onAttached.removeListener(listener);
+ resolve();
+ });
+ });
+ };
+
+ let promiseTabUpdated = (expected) => {
+ return new Promise(resolve => {
+ browser.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) {
+ if (changeInfo.url === expected) {
+ browser.tabs.onUpdated.removeListener(listener);
+ resolve();
+ }
+ });
+ });
+ };
+
+ try {
+ let window = await browser.windows.getCurrent();
+ let windowId = window.id;
+
+ browser.test.log("Create additional tab in window 1");
+ let tab = await browser.tabs.create({windowId, url: "about:blank"});
+ let tabId = tab.id;
+
+ browser.test.log("Create a new window, adopting the new tab");
+
+ // Note that we want to check against actual boolean values for
+ // all of the `incognito` property tests.
+ browser.test.assertEq(false, tab.incognito, "Tab is not private");
+
+ {
+ let [, window] = await Promise.all([
+ promiseTabAttached(),
+ browser.windows.create({tabId: tabId}),
+ ]);
+ browser.test.assertEq(false, window.incognito, "New window is not private");
+ browser.test.assertEq(tabId, window.tabs[0].id, "tabs property populated correctly");
+
+ browser.test.log("Close the new window");
+ await browser.windows.remove(window.id);
+ }
+
+ {
+ browser.test.log("Create a new private window");
+ let privateWindow = await browser.windows.create({incognito: true});
+ browser.test.assertEq(true, privateWindow.incognito, "Private window is private");
+
+ browser.test.log("Create additional tab in private window");
+ let privateTab = await browser.tabs.create({windowId: privateWindow.id});
+ browser.test.assertEq(true, privateTab.incognito, "Private tab is private");
+
+ browser.test.log("Create a new window, adopting the new private tab");
+ let [, newWindow] = await Promise.all([
+ promiseTabAttached(),
+ browser.windows.create({tabId: privateTab.id}),
+ ]);
+ browser.test.assertEq(true, newWindow.incognito, "New private window is private");
+
+ browser.test.log("Close the new private window");
+ await browser.windows.remove(newWindow.id);
+
+ browser.test.log("Close the private window");
+ await browser.windows.remove(privateWindow.id);
+ }
+
+
+ browser.test.log("Try to create a window with both a tab and a URL");
+ [tab] = await browser.tabs.query({windowId, active: true});
+ await browser.test.assertRejects(
+ browser.windows.create({tabId: tab.id, url: "http://example.com/"}),
+ /`tabId` may not be used in conjunction with `url`/,
+ "Create call failed as expected");
+
+ browser.test.log("Try to create a window with both a tab and an invalid incognito setting");
+ await browser.test.assertRejects(
+ browser.windows.create({tabId: tab.id, incognito: true}),
+ /`incognito` property must match the incognito state of tab/,
+ "Create call failed as expected");
+
+
+ browser.test.log("Try to create a window with an invalid tabId");
+ await browser.test.assertRejects(
+ browser.windows.create({tabId: 0}),
+ /Invalid tab ID: 0/,
+ "Create call failed as expected");
+
+
+ browser.test.log("Try to create a window with two URLs");
+ let readyPromise = Promise.all([
+ // tabs.onUpdated can be invoked between the call of windows.create and
+ // the invocation of its callback/promise, so set up the listeners
+ // before creating the window.
+ promiseTabUpdated("http://example.com/"),
+ promiseTabUpdated("http://example.org/"),
+ ]);
+
+ window = await browser.windows.create({url: ["http://example.com/", "http://example.org/"]});
+ await readyPromise;
+
+ browser.test.assertEq(2, window.tabs.length, "2 tabs were opened in new window");
+ browser.test.assertEq("about:blank", window.tabs[0].url, "about:blank, page not loaded yet");
+ browser.test.assertEq("about:blank", window.tabs[1].url, "about:blank, page not loaded yet");
+
+ window = await browser.windows.get(window.id, {populate: true});
+
+ browser.test.assertEq(2, window.tabs.length, "2 tabs were opened in new window");
+ browser.test.assertEq("http://example.com/", window.tabs[0].url, "Correct URL was loaded in tab 1");
+ browser.test.assertEq("http://example.org/", window.tabs[1].url, "Correct URL was loaded in tab 2");
+
+ await browser.windows.remove(window.id);
+
+ browser.test.notifyPass("window-create");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-create");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "permissions": ["tabs"],
+ },
+
+ background,
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-create");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_create_url.js b/browser/components/extensions/test/browser/browser_ext_windows_create_url.js
new file mode 100644
index 000000000..c5c7aaf20
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_create_url.js
@@ -0,0 +1,84 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testWindowCreate() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["tabs"],
+ },
+
+ background: async function() {
+ const EXTENSION_URL = browser.runtime.getURL("test.html");
+ const REMOTE_URL = browser.runtime.getURL("test.html");
+
+ let windows = new class extends Map { // eslint-disable-line new-parens
+ get(id) {
+ if (!this.has(id)) {
+ let window = {
+ tabs: new Map(),
+ };
+ window.promise = new Promise(resolve => {
+ window.resolvePromise = resolve;
+ });
+
+ this.set(id, window);
+ }
+
+ return super.get(id);
+ }
+ };
+
+ browser.tabs.onUpdated.addListener((tabId, changed, tab) => {
+ if (changed.status == "complete" && tab.url !== "about:blank") {
+ let window = windows.get(tab.windowId);
+ window.tabs.set(tab.index, tab);
+
+ if (window.tabs.size === window.expectedTabs) {
+ window.resolvePromise(window);
+ }
+ }
+ });
+
+ async function create(options) {
+ let window = await browser.windows.create(options);
+ let win = windows.get(window.id);
+
+ win.expectedTabs = Array.isArray(options.url) ? options.url.length : 1;
+
+ return win.promise;
+ }
+
+ try {
+ let windows = await Promise.all([
+ create({url: REMOTE_URL}),
+ create({url: "test.html"}),
+ create({url: EXTENSION_URL}),
+ create({url: [REMOTE_URL, "test.html", EXTENSION_URL]}),
+ ]);
+ browser.test.assertEq(REMOTE_URL, windows[0].tabs.get(0).url, "Single, absolute, remote URL");
+
+ browser.test.assertEq(REMOTE_URL, windows[1].tabs.get(0).url, "Single, relative URL");
+
+ browser.test.assertEq(REMOTE_URL, windows[2].tabs.get(0).url, "Single, absolute, extension URL");
+
+ browser.test.assertEq(REMOTE_URL, windows[3].tabs.get(0).url, "url[0]: Absolute, remote URL");
+ browser.test.assertEq(EXTENSION_URL, windows[3].tabs.get(1).url, "url[1]: Relative URL");
+ browser.test.assertEq(EXTENSION_URL, windows[3].tabs.get(2).url, "url[2]: Absolute, extension URL");
+
+ browser.test.notifyPass("window-create-url");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-create-url");
+ }
+ },
+
+ files: {
+ "test.html": `<DOCTYPE html><html><head><meta charset="utf-8"></head></html>`,
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-create-url");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_events.js b/browser/components/extensions/test/browser/browser_ext_windows_events.js
new file mode 100644
index 000000000..dc3485b98
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_events.js
@@ -0,0 +1,115 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+SimpleTest.requestCompleteLog();
+
+add_task(function* testWindowsEvents() {
+ function background() {
+ browser.windows.onCreated.addListener(window => {
+ browser.test.log(`onCreated: windowId=${window.id}`);
+
+ browser.test.assertTrue(Number.isInteger(window.id),
+ "Window object's id is an integer");
+ browser.test.assertEq("normal", window.type,
+ "Window object returned with the correct type");
+ browser.test.sendMessage("window-created", window.id);
+ });
+
+ let lastWindowId, os;
+ browser.windows.onFocusChanged.addListener(async windowId => {
+ browser.test.log(`onFocusChange: windowId=${windowId} lastWindowId=${lastWindowId}`);
+
+ if (windowId === browser.windows.WINDOW_ID_NONE && os === "linux") {
+ browser.test.log("Ignoring a superfluous WINDOW_ID_NONE (blur) event on Linux");
+ return;
+ }
+
+ browser.test.assertTrue(lastWindowId !== windowId,
+ "onFocusChanged fired once for the given window");
+ lastWindowId = windowId;
+
+ browser.test.assertTrue(Number.isInteger(windowId),
+ "windowId is an integer");
+
+ let window = await browser.windows.getLastFocused();
+
+ browser.test.assertEq(windowId, window.id,
+ "Last focused window has the correct id");
+ browser.test.sendMessage(`window-focus-changed`, window.id);
+ });
+
+ browser.windows.onRemoved.addListener(windowId => {
+ browser.test.log(`onRemoved: windowId=${windowId}`);
+
+ browser.test.assertTrue(Number.isInteger(windowId),
+ "windowId is an integer");
+ browser.test.sendMessage(`window-removed`, windowId);
+ browser.test.notifyPass("windows.events");
+ });
+
+ browser.runtime.getPlatformInfo(info => {
+ os = info.os;
+ browser.test.sendMessage("ready");
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: `(${background})()`,
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ let {Management: {global: {WindowManager}}} = Cu.import("resource://gre/modules/Extension.jsm", {});
+
+ let currentWindow = window;
+ let currentWindowId = WindowManager.getId(currentWindow);
+ info(`Current window ID: ${currentWindowId}`);
+
+ info(`Create browser window 1`);
+ let win1 = yield BrowserTestUtils.openNewBrowserWindow();
+ let win1Id = yield extension.awaitMessage("window-created");
+ info(`Window 1 ID: ${win1Id}`);
+
+ // This shouldn't be necessary, but tests intermittently fail, so let's give
+ // it a try.
+ win1.focus();
+
+ let winId = yield extension.awaitMessage(`window-focus-changed`);
+ is(winId, win1Id, "Got focus change event for the correct window ID.");
+
+ info(`Create browser window 2`);
+ let win2 = yield BrowserTestUtils.openNewBrowserWindow();
+ let win2Id = yield extension.awaitMessage("window-created");
+ info(`Window 2 ID: ${win2Id}`);
+
+ win2.focus();
+
+ winId = yield extension.awaitMessage(`window-focus-changed`);
+ is(winId, win2Id, "Got focus change event for the correct window ID.");
+
+ info(`Focus browser window 1`);
+ yield focusWindow(win1);
+
+ winId = yield extension.awaitMessage(`window-focus-changed`);
+ is(winId, win1Id, "Got focus change event for the correct window ID.");
+
+ info(`Close browser window 2`);
+ yield BrowserTestUtils.closeWindow(win2);
+
+ winId = yield extension.awaitMessage(`window-removed`);
+ is(winId, win2Id, "Got removed event for the correct window ID.");
+
+ info(`Close browser window 1`);
+ yield BrowserTestUtils.closeWindow(win1);
+
+ winId = yield extension.awaitMessage(`window-removed`);
+ is(winId, win1Id, "Got removed event for the correct window ID.");
+
+ winId = yield extension.awaitMessage(`window-focus-changed`);
+ is(winId, currentWindowId, "Got focus change event for the correct window ID.");
+
+ yield extension.awaitFinish("windows.events");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_size.js b/browser/components/extensions/test/browser/browser_ext_windows_size.js
new file mode 100644
index 000000000..be822fea1
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_size.js
@@ -0,0 +1,114 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testWindowCreate() {
+ let extension = ExtensionTestUtils.loadExtension({
+ async background() {
+ let _checkWindowPromise;
+ browser.test.onMessage.addListener((msg, arg) => {
+ if (msg == "checked-window") {
+ _checkWindowPromise.resolve(arg);
+ _checkWindowPromise = null;
+ }
+ });
+
+ let getWindowSize = () => {
+ return new Promise(resolve => {
+ _checkWindowPromise = {resolve};
+ browser.test.sendMessage("check-window");
+ });
+ };
+
+ const KEYS = ["left", "top", "width", "height"];
+ function checkGeom(expected, actual) {
+ for (let key of KEYS) {
+ browser.test.assertEq(expected[key], actual[key], `Expected '${key}' value`);
+ }
+ }
+
+ let windowId;
+ async function checkWindow(expected, retries = 5) {
+ let geom = await getWindowSize();
+
+ if (retries && KEYS.some(key => expected[key] != geom[key])) {
+ browser.test.log(`Got mismatched size (${JSON.stringify(expected)} != ${JSON.stringify(geom)}). ` +
+ `Retrying after a short delay.`);
+
+ await new Promise(resolve => setTimeout(resolve, 200));
+
+ return checkWindow(expected, retries - 1);
+ }
+
+ browser.test.log(`Check actual window size`);
+ checkGeom(expected, geom);
+
+ browser.test.log("Check API-reported window size");
+
+ geom = await browser.windows.get(windowId);
+
+ checkGeom(expected, geom);
+ }
+
+ try {
+ let geom = {left: 100, top: 100, width: 500, height: 300};
+
+ let window = await browser.windows.create(geom);
+ windowId = window.id;
+
+ await checkWindow(geom);
+
+ let update = {left: 150, width: 600};
+ Object.assign(geom, update);
+ await browser.windows.update(windowId, update);
+ await checkWindow(geom);
+
+ update = {top: 150, height: 400};
+ Object.assign(geom, update);
+ await browser.windows.update(windowId, update);
+ await checkWindow(geom);
+
+ geom = {left: 200, top: 200, width: 800, height: 600};
+ await browser.windows.update(windowId, geom);
+ await checkWindow(geom);
+
+ let platformInfo = await browser.runtime.getPlatformInfo();
+ if (platformInfo.os != "linux") {
+ geom = {left: -50, top: -50, width: 800, height: 600};
+ await browser.windows.update(windowId, geom);
+ await checkWindow(geom);
+ }
+
+ await browser.windows.remove(windowId);
+ browser.test.notifyPass("window-size");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-size");
+ }
+ },
+ });
+
+ let latestWindow;
+ let windowListener = (window, topic) => {
+ if (topic == "domwindowopened") {
+ latestWindow = window;
+ }
+ };
+ Services.ww.registerNotification(windowListener);
+
+ extension.onMessage("check-window", () => {
+ extension.sendMessage("checked-window", {
+ top: latestWindow.screenY,
+ left: latestWindow.screenX,
+ width: latestWindow.outerWidth,
+ height: latestWindow.outerHeight,
+ });
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-size");
+ yield extension.unload();
+
+ Services.ww.unregisterNotification(windowListener);
+ latestWindow = null;
+});
diff --git a/browser/components/extensions/test/browser/browser_ext_windows_update.js b/browser/components/extensions/test/browser/browser_ext_windows_update.js
new file mode 100644
index 000000000..b9475547a
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_windows_update.js
@@ -0,0 +1,189 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* () {
+ function promiseWaitForFocus(window) {
+ return new Promise(resolve => {
+ waitForFocus(function() {
+ ok(Services.focus.activeWindow === window, "correct window focused");
+ resolve();
+ }, window);
+ });
+ }
+
+ let window1 = window;
+ let window2 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ Services.focus.activeWindow = window2;
+ yield promiseWaitForFocus(window2);
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: function() {
+ browser.windows.getAll(undefined, function(wins) {
+ browser.test.assertEq(wins.length, 2, "should have two windows");
+
+ // Sort the unfocused window to the lower index.
+ wins.sort(function(win1, win2) {
+ if (win1.focused === win2.focused) {
+ return 0;
+ }
+
+ return win1.focused ? 1 : -1;
+ });
+
+ browser.windows.update(wins[0].id, {focused: true}, function() {
+ browser.test.sendMessage("check");
+ });
+ });
+ },
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("check")]);
+
+ yield promiseWaitForFocus(window1);
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(window2);
+});
+
+
+add_task(function* testWindowUpdate() {
+ let extension = ExtensionTestUtils.loadExtension({
+ async background() {
+ let _checkWindowPromise;
+ browser.test.onMessage.addListener(msg => {
+ if (msg == "checked-window") {
+ _checkWindowPromise.resolve();
+ _checkWindowPromise = null;
+ }
+ });
+
+ let os;
+ function checkWindow(expected) {
+ return new Promise(resolve => {
+ _checkWindowPromise = {resolve};
+ browser.test.sendMessage("check-window", expected);
+ });
+ }
+
+ let currentWindowId;
+ async function updateWindow(windowId, params, expected) {
+ let window = await browser.windows.update(windowId, params);
+
+ browser.test.assertEq(currentWindowId, window.id, "Expected WINDOW_ID_CURRENT to refer to the same window");
+ for (let key of Object.keys(params)) {
+ if (key == "state" && os == "mac" && params.state == "normal") {
+ // OS-X doesn't have a hard distinction between "normal" and
+ // "maximized" states.
+ browser.test.assertTrue(window.state == "normal" || window.state == "maximized",
+ `Expected window.state (currently ${window.state}) to be "normal" but will accept "maximized"`);
+ } else {
+ browser.test.assertEq(params[key], window[key], `Got expected value for window.${key}`);
+ }
+ }
+
+ return checkWindow(expected);
+ }
+
+ try {
+ let windowId = browser.windows.WINDOW_ID_CURRENT;
+
+ ({os} = await browser.runtime.getPlatformInfo());
+
+ let window = await browser.windows.getCurrent();
+ currentWindowId = window.id;
+
+ await updateWindow(windowId, {state: "maximized"}, {state: "STATE_MAXIMIZED"});
+ await updateWindow(windowId, {state: "minimized"}, {state: "STATE_MINIMIZED"});
+ await updateWindow(windowId, {state: "normal"}, {state: "STATE_NORMAL"});
+ await updateWindow(windowId, {state: "fullscreen"}, {state: "STATE_FULLSCREEN"});
+ await updateWindow(windowId, {state: "normal"}, {state: "STATE_NORMAL"});
+
+ browser.test.notifyPass("window-update");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-update");
+ }
+ },
+ });
+
+ extension.onMessage("check-window", expected => {
+ if (expected.state != null) {
+ let {windowState} = window;
+ if (window.fullScreen) {
+ windowState = window.STATE_FULLSCREEN;
+ }
+
+ // Temporarily accepting STATE_MAXIMIZED on Linux because of bug 1307759.
+ if (expected.state == "STATE_NORMAL" && (AppConstants.platform == "macosx" || AppConstants.platform == "linux")) {
+ ok(windowState == window.STATE_NORMAL || windowState == window.STATE_MAXIMIZED,
+ `Expected windowState (currently ${windowState}) to be STATE_NORMAL but will accept STATE_MAXIMIZED`);
+ } else {
+ is(windowState, window[expected.state],
+ `Expected window state to be ${expected.state}`);
+ }
+ }
+
+ extension.sendMessage("checked-window");
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-update");
+ yield extension.unload();
+});
+
+add_task(function* () {
+ let window2 = yield BrowserTestUtils.openNewBrowserWindow();
+
+ let extension = ExtensionTestUtils.loadExtension({
+ background: function() {
+ browser.windows.getAll(undefined, function(wins) {
+ browser.test.assertEq(wins.length, 2, "should have two windows");
+
+ let unfocused = wins.find(win => !win.focused);
+ browser.windows.update(unfocused.id, {drawAttention: true}, function() {
+ browser.test.sendMessage("check");
+ });
+ });
+ },
+ });
+
+ yield Promise.all([extension.startup(), extension.awaitMessage("check")]);
+
+ yield extension.unload();
+
+ yield BrowserTestUtils.closeWindow(window2);
+});
+
+
+// Tests that incompatible parameters can't be used together.
+add_task(function* testWindowUpdateParams() {
+ let extension = ExtensionTestUtils.loadExtension({
+ async background() {
+ try {
+ for (let state of ["minimized", "maximized", "fullscreen"]) {
+ for (let param of ["left", "top", "width", "height"]) {
+ let expected = `"state": "${state}" may not be combined with "left", "top", "width", or "height"`;
+
+ let windowId = browser.windows.WINDOW_ID_CURRENT;
+ await browser.test.assertRejects(
+ browser.windows.update(windowId, {state, [param]: 100}),
+ RegExp(expected),
+ `Got expected error for create(${param}=100`);
+ }
+ }
+
+ browser.test.notifyPass("window-update-params");
+ } catch (e) {
+ browser.test.fail(`${e} :: ${e.stack}`);
+ browser.test.notifyFail("window-update-params");
+ }
+ },
+ });
+
+ yield extension.startup();
+ yield extension.awaitFinish("window-update-params");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/browser/context.html b/browser/components/extensions/test/browser/context.html
new file mode 100644
index 000000000..954feea52
--- /dev/null
+++ b/browser/components/extensions/test/browser/context.html
@@ -0,0 +1,23 @@
+<html>
+ <head>
+ <meta charset="utf-8">
+ </head>
+ <body>
+ just some text 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
+ <img src="ctxmenu-image.png" id="img1">
+
+ <p>
+ <a href="some-link" id="link1">Some link</a>
+ </p>
+
+ <p>
+ <a href="image-around-some-link">
+ <img src="ctxmenu-image.png" id="img-wrapped-in-link">
+ </a>
+ </p>
+
+ <p>
+ <input type="text" id="edit-me">
+ </p>
+ </body>
+</html>
diff --git a/browser/components/extensions/test/browser/context_tabs_onUpdated_iframe.html b/browser/components/extensions/test/browser/context_tabs_onUpdated_iframe.html
new file mode 100644
index 000000000..0e9b54b52
--- /dev/null
+++ b/browser/components/extensions/test/browser/context_tabs_onUpdated_iframe.html
@@ -0,0 +1,19 @@
+<html>
+ <body>
+ <h3>test iframe</h3>
+ <script>
+ "use strict";
+
+ window.onload = function() {
+ window.onhashchange = function() {
+ window.parent.postMessage("updated-iframe-url", "*");
+ };
+ // NOTE: without the this setTimeout the location change is not fired
+ // even without the "fire only for top level windows" fix
+ setTimeout(function() {
+ window.location.hash = "updated-iframe-url";
+ }, 0);
+ };
+ </script>
+ </body>
+</html>
diff --git a/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html b/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html
new file mode 100644
index 000000000..0f2ce1e8f
--- /dev/null
+++ b/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html
@@ -0,0 +1,18 @@
+<html>
+ <body>
+ <h3>test page</h3>
+ <iframe src="about:blank"></iframe>
+ <script>
+ "use strict";
+
+ window.onmessage = function(evt) {
+ if (evt.data === "updated-iframe-url") {
+ window.postMessage("frame-updated", "*");
+ }
+ };
+ window.onload = function() {
+ document.querySelector("iframe").setAttribute("src", "context_tabs_onUpdated_iframe.html");
+ };
+ </script>
+ </body>
+</html>
diff --git a/browser/components/extensions/test/browser/ctxmenu-image.png b/browser/components/extensions/test/browser/ctxmenu-image.png
new file mode 100644
index 000000000..4c3be5084
--- /dev/null
+++ b/browser/components/extensions/test/browser/ctxmenu-image.png
Binary files differ
diff --git a/browser/components/extensions/test/browser/file_bypass_cache.sjs b/browser/components/extensions/test/browser/file_bypass_cache.sjs
new file mode 100644
index 000000000..c91c76b88
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_bypass_cache.sjs
@@ -0,0 +1,11 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80 ft=javascript: */
+"use strict";
+
+function handleRequest(request, response) {
+ response.setHeader("Content-Type", "text/plain; charset=UTF-8", false);
+
+ if (request.hasHeader("pragma") && request.hasHeader("cache-control")) {
+ response.write(`${request.getHeader("pragma")}:${request.getHeader("cache-control")}`);
+ }
+} \ No newline at end of file
diff --git a/browser/components/extensions/test/browser/file_dummy.html b/browser/components/extensions/test/browser/file_dummy.html
new file mode 100644
index 000000000..1a87e2840
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_dummy.html
@@ -0,0 +1,9 @@
+<html>
+<head>
+<title>Dummy test page</title>
+<meta http-equiv="Content-Type" content="text/html;charset=utf-8"></meta>
+</head>
+<body>
+<p>Dummy test page</p>
+</body>
+</html>
diff --git a/browser/components/extensions/test/browser/file_iframe_document.html b/browser/components/extensions/test/browser/file_iframe_document.html
new file mode 100644
index 000000000..fcadccf02
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_iframe_document.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ <iframe src="/"></iframe>
+</body>
+</html>
diff --git a/browser/components/extensions/test/browser/file_iframe_document.sjs b/browser/components/extensions/test/browser/file_iframe_document.sjs
new file mode 100644
index 000000000..661a768af
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_iframe_document.sjs
@@ -0,0 +1,41 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80 ft=javascript: */
+"use strict";
+
+// This script slows the load of an HTML document so that we can reliably test
+// all phases of the load cycle supported by the extension API.
+
+/* eslint-disable no-unused-vars */
+
+const DELAY = 1 * 1000; // Delay one second before completing the request.
+
+const Ci = Components.interfaces;
+
+let nsTimer = Components.Constructor("@mozilla.org/timer;1", "nsITimer", "initWithCallback");
+
+let timer;
+
+function handleRequest(request, response) {
+ response.processAsync();
+
+ response.setHeader("Content-Type", "text/html", false);
+ response.setHeader("Cache-Control", "no-cache", false);
+ response.write(`<!DOCTYPE html>
+ <html lang="en">
+ <head>
+ <meta charset="UTF-8">
+ <title></title>
+ </head>
+ <body>
+ `);
+
+ // Note: We need to store a reference to the timer to prevent it from being
+ // canceled when it's GCed.
+ timer = new nsTimer(() => {
+ response.write(`
+ <iframe src="/"></iframe>
+ </body>
+ </html>`);
+ response.finish();
+ }, DELAY, Ci.nsITimer.TYPE_ONE_SHOT);
+}
diff --git a/browser/components/extensions/test/browser/file_language_fr_en.html b/browser/components/extensions/test/browser/file_language_fr_en.html
new file mode 100644
index 000000000..5e3c7b3b0
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_language_fr_en.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="fr">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ France is the largest country in Western Europe and the third-largest in Europe as a whole.
+ A accès aux chiens et aux frontaux qui lui ont été il peut consulter et modifier ses collections et exporter
+ Cet article concerne le pays européen aujourd’hui appelé République française. Pour d’autres usages du nom France,
+ Pour une aide rapide et effective, veuiller trouver votre aide dans le menu ci-dessus.
+ Motoring events began soon after the construction of the first successful gasoline-fueled automobiles. The quick brown fox jumps over the lazy dog.
+</body>
+</html>
diff --git a/browser/components/extensions/test/browser/file_language_ja.html b/browser/components/extensions/test/browser/file_language_ja.html
new file mode 100644
index 000000000..ed07ba70e
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_language_ja.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="ja">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ このペ ジでは アカウントに指定された予算の履歴を一覧にしています それぞれの項目には 予算額と特定期間のステ タスが表示されます 現在または今後の予算を設定するには
+</body>
+</html>
diff --git a/browser/components/extensions/test/browser/file_language_tlh.html b/browser/components/extensions/test/browser/file_language_tlh.html
new file mode 100644
index 000000000..dd7da7bdb
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_language_tlh.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="tlh">
+<head>
+ <meta charset="UTF-8">
+ <title></title>
+</head>
+<body>
+ tlhIngan maH!
+ Hab SoSlI' Quch!
+ Heghlu'meH QaQ jajvam
+</body>
+</html>
diff --git a/browser/components/extensions/test/browser/file_popup_api_injection_a.html b/browser/components/extensions/test/browser/file_popup_api_injection_a.html
new file mode 100644
index 000000000..750ff1db3
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_popup_api_injection_a.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <script type="application/javascript">
+ "use strict";
+ throw new Error(`WebExt Privilege Escalation: BrowserAction: typeof(browser) = ${typeof(browser)}`);
+ </script>
+</head>
+</html>
diff --git a/browser/components/extensions/test/browser/file_popup_api_injection_b.html b/browser/components/extensions/test/browser/file_popup_api_injection_b.html
new file mode 100644
index 000000000..b8c287e55
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_popup_api_injection_b.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <script type="application/javascript">
+ "use strict";
+ throw new Error(`WebExt Privilege Escalation: PageAction: typeof(browser) = ${typeof(browser)}`);
+ </script>
+</head>
+</html>
diff --git a/browser/components/extensions/test/browser/head.js b/browser/components/extensions/test/browser/head.js
new file mode 100644
index 000000000..f8d59c944
--- /dev/null
+++ b/browser/components/extensions/test/browser/head.js
@@ -0,0 +1,263 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* exported CustomizableUI makeWidgetId focusWindow forceGC
+ * getBrowserActionWidget
+ * clickBrowserAction clickPageAction
+ * getBrowserActionPopup getPageActionPopup
+ * closeBrowserAction closePageAction
+ * promisePopupShown promisePopupHidden
+ * openContextMenu closeContextMenu
+ * openExtensionContextMenu closeExtensionContextMenu
+ * imageBuffer getListStyleImage getPanelForNode
+ * awaitExtensionPanel awaitPopupResize
+ * promiseContentDimensions alterContent
+ */
+
+var {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm");
+var {CustomizableUI} = Cu.import("resource:///modules/CustomizableUI.jsm");
+
+// Bug 1239884: Our tests occasionally hit a long GC pause at unpredictable
+// times in debug builds, which results in intermittent timeouts. Until we have
+// a better solution, we force a GC after certain strategic tests, which tend to
+// accumulate a high number of unreaped windows.
+function forceGC() {
+ if (AppConstants.DEBUG) {
+ Cu.forceGC();
+ }
+}
+
+function makeWidgetId(id) {
+ id = id.toLowerCase();
+ return id.replace(/[^a-z0-9_-]/g, "_");
+}
+
+var focusWindow = Task.async(function* focusWindow(win) {
+ let fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
+ if (fm.activeWindow == win) {
+ return;
+ }
+
+ let promise = new Promise(resolve => {
+ win.addEventListener("focus", function listener() {
+ win.removeEventListener("focus", listener, true);
+ resolve();
+ }, true);
+ });
+
+ win.focus();
+ yield promise;
+});
+
+let img = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==";
+var imageBuffer = Uint8Array.from(atob(img), byte => byte.charCodeAt(0)).buffer;
+
+function getListStyleImage(button) {
+ let style = button.ownerDocument.defaultView.getComputedStyle(button);
+
+ let match = /^url\("(.*)"\)$/.exec(style.listStyleImage);
+
+ return match && match[1];
+}
+
+function promisePopupShown(popup) {
+ return new Promise(resolve => {
+ if (popup.state == "open") {
+ resolve();
+ } else {
+ let onPopupShown = event => {
+ popup.removeEventListener("popupshown", onPopupShown);
+ resolve();
+ };
+ popup.addEventListener("popupshown", onPopupShown);
+ }
+ });
+}
+
+function promisePopupHidden(popup) {
+ return new Promise(resolve => {
+ let onPopupHidden = event => {
+ popup.removeEventListener("popuphidden", onPopupHidden);
+ resolve();
+ };
+ popup.addEventListener("popuphidden", onPopupHidden);
+ });
+}
+
+function promiseContentDimensions(browser) {
+ return ContentTask.spawn(browser, null, function* () {
+ function copyProps(obj, props) {
+ let res = {};
+ for (let prop of props) {
+ res[prop] = obj[prop];
+ }
+ return res;
+ }
+
+ return {
+ window: copyProps(content,
+ ["innerWidth", "innerHeight", "outerWidth", "outerHeight",
+ "scrollX", "scrollY", "scrollMaxX", "scrollMaxY"]),
+ body: copyProps(content.document.body,
+ ["clientWidth", "clientHeight", "scrollWidth", "scrollHeight"]),
+ root: copyProps(content.document.documentElement,
+ ["clientWidth", "clientHeight", "scrollWidth", "scrollHeight"]),
+
+ isStandards: content.document.compatMode !== "BackCompat",
+ };
+ });
+}
+
+function* awaitPopupResize(browser) {
+ return BrowserTestUtils.waitForEvent(browser, "WebExtPopupResized",
+ event => event.detail === "delayed");
+}
+
+function alterContent(browser, task, arg = null) {
+ return Promise.all([
+ ContentTask.spawn(browser, arg, task),
+ awaitPopupResize(browser),
+ ]).then(() => {
+ return promiseContentDimensions(browser);
+ });
+}
+
+function getPanelForNode(node) {
+ while (node.localName != "panel") {
+ node = node.parentNode;
+ }
+ return node;
+}
+
+var awaitBrowserLoaded = browser => ContentTask.spawn(browser, null, () => {
+ if (content.document.readyState !== "complete") {
+ return ContentTaskUtils.waitForEvent(content, "load").then(() => {});
+ }
+});
+
+var awaitExtensionPanel = Task.async(function* (extension, win = window, awaitLoad = true) {
+ let {originalTarget: browser} = yield BrowserTestUtils.waitForEvent(
+ win.document, "WebExtPopupLoaded", true,
+ event => event.detail.extension.id === extension.id);
+
+ yield Promise.all([
+ promisePopupShown(getPanelForNode(browser)),
+
+ awaitLoad && awaitBrowserLoaded(browser),
+ ]);
+
+ return browser;
+});
+
+function getBrowserActionWidget(extension) {
+ return CustomizableUI.getWidget(makeWidgetId(extension.id) + "-browser-action");
+}
+
+function getBrowserActionPopup(extension, win = window) {
+ let group = getBrowserActionWidget(extension);
+
+ if (group.areaType == CustomizableUI.TYPE_TOOLBAR) {
+ return win.document.getElementById("customizationui-widget-panel");
+ }
+ return win.PanelUI.panel;
+}
+
+var showBrowserAction = Task.async(function* (extension, win = window) {
+ let group = getBrowserActionWidget(extension);
+ let widget = group.forWindow(win);
+
+ if (group.areaType == CustomizableUI.TYPE_TOOLBAR) {
+ ok(!widget.overflowed, "Expect widget not to be overflowed");
+ } else if (group.areaType == CustomizableUI.TYPE_MENU_PANEL) {
+ yield win.PanelUI.show();
+ }
+});
+
+var clickBrowserAction = Task.async(function* (extension, win = window) {
+ yield showBrowserAction(extension, win);
+
+ let widget = getBrowserActionWidget(extension).forWindow(win);
+
+ EventUtils.synthesizeMouseAtCenter(widget.node, {}, win);
+});
+
+function closeBrowserAction(extension, win = window) {
+ let group = getBrowserActionWidget(extension);
+
+ let node = win.document.getElementById(group.viewId);
+ CustomizableUI.hidePanelForNode(node);
+
+ return Promise.resolve();
+}
+
+function* openContextMenu(selector = "#img1") {
+ let contentAreaContextMenu = document.getElementById("contentAreaContextMenu");
+ let popupShownPromise = BrowserTestUtils.waitForEvent(contentAreaContextMenu, "popupshown");
+ yield BrowserTestUtils.synthesizeMouseAtCenter(selector, {type: "contextmenu"}, gBrowser.selectedBrowser);
+ yield popupShownPromise;
+ return contentAreaContextMenu;
+}
+
+function* closeContextMenu() {
+ let contentAreaContextMenu = document.getElementById("contentAreaContextMenu");
+ let popupHiddenPromise = BrowserTestUtils.waitForEvent(contentAreaContextMenu, "popuphidden");
+ contentAreaContextMenu.hidePopup();
+ yield popupHiddenPromise;
+}
+
+function* openExtensionContextMenu(selector = "#img1") {
+ let contextMenu = yield openContextMenu(selector);
+ let topLevelMenu = contextMenu.getElementsByAttribute("ext-type", "top-level-menu");
+
+ // Return null if the extension only has one item and therefore no extension menu.
+ if (topLevelMenu.length == 0) {
+ return null;
+ }
+
+ let extensionMenu = topLevelMenu[0].childNodes[0];
+ let popupShownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
+ EventUtils.synthesizeMouseAtCenter(extensionMenu, {});
+ yield popupShownPromise;
+ return extensionMenu;
+}
+
+function* closeExtensionContextMenu(itemToSelect) {
+ let contentAreaContextMenu = document.getElementById("contentAreaContextMenu");
+ let popupHiddenPromise = BrowserTestUtils.waitForEvent(contentAreaContextMenu, "popuphidden");
+ EventUtils.synthesizeMouseAtCenter(itemToSelect, {});
+ yield popupHiddenPromise;
+}
+
+function getPageActionPopup(extension, win = window) {
+ let panelId = makeWidgetId(extension.id) + "-panel";
+ return win.document.getElementById(panelId);
+}
+
+function clickPageAction(extension, win = window) {
+ // This would normally be set automatically on navigation, and cleared
+ // when the user types a value into the URL bar, to show and hide page
+ // identity info and icons such as page action buttons.
+ //
+ // Unfortunately, that doesn't happen automatically in browser chrome
+ // tests.
+ /* globals SetPageProxyState */
+ SetPageProxyState("valid");
+
+ let pageActionId = makeWidgetId(extension.id) + "-page-action";
+ let elem = win.document.getElementById(pageActionId);
+
+ EventUtils.synthesizeMouseAtCenter(elem, {}, win);
+ return new Promise(SimpleTest.executeSoon);
+}
+
+function closePageAction(extension, win = window) {
+ let node = getPageActionPopup(extension, win);
+ if (node) {
+ return promisePopupShown(node).then(() => {
+ node.hidePopup();
+ });
+ }
+
+ return Promise.resolve();
+}
diff --git a/browser/components/extensions/test/browser/head_pageAction.js b/browser/components/extensions/test/browser/head_pageAction.js
new file mode 100644
index 000000000..f2d81e512
--- /dev/null
+++ b/browser/components/extensions/test/browser/head_pageAction.js
@@ -0,0 +1,157 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* exported runTests */
+/* globals getListStyleImage */
+
+function* runTests(options) {
+ function background(getTests) {
+ let tabs;
+ let tests;
+
+ // Gets the current details of the page action, and returns a
+ // promise that resolves to an object containing them.
+ async function getDetails() {
+ let [tab] = await browser.tabs.query({active: true, currentWindow: true});
+ let tabId = tab.id;
+
+ browser.test.log(`Get details: tab={id: ${tabId}, url: ${JSON.stringify(tab.url)}}`);
+
+ return {
+ title: await browser.pageAction.getTitle({tabId}),
+ popup: await browser.pageAction.getPopup({tabId}),
+ };
+ }
+
+
+ // Runs the next test in the `tests` array, checks the results,
+ // and passes control back to the outer test scope.
+ function nextTest() {
+ let test = tests.shift();
+
+ test(async expecting => {
+ function finish() {
+ // Check that the actual icon has the expected values, then
+ // run the next test.
+ browser.test.sendMessage("nextTest", expecting, tests.length);
+ }
+
+ if (expecting) {
+ // Check that the API returns the expected values, and then
+ // run the next test.
+ let details = await getDetails();
+
+ browser.test.assertEq(expecting.title, details.title,
+ "expected value from getTitle");
+
+ browser.test.assertEq(expecting.popup, details.popup,
+ "expected value from getPopup");
+ }
+
+ finish();
+ });
+ }
+
+ async function runTests() {
+ tabs = [];
+ tests = getTests(tabs);
+
+ let resultTabs = await browser.tabs.query({active: true, currentWindow: true});
+
+ tabs[0] = resultTabs[0].id;
+
+ nextTest();
+ }
+
+ browser.test.onMessage.addListener((msg) => {
+ if (msg == "runTests") {
+ runTests();
+ } else if (msg == "runNextTest") {
+ nextTest();
+ } else {
+ browser.test.fail(`Unexpected message: ${msg}`);
+ }
+ });
+
+ runTests();
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: options.manifest,
+
+ files: options.files || {},
+
+ background: `(${background})(${options.getTests})`,
+ });
+
+ let pageActionId;
+ let currentWindow = window;
+ let windows = [];
+
+ function checkDetails(details) {
+ let image = currentWindow.document.getElementById(pageActionId);
+ if (details == null) {
+ ok(image == null || image.hidden, "image is hidden");
+ } else {
+ ok(image, "image exists");
+
+ is(getListStyleImage(image), details.icon, "icon URL is correct");
+
+ let title = details.title || options.manifest.name;
+ is(image.getAttribute("tooltiptext"), title, "image title is correct");
+ is(image.getAttribute("aria-label"), title, "image aria-label is correct");
+ // TODO: Popup URL.
+ }
+ }
+
+ let testNewWindows = 1;
+
+ let awaitFinish = new Promise(resolve => {
+ extension.onMessage("nextTest", (expecting, testsRemaining) => {
+ if (!pageActionId) {
+ pageActionId = `${makeWidgetId(extension.id)}-page-action`;
+ }
+
+ checkDetails(expecting);
+
+ if (testsRemaining) {
+ extension.sendMessage("runNextTest");
+ } else if (testNewWindows) {
+ testNewWindows--;
+
+ BrowserTestUtils.openNewBrowserWindow().then(window => {
+ windows.push(window);
+ currentWindow = window;
+ return focusWindow(window);
+ }).then(() => {
+ extension.sendMessage("runTests");
+ });
+ } else {
+ resolve();
+ }
+ });
+ });
+
+ yield SpecialPowers.pushPrefEnv({set: [["general.useragent.locale", "es-ES"]]});
+
+ yield extension.startup();
+
+ yield awaitFinish;
+
+ yield extension.unload();
+
+ yield SpecialPowers.popPrefEnv();
+
+ let node = document.getElementById(pageActionId);
+ is(node, null, "pageAction image removed from document");
+
+ currentWindow = null;
+ for (let win of windows.splice(0)) {
+ node = win.document.getElementById(pageActionId);
+ is(node, null, "pageAction image removed from second document");
+
+ yield BrowserTestUtils.closeWindow(win);
+ }
+}
+
diff --git a/browser/components/extensions/test/browser/head_sessions.js b/browser/components/extensions/test/browser/head_sessions.js
new file mode 100644
index 000000000..ca3a86c24
--- /dev/null
+++ b/browser/components/extensions/test/browser/head_sessions.js
@@ -0,0 +1,47 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* exported recordInitialTimestamps onlyNewItemsFilter checkRecentlyClosed */
+
+let initialTimestamps = [];
+
+function recordInitialTimestamps(timestamps) {
+ initialTimestamps = timestamps;
+}
+
+function onlyNewItemsFilter(item) {
+ return !initialTimestamps.includes(item.lastModified);
+}
+
+function checkWindow(window) {
+ for (let prop of ["focused", "incognito", "alwaysOnTop"]) {
+ is(window[prop], false, `closed window has the expected value for ${prop}`);
+ }
+ for (let prop of ["state", "type"]) {
+ is(window[prop], "normal", `closed window has the expected value for ${prop}`);
+ }
+}
+
+function checkTab(tab, windowId, incognito) {
+ for (let prop of ["selected", "highlighted", "active", "pinned"]) {
+ is(tab[prop], false, `closed tab has the expected value for ${prop}`);
+ }
+ is(tab.windowId, windowId, "closed tab has the expected value for windowId");
+ is(tab.incognito, incognito, "closed tab has the expected value for incognito");
+}
+
+function checkRecentlyClosed(recentlyClosed, expectedCount, windowId, incognito = false) {
+ let sessionIds = new Set();
+ is(recentlyClosed.length, expectedCount, "the expected number of closed tabs/windows was found");
+ for (let item of recentlyClosed) {
+ if (item.window) {
+ sessionIds.add(item.window.sessionId);
+ checkWindow(item.window);
+ } else if (item.tab) {
+ sessionIds.add(item.tab.sessionId);
+ checkTab(item.tab, windowId, incognito);
+ }
+ }
+ is(sessionIds.size, expectedCount, "each item has a unique sessionId");
+}
diff --git a/browser/components/extensions/test/browser/searchSuggestionEngine.sjs b/browser/components/extensions/test/browser/searchSuggestionEngine.sjs
new file mode 100644
index 000000000..1978b4f66
--- /dev/null
+++ b/browser/components/extensions/test/browser/searchSuggestionEngine.sjs
@@ -0,0 +1,9 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function handleRequest(req, resp) {
+ let suffixes = ["foo", "bar"];
+ let data = [req.queryString, suffixes.map(s => req.queryString + s)];
+ resp.setHeader("Content-Type", "application/json", false);
+ resp.write(JSON.stringify(data));
+}
diff --git a/browser/components/extensions/test/browser/searchSuggestionEngine.xml b/browser/components/extensions/test/browser/searchSuggestionEngine.xml
new file mode 100644
index 000000000..703d45925
--- /dev/null
+++ b/browser/components/extensions/test/browser/searchSuggestionEngine.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+
+<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
+<ShortName>browser_searchSuggestionEngine searchSuggestionEngine.xml</ShortName>
+<Url type="application/x-suggestions+json" method="GET" template="http://mochi.test:8888/browser/browser/components/extensions/test/browser/searchSuggestionEngine.sjs?{searchTerms}"/>
+<Url type="text/html" method="GET" template="http://mochi.test:8888/" rel="searchform"/>
+</SearchPlugin>
diff --git a/browser/components/extensions/test/mochitest/mochitest.ini b/browser/components/extensions/test/mochitest/mochitest.ini
new file mode 100644
index 000000000..39290db61
--- /dev/null
+++ b/browser/components/extensions/test/mochitest/mochitest.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+support-files =
+ ../../../../../toolkit/components/extensions/test/mochitest/test_ext_all_apis.js
+tags = webextensions
+
+[test_ext_all_apis.html]
diff --git a/browser/components/extensions/test/mochitest/test_ext_all_apis.html b/browser/components/extensions/test/mochitest/test_ext_all_apis.html
new file mode 100644
index 000000000..176d380c2
--- /dev/null
+++ b/browser/components/extensions/test/mochitest/test_ext_all_apis.html
@@ -0,0 +1,75 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>WebExtension test</title>
+ <meta charset="utf-8">
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
+</head>
+<body>
+<script>
+"use strict";
+/* exported expectedContentApisTargetSpecific, expectedBackgroundApisTargetSpecific */
+let expectedContentApisTargetSpecific = [
+];
+
+let expectedBackgroundApisTargetSpecific = [
+ "tabs.MutedInfoReason",
+ "tabs.TAB_ID_NONE",
+ "tabs.TabStatus",
+ "tabs.WindowType",
+ "tabs.ZoomSettingsMode",
+ "tabs.ZoomSettingsScope",
+ "tabs.connect",
+ "tabs.create",
+ "tabs.detectLanguage",
+ "tabs.duplicate",
+ "tabs.executeScript",
+ "tabs.get",
+ "tabs.getCurrent",
+ "tabs.getZoom",
+ "tabs.getZoomSettings",
+ "tabs.highlight",
+ "tabs.insertCSS",
+ "tabs.move",
+ "tabs.onActivated",
+ "tabs.onAttached",
+ "tabs.onCreated",
+ "tabs.onDetached",
+ "tabs.onHighlighted",
+ "tabs.onMoved",
+ "tabs.onRemoved",
+ "tabs.onReplaced",
+ "tabs.onUpdated",
+ "tabs.onZoomChange",
+ "tabs.query",
+ "tabs.reload",
+ "tabs.remove",
+ "tabs.removeCSS",
+ "tabs.sendMessage",
+ "tabs.setZoom",
+ "tabs.setZoomSettings",
+ "tabs.update",
+ "windows.CreateType",
+ "windows.WINDOW_ID_CURRENT",
+ "windows.WINDOW_ID_NONE",
+ "windows.WindowState",
+ "windows.WindowType",
+ "windows.create",
+ "windows.get",
+ "windows.getAll",
+ "windows.getCurrent",
+ "windows.getLastFocused",
+ "windows.onCreated",
+ "windows.onFocusChanged",
+ "windows.onRemoved",
+ "windows.remove",
+ "windows.update",
+];
+</script>
+<script src="test_ext_all_apis.js"></script>
+
+</body>
+</html>
diff --git a/browser/components/extensions/test/xpcshell/.eslintrc.js b/browser/components/extensions/test/xpcshell/.eslintrc.js
new file mode 100644
index 000000000..2bfe540ea
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/.eslintrc.js
@@ -0,0 +1,9 @@
+"use strict";
+
+module.exports = { // eslint-disable-line no-undef
+ "extends": "../../../../../testing/xpcshell/xpcshell.eslintrc.js",
+
+ "globals": {
+ "browser": false,
+ },
+};
diff --git a/browser/components/extensions/test/xpcshell/head.js b/browser/components/extensions/test/xpcshell/head.js
new file mode 100644
index 000000000..de4a4a3f6
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/head.js
@@ -0,0 +1,55 @@
+"use strict";
+
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+/* exported createHttpServer */
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
+ "resource://gre/modules/AppConstants.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Extension",
+ "resource://gre/modules/Extension.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "ExtensionData",
+ "resource://gre/modules/Extension.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "ExtensionManagement",
+ "resource://gre/modules/ExtensionManagement.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "ExtensionTestUtils",
+ "resource://testing-common/ExtensionXPCShellUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
+ "resource://gre/modules/FileUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "HttpServer",
+ "resource://testing-common/httpd.js");
+XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
+ "resource://gre/modules/NetUtil.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Schemas",
+ "resource://gre/modules/Schemas.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+ "resource://gre/modules/Services.jsm");
+
+ExtensionTestUtils.init(this);
+
+
+/**
+ * Creates a new HttpServer for testing, and begins listening on the
+ * specified port. Automatically shuts down the server when the test
+ * unit ends.
+ *
+ * @param {integer} [port]
+ * The port to listen on. If omitted, listen on a random
+ * port. The latter is the preferred behavior.
+ *
+ * @returns {HttpServer}
+ */
+function createHttpServer(port = -1) {
+ let server = new HttpServer();
+ server.start(port);
+
+ do_register_cleanup(() => {
+ return new Promise(resolve => {
+ server.stop(resolve);
+ });
+ });
+
+ return server;
+}
diff --git a/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js b/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js
new file mode 100644
index 000000000..142c0a37c
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js
@@ -0,0 +1,601 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function backgroundScript() {
+ let unsortedId, ourId;
+ let initialBookmarkCount = 0;
+ let createdBookmarks = new Set();
+ let createdFolderId;
+ let collectedEvents = [];
+ const nonExistentId = "000000000000";
+ const bookmarkGuids = {
+ menuGuid: "menu________",
+ toolbarGuid: "toolbar_____",
+ unfiledGuid: "unfiled_____",
+ };
+
+ function checkOurBookmark(bookmark) {
+ browser.test.assertEq(ourId, bookmark.id, "Bookmark has the expected Id");
+ browser.test.assertTrue("parentId" in bookmark, "Bookmark has a parentId");
+ browser.test.assertEq(0, bookmark.index, "Bookmark has the expected index"); // We assume there are no other bookmarks.
+ browser.test.assertEq("http://example.org/", bookmark.url, "Bookmark has the expected url");
+ browser.test.assertEq("test bookmark", bookmark.title, "Bookmark has the expected title");
+ browser.test.assertTrue("dateAdded" in bookmark, "Bookmark has a dateAdded");
+ browser.test.assertFalse("dateGroupModified" in bookmark, "Bookmark does not have a dateGroupModified");
+ browser.test.assertFalse("unmodifiable" in bookmark, "Bookmark is not unmodifiable");
+ }
+
+ function checkBookmark(expected, bookmark) {
+ browser.test.assertEq(expected.url, bookmark.url, "Bookmark has the expected url");
+ browser.test.assertEq(expected.title, bookmark.title, "Bookmark has the expected title");
+ browser.test.assertEq(expected.index, bookmark.index, "Bookmark has expected index");
+ if ("parentId" in expected) {
+ browser.test.assertEq(expected.parentId, bookmark.parentId, "Bookmark has the expected parentId");
+ }
+ }
+
+ function expectedError() {
+ browser.test.fail("Did not get expected error");
+ }
+
+ function checkOnCreated(id, parentId, index, title, url, dateAdded) {
+ let createdData = collectedEvents.pop();
+ browser.test.assertEq("onCreated", createdData.event, "onCreated was the last event received");
+ browser.test.assertEq(id, createdData.id, "onCreated event received the expected id");
+ let bookmark = createdData.bookmark;
+ browser.test.assertEq(id, bookmark.id, "onCreated event received the expected bookmark id");
+ browser.test.assertEq(parentId, bookmark.parentId, "onCreated event received the expected bookmark parentId");
+ browser.test.assertEq(index, bookmark.index, "onCreated event received the expected bookmark index");
+ browser.test.assertEq(title, bookmark.title, "onCreated event received the expected bookmark title");
+ browser.test.assertEq(url, bookmark.url, "onCreated event received the expected bookmark url");
+ browser.test.assertEq(dateAdded, bookmark.dateAdded, "onCreated event received the expected bookmark dateAdded");
+ }
+
+ function checkOnChanged(id, url, title) {
+ // If both url and title are changed, then url is fired last.
+ let changedData = collectedEvents.pop();
+ browser.test.assertEq("onChanged", changedData.event, "onChanged was the last event received");
+ browser.test.assertEq(id, changedData.id, "onChanged event received the expected id");
+ browser.test.assertEq(url, changedData.info.url, "onChanged event received the expected url");
+ // title is fired first.
+ changedData = collectedEvents.pop();
+ browser.test.assertEq("onChanged", changedData.event, "onChanged was the last event received");
+ browser.test.assertEq(id, changedData.id, "onChanged event received the expected id");
+ browser.test.assertEq(title, changedData.info.title, "onChanged event received the expected title");
+ }
+
+ function checkOnMoved(id, parentId, oldParentId, index, oldIndex) {
+ let movedData = collectedEvents.pop();
+ browser.test.assertEq("onMoved", movedData.event, "onMoved was the last event received");
+ browser.test.assertEq(id, movedData.id, "onMoved event received the expected id");
+ let info = movedData.info;
+ browser.test.assertEq(parentId, info.parentId, "onMoved event received the expected parentId");
+ browser.test.assertEq(oldParentId, info.oldParentId, "onMoved event received the expected oldParentId");
+ browser.test.assertEq(index, info.index, "onMoved event received the expected index");
+ browser.test.assertEq(oldIndex, info.oldIndex, "onMoved event received the expected oldIndex");
+ }
+
+ function checkOnRemoved(id, parentId, index, url) {
+ let removedData = collectedEvents.pop();
+ browser.test.assertEq("onRemoved", removedData.event, "onRemoved was the last event received");
+ browser.test.assertEq(id, removedData.id, "onRemoved event received the expected id");
+ let info = removedData.info;
+ browser.test.assertEq(parentId, removedData.info.parentId, "onRemoved event received the expected parentId");
+ browser.test.assertEq(index, removedData.info.index, "onRemoved event received the expected index");
+ let node = info.node;
+ browser.test.assertEq(id, node.id, "onRemoved event received the expected node id");
+ browser.test.assertEq(parentId, node.parentId, "onRemoved event received the expected node parentId");
+ browser.test.assertEq(index, node.index, "onRemoved event received the expected node index");
+ browser.test.assertEq(url, node.url, "onRemoved event received the expected node url");
+ }
+
+ browser.bookmarks.onChanged.addListener((id, info) => {
+ collectedEvents.push({event: "onChanged", id, info});
+ });
+
+ browser.bookmarks.onCreated.addListener((id, bookmark) => {
+ collectedEvents.push({event: "onCreated", id, bookmark});
+ });
+
+ browser.bookmarks.onMoved.addListener((id, info) => {
+ collectedEvents.push({event: "onMoved", id, info});
+ });
+
+ browser.bookmarks.onRemoved.addListener((id, info) => {
+ collectedEvents.push({event: "onRemoved", id, info});
+ });
+
+ browser.bookmarks.get(["not-a-bookmark-guid"]).then(expectedError, invalidGuidError => {
+ browser.test.assertTrue(
+ invalidGuidError.message.includes("Invalid value for property 'guid': not-a-bookmark-guid"),
+ "Expected error thrown when trying to get a bookmark using an invalid guid"
+ );
+
+ return browser.bookmarks.get([nonExistentId]).then(expectedError, nonExistentIdError => {
+ browser.test.assertTrue(
+ nonExistentIdError.message.includes("Bookmark not found"),
+ "Expected error thrown when trying to get a bookmark using a non-existent Id"
+ );
+ });
+ }).then(() => {
+ return browser.bookmarks.search({});
+ }).then(results => {
+ initialBookmarkCount = results.length;
+ return browser.bookmarks.create({title: "test bookmark", url: "http://example.org"});
+ }).then(result => {
+ ourId = result.id;
+ checkOurBookmark(result);
+ browser.test.assertEq(1, collectedEvents.length, "1 expected event received");
+ checkOnCreated(ourId, bookmarkGuids.unfiledGuid, 0, "test bookmark", "http://example.org/", result.dateAdded);
+
+ return browser.bookmarks.get(ourId);
+ }).then(results => {
+ browser.test.assertEq(results.length, 1);
+ checkOurBookmark(results[0]);
+
+ unsortedId = results[0].parentId;
+ return browser.bookmarks.get(unsortedId);
+ }).then(results => {
+ let folder = results[0];
+ browser.test.assertEq(1, results.length, "1 bookmark was returned");
+
+ browser.test.assertEq(unsortedId, folder.id, "Folder has the expected id");
+ browser.test.assertTrue("parentId" in folder, "Folder has a parentId");
+ browser.test.assertTrue("index" in folder, "Folder has an index");
+ browser.test.assertFalse("url" in folder, "Folder does not have a url");
+ browser.test.assertEq("Other Bookmarks", folder.title, "Folder has the expected title");
+ browser.test.assertTrue("dateAdded" in folder, "Folder has a dateAdded");
+ browser.test.assertTrue("dateGroupModified" in folder, "Folder has a dateGroupModified");
+ browser.test.assertFalse("unmodifiable" in folder, "Folder is not unmodifiable"); // TODO: Do we want to enable this?
+
+ return browser.bookmarks.getChildren(unsortedId);
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "The folder has one child");
+ checkOurBookmark(results[0]);
+
+ return browser.bookmarks.update(nonExistentId, {title: "new test title"}).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("No bookmarks found for the provided GUID"),
+ "Expected error thrown when trying to update a non-existent bookmark"
+ );
+
+ return browser.bookmarks.update(ourId, {title: "new test title", url: "http://example.com/"});
+ });
+ }).then(result => {
+ browser.test.assertEq("new test title", result.title, "Updated bookmark has the expected title");
+ browser.test.assertEq("http://example.com/", result.url, "Updated bookmark has the expected URL");
+ browser.test.assertEq(ourId, result.id, "Updated bookmark has the expected id");
+
+ browser.test.assertEq(2, collectedEvents.length, "2 expected events received");
+ checkOnChanged(ourId, "http://example.com/", "new test title");
+
+ return Promise.resolve().then(() => {
+ return browser.bookmarks.update(ourId, {url: "this is not a valid url"});
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Invalid bookmark:"),
+ "Expected error thrown when trying update with an invalid url"
+ );
+ return browser.bookmarks.getTree();
+ });
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "getTree returns one result");
+ let bookmark = results[0].children.find(bookmarkItem => bookmarkItem.id == unsortedId);
+ browser.test.assertEq(
+ "Other Bookmarks",
+ bookmark.title,
+ "Folder returned from getTree has the expected title"
+ );
+
+ return browser.bookmarks.create({parentId: "invalid"}).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Invalid bookmark"),
+ "Expected error thrown when trying to create a bookmark with an invalid parentId"
+ );
+ browser.test.assertTrue(
+ error.message.includes(`"parentGuid":"invalid"`),
+ "Expected error thrown when trying to create a bookmark with an invalid parentId"
+ );
+ });
+ }).then(() => {
+ return browser.bookmarks.remove(ourId);
+ }).then(result => {
+ browser.test.assertEq(undefined, result, "Removing a bookmark returns undefined");
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnRemoved(ourId, bookmarkGuids.unfiledGuid, 0, "http://example.com/");
+
+ return browser.bookmarks.get(ourId).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Bookmark not found"),
+ "Expected error thrown when trying to get a removed bookmark"
+ );
+ });
+ }).then(() => {
+ return browser.bookmarks.remove(nonExistentId).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("No bookmarks found for the provided GUID"),
+ "Expected error thrown when trying removed a non-existent bookmark"
+ );
+ });
+ }).then(() => {
+ // test bookmarks.search
+ return Promise.all([
+ browser.bookmarks.create({title: "MØzillä", url: "http://møzîllä.örg/"}),
+ browser.bookmarks.create({title: "Example", url: "http://example.org/"}),
+ browser.bookmarks.create({title: "Mozilla Folder"}),
+ browser.bookmarks.create({title: "EFF", url: "http://eff.org/"}),
+ browser.bookmarks.create({title: "Menu Item", url: "http://menu.org/", parentId: bookmarkGuids.menuGuid}),
+ browser.bookmarks.create({title: "Toolbar Item", url: "http://toolbar.org/", parentId: bookmarkGuids.toolbarGuid}),
+ ]);
+ }).then(results => {
+ browser.test.assertEq(6, collectedEvents.length, "6 expected events received");
+ checkOnCreated(results[5].id, bookmarkGuids.toolbarGuid, 0, "Toolbar Item", "http://toolbar.org/", results[5].dateAdded);
+ checkOnCreated(results[4].id, bookmarkGuids.menuGuid, 0, "Menu Item", "http://menu.org/", results[4].dateAdded);
+ checkOnCreated(results[3].id, bookmarkGuids.unfiledGuid, 0, "EFF", "http://eff.org/", results[3].dateAdded);
+ checkOnCreated(results[2].id, bookmarkGuids.unfiledGuid, 0, "Mozilla Folder", undefined, results[2].dateAdded);
+ checkOnCreated(results[1].id, bookmarkGuids.unfiledGuid, 0, "Example", "http://example.org/", results[1].dateAdded);
+ checkOnCreated(results[0].id, bookmarkGuids.unfiledGuid, 0, "MØzillä", "http://møzîllä.örg/", results[0].dateAdded);
+
+ for (let result of results) {
+ if (result.title !== "Mozilla Folder") {
+ createdBookmarks.add(result.id);
+ }
+ }
+ let folderResult = results[2];
+ createdFolderId = folderResult.id;
+ return Promise.all([
+ browser.bookmarks.create({title: "Mozilla", url: "http://allizom.org/", parentId: createdFolderId}),
+ browser.bookmarks.create({title: "Mozilla Corporation", url: "http://allizom.com/", parentId: createdFolderId}),
+ browser.bookmarks.create({title: "Firefox", url: "http://allizom.org/firefox/", parentId: createdFolderId}),
+ ]).then(newBookmarks => {
+ browser.test.assertEq(3, collectedEvents.length, "3 expected events received");
+ checkOnCreated(newBookmarks[2].id, createdFolderId, 0, "Firefox", "http://allizom.org/firefox/", newBookmarks[2].dateAdded);
+ checkOnCreated(newBookmarks[1].id, createdFolderId, 0, "Mozilla Corporation", "http://allizom.com/", newBookmarks[1].dateAdded);
+ checkOnCreated(newBookmarks[0].id, createdFolderId, 0, "Mozilla", "http://allizom.org/", newBookmarks[0].dateAdded);
+
+ return browser.bookmarks.create({
+ title: "About Mozilla",
+ url: "http://allizom.org/about/",
+ parentId: createdFolderId,
+ index: 1,
+ });
+ }).then(result => {
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnCreated(result.id, createdFolderId, 1, "About Mozilla", "http://allizom.org/about/", result.dateAdded);
+
+ // returns all items on empty object
+ return browser.bookmarks.search({});
+ }).then(bookmarksSearchResults => {
+ browser.test.assertTrue(bookmarksSearchResults.length >= 9, "At least as many bookmarks as added were returned by search({})");
+
+ return Promise.resolve().then(() => {
+ return browser.bookmarks.remove(createdFolderId);
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Cannot remove a non-empty folder"),
+ "Expected error thrown when trying to remove a non-empty folder"
+ );
+ return browser.bookmarks.getSubTree(createdFolderId);
+ });
+ });
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of nodes returned by getSubTree");
+ browser.test.assertEq("Mozilla Folder", results[0].title, "Folder has the expected title");
+ let children = results[0].children;
+ browser.test.assertEq(4, children.length, "Expected number of bookmarks returned by getSubTree");
+ browser.test.assertEq("Firefox", children[0].title, "Bookmark has the expected title");
+ browser.test.assertEq("About Mozilla", children[1].title, "Bookmark has the expected title");
+ browser.test.assertEq(1, children[1].index, "Bookmark has the expected index");
+ browser.test.assertEq("Mozilla Corporation", children[2].title, "Bookmark has the expected title");
+ browser.test.assertEq("Mozilla", children[3].title, "Bookmark has the expected title");
+
+ // throws an error for invalid query objects
+ Promise.resolve().then(() => {
+ return browser.bookmarks.search();
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Incorrect argument types for bookmarks.search"),
+ "Expected error thrown when trying to search with no arguments"
+ );
+ });
+
+ Promise.resolve().then(() => {
+ return browser.bookmarks.search(null);
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Incorrect argument types for bookmarks.search"),
+ "Expected error thrown when trying to search with null as an argument"
+ );
+ });
+
+ Promise.resolve().then(() => {
+ return browser.bookmarks.search(function() {});
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Incorrect argument types for bookmarks.search"),
+ "Expected error thrown when trying to search with a function as an argument"
+ );
+ });
+
+ Promise.resolve().then(() => {
+ return browser.bookmarks.search({banana: "banana"});
+ }).then(expectedError, error => {
+ let substr = `an unexpected "banana" property`;
+ browser.test.assertTrue(
+ error.message.includes(substr),
+ `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`);
+ });
+
+ Promise.resolve().then(() => {
+ return browser.bookmarks.search({url: "spider-man vs. batman"});
+ }).then(expectedError, error => {
+ let substr = 'must match the format "url"';
+ browser.test.assertTrue(
+ error.message.includes(substr),
+ `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`);
+ });
+
+ // queries the full url
+ return browser.bookmarks.search("http://example.org/");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for url search");
+ checkBookmark({title: "Example", url: "http://example.org/", index: 2}, results[0]);
+
+ // queries a partial url
+ return browser.bookmarks.search("example.org");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for url search");
+ checkBookmark({title: "Example", url: "http://example.org/", index: 2}, results[0]);
+
+ // queries the title
+ return browser.bookmarks.search("EFF");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for title search");
+ checkBookmark({title: "EFF", url: "http://eff.org/", index: 0, parentId: bookmarkGuids.unfiledGuid}, results[0]);
+
+ // finds menu items
+ return browser.bookmarks.search("Menu Item");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for menu item search");
+ checkBookmark({title: "Menu Item", url: "http://menu.org/", index: 0, parentId: bookmarkGuids.menuGuid}, results[0]);
+
+ // finds toolbar items
+ return browser.bookmarks.search("Toolbar Item");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for toolbar item search");
+ checkBookmark({title: "Toolbar Item", url: "http://toolbar.org/", index: 0, parentId: bookmarkGuids.toolbarGuid}, results[0]);
+
+ // finds folders
+ return browser.bookmarks.search("Mozilla Folder");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of folders returned");
+ browser.test.assertEq("Mozilla Folder", results[0].title, "Folder has the expected title");
+
+ // is case-insensitive
+ return browser.bookmarks.search("corporation");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returnedfor case-insensitive search");
+ browser.test.assertEq("Mozilla Corporation", results[0].title, "Bookmark has the expected title");
+
+ // is case-insensitive for non-ascii
+ return browser.bookmarks.search("MøZILLÄ");
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for non-ascii search");
+ browser.test.assertEq("MØzillä", results[0].title, "Bookmark has the expected title");
+
+ // returns multiple results
+ return browser.bookmarks.search("allizom");
+ }).then(results => {
+ browser.test.assertEq(4, results.length, "Expected number of multiple results returned");
+ browser.test.assertEq("Mozilla", results[0].title, "Bookmark has the expected title");
+ browser.test.assertEq("Mozilla Corporation", results[1].title, "Bookmark has the expected title");
+ browser.test.assertEq("Firefox", results[2].title, "Bookmark has the expected title");
+ browser.test.assertEq("About Mozilla", results[3].title, "Bookmark has the expected title");
+
+ // accepts a url field
+ return browser.bookmarks.search({url: "http://allizom.com/"});
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for url field");
+ checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
+
+ // normalizes urls
+ return browser.bookmarks.search({url: "http://allizom.com"});
+ }).then(results => {
+ browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field");
+ checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
+
+ // normalizes urls even more
+ return browser.bookmarks.search({url: "http:allizom.com"});
+ }).then(results => {
+ browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field");
+ checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
+
+ // accepts a title field
+ return browser.bookmarks.search({title: "Mozilla"});
+ }).then(results => {
+ browser.test.assertEq(results.length, 1, "Expected number of results returned for title field");
+ checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]);
+
+ // can combine title and query
+ return browser.bookmarks.search({title: "Mozilla", query: "allizom"});
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "Expected number of results returned for title and query fields");
+ checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]);
+
+ // uses AND conditions
+ return browser.bookmarks.search({title: "EFF", query: "allizom"});
+ }).then(results => {
+ browser.test.assertEq(
+ 0,
+ results.length,
+ "Expected number of results returned for non-matching title and query fields"
+ );
+
+ // returns an empty array on item not found
+ return browser.bookmarks.search("microsoft");
+ }).then(results => {
+ browser.test.assertEq(0, results.length, "Expected number of results returned for non-matching search");
+
+ return Promise.resolve().then(() => {
+ return browser.bookmarks.getRecent("");
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Incorrect argument types for bookmarks.getRecent"),
+ "Expected error thrown when calling getRecent with an empty string"
+ );
+ });
+ }).then(() => {
+ return Promise.resolve().then(() => {
+ return browser.bookmarks.getRecent(1.234);
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Incorrect argument types for bookmarks.getRecent"),
+ "Expected error thrown when calling getRecent with a decimal number"
+ );
+ });
+ }).then(() => {
+ return Promise.all([
+ browser.bookmarks.search("corporation"),
+ browser.bookmarks.getChildren(bookmarkGuids.menuGuid),
+ ]);
+ }).then(results => {
+ let corporationBookmark = results[0][0];
+ let childCount = results[1].length;
+
+ browser.test.assertEq(2, corporationBookmark.index, "Bookmark has the expected index");
+
+ return browser.bookmarks.move(corporationBookmark.id, {index: 0}).then(result => {
+ browser.test.assertEq(0, result.index, "Bookmark has the expected index");
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnMoved(corporationBookmark.id, createdFolderId, createdFolderId, 0, 2);
+
+ return browser.bookmarks.move(corporationBookmark.id, {parentId: bookmarkGuids.menuGuid});
+ }).then(result => {
+ browser.test.assertEq(bookmarkGuids.menuGuid, result.parentId, "Bookmark has the expected parent");
+ browser.test.assertEq(childCount, result.index, "Bookmark has the expected index");
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnMoved(corporationBookmark.id, bookmarkGuids.menuGuid, createdFolderId, 1, 0);
+
+ return browser.bookmarks.move(corporationBookmark.id, {index: 0});
+ }).then(result => {
+ browser.test.assertEq(bookmarkGuids.menuGuid, result.parentId, "Bookmark has the expected parent");
+ browser.test.assertEq(0, result.index, "Bookmark has the expected index");
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnMoved(corporationBookmark.id, bookmarkGuids.menuGuid, bookmarkGuids.menuGuid, 0, 1);
+
+ return browser.bookmarks.move(corporationBookmark.id, {parentId: bookmarkGuids.toolbarGuid, index: 1});
+ }).then(result => {
+ browser.test.assertEq(bookmarkGuids.toolbarGuid, result.parentId, "Bookmark has the expected parent");
+ browser.test.assertEq(1, result.index, "Bookmark has the expected index");
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnMoved(corporationBookmark.id, bookmarkGuids.toolbarGuid, bookmarkGuids.menuGuid, 1, 0);
+
+ createdBookmarks.add(corporationBookmark.id);
+ });
+ }).then(() => {
+ return browser.bookmarks.getRecent(4);
+ }).then(results => {
+ browser.test.assertEq(4, results.length, "Expected number of results returned by getRecent");
+ let prevDate = results[0].dateAdded;
+ for (let bookmark of results) {
+ browser.test.assertTrue(bookmark.dateAdded <= prevDate, "The recent bookmarks are sorted by dateAdded");
+ prevDate = bookmark.dateAdded;
+ }
+ let bookmarksByTitle = results.sort((a, b) => {
+ return a.title.localeCompare(b.title);
+ });
+ browser.test.assertEq("About Mozilla", bookmarksByTitle[0].title, "Bookmark has the expected title");
+ browser.test.assertEq("Firefox", bookmarksByTitle[1].title, "Bookmark has the expected title");
+ browser.test.assertEq("Mozilla", bookmarksByTitle[2].title, "Bookmark has the expected title");
+ browser.test.assertEq("Mozilla Corporation", bookmarksByTitle[3].title, "Bookmark has the expected title");
+
+ return browser.bookmarks.search({});
+ }).then(results => {
+ let startBookmarkCount = results.length;
+
+ return browser.bookmarks.search({title: "Mozilla Folder"}).then(result => {
+ return browser.bookmarks.removeTree(result[0].id);
+ }).then(() => {
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnRemoved(createdFolderId, bookmarkGuids.unfiledGuid, 1);
+
+ return browser.bookmarks.search({}).then(searchResults => {
+ browser.test.assertEq(
+ startBookmarkCount - 4,
+ searchResults.length,
+ "Expected number of results returned after removeTree");
+ });
+ });
+ }).then(() => {
+ return browser.bookmarks.create({title: "Empty Folder"});
+ }).then(result => {
+ let emptyFolderId = result.id;
+
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnCreated(emptyFolderId, bookmarkGuids.unfiledGuid, 3, "Empty Folder", undefined, result.dateAdded);
+
+ browser.test.assertEq("Empty Folder", result.title, "Folder has the expected title");
+ return browser.bookmarks.remove(emptyFolderId).then(() => {
+ browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
+ checkOnRemoved(emptyFolderId, bookmarkGuids.unfiledGuid, 3);
+
+ return browser.bookmarks.get(emptyFolderId).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("Bookmark not found"),
+ "Expected error thrown when trying to get a removed folder"
+ );
+ });
+ });
+ }).then(() => {
+ return browser.bookmarks.getChildren(nonExistentId).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("root is null"),
+ "Expected error thrown when trying to getChildren for a non-existent folder"
+ );
+ });
+ }).then(() => {
+ return Promise.resolve().then(() => {
+ return browser.bookmarks.move(nonExistentId, {});
+ }).then(expectedError, error => {
+ browser.test.assertTrue(
+ error.message.includes("No bookmarks found for the provided GUID"),
+ "Expected error thrown when calling move with a non-existent bookmark"
+ );
+ });
+ }).then(() => {
+ // remove all created bookmarks
+ let promises = Array.from(createdBookmarks, guid => browser.bookmarks.remove(guid));
+ return Promise.all(promises);
+ }).then(() => {
+ browser.test.assertEq(createdBookmarks.size, collectedEvents.length, "expected number of events received");
+
+ return browser.bookmarks.search({});
+ }).then(results => {
+ browser.test.assertEq(initialBookmarkCount, results.length, "All created bookmarks have been removed");
+
+ return browser.test.notifyPass("bookmarks");
+ }).catch(error => {
+ browser.test.fail(`Error: ${String(error)} :: ${error.stack}`);
+ browser.test.notifyFail("bookmarks");
+ });
+}
+
+let extensionData = {
+ background: `(${backgroundScript})()`,
+ manifest: {
+ permissions: ["bookmarks"],
+ },
+};
+
+add_task(function* test_contentscript() {
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+ yield extension.startup();
+ yield extension.awaitFinish("bookmarks");
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/xpcshell/test_ext_history.js b/browser/components/extensions/test/xpcshell/test_ext_history.js
new file mode 100644
index 000000000..78df33151
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/test_ext_history.js
@@ -0,0 +1,487 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
+ "resource://testing-common/PlacesTestUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+ "resource://gre/modules/PlacesUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "ExtensionUtils",
+ "resource://gre/modules/ExtensionUtils.jsm");
+
+add_task(function* test_delete() {
+ function background() {
+ let historyClearedCount = 0;
+ let removedUrls = [];
+
+ browser.history.onVisitRemoved.addListener(data => {
+ if (data.allHistory) {
+ historyClearedCount++;
+ browser.test.assertEq(0, data.urls.length, "onVisitRemoved received an empty urls array");
+ } else {
+ browser.test.assertEq(1, data.urls.length, "onVisitRemoved received one URL");
+ removedUrls.push(data.urls[0]);
+ }
+ });
+
+ browser.test.onMessage.addListener((msg, arg) => {
+ if (msg === "delete-url") {
+ browser.history.deleteUrl({url: arg}).then(result => {
+ browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing");
+ browser.test.sendMessage("url-deleted");
+ });
+ } else if (msg === "delete-range") {
+ browser.history.deleteRange(arg).then(result => {
+ browser.test.assertEq(undefined, result, "browser.history.deleteRange returns nothing");
+ browser.test.sendMessage("range-deleted", removedUrls);
+ });
+ } else if (msg === "delete-all") {
+ browser.history.deleteAll().then(result => {
+ browser.test.assertEq(undefined, result, "browser.history.deleteAll returns nothing");
+ browser.test.sendMessage("history-cleared", [historyClearedCount, removedUrls]);
+ });
+ }
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ const BASE_URL = "http://mozilla.com/test_history/";
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["history"],
+ },
+ background: `(${background})()`,
+ });
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+ yield PlacesTestUtils.clearHistory();
+
+ let historyClearedCount;
+ let visits = [];
+ let visitDate = new Date(1999, 9, 9, 9, 9).getTime();
+
+ function pushVisit(subvisits) {
+ visitDate += 1000;
+ subvisits.push({date: new Date(visitDate)});
+ }
+
+ // Add 5 visits for one uri and 3 visits for 3 others
+ for (let i = 0; i < 4; ++i) {
+ let visit = {
+ url: `${BASE_URL}${i}`,
+ title: "visit " + i,
+ visits: [],
+ };
+ if (i === 0) {
+ for (let j = 0; j < 5; ++j) {
+ pushVisit(visit.visits);
+ }
+ } else {
+ pushVisit(visit.visits);
+ }
+ visits.push(visit);
+ }
+
+ yield PlacesUtils.history.insertMany(visits);
+ equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 5, "5 visits for uri found in history database");
+
+ let testUrl = visits[2].url;
+ ok((yield PlacesTestUtils.isPageInDB(testUrl)), "expected url found in history database");
+
+ extension.sendMessage("delete-url", testUrl);
+ yield extension.awaitMessage("url-deleted");
+ equal((yield PlacesTestUtils.isPageInDB(testUrl)), false, "expected url not found in history database");
+
+ // delete 3 of the 5 visits for url 1
+ let filter = {
+ startTime: visits[0].visits[0].date,
+ endTime: visits[0].visits[2].date,
+ };
+
+ extension.sendMessage("delete-range", filter);
+ let removedUrls = yield extension.awaitMessage("range-deleted");
+ ok(!removedUrls.includes(visits[0].url), `${visits[0].url} not received by onVisitRemoved`);
+ ok((yield PlacesTestUtils.isPageInDB(visits[0].url)), "expected uri found in history database");
+ equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 2, "2 visits for uri found in history database");
+ ok((yield PlacesTestUtils.isPageInDB(visits[1].url)), "expected uri found in history database");
+ equal((yield PlacesTestUtils.visitsInDB(visits[1].url)), 1, "1 visit for uri found in history database");
+
+ // delete the rest of the visits for url 1, and the visit for url 2
+ filter.startTime = visits[0].visits[0].date;
+ filter.endTime = visits[1].visits[0].date;
+
+ extension.sendMessage("delete-range", filter);
+ yield extension.awaitMessage("range-deleted");
+
+ equal((yield PlacesTestUtils.isPageInDB(visits[0].url)), false, "expected uri not found in history database");
+ equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 0, "0 visits for uri found in history database");
+ equal((yield PlacesTestUtils.isPageInDB(visits[1].url)), false, "expected uri not found in history database");
+ equal((yield PlacesTestUtils.visitsInDB(visits[1].url)), 0, "0 visits for uri found in history database");
+
+ ok((yield PlacesTestUtils.isPageInDB(visits[3].url)), "expected uri found in history database");
+
+ extension.sendMessage("delete-all");
+ [historyClearedCount, removedUrls] = yield extension.awaitMessage("history-cleared");
+ equal(PlacesUtils.history.hasHistoryEntries, false, "history is empty");
+ equal(historyClearedCount, 2, "onVisitRemoved called for each clearing of history");
+ equal(removedUrls.length, 3, "onVisitRemoved called the expected number of times");
+ for (let i = 1; i < 3; ++i) {
+ let url = visits[i].url;
+ ok(removedUrls.includes(url), `${url} received by onVisitRemoved`);
+ }
+ yield extension.unload();
+});
+
+add_task(function* test_search() {
+ const SINGLE_VISIT_URL = "http://example.com/";
+ const DOUBLE_VISIT_URL = "http://example.com/2/";
+ const MOZILLA_VISIT_URL = "http://mozilla.com/";
+ const REFERENCE_DATE = new Date();
+ // pages/visits to add via History.insert
+ const PAGE_INFOS = [
+ {
+ url: SINGLE_VISIT_URL,
+ title: `test visit for ${SINGLE_VISIT_URL}`,
+ visits: [
+ {date: new Date(Number(REFERENCE_DATE) - 1000)},
+ ],
+ },
+ {
+ url: DOUBLE_VISIT_URL,
+ title: `test visit for ${DOUBLE_VISIT_URL}`,
+ visits: [
+ {date: REFERENCE_DATE},
+ {date: new Date(Number(REFERENCE_DATE) - 2000)},
+ ],
+ },
+ {
+ url: MOZILLA_VISIT_URL,
+ title: `test visit for ${MOZILLA_VISIT_URL}`,
+ visits: [
+ {date: new Date(Number(REFERENCE_DATE) - 3000)},
+ ],
+ },
+ ];
+
+ function background(BGSCRIPT_REFERENCE_DATE) {
+ const futureTime = Date.now() + 24 * 60 * 60 * 1000;
+
+ browser.test.onMessage.addListener(msg => {
+ browser.history.search({text: ""}).then(results => {
+ browser.test.sendMessage("empty-search", results);
+ return browser.history.search({text: "mozilla.com"});
+ }).then(results => {
+ browser.test.sendMessage("text-search", results);
+ return browser.history.search({text: "example.com", maxResults: 1});
+ }).then(results => {
+ browser.test.sendMessage("max-results-search", results);
+ return browser.history.search({text: "", startTime: BGSCRIPT_REFERENCE_DATE - 2000, endTime: BGSCRIPT_REFERENCE_DATE - 1000});
+ }).then(results => {
+ browser.test.sendMessage("date-range-search", results);
+ return browser.history.search({text: "", startTime: futureTime});
+ }).then(results => {
+ browser.test.assertEq(0, results.length, "no results returned for late start time");
+ return browser.history.search({text: "", endTime: 0});
+ }).then(results => {
+ browser.test.assertEq(0, results.length, "no results returned for early end time");
+ return browser.history.search({text: "", startTime: Date.now(), endTime: 0});
+ }).then(results => {
+ browser.test.fail("history.search rejects with startTime that is after the endTime");
+ }, error => {
+ browser.test.assertEq(
+ "The startTime cannot be after the endTime",
+ error.message,
+ "history.search rejects with startTime that is after the endTime");
+ }).then(() => {
+ browser.test.notifyPass("search");
+ });
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["history"],
+ },
+ background: `(${background})(${Number(REFERENCE_DATE)})`,
+ });
+
+ function findResult(url, results) {
+ return results.find(r => r.url === url);
+ }
+
+ function checkResult(results, url, expectedCount) {
+ let result = findResult(url, results);
+ notEqual(result, null, `history.search result was found for ${url}`);
+ equal(result.visitCount, expectedCount, `history.search reports ${expectedCount} visit(s)`);
+ equal(result.title, `test visit for ${url}`, "title for search result is correct");
+ }
+
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+ yield PlacesTestUtils.clearHistory();
+
+ yield PlacesUtils.history.insertMany(PAGE_INFOS);
+
+ extension.sendMessage("check-history");
+
+ let results = yield extension.awaitMessage("empty-search");
+ equal(results.length, 3, "history.search with empty text returned 3 results");
+ checkResult(results, SINGLE_VISIT_URL, 1);
+ checkResult(results, DOUBLE_VISIT_URL, 2);
+ checkResult(results, MOZILLA_VISIT_URL, 1);
+
+ results = yield extension.awaitMessage("text-search");
+ equal(results.length, 1, "history.search with specific text returned 1 result");
+ checkResult(results, MOZILLA_VISIT_URL, 1);
+
+ results = yield extension.awaitMessage("max-results-search");
+ equal(results.length, 1, "history.search with maxResults returned 1 result");
+ checkResult(results, DOUBLE_VISIT_URL, 2);
+
+ results = yield extension.awaitMessage("date-range-search");
+ equal(results.length, 2, "history.search with a date range returned 2 result");
+ checkResult(results, DOUBLE_VISIT_URL, 2);
+ checkResult(results, SINGLE_VISIT_URL, 1);
+
+ yield extension.awaitFinish("search");
+ yield extension.unload();
+ yield PlacesTestUtils.clearHistory();
+});
+
+add_task(function* test_add_url() {
+ function background() {
+ const TEST_DOMAIN = "http://example.com/";
+
+ browser.test.onMessage.addListener((msg, testData) => {
+ let [details, type] = testData;
+ details.url = details.url || `${TEST_DOMAIN}${type}`;
+ if (msg === "add-url") {
+ details.title = `Title for ${type}`;
+ browser.history.addUrl(details).then(() => {
+ return browser.history.search({text: details.url});
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "1 result found when searching for added URL");
+ browser.test.sendMessage("url-added", {details, result: results[0]});
+ });
+ } else if (msg === "expect-failure") {
+ let expectedMsg = testData[2];
+ browser.history.addUrl(details).then(() => {
+ browser.test.fail(`Expected error thrown for ${type}`);
+ }, error => {
+ browser.test.assertTrue(
+ error.message.includes(expectedMsg),
+ `"Expected error thrown when trying to add a URL with ${type}`
+ );
+ browser.test.sendMessage("add-failed");
+ });
+ }
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let addTestData = [
+ [{}, "default"],
+ [{visitTime: new Date()}, "with_date"],
+ [{visitTime: Date.now()}, "with_ms_number"],
+ [{visitTime: new Date().toISOString()}, "with_iso_string"],
+ [{transition: "typed"}, "valid_transition"],
+ ];
+
+ let failTestData = [
+ [{transition: "generated"}, "an invalid transition", "|generated| is not a supported transition for history"],
+ [{visitTime: Date.now() + 1000000}, "a future date", "cannot be a future date"],
+ [{url: "about.config"}, "an invalid url", "about.config is not a valid URL"],
+ ];
+
+ function* checkUrl(results) {
+ ok((yield PlacesTestUtils.isPageInDB(results.details.url)), `${results.details.url} found in history database`);
+ ok(PlacesUtils.isValidGuid(results.result.id), "URL was added with a valid id");
+ equal(results.result.title, results.details.title, "URL was added with the correct title");
+ if (results.details.visitTime) {
+ equal(results.result.lastVisitTime,
+ Number(ExtensionUtils.normalizeTime(results.details.visitTime)),
+ "URL was added with the correct date");
+ }
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["history"],
+ },
+ background: `(${background})()`,
+ });
+
+ yield PlacesTestUtils.clearHistory();
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ for (let data of addTestData) {
+ extension.sendMessage("add-url", data);
+ let results = yield extension.awaitMessage("url-added");
+ yield checkUrl(results);
+ }
+
+ for (let data of failTestData) {
+ extension.sendMessage("expect-failure", data);
+ yield extension.awaitMessage("add-failed");
+ }
+
+ yield extension.unload();
+});
+
+add_task(function* test_get_visits() {
+ function background() {
+ const TEST_DOMAIN = "http://example.com/";
+ const FIRST_DATE = Date.now();
+ const INITIAL_DETAILS = {
+ url: TEST_DOMAIN,
+ visitTime: FIRST_DATE,
+ transition: "link",
+ };
+
+ let visitIds = new Set();
+
+ function checkVisit(visit, expected) {
+ visitIds.add(visit.visitId);
+ browser.test.assertEq(expected.visitTime, visit.visitTime, "visit has the correct visitTime");
+ browser.test.assertEq(expected.transition, visit.transition, "visit has the correct transition");
+ browser.history.search({text: expected.url}).then(results => {
+ // all results will have the same id, so we only need to use the first one
+ browser.test.assertEq(results[0].id, visit.id, "visit has the correct id");
+ });
+ }
+
+ let details = Object.assign({}, INITIAL_DETAILS);
+
+ browser.history.addUrl(details).then(() => {
+ return browser.history.getVisits({url: details.url});
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "the expected number of visits were returned");
+ checkVisit(results[0], details);
+ details.url = `${TEST_DOMAIN}/1/`;
+ return browser.history.addUrl(details);
+ }).then(() => {
+ return browser.history.getVisits({url: details.url});
+ }).then(results => {
+ browser.test.assertEq(1, results.length, "the expected number of visits were returned");
+ checkVisit(results[0], details);
+ details.visitTime = FIRST_DATE - 1000;
+ details.transition = "typed";
+ return browser.history.addUrl(details);
+ }).then(() => {
+ return browser.history.getVisits({url: details.url});
+ }).then(results => {
+ browser.test.assertEq(2, results.length, "the expected number of visits were returned");
+ checkVisit(results[0], INITIAL_DETAILS);
+ checkVisit(results[1], details);
+ }).then(() => {
+ browser.test.assertEq(3, visitIds.size, "each visit has a unique visitId");
+ browser.test.notifyPass("get-visits");
+ });
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["history"],
+ },
+ background: `(${background})()`,
+ });
+
+ yield PlacesTestUtils.clearHistory();
+ yield extension.startup();
+
+ yield extension.awaitFinish("get-visits");
+ yield extension.unload();
+});
+
+add_task(function* test_on_visited() {
+ const SINGLE_VISIT_URL = "http://example.com/1/";
+ const DOUBLE_VISIT_URL = "http://example.com/2/";
+ let visitDate = new Date(1999, 9, 9, 9, 9).getTime();
+
+ // pages/visits to add via History.insertMany
+ const PAGE_INFOS = [
+ {
+ url: SINGLE_VISIT_URL,
+ title: `visit to ${SINGLE_VISIT_URL}`,
+ visits: [
+ {date: new Date(visitDate)},
+ ],
+ },
+ {
+ url: DOUBLE_VISIT_URL,
+ title: `visit to ${DOUBLE_VISIT_URL}`,
+ visits: [
+ {date: new Date(visitDate += 1000)},
+ {date: new Date(visitDate += 1000)},
+ ],
+ },
+ ];
+
+ function background() {
+ let onVisitedData = [];
+
+ browser.history.onVisited.addListener(data => {
+ if (data.url.includes("moz-extension")) {
+ return;
+ }
+ onVisitedData.push(data);
+ if (onVisitedData.length == 3) {
+ browser.test.sendMessage("on-visited-data", onVisitedData);
+ }
+ });
+
+ browser.test.sendMessage("ready");
+ }
+
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ permissions: ["history"],
+ },
+ background: `(${background})()`,
+ });
+
+ yield PlacesTestUtils.clearHistory();
+ yield extension.startup();
+ yield extension.awaitMessage("ready");
+
+ yield PlacesUtils.history.insertMany(PAGE_INFOS);
+
+ let onVisitedData = yield extension.awaitMessage("on-visited-data");
+
+ function checkOnVisitedData(index, expected) {
+ let onVisited = onVisitedData[index];
+ ok(PlacesUtils.isValidGuid(onVisited.id), "onVisited received a valid id");
+ equal(onVisited.url, expected.url, "onVisited received the expected url");
+ // Title will be blank until bug 1287928 lands
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1287928
+ equal(onVisited.title, "", "onVisited received a blank title");
+ equal(onVisited.lastVisitTime, expected.time, "onVisited received the expected time");
+ equal(onVisited.visitCount, expected.visitCount, "onVisited received the expected visitCount");
+ }
+
+ let expected = {
+ url: PAGE_INFOS[0].url,
+ title: PAGE_INFOS[0].title,
+ time: PAGE_INFOS[0].visits[0].date.getTime(),
+ visitCount: 1,
+ };
+ checkOnVisitedData(0, expected);
+
+ expected.url = PAGE_INFOS[1].url;
+ expected.title = PAGE_INFOS[1].title;
+ expected.time = PAGE_INFOS[1].visits[0].date.getTime();
+ checkOnVisitedData(1, expected);
+
+ expected.time = PAGE_INFOS[1].visits[1].date.getTime();
+ expected.visitCount = 2;
+ checkOnVisitedData(2, expected);
+
+ yield extension.unload();
+});
diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js
new file mode 100644
index 000000000..4de7afe01
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js
@@ -0,0 +1,24 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+
+add_task(function* test_manifest_commands() {
+ let normalized = yield ExtensionTestUtils.normalizeManifest({
+ "commands": {
+ "toggle-feature": {
+ "suggested_key": {"default": "Shifty+Y"},
+ "description": "Send a 'toggle-feature' event to the extension",
+ },
+ },
+ });
+
+ let expectedError = (
+ String.raw`commands.toggle-feature.suggested_key.default: Value must either: ` +
+ String.raw`match the pattern /^\s*(Alt|Ctrl|Command|MacCtrl)\s*\+\s*(Shift\s*\+\s*)?([A-Z0-9]|Comma|Period|Home|End|PageUp|PageDown|Space|Insert|Delete|Up|Down|Left|Right)\s*$/, or ` +
+ String.raw`match the pattern /^(MediaNextTrack|MediaPlayPause|MediaPrevTrack|MediaStop)$/`
+ );
+
+ ok(normalized.error.includes(expectedError),
+ `The manifest error ${JSON.stringify(normalized.error)} must contain ${JSON.stringify(expectedError)}`);
+});
diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js
new file mode 100644
index 000000000..2cb141235
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js
@@ -0,0 +1,61 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+function* testKeyword(params) {
+ let normalized = yield ExtensionTestUtils.normalizeManifest({
+ "omnibox": {
+ "keyword": params.keyword,
+ },
+ });
+
+ if (params.expectError) {
+ let expectedError = (
+ String.raw`omnibox.keyword: String "${params.keyword}" ` +
+ String.raw`must match /^[^?\s:]([^\s:]*[^/\s:])?$/`
+ );
+ ok(normalized.error.includes(expectedError),
+ `The manifest error ${JSON.stringify(normalized.error)} ` +
+ `must contain ${JSON.stringify(expectedError)}`);
+ } else {
+ equal(normalized.error, undefined, "Should not have an error");
+ equal(normalized.errors.length, 0, "Should not have warnings");
+ }
+}
+
+add_task(function* test_manifest_commands() {
+ // accepted single character keywords
+ yield testKeyword({keyword: "a", expectError: false});
+ yield testKeyword({keyword: "-", expectError: false});
+ yield testKeyword({keyword: "嗨", expectError: false});
+ yield testKeyword({keyword: "*", expectError: false});
+ yield testKeyword({keyword: "/", expectError: false});
+
+ // rejected single character keywords
+ yield testKeyword({keyword: "?", expectError: true});
+ yield testKeyword({keyword: " ", expectError: true});
+ yield testKeyword({keyword: ":", expectError: true});
+
+ // accepted multi-character keywords
+ yield testKeyword({keyword: "aa", expectError: false});
+ yield testKeyword({keyword: "http", expectError: false});
+ yield testKeyword({keyword: "f?a", expectError: false});
+ yield testKeyword({keyword: "fa?", expectError: false});
+ yield testKeyword({keyword: "f/x", expectError: false});
+ yield testKeyword({keyword: "/fx", expectError: false});
+
+ // rejected multi-character keywords
+ yield testKeyword({keyword: " a", expectError: true});
+ yield testKeyword({keyword: "a ", expectError: true});
+ yield testKeyword({keyword: " ", expectError: true});
+ yield testKeyword({keyword: " a ", expectError: true});
+ yield testKeyword({keyword: "?fx", expectError: true});
+ yield testKeyword({keyword: "fx/", expectError: true});
+ yield testKeyword({keyword: "f:x", expectError: true});
+ yield testKeyword({keyword: "fx:", expectError: true});
+ yield testKeyword({keyword: "f x", expectError: true});
+
+ // miscellaneous tests
+ yield testKeyword({keyword: "こんにちは", expectError: false});
+ yield testKeyword({keyword: "http://", expectError: true});
+});
diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js
new file mode 100644
index 000000000..2c436535d
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js
@@ -0,0 +1,57 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* globals chrome */
+
+function* testPermission(options) {
+ function background(bgOptions) {
+ browser.test.sendMessage("typeof-namespace", {
+ browser: typeof browser[bgOptions.namespace],
+ chrome: typeof chrome[bgOptions.namespace],
+ });
+ }
+
+ let extensionDetails = {
+ background: `(${background})(${JSON.stringify(options)})`,
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionDetails);
+
+ yield extension.startup();
+
+ let types = yield extension.awaitMessage("typeof-namespace");
+ equal(types.browser, "undefined", `Type of browser.${options.namespace} without manifest entry`);
+ equal(types.chrome, "undefined", `Type of chrome.${options.namespace} without manifest entry`);
+
+ yield extension.unload();
+
+ extensionDetails.manifest = options.manifest;
+ extension = ExtensionTestUtils.loadExtension(extensionDetails);
+
+ yield extension.startup();
+
+ types = yield extension.awaitMessage("typeof-namespace");
+ equal(types.browser, "object", `Type of browser.${options.namespace} with manifest entry`);
+ equal(types.chrome, "object", `Type of chrome.${options.namespace} with manifest entry`);
+
+ yield extension.unload();
+}
+
+add_task(function* test_browserAction() {
+ yield testPermission({
+ namespace: "browserAction",
+ manifest: {
+ browser_action: {},
+ },
+ });
+});
+
+add_task(function* test_pageAction() {
+ yield testPermission({
+ namespace: "pageAction",
+ manifest: {
+ page_action: {},
+ },
+ });
+});
diff --git a/browser/components/extensions/test/xpcshell/xpcshell.ini b/browser/components/extensions/test/xpcshell/xpcshell.ini
new file mode 100644
index 000000000..b9148a697
--- /dev/null
+++ b/browser/components/extensions/test/xpcshell/xpcshell.ini
@@ -0,0 +1,11 @@
+[DEFAULT]
+head = head.js
+tail =
+firefox-appdir = browser
+tags = webextensions
+
+[test_ext_bookmarks.js]
+[test_ext_history.js]
+[test_ext_manifest_commands.js]
+[test_ext_manifest_omnibox.js]
+[test_ext_manifest_permissions.js]