diff options
author | wolfbeast <mcwerewolf@wolfbeast.com> | 2019-06-04 18:21:04 +0200 |
---|---|---|
committer | wolfbeast <mcwerewolf@wolfbeast.com> | 2019-06-04 18:21:04 +0200 |
commit | dee00a8a79394559e0e868cc72464c2de24583ac (patch) | |
tree | 18dc2e3db8127ceabcf9b03416b135bced2976ad /application/basilisk/components | |
parent | 851cfd198bc01020cd411d4f1cd6586222700269 (diff) | |
parent | 363bfeb2c06e5f57136ebdab8da1ebeba0591520 (diff) | |
download | UXP-dee00a8a79394559e0e868cc72464c2de24583ac.tar UXP-dee00a8a79394559e0e868cc72464c2de24583ac.tar.gz UXP-dee00a8a79394559e0e868cc72464c2de24583ac.tar.lz UXP-dee00a8a79394559e0e868cc72464c2de24583ac.tar.xz UXP-dee00a8a79394559e0e868cc72464c2de24583ac.zip |
Merge branch 'master' into Basilisk-release
Diffstat (limited to 'application/basilisk/components')
77 files changed, 8767 insertions, 3433 deletions
diff --git a/application/basilisk/components/about/AboutRedirector.cpp b/application/basilisk/components/about/AboutRedirector.cpp index b77949ea7..d52b063e2 100644 --- a/application/basilisk/components/about/AboutRedirector.cpp +++ b/application/basilisk/components/about/AboutRedirector.cpp @@ -80,11 +80,6 @@ static RedirEntry kRedirMap[] = { nsIAboutModule::ALLOW_SCRIPT }, { - "robots", "chrome://browser/content/aboutRobots.xhtml", - nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | - nsIAboutModule::ALLOW_SCRIPT - }, - { "searchreset", "chrome://browser/content/search/searchReset.xhtml", nsIAboutModule::ALLOW_SCRIPT | nsIAboutModule::HIDE_FROM_ABOUTABOUT @@ -97,10 +92,16 @@ static RedirEntry kRedirMap[] = { "welcomeback", "chrome://browser/content/aboutWelcomeBack.xhtml", nsIAboutModule::ALLOW_SCRIPT }, +#ifdef MOZ_SERVICES_SYNC + { + "sync-progress", "chrome://browser/content/sync/progress.xhtml", + nsIAboutModule::ALLOW_SCRIPT + }, { "sync-tabs", "chrome://browser/content/sync/aboutSyncTabs.xul", nsIAboutModule::ALLOW_SCRIPT }, +#endif { "home", "chrome://browser/content/abouthome/aboutHome.xhtml", nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | nsIAboutModule::URI_MUST_LOAD_IN_CHILD | @@ -128,10 +129,6 @@ static RedirEntry kRedirMap[] = { }, #endif { - "accounts", "chrome://browser/content/aboutaccounts/aboutaccounts.xhtml", - nsIAboutModule::ALLOW_SCRIPT - }, - { "reader", "chrome://global/content/reader/aboutReader.html", nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | nsIAboutModule::ALLOW_SCRIPT | diff --git a/application/basilisk/components/build/nsModule.cpp b/application/basilisk/components/build/nsModule.cpp index 1baccd710..3fdde8823 100644 --- a/application/basilisk/components/build/nsModule.cpp +++ b/application/basilisk/components/build/nsModule.cpp @@ -94,16 +94,17 @@ static const mozilla::Module::ContractIDEntry kBrowserContracts[] = { { NS_ABOUT_MODULE_CONTRACTID_PREFIX "feeds", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "privatebrowsing", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "rights", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, - { NS_ABOUT_MODULE_CONTRACTID_PREFIX "robots", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "searchreset", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "sessionrestore", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "welcomeback", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, +#ifdef MOZ_SERVICES_SYNC { NS_ABOUT_MODULE_CONTRACTID_PREFIX "sync-tabs", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, + { NS_ABOUT_MODULE_CONTRACTID_PREFIX "sync-progress", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, +#endif { NS_ABOUT_MODULE_CONTRACTID_PREFIX "home", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "newtab", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "preferences", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "downloads", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, - { NS_ABOUT_MODULE_CONTRACTID_PREFIX "accounts", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, #ifdef MOZ_SERVICES_HEALTHREPORT { NS_ABOUT_MODULE_CONTRACTID_PREFIX "healthreport", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, #endif diff --git a/application/basilisk/components/contextualidentity/content/usercontext.css b/application/basilisk/components/contextualidentity/content/usercontext.css deleted file mode 100644 index 728275d9f..000000000 --- a/application/basilisk/components/contextualidentity/content/usercontext.css +++ /dev/null @@ -1,91 +0,0 @@ -[data-identity-color="blue"] { - --identity-tab-color: #0996f8; - --identity-icon-color: #00a7e0; -} - -[data-identity-color="turquoise"] { - --identity-tab-color: #01bdad; - --identity-icon-color: #01bdad; -} - -[data-identity-color="green"] { - --identity-tab-color: #57bd35; - --identity-icon-color: #7dc14c; -} - -[data-identity-color="yellow"] { - --identity-tab-color: #ffcb00; - --identity-icon-color: #ffcb00; -} - -[data-identity-color="orange"] { - --identity-tab-color: #ff9216; - --identity-icon-color: #ff9216; -} - -[data-identity-color="red"] { - --identity-tab-color: #d92215; - --identity-icon-color: #d92215; -} - -[data-identity-color="pink"] { - --identity-tab-color: #ea385e; - --identity-icon-color: #ee5195; -} - -[data-identity-color="purple"] { - --identity-tab-color: #7a2f7a; - --identity-icon-color: #7a2f7a; -} - -[data-identity-icon="fingerprint"] { - --identity-icon: url("chrome://browser/content/usercontext.svg#fingerprint"); -} - -[data-identity-icon="briefcase"] { - --identity-icon: url("chrome://browser/content/usercontext.svg#briefcase"); -} - -[data-identity-icon="dollar"] { - --identity-icon: url("chrome://browser/content/usercontext.svg#dollar"); -} - -[data-identity-icon="cart"] { - --identity-icon: url("chrome://browser/content/usercontext.svg#cart"); -} - -[data-identity-icon="circle"] { - --identity-icon: url("chrome://browser/content/usercontext.svg#circle"); -} - -#userContext-indicator { - height: 16px; - width: 16px; -} - -#userContext-label { - margin-inline-end: 3px; - color: var(--identity-tab-color); -} - -#userContext-icons { - -moz-box-align: center; -} - -.tabbrowser-tab[usercontextid] { - background-image: linear-gradient(to right, transparent 20%, var(--identity-tab-color) 30%, var(--identity-tab-color) 70%, transparent 80%); - background-size: auto 2px; - background-repeat: no-repeat; -} - -.userContext-icon, -.menuitem-iconic[data-usercontextid] > .menu-iconic-left > .menu-iconic-icon, -.subviewbutton[usercontextid] > .toolbarbutton-icon, -#userContext-indicator { - background-image: var(--identity-icon); - filter: url(chrome://browser/skin/filters.svg#fill); - fill: var(--identity-icon-color); - background-size: contain; - background-repeat: no-repeat; - background-position: center center; -} diff --git a/application/basilisk/components/contextualidentity/jar.mn b/application/basilisk/components/contextualidentity/jar.mn deleted file mode 100644 index 848245949..000000000 --- a/application/basilisk/components/contextualidentity/jar.mn +++ /dev/null @@ -1,6 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -browser.jar: - content/browser/usercontext/usercontext.css (content/usercontext.css) diff --git a/application/basilisk/components/customizableui/CustomizableUI.jsm b/application/basilisk/components/customizableui/CustomizableUI.jsm index cb0f519b2..d56d63d99 100644 --- a/application/basilisk/components/customizableui/CustomizableUI.jsm +++ b/application/basilisk/components/customizableui/CustomizableUI.jsm @@ -158,10 +158,7 @@ var gUIStateBeforeReset = { XPCOMUtils.defineLazyGetter(this, "log", () => { let scope = {}; Cu.import("resource://gre/modules/Console.jsm", scope); - let debug; - try { - debug = Services.prefs.getBoolPref(kPrefCustomizationDebug); - } catch (ex) {} + let debug = Services.prefs.getBoolPref(kPrefCustomizationDebug, false); let consoleOptions = { maxLogLevel: debug ? "all" : "log", prefix: "CustomizableUI", @@ -200,7 +197,6 @@ var CustomizableUIInternal = { "find-button", "preferences-button", "add-ons-button", - "sync-button", ]; if (!AppConstants.MOZ_DEV_EDITION) { @@ -1915,16 +1911,10 @@ var CustomizableUIInternal = { // state immediately when a browser window opens, which is important for // other consumers of this API. loadSavedState: function() { - let state = null; - try { - state = Services.prefs.getCharPref(kPrefCustomizationState); - } catch (e) { - log.debug("No saved state found"); - // This will fail if nothing has been customized, so silently fall back to - // the defaults. - } - + let state = Services.prefs.getCharPref(kPrefCustomizationState, ""); if (!state) { + log.debug("No saved state found"); + // Nothing has been customized, so silently fall back to the defaults. return; } try { @@ -2209,10 +2199,7 @@ var CustomizableUIInternal = { this.notifyListeners("onWidgetAdded", widget.id, widget.currentArea, widget.currentPosition); } else if (widgetMightNeedAutoAdding) { - let autoAdd = true; - try { - autoAdd = Services.prefs.getBoolPref(kPrefCustomizationAutoAdd); - } catch (e) {} + let autoAdd = Services.prefs.getBoolPref(kPrefCustomizationAutoAdd, true); // If the widget doesn't have an existing placement, and it hasn't been // seen before, then add it to its default area so it can be used. diff --git a/application/basilisk/components/customizableui/CustomizableWidgets.jsm b/application/basilisk/components/customizableui/CustomizableWidgets.jsm index 09b3f167e..d4a191a97 100644 --- a/application/basilisk/components/customizableui/CustomizableWidgets.jsm +++ b/application/basilisk/components/customizableui/CustomizableWidgets.jsm @@ -23,8 +23,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "CharsetMenu", "resource://gre/modules/CharsetMenu.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", "resource://gre/modules/PrivateBrowsingUtils.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "ContextualIdentityService", - "resource://gre/modules/ContextualIdentityService.jsm"); XPCOMUtils.defineLazyGetter(this, "CharsetBundle", function() { const kCharsetBundle = "chrome://global/locale/charsetMenu.properties"; @@ -42,10 +40,7 @@ const kWidePanelItemClass = "panel-wide-item"; XPCOMUtils.defineLazyGetter(this, "log", () => { let scope = {}; Cu.import("resource://gre/modules/Console.jsm", scope); - let debug; - try { - debug = Services.prefs.getBoolPref(kPrefCustomizationDebug); - } catch (ex) {} + let debug = Services.prefs.getBoolPref(kPrefCustomizationDebug, false); let consoleOptions = { maxLogLevel: debug ? "all" : "log", prefix: "CustomizableWidgets", @@ -288,144 +283,6 @@ const CustomizableWidgets = [ log.debug("History view is being hidden!"); } }, { - id: "sync-button", - label: "remotetabs-panelmenu.label", - tooltiptext: "remotetabs-panelmenu.tooltiptext2", - type: "view", - viewId: "PanelUI-remotetabs", - defaultArea: CustomizableUI.AREA_PANEL, - deckIndices: { - DECKINDEX_TABS: 0, - DECKINDEX_TABSDISABLED: 1, - DECKINDEX_FETCHING: 2, - DECKINDEX_NOCLIENTS: 3, - }, - onCreated(aNode) { - // Add an observer to the button so we get the animation during sync. - // (Note the observer sets many attributes, including label and - // tooltiptext, but we only want the 'syncstatus' attribute for the - // animation) - let doc = aNode.ownerDocument; - let obnode = doc.createElementNS(kNSXUL, "observes"); - obnode.setAttribute("element", "sync-status"); - obnode.setAttribute("attribute", "syncstatus"); - aNode.appendChild(obnode); - }, - setDeckIndex(index) { - let deck = this._tabsList.ownerDocument.getElementById("PanelUI-remotetabs-deck"); - // We call setAttribute instead of relying on the XBL property setter due - // to things going wrong when we try and set the index before the XBL - // binding has been created - see bug 1241851 for the gory details. - deck.setAttribute("selectedIndex", index); - }, - - _showTabsPromise: Promise.resolve(), - // Update the tab list after any existing in-flight updates are complete. - _showTabs() { - this._showTabsPromise = this._showTabsPromise.then(() => { - return this.__showTabs(); - }); - }, - // Return a new promise to update the tab list. - __showTabs() { - let doc = this._tabsList.ownerDocument; - return SyncedTabs.getTabClients().then(clients => { - // The view may have been hidden while the promise was resolving. - if (!this._tabsList) { - return; - } - if (clients.length === 0 && !SyncedTabs.hasSyncedThisSession) { - // the "fetching tabs" deck is being shown - let's leave it there. - // When that first sync completes we'll be notified and update. - return; - } - - if (clients.length === 0) { - this.setDeckIndex(this.deckIndices.DECKINDEX_NOCLIENTS); - return; - } - - this.setDeckIndex(this.deckIndices.DECKINDEX_TABS); - this._clearTabList(); - SyncedTabs.sortTabClientsByLastUsed(clients, 50 /* maxTabs */); - let fragment = doc.createDocumentFragment(); - - for (let client of clients) { - // add a menu separator for all clients other than the first. - if (fragment.lastChild) { - let separator = doc.createElementNS(kNSXUL, "menuseparator"); - fragment.appendChild(separator); - } - this._appendClient(client, fragment); - } - this._tabsList.appendChild(fragment); - }).catch(err => { - Cu.reportError(err); - }).then(() => { - // an observer for tests. - Services.obs.notifyObservers(null, "synced-tabs-menu:test:tabs-updated", null); - }); - }, - _clearTabList () { - let list = this._tabsList; - while (list.lastChild) { - list.lastChild.remove(); - } - }, - _showNoClientMessage() { - this._appendMessageLabel("notabslabel"); - }, - _appendMessageLabel(messageAttr, appendTo = null) { - if (!appendTo) { - appendTo = this._tabsList; - } - let message = this._tabsList.getAttribute(messageAttr); - let doc = this._tabsList.ownerDocument; - let messageLabel = doc.createElementNS(kNSXUL, "label"); - messageLabel.textContent = message; - appendTo.appendChild(messageLabel); - return messageLabel; - }, - _appendClient: function (client, attachFragment) { - let doc = attachFragment.ownerDocument; - // Create the element for the remote client. - let clientItem = doc.createElementNS(kNSXUL, "label"); - clientItem.setAttribute("itemtype", "client"); - let window = doc.defaultView; - clientItem.setAttribute("tooltiptext", - window.gSyncUI.formatLastSyncDate(new Date(client.lastModified))); - clientItem.textContent = client.name; - - attachFragment.appendChild(clientItem); - - if (client.tabs.length == 0) { - let label = this._appendMessageLabel("notabsforclientlabel", attachFragment); - label.setAttribute("class", "PanelUI-remotetabs-notabsforclient-label"); - } else { - for (let tab of client.tabs) { - let tabEnt = this._createTabElement(doc, tab); - attachFragment.appendChild(tabEnt); - } - } - }, - _createTabElement(doc, tabInfo) { - let item = doc.createElementNS(kNSXUL, "toolbarbutton"); - let tooltipText = (tabInfo.title ? tabInfo.title + "\n" : "") + tabInfo.url; - item.setAttribute("itemtype", "tab"); - item.setAttribute("class", "subviewbutton"); - item.setAttribute("targetURI", tabInfo.url); - item.setAttribute("label", tabInfo.title != "" ? tabInfo.title : tabInfo.url); - item.setAttribute("image", tabInfo.icon); - item.setAttribute("tooltiptext", tooltipText); - // We need to use "click" instead of "command" here so openUILink - // respects different buttons (eg, to open in a new tab). - item.addEventListener("click", e => { - doc.defaultView.openUILink(tabInfo.url, e); - CustomizableUI.hidePanelForNode(item); - }); - return item; - }, - }, { id: "privatebrowsing-button", shortcutId: "key_privatebrowsing", defaultArea: CustomizableUI.AREA_PANEL, @@ -977,89 +834,6 @@ const CustomizableWidgets = [ let win = aEvent.view; win.MailIntegration.sendLinkForBrowser(win.gBrowser.selectedBrowser) } - }, { - id: "containers-panelmenu", - type: "view", - viewId: "PanelUI-containers", - hasObserver: false, - onCreated: function(aNode) { - let doc = aNode.ownerDocument; - let win = doc.defaultView; - let items = doc.getElementById("PanelUI-containersItems"); - - let onItemCommand = function (aEvent) { - let item = aEvent.target; - if (item.hasAttribute("usercontextid")) { - let userContextId = parseInt(item.getAttribute("usercontextid")); - win.openUILinkIn(win.BROWSER_NEW_TAB_URL, "tab", {userContextId}); - } - }; - items.addEventListener("command", onItemCommand); - - if (PrivateBrowsingUtils.isWindowPrivate(win)) { - aNode.setAttribute("disabled", "true"); - } - - this.updateVisibility(aNode); - - if (!this.hasObserver) { - Services.prefs.addObserver("privacy.userContext.enabled", this, true); - this.hasObserver = true; - } - }, - onViewShowing: function(aEvent) { - let doc = aEvent.target.ownerDocument; - - let items = doc.getElementById("PanelUI-containersItems"); - - while (items.firstChild) { - items.firstChild.remove(); - } - - let fragment = doc.createDocumentFragment(); - let bundle = doc.getElementById("bundle_browser"); - - ContextualIdentityService.getIdentities().forEach(identity => { - let label = ContextualIdentityService.getUserContextLabel(identity.userContextId); - - let item = doc.createElementNS(kNSXUL, "toolbarbutton"); - item.setAttribute("label", label); - item.setAttribute("usercontextid", identity.userContextId); - item.setAttribute("class", "subviewbutton"); - item.setAttribute("data-identity-color", identity.color); - item.setAttribute("data-identity-icon", identity.icon); - - fragment.appendChild(item); - }); - - fragment.appendChild(doc.createElementNS(kNSXUL, "menuseparator")); - - let item = doc.createElementNS(kNSXUL, "toolbarbutton"); - item.setAttribute("label", bundle.getString("userContext.aboutPage.label")); - item.setAttribute("command", "Browser:OpenAboutContainers"); - item.setAttribute("class", "subviewbutton"); - fragment.appendChild(item); - - items.appendChild(fragment); - }, - - updateVisibility(aNode) { - aNode.hidden = !Services.prefs.getBoolPref("privacy.userContext.enabled"); - }, - - observe(aSubject, aTopic, aData) { - let {instances} = CustomizableUI.getWidget("containers-panelmenu"); - for (let {node} of instances) { - if (node) { - this.updateVisibility(node); - } - } - }, - - QueryInterface: XPCOMUtils.generateQI([ - Ci.nsISupportsWeakReference, - Ci.nsIObserver - ]), }]; let preferencesButton = { diff --git a/application/basilisk/components/customizableui/CustomizeMode.jsm b/application/basilisk/components/customizableui/CustomizeMode.jsm index 4365ddfbc..2958655d2 100644 --- a/application/basilisk/components/customizableui/CustomizeMode.jsm +++ b/application/basilisk/components/customizableui/CustomizeMode.jsm @@ -40,9 +40,7 @@ let gDebug; XPCOMUtils.defineLazyGetter(this, "log", () => { let scope = {}; Cu.import("resource://gre/modules/Console.jsm", scope); - try { - gDebug = Services.prefs.getBoolPref(kPrefCustomizationDebug); - } catch (ex) {} + gDebug = Services.prefs.getBoolPref(kPrefCustomizationDebug, false); let consoleOptions = { maxLogLevel: gDebug ? "all" : "log", prefix: "CustomizeMode", @@ -1503,10 +1501,7 @@ CustomizeMode.prototype = { if (!AppConstants.CAN_DRAW_IN_TITLEBAR) { return; } - let drawInTitlebar = true; - try { - drawInTitlebar = Services.prefs.getBoolPref(kDrawInTitlebarPref); - } catch (ex) { } + let drawInTitlebar = Services.prefs.getBoolPref(kDrawInTitlebarPref, true); let button = this.document.getElementById("customization-titlebar-visibility-button"); // Drawing in the titlebar means 'hiding' the titlebar: if (drawInTitlebar) { diff --git a/application/basilisk/components/customizableui/content/panelUI.inc.xul b/application/basilisk/components/customizableui/content/panelUI.inc.xul index 8ebd93327..da8077554 100644 --- a/application/basilisk/components/customizableui/content/panelUI.inc.xul +++ b/application/basilisk/components/customizableui/content/panelUI.inc.xul @@ -20,27 +20,6 @@ oncommand="gMenuButtonUpdateBadge.onMenuPanelCommand(event);" wrap="true" hidden="true"/> - <hbox id="PanelUI-footer-fxa"> - <hbox id="PanelUI-fxa-status" - defaultlabel="&fxaSignIn.label;" - signedinTooltiptext="&syncSettings.label;" - tooltiptext="&syncSettings.label;" - errorlabel="&fxaSignInError.label;" - unverifiedlabel="&fxaUnverified.label;" - settingslabel="&syncSettings.label;" - onclick="if (event.which == 1) gFxAccounts.onMenuPanelCommand();"> - <image id="PanelUI-fxa-avatar"/> - <toolbarbutton id="PanelUI-fxa-label" - fxabrandname="&syncBrand.fxAccount.label;"/> - </hbox> - <toolbarseparator/> - <toolbarbutton id="PanelUI-fxa-icon" - oncommand="gSyncUI.doSync();" - closemenu="none"> - <observes element="sync-status" attribute="syncstatus"/> - <observes element="sync-status" attribute="tooltiptext"/> - </toolbarbutton> - </hbox> <hbox id="PanelUI-footer-inner"> <toolbarbutton id="PanelUI-customize" label="&appMenuCustomize.label;" @@ -103,95 +82,6 @@ oncommand="PlacesCommandHook.showPlacesOrganizer('History'); CustomizableUI.hidePanelForNode(this);"/> </panelview> - <panelview id="PanelUI-remotetabs" flex="1" class="PanelUI-subView"> - <label value="&appMenuRemoteTabs.label;" class="panel-subview-header"/> - <vbox class="panel-subview-body"> - <!-- this widget has 3 boxes in the body, but only 1 is ever visible --> - <!-- When Sync is ready to sync --> - <vbox id="PanelUI-remotetabs-main" observes="sync-syncnow-state"> - <vbox id="PanelUI-remotetabs-buttons"> - <toolbarbutton id="PanelUI-remotetabs-view-sidebar" - class="subviewbutton" - oncommand="BrowserOpenSyncTabs();" - label="&appMenuRemoteTabs.sidebar.label;"/> - <toolbarbutton id="PanelUI-remotetabs-syncnow" - observes="sync-status" - class="subviewbutton" - oncommand="gSyncUI.doSync();" - closemenu="none"/> - <menuseparator id="PanelUI-remotetabs-separator"/> - </vbox> - <deck id="PanelUI-remotetabs-deck"> - <!-- Sync is ready to Sync and the "tabs" engine is enabled --> - <vbox id="PanelUI-remotetabs-tabspane"> - <vbox id="PanelUI-remotetabs-tabslist" - notabsforclientlabel="&appMenuRemoteTabs.notabs.label;" - /> - </vbox> - <!-- Sync is ready to Sync but the "tabs" engine isn't enabled--> - <hbox id="PanelUI-remotetabs-tabsdisabledpane" pack="center" flex="1"> - <vbox class="PanelUI-remotetabs-instruction-box"> - <hbox pack="center"> - <image class="fxaSyncIllustration" alt=""/> - </hbox> - <label class="PanelUI-remotetabs-instruction-label">&appMenuRemoteTabs.tabsnotsyncing.label;</label> - <hbox pack="center"> - <toolbarbutton class="PanelUI-remotetabs-prefs-button" - label="&appMenuRemoteTabs.openprefs.label;" - oncommand="gSyncUI.openSetup(null, 'synced-tabs');"/> - </hbox> - </vbox> - </hbox> - <!-- Sync is ready to Sync but we are still fetching the tabs to show --> - <vbox id="PanelUI-remotetabs-fetching"> - <!-- Show intentionally blank panel, see bug 1239845 --> - </vbox> - <!-- Sync has only 1 (ie, this) device connected --> - <hbox id="PanelUI-remotetabs-nodevicespane" pack="center" flex="1"> - <vbox class="PanelUI-remotetabs-instruction-box"> - <hbox pack="center"> - <image class="fxaSyncIllustration" alt=""/> - </hbox> - <label class="PanelUI-remotetabs-instruction-title">&appMenuRemoteTabs.noclients.title;</label> - <label class="PanelUI-remotetabs-instruction-label">&appMenuRemoteTabs.noclients.subtitle;</label> - <!-- The inner HTML for PanelUI-remotetabs-mobile-promo is built at runtime --> - <label id="PanelUI-remotetabs-mobile-promo" fxAccountsBrand="&syncBrand.fxAccount.label;"/> - </vbox> - </hbox> - </deck> - </vbox> - <!-- a box to ensure contained boxes are centered horizonally --> - <hbox pack="center" flex="1"> - <!-- When Sync is not configured --> - <vbox id="PanelUI-remotetabs-setupsync" - flex="1" - align="center" - class="PanelUI-remotetabs-instruction-box" - observes="sync-setup-state"> - <image class="fxaSyncIllustration" alt=""/> - <label class="PanelUI-remotetabs-instruction-label">&appMenuRemoteTabs.notsignedin.label;</label> - <toolbarbutton class="PanelUI-remotetabs-prefs-button" - label="&appMenuRemoteTabs.signin.label;" - oncommand="gSyncUI.openSetup(null, 'synced-tabs');"/> - </vbox> - <!-- When Sync needs re-authentication. This uses the exact same messaging - as "Sync is not configured" but remains a separate box so we get - the goodness of observing broadcasters to manage the hidden states --> - <vbox id="PanelUI-remotetabs-reauthsync" - flex="1" - align="center" - class="PanelUI-remotetabs-instruction-box" - observes="sync-reauth-state"> - <image class="fxaSyncIllustration" alt=""/> - <label class="PanelUI-remotetabs-instruction-label">&appMenuRemoteTabs.notsignedin.label;</label> - <toolbarbutton class="PanelUI-remotetabs-prefs-button" - label="&appMenuRemoteTabs.signin.label;" - oncommand="gSyncUI.openSetup(null, 'synced-tabs');"/> - </vbox> - </hbox> - </vbox> - </panelview> - <panelview id="PanelUI-bookmarks" flex="1" class="PanelUI-subView"> <label value="&bookmarksMenu.label;" class="panel-subview-header"/> <vbox class="panel-subview-body"> diff --git a/application/basilisk/components/customizableui/moz.build b/application/basilisk/components/customizableui/moz.build index 034630dc9..5797a03b0 100644 --- a/application/basilisk/components/customizableui/moz.build +++ b/application/basilisk/components/customizableui/moz.build @@ -9,7 +9,6 @@ DIRS += [ ] EXTRA_JS_MODULES += [ - 'CustomizableUI.jsm', 'CustomizableWidgets.jsm', 'CustomizeMode.jsm', 'DragPositionManager.jsm', @@ -17,5 +16,9 @@ EXTRA_JS_MODULES += [ 'ScrollbarSampler.jsm', ] +EXTRA_PP_JS_MODULES += [ + 'CustomizableUI.jsm', +] + if CONFIG['MOZ_WIDGET_TOOLKIT'] in ('windows', 'cocoa'): DEFINES['CAN_DRAW_IN_TITLEBAR'] = 1 diff --git a/application/basilisk/components/distribution.js b/application/basilisk/components/distribution.js index 589129a5a..c30e4dcfe 100644 --- a/application/basilisk/components/distribution.js +++ b/application/basilisk/components/distribution.js @@ -22,10 +22,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", this.DistributionCustomizer = function DistributionCustomizer() { // For parallel xpcshell testing purposes allow loading the distribution.ini // file from the profile folder through an hidden pref. - let loadFromProfile = false; - try { - loadFromProfile = Services.prefs.getBoolPref("distribution.testing.loadFromProfile"); - } catch (ex) {} + loadFromProfile = Services.prefs.getBoolPref("distribution.testing.loadFromProfile", false); let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. getService(Ci.nsIProperties); try { @@ -60,13 +57,7 @@ DistributionCustomizer.prototype = { }, get _locale() { - let locale; - try { - locale = this._prefs.getCharPref("general.useragent.locale"); - } - catch (e) { - locale = "en-US"; - } + let locale = this._prefs.getCharPref("general.useragent.locale", "en-US"); this.__defineGetter__("_locale", () => locale); return this._locale; }, @@ -291,11 +282,7 @@ DistributionCustomizer.prototype = { this._ini.getString("Global", "id") + ".bookmarksProcessed"; } - let bmProcessed = false; - try { - bmProcessed = this._prefs.getBoolPref(bmProcessedPref); - } - catch (e) {} + let bmProcessed = this._prefs.getBoolPref(bmProcessedPref, false); if (!bmProcessed) { if (sections["BookmarksMenu"]) diff --git a/application/basilisk/components/feeds/FeedWriter.js b/application/basilisk/components/feeds/FeedWriter.js index 20f1399b0..ceb2a7e2f 100644 --- a/application/basilisk/components/feeds/FeedWriter.js +++ b/application/basilisk/components/feeds/FeedWriter.js @@ -19,12 +19,7 @@ function LOG(str) { let prefB = Cc["@mozilla.org/preferences-service;1"]. getService(Ci.nsIPrefBranch); - let shouldLog = false; - try { - shouldLog = prefB.getBoolPref("feeds.log"); - } - catch (ex) { - } + let shouldLog = prefB.getBoolPref("feeds.log", false); if (shouldLog) dump("*** Feeds: " + str + "\n"); diff --git a/application/basilisk/components/feeds/WebContentConverter.js b/application/basilisk/components/feeds/WebContentConverter.js index 2cb5cd145..159eca7c2 100644 --- a/application/basilisk/components/feeds/WebContentConverter.js +++ b/application/basilisk/components/feeds/WebContentConverter.js @@ -187,13 +187,8 @@ const Utils = { // check if it is in the black list let pb = Services.prefs; - let allowed; - try { - allowed = pb.getBoolPref(PREF_HANDLER_EXTERNAL_PREFIX + "." + aProtocol); - } - catch (e) { - allowed = pb.getBoolPref(PREF_HANDLER_EXTERNAL_PREFIX + "-default"); - } + let allowed = pb.getBoolPref(PREF_HANDLER_EXTERNAL_PREFIX + "." + aProtocol, + pb.getBoolPref(PREF_HANDLER_EXTERNAL_PREFIX + "-default")); if (!allowed) { throw this.getSecurityError( `Not allowed to register a protocol handler for ${aProtocol}`, diff --git a/application/basilisk/components/moz.build b/application/basilisk/components/moz.build index a7685cbfc..a9c29936b 100644 --- a/application/basilisk/components/moz.build +++ b/application/basilisk/components/moz.build @@ -6,7 +6,6 @@ DIRS += [ 'about', - 'contextualidentity', 'customizableui', 'dirprovider', 'downloads', @@ -20,10 +19,12 @@ DIRS += [ 'sessionstore', 'shell', 'selfsupport', - 'syncedtabs', 'translation', ] +if CONFIG['MOZ_SERVICES_SYNC']: + DIRS += ['sync'] + DIRS += ['build'] XPIDL_SOURCES += [ @@ -36,6 +37,9 @@ XPIDL_MODULE = 'browsercompsbase' EXTRA_COMPONENTS += [ 'BrowserComponents.manifest', 'nsBrowserContentHandler.js', +] + +EXTRA_PP_COMPONENTS += [ 'nsBrowserGlue.js', ] diff --git a/application/basilisk/components/newtab/NewTabPrefsProvider.jsm b/application/basilisk/components/newtab/NewTabPrefsProvider.jsm index c1d8b4149..ad0d1e581 100644 --- a/application/basilisk/components/newtab/NewTabPrefsProvider.jsm +++ b/application/basilisk/components/newtab/NewTabPrefsProvider.jsm @@ -21,7 +21,6 @@ const gPrefsMap = new Map([ ["browser.newtabpage.remote.mode", "str"], ["browser.newtabpage.remote.version", "str"], ["browser.newtabpage.enabled", "bool"], - ["browser.newtabpage.enhanced", "bool"], ["browser.newtabpage.introShown", "bool"], ["browser.newtabpage.updateIntroShown", "bool"], ["browser.newtabpage.pinned", "str"], @@ -34,7 +33,6 @@ const gPrefsMap = new Map([ // prefs that are important for the newtab page const gNewtabPagePrefs = new Set([ "browser.newtabpage.enabled", - "browser.newtabpage.enhanced", "browser.newtabpage.pinned", "browser.newtabpage.blocked", "browser.newtabpage.introShown", diff --git a/application/basilisk/components/nsBrowserContentHandler.js b/application/basilisk/components/nsBrowserContentHandler.js index 74144fc1b..d65e52594 100644 --- a/application/basilisk/components/nsBrowserContentHandler.js +++ b/application/basilisk/components/nsBrowserContentHandler.js @@ -100,20 +100,14 @@ const OVERRIDE_NEW_BUILD_ID = 3; * OVERRIDE_NONE otherwise. */ function needHomepageOverride(prefb) { - var savedmstone = null; - try { - savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone"); - } catch (e) {} + var savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone", ""); if (savedmstone == "ignore") return OVERRIDE_NONE; var mstone = Services.appinfo.platformVersion; - var savedBuildID = null; - try { - savedBuildID = prefb.getCharPref("browser.startup.homepage_override.buildID"); - } catch (e) {} + var savedBuildID = prefb.getCharPref("browser.startup.homepage_override.buildID", ""); var buildID = Services.appinfo.platformBuildID; @@ -489,10 +483,7 @@ nsBrowserContentHandler.prototype = { // URL if we do end up showing an overridePage. This makes it possible // to have the overridePage's content vary depending on the version we're // upgrading from. - let old_mstone = "unknown"; - try { - old_mstone = Services.prefs.getCharPref("browser.startup.homepage_override.mstone"); - } catch (ex) {} + let old_mstone = Services.prefs.getCharPref("browser.startup.homepage_override.mstone", "unknown"); override = needHomepageOverride(prefb); if (override != OVERRIDE_NONE) { switch (override) { diff --git a/application/basilisk/components/nsBrowserGlue.js b/application/basilisk/components/nsBrowserGlue.js index d77e97f87..d29009b13 100644 --- a/application/basilisk/components/nsBrowserGlue.js +++ b/application/basilisk/components/nsBrowserGlue.js @@ -32,7 +32,6 @@ XPCOMUtils.defineLazyServiceGetter(this, "AlertsService", "@mozilla.org/alerts-s ["ContentPrefServiceParent", "resource://gre/modules/ContentPrefServiceParent.jsm"], ["ContentSearch", "resource:///modules/ContentSearch.jsm"], ["DateTimePickerHelper", "resource://gre/modules/DateTimePickerHelper.jsm"], - ["DirectoryLinksProvider", "resource:///modules/DirectoryLinksProvider.jsm"], ["Feeds", "resource:///modules/Feeds.jsm"], ["FileUtils", "resource://gre/modules/FileUtils.jsm"], ["FormValidationHandler", "resource:///modules/FormValidationHandler.jsm"], @@ -143,6 +142,7 @@ BrowserGlue.prototype = { Services.prefs.savePrefFile(null); }, +#ifdef MOZ_SERVICES_SYNC _setSyncAutoconnectDelay: function BG__setSyncAutoconnectDelay() { // Assume that a non-zero value for services.sync.autoconnectDelay should override if (Services.prefs.prefHasUserValue("services.sync.autoconnectDelay")) { @@ -164,6 +164,7 @@ BrowserGlue.prototype = { Cu.import("resource://services-sync/main.js"); Weave.Service.scheduler.delayedAutoConnect(delay); }, +#endif // nsIObserver implementation observe: function BG_observe(subject, topic, data) { @@ -210,18 +211,14 @@ BrowserGlue.prototype = { this._setPrefToSaveSession(); } break; +#ifdef MOZ_SERVICES_SYNC case "weave:service:ready": this._setSyncAutoconnectDelay(); break; - case "fxaccounts:onverified": - this._showSyncStartedDoorhanger(); - break; - case "fxaccounts:device_disconnected": - this._onDeviceDisconnected(); - break; - case "weave:engine:clients:display-uris": - this._onDisplaySyncURIs(subject); - break; + case "weave:engine:clients:display-uri": + this._onDisplaySyncURI(subject); + break; +#endif case "session-save": this._setPrefToSaveSession(true); subject.QueryInterface(Ci.nsISupportsPRBool); @@ -428,10 +425,10 @@ BrowserGlue.prototype = { os.addObserver(this, "browser-lastwindow-close-requested", false); os.addObserver(this, "browser-lastwindow-close-granted", false); } +#ifdef MOZ_SERVICES_SYNC os.addObserver(this, "weave:service:ready", false); - os.addObserver(this, "fxaccounts:onverified", false); - os.addObserver(this, "fxaccounts:device_disconnected", false); - os.addObserver(this, "weave:engine:clients:display-uris", false); + os.addObserver(this, "weave:engine:clients:display-uri", false); +#endif os.addObserver(this, "session-save", false); os.addObserver(this, "places-init-complete", false); this._isPlacesInitObserver = true; @@ -479,10 +476,10 @@ BrowserGlue.prototype = { os.removeObserver(this, "browser-lastwindow-close-requested"); os.removeObserver(this, "browser-lastwindow-close-granted"); } +#ifdef MOZ_SERVICES_SYNC os.removeObserver(this, "weave:service:ready"); - os.removeObserver(this, "fxaccounts:onverified"); - os.removeObserver(this, "fxaccounts:device_disconnected"); - os.removeObserver(this, "weave:engine:clients:display-uris"); + os.removeObserver(this, "weave:engine:clients:display-uri"); +#endif os.removeObserver(this, "session-save"); if (this._bookmarksBackupIdleTime) { this._idleService.removeIdleObserver(this, this._bookmarksBackupIdleTime); @@ -657,9 +654,7 @@ BrowserGlue.prototype = { webrtcUI.init(); AboutHome.init(); - DirectoryLinksProvider.init(); NewTabUtils.init(); - NewTabUtils.links.addProvider(DirectoryLinksProvider); AboutNewTab.init(); NewTabMessages.init(); @@ -963,10 +958,7 @@ BrowserGlue.prototype = { // Offer to reset a user's profile if it hasn't been used for 60 days. const OFFER_PROFILE_RESET_INTERVAL_MS = 60 * 24 * 60 * 60 * 1000; let lastUse = Services.appinfo.replacedLockTime; - let disableResetPrompt = false; - try { - disableResetPrompt = Services.prefs.getBoolPref("browser.disableResetPrompt"); - } catch (e) {} + let disableResetPrompt = Services.prefs.getBoolPref("browser.disableResetPrompt", false); if (!disableResetPrompt && lastUse && Date.now() - lastUse >= OFFER_PROFILE_RESET_INTERVAL_MS) { @@ -1507,10 +1499,7 @@ BrowserGlue.prototype = { } catch (ex) {} // Support legacy bookmarks.html format for apps that depend on that format. - let autoExportHTML = false; - try { - autoExportHTML = Services.prefs.getBoolPref("browser.bookmarks.autoExportHTML"); - } catch (ex) {} // Do not export. + let autoExportHTML = Services.prefs.getBoolPref("browser.bookmarks.autoExportHTML", false); // Do not export. if (autoExportHTML) { // Sqlite.jsm and Places shutdown happen at profile-before-change, thus, // to be on the safe side, this should run earlier. @@ -1580,10 +1569,7 @@ BrowserGlue.prototype = { // An import operation is about to run. // Don't try to recreate smart bookmarks if autoExportHTML is true or // smart bookmarks are disabled. - let smartBookmarksVersion = 0; - try { - smartBookmarksVersion = Services.prefs.getIntPref("browser.places.smartBookmarksVersion"); - } catch (ex) {} + let smartBookmarksVersion = Services.prefs.getIntPref("browser.places.smartBookmarksVersion", 0); if (!autoExportHTML && smartBookmarksVersion != -1) Services.prefs.setIntPref("browser.places.smartBookmarksVersion", 0); @@ -1934,10 +1920,7 @@ BrowserGlue.prototype = { // Refactor urlbar suggestion preferences to make it extendable and // allow new suggestion types (e.g: search suggestions). let types = ["history", "bookmark", "openpage"]; - let defaultBehavior = 0; - try { - defaultBehavior = Services.prefs.getIntPref("browser.urlbar.default.behavior"); - } catch (ex) {} + let defaultBehavior = Services.prefs.getIntPref("browser.urlbar.default.behavior", 0); try { let autocompleteEnabled = Services.prefs.getBoolPref("browser.urlbar.autocomplete.enabled"); if (!autocompleteEnabled) { @@ -1990,12 +1973,8 @@ BrowserGlue.prototype = { if (currentUIVersion < 30) { // Convert old devedition theme pref to lightweight theme storage - let lightweightThemeSelected = false; - let selectedThemeID = null; - try { - lightweightThemeSelected = Services.prefs.prefHasUserValue("lightweightThemes.selectedThemeID"); - selectedThemeID = Services.prefs.getCharPref("lightweightThemes.selectedThemeID"); - } catch (e) {} + let lightweightThemeSelected = Services.prefs.prefHasUserValue("lightweightThemes.selectedThemeID", false); + let selectedThemeID = Services.prefs.getCharPref("lightweightThemes.selectedThemeID", ""); let defaultThemeSelected = false; try { @@ -2145,10 +2124,7 @@ BrowserGlue.prototype = { const MAX_RESULTS = 10; // Get current smart bookmarks version. If not set, create them. - let smartBookmarksCurrentVersion = 0; - try { - smartBookmarksCurrentVersion = Services.prefs.getIntPref(SMART_BOOKMARKS_PREF); - } catch (ex) {} + let smartBookmarksCurrentVersion = Services.prefs.getIntPref(SMART_BOOKMARKS_PREF, 0); // If version is current, or smart bookmarks are disabled, bail out. if (smartBookmarksCurrentVersion == -1 || @@ -2274,90 +2250,29 @@ BrowserGlue.prototype = { chromeWindow.openPreferences(...args); }, +#ifdef MOZ_SERVICES_SYNC /** - * Called as an observer when Sync's "display URIs" notification is fired. + * Called as an observer when Sync's "display URI" notification is fired. * - * We open the received URIs in background tabs. + * We open the received URI in a background tab. + * + * Eventually, this will likely be replaced by a more robust tab syncing + * feature. This functionality is considered somewhat evil by UX because it + * opens a new tab automatically without any prompting. However, it is a + * lesser evil than sending a tab to a specific device (from e.g. Fennec) + * and having nothing happen on the receiving end. */ - _onDisplaySyncURIs: function _onDisplaySyncURIs(data) { + _onDisplaySyncURI: function _onDisplaySyncURI(data) { try { - // The payload is wrapped weirdly because of how Sync does notifications. - const URIs = data.wrappedJSObject.object; - - const findWindow = () => RecentWindow.getMostRecentBrowserWindow({private: false}); - - // win can be null, but it's ok, we'll assign it later in openTab() - let win = findWindow(); + let tabbrowser = RecentWindow.getMostRecentBrowserWindow({private: false}).gBrowser; - const openTab = URI => { - let tab; - if (!win) { - Services.appShell.hiddenDOMWindow.open(URI.uri); - win = findWindow(); - tab = win.gBrowser.tabs[0]; - } else { - tab = win.gBrowser.addTab(URI.uri); - } - tab.setAttribute("attention", true); - return tab; - }; - - const firstTab = openTab(URIs[0]); - URIs.slice(1).forEach(URI => openTab(URI)); - - let title, body; - const deviceName = Weave.Service.clientsEngine.getClientName(URIs[0].clientId); - const bundle = Services.strings.createBundle("chrome://browser/locale/accounts.properties"); - if (URIs.length == 1) { - // Due to bug 1305895, tabs from iOS may not have device information, so - // we have separate strings to handle those cases. (See Also - // unnamedTabsArrivingNotificationNoDevice.body below) - if (deviceName) { - title = bundle.formatStringFromName("tabArrivingNotificationWithDevice.title", [deviceName], 1); - } else { - title = bundle.GetStringFromName("tabArrivingNotification.title"); - } - // Use the page URL as the body. We strip the fragment and query to - // reduce size, and also format it the same way that the url bar would. - body = URIs[0].uri.replace(/[?#].*$/, ""); - if (win.gURLBar) { - body = win.gURLBar.trimValue(body); - } - } else { - title = bundle.GetStringFromName("tabsArrivingNotification.title"); - const allSameDevice = URIs.every(URI => URI.clientId == URIs[0].clientId); - const unknownDevice = allSameDevice && !deviceName; - let tabArrivingBody; - if (unknownDevice) { - tabArrivingBody = "unnamedTabsArrivingNotificationNoDevice.body"; - } else if (allSameDevice) { - tabArrivingBody = "unnamedTabsArrivingNotification2.body"; - } else { - tabArrivingBody = "unnamedTabsArrivingNotificationMultiple2.body" - } - - body = bundle.GetStringFromName(tabArrivingBody); - body = PluralForm.get(URIs.length, body); - body = body.replace("#1", URIs.length); - body = body.replace("#2", deviceName); - } - - const clickCallback = (subject, topic, data) => { - if (topic == "alertclickcallback") { - win.gBrowser.selectedTab = firstTab; - } - } - - // Specify an icon because on Windows no icon is shown at the moment - let imageURL; - if (AppConstants.platform == "win") { - imageURL = "chrome://branding/content/icon64.png"; - } - AlertsService.showAlertNotification(imageURL, title, body, true, null, clickCallback); + // The payload is wrapped weirdly because of how Sync does notifications. + tabbrowser.addTab(data.wrappedJSObject.object.uri); } catch (ex) { - Cu.reportError("Error displaying tab(s) received by Sync: " + ex); + Cu.reportError("Error displaying tab received by Sync: " + ex); } }, +#endif _onDeviceDisconnected() { let bundle = Services.strings.createBundle("chrome://browser/locale/accounts.properties"); diff --git a/application/basilisk/components/places/PlacesUIUtils.jsm b/application/basilisk/components/places/PlacesUIUtils.jsm index 17fa276aa..035fc12c2 100644 --- a/application/basilisk/components/places/PlacesUIUtils.jsm +++ b/application/basilisk/components/places/PlacesUIUtils.jsm @@ -1418,9 +1418,9 @@ this.PlacesUIUtils = { }, shouldShowTabsFromOtherComputersMenuitem: function() { - let weaveOK = Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED && - Weave.Svc.Prefs.get("firstSync", "") != "notReady"; - return weaveOK; +#ifdef MOZ_SERVICES_SYNC + // Weave code to enable menu item +#endif }, /** diff --git a/application/basilisk/components/places/moz.build b/application/basilisk/components/places/moz.build index 9e5a2c074..ea6d43538 100644 --- a/application/basilisk/components/places/moz.build +++ b/application/basilisk/components/places/moz.build @@ -6,6 +6,6 @@ JAR_MANIFESTS += ['jar.mn'] -EXTRA_JS_MODULES += [ +EXTRA_PP_JS_MODULES += [ 'PlacesUIUtils.jsm', ] diff --git a/application/basilisk/components/preferences/containers.js b/application/basilisk/components/preferences/containers.js deleted file mode 100644 index 6ca5853f7..000000000 --- a/application/basilisk/components/preferences/containers.js +++ /dev/null @@ -1,176 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -Components.utils.import("resource://gre/modules/Services.jsm"); -Components.utils.import("resource://gre/modules/ContextualIdentityService.jsm"); - -const containersBundle = Services.strings.createBundle("chrome://browser/locale/preferences/containers.properties"); - -const HTMLNS = "http://www.w3.org/1999/xhtml"; - -let gContainersManager = { - icons: [ - "fingerprint", - "briefcase", - "dollar", - "cart", - "circle" - ], - - colors: [ - "blue", - "turquoise", - "green", - "yellow", - "orange", - "red", - "pink", - "purple" - ], - - onLoad() { - let params = window.arguments[0] || {}; - this.init(params); - }, - - init(aParams) { - this.userContextId = aParams.userContextId || null; - this.identity = aParams.identity; - - if (aParams.windowTitle) { - document.title = aParams.windowTitle; - } - - const iconWrapper = document.getElementById("iconWrapper"); - iconWrapper.appendChild(this.createIconButtons()); - - const colorWrapper = document.getElementById("colorWrapper"); - colorWrapper.appendChild(this.createColorSwatches()); - - if (this.identity.name) { - const name = document.getElementById("name"); - name.value = this.identity.name; - this.checkForm(); - } - - this.setLabelsMinWidth(); - - // This is to prevent layout jank caused by the svgs and outlines rendering at different times - document.getElementById("containers-content").removeAttribute("hidden"); - }, - - setLabelsMinWidth() { - const labelMinWidth = containersBundle.GetStringFromName("containers.labelMinWidth"); - const labels = [ - document.getElementById("nameLabel"), - document.getElementById("iconLabel"), - document.getElementById("colorLabel") - ]; - for (let label of labels) { - label.style.minWidth = labelMinWidth; - } - }, - - uninit() { - }, - - // Check if name string as to if the form can be submitted - checkForm() { - const name = document.getElementById("name"); - let btnApplyChanges = document.getElementById("btnApplyChanges"); - if (!name.value) { - btnApplyChanges.setAttribute("disabled", true); - } else { - btnApplyChanges.removeAttribute("disabled"); - } - }, - - createIconButtons(defaultIcon) { - let radiogroup = document.createElement("radiogroup"); - radiogroup.setAttribute("id", "icon"); - radiogroup.className = "icon-buttons"; - - for (let icon of this.icons) { - let iconSwatch = document.createElement("radio"); - iconSwatch.id = "iconbutton-" + icon; - iconSwatch.name = "icon"; - iconSwatch.type = "radio"; - iconSwatch.value = icon; - - if (this.identity.icon && this.identity.icon == icon) { - iconSwatch.setAttribute("selected", true); - } - - iconSwatch.setAttribute("label", - containersBundle.GetStringFromName(`containers.${icon}.label`)); - let iconElement = document.createElement("hbox"); - iconElement.className = 'userContext-icon'; - iconElement.setAttribute("data-identity-icon", icon); - - iconSwatch.appendChild(iconElement); - radiogroup.appendChild(iconSwatch); - } - - return radiogroup; - }, - - createColorSwatches(defaultColor) { - let radiogroup = document.createElement("radiogroup"); - radiogroup.setAttribute("id", "color"); - - for (let color of this.colors) { - let colorSwatch = document.createElement("radio"); - colorSwatch.id = "colorswatch-" + color; - colorSwatch.name = "color"; - colorSwatch.type = "radio"; - colorSwatch.value = color; - - if (this.identity.color && this.identity.color == color) { - colorSwatch.setAttribute("selected", true); - } - - colorSwatch.setAttribute("label", - containersBundle.GetStringFromName(`containers.${color}.label`)); - let iconElement = document.createElement("hbox"); - iconElement.className = 'userContext-icon'; - iconElement.setAttribute("data-identity-icon", "circle"); - iconElement.setAttribute("data-identity-color", color); - - colorSwatch.appendChild(iconElement); - radiogroup.appendChild(colorSwatch); - } - return radiogroup; - }, - - onApplyChanges() { - let icon = document.getElementById("icon").value; - let color = document.getElementById("color").value; - let name = document.getElementById("name").value; - - if (this.icons.indexOf(icon) == -1) { - throw "Internal error. The icon value doesn't match."; - } - - if (this.colors.indexOf(color) == -1) { - throw "Internal error. The color value doesn't match."; - } - - if (this.userContextId) { - ContextualIdentityService.update(this.userContextId, - name, - icon, - color); - } else { - ContextualIdentityService.create(name, - icon, - color); - } - window.parent.location.reload() - }, - - onWindowKeyPress(aEvent) { - if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE) - window.close(); - } -} diff --git a/application/basilisk/components/preferences/containers.xul b/application/basilisk/components/preferences/containers.xul deleted file mode 100644 index 62a775fe4..000000000 --- a/application/basilisk/components/preferences/containers.xul +++ /dev/null @@ -1,52 +0,0 @@ -<?xml version="1.0"?> - -<!-- This Source Code Form is subject to the terms of the Mozilla Public - - License, v. 2.0. If a copy of the MPL was not distributed with this - - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - -<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> -<?xml-stylesheet href="chrome://browser/skin/preferences/containers.css" type="text/css"?> - -<!DOCTYPE dialog SYSTEM "chrome://browser/locale/preferences/containers.dtd" > - -<window id="ContainersDialog" class="windowDialog" - windowtype="Browser:Permissions" - title="&window.title;" - xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" - style="width: &window.width;;" - onload="gContainersManager.onLoad();" - onunload="gContainersManager.uninit();" - persist="screenX screenY width height" - onkeypress="gContainersManager.onWindowKeyPress(event);"> - - <script src="chrome://global/content/treeUtils.js"/> - <script src="chrome://browser/content/preferences/containers.js"/> - - <stringbundle id="bundlePreferences" - src="chrome://browser/locale/preferences/preferences.properties"/> - - <keyset> - <key key="&windowClose.key;" modifiers="accel" oncommand="window.close();"/> - </keyset> - - <vbox class="contentPane largeDialogContainer" flex="1" hidden="true" id="containers-content"> - <description id="permissionsText" control="url"/> - <separator class="thin"/> - <hbox align="start"> - <label id="nameLabel" control="url" value="&name.label;" accesskey="&name.accesskey;"/> - <textbox id="name" flex="1" onkeyup="gContainersManager.checkForm();" /> - </hbox> - <hbox align="center" id="iconWrapper"> - <label id="iconLabel" control="url" value="&icon.label;" accesskey="&icon.accesskey;"/> - </hbox> - <hbox align="center" id="colorWrapper"> - <label id="colorLabel" control="url" value="&color.label;" accesskey="&color.accesskey;"/> - </hbox> - </vbox> - <vbox> - <hbox class="actionButtons" align="right" flex="1"> - <button id="btnApplyChanges" disabled="true" oncommand="gContainersManager.onApplyChanges();" icon="save" - label="&button.ok.label;" accesskey="&button.ok.accesskey;"/> - </hbox> - </vbox> -</window> diff --git a/application/basilisk/components/preferences/cookies.js b/application/basilisk/components/preferences/cookies.js index c420855f8..4ede5b6e6 100644 --- a/application/basilisk/components/preferences/cookies.js +++ b/application/basilisk/components/preferences/cookies.js @@ -7,12 +7,8 @@ const nsICookie = Components.interfaces.nsICookie; Components.utils.import("resource://gre/modules/AppConstants.jsm"); Components.utils.import("resource://gre/modules/PluralForm.jsm"); -Components.utils.import("resource://gre/modules/Services.jsm") Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "ContextualIdentityService", - "resource://gre/modules/ContextualIdentityService.jsm"); - var gCookiesWindow = { _cm : Components.classes["@mozilla.org/cookiemanager;1"] .getService(Components.interfaces.nsICookieManager), @@ -38,10 +34,6 @@ var gCookiesWindow = { this._populateList(true); document.getElementById("filter").focus(); - - if (!Services.prefs.getBoolPref("privacy.userContext.enabled")) { - document.getElementById("userContextRow").hidden = true; - } }, uninit: function () { @@ -82,24 +74,11 @@ var gCookiesWindow = { aCookieB.originAttributes); }, - _isPrivateCookie: function (aCookie) { - let { userContextId } = aCookie.originAttributes; - if (!userContextId) { - // Default identity is public. - return false; - } - return !ContextualIdentityService.getIdentityFromId(userContextId).public; - }, - observe: function (aCookie, aTopic, aData) { if (aTopic != "cookie-changed") return; if (aCookie instanceof Components.interfaces.nsICookie) { - if (this._isPrivateCookie(aCookie)) { - return; - } - var strippedHost = this._makeStrippedHost(aCookie.host); if (aData == "changed") this._handleCookieChanged(aCookie, strippedHost); @@ -498,9 +477,6 @@ var gCookiesWindow = { while (e.hasMoreElements()) { var cookie = e.getNext(); if (cookie && cookie instanceof Components.interfaces.nsICookie) { - if (this._isPrivateCookie(cookie)) { - continue; - } var strippedHost = this._makeStrippedHost(cookie.host); this._addCookie(strippedHost, cookie, hostCount); @@ -524,17 +500,9 @@ var gCookiesWindow = { return this._bundle.getString("expireAtEndOfSession"); }, - _getUserContextString: function(aUserContextId) { - if (parseInt(aUserContextId) == 0) { - return this._bundle.getString("defaultUserContextLabel"); - } - - return ContextualIdentityService.getUserContextLabel(aUserContextId); - }, - _updateCookieData: function (aItem) { var seln = this._view.selection; - var ids = ["name", "value", "host", "path", "isSecure", "expires", "userContext"]; + var ids = ["name", "value", "host", "path", "isSecure", "expires"]; var properties; if (aItem && !aItem.container && seln.count > 0) { @@ -543,8 +511,7 @@ var gCookiesWindow = { isDomain: aItem.isDomain ? this._bundle.getString("domainColon") : this._bundle.getString("hostColon"), isSecure: aItem.isSecure ? this._bundle.getString("forSecureOnly") - : this._bundle.getString("forAnyConnection"), - userContext: this._getUserContextString(aItem.originAttributes.userContextId) }; + : this._bundle.getString("forAnyConnection") }; for (let id of ids) { document.getElementById(id).disabled = false; } @@ -553,7 +520,7 @@ var gCookiesWindow = { var noneSelected = this._bundle.getString("noCookieSelected"); properties = { name: noneSelected, value: noneSelected, host: noneSelected, path: noneSelected, expires: noneSelected, - isSecure: noneSelected, userContext: noneSelected }; + isSecure: noneSelected }; for (let id of ids) { document.getElementById(id).disabled = true; } diff --git a/application/basilisk/components/preferences/cookies.xul b/application/basilisk/components/preferences/cookies.xul index bd60d9346..d5fefdef7 100644 --- a/application/basilisk/components/preferences/cookies.xul +++ b/application/basilisk/components/preferences/cookies.xul @@ -85,10 +85,6 @@ <hbox pack="end"><label id="expiresLabel" control="expires" value="&props.expires.label;"/></hbox> <textbox id="expires" readonly="true" class="plain"/> </row> - <row align="center" id="userContextRow"> - <hbox pack="end"><label id="userContextLabel" control="userContext" value="&props.container.label;"/></hbox> - <textbox id="userContext" readonly="true" class="plain"/> - </row> </rows> </grid> </hbox> diff --git a/application/basilisk/components/preferences/handlers.css b/application/basilisk/components/preferences/handlers.css index 6af75a08b..d5f100831 100644 --- a/application/basilisk/components/preferences/handlers.css +++ b/application/basilisk/components/preferences/handlers.css @@ -10,10 +10,6 @@ -moz-binding: url("chrome://browser/content/preferences/handlers.xml#handler-selected"); } -#containersView > richlistitem { - -moz-binding: url("chrome://browser/content/preferences/handlers.xml#container"); -} - /** * Make the icons appear. * Note: we display the icon box for every item whether or not it has an icon diff --git a/application/basilisk/components/preferences/handlers.xml b/application/basilisk/components/preferences/handlers.xml index 0c629d759..ad07a493c 100644 --- a/application/basilisk/components/preferences/handlers.xml +++ b/application/basilisk/components/preferences/handlers.xml @@ -69,29 +69,6 @@ </binding> - <binding id="container"> - <content> - <xul:hbox flex="1" equalsize="always"> - <xul:hbox flex="1" align="center"> - <xul:hbox xbl:inherits="data-identity-icon=containerIcon,data-identity-color=containerColor" height="24" width="24" class="userContext-icon"/> - <xul:label flex="1" crop="end" xbl:inherits="value=containerName"/> - </xul:hbox> - <xul:hbox flex="1" align="right"> - <xul:button anonid="preferencesButton" - xbl:inherits="value=userContextId" - onclick="gContainersPane.onPeferenceClick(event.originalTarget)"> - Preferences - </xul:button> - <xul:button anonid="removeButton" - xbl:inherits="value=userContextId" - onclick="gContainersPane.onRemoveClick(event.originalTarget)"> - Remove - </xul:button> - </xul:hbox> - </xul:hbox> - </content> - </binding> - <binding id="offlineapp" extends="chrome://global/content/bindings/listbox.xml#listitem"> <content> diff --git a/application/basilisk/components/preferences/in-content/containers.js b/application/basilisk/components/preferences/in-content/containers.js deleted file mode 100644 index 758e45fff..000000000 --- a/application/basilisk/components/preferences/in-content/containers.js +++ /dev/null @@ -1,73 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -Components.utils.import("resource://gre/modules/AppConstants.jsm"); -Components.utils.import("resource://gre/modules/ContextualIdentityService.jsm"); - -const containersBundle = Services.strings.createBundle("chrome://browser/locale/preferences/containers.properties"); - -const defaultContainerIcon = "fingerprint"; -const defaultContainerColor = "blue"; - -let gContainersPane = { - - init() { - this._list = document.getElementById("containersView"); - - document.getElementById("backContainersLink").addEventListener("click", function () { - gotoPref("privacy"); - }); - - this._rebuildView(); - }, - - _rebuildView() { - const containers = ContextualIdentityService.getIdentities(); - while (this._list.firstChild) { - this._list.firstChild.remove(); - } - for (let container of containers) { - let item = document.createElement("richlistitem"); - item.setAttribute("containerName", ContextualIdentityService.getUserContextLabel(container.userContextId)); - item.setAttribute("containerIcon", container.icon); - item.setAttribute("containerColor", container.color); - item.setAttribute("userContextId", container.userContextId); - - this._list.appendChild(item); - } - }, - - onRemoveClick(button) { - let userContextId = button.getAttribute("value"); - ContextualIdentityService.remove(userContextId); - this._rebuildView(); - }, - onPeferenceClick(button) { - this.openPreferenceDialog(button.getAttribute("value")); - }, - - onAddButtonClick(button) { - this.openPreferenceDialog(null); - }, - - openPreferenceDialog(userContextId) { - let identity = { - name: "", - icon: defaultContainerIcon, - color: defaultContainerColor - }; - let title; - if (userContextId) { - identity = ContextualIdentityService.getIdentityFromId(userContextId); - // This is required to get the translation string from defaults - identity.name = ContextualIdentityService.getUserContextLabel(identity.userContextId); - title = containersBundle.formatStringFromName("containers.updateContainerTitle", [identity.name], 1); - } - - const params = { userContextId, identity, windowTitle: title }; - gSubDialog.open("chrome://browser/content/preferences/containers.xul", - null, params); - } - -}; diff --git a/application/basilisk/components/preferences/in-content/containers.xul b/application/basilisk/components/preferences/in-content/containers.xul deleted file mode 100644 index e83bac1c3..000000000 --- a/application/basilisk/components/preferences/in-content/containers.xul +++ /dev/null @@ -1,54 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -<!-- Containers panel --> - -<script type="application/javascript" - src="chrome://browser/content/preferences/in-content/containers.js"/> - -<preferences id="containerPreferences" hidden="true" data-category="paneContainer"> - <!-- Containers --> - <preference id="privacy.userContext.enabled" - name="privacy.userContext.enabled" - type="bool"/> - -</preferences> - -<hbox hidden="true" - class="container-header-links" - data-category="paneContainers"> - <label class="text-link" id="backContainersLink" value="&backLink.label;" /> -</hbox> - -<hbox id="header-containers" - class="header" - hidden="true" - data-category="paneContainers"> - <label class="header-name" flex="1">&paneContainers.title;</label> - <button class="help-button" - aria-label="&helpButton.label;"/> -</hbox> - -<!-- Containers --> -<groupbox id="browserContainersGroup" data-category="paneContainers" hidden="true"> - <vbox id="browserContainersbox"> - - <richlistbox id="containersView" orient="vertical" persist="lastSelectedType" - flex="1"> - <listheader equalsize="always"> - <treecol id="typeColumn" label="&label.label;" value="type" - persist="sortDirection" - flex="1" sortDirection="ascending"/> - <treecol id="actionColumn" value="action" - persist="sortDirection" - flex="1"/> - </listheader> - </richlistbox> - </vbox> - <vbox> - <hbox flex="1"> - <button onclick="gContainersPane.onAddButtonClick();" accesskey="&addButton.accesskey;" label="&addButton.label;"/> - </hbox> - </vbox> -</groupbox> diff --git a/application/basilisk/components/preferences/in-content/jar.mn b/application/basilisk/components/preferences/in-content/jar.mn index e61a88856..21f5ccf38 100644 --- a/application/basilisk/components/preferences/in-content/jar.mn +++ b/application/basilisk/components/preferences/in-content/jar.mn @@ -3,16 +3,17 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. browser.jar: - content/browser/preferences/in-content/preferences.js +* content/browser/preferences/in-content/preferences.js * content/browser/preferences/in-content/preferences.xul content/browser/preferences/in-content/subdialogs.js content/browser/preferences/in-content/main.js * content/browser/preferences/in-content/privacy.js - content/browser/preferences/in-content/containers.js content/browser/preferences/in-content/advanced.js content/browser/preferences/in-content/applications.js * content/browser/preferences/in-content/content.js +#ifdef MOZ_SERVICES_SYNC content/browser/preferences/in-content/sync.js +#endif * content/browser/preferences/in-content/security.js content/browser/preferences/in-content/search.js diff --git a/application/basilisk/components/preferences/in-content/preferences.js b/application/basilisk/components/preferences/in-content/preferences.js index e18ab4b04..69cb180d5 100644 --- a/application/basilisk/components/preferences/in-content/preferences.js +++ b/application/basilisk/components/preferences/in-content/preferences.js @@ -61,11 +61,12 @@ function init_all() { register_module("paneGeneral", gMainPane); register_module("paneSearch", gSearchPane); register_module("panePrivacy", gPrivacyPane); - register_module("paneContainers", gContainersPane); register_module("paneAdvanced", gAdvancedPane); register_module("paneApplications", gApplicationsPane); register_module("paneContent", gContentPane); +#ifdef MOZ_SERVICES_SYNC register_module("paneSync", gSyncPane); +#endif register_module("paneSecurity", gSecurityPane); let categories = document.getElementById("categories"); diff --git a/application/basilisk/components/preferences/in-content/preferences.xul b/application/basilisk/components/preferences/in-content/preferences.xul index 7ec7ef119..61639aa19 100644 --- a/application/basilisk/components/preferences/in-content/preferences.xul +++ b/application/basilisk/components/preferences/in-content/preferences.xul @@ -13,7 +13,6 @@ href="chrome://browser/content/preferences/handlers.css"?> <?xml-stylesheet href="chrome://browser/skin/preferences/applications.css"?> <?xml-stylesheet href="chrome://browser/skin/preferences/in-content/search.css"?> -<?xml-stylesheet href="chrome://browser/skin/preferences/in-content/containers.css"?> <!DOCTYPE page [ <!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> @@ -23,12 +22,12 @@ <!ENTITY % privacyDTD SYSTEM "chrome://browser/locale/preferences/privacy.dtd"> <!ENTITY % tabsDTD SYSTEM "chrome://browser/locale/preferences/tabs.dtd"> <!ENTITY % searchDTD SYSTEM "chrome://browser/locale/preferences/search.dtd"> +#ifdef MOZ_SERVICES_SYNC <!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> <!ENTITY % syncDTD SYSTEM "chrome://browser/locale/preferences/sync.dtd"> +#endif <!ENTITY % securityDTD SYSTEM "chrome://browser/locale/preferences/security.dtd"> -<!ENTITY % containersDTD SYSTEM - "chrome://browser/locale/preferences/containers.dtd"> <!ENTITY % sanitizeDTD SYSTEM "chrome://browser/locale/sanitize.dtd"> <!ENTITY % mainDTD SYSTEM "chrome://browser/locale/preferences/main.dtd"> <!ENTITY % aboutHomeDTD SYSTEM "chrome://browser/locale/aboutHome.dtd"> @@ -43,10 +42,11 @@ %privacyDTD; %tabsDTD; %searchDTD; +#ifdef MOZ_SERVICES_SYNC %syncBrandDTD; %syncDTD; +#endif %securityDTD; -%containersDTD; %sanitizeDTD; %mainDTD; %aboutHomeDTD; @@ -134,12 +134,6 @@ <label class="category-name" flex="1">&panePrivacy.title;</label> </richlistitem> - <richlistitem id="category-containers" - class="category" - value="paneContainers" - helpTopic="prefs-containers" - hidden="true"/> - <richlistitem id="category-security" class="category" value="paneSecurity" @@ -150,6 +144,7 @@ <label class="category-name" flex="1">&paneSecurity.title;</label> </richlistitem> +#ifdef MOZ_SERVICES_SYNC <richlistitem id="category-sync" class="category" value="paneSync" @@ -159,6 +154,7 @@ <image class="category-icon"/> <label class="category-name" flex="1">&paneSync.title;</label> </richlistitem> +#endif <richlistitem id="category-advanced" class="category" @@ -183,12 +179,13 @@ #include main.xul #include search.xul #include privacy.xul -#include containers.xul #include advanced.xul #include applications.xul #include content.xul #include security.xul +#ifdef MOZ_SERVICES_SYNC #include sync.xul +#endif </prefpane> </vbox> diff --git a/application/basilisk/components/preferences/in-content/privacy.js b/application/basilisk/components/preferences/in-content/privacy.js index eab606e36..a976fb4fa 100644 --- a/application/basilisk/components/preferences/in-content/privacy.js +++ b/application/basilisk/components/preferences/in-content/privacy.js @@ -5,8 +5,6 @@ Components.utils.import("resource://gre/modules/AppConstants.jsm"); Components.utils.import("resource://gre/modules/PluralForm.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "ContextualIdentityService", - "resource://gre/modules/ContextualIdentityService.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", "resource://gre/modules/PluralForm.jsm"); @@ -62,59 +60,6 @@ var gPrivacyPane = { }, /** - * Show the Containers UI depending on the privacy.userContext.ui.enabled pref. - */ - _initBrowserContainers: function () { - if (!Services.prefs.getBoolPref("privacy.userContext.ui.enabled")) { - return; - } - - let link = document.getElementById("browserContainersLearnMore"); - link.href = Services.urlFormatter.formatURLPref("app.support.baseURL") + "containers"; - - document.getElementById("browserContainersbox").hidden = false; - - document.getElementById("browserContainersCheckbox").checked = - Services.prefs.getBoolPref("privacy.userContext.enabled"); - }, - - _checkBrowserContainers: function(event) { - let checkbox = document.getElementById("browserContainersCheckbox"); - if (checkbox.checked) { - Services.prefs.setBoolPref("privacy.userContext.enabled", true); - return; - } - - let count = ContextualIdentityService.countContainerTabs(); - if (count == 0) { - Services.prefs.setBoolPref("privacy.userContext.enabled", false); - return; - } - - let bundlePreferences = document.getElementById("bundlePreferences"); - - let title = bundlePreferences.getString("disableContainersAlertTitle"); - let message = PluralForm.get(count, bundlePreferences.getString("disableContainersMsg")) - .replace("#S", count) - let okButton = PluralForm.get(count, bundlePreferences.getString("disableContainersOkButton")) - .replace("#S", count) - let cancelButton = bundlePreferences.getString("disableContainersButton2"); - - let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) + - (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1); - - let rv = Services.prompt.confirmEx(window, title, message, buttonFlags, - okButton, cancelButton, null, null, {}); - if (rv == 0) { - ContextualIdentityService.closeAllContainerTabs(); - Services.prefs.setBoolPref("privacy.userContext.enabled", false); - return; - } - - checkbox.checked = true; - }, - - /** * Sets up the UI for the number of days of history to keep, and updates the * label of the "Clear Now..." button. */ @@ -136,7 +81,6 @@ var gPrivacyPane = { this._initTrackingProtectionPBM(); #endif this._initAutocomplete(); - this._initBrowserContainers(); setEventListener("privacy.sanitize.sanitizeOnShutdown", "change", gPrivacyPane._updateSanitizeSettingsButton); @@ -184,10 +128,6 @@ var gPrivacyPane = { setEventListener("changeBlockListPBM", "command", gPrivacyPane.showBlockLists); #endif - setEventListener("browserContainersCheckbox", "command", - gPrivacyPane._checkBrowserContainers); - setEventListener("browserContainersSettings", "command", - gPrivacyPane.showContainerSettings); }, #ifdef MOZ_SAFE_BROWSING @@ -489,13 +429,6 @@ var gPrivacyPane = { }, #endif - /** - * Displays container panel for customising and adding containers. - */ - showContainerSettings() { - gotoPref("containers"); - }, - #ifdef MOZ_SAFE_BROWSING /** * Displays the available block lists for tracking protection. @@ -702,25 +635,4 @@ var gPrivacyPane = { settingsButton.disabled = !sanitizeOnShutdownPref.value; }, - - // CONTAINERS - - /* - * preferences: - * - * privacy.userContext.enabled - * - true if containers is enabled - */ - - /** - * Enables/disables the Settings button used to configure containers - */ - readBrowserContainersCheckbox: function () - { - var pref = document.getElementById("privacy.userContext.enabled"); - var settings = document.getElementById("browserContainersSettings"); - - settings.disabled = !pref.value; - } - }; diff --git a/application/basilisk/components/preferences/in-content/privacy.xul b/application/basilisk/components/preferences/in-content/privacy.xul index e6cdc5dd2..691cd6bf9 100644 --- a/application/basilisk/components/preferences/in-content/privacy.xul +++ b/application/basilisk/components/preferences/in-content/privacy.xul @@ -302,28 +302,3 @@ &suggestionSettings.label; </label> </groupbox> - -<!-- Containers --> -<groupbox id="browserContainersGroup" data-category="panePrivacy" hidden="true"> - <vbox id="browserContainersbox" hidden="true"> - <caption><label>&browserContainersHeader.label; - <label id="browserContainersLearnMore" class="text-link" - value="&browserContainersLearnMore.label;"/> - </label></caption> - <hbox align="start"> - <vbox> - <checkbox id="browserContainersCheckbox" - label="&browserContainersEnabled.label;" - accesskey="&browserContainersEnabled.accesskey;" - preference="privacy.userContext.enabled" - onsyncfrompreference="return gPrivacyPane.readBrowserContainersCheckbox();"/> - </vbox> - <spacer flex="1"/> - <vbox> - <button id="browserContainersSettings" - label="&browserContainersSettings.label;" - accesskey="&browserContainersSettings.accesskey;"/> - </vbox> - </hbox> - </vbox> -</groupbox> diff --git a/application/basilisk/components/preferences/in-content/sync.js b/application/basilisk/components/preferences/in-content/sync.js index 2600677a8..917b5f123 100644 --- a/application/basilisk/components/preferences/in-content/sync.js +++ b/application/basilisk/components/preferences/in-content/sync.js @@ -1,32 +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/. */ + * 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/. */ Components.utils.import("resource://services-sync/main.js"); Components.utils.import("resource://gre/modules/Services.jsm"); -XPCOMUtils.defineLazyGetter(this, "FxAccountsCommon", function () { - return Components.utils.import("resource://gre/modules/FxAccountsCommon.js", {}); -}); - -XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", - "resource://gre/modules/FxAccounts.jsm"); - const PAGE_NO_ACCOUNT = 0; const PAGE_HAS_ACCOUNT = 1; const PAGE_NEEDS_UPDATE = 2; -const FXA_PAGE_LOGGED_OUT = 3; -const FXA_PAGE_LOGGED_IN = 4; - -// Indexes into the "login status" deck. -// We are in a successful verified state - everything should work! -const FXA_LOGIN_VERIFIED = 0; -// We have logged in to an unverified account. -const FXA_LOGIN_UNVERIFIED = 1; -// We are logged in locally, but the server rejected our credentials. -const FXA_LOGIN_FAILED = 2; var gSyncPane = { + _stringBundle: null, prefArray: ["engine.bookmarks", "engine.passwords", "engine.prefs", "engine.tabs", "engine.history"], @@ -45,13 +29,11 @@ var gSyncPane = { needsUpdate: function () { this.page = PAGE_NEEDS_UPDATE; let label = document.getElementById("loginError"); - label.textContent = Weave.Utils.getErrorString(Weave.Status.login); + label.value = Weave.Utils.getErrorString(Weave.Status.login); label.className = "error"; }, init: function () { - this._setupEventListeners(); - // If the Service hasn't finished initializing, wait for it. let xps = Components.classes["@mozilla.org/weave/service;1"] .getService(Components.interfaces.nsISupports) @@ -62,10 +44,6 @@ var gSyncPane = { return; } - // it may take some time before we can determine what provider to use - // and the state of that provider, so show the "please wait" page. - this._showLoadPage(xps); - let onUnload = function () { window.removeEventListener("unload", onUnload, false); try { @@ -85,247 +63,50 @@ var gSyncPane = { xps.ensureLoaded(); }, - _showLoadPage: function (xps) { - let username; - try { - username = Services.prefs.getCharPref("services.sync.username"); - } catch (e) {} - if (!username) { - this.page = FXA_PAGE_LOGGED_OUT; - } else if (xps.fxAccountsEnabled) { - // Use cached values while we wait for the up-to-date values - let cachedComputerName; - try { - cachedComputerName = Services.prefs.getCharPref("services.sync.client.name"); - } - catch (e) { - cachedComputerName = ""; - } - document.getElementById("fxaEmailAddress1").textContent = username; - this._populateComputerName(cachedComputerName); - this.page = FXA_PAGE_LOGGED_IN; - } else { // Old Sync - this.page = PAGE_HAS_ACCOUNT; - } - }, - _init: function () { let topics = ["weave:service:login:error", "weave:service:login:finish", - "weave:service:start-over:finish", + "weave:service:start-over", "weave:service:setup-complete", - "weave:service:logout:finish", - FxAccountsCommon.ONVERIFIED_NOTIFICATION, - FxAccountsCommon.ONLOGIN_NOTIFICATION, - FxAccountsCommon.ON_PROFILE_CHANGE_NOTIFICATION, - ]; + "weave:service:logout:finish"]; + // Add the observers now and remove them on unload - // XXXzpao This should use Services.obs.* but Weave's Obs does nice handling + //XXXzpao This should use Services.obs.* but Weave's Obs does nice handling // of `this`. Fix in a followup. (bug 583347) topics.forEach(function (topic) { Weave.Svc.Obs.add(topic, this.updateWeavePrefs, this); }, this); - window.addEventListener("unload", function() { topics.forEach(function (topic) { Weave.Svc.Obs.remove(topic, this.updateWeavePrefs, this); }, gSyncPane); }, false); - XPCOMUtils.defineLazyGetter(this, '_stringBundle', () => { - return Services.strings.createBundle("chrome://browser/locale/preferences/preferences.properties"); - }); - - XPCOMUtils.defineLazyGetter(this, '_accountsStringBundle', () => { - return Services.strings.createBundle("chrome://browser/locale/accounts.properties"); - }); - - let url = Services.prefs.getCharPref("identity.mobilepromo.android") + "sync-preferences"; - document.getElementById("fxaMobilePromo-android").setAttribute("href", url); - document.getElementById("fxaMobilePromo-android-hasFxaAccount").setAttribute("href", url); - url = Services.prefs.getCharPref("identity.mobilepromo.ios") + "sync-preferences"; - document.getElementById("fxaMobilePromo-ios").setAttribute("href", url); - document.getElementById("fxaMobilePromo-ios-hasFxaAccount").setAttribute("href", url); - - document.getElementById("tosPP-small-ToS").setAttribute("href", gSyncUtils.tosURL); - document.getElementById("tosPP-normal-ToS").setAttribute("href", gSyncUtils.tosURL); - document.getElementById("tosPP-small-PP").setAttribute("href", gSyncUtils.privacyPolicyURL); - document.getElementById("tosPP-normal-PP").setAttribute("href", gSyncUtils.privacyPolicyURL); - - fxAccounts.promiseAccountsManageURI(this._getEntryPoint()).then(url => { - document.getElementById("verifiedManage").setAttribute("href", url); - }); - + this._stringBundle = + Services.strings.createBundle("chrome://browser/locale/preferences/preferences.properties"); this.updateWeavePrefs(); - - this._initProfileImageUI(); - }, - - _toggleComputerNameControls: function(editMode) { - let textbox = document.getElementById("fxaSyncComputerName"); - textbox.disabled = !editMode; - document.getElementById("fxaChangeDeviceName").hidden = editMode; - document.getElementById("fxaCancelChangeDeviceName").hidden = !editMode; - document.getElementById("fxaSaveChangeDeviceName").hidden = !editMode; - }, - - _focusComputerNameTextbox: function() { - let textbox = document.getElementById("fxaSyncComputerName"); - let valLength = textbox.value.length; - textbox.focus(); - textbox.setSelectionRange(valLength, valLength); - }, - - _blurComputerNameTextbox: function() { - document.getElementById("fxaSyncComputerName").blur(); - }, - - _focusAfterComputerNameTextbox: function() { - // Focus the most appropriate element that's *not* the "computer name" box. - Services.focus.moveFocus(window, - document.getElementById("fxaSyncComputerName"), - Services.focus.MOVEFOCUS_FORWARD, 0); - }, - - _updateComputerNameValue: function(save) { - if (save) { - let textbox = document.getElementById("fxaSyncComputerName"); - Weave.Service.clientsEngine.localName = textbox.value; - } - this._populateComputerName(Weave.Service.clientsEngine.localName); - }, - - _setupEventListeners: function() { - function setEventListener(aId, aEventType, aCallback) - { - document.getElementById(aId) - .addEventListener(aEventType, aCallback.bind(gSyncPane)); - } - - setEventListener("noAccountSetup", "click", function (aEvent) { - aEvent.stopPropagation(); - gSyncPane.openSetup(null); - }); - setEventListener("noAccountPair", "click", function (aEvent) { - aEvent.stopPropagation(); - gSyncPane.openSetup('pair'); - }); - setEventListener("syncChangePassword", "command", - () => gSyncUtils.changePassword()); - setEventListener("syncResetPassphrase", "command", - () => gSyncUtils.resetPassphrase()); - setEventListener("syncReset", "command", gSyncPane.resetSync); - setEventListener("syncAddDeviceLabel", "click", function () { - gSyncPane.openAddDevice(); - return false; - }); - setEventListener("syncEnginesList", "select", function () { - if (this.selectedCount) - this.clearSelection(); - }); - setEventListener("syncComputerName", "change", function (e) { - gSyncUtils.changeName(e.target); - }); - setEventListener("fxaChangeDeviceName", "command", function () { - this._toggleComputerNameControls(true); - this._focusComputerNameTextbox(); - }); - setEventListener("fxaCancelChangeDeviceName", "command", function () { - // We explicitly blur the textbox because of bug 75324, then after - // changing the state of the buttons, force focus to whatever the focus - // manager thinks should be next (which on the mac, depends on an OSX - // keyboard access preference) - this._blurComputerNameTextbox(); - this._toggleComputerNameControls(false); - this._updateComputerNameValue(false); - this._focusAfterComputerNameTextbox(); - }); - setEventListener("fxaSaveChangeDeviceName", "command", function () { - // Work around bug 75324 - see above. - this._blurComputerNameTextbox(); - this._toggleComputerNameControls(false); - this._updateComputerNameValue(true); - this._focusAfterComputerNameTextbox(); - }); - setEventListener("unlinkDevice", "click", function () { - gSyncPane.startOver(true); - return false; - }); - setEventListener("loginErrorUpdatePass", "click", function () { - gSyncPane.updatePass(); - return false; - }); - setEventListener("loginErrorResetPass", "click", function () { - gSyncPane.resetPass(); - return false; - }); - setEventListener("loginErrorStartOver", "click", function () { - gSyncPane.startOver(true); - return false; - }); - setEventListener("noFxaSignUp", "command", function () { - gSyncPane.signUp(); - return false; - }); - setEventListener("noFxaSignIn", "command", function () { - gSyncPane.signIn(); - return false; - }); - setEventListener("fxaUnlinkButton", "command", function () { - gSyncPane.unlinkFirefoxAccount(true); - }); - setEventListener("verifyFxaAccount", "command", - gSyncPane.verifyFirefoxAccount); - setEventListener("unverifiedUnlinkFxaAccount", "command", function () { - /* no warning as account can't have previously synced */ - gSyncPane.unlinkFirefoxAccount(false); - }); - setEventListener("rejectReSignIn", "command", - gSyncPane.reSignIn); - setEventListener("rejectUnlinkFxaAccount", "command", function () { - gSyncPane.unlinkFirefoxAccount(true); - }); - setEventListener("fxaSyncComputerName", "keypress", function (e) { - if (e.keyCode == KeyEvent.DOM_VK_RETURN) { - document.getElementById("fxaSaveChangeDeviceName").click(); - } else if (e.keyCode == KeyEvent.DOM_VK_ESCAPE) { - document.getElementById("fxaCancelChangeDeviceName").click(); - } - }); - }, - - _initProfileImageUI: function () { - try { - if (Services.prefs.getBoolPref("identity.fxaccounts.profile_image.enabled")) { - document.getElementById("fxaProfileImage").hidden = false; - } - } catch (e) { } + document.getElementById("weavePrefsDeck").setAttribute("hidden", ""); }, updateWeavePrefs: function () { - let service = Components.classes["@mozilla.org/weave/service;1"] - .getService(Components.interfaces.nsISupports) - .wrappedJSObject; if (Weave.Status.service == Weave.CLIENT_NOT_CONFIGURED || Weave.Svc.Prefs.get("firstSync", "") == "notReady") { this.page = PAGE_NO_ACCOUNT; - // else: sync was previously configured for the legacy provider, so we - // make the "old" panels available. } else if (Weave.Status.login == Weave.LOGIN_FAILED_INVALID_PASSPHRASE || Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) { this.needsUpdate(); } else { this.page = PAGE_HAS_ACCOUNT; - document.getElementById("accountName").textContent = Weave.Service.identity.account; + document.getElementById("accountName").value = Weave.Service.identity.account; document.getElementById("syncComputerName").value = Weave.Service.clientsEngine.localName; - document.getElementById("tosPP-normal").hidden = this._usingCustomServer; + document.getElementById("tosPP").hidden = this._usingCustomServer; } }, startOver: function (showDialog) { if (showDialog) { let flags = Services.prompt.BUTTON_POS_0 * Services.prompt.BUTTON_TITLE_IS_STRING + - Services.prompt.BUTTON_POS_1 * Services.prompt.BUTTON_TITLE_CANCEL + + Services.prompt.BUTTON_POS_1 * Services.prompt.BUTTON_TITLE_CANCEL + Services.prompt.BUTTON_POS_1_DEFAULT; let buttonChoice = Services.prompt.confirmEx(window, @@ -336,8 +117,9 @@ var gSyncPane = { null, null, null, {}); // If the user selects cancel, just bail - if (buttonChoice == 1) + if (buttonChoice == 1) { return; + } } Weave.Service.startOver(); @@ -345,33 +127,19 @@ var gSyncPane = { }, updatePass: function () { - if (Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) + if (Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) { gSyncUtils.changePassword(); - else + } else { gSyncUtils.updatePassphrase(); + } }, resetPass: function () { - if (Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) + if (Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) { gSyncUtils.resetPassword(); - else + } else { gSyncUtils.resetPassphrase(); - }, - - _getEntryPoint: function () { - let params = new URLSearchParams(document.URL.split("#")[0].split("?")[1] || ""); - return params.get("entrypoint") || "preferences"; - }, - - _openAboutAccounts: function(action) { - let entryPoint = this._getEntryPoint(); - let params = new URLSearchParams(); - if (action) { - params.set("action", action); } - params.set("entrypoint", entryPoint); - - this.replaceTabWithUrl("about:accounts?" + params); }, /** @@ -384,163 +152,16 @@ var gSyncPane = { * "reset" -- reset sync */ openSetup: function (wizardType) { - let service = Components.classes["@mozilla.org/weave/service;1"] - .getService(Components.interfaces.nsISupports) - .wrappedJSObject; - - if (service.fxAccountsEnabled) { - this._openAboutAccounts(); + let win = Services.wm.getMostRecentWindow("Weave:AccountSetup"); + if (win) { + win.focus(); } else { - let win = Services.wm.getMostRecentWindow("Weave:AccountSetup"); - if (win) - win.focus(); - else { - window.openDialog("chrome://browser/content/sync/setup.xul", - "weaveSetup", "centerscreen,chrome,resizable=no", - wizardType); - } - } - }, - - openContentInBrowser: function(url, options) { - let win = Services.wm.getMostRecentWindow("navigator:browser"); - if (!win) { - // no window to use, so use _openLink to create a new one. We don't - // always use that as it prefers to open a new window rather than use - // an existing one. - gSyncUtils._openLink(url); - return; - } - win.switchToTabHavingURI(url, true, options); - }, - - // Replace the current tab with the specified URL. - replaceTabWithUrl(url) { - // Get the <browser> element hosting us. - let browser = window.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIWebNavigation) - .QueryInterface(Ci.nsIDocShell) - .chromeEventHandler; - // And tell it to load our URL. - browser.loadURI(url); - }, - - signUp: function() { - this._openAboutAccounts("signup"); - }, - - signIn: function() { - this._openAboutAccounts("signin"); - }, - - reSignIn: function() { - this._openAboutAccounts("reauth"); - }, - - - clickOrSpaceOrEnterPressed: function(event) { - // Note: charCode is deprecated, but 'char' not yet implemented. - // Replace charCode with char when implemented, see Bug 680830 - return ((event.type == "click" && event.button == 0) || - (event.type == "keypress" && - (event.charCode == KeyEvent.DOM_VK_SPACE || event.keyCode == KeyEvent.DOM_VK_RETURN))); - }, - - openChangeProfileImage: function(event) { - if (this.clickOrSpaceOrEnterPressed(event)) { - fxAccounts.promiseAccountsChangeProfileURI(this._getEntryPoint(), "avatar") - .then(url => { - this.openContentInBrowser(url, { - replaceQueryString: true - }); - }); - // Prevent page from scrolling on the space key. - event.preventDefault(); + window.openDialog("chrome://browser/content/sync/setup.xul", + "weaveSetup", "centerscreen,chrome,resizable=no", + wizardType); } }, - openManageFirefoxAccount: function(event) { - if (this.clickOrSpaceOrEnterPressed(event)) { - this.manageFirefoxAccount(); - // Prevent page from scrolling on the space key. - event.preventDefault(); - } - }, - - manageFirefoxAccount: function() { - fxAccounts.promiseAccountsManageURI(this._getEntryPoint()) - .then(url => { - this.openContentInBrowser(url, { - replaceQueryString: true - }); - }); - }, - - verifyFirefoxAccount: function() { - let showVerifyNotification = (data) => { - let isError = !data; - let maybeNot = isError ? "Not" : ""; - let sb = this._accountsStringBundle; - let title = sb.GetStringFromName("verification" + maybeNot + "SentTitle"); - let email = !isError && data ? data.email : ""; - let body = sb.formatStringFromName("verification" + maybeNot + "SentBody", [email], 1); - new Notification(title, { body }) - } - - let onError = () => { - showVerifyNotification(); - }; - - let onSuccess = data => { - if (data) { - showVerifyNotification(data); - } else { - onError(); - } - }; - - fxAccounts.resendVerificationEmail() - .then(fxAccounts.getSignedInUser, onError) - .then(onSuccess, onError); - }, - - openOldSyncSupportPage: function() { - let url = Services.urlFormatter.formatURLPref("app.support.baseURL") + "old-sync"; - this.openContentInBrowser(url); - }, - - unlinkFirefoxAccount: function(confirm) { - if (confirm) { - // We use a string bundle shared with aboutAccounts. - let sb = Services.strings.createBundle("chrome://browser/locale/syncSetup.properties"); - let disconnectLabel = sb.GetStringFromName("disconnect.label"); - let title = sb.GetStringFromName("disconnect.verify.title"); - let body = sb.GetStringFromName("disconnect.verify.bodyHeading") + - "\n\n" + - sb.GetStringFromName("disconnect.verify.bodyText"); - let ps = Services.prompt; - let buttonFlags = (ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING) + - (ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL) + - ps.BUTTON_POS_1_DEFAULT; - - let factory = Cc["@mozilla.org/prompter;1"] - .getService(Ci.nsIPromptFactory); - let prompt = factory.getPrompt(window, Ci.nsIPrompt); - let bag = prompt.QueryInterface(Ci.nsIWritablePropertyBag2); - bag.setPropertyAsBool("allowTabModal", true); - - let pressed = prompt.confirmEx(title, body, buttonFlags, - disconnectLabel, null, null, null, {}); - - if (pressed != 0) { // 0 is the "continue" button - return; - } - } - fxAccounts.signOut().then(() => { - this.updateWeavePrefs(); - }); - }, - openQuotaDialog: function () { let win = Services.wm.getMostRecentWindow("Sync:ViewQuota"); if (win) { @@ -552,27 +173,21 @@ var gSyncPane = { }, openAddDevice: function () { - if (!Weave.Utils.ensureMPUnlocked()) + if (!Weave.Utils.ensureMPUnlocked()) { return; + } let win = Services.wm.getMostRecentWindow("Sync:AddDevice"); - if (win) + if (win) { win.focus(); - else + } else { window.openDialog("chrome://browser/content/sync/addDevice.xul", "syncAddDevice", "centerscreen,chrome,resizable=no"); + } }, resetSync: function () { this.openSetup("reset"); }, - - _populateComputerName(value) { - let textbox = document.getElementById("fxaSyncComputerName"); - if (!textbox.hasAttribute("placeholder")) { - textbox.setAttribute("placeholder", - Weave.Utils.getDefaultDeviceName()); - } - textbox.value = value; - }, }; + diff --git a/application/basilisk/components/preferences/in-content/sync.xul b/application/basilisk/components/preferences/in-content/sync.xul index cf0e81ca1..7ca075483 100644 --- a/application/basilisk/components/preferences/in-content/sync.xul +++ b/application/basilisk/components/preferences/in-content/sync.xul @@ -1,35 +1,22 @@ -# 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/. - -<!-- Sync panel --> - -<preferences id="syncEnginePrefs" hidden="true" data-category="paneSync"> - <preference id="engine.addons" - name="services.sync.engine.addons" - type="bool"/> - <preference id="engine.bookmarks" - name="services.sync.engine.bookmarks" - type="bool"/> - <preference id="engine.history" - name="services.sync.engine.history" - type="bool"/> - <preference id="engine.tabs" - name="services.sync.engine.tabs" - type="bool"/> - <preference id="engine.prefs" - name="services.sync.engine.prefs" - type="bool"/> - <preference id="engine.passwords" - name="services.sync.engine.passwords" - type="bool"/> -</preferences> +<!-- 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/. --> <script type="application/javascript" src="chrome://browser/content/preferences/in-content/sync.js"/> <script type="application/javascript" src="chrome://browser/content/sync/utils.js"/> +<preferences> +<!-- <preference id="engine.addons" name="services.sync.engine.addons" type="bool"/> --> + <preference id="engine.bookmarks" name="services.sync.engine.bookmarks" type="bool"/> + <preference id="engine.history" name="services.sync.engine.history" type="bool"/> + <preference id="engine.tabs" name="services.sync.engine.tabs" type="bool"/> + <preference id="engine.prefs" name="services.sync.engine.prefs" type="bool"/> + <preference id="engine.passwords" name="services.sync.engine.passwords" type="bool"/> +</preferences> + + <hbox id="header-sync" class="header" hidden="true" @@ -39,22 +26,19 @@ </hbox> <deck id="weavePrefsDeck" data-category="paneSync" hidden="true"> - <!-- These panels are for the "legacy" sync provider --> <vbox id="noAccount" align="center"> <spacer flex="1"/> <description id="syncDesc"> &weaveDesc.label; </description> <separator/> - <label id="noAccountSetup" class="text-link"> - &setupButton.label; - </label> - <vbox id="pairDevice"> - <separator/> - <label id="noAccountPair" class="text-link"> - &pairDevice.label; - </label> - </vbox> + <label class="text-link" + onclick="event.stopPropagation(); gSyncPane.openSetup(null);" + value="&setupButton.label;"/> + <separator/> + <label class="text-link" + onclick="event.stopPropagation(); gSyncPane.openSetup('pair');" + value="&pairDevice.label;"/> <spacer flex="3"/> </vbox> @@ -63,7 +47,7 @@ <!-- label is set to account name --> <caption id="accountCaption" align="center"> <image id="accountCaptionImage"/> - <label id="accountName"/> + <label id="accountName" value=""/> </caption> <hbox> @@ -71,28 +55,39 @@ label="&manageAccount.label;" accesskey="&manageAccount.accesskey;"> <menupopup> - <menuitem id="syncViewQuota" label="&viewQuota.label;" + <menuitem label="&viewQuota.label;" oncommand="gSyncPane.openQuotaDialog();"/> - <menuseparator/> - <menuitem id="syncChangePassword" label="&changePassword2.label;"/> - <menuitem id="syncResetPassphrase" label="&myRecoveryKey.label;"/> <menuseparator/> - <menuitem id="syncReset" label="&resetSync2.label;"/> + <menuitem label="&changePassword2.label;" + oncommand="gSyncUtils.changePassword();"/> + <menuitem label="&myRecoveryKey.label;" + oncommand="gSyncUtils.resetPassphrase();"/> + <menuseparator/> + <menuitem label="&resetSync2.label;" + oncommand="gSyncPane.resetSync();"/> </menupopup> </button> </hbox> <hbox> <label id="syncAddDeviceLabel" - class="text-link"> - &pairDevice.label; - </label> + class="text-link" + onclick="gSyncPane.openAddDevice(); return false;" + value="&pairDevice.label;"/> </hbox> <vbox> - <label>&syncMy.label;</label> + <label value="&syncMy.label;" /> <richlistbox id="syncEnginesList" - orient="vertical"> + orient="vertical" + onselect="if (this.selectedCount) this.clearSelection();"> + <!-- + <richlistitem> + <checkbox label="&engine.addons.label;" + accesskey="&engine.addons.accesskey;" + preference="engine.addons"/> + </richlistitem> + --> <richlistitem> <checkbox label="&engine.bookmarks.label;" accesskey="&engine.bookmarks.accesskey;" @@ -130,228 +125,42 @@ </columns> <rows> <row align="center"> - <label control="syncComputerName"> - &syncDeviceName.label; - </label> - <textbox id="syncComputerName"/> + <label value="&syncDeviceName.label;" + accesskey="&syncDeviceName.accesskey;" + control="syncComputerName"/> + <textbox id="syncComputerName" + onchange="gSyncUtils.changeName(this)"/> </row> </rows> </grid> <hbox> - <label id="unlinkDevice" class="text-link"> - &unlinkDevice.label; - </label> + <label class="text-link" + onclick="gSyncPane.startOver(true); return false;" + value="&unlinkDevice.label;"/> </hbox> </groupbox> - <vbox id="tosPP-normal"> - <label id="tosPP-normal-ToS" class="text-link"> - &prefs.tosLink.label; - </label> - <label id="tosPP-normal-PP" class="text-link"> - &prefs.ppLink.label; - </label> - </vbox> - </vbox> - - <vbox id="needsUpdate" align="center" pack="center"> - <hbox> - <label id="loginError"/> - <label id="loginErrorUpdatePass" class="text-link"> - &updatePass.label; - </label> - <label id="loginErrorResetPass" class="text-link"> - &resetPass.label; - </label> + <hbox id="tosPP" pack="center"> + <label class="text-link" + onclick="event.stopPropagation();gSyncUtils.openToS();" + value="&prefs.tosLink.label;"/> + <label class="text-link" + onclick="event.stopPropagation();gSyncUtils.openPrivacyPolicy();" + value="&prefs.ppLink.label;"/> </hbox> - <label id="loginErrorStartOver" class="text-link"> - &unlinkDevice.label; - </label> </vbox> - <!-- These panels are for the Firefox Accounts identity provider --> - <vbox id="noFxaAccount"> - <hbox> - <vbox id="fxaContentWrapper"> - <groupbox id="noFxaGroup"> - <vbox> - <label id="noFxaCaption">&signedOut.caption;</label> - <description id="noFxaDescription" flex="1">&signedOut.description;</description> - <hbox class="fxaAccountBox"> - <vbox> - <image class="fxaFirefoxLogo"/> - </vbox> - <vbox flex="1"> - <label id="signedOutAccountBoxTitle">&signedOut.accountBox.title;</label> - <hbox class="fxaAccountBoxButtons"> - <button id="noFxaSignUp" label="&signedOut.accountBox.create;" accesskey="&signedOut.accountBox.create.accesskey;"></button> - <button id="noFxaSignIn" label="&signedOut.accountBox.signin;" accesskey="&signedOut.accountBox.signin.accesskey;"></button> - </hbox> - </vbox> - </hbox> - </vbox> - </groupbox> - </vbox> - <vbox> - <image class="fxaSyncIllustration"/> - </vbox> - </hbox> - <label class="fxaMobilePromo"> - &mobilePromo3.start;<!-- We put these comments to avoid inserting white spaces - --><label id="fxaMobilePromo-android" - class="androidLink text-link"><!-- - -->&mobilePromo3.androidLink;</label><!-- - -->&mobilePromo3.iOSBefore;<!-- - --><label id="fxaMobilePromo-ios" - class="iOSLink text-link"><!-- - -->&mobilePromo3.iOSLink;</label><!-- - -->&mobilePromo3.end; - </label> - </vbox> - - <vbox id="hasFxaAccount"> + <vbox id="needsUpdate" align="center" pack="center"> <hbox> - <vbox id="fxaContentWrapper"> - <groupbox id="fxaGroup"> - <caption><label>&syncBrand.fxAccount.label;</label></caption> - <deck id="fxaLoginStatus"> - - <!-- logged in and verified and all is good --> - <hbox id="fxaLoginVerified" class="fxaAccountBox"> - <vbox align="center" pack="center"> - <image id="fxaProfileImage" class="actionable" - role="button" - onclick="gSyncPane.openChangeProfileImage(event);" hidden="true" - onkeypress="gSyncPane.openChangeProfileImage(event);" - tooltiptext="&profilePicture.tooltip;"/> - </vbox> - <vbox flex="1" pack="center"> - <label id="fxaDisplayName" hidden="true"/> - <label id="fxaEmailAddress1"/> - <hbox class="fxaAccountBoxButtons"> - <button id="fxaUnlinkButton" label="&disconnect.label;" accesskey="&disconnect.accesskey;"/> - <html:a id="verifiedManage" target="_blank" - accesskey="&verifiedManage.accesskey;" - onkeypress="gSyncPane.openManageFirefoxAccount(event);"><!-- - -->&verifiedManage.label;</html:a> - </hbox> - </vbox> - </hbox> - - <!-- logged in to an unverified account --> - <hbox id="fxaLoginUnverified" class="fxaAccountBox"> - <vbox> - <image id="fxaProfileImage"/> - </vbox> - <vbox flex="1"> - <hbox> - <vbox><image id="fxaLoginRejectedWarning"/></vbox> - <description flex="1"> - &signedInUnverified.beforename.label; - <label id="fxaEmailAddress2"/> - &signedInUnverified.aftername.label; - </description> - </hbox> - <hbox class="fxaAccountBoxButtons"> - <button id="verifyFxaAccount" accesskey="&verify.accesskey;">&verify.label;</button> - <button id="unverifiedUnlinkFxaAccount" accesskey="&forget.accesskey;">&forget.label;</button> - </hbox> - </vbox> - </hbox> - - <!-- logged in locally but server rejected credentials --> - <hbox id="fxaLoginRejected" class="fxaAccountBox"> - <vbox> - <image id="fxaProfileImage"/> - </vbox> - <vbox flex="1"> - <hbox> - <vbox><image id="fxaLoginRejectedWarning"/></vbox> - <description flex="1"> - &signedInLoginFailure.beforename.label; - <label id="fxaEmailAddress3"/> - &signedInLoginFailure.aftername.label; - </description> - </hbox> - <hbox class="fxaAccountBoxButtons"> - <button id="rejectReSignIn" accessky="&signIn.accesskey;">&signIn.label;</button> - <button id="rejectUnlinkFxaAccount" accesskey="&forget.accesskey;">&forget.label;</button> - </hbox> - </vbox> - </hbox> - </deck> - </groupbox> - <groupbox id="syncOptions"> - <caption><label>&signedIn.engines.label;</label></caption> - <hbox id="fxaSyncEngines"> - <vbox align="start" flex="1"> - <checkbox label="&engine.tabs.label;" - accesskey="&engine.tabs.accesskey;" - preference="engine.tabs"/> - <checkbox label="&engine.bookmarks.label;" - accesskey="&engine.bookmarks.accesskey;" - preference="engine.bookmarks"/> - <checkbox label="&engine.passwords.label;" - accesskey="&engine.passwords.accesskey;" - preference="engine.passwords"/> - </vbox> - <vbox align="start" flex="1"> - <checkbox label="&engine.history.label;" - accesskey="&engine.history.accesskey;" - preference="engine.history"/> - <checkbox label="&engine.addons.label;" - accesskey="&engine.addons.accesskey;" - preference="engine.addons"/> - <checkbox label="&engine.prefs.label;" - accesskey="&engine.prefs.accesskey;" - preference="engine.prefs"/> - </vbox> - <spacer/> - </hbox> - </groupbox> - </vbox> - <vbox> - <image class="fxaSyncIllustration"/> - </vbox> + <label id="loginError" value=""/> + <label class="text-link" + onclick="gSyncPane.updatePass(); return false;" + value="&updatePass.label;"/> + <label class="text-link" + onclick="gSyncPane.resetPass(); return false;" + value="&resetPass.label;"/> </hbox> - <groupbox> - <caption> - <label control="fxaSyncComputerName"> - &fxaSyncDeviceName.label; - </label> - </caption> - <hbox id="fxaDeviceName"> - <textbox id="fxaSyncComputerName" disabled="true"/> - <hbox> - <button id="fxaChangeDeviceName" - label="&changeSyncDeviceName.label;" - accesskey="&changeSyncDeviceName.accesskey;"/> - <button id="fxaCancelChangeDeviceName" - label="&cancelChangeSyncDeviceName.label;" - accesskey="&cancelChangeSyncDeviceName.accesskey;" - hidden="true"/> - <button id="fxaSaveChangeDeviceName" - label="&saveChangeSyncDeviceName.label;" - accesskey="&saveChangeSyncDeviceName.accesskey;" - hidden="true"/> - </hbox> - </hbox> - </groupbox> - <label class="fxaMobilePromo"> - &mobilePromo3.start;<!-- We put these comments to avoid inserting white spaces - --><label class="androidLink text-link" id="fxaMobilePromo-android-hasFxaAccount"><!-- - -->&mobilePromo3.androidLink;</label><!-- - -->&mobilePromo3.iOSBefore;<!-- - --><label class="iOSLink text-link" id="fxaMobilePromo-ios-hasFxaAccount"><!-- - -->&mobilePromo3.iOSLink;</label><!-- - -->&mobilePromo3.end; - </label> - <vbox id="tosPP-small" align="start"> - <label id="tosPP-small-ToS" class="text-link"> - &prefs.tosLink.label; - </label> - <label id="tosPP-small-PP" class="text-link"> - &fxaPrivacyNotice.link.label; - </label> - </vbox> + <label class="text-link" + onclick="gSyncPane.startOver(true); return false;" + value="&unlinkDevice.label;"/> </vbox> </deck> diff --git a/application/basilisk/components/preferences/jar.mn b/application/basilisk/components/preferences/jar.mn index 5b24e89df..f74be0820 100644 --- a/application/basilisk/components/preferences/jar.mn +++ b/application/basilisk/components/preferences/jar.mn @@ -24,8 +24,6 @@ browser.jar: * content/browser/preferences/languages.xul content/browser/preferences/languages.js content/browser/preferences/permissions.xul - content/browser/preferences/containers.xul - content/browser/preferences/containers.js content/browser/preferences/permissions.js content/browser/preferences/sanitize.xul content/browser/preferences/sanitize.js diff --git a/application/basilisk/components/search/moz.build b/application/basilisk/components/search/moz.build index aac3a838c..b406d5f16 100644 --- a/application/basilisk/components/search/moz.build +++ b/application/basilisk/components/search/moz.build @@ -4,4 +4,6 @@ # 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/. +DIRS += ['service'] + JAR_MANIFESTS += ['jar.mn'] diff --git a/application/basilisk/components/search/service/SearchStaticData.jsm b/application/basilisk/components/search/service/SearchStaticData.jsm new file mode 100644 index 000000000..de2be695c --- /dev/null +++ b/application/basilisk/components/search/service/SearchStaticData.jsm @@ -0,0 +1,43 @@ +/* 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/. */ + +/* + * This module contains additional data about default search engines that is the + * same across all languages. This information is defined outside of the actual + * search engine definition files, so that localizers don't need to update them + * when a change is made. + * + * This separate module is also easily overridable, in case a hotfix is needed. + * No high-level processing logic is applied here. + */ + +"use strict"; + +this.EXPORTED_SYMBOLS = [ + "SearchStaticData", +]; + +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; + +// To update this list of known alternate domains, just cut-and-paste from +// https://www.google.com/supported_domains +const gGoogleDomainsSource = ".google.com .google.ad .google.ae .google.com.af .google.com.ag .google.com.ai .google.al .google.am .google.co.ao .google.com.ar .google.as .google.at .google.com.au .google.az .google.ba .google.com.bd .google.be .google.bf .google.bg .google.com.bh .google.bi .google.bj .google.com.bn .google.com.bo .google.com.br .google.bs .google.bt .google.co.bw .google.by .google.com.bz .google.ca .google.cd .google.cf .google.cg .google.ch .google.ci .google.co.ck .google.cl .google.cm .google.cn .google.com.co .google.co.cr .google.com.cu .google.cv .google.com.cy .google.cz .google.de .google.dj .google.dk .google.dm .google.com.do .google.dz .google.com.ec .google.ee .google.com.eg .google.es .google.com.et .google.fi .google.com.fj .google.fm .google.fr .google.ga .google.ge .google.gg .google.com.gh .google.com.gi .google.gl .google.gm .google.gp .google.gr .google.com.gt .google.gy .google.com.hk .google.hn .google.hr .google.ht .google.hu .google.co.id .google.ie .google.co.il .google.im .google.co.in .google.iq .google.is .google.it .google.je .google.com.jm .google.jo .google.co.jp .google.co.ke .google.com.kh .google.ki .google.kg .google.co.kr .google.com.kw .google.kz .google.la .google.com.lb .google.li .google.lk .google.co.ls .google.lt .google.lu .google.lv .google.com.ly .google.co.ma .google.md .google.me .google.mg .google.mk .google.ml .google.com.mm .google.mn .google.ms .google.com.mt .google.mu .google.mv .google.mw .google.com.mx .google.com.my .google.co.mz .google.com.na .google.com.nf .google.com.ng .google.com.ni .google.ne .google.nl .google.no .google.com.np .google.nr .google.nu .google.co.nz .google.com.om .google.com.pa .google.com.pe .google.com.pg .google.com.ph .google.com.pk .google.pl .google.pn .google.com.pr .google.ps .google.pt .google.com.py .google.com.qa .google.ro .google.ru .google.rw .google.com.sa .google.com.sb .google.sc .google.se .google.com.sg .google.sh .google.si .google.sk .google.com.sl .google.sn .google.so .google.sm .google.sr .google.st .google.com.sv .google.td .google.tg .google.co.th .google.com.tj .google.tk .google.tl .google.tm .google.tn .google.to .google.com.tr .google.tt .google.com.tw .google.co.tz .google.com.ua .google.co.ug .google.co.uk .google.com.uy .google.co.uz .google.com.vc .google.co.ve .google.vg .google.co.vi .google.com.vn .google.vu .google.ws .google.rs .google.co.za .google.co.zm .google.co.zw .google.cat"; +const gGoogleDomains = gGoogleDomainsSource.split(" ").map(d => "www" + d); + +this.SearchStaticData = { + /** + * Returns a list of alternate domains for a given search engine domain. + * + * @param aDomain + * Lowercase host name to look up. For example, if this argument is + * "www.google.com" or "www.google.co.uk", the function returns the + * full list of supported Google domains. + * + * @return Array containing one entry for each alternate host name, or empty + * array if none is known. The returned array should not be modified. + */ + getAlternateDomains: function (aDomain) { + return gGoogleDomains.indexOf(aDomain) == -1 ? [] : gGoogleDomains; + }, +}; diff --git a/application/basilisk/components/search/service/SearchSuggestionController.jsm b/application/basilisk/components/search/service/SearchSuggestionController.jsm new file mode 100644 index 000000000..952838c0c --- /dev/null +++ b/application/basilisk/components/search/service/SearchSuggestionController.jsm @@ -0,0 +1,398 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +this.EXPORTED_SYMBOLS = ["SearchSuggestionController"]; + +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/Promise.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "NS_ASSERT", "resource://gre/modules/debug.js"); + +const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json"; +const DEFAULT_FORM_HISTORY_PARAM = "searchbar-history"; +const HTTP_OK = 200; +const REMOTE_TIMEOUT = 500; // maximum time (ms) to wait before giving up on a remote suggestions +const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled"; + +/** + * Remote search suggestions will be shown if gRemoteSuggestionsEnabled + * is true. Global because only one pref observer is needed for all instances. + */ +var gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF); +Services.prefs.addObserver(BROWSER_SUGGEST_PREF, function(aSubject, aTopic, aData) { + gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF); +}, false); + +/** + * SearchSuggestionController.jsm exists as a helper module to allow multiple consumers to request and display + * search suggestions from a given engine, regardless of the base implementation. Much of this + * code was originally in nsSearchSuggestions.js until it was refactored to separate it from the + * nsIAutoCompleteSearch dependency. + * One instance of SearchSuggestionController should be used per field since form history results are cached. + */ + +/** + * @param {function} [callback] - Callback for search suggestion results. You can use the promise + * returned by the search method instead if you prefer. + * @constructor + */ +this.SearchSuggestionController = function SearchSuggestionController(callback = null) { + this._callback = callback; +}; + +this.SearchSuggestionController.prototype = { + /** + * The maximum number of local form history results to return. This limit is + * only enforced if remote results are also returned. + */ + maxLocalResults: 5, + + /** + * The maximum number of remote search engine results to return. + * We'll actually only display at most + * maxRemoteResults - <displayed local results count> remote results. + */ + maxRemoteResults: 10, + + /** + * The maximum time (ms) to wait before giving up on a remote suggestions. + */ + remoteTimeout: REMOTE_TIMEOUT, + + /** + * The additional parameter used when searching form history. + */ + formHistoryParam: DEFAULT_FORM_HISTORY_PARAM, + + // Private properties + /** + * The last form history result used to improve the performance of subsequent searches. + * This shouldn't be used for any other purpose as it is never cleared and therefore could be stale. + */ + _formHistoryResult: null, + + /** + * The remote server timeout timer, if applicable. The timer starts when form history + * search is completed. + */ + _remoteResultTimer: null, + + /** + * The deferred for the remote results before its promise is resolved. + */ + _deferredRemoteResult: null, + + /** + * The optional result callback registered from the constructor. + */ + _callback: null, + + /** + * The XMLHttpRequest object for remote results. + */ + _request: null, + + // Public methods + + /** + * Fetch search suggestions from all of the providers. Fetches in progress will be stopped and + * results from them will not be provided. + * + * @param {string} searchTerm - the term to provide suggestions for + * @param {bool} privateMode - whether the request is being made in the context of private browsing + * @param {nsISearchEngine} engine - search engine for the suggestions. + * @param {int} userContextId - the userContextId of the selected tab. + * + * @return {Promise} resolving to an object containing results or null. + */ + fetch: function(searchTerm, privateMode, engine, userContextId) { + // There is no smart filtering from previous results here (as there is when looking through + // history/form data) because the result set returned by the server is different for every typed + // value - e.g. "ocean breathes" does not return a subset of the results returned for "ocean". + + this.stop(); + + if (!Services.search.isInitialized) { + throw new Error("Search not initialized yet (how did you get here?)"); + } + if (typeof privateMode === "undefined") { + throw new Error("The privateMode argument is required to avoid unintentional privacy leaks"); + } + if (!(engine instanceof Ci.nsISearchEngine)) { + throw new Error("Invalid search engine"); + } + if (!this.maxLocalResults && !this.maxRemoteResults) { + throw new Error("Zero results expected, what are you trying to do?"); + } + if (this.maxLocalResults < 0 || this.maxRemoteResults < 0) { + throw new Error("Number of requested results must be positive"); + } + + // Array of promises to resolve before returning results. + let promises = []; + this._searchString = searchTerm; + + // Remote results + if (searchTerm && gRemoteSuggestionsEnabled && this.maxRemoteResults && + engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON)) { + this._deferredRemoteResult = this._fetchRemote(searchTerm, engine, privateMode, userContextId); + promises.push(this._deferredRemoteResult.promise); + } + + // Local results from form history + if (this.maxLocalResults) { + let deferredHistoryResult = this._fetchFormHistory(searchTerm); + promises.push(deferredHistoryResult.promise); + } + + function handleRejection(reason) { + if (reason == "HTTP request aborted") { + // Do nothing since this is normal. + return null; + } + Cu.reportError("SearchSuggestionController rejection: " + reason); + return null; + } + return Promise.all(promises).then(this._dedupeAndReturnResults.bind(this), handleRejection); + }, + + /** + * Stop pending fetches so no results are returned from them. + * + * Note: If there was no remote results fetched, the fetching cannot be stopped and local results + * will still be returned because stopping relies on aborting the XMLHTTPRequest to reject the + * promise for Promise.all. + */ + stop: function() { + if (this._request) { + this._request.abort(); + } else if (!this.maxRemoteResults) { + Cu.reportError("SearchSuggestionController: Cannot stop fetching if remote results were not "+ + "requested"); + } + this._reset(); + }, + + // Private methods + + _fetchFormHistory: function(searchTerm) { + let deferredFormHistory = Promise.defer(); + + let acSearchObserver = { + // Implements nsIAutoCompleteSearch + onSearchResult: (search, result) => { + this._formHistoryResult = result; + + if (this._request) { + this._remoteResultTimer = Cc["@mozilla.org/timer;1"]. + createInstance(Ci.nsITimer); + this._remoteResultTimer.initWithCallback(this._onRemoteTimeout.bind(this), + this.remoteTimeout || REMOTE_TIMEOUT, + Ci.nsITimer.TYPE_ONE_SHOT); + } + + switch (result.searchResult) { + case Ci.nsIAutoCompleteResult.RESULT_SUCCESS: + case Ci.nsIAutoCompleteResult.RESULT_NOMATCH: + if (result.searchString !== this._searchString) { + deferredFormHistory.resolve("Unexpected response, this._searchString does not match form history response"); + return; + } + let fhEntries = []; + for (let i = 0; i < result.matchCount; ++i) { + fhEntries.push(result.getValueAt(i)); + } + deferredFormHistory.resolve({ + result: fhEntries, + formHistoryResult: result, + }); + break; + case Ci.nsIAutoCompleteResult.RESULT_FAILURE: + case Ci.nsIAutoCompleteResult.RESULT_IGNORED: + deferredFormHistory.resolve("Form History returned RESULT_FAILURE or RESULT_IGNORED"); + break; + } + }, + }; + + let formHistory = Cc["@mozilla.org/autocomplete/search;1?name=form-history"]. + createInstance(Ci.nsIAutoCompleteSearch); + formHistory.startSearch(searchTerm, this.formHistoryParam || DEFAULT_FORM_HISTORY_PARAM, + this._formHistoryResult, + acSearchObserver); + return deferredFormHistory; + }, + + /** + * Fetch suggestions from the search engine over the network. + */ + _fetchRemote: function(searchTerm, engine, privateMode, userContextId) { + let deferredResponse = Promise.defer(); + this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. + createInstance(Ci.nsIXMLHttpRequest); + let submission = engine.getSubmission(searchTerm, + SEARCH_RESPONSE_SUGGESTION_JSON); + let method = (submission.postData ? "POST" : "GET"); + this._request.open(method, submission.uri.spec, true); + + this._request.setOriginAttributes({userContextId, + privateBrowsingId: privateMode ? 1 : 0}); + + this._request.mozBackgroundRequest = true; // suppress dialogs and fail silently + + this._request.addEventListener("load", this._onRemoteLoaded.bind(this, deferredResponse)); + this._request.addEventListener("error", (evt) => deferredResponse.resolve("HTTP error")); + // Reject for an abort assuming it's always from .stop() in which case we shouldn't return local + // or remote results for existing searches. + this._request.addEventListener("abort", (evt) => deferredResponse.reject("HTTP request aborted")); + + this._request.send(submission.postData); + + return deferredResponse; + }, + + /** + * Called when the request completed successfully (thought the HTTP status could be anything) + * so we can handle the response data. + * @private + */ + _onRemoteLoaded: function(deferredResponse) { + if (!this._request) { + deferredResponse.resolve("Got HTTP response after the request was cancelled"); + return; + } + + let status, serverResults; + try { + status = this._request.status; + } catch (e) { + // The XMLHttpRequest can throw NS_ERROR_NOT_AVAILABLE. + deferredResponse.resolve("Unknown HTTP status: " + e); + return; + } + + if (status != HTTP_OK || this._request.responseText == "") { + deferredResponse.resolve("Non-200 status or empty HTTP response: " + status); + return; + } + + try { + serverResults = JSON.parse(this._request.responseText); + } catch (ex) { + deferredResponse.resolve("Failed to parse suggestion JSON: " + ex); + return; + } + + if (!serverResults[0] || + this._searchString.localeCompare(serverResults[0], undefined, + { sensitivity: "base" })) { + // something is wrong here so drop remote results + deferredResponse.resolve("Unexpected response, this._searchString does not match remote response"); + return; + } + let results = serverResults[1] || []; + deferredResponse.resolve({ result: results }); + }, + + /** + * Called when this._remoteResultTimer fires indicating the remote request took too long. + */ + _onRemoteTimeout: function () { + this._request = null; + + // FIXME: bug 387341 + // Need to break the cycle between us and the timer. + this._remoteResultTimer = null; + + // The XMLHTTPRequest for suggest results is taking too long + // so send out the form history results and cancel the request. + if (this._deferredRemoteResult) { + this._deferredRemoteResult.resolve("HTTP Timeout"); + this._deferredRemoteResult = null; + } + }, + + /** + * @param {Array} suggestResults - an array of result objects from different sources (local or remote) + * @return {Object} + */ + _dedupeAndReturnResults: function(suggestResults) { + if (this._searchString === null) { + // _searchString can be null if stop() was called and remote suggestions + // were disabled (stopping if we are fetching remote suggestions will + // cause a promise rejection before we reach _dedupeAndReturnResults). + return null; + } + + let results = { + term: this._searchString, + remote: [], + local: [], + formHistoryResult: null, + }; + + for (let result of suggestResults) { + if (typeof result === "string") { // Failure message + Cu.reportError("SearchSuggestionController: " + result); + } else if (result.formHistoryResult) { // Local results have a formHistoryResult property. + results.formHistoryResult = result.formHistoryResult; + results.local = result.result || []; + } else { // Remote result + results.remote = result.result || []; + } + } + + // If we have remote results, cap the number of local results + if (results.remote.length) { + results.local = results.local.slice(0, this.maxLocalResults); + } + + // We don't want things to appear in both history and suggestions so remove entries from + // remote results that are already in local. + if (results.remote.length && results.local.length) { + for (let i = 0; i < results.local.length; ++i) { + let term = results.local[i]; + let dupIndex = results.remote.indexOf(term); + if (dupIndex != -1) { + results.remote.splice(dupIndex, 1); + } + } + } + + // Trim the number of results to the maximum requested (now that we've pruned dupes). + results.remote = + results.remote.slice(0, this.maxRemoteResults - results.local.length); + + if (this._callback) { + this._callback(results); + } + this._reset(); + + return results; + }, + + _reset: function() { + this._request = null; + if (this._remoteResultTimer) { + this._remoteResultTimer.cancel(); + this._remoteResultTimer = null; + } + this._deferredRemoteResult = null; + this._searchString = null; + }, +}; + +/** + * Determines whether the given engine offers search suggestions. + * + * @param {nsISearchEngine} engine - The search engine + * @return {boolean} True if the engine offers suggestions and false otherwise. + */ +this.SearchSuggestionController.engineOffersSuggestions = function(engine) { + return engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON); +}; diff --git a/application/basilisk/components/search/service/moz.build b/application/basilisk/components/search/service/moz.build new file mode 100644 index 000000000..423faeffd --- /dev/null +++ b/application/basilisk/components/search/service/moz.build @@ -0,0 +1,24 @@ +# -*- 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/. + +DIST_SUBDIR = '' + +DEFINES['HAVE_SIDEBAR'] = True + +EXTRA_COMPONENTS += [ + 'nsSearchSuggestions.js', + 'nsSidebar.js', +] + +EXTRA_PP_COMPONENTS += [ + 'nsSearchService.js', + 'toolkitsearch.manifest', +] + +EXTRA_JS_MODULES += [ + 'SearchStaticData.jsm', + 'SearchSuggestionController.jsm', +] diff --git a/application/basilisk/components/search/service/nsSearchService.js b/application/basilisk/components/search/service/nsSearchService.js new file mode 100644 index 000000000..b4db31dee --- /dev/null +++ b/application/basilisk/components/search/service/nsSearchService.js @@ -0,0 +1,4324 @@ +/* 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/. */ + +const Ci = Components.interfaces; +const Cc = Components.classes; +const Cr = Components.results; +const Cu = Components.utils; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/Promise.jsm"); +Cu.import("resource://gre/modules/debug.js"); +Cu.import("resource://gre/modules/AppConstants.jsm"); + +XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown", + "resource://gre/modules/AsyncShutdown.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask", + "resource://gre/modules/DeferredTask.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "OS", + "resource://gre/modules/osfile.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Task", + "resource://gre/modules/Task.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Deprecated", + "resource://gre/modules/Deprecated.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "SearchStaticData", + "resource://gre/modules/SearchStaticData.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "setTimeout", + "resource://gre/modules/Timer.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "clearTimeout", + "resource://gre/modules/Timer.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Lz4", + "resource://gre/modules/lz4.js"); + +XPCOMUtils.defineLazyServiceGetter(this, "gTextToSubURI", + "@mozilla.org/intl/texttosuburi;1", + "nsITextToSubURI"); +XPCOMUtils.defineLazyServiceGetter(this, "gEnvironment", + "@mozilla.org/process/environment;1", + "nsIEnvironment"); + +Cu.importGlobalProperties(["XMLHttpRequest"]); + +// A text encoder to UTF8, used whenever we commit the cache to disk. +XPCOMUtils.defineLazyGetter(this, "gEncoder", + function() { + return new TextEncoder(); + }); + +const MODE_RDONLY = 0x01; +const MODE_WRONLY = 0x02; +const MODE_CREATE = 0x08; +const MODE_APPEND = 0x10; +const MODE_TRUNCATE = 0x20; + +// Directory service keys +const NS_APP_SEARCH_DIR_LIST = "SrchPluginsDL"; +const NS_APP_DISTRIBUTION_SEARCH_DIR_LIST = "SrchPluginsDistDL"; +const NS_APP_USER_SEARCH_DIR = "UsrSrchPlugns"; +const NS_APP_SEARCH_DIR = "SrchPlugns"; +const NS_APP_USER_PROFILE_50_DIR = "ProfD"; + +// Loading plugins from NS_APP_SEARCH_DIR is no longer supported. +// Instead, we now load plugins from APP_SEARCH_PREFIX, where a +// list.txt file needs to exist to list available engines. +const APP_SEARCH_PREFIX = "resource://search-plugins/"; + +// See documentation in nsIBrowserSearchService.idl. +const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified"; +const QUIT_APPLICATION_TOPIC = "quit-application"; + +const SEARCH_ENGINE_REMOVED = "engine-removed"; +const SEARCH_ENGINE_ADDED = "engine-added"; +const SEARCH_ENGINE_CHANGED = "engine-changed"; +const SEARCH_ENGINE_LOADED = "engine-loaded"; +const SEARCH_ENGINE_CURRENT = "engine-current"; +const SEARCH_ENGINE_DEFAULT = "engine-default"; + +// The following constants are left undocumented in nsIBrowserSearchService.idl +// For the moment, they are meant for testing/debugging purposes only. + +/** + * Topic used for events involving the service itself. + */ +const SEARCH_SERVICE_TOPIC = "browser-search-service"; + +/** + * Sent whenever the cache is fully written to disk. + */ +const SEARCH_SERVICE_CACHE_WRITTEN = "write-cache-to-disk-complete"; + +// Delay for lazy serialization (ms) +const LAZY_SERIALIZE_DELAY = 100; + +// Delay for batching invalidation of the JSON cache (ms) +const CACHE_INVALIDATION_DELAY = 1000; + +// Current cache version. This should be incremented if the format of the cache +// file is modified. +const CACHE_VERSION = 1; + +const CACHE_FILENAME = "search.json.mozlz4"; + +const NEW_LINES = /(\r\n|\r|\n)/; + +// Set an arbitrary cap on the maximum icon size. Without this, large icons can +// cause big delays when loading them at startup. +const MAX_ICON_SIZE = 32768; + +// Default charset to use for sending search parameters. ISO-8859-1 is used to +// match previous nsInternetSearchService behavior. +const DEFAULT_QUERY_CHARSET = "ISO-8859-1"; + +const SEARCH_BUNDLE = "chrome://global/locale/search/search.properties"; +const BRAND_BUNDLE = "chrome://branding/locale/brand.properties"; + +const OPENSEARCH_NS_10 = "http://a9.com/-/spec/opensearch/1.0/"; +const OPENSEARCH_NS_11 = "http://a9.com/-/spec/opensearch/1.1/"; + +// Although the specification at http://opensearch.a9.com/spec/1.1/description/ +// gives the namespace names defined above, many existing OpenSearch engines +// are using the following versions. We therefore allow either. +const OPENSEARCH_NAMESPACES = [ + OPENSEARCH_NS_11, OPENSEARCH_NS_10, + "http://a9.com/-/spec/opensearchdescription/1.1/", + "http://a9.com/-/spec/opensearchdescription/1.0/" +]; + +const OPENSEARCH_LOCALNAME = "OpenSearchDescription"; + +const MOZSEARCH_NS_10 = "http://www.mozilla.org/2006/browser/search/"; +const MOZSEARCH_LOCALNAME = "SearchPlugin"; + +const URLTYPE_SUGGEST_JSON = "application/x-suggestions+json"; +const URLTYPE_SEARCH_HTML = "text/html"; +const URLTYPE_OPENSEARCH = "application/opensearchdescription+xml"; + +const BROWSER_SEARCH_PREF = "browser.search."; +const LOCALE_PREF = "general.useragent.locale"; + +const USER_DEFINED = "{searchTerms}"; + +// Custom search parameters +const MOZ_OFFICIAL = AppConstants.MOZ_OFFICIAL_BRANDING ? "official" : "unofficial"; + +const MOZ_PARAM_LOCALE = /\{moz:locale\}/g; +const MOZ_PARAM_DIST_ID = /\{moz:distributionID\}/g; +const MOZ_PARAM_OFFICIAL = /\{moz:official\}/g; + +// Supported OpenSearch parameters +// See http://opensearch.a9.com/spec/1.1/querysyntax/#core +const OS_PARAM_USER_DEFINED = /\{searchTerms\??\}/g; +const OS_PARAM_INPUT_ENCODING = /\{inputEncoding\??\}/g; +const OS_PARAM_LANGUAGE = /\{language\??\}/g; +const OS_PARAM_OUTPUT_ENCODING = /\{outputEncoding\??\}/g; + +// Default values +const OS_PARAM_LANGUAGE_DEF = "*"; +const OS_PARAM_OUTPUT_ENCODING_DEF = "UTF-8"; +const OS_PARAM_INPUT_ENCODING_DEF = "UTF-8"; + +// "Unsupported" OpenSearch parameters. For example, we don't support +// page-based results, so if the engine requires that we send the "page index" +// parameter, we'll always send "1". +const OS_PARAM_COUNT = /\{count\??\}/g; +const OS_PARAM_START_INDEX = /\{startIndex\??\}/g; +const OS_PARAM_START_PAGE = /\{startPage\??\}/g; + +// Default values +const OS_PARAM_COUNT_DEF = "20"; // 20 results +const OS_PARAM_START_INDEX_DEF = "1"; // start at 1st result +const OS_PARAM_START_PAGE_DEF = "1"; // 1st page + +// Optional parameter +const OS_PARAM_OPTIONAL = /\{(?:\w+:)?\w+\?\}/g; + +// A array of arrays containing parameters that we don't fully support, and +// their default values. We will only send values for these parameters if +// required, since our values are just really arbitrary "guesses" that should +// give us the output we want. +var OS_UNSUPPORTED_PARAMS = [ + [OS_PARAM_COUNT, OS_PARAM_COUNT_DEF], + [OS_PARAM_START_INDEX, OS_PARAM_START_INDEX_DEF], + [OS_PARAM_START_PAGE, OS_PARAM_START_PAGE_DEF], +]; + +// The default engine update interval, in days. This is only used if an engine +// specifies an updateURL, but not an updateInterval. +const SEARCH_DEFAULT_UPDATE_INTERVAL = 7; + +// The default interval before checking again for the name of the +// default engine for the region, in seconds. Only used if the response +// from the server doesn't specify an interval. +const SEARCH_GEO_DEFAULT_UPDATE_INTERVAL = 2592000; // 30 days. + +this.__defineGetter__("FileUtils", function() { + delete this.FileUtils; + Components.utils.import("resource://gre/modules/FileUtils.jsm"); + return FileUtils; +}); + +this.__defineGetter__("NetUtil", function() { + delete this.NetUtil; + Components.utils.import("resource://gre/modules/NetUtil.jsm"); + return NetUtil; +}); + +this.__defineGetter__("gChromeReg", function() { + delete this.gChromeReg; + return this.gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]. + getService(Ci.nsIChromeRegistry); +}); + +/** + * Prefixed to all search debug output. + */ +const SEARCH_LOG_PREFIX = "*** Search: "; + +/** + * Outputs aText to the JavaScript console as well as to stdout. + */ +function DO_LOG(aText) { + dump(SEARCH_LOG_PREFIX + aText + "\n"); + Services.console.logStringMessage(aText); +} + +/** + * In debug builds, use a live, pref-based (browser.search.log) LOG function + * to allow enabling/disabling without a restart. Otherwise, don't log at all by + * default. This can be overridden at startup by the pref, see SearchService's + * _init method. + */ +var LOG = function() {}; + +if (AppConstants.DEBUG) { + LOG = function (aText) { + if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "log", false)) { + DO_LOG(aText); + } + }; +} + +/** + * Presents an assertion dialog in non-release builds and throws. + * @param message + * A message to display + * @param resultCode + * The NS_ERROR_* value to throw. + * @throws resultCode + */ +function ERROR(message, resultCode) { + NS_ASSERT(false, SEARCH_LOG_PREFIX + message); + throw Components.Exception(message, resultCode); +} + +/** + * Logs the failure message (if browser.search.log is enabled) and throws. + * @param message + * A message to display + * @param resultCode + * The NS_ERROR_* value to throw. + * @throws resultCode or NS_ERROR_INVALID_ARG if resultCode isn't specified. + */ +function FAIL(message, resultCode) { + LOG(message); + throw Components.Exception(message, resultCode || Cr.NS_ERROR_INVALID_ARG); +} + +/** + * Truncates big blobs of (data-)URIs to console-friendly sizes + * @param str + * String to tone down + * @param len + * Maximum length of the string to return. Defaults to the length of a tweet. + */ +function limitURILength(str, len) { + len = len || 140; + if (str.length > len) + return str.slice(0, len) + "..."; + return str; +} + +/** + * Ensures an assertion is met before continuing. Should be used to indicate + * fatal errors. + * @param assertion + * An assertion that must be met + * @param message + * A message to display if the assertion is not met + * @param resultCode + * The NS_ERROR_* value to throw if the assertion is not met + * @throws resultCode + */ +function ENSURE_WARN(assertion, message, resultCode) { + NS_ASSERT(assertion, SEARCH_LOG_PREFIX + message); + if (!assertion) + throw Components.Exception(message, resultCode); +} + +function loadListener(aChannel, aEngine, aCallback) { + this._channel = aChannel; + this._bytes = []; + this._engine = aEngine; + this._callback = aCallback; +} +loadListener.prototype = { + _callback: null, + _channel: null, + _countRead: 0, + _engine: null, + _stream: null, + + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIRequestObserver, + Ci.nsIStreamListener, + Ci.nsIChannelEventSink, + Ci.nsIInterfaceRequestor, + // See FIXME comment below. + Ci.nsIHttpEventSink, + Ci.nsIProgressEventSink + ]), + + // nsIRequestObserver + onStartRequest: function SRCH_loadStartR(aRequest, aContext) { + LOG("loadListener: Starting request: " + aRequest.name); + this._stream = Cc["@mozilla.org/binaryinputstream;1"]. + createInstance(Ci.nsIBinaryInputStream); + }, + + onStopRequest: function SRCH_loadStopR(aRequest, aContext, aStatusCode) { + LOG("loadListener: Stopping request: " + aRequest.name); + + var requestFailed = !Components.isSuccessCode(aStatusCode); + if (!requestFailed && (aRequest instanceof Ci.nsIHttpChannel)) + requestFailed = !aRequest.requestSucceeded; + + if (requestFailed || this._countRead == 0) { + LOG("loadListener: request failed!"); + // send null so the callback can deal with the failure + this._callback(null, this._engine); + } else + this._callback(this._bytes, this._engine); + this._channel = null; + this._engine = null; + }, + + // nsIStreamListener + onDataAvailable: function SRCH_loadDAvailable(aRequest, aContext, + aInputStream, aOffset, + aCount) { + this._stream.setInputStream(aInputStream); + + // Get a byte array of the data + this._bytes = this._bytes.concat(this._stream.readByteArray(aCount)); + this._countRead += aCount; + }, + + // nsIChannelEventSink + asyncOnChannelRedirect: function SRCH_loadCRedirect(aOldChannel, aNewChannel, + aFlags, callback) { + this._channel = aNewChannel; + callback.onRedirectVerifyCallback(Components.results.NS_OK); + }, + + // nsIInterfaceRequestor + getInterface: function SRCH_load_GI(aIID) { + return this.QueryInterface(aIID); + }, + + // FIXME: bug 253127 + // nsIHttpEventSink + onRedirect: function (aChannel, aNewChannel) {}, + // nsIProgressEventSink + onProgress: function (aRequest, aContext, aProgress, aProgressMax) {}, + onStatus: function (aRequest, aContext, aStatus, aStatusArg) {} +} + +function isPartnerBuild() { + try { + let distroID = Services.prefs.getCharPref("distribution.id"); + + // Mozilla-provided builds (i.e. funnelcake) are not partner builds + if (distroID && !distroID.startsWith("mozilla")) { + return true; + } + } catch (e) {} + + return false; +} + +function getVerificationHash(aName) { + let disclaimer = "By modifying this file, I agree that I am doing so " + + "only within $appName itself, using official, user-driven search " + + "engine selection processes, and in a way which does not circumvent " + + "user consent. I acknowledge that any attempt to change this file " + + "from outside of $appName is a malicious act, and will be responded " + + "to accordingly." + + let salt = OS.Path.basename(OS.Constants.Path.profileDir) + aName + + disclaimer.replace(/\$appName/g, Services.appinfo.name); + + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = "UTF-8"; + + // Data is an array of bytes. + let data = converter.convertToByteArray(salt, {}); + let hasher = Cc["@mozilla.org/security/hash;1"] + .createInstance(Ci.nsICryptoHash); + hasher.init(hasher.SHA256); + hasher.update(data, data.length); + + return hasher.finish(true); +} + +/** + * Safely close a nsISafeOutputStream. + * @param aFOS + * The file output stream to close. + */ +function closeSafeOutputStream(aFOS) { + if (aFOS instanceof Ci.nsISafeOutputStream) { + try { + aFOS.finish(); + return; + } catch (e) { } + } + aFOS.close(); +} + +/** + * Wrapper function for nsIIOService::newURI. + * @param aURLSpec + * The URL string from which to create an nsIURI. + * @returns an nsIURI object, or null if the creation of the URI failed. + */ +function makeURI(aURLSpec, aCharset) { + try { + return NetUtil.newURI(aURLSpec, aCharset); + } catch (ex) { } + + return null; +} + +/** + * Wrapper function for nsIIOService::newChannel2. + * @param url + * The URL string from which to create an nsIChannel. + * @returns an nsIChannel object, or null if the url is invalid. + */ +function makeChannel(url) { + try { + return NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true + }); + } catch (ex) { } + + return null; +} + +/** + * Gets a directory from the directory service. + * @param aKey + * The directory service key indicating the directory to get. + */ +function getDir(aKey, aIFace) { + if (!aKey) + FAIL("getDir requires a directory key!"); + + return Services.dirsvc.get(aKey, aIFace || Ci.nsIFile); +} + +/** + * Gets the current value of the locale. It's possible for this preference to + * be localized, so we have to do a little extra work here. Similar code + * exists in nsHttpHandler.cpp when building the UA string. + */ +function getLocale() { + let locale = getLocalizedPref(LOCALE_PREF); + if (locale) + return locale; + + // Not localized. + return Services.prefs.getCharPref(LOCALE_PREF); +} + +/** + * Wrapper for nsIPrefBranch::getComplexValue. + * @param aPrefName + * The name of the pref to get. + * @returns aDefault if the requested pref doesn't exist. + */ +function getLocalizedPref(aPrefName, aDefault) { + const nsIPLS = Ci.nsIPrefLocalizedString; + try { + return Services.prefs.getComplexValue(aPrefName, nsIPLS).data; + } catch (ex) {} + + return aDefault; +} + +/** + * @return a sanitized name to be used as a filename, or a random name + * if a sanitized name cannot be obtained (if aName contains + * no valid characters). + */ +function sanitizeName(aName) { + const maxLength = 60; + const minLength = 1; + var name = aName.toLowerCase(); + name = name.replace(/\s+/g, "-"); + name = name.replace(/[^-a-z0-9]/g, ""); + + // Use a random name if our input had no valid characters. + if (name.length < minLength) + name = Math.random().toString(36).replace(/^.*\./, ''); + + // Force max length. + return name.substring(0, maxLength); +} + +/** + * Retrieve a pref from the search param branch. + * + * @param prefName + * The name of the pref. + **/ +function getMozParamPref(prefName) { + let branch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF + "param."); + return encodeURIComponent(branch.getCharPref(prefName)); +} + +/** + * Notifies watchers of SEARCH_ENGINE_TOPIC about changes to an engine or to + * the state of the search service. + * + * @param aEngine + * The nsISearchEngine object to which the change applies. + * @param aVerb + * A verb describing the change. + * + * @see nsIBrowserSearchService.idl + */ +var gInitialized = false; +function notifyAction(aEngine, aVerb) { + if (gInitialized) { + LOG("NOTIFY: Engine: \"" + aEngine.name + "\"; Verb: \"" + aVerb + "\""); + Services.obs.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb); + } +} + +function parseJsonFromStream(aInputStream) { + const json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON); + const data = json.decodeFromStream(aInputStream, aInputStream.available()); + return data; +} + +/** + * Simple object representing a name/value pair. + */ +function QueryParameter(aName, aValue, aPurpose) { + if (!aName || (aValue == null)) + FAIL("missing name or value for QueryParameter!"); + + this.name = aName; + this.value = aValue; + this.purpose = aPurpose; +} + +/** + * Perform OpenSearch parameter substitution on aParamValue. + * + * @param aParamValue + * A string containing OpenSearch search parameters. + * @param aSearchTerms + * The user-provided search terms. This string will inserted into + * aParamValue as the value of the OS_PARAM_USER_DEFINED parameter. + * This value must already be escaped appropriately - it is inserted + * as-is. + * @param aEngine + * The engine which owns the string being acted on. + * + * @see http://opensearch.a9.com/spec/1.1/querysyntax/#core + */ +function ParamSubstitution(aParamValue, aSearchTerms, aEngine) { + var value = aParamValue; + + var distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID", + Services.appinfo.distributionID || ""); + var official = MOZ_OFFICIAL; + try { + if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "official")) + official = "official"; + else + official = "unofficial"; + } + catch (ex) { } + + // Custom search parameters. These are only available to default search + // engines. + if (aEngine._isDefault) { + value = value.replace(MOZ_PARAM_LOCALE, getLocale()); + value = value.replace(MOZ_PARAM_DIST_ID, distributionID); + value = value.replace(MOZ_PARAM_OFFICIAL, official); + } + + // Insert the OpenSearch parameters we're confident about + value = value.replace(OS_PARAM_USER_DEFINED, aSearchTerms); + value = value.replace(OS_PARAM_INPUT_ENCODING, aEngine.queryCharset); + value = value.replace(OS_PARAM_LANGUAGE, + getLocale() || OS_PARAM_LANGUAGE_DEF); + value = value.replace(OS_PARAM_OUTPUT_ENCODING, + OS_PARAM_OUTPUT_ENCODING_DEF); + + // Replace any optional parameters + value = value.replace(OS_PARAM_OPTIONAL, ""); + + // Insert any remaining required params with our default values + for (var i = 0; i < OS_UNSUPPORTED_PARAMS.length; ++i) { + value = value.replace(OS_UNSUPPORTED_PARAMS[i][0], + OS_UNSUPPORTED_PARAMS[i][1]); + } + + return value; +} + +/** + * Creates an engineURL object, which holds the query URL and all parameters. + * + * @param aType + * A string containing the name of the MIME type of the search results + * returned by this URL. + * @param aMethod + * The HTTP request method. Must be a case insensitive value of either + * "GET" or "POST". + * @param aTemplate + * The URL to which search queries should be sent. For GET requests, + * must contain the string "{searchTerms}", to indicate where the user + * entered search terms should be inserted. + * @param aResultDomain + * The root domain for this URL. Defaults to the template's host. + * + * @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag + * + * @throws NS_ERROR_NOT_IMPLEMENTED if aType is unsupported. + */ +function EngineURL(aType, aMethod, aTemplate, aResultDomain) { + if (!aType || !aMethod || !aTemplate) + FAIL("missing type, method or template for EngineURL!"); + + var method = aMethod.toUpperCase(); + var type = aType.toLowerCase(); + + if (method != "GET" && method != "POST") + FAIL("method passed to EngineURL must be \"GET\" or \"POST\""); + + this.type = type; + this.method = method; + this.params = []; + this.rels = []; + // Don't serialize expanded mozparams + this.mozparams = {}; + + var templateURI = makeURI(aTemplate); + if (!templateURI) + FAIL("new EngineURL: template is not a valid URI!", Cr.NS_ERROR_FAILURE); + + switch (templateURI.scheme) { + case "http": + case "https": + // Disable these for now, see bug 295018 + // case "file": + // case "resource": + this.template = aTemplate; + break; + default: + FAIL("new EngineURL: template uses invalid scheme!", Cr.NS_ERROR_FAILURE); + } + + // If no resultDomain was specified in the engine definition file, use the + // host from the template. + this.resultDomain = aResultDomain || templateURI.host; + // We never want to return a "www." prefix, so eventually strip it. + if (this.resultDomain.startsWith("www.")) { + this.resultDomain = this.resultDomain.substr(4); + } +} +EngineURL.prototype = { + + addParam: function SRCH_EURL_addParam(aName, aValue, aPurpose) { + this.params.push(new QueryParameter(aName, aValue, aPurpose)); + }, + + // Note: This method requires that aObj has a unique name or the previous MozParams entry with + // that name will be overwritten. + _addMozParam: function SRCH_EURL__addMozParam(aObj) { + aObj.mozparam = true; + this.mozparams[aObj.name] = aObj; + }, + + getSubmission: function SRCH_EURL_getSubmission(aSearchTerms, aEngine, aPurpose) { + var url = ParamSubstitution(this.template, aSearchTerms, aEngine); + // Default to searchbar if the purpose is not provided + var purpose = aPurpose || "searchbar"; + + // If a particular purpose isn't defined in the plugin, fallback to 'searchbar'. + if (!this.params.some(p => p.purpose !== undefined && p.purpose == purpose)) + purpose = "searchbar"; + + // Create an application/x-www-form-urlencoded representation of our params + // (name=value&name=value&name=value) + var dataString = ""; + for (var i = 0; i < this.params.length; ++i) { + var param = this.params[i]; + + // If this parameter has a purpose, only add it if the purpose matches + if (param.purpose !== undefined && param.purpose != purpose) + continue; + + var value = ParamSubstitution(param.value, aSearchTerms, aEngine); + + dataString += (i > 0 ? "&" : "") + param.name + "=" + value; + } + + var postData = null; + if (this.method == "GET") { + // GET method requests have no post data, and append the encoded + // query string to the url... + if (url.indexOf("?") == -1 && dataString) + url += "?"; + url += dataString; + } else if (this.method == "POST") { + // POST method requests must wrap the encoded text in a MIME + // stream and supply that as POSTDATA. + var stringStream = Cc["@mozilla.org/io/string-input-stream;1"]. + createInstance(Ci.nsIStringInputStream); + stringStream.data = dataString; + + postData = Cc["@mozilla.org/network/mime-input-stream;1"]. + createInstance(Ci.nsIMIMEInputStream); + postData.addHeader("Content-Type", "application/x-www-form-urlencoded"); + postData.addContentLength = true; + postData.setData(stringStream); + } + + return new Submission(makeURI(url), postData); + }, + + _getTermsParameterName: function SRCH_EURL__getTermsParameterName() { + let queryParam = this.params.find(p => p.value == USER_DEFINED); + return queryParam ? queryParam.name : ""; + }, + + _hasRelation: function SRC_EURL__hasRelation(aRel) { + return this.rels.some(e => e == aRel.toLowerCase()); + }, + + _initWithJSON: function SRC_EURL__initWithJSON(aJson, aEngine) { + if (!aJson.params) + return; + + this.rels = aJson.rels; + + for (let i = 0; i < aJson.params.length; ++i) { + let param = aJson.params[i]; + if (param.mozparam) { + if (param.condition == "pref") { + let value = getMozParamPref(param.pref); + this.addParam(param.name, value); + } + this._addMozParam(param); + } + else + this.addParam(param.name, param.value, param.purpose || undefined); + } + }, + + /** + * Creates a JavaScript object that represents this URL. + * @returns An object suitable for serialization as JSON. + **/ + toJSON: function SRCH_EURL_toJSON() { + var json = { + template: this.template, + rels: this.rels, + resultDomain: this.resultDomain + }; + + if (this.type != URLTYPE_SEARCH_HTML) + json.type = this.type; + if (this.method != "GET") + json.method = this.method; + + function collapseMozParams(aParam) { + return this.mozparams[aParam.name] || aParam; + } + json.params = this.params.map(collapseMozParams, this); + + return json; + } +}; + +/** + * nsISearchEngine constructor. + * @param aLocation + * A nsILocalFile or nsIURI object representing the location of the + * search engine data file. + * @param aIsReadOnly + * Boolean indicating whether the engine should be treated as read-only. + */ +function Engine(aLocation, aIsReadOnly) { + this._readOnly = aIsReadOnly; + this._urls = []; + this._metaData = {}; + + let file, uri; + if (typeof aLocation == "string") { + this._shortName = aLocation; + } else if (aLocation instanceof Ci.nsILocalFile) { + if (!aIsReadOnly) { + // This is an engine that was installed in NS_APP_USER_SEARCH_DIR by a + // previous version. We are converting the file to an engine stored only + // in JSON, but we need to keep the reference to the profile file to + // remove it if the user ever removes the engine. + this._filePath = aLocation.persistentDescriptor; + } + file = aLocation; + } else if (aLocation instanceof Ci.nsIURI) { + switch (aLocation.scheme) { + case "https": + case "http": + case "ftp": + case "data": + case "file": + case "resource": + case "chrome": + uri = aLocation; + break; + default: + ERROR("Invalid URI passed to the nsISearchEngine constructor", + Cr.NS_ERROR_INVALID_ARG); + } + } else + ERROR("Engine location is neither a File nor a URI object", + Cr.NS_ERROR_INVALID_ARG); + + if (!this._shortName) { + // If we don't have a shortName at this point, it's the first time we load + // this engine, so let's generate the shortName, id and loadPath values. + let shortName; + if (file) { + shortName = file.leafName; + } + else if (uri && uri instanceof Ci.nsIURL) { + if (aIsReadOnly || (gEnvironment.get("XPCSHELL_TEST_PROFILE_DIR") && + uri.scheme == "resource")) { + shortName = uri.fileName; + } + } + if (shortName && shortName.endsWith(".xml")) { + this._shortName = shortName.slice(0, -4); + } + this._loadPath = this.getAnonymizedLoadPath(file, uri); + + if (!shortName && !aIsReadOnly) { + // We are in the process of downloading and installing the engine. + // We'll have the shortName and id once we are done parsing it. + return; + } + + // Build the id used for the legacy metadata storage, so that we + // can do a one-time import of data from old profiles. + if (this._isDefault || + (uri && uri.spec.startsWith(APP_SEARCH_PREFIX))) { + // The second part of the check is to catch engines from language packs. + // They aren't default engines (because they aren't app-shipped), but we + // still need to give their id an [app] prefix for backward compat. + this._id = "[app]/" + this._shortName + ".xml"; + } + else if (!aIsReadOnly) { + this._id = "[profile]/" + this._shortName + ".xml"; + } + else { + // If the engine is neither a default one, nor a user-installed one, + // it must be extension-shipped, so use the full path as id. + LOG("Setting _id to full path for engine from " + this._loadPath); + this._id = file ? file.path : uri.spec; + } + } +} + +Engine.prototype = { + // Data set by the user. + _metaData: null, + // The data describing the engine, in the form of an XML document element. + _data: null, + // Whether or not the engine is readonly. + _readOnly: true, + // Anonymized path of where we initially loaded the engine from. + // This will stay null for engines installed in the profile before we moved + // to a JSON storage. + _loadPath: null, + // The engine's description + _description: "", + // Used to store the engine to replace, if we're an update to an existing + // engine. + _engineToUpdate: null, + // Set to true if the engine has a preferred icon (an icon that should not be + // overridden by a non-preferred icon). + _hasPreferredIcon: null, + // The engine's name. + _name: null, + // The name of the charset used to submit the search terms. + _queryCharset: null, + // The engine's raw SearchForm value (URL string pointing to a search form). + __searchForm: null, + get _searchForm() { + return this.__searchForm; + }, + set _searchForm(aValue) { + if (/^https?:/i.test(aValue)) + this.__searchForm = aValue; + else + LOG("_searchForm: Invalid URL dropped for " + this._name || + "the current engine"); + }, + // Whether to obtain user confirmation before adding the engine. This is only + // used when the engine is first added to the list. + _confirm: false, + // Whether to set this as the current engine as soon as it is loaded. This + // is only used when the engine is first added to the list. + _useNow: false, + // A function to be invoked when this engine object's addition completes (or + // fails). Only used for installation via addEngine. + _installCallback: null, + // The number of days between update checks for new versions + _updateInterval: null, + // The url to check at for a new update + _updateURL: null, + // The url to check for a new icon + _iconUpdateURL: null, + /* The extension ID if added by an extension. */ + _extensionID: null, + + /** + * Retrieves the data from the engine's file. + * The document element is placed in the engine's data field. + */ + _initFromFile: function SRCH_ENG_initFromFile(file) { + if (!file || !file.exists()) + FAIL("File must exist before calling initFromFile!", Cr.NS_ERROR_UNEXPECTED); + + var fileInStream = Cc["@mozilla.org/network/file-input-stream;1"]. + createInstance(Ci.nsIFileInputStream); + + fileInStream.init(file, MODE_RDONLY, FileUtils.PERMS_FILE, false); + + var domParser = Cc["@mozilla.org/xmlextras/domparser;1"]. + createInstance(Ci.nsIDOMParser); + var doc = domParser.parseFromStream(fileInStream, "UTF-8", + file.fileSize, + "text/xml"); + + this._data = doc.documentElement; + fileInStream.close(); + + // Now that the data is loaded, initialize the engine object + this._initFromData(); + }, + + /** + * Retrieves the data from the engine's file asynchronously. + * The document element is placed in the engine's data field. + * + * @param file The file to load the search plugin from. + * + * @returns {Promise} A promise, resolved successfully if initializing from + * data succeeds, rejected if it fails. + */ + _asyncInitFromFile: Task.async(function* (file) { + if (!file || !(yield OS.File.exists(file.path))) + FAIL("File must exist before calling initFromFile!", Cr.NS_ERROR_UNEXPECTED); + + let fileURI = NetUtil.ioService.newFileURI(file); + yield this._retrieveSearchXMLData(fileURI.spec); + + // Now that the data is loaded, initialize the engine object + this._initFromData(); + }), + + /** + * Retrieves the engine data from a URI. Initializes the engine, flushes to + * disk, and notifies the search service once initialization is complete. + * + * @param uri The uri to load the search plugin from. + */ + _initFromURIAndLoad: function SRCH_ENG_initFromURIAndLoad(uri) { + ENSURE_WARN(uri instanceof Ci.nsIURI, + "Must have URI when calling _initFromURIAndLoad!", + Cr.NS_ERROR_UNEXPECTED); + + LOG("_initFromURIAndLoad: Downloading engine from: \"" + uri.spec + "\"."); + + var chan = NetUtil.newChannel({ + uri: uri, + loadUsingSystemPrincipal: true + }); + + if (this._engineToUpdate && (chan instanceof Ci.nsIHttpChannel)) { + var lastModified = this._engineToUpdate.getAttr("updatelastmodified"); + if (lastModified) + chan.setRequestHeader("If-Modified-Since", lastModified, false); + } + this._uri = uri; + var listener = new loadListener(chan, this, this._onLoad); + chan.notificationCallbacks = listener; + chan.asyncOpen2(listener); + }, + + /** + * Retrieves the engine data from a URI asynchronously and initializes it. + * + * @param uri The uri to load the search plugin from. + * + * @returns {Promise} A promise, resolved successfully if retrieveing data + * succeeds. + */ + _asyncInitFromURI: Task.async(function* (uri) { + LOG("_asyncInitFromURI: Loading engine from: \"" + uri.spec + "\"."); + yield this._retrieveSearchXMLData(uri.spec); + // Now that the data is loaded, initialize the engine object + this._initFromData(); + }), + + /** + * Retrieves the engine data for a given URI asynchronously. + * + * @returns {Promise} A promise, resolved successfully if retrieveing data + * succeeds. + */ + _retrieveSearchXMLData: function SRCH_ENG__retrieveSearchXMLData(aURL) { + let deferred = Promise.defer(); + let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. + createInstance(Ci.nsIXMLHttpRequest); + request.overrideMimeType("text/xml"); + request.onload = (aEvent) => { + let responseXML = aEvent.target.responseXML; + this._data = responseXML.documentElement; + deferred.resolve(); + }; + request.onerror = function(aEvent) { + deferred.resolve(); + }; + request.open("GET", aURL, true); + request.send(); + + return deferred.promise; + }, + + _initFromURISync: function SRCH_ENG_initFromURISync(uri) { + ENSURE_WARN(uri instanceof Ci.nsIURI, + "Must have URI when calling _initFromURISync!", + Cr.NS_ERROR_UNEXPECTED); + + ENSURE_WARN(uri.schemeIs("resource"), "_initFromURISync called for non-resource URI", + Cr.NS_ERROR_FAILURE); + + LOG("_initFromURISync: Loading engine from: \"" + uri.spec + "\"."); + + var chan = NetUtil.newChannel({ + uri: uri, + loadUsingSystemPrincipal: true + }); + + var stream = chan.open2(); + var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. + createInstance(Ci.nsIDOMParser); + var doc = parser.parseFromStream(stream, "UTF-8", stream.available(), "text/xml"); + + this._data = doc.documentElement; + + // Now that the data is loaded, initialize the engine object + this._initFromData(); + }, + + /** + * Attempts to find an EngineURL object in the set of EngineURLs for + * this Engine that has the given type string. (This corresponds to the + * "type" attribute in the "Url" node in the OpenSearch spec.) + * This method will return the first matching URL object found, or null + * if no matching URL is found. + * + * @param aType string to match the EngineURL's type attribute + * @param aRel [optional] only return URLs that with this rel value + */ + _getURLOfType: function SRCH_ENG__getURLOfType(aType, aRel) { + for (var i = 0; i < this._urls.length; ++i) { + if (this._urls[i].type == aType && (!aRel || this._urls[i]._hasRelation(aRel))) + return this._urls[i]; + } + + return null; + }, + + _confirmAddEngine: function SRCH_SVC_confirmAddEngine() { + var stringBundle = Services.strings.createBundle(SEARCH_BUNDLE); + var titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle"); + + // Display only the hostname portion of the URL. + var dialogMessage = + stringBundle.formatStringFromName("addEngineConfirmation", + [this._name, this._uri.host], 2); + var checkboxMessage = null; + if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "noCurrentEngine", false)) + checkboxMessage = stringBundle.GetStringFromName("addEngineAsCurrentText"); + + var addButtonLabel = + stringBundle.GetStringFromName("addEngineAddButtonLabel"); + + var ps = Services.prompt; + var buttonFlags = (ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0) + + (ps.BUTTON_TITLE_CANCEL * ps.BUTTON_POS_1) + + ps.BUTTON_POS_0_DEFAULT; + + var checked = {value: false}; + // confirmEx returns the index of the button that was pressed. Since "Add" + // is button 0, we want to return the negation of that value. + var confirm = !ps.confirmEx(null, + titleMessage, + dialogMessage, + buttonFlags, + addButtonLabel, + null, null, // button 1 & 2 names not used + checkboxMessage, + checked); + + return {confirmed: confirm, useNow: checked.value}; + }, + + /** + * Handle the successful download of an engine. Initializes the engine and + * triggers parsing of the data. The engine is then flushed to disk. Notifies + * the search service once initialization is complete. + */ + _onLoad: function SRCH_ENG_onLoad(aBytes, aEngine) { + /** + * Handle an error during the load of an engine by notifying the engine's + * error callback, if any. + */ + function onError(errorCode = Ci.nsISearchInstallCallback.ERROR_UNKNOWN_FAILURE) { + // Notify the callback of the failure + if (aEngine._installCallback) { + aEngine._installCallback(errorCode); + } + } + + function promptError(strings = {}, error = undefined) { + onError(error); + + if (aEngine._engineToUpdate) { + // We're in an update, so just fail quietly + LOG("updating " + aEngine._engineToUpdate.name + " failed"); + return; + } + var brandBundle = Services.strings.createBundle(BRAND_BUNDLE); + var brandName = brandBundle.GetStringFromName("brandShortName"); + + var searchBundle = Services.strings.createBundle(SEARCH_BUNDLE); + var msgStringName = strings.error || "error_loading_engine_msg2"; + var titleStringName = strings.title || "error_loading_engine_title"; + var title = searchBundle.GetStringFromName(titleStringName); + var text = searchBundle.formatStringFromName(msgStringName, + [brandName, aEngine._location], + 2); + + Services.ww.getNewPrompter(null).alert(title, text); + } + + if (!aBytes) { + promptError(); + return; + } + + var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. + createInstance(Ci.nsIDOMParser); + var doc = parser.parseFromBuffer(aBytes, aBytes.length, "text/xml"); + aEngine._data = doc.documentElement; + + try { + // Initialize the engine from the obtained data + aEngine._initFromData(); + } catch (ex) { + LOG("_onLoad: Failed to init engine!\n" + ex); + // Report an error to the user + promptError(); + return; + } + + if (aEngine._engineToUpdate) { + let engineToUpdate = aEngine._engineToUpdate.wrappedJSObject; + + // Make this new engine use the old engine's shortName, and preserve + // metadata. + aEngine._shortName = engineToUpdate._shortName; + Object.keys(engineToUpdate._metaData).forEach(key => { + aEngine.setAttr(key, engineToUpdate.getAttr(key)); + }); + aEngine._loadPath = engineToUpdate._loadPath; + + // Keep track of the last modified date, so that we can make conditional + // requests for future updates. + aEngine.setAttr("updatelastmodified", (new Date()).toUTCString()); + + // Set the new engine's icon, if it doesn't yet have one. + if (!aEngine._iconURI && engineToUpdate._iconURI) + aEngine._iconURI = engineToUpdate._iconURI; + } else { + // Check that when adding a new engine (e.g., not updating an + // existing one), a duplicate engine does not already exist. + if (Services.search.getEngineByName(aEngine.name)) { + // If we're confirming the engine load, then display a "this is a + // duplicate engine" prompt; otherwise, fail silently. + if (aEngine._confirm) { + promptError({ error: "error_duplicate_engine_msg", + title: "error_invalid_engine_title" + }, Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); + } else { + onError(Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); + } + LOG("_onLoad: duplicate engine found, bailing"); + return; + } + + // If requested, confirm the addition now that we have the title. + // This property is only ever true for engines added via + // nsIBrowserSearchService::addEngine. + if (aEngine._confirm) { + var confirmation = aEngine._confirmAddEngine(); + LOG("_onLoad: confirm is " + confirmation.confirmed + + "; useNow is " + confirmation.useNow); + if (!confirmation.confirmed) { + onError(); + return; + } + aEngine._useNow = confirmation.useNow; + } + + aEngine._shortName = sanitizeName(aEngine.name); + aEngine._loadPath = aEngine.getAnonymizedLoadPath(null, aEngine._uri); + aEngine.setAttr("loadPathHash", getVerificationHash(aEngine._loadPath)); + } + + // Notify the search service of the successful load. It will deal with + // updates by checking aEngine._engineToUpdate. + notifyAction(aEngine, SEARCH_ENGINE_LOADED); + + // Notify the callback if needed + if (aEngine._installCallback) { + aEngine._installCallback(); + } + }, + + /** + * Creates a key by serializing an object that contains the icon's width + * and height. + * + * @param aWidth + * Width of the icon. + * @param aHeight + * Height of the icon. + * @returns key string + */ + _getIconKey: function SRCH_ENG_getIconKey(aWidth, aHeight) { + let keyObj = { + width: aWidth, + height: aHeight + }; + + return JSON.stringify(keyObj); + }, + + /** + * Add an icon to the icon map used by getIconURIBySize() and getIcons(). + * + * @param aWidth + * Width of the icon. + * @param aHeight + * Height of the icon. + * @param aURISpec + * String with the icon's URI. + */ + _addIconToMap: function SRCH_ENG_addIconToMap(aWidth, aHeight, aURISpec) { + if (aWidth == 16 && aHeight == 16) { + // The 16x16 icon is stored in _iconURL, we don't need to store it twice. + return; + } + + // Use an object instead of a Map() because it needs to be serializable. + this._iconMapObj = this._iconMapObj || {}; + let key = this._getIconKey(aWidth, aHeight); + this._iconMapObj[key] = aURISpec; + }, + + /** + * Sets the .iconURI property of the engine. If both aWidth and aHeight are + * provided an entry will be added to _iconMapObj that will enable accessing + * icon's data through getIcons() and getIconURIBySize() APIs. + * + * @param aIconURL + * A URI string pointing to the engine's icon. Must have a http[s], + * ftp, or data scheme. Icons with HTTP[S] or FTP schemes will be + * downloaded and converted to data URIs for storage in the engine + * XML files, if the engine is not readonly. + * @param aIsPreferred + * Whether or not this icon is to be preferred. Preferred icons can + * override non-preferred icons. + * @param aWidth (optional) + * Width of the icon. + * @param aHeight (optional) + * Height of the icon. + */ + _setIcon: function SRCH_ENG_setIcon(aIconURL, aIsPreferred, aWidth, aHeight) { + var uri = makeURI(aIconURL); + + // Ignore bad URIs + if (!uri) + return; + + LOG("_setIcon: Setting icon url \"" + limitURILength(uri.spec) + "\" for engine \"" + + this.name + "\"."); + // Only accept remote icons from http[s] or ftp + switch (uri.scheme) { + case "resource": + case "chrome": + // We only allow chrome and resource icon URLs for built-in search engines + if (!this._isDefault) { + return; + } + // Fall through to the data case + case "data": + if (!this._hasPreferredIcon || aIsPreferred) { + this._iconURI = uri; + notifyAction(this, SEARCH_ENGINE_CHANGED); + this._hasPreferredIcon = aIsPreferred; + } + + if (aWidth && aHeight) { + this._addIconToMap(aWidth, aHeight, aIconURL) + } + break; + case "http": + case "https": + case "ftp": + LOG("_setIcon: Downloading icon: \"" + uri.spec + + "\" for engine: \"" + this.name + "\""); + var chan = NetUtil.newChannel({ + uri: uri, + loadUsingSystemPrincipal: true + }); + + let iconLoadCallback = function (aByteArray, aEngine) { + // This callback may run after we've already set a preferred icon, + // so check again. + if (aEngine._hasPreferredIcon && !aIsPreferred) + return; + + if (!aByteArray || aByteArray.length > MAX_ICON_SIZE) { + LOG("iconLoadCallback: load failed, or the icon was too large!"); + return; + } + + let type = chan.contentType; + if (!type.startsWith("image/")) + type = "image/x-icon"; + let dataURL = "data:" + type + ";base64," + + btoa(String.fromCharCode.apply(null, aByteArray)); + + aEngine._iconURI = makeURI(dataURL); + + if (aWidth && aHeight) { + aEngine._addIconToMap(aWidth, aHeight, dataURL) + } + + notifyAction(aEngine, SEARCH_ENGINE_CHANGED); + aEngine._hasPreferredIcon = aIsPreferred; + }; + + // If we're currently acting as an "update engine", then the callback + // should set the icon on the engine we're updating and not us, since + // |this| might be gone by the time the callback runs. + var engineToSet = this._engineToUpdate || this; + + var listener = new loadListener(chan, engineToSet, iconLoadCallback); + chan.notificationCallbacks = listener; + chan.asyncOpen2(listener); + break; + } + }, + + /** + * Initialize this Engine object from the collected data. + */ + _initFromData: function SRCH_ENG_initFromData() { + ENSURE_WARN(this._data, "Can't init an engine with no data!", + Cr.NS_ERROR_UNEXPECTED); + + // Ensure we have a supported engine type before attempting to parse it. + let element = this._data; + if ((element.localName == MOZSEARCH_LOCALNAME && + element.namespaceURI == MOZSEARCH_NS_10) || + (element.localName == OPENSEARCH_LOCALNAME && + OPENSEARCH_NAMESPACES.indexOf(element.namespaceURI) != -1)) { + LOG("_init: Initing search plugin from " + this._location); + + this._parse(); + + } else + FAIL(this._location + " is not a valid search plugin.", Cr.NS_ERROR_FAILURE); + + // No need to keep a ref to our data (which in some cases can be a document + // element) past this point + this._data = null; + }, + + /** + * Initialize this Engine object from a collection of metadata. + */ + _initFromMetadata: function SRCH_ENG_initMetaData(aName, aIconURL, aAlias, + aDescription, aMethod, + aTemplate, aExtensionID) { + ENSURE_WARN(!this._readOnly, + "Can't call _initFromMetaData on a readonly engine!", + Cr.NS_ERROR_FAILURE); + + this._urls.push(new EngineURL(URLTYPE_SEARCH_HTML, aMethod, aTemplate)); + + this._name = aName; + this.alias = aAlias; + this._description = aDescription; + this._setIcon(aIconURL, true); + this._extensionID = aExtensionID; + }, + + /** + * Extracts data from an OpenSearch URL element and creates an EngineURL + * object which is then added to the engine's list of URLs. + * + * @throws NS_ERROR_FAILURE if a URL object could not be created. + * + * @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag. + * @see EngineURL() + */ + _parseURL: function SRCH_ENG_parseURL(aElement) { + var type = aElement.getAttribute("type"); + // According to the spec, method is optional, defaulting to "GET" if not + // specified + var method = aElement.getAttribute("method") || "GET"; + var template = aElement.getAttribute("template"); + var resultDomain = aElement.getAttribute("resultdomain"); + + try { + var url = new EngineURL(type, method, template, resultDomain); + } catch (ex) { + FAIL("_parseURL: failed to add " + template + " as a URL", + Cr.NS_ERROR_FAILURE); + } + + if (aElement.hasAttribute("rel")) + url.rels = aElement.getAttribute("rel").toLowerCase().split(/\s+/); + + for (var i = 0; i < aElement.childNodes.length; ++i) { + var param = aElement.childNodes[i]; + if (param.localName == "Param") { + try { + url.addParam(param.getAttribute("name"), param.getAttribute("value")); + } catch (ex) { + // Ignore failure + LOG("_parseURL: Url element has an invalid param"); + } + } else if (param.localName == "MozParam" && + // We only support MozParams for default search engines + this._isDefault) { + var value; + let condition = param.getAttribute("condition"); + + // MozParams must have a condition to be valid + if (!condition) { + let engineLoc = this._location; + let paramName = param.getAttribute("name"); + LOG("_parseURL: MozParam (" + paramName + ") without a condition attribute found parsing engine: " + engineLoc); + continue; + } + + switch (condition) { + case "purpose": + url.addParam(param.getAttribute("name"), + param.getAttribute("value"), + param.getAttribute("purpose")); + // _addMozParam is not needed here since it can be serialized fine without. _addMozParam + // also requires a unique "name" which is not normally the case when @purpose is used. + break; + case "pref": + try { + value = getMozParamPref(param.getAttribute("pref"), value); + url.addParam(param.getAttribute("name"), value); + url._addMozParam({"pref": param.getAttribute("pref"), + "name": param.getAttribute("name"), + "condition": "pref"}); + } catch (e) { } + break; + default: + let engineLoc = this._location; + let paramName = param.getAttribute("name"); + LOG("_parseURL: MozParam (" + paramName + ") has an unknown condition: " + condition + ". Found parsing engine: " + engineLoc); + break; + } + } + } + + this._urls.push(url); + }, + + /** + * Get the icon from an OpenSearch Image element. + * @see http://opensearch.a9.com/spec/1.1/description/#image + */ + _parseImage: function SRCH_ENG_parseImage(aElement) { + LOG("_parseImage: Image textContent: \"" + limitURILength(aElement.textContent) + "\""); + + let width = parseInt(aElement.getAttribute("width"), 10); + let height = parseInt(aElement.getAttribute("height"), 10); + let isPrefered = width == 16 && height == 16; + + if (isNaN(width) || isNaN(height) || width <= 0 || height <=0) { + LOG("OpenSearch image element must have positive width and height."); + return; + } + + this._setIcon(aElement.textContent, isPrefered, width, height); + }, + + /** + * Extract search engine information from the collected data to initialize + * the engine object. + */ + _parse: function SRCH_ENG_parse() { + var doc = this._data; + + // The OpenSearch spec sets a default value for the input encoding. + this._queryCharset = OS_PARAM_INPUT_ENCODING_DEF; + + for (var i = 0; i < doc.childNodes.length; ++i) { + var child = doc.childNodes[i]; + switch (child.localName) { + case "ShortName": + this._name = child.textContent; + break; + case "Description": + this._description = child.textContent; + break; + case "Url": + try { + this._parseURL(child); + } catch (ex) { + // Parsing of the element failed, just skip it. + LOG("_parse: failed to parse URL child: " + ex); + } + break; + case "Image": + this._parseImage(child); + break; + case "InputEncoding": + this._queryCharset = child.textContent.toUpperCase(); + break; + + // Non-OpenSearch elements + case "SearchForm": + this._searchForm = child.textContent; + break; + case "UpdateUrl": + this._updateURL = child.textContent; + break; + case "UpdateInterval": + this._updateInterval = parseInt(child.textContent); + break; + case "IconUpdateUrl": + this._iconUpdateURL = child.textContent; + break; + case "ExtensionID": + this._extensionID = child.textContent; + break; + } + } + if (!this.name || (this._urls.length == 0)) + FAIL("_parse: No name, or missing URL!", Cr.NS_ERROR_FAILURE); + if (!this.supportsResponseType(URLTYPE_SEARCH_HTML)) + FAIL("_parse: No text/html result type!", Cr.NS_ERROR_FAILURE); + }, + + /** + * Init from a JSON record. + **/ + _initWithJSON: function SRCH_ENG__initWithJSON(aJson) { + this._name = aJson._name; + this._shortName = aJson._shortName; + this._loadPath = aJson._loadPath; + this._description = aJson.description; + this._hasPreferredIcon = aJson._hasPreferredIcon == undefined; + this._queryCharset = aJson.queryCharset || DEFAULT_QUERY_CHARSET; + this.__searchForm = aJson.__searchForm; + this._updateInterval = aJson._updateInterval || null; + this._updateURL = aJson._updateURL || null; + this._iconUpdateURL = aJson._iconUpdateURL || null; + this._readOnly = aJson._readOnly == undefined; + this._iconURI = makeURI(aJson._iconURL); + this._iconMapObj = aJson._iconMapObj; + this._metaData = aJson._metaData || {}; + if (aJson.filePath) { + this._filePath = aJson.filePath; + } + if (aJson.dirPath) { + this._dirPath = aJson.dirPath; + this._dirLastModifiedTime = aJson.dirLastModifiedTime; + } + if (aJson.extensionID) { + this._extensionID = aJson.extensionID; + } + for (let i = 0; i < aJson._urls.length; ++i) { + let url = aJson._urls[i]; + let engineURL = new EngineURL(url.type || URLTYPE_SEARCH_HTML, + url.method || "GET", url.template, + url.resultDomain || undefined); + engineURL._initWithJSON(url, this); + this._urls.push(engineURL); + } + }, + + /** + * Creates a JavaScript object that represents this engine. + * @returns An object suitable for serialization as JSON. + **/ + toJSON: function SRCH_ENG_toJSON() { + var json = { + _name: this._name, + _shortName: this._shortName, + _loadPath: this._loadPath, + description: this.description, + __searchForm: this.__searchForm, + _iconURL: this._iconURL, + _iconMapObj: this._iconMapObj, + _metaData: this._metaData, + _urls: this._urls + }; + + if (this._updateInterval) + json._updateInterval = this._updateInterval; + if (this._updateURL) + json._updateURL = this._updateURL; + if (this._iconUpdateURL) + json._iconUpdateURL = this._iconUpdateURL; + if (!this._hasPreferredIcon) + json._hasPreferredIcon = this._hasPreferredIcon; + if (this.queryCharset != DEFAULT_QUERY_CHARSET) + json.queryCharset = this.queryCharset; + if (!this._readOnly) + json._readOnly = this._readOnly; + if (this._filePath) { + // File path is stored so that we can remove legacy xml files + // from the profile if the user removes the engine. + json.filePath = this._filePath; + } + if (this._dirPath) { + // The directory path is only stored for extension-shipped engines, + // it's used to invalidate the cache. + json.dirPath = this._dirPath; + json.dirLastModifiedTime = this._dirLastModifiedTime; + } + if (this._extensionID) { + json.extensionID = this._extensionID; + } + + return json; + }, + + setAttr(name, val) { + this._metaData[name] = val; + }, + + getAttr(name) { + return this._metaData[name] || undefined; + }, + + // nsISearchEngine + get alias() { + return this.getAttr("alias"); + }, + set alias(val) { + var value = val ? val.trim() : null; + this.setAttr("alias", value); + notifyAction(this, SEARCH_ENGINE_CHANGED); + }, + + /** + * Return the built-in identifier of app-provided engines. + * + * Note that this identifier is substantially similar to _id, with the + * following exceptions: + * + * * There is no trailing file extension. + * * There is no [app] prefix. + * + * @return a string identifier, or null. + */ + get identifier() { + // No identifier if If the engine isn't app-provided + return this._isDefault ? this._shortName : null; + }, + + get description() { + return this._description; + }, + + get hidden() { + return this.getAttr("hidden") || false; + }, + set hidden(val) { + var value = !!val; + if (value != this.hidden) { + this.setAttr("hidden", value); + notifyAction(this, SEARCH_ENGINE_CHANGED); + } + }, + + get iconURI() { + if (this._iconURI) + return this._iconURI; + return null; + }, + + get _iconURL() { + if (!this._iconURI) + return ""; + return this._iconURI.spec; + }, + + // Where the engine is being loaded from: will return the URI's spec if the + // engine is being downloaded and does not yet have a file. This is only used + // for logging and error messages. + get _location() { + if (this._uri) + return this._uri.spec; + + return this._loadPath; + }, + + // This indicates where we found the .xml file to load the engine, + // and attempts to hide user-identifiable data (such as username). + getAnonymizedLoadPath(file, uri) { + /* Examples of expected output: + * jar:[app]/omni.ja!browser/engine.xml + * 'browser' here is the name of the chrome package, not a folder. + * [profile]/searchplugins/engine.xml + * [distribution]/searchplugins/common/engine.xml + * [other]/engine.xml + */ + + const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD"; + const NS_APP_USER_PROFILE_50_DIR = "ProfD"; + const XRE_APP_DISTRIBUTION_DIR = "XREAppDist"; + + const knownDirs = { + app: NS_XPCOM_CURRENT_PROCESS_DIR, + profile: NS_APP_USER_PROFILE_50_DIR, + distribution: XRE_APP_DISTRIBUTION_DIR + }; + + let leafName = this._shortName; + if (!leafName) + return "null"; + leafName += ".xml"; + + let prefix = "", suffix = ""; + if (!file) { + if (uri.schemeIs("resource")) { + uri = makeURI(Services.io.getProtocolHandler("resource") + .QueryInterface(Ci.nsISubstitutingProtocolHandler) + .resolveURI(uri)); + } + let scheme = uri.scheme; + let packageName = ""; + if (scheme == "chrome") { + packageName = uri.hostPort; + uri = gChromeReg.convertChromeURL(uri); + } + + if (AppConstants.platform == "android") { + // On Android the omni.ja file isn't at the same path as the binary + // used to start the process. We tweak the path here so that the code + // shared with Desktop will correctly identify files from the omni.ja + // file as coming from the [app] folder. + let appPath = Services.io.getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler) + .getSubstitution("android"); + if (appPath) { + appPath = appPath.spec; + let spec = uri.spec; + if (spec.includes(appPath)) { + let appURI = Services.io.newFileURI(getDir(knownDirs["app"])); + uri = NetUtil.newURI(spec.replace(appPath, appURI.spec)); + } + } + } + + if (uri instanceof Ci.nsINestedURI) { + prefix = "jar:"; + suffix = "!" + packageName + "/" + leafName; + uri = uri.innermostURI; + } + if (uri instanceof Ci.nsIFileURL) { + file = uri.file; + } else { + let path = "[" + scheme + "]"; + if (/^(?:https?|ftp)$/.test(scheme)) { + path += uri.host; + } + return path + "/" + leafName; + } + } + + let id; + let enginePath = file.path; + + for (let key in knownDirs) { + let path; + try { + path = getDir(knownDirs[key]).path; + } catch (e) { + // Getting XRE_APP_DISTRIBUTION_DIR throws during unit tests. + continue; + } + if (enginePath.startsWith(path)) { + id = "[" + key + "]" + enginePath.slice(path.length).replace(/\\/g, "/"); + break; + } + } + + // If the folder doesn't have a known ancestor, don't record its path to + // avoid leaking user identifiable data. + if (!id) + id = "[other]/" + file.leafName; + + return prefix + id + suffix; + }, + + get _isDefault() { + // If we don't have a shortName, the engine is being parsed from a + // downloaded file, so this can't be a default engine. + if (!this._shortName) + return false; + + // An engine is a default one if we initially loaded it from the application + // or distribution directory. + if (/^(?:jar:)?(?:\[app\]|\[distribution\])/.test(this._loadPath)) + return true; + + // If we are using a non-default locale or in the xpcshell test case, + // we'll accept as a 'default' engine anything that has been registered at + // resource://search-plugins/ even if the file doesn't come from the + // application folder. If not, skip costly additional checks. + if (!Services.prefs.prefHasUserValue(LOCALE_PREF) && + !gEnvironment.get("XPCSHELL_TEST_PROFILE_DIR")) + return false; + + // Some xpcshell tests use the search service without registering + // resource://search-plugins/. + if (!Services.io.getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler) + .hasSubstitution("search-plugins")) + return false; + + let uri = makeURI(APP_SEARCH_PREFIX + this._shortName + ".xml"); + if (this.getAnonymizedLoadPath(null, uri) == this._loadPath) { + // This isn't a real default engine, but it's very close. + LOG("_isDefault, pretending " + this._loadPath + " is a default engine"); + return true; + } + + return false; + }, + + get _hasUpdates() { + // Whether or not the engine has an update URL + let selfURL = this._getURLOfType(URLTYPE_OPENSEARCH, "self"); + return !!(this._updateURL || this._iconUpdateURL || selfURL); + }, + + get name() { + return this._name; + }, + + get searchForm() { + return this._getSearchFormWithPurpose(); + }, + + _getSearchFormWithPurpose(aPurpose = "") { + // First look for a <Url rel="searchform"> + var searchFormURL = this._getURLOfType(URLTYPE_SEARCH_HTML, "searchform"); + if (searchFormURL) { + let submission = searchFormURL.getSubmission("", this, aPurpose); + + // If the rel=searchform URL is not type="get" (i.e. has postData), + // ignore it, since we can only return a URL. + if (!submission.postData) + return submission.uri.spec; + } + + if (!this._searchForm) { + // No SearchForm specified in the engine definition file, use the prePath + // (e.g. https://foo.com for https://foo.com/search.php?q=bar). + var htmlUrl = this._getURLOfType(URLTYPE_SEARCH_HTML); + ENSURE_WARN(htmlUrl, "Engine has no HTML URL!", Cr.NS_ERROR_UNEXPECTED); + this._searchForm = makeURI(htmlUrl.template).prePath; + } + + return ParamSubstitution(this._searchForm, "", this); + }, + + get queryCharset() { + if (this._queryCharset) + return this._queryCharset; + return this._queryCharset = "windows-1252"; // the default + }, + + // from nsISearchEngine + addParam: function SRCH_ENG_addParam(aName, aValue, aResponseType) { + if (!aName || (aValue == null)) + FAIL("missing name or value for nsISearchEngine::addParam!"); + ENSURE_WARN(!this._readOnly, + "called nsISearchEngine::addParam on a read-only engine!", + Cr.NS_ERROR_FAILURE); + if (!aResponseType) + aResponseType = URLTYPE_SEARCH_HTML; + + var url = this._getURLOfType(aResponseType); + if (!url) + FAIL("Engine object has no URL for response type " + aResponseType, + Cr.NS_ERROR_FAILURE); + + url.addParam(aName, aValue); + }, + + get _defaultMobileResponseType() { + let type = URLTYPE_SEARCH_HTML; + + let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); + let isTablet = sysInfo.get("tablet"); + if (isTablet && this.supportsResponseType("application/x-moz-tabletsearch")) { + // Check for a tablet-specific search URL override + type = "application/x-moz-tabletsearch"; + } else if (!isTablet && this.supportsResponseType("application/x-moz-phonesearch")) { + // Check for a phone-specific search URL override + type = "application/x-moz-phonesearch"; + } + + delete this._defaultMobileResponseType; + return this._defaultMobileResponseType = type; + }, + + get _isWhiteListed() { + let url = this._getURLOfType(URLTYPE_SEARCH_HTML).template; + let hostname = makeURI(url).host; + let whitelist = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF) + .getCharPref("reset.whitelist") + .split(","); + if (whitelist.includes(hostname)) { + LOG("The hostname " + hostname + " is white listed, " + + "we won't show the search reset prompt"); + return true; + } + + return false; + }, + + // from nsISearchEngine + getSubmission: function SRCH_ENG_getSubmission(aData, aResponseType, aPurpose) { + if (!aResponseType) { + aResponseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : + URLTYPE_SEARCH_HTML; + } + + if (aResponseType == URLTYPE_SEARCH_HTML && + Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).getBoolPref("reset.enabled") && + this.name == Services.search.currentEngine.name && + !this._isDefault && + this.name != Services.search.originalDefaultEngine.name && + (!this.getAttr("loadPathHash") || + this.getAttr("loadPathHash") != getVerificationHash(this._loadPath)) && + !this._isWhiteListed) { + let url = "about:searchreset"; + let data = []; + if (aData) + data.push("data=" + encodeURIComponent(aData)); + if (aPurpose) + data.push("purpose=" + aPurpose); + if (data.length) + url += "?" + data.join("&"); + return new Submission(makeURI(url)); + } + + var url = this._getURLOfType(aResponseType); + + if (!url) + return null; + + if (!aData) { + // Return a dummy submission object with our searchForm attribute + return new Submission(makeURI(this._getSearchFormWithPurpose(aPurpose))); + } + + LOG("getSubmission: In data: \"" + aData + "\"; Purpose: \"" + aPurpose + "\""); + var data = ""; + try { + data = gTextToSubURI.ConvertAndEscape(this.queryCharset, aData); + } catch (ex) { + LOG("getSubmission: Falling back to default queryCharset!"); + data = gTextToSubURI.ConvertAndEscape(DEFAULT_QUERY_CHARSET, aData); + } + LOG("getSubmission: Out data: \"" + data + "\""); + return url.getSubmission(data, this, aPurpose); + }, + + // from nsISearchEngine + supportsResponseType: function SRCH_ENG_supportsResponseType(type) { + return (this._getURLOfType(type) != null); + }, + + // from nsISearchEngine + getResultDomain: function SRCH_ENG_getResultDomain(aResponseType) { + if (!aResponseType) { + aResponseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : + URLTYPE_SEARCH_HTML; + } + + LOG("getResultDomain: responseType: \"" + aResponseType + "\""); + + let url = this._getURLOfType(aResponseType); + if (url) + return url.resultDomain; + return ""; + }, + + /** + * Returns URL parsing properties used by _buildParseSubmissionMap. + */ + getURLParsingInfo: function () { + let responseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : + URLTYPE_SEARCH_HTML; + + LOG("getURLParsingInfo: responseType: \"" + responseType + "\""); + + let url = this._getURLOfType(responseType); + if (!url || url.method != "GET") { + return null; + } + + let termsParameterName = url._getTermsParameterName(); + if (!termsParameterName) { + return null; + } + + let templateUrl = NetUtil.newURI(url.template).QueryInterface(Ci.nsIURL); + return { + mainDomain: templateUrl.host, + path: templateUrl.filePath.toLowerCase(), + termsParameterName: termsParameterName, + }; + }, + + // nsISupports + QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchEngine]), + + get wrappedJSObject() { + return this; + }, + + /** + * Returns a string with the URL to an engine's icon matching both width and + * height. Returns null if icon with specified dimensions is not found. + * + * @param width + * Width of the requested icon. + * @param height + * Height of the requested icon. + */ + getIconURLBySize: function SRCH_ENG_getIconURLBySize(aWidth, aHeight) { + if (aWidth == 16 && aHeight == 16) + return this._iconURL; + + if (!this._iconMapObj) + return null; + + let key = this._getIconKey(aWidth, aHeight); + if (key in this._iconMapObj) { + return this._iconMapObj[key]; + } + return null; + }, + + /** + * Gets an array of all available icons. Each entry is an object with + * width, height and url properties. width and height are numeric and + * represent the icon's dimensions. url is a string with the URL for + * the icon. + */ + getIcons: function SRCH_ENG_getIcons() { + let result = []; + if (this._iconURL) + result.push({width: 16, height: 16, url: this._iconURL}); + + if (!this._iconMapObj) + return result; + + for (let key of Object.keys(this._iconMapObj)) { + let iconSize = JSON.parse(key); + result.push({ + width: iconSize.width, + height: iconSize.height, + url: this._iconMapObj[key] + }); + } + + return result; + }, + + /** + * Opens a speculative connection to the engine's search URI + * (and suggest URI, if different) to reduce request latency + * + * @param options + * An object that must contain the following fields: + * {window} the content window for the window performing the search + * + * @throws NS_ERROR_INVALID_ARG if options is omitted or lacks required + * elemeents + */ + speculativeConnect: function SRCH_ENG_speculativeConnect(options) { + if (!options || !options.window) { + Cu.reportError("invalid options arg passed to nsISearchEngine.speculativeConnect"); + throw Cr.NS_ERROR_INVALID_ARG; + } + let connector = + Services.io.QueryInterface(Components.interfaces.nsISpeculativeConnect); + + let searchURI = this.getSubmission("dummy").uri; + + let callbacks = options.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) + .getInterface(Components.interfaces.nsIWebNavigation) + .QueryInterface(Components.interfaces.nsILoadContext); + + connector.speculativeConnect(searchURI, callbacks); + + if (this.supportsResponseType(URLTYPE_SUGGEST_JSON)) { + let suggestURI = this.getSubmission("dummy", URLTYPE_SUGGEST_JSON).uri; + if (suggestURI.prePath != searchURI.prePath) + connector.speculativeConnect(suggestURI, callbacks); + } + }, +}; + +// nsISearchSubmission +function Submission(aURI, aPostData = null) { + this._uri = aURI; + this._postData = aPostData; +} +Submission.prototype = { + get uri() { + return this._uri; + }, + get postData() { + return this._postData; + }, + QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchSubmission]) +} + +// nsISearchParseSubmissionResult +function ParseSubmissionResult(aEngine, aTerms, aTermsOffset, aTermsLength) { + this._engine = aEngine; + this._terms = aTerms; + this._termsOffset = aTermsOffset; + this._termsLength = aTermsLength; +} +ParseSubmissionResult.prototype = { + get engine() { + return this._engine; + }, + get terms() { + return this._terms; + }, + get termsOffset() { + return this._termsOffset; + }, + get termsLength() { + return this._termsLength; + }, + QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchParseSubmissionResult]), +} + +const gEmptyParseSubmissionResult = + Object.freeze(new ParseSubmissionResult(null, "", -1, 0)); + +function executeSoon(func) { + Services.tm.mainThread.dispatch(func, Ci.nsIThread.DISPATCH_NORMAL); +} + +/** + * Check for sync initialization has completed or not. + * + * @param {aPromise} A promise. + * + * @returns the value returned by the invoked method. + * @throws NS_ERROR_ALREADY_INITIALIZED if sync initialization has completed. + */ +function checkForSyncCompletion(aPromise) { + return aPromise.then(function(aValue) { + if (gInitialized) { + throw Components.Exception("Synchronous fallback was called and has " + + "finished so no need to pursue asynchronous " + + "initialization", + Cr.NS_ERROR_ALREADY_INITIALIZED); + } + return aValue; + }); +} + +// nsIBrowserSearchService +function SearchService() { + // Replace empty LOG function with the useful one if the log pref is set. + if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "log", false)) + LOG = DO_LOG; + + this._initObservers = Promise.defer(); +} + +SearchService.prototype = { + classID: Components.ID("{7319788a-fe93-4db3-9f39-818cf08f4256}"), + + // The current status of initialization. Note that it does not determine if + // initialization is complete, only if an error has been encountered so far. + _initRV: Cr.NS_OK, + + // The boolean indicates that the initialization has started or not. + _initStarted: null, + + // Reading the JSON cache file is the first thing done during initialization. + // During the async init, we save it in a field so that if we have to do a + // sync init before the async init finishes, we can avoid reading the cache + // with sync disk I/O and handling lz4 decompression synchronously. + // This is set back to null as soon as the initialization is finished. + _cacheFileJSON: null, + + // If initialization has not been completed yet, perform synchronous + // initialization. + // Throws in case of initialization error. + _ensureInitialized: function SRCH_SVC__ensureInitialized() { + if (gInitialized) { + if (!Components.isSuccessCode(this._initRV)) { + LOG("_ensureInitialized: failure"); + throw this._initRV; + } + return; + } + + let performanceWarning = + "Search service falling back to synchronous initialization. " + + "This is generally the consequence of an add-on using a deprecated " + + "search service API."; + Deprecated.perfWarning(performanceWarning, "https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIBrowserSearchService#async_warning"); + LOG(performanceWarning); + + this._syncInit(); + if (!Components.isSuccessCode(this._initRV)) { + throw this._initRV; + } + }, + + // Synchronous implementation of the initializer. + // Used by |_ensureInitialized| as a fallback if initialization is not + // complete. + _syncInit: function SRCH_SVC__syncInit() { + LOG("_syncInit start"); + this._initStarted = true; + + let cache = this._readCacheFile(); + if (cache.metaData) + this._metaData = cache.metaData; + + try { + this._syncLoadEngines(cache); + } catch (ex) { + this._initRV = Cr.NS_ERROR_FAILURE; + LOG("_syncInit: failure loading engines: " + ex); + } + this._addObservers(); + + gInitialized = true; + this._cacheFileJSON = null; + + this._initObservers.resolve(this._initRV); + + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); + Services.telemetry.getHistogramById("SEARCH_SERVICE_INIT_SYNC").add(true); + this._recordEngineTelemetry(); + + LOG("_syncInit end"); + }, + + /** + * Asynchronous implementation of the initializer. + * + * @returns {Promise} A promise, resolved successfully if the initialization + * succeeds. + */ + _asyncInit: Task.async(function* () { + LOG("_asyncInit start"); + + // See if we have a cache file so we don't have to parse a bunch of XML. + let cache = {}; + // Not using checkForSyncCompletion here because we want to ensure we + // fetch the country code and geo specific defaults asynchronously even + // if a sync init has been forced. + cache = yield this._asyncReadCacheFile(); + + if (!gInitialized && cache.metaData) + this._metaData = cache.metaData; + + try { + yield checkForSyncCompletion(this._asyncLoadEngines(cache)); + } catch (ex) { + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + throw ex; + } + this._initRV = Cr.NS_ERROR_FAILURE; + LOG("_asyncInit: failure loading engines: " + ex); + } + this._addObservers(); + gInitialized = true; + this._cacheFileJSON = null; + this._initObservers.resolve(this._initRV); + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); + Services.telemetry.getHistogramById("SEARCH_SERVICE_INIT_SYNC").add(false); + this._recordEngineTelemetry(); + + LOG("_asyncInit: Completed _asyncInit"); + }), + + _metaData: { }, + setGlobalAttr(name, val) { + this._metaData[name] = val; + this.batchTask.disarm(); + this.batchTask.arm(); + }, + setVerifiedGlobalAttr(name, val) { + this.setGlobalAttr(name, val); + this.setGlobalAttr(name + "Hash", getVerificationHash(val)); + }, + + getGlobalAttr(name) { + return this._metaData[name] || undefined; + }, + getVerifiedGlobalAttr(name) { + let val = this.getGlobalAttr(name); + if (val && this.getGlobalAttr(name + "Hash") != getVerificationHash(val)) { + LOG("getVerifiedGlobalAttr, invalid hash for " + name); + return ""; + } + return val; + }, + + _engines: { }, + __sortedEngines: null, + _visibleDefaultEngines: [], + get _sortedEngines() { + if (!this.__sortedEngines) + return this._buildSortedEngineList(); + return this.__sortedEngines; + }, + + // Get the original Engine object that is the default for this region, + // ignoring changes the user may have subsequently made. + get originalDefaultEngine() { + let defaultEngine = this.getVerifiedGlobalAttr("searchDefault"); + if (!defaultEngine) { + let defaultPrefB = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF); + let nsIPLS = Ci.nsIPrefLocalizedString; + + try { + defaultEngine = defaultPrefB.getComplexValue("defaultenginename", nsIPLS).data; + } catch (ex) { + // If the default pref is invalid (e.g. an add-on set it to a bogus value) + // getEngineByName will just return null, which is the best we can do. + } + } + + return this.getEngineByName(defaultEngine); + }, + + resetToOriginalDefaultEngine: function SRCH_SVC__resetToOriginalDefaultEngine() { + this.currentEngine = this.originalDefaultEngine; + }, + + _buildCache: function SRCH_SVC__buildCache() { + if (this._batchTask) + this._batchTask.disarm(); + + let cache = {}; + let locale = getLocale(); + let buildID = Services.appinfo.platformBuildID; + + // Allows us to force a cache refresh should the cache format change. + cache.version = CACHE_VERSION; + // We don't want to incur the costs of stat()ing each plugin on every + // startup when the only (supported) time they will change is during + // app updates (where the buildID is obviously going to change). + // Extension-shipped plugins are the only exception to this, but their + // directories are blown away during updates, so we'll detect their changes. + cache.buildID = buildID; + cache.locale = locale; + + cache.visibleDefaultEngines = this._visibleDefaultEngines; + cache.metaData = this._metaData; + cache.engines = []; + + for (let name in this._engines) { + cache.engines.push(this._engines[name]); + } + + try { + if (!cache.engines.length) + throw "cannot write without any engine."; + + LOG("_buildCache: Writing to cache file."); + let path = OS.Path.join(OS.Constants.Path.profileDir, CACHE_FILENAME); + let data = gEncoder.encode(JSON.stringify(cache)); + let promise = OS.File.writeAtomic(path, data, {compression: "lz4", + tmpPath: path + ".tmp"}); + + promise.then( + function onSuccess() { + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, SEARCH_SERVICE_CACHE_WRITTEN); + }, + function onError(e) { + LOG("_buildCache: failure during writeAtomic: " + e); + } + ); + } catch (ex) { + LOG("_buildCache: Could not write to cache file: " + ex); + } + }, + + _syncLoadEngines: function SRCH_SVC__syncLoadEngines(cache) { + LOG("_syncLoadEngines: start"); + // See if we have a cache file so we don't have to parse a bunch of XML. + let chromeURIs = this._findJAREngines(); + + let distDirs = []; + let locations; + try { + locations = getDir(NS_APP_DISTRIBUTION_SEARCH_DIR_LIST, + Ci.nsISimpleEnumerator); + } catch (e) { + // NS_APP_DISTRIBUTION_SEARCH_DIR_LIST is defined by each app + // so this throws during unit tests (but not xpcshell tests). + locations = {hasMoreElements: () => false}; + } + while (locations.hasMoreElements()) { + let dir = locations.getNext().QueryInterface(Ci.nsIFile); + if (dir.directoryEntries.hasMoreElements()) + distDirs.push(dir); + } + + let otherDirs = []; + let userSearchDir = getDir(NS_APP_USER_SEARCH_DIR); + locations = getDir(NS_APP_SEARCH_DIR_LIST, Ci.nsISimpleEnumerator); + while (locations.hasMoreElements()) { + let dir = locations.getNext().QueryInterface(Ci.nsIFile); + if ((!cache.engines || !dir.equals(userSearchDir)) && + dir.directoryEntries.hasMoreElements()) + otherDirs.push(dir); + } + + function modifiedDir(aDir) { + return cacheOtherPaths.get(aDir.path) != aDir.lastModifiedTime; + } + + function notInCacheVisibleEngines(aEngineName) { + return cache.visibleDefaultEngines.indexOf(aEngineName) == -1; + } + + let buildID = Services.appinfo.platformBuildID; + let cacheOtherPaths = new Map(); + if (cache.engines) { + for (let engine of cache.engines) { + if (engine._dirPath) { + cacheOtherPaths.set(engine._dirPath, engine._dirLastModifiedTime); + } + } + } + + let rebuildCache = !cache.engines || + cache.version != CACHE_VERSION || + cache.locale != getLocale() || + cache.buildID != buildID || + cacheOtherPaths.size != otherDirs.length || + otherDirs.some(d => !cacheOtherPaths.has(d.path)) || + cache.visibleDefaultEngines.length != this._visibleDefaultEngines.length || + this._visibleDefaultEngines.some(notInCacheVisibleEngines) || + otherDirs.some(modifiedDir); + + if (rebuildCache) { + LOG("_loadEngines: Absent or outdated cache. Loading engines from disk."); + distDirs.forEach(this._loadEnginesFromDir, this); + + this._loadFromChromeURLs(chromeURIs); + + LOG("_loadEngines: load user-installed engines from the obsolete cache"); + this._loadEnginesFromCache(cache, true); + + otherDirs.forEach(this._loadEnginesFromDir, this); + + this._loadEnginesMetadataFromCache(cache); + this._buildCache(); + return; + } + + LOG("_loadEngines: loading from cache directories"); + this._loadEnginesFromCache(cache); + + LOG("_loadEngines: done"); + }, + + /** + * Loads engines asynchronously. + * + * @returns {Promise} A promise, resolved successfully if loading data + * succeeds. + */ + _asyncLoadEngines: Task.async(function* (cache) { + LOG("_asyncLoadEngines: start"); + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "find-jar-engines"); + let chromeURIs = + yield checkForSyncCompletion(this._asyncFindJAREngines()); + + // Get the non-empty distribution directories into distDirs... + let distDirs = []; + let locations; + try { + locations = getDir(NS_APP_DISTRIBUTION_SEARCH_DIR_LIST, + Ci.nsISimpleEnumerator); + } catch (e) { + // NS_APP_DISTRIBUTION_SEARCH_DIR_LIST is defined by each app + // so this throws during unit tests (but not xpcshell tests). + locations = {hasMoreElements: () => false}; + } + while (locations.hasMoreElements()) { + let dir = locations.getNext().QueryInterface(Ci.nsIFile); + let iterator = new OS.File.DirectoryIterator(dir.path, + { winPattern: "*.xml" }); + try { + // Add dir to distDirs if it contains any files. + yield checkForSyncCompletion(iterator.next()); + distDirs.push(dir); + } catch (ex) { + // Catch for StopIteration exception. + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + throw ex; + } + } finally { + iterator.close(); + } + } + + // Add the non-empty directories of NS_APP_SEARCH_DIR_LIST to + // otherDirs... + let otherDirs = []; + let userSearchDir = getDir(NS_APP_USER_SEARCH_DIR); + locations = getDir(NS_APP_SEARCH_DIR_LIST, Ci.nsISimpleEnumerator); + while (locations.hasMoreElements()) { + let dir = locations.getNext().QueryInterface(Ci.nsIFile); + if (cache.engines && dir.equals(userSearchDir)) + continue; + let iterator = new OS.File.DirectoryIterator(dir.path, + { winPattern: "*.xml" }); + try { + // Add dir to otherDirs if it contains any files. + yield checkForSyncCompletion(iterator.next()); + otherDirs.push(dir); + } catch (ex) { + // Catch for StopIteration exception. + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + throw ex; + } + } finally { + iterator.close(); + } + } + + let hasModifiedDir = Task.async(function* (aList) { + let modifiedDir = false; + + for (let dir of aList) { + let lastModifiedTime = cacheOtherPaths.get(dir.path); + if (!lastModifiedTime) { + continue; + } + + let info = yield OS.File.stat(dir.path); + if (lastModifiedTime != info.lastModificationDate.getTime()) { + modifiedDir = true; + break; + } + } + return modifiedDir; + }); + + function notInCacheVisibleEngines(aEngineName) { + return cache.visibleDefaultEngines.indexOf(aEngineName) == -1; + } + + let buildID = Services.appinfo.platformBuildID; + let cacheOtherPaths = new Map(); + if (cache.engines) { + for (let engine of cache.engines) { + if (engine._dirPath) { + cacheOtherPaths.set(engine._dirPath, engine._dirLastModifiedTime); + } + } + } + + let rebuildCache = !cache.engines || + cache.version != CACHE_VERSION || + cache.locale != getLocale() || + cache.buildID != buildID || + cacheOtherPaths.size != otherDirs.length || + otherDirs.some(d => !cacheOtherPaths.has(d.path)) || + cache.visibleDefaultEngines.length != this._visibleDefaultEngines.length || + this._visibleDefaultEngines.some(notInCacheVisibleEngines) || + (yield checkForSyncCompletion(hasModifiedDir(otherDirs))); + + if (rebuildCache) { + LOG("_asyncLoadEngines: Absent or outdated cache. Loading engines from disk."); + for (let loadDir of distDirs) { + let enginesFromDir = + yield checkForSyncCompletion(this._asyncLoadEnginesFromDir(loadDir)); + enginesFromDir.forEach(this._addEngineToStore, this); + } + let enginesFromURLs = + yield checkForSyncCompletion(this._asyncLoadFromChromeURLs(chromeURIs)); + enginesFromURLs.forEach(this._addEngineToStore, this); + + LOG("_asyncLoadEngines: loading user-installed engines from the obsolete cache"); + this._loadEnginesFromCache(cache, true); + + for (let loadDir of otherDirs) { + let enginesFromDir = + yield checkForSyncCompletion(this._asyncLoadEnginesFromDir(loadDir)); + enginesFromDir.forEach(this._addEngineToStore, this); + } + + this._loadEnginesMetadataFromCache(cache); + this._buildCache(); + return; + } + + LOG("_asyncLoadEngines: loading from cache directories"); + this._loadEnginesFromCache(cache); + + LOG("_asyncLoadEngines: done"); + }), + + _asyncReInit: function () { + LOG("_asyncReInit"); + // Start by clearing the initialized state, so we don't abort early. + gInitialized = false; + + Task.spawn(function* () { + try { + if (this._batchTask) { + LOG("finalizing batch task"); + let task = this._batchTask; + this._batchTask = null; + yield task.finalize(); + } + + // Clear the engines, too, so we don't stick with the stale ones. + this._engines = {}; + this.__sortedEngines = null; + this._currentEngine = null; + this._visibleDefaultEngines = []; + this._metaData = {}; + this._cacheFileJSON = null; + + // Tests that want to force a synchronous re-initialization need to + // be notified when we are done uninitializing. + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, + "uninit-complete"); + + let cache = {}; + cache = yield this._asyncReadCacheFile(); + if (!gInitialized && cache.metaData) + this._metaData = cache.metaData; + + yield this._asyncLoadEngines(cache); + + // Typically we'll re-init as a result of a pref observer, + // so signal to 'callers' that we're done. + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); + this._recordEngineTelemetry(); + gInitialized = true; + } catch (err) { + LOG("Reinit failed: " + err); + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-failed"); + } finally { + Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-complete"); + } + }.bind(this)); + }, + + /** + * Read the cache file synchronously. This also imports data from the old + * search-metadata.json file if needed. + * + * @returns A JS object containing the cached data. + */ + _readCacheFile: function SRCH_SVC__readCacheFile() { + if (this._cacheFileJSON) { + return this._cacheFileJSON; + } + + let cacheFile = getDir(NS_APP_USER_PROFILE_50_DIR); + cacheFile.append(CACHE_FILENAME); + + let stream; + try { + stream = Cc["@mozilla.org/network/file-input-stream;1"]. + createInstance(Ci.nsIFileInputStream); + stream.init(cacheFile, MODE_RDONLY, FileUtils.PERMS_FILE, 0); + + let bis = Cc["@mozilla.org/binaryinputstream;1"] + .createInstance(Ci.nsIBinaryInputStream); + bis.setInputStream(stream); + + let count = stream.available(); + let array = new Uint8Array(count); + bis.readArrayBuffer(count, array.buffer); + + let bytes = Lz4.decompressFileContent(array); + let json = JSON.parse(new TextDecoder().decode(bytes)); + if (!json.engines || !json.engines.length) + throw "no engine in the file"; + return json; + } catch (ex) { + LOG("_readCacheFile: Error reading cache file: " + ex); + } finally { + stream.close(); + } + + try { + cacheFile.leafName = "search-metadata.json"; + stream = Cc["@mozilla.org/network/file-input-stream;1"]. + createInstance(Ci.nsIFileInputStream); + stream.init(cacheFile, MODE_RDONLY, FileUtils.PERMS_FILE, 0); + let metadata = parseJsonFromStream(stream); + let json = {}; + if ("[global]" in metadata) { + LOG("_readCacheFile: migrating metadata from search-metadata.json"); + let data = metadata["[global]"]; + json.metaData = {}; + let fields = ["searchDefault", "searchDefaultHash", "searchDefaultExpir", + "current", "hash", + "visibleDefaultEngines", "visibleDefaultEnginesHash"]; + for (let field of fields) { + let name = field.toLowerCase(); + if (name in data) + json.metaData[field] = data[name]; + } + } + delete metadata["[global]"]; + json._oldMetadata = metadata; + + return json; + } catch (ex) { + LOG("_readCacheFile: failed to read old metadata: " + ex); + return {}; + } finally { + stream.close(); + } + }, + + /** + * Read the cache file asynchronously. This also imports data from the old + * search-metadata.json file if needed. + * + * @returns {Promise} A promise, resolved successfully if retrieveing data + * succeeds. + */ + _asyncReadCacheFile: Task.async(function* () { + let json; + try { + let cacheFilePath = OS.Path.join(OS.Constants.Path.profileDir, CACHE_FILENAME); + let bytes = yield OS.File.read(cacheFilePath, {compression: "lz4"}); + json = JSON.parse(new TextDecoder().decode(bytes)); + if (!json.engines || !json.engines.length) + throw "no engine in the file"; + this._cacheFileJSON = json; + } catch (ex) { + LOG("_asyncReadCacheFile: Error reading cache file: " + ex); + json = {}; + + let oldMetadata = + OS.Path.join(OS.Constants.Path.profileDir, "search-metadata.json"); + try { + let bytes = yield OS.File.read(oldMetadata); + let metadata = JSON.parse(new TextDecoder().decode(bytes)); + if ("[global]" in metadata) { + LOG("_asyncReadCacheFile: migrating metadata from search-metadata.json"); + let data = metadata["[global]"]; + json.metaData = {}; + let fields = ["searchDefault", "searchDefaultHash", "searchDefaultExpir", + "current", "hash", + "visibleDefaultEngines", "visibleDefaultEnginesHash"]; + for (let field of fields) { + let name = field.toLowerCase(); + if (name in data) + json.metaData[field] = data[name]; + } + } + delete metadata["[global]"]; + json._oldMetadata = metadata; + } catch (ex) {} + } + return json; + }), + + _batchTask: null, + get batchTask() { + if (!this._batchTask) { + let task = function taskCallback() { + LOG("batchTask: Invalidating engine cache"); + this._buildCache(); + }.bind(this); + this._batchTask = new DeferredTask(task, CACHE_INVALIDATION_DELAY); + } + return this._batchTask; + }, + + _addEngineToStore: function SRCH_SVC_addEngineToStore(aEngine) { + LOG("_addEngineToStore: Adding engine: \"" + aEngine.name + "\""); + + // See if there is an existing engine with the same name. However, if this + // engine is updating another engine, it's allowed to have the same name. + var hasSameNameAsUpdate = (aEngine._engineToUpdate && + aEngine.name == aEngine._engineToUpdate.name); + if (aEngine.name in this._engines && !hasSameNameAsUpdate) { + LOG("_addEngineToStore: Duplicate engine found, aborting!"); + return; + } + + if (aEngine._engineToUpdate) { + // We need to replace engineToUpdate with the engine that just loaded. + var oldEngine = aEngine._engineToUpdate; + + // Remove the old engine from the hash, since it's keyed by name, and our + // name might change (the update might have a new name). + delete this._engines[oldEngine.name]; + + // Hack: we want to replace the old engine with the new one, but since + // people may be holding refs to the nsISearchEngine objects themselves, + // we'll just copy over all "private" properties (those without a getter + // or setter) from one object to the other. + for (var p in aEngine) { + if (!(aEngine.__lookupGetter__(p) || aEngine.__lookupSetter__(p))) + oldEngine[p] = aEngine[p]; + } + aEngine = oldEngine; + aEngine._engineToUpdate = null; + + // Add the engine back + this._engines[aEngine.name] = aEngine; + notifyAction(aEngine, SEARCH_ENGINE_CHANGED); + } else { + // Not an update, just add the new engine. + this._engines[aEngine.name] = aEngine; + // Only add the engine to the list of sorted engines if the initial list + // has already been built (i.e. if this.__sortedEngines is non-null). If + // it hasn't, we're loading engines from disk and the sorted engine list + // will be built once we need it. + if (this.__sortedEngines) { + this.__sortedEngines.push(aEngine); + this._saveSortedEngineList(); + } + notifyAction(aEngine, SEARCH_ENGINE_ADDED); + } + + if (aEngine._hasUpdates) { + // Schedule the engine's next update, if it isn't already. + if (!aEngine.getAttr("updateexpir")) + engineUpdateService.scheduleNextUpdate(aEngine); + } + }, + + _loadEnginesMetadataFromCache: function SRCH_SVC__loadEnginesMetadataFromCache(cache) { + if (cache._oldMetadata) { + // If we have old metadata in the cache, we had no valid cache + // file and read data from search-metadata.json. + for (let name in this._engines) { + let engine = this._engines[name]; + if (engine._id && cache._oldMetadata[engine._id]) + engine._metaData = cache._oldMetadata[engine._id]; + } + return; + } + + if (!cache.engines) + return; + + for (let engine of cache.engines) { + let name = engine._name; + if (name in this._engines) { + LOG("_loadEnginesMetadataFromCache, transfering metadata for " + name); + this._engines[name]._metaData = engine._metaData; + } + } + }, + + _loadEnginesFromCache: function SRCH_SVC__loadEnginesFromCache(cache, + skipReadOnly) { + if (!cache.engines) + return; + + LOG("_loadEnginesFromCache: Loading " + + cache.engines.length + " engines from cache"); + + let skippedEngines = 0; + for (let engine of cache.engines) { + if (skipReadOnly && engine._readOnly == undefined) { + ++skippedEngines; + continue; + } + + this._loadEngineFromCache(engine); + } + + if (skippedEngines) { + LOG("_loadEnginesFromCache: skipped " + skippedEngines + " read-only engines."); + } + }, + + _loadEngineFromCache: function SRCH_SVC__loadEngineFromCache(json) { + try { + let engine = new Engine(json._shortName, json._readOnly == undefined); + engine._initWithJSON(json); + this._addEngineToStore(engine); + } catch (ex) { + LOG("Failed to load " + json._name + " from cache: " + ex); + LOG("Engine JSON: " + json.toSource()); + } + }, + + _loadEnginesFromDir: function SRCH_SVC__loadEnginesFromDir(aDir) { + LOG("_loadEnginesFromDir: Searching in " + aDir.path + " for search engines."); + + // Check whether aDir is the user profile dir + var isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR)); + + var files = aDir.directoryEntries + .QueryInterface(Ci.nsIDirectoryEnumerator); + + while (files.hasMoreElements()) { + var file = files.nextFile; + + // Ignore hidden and empty files, and directories + if (!file.isFile() || file.fileSize == 0 || file.isHidden()) + continue; + + var fileURL = NetUtil.ioService.newFileURI(file).QueryInterface(Ci.nsIURL); + var fileExtension = fileURL.fileExtension.toLowerCase(); + + if (fileExtension != "xml") { + // Not an engine + continue; + } + + var addedEngine = null; + try { + addedEngine = new Engine(file, !isInProfile); + addedEngine._initFromFile(file); + if (!isInProfile && !addedEngine._isDefault) { + addedEngine._dirPath = aDir.path; + addedEngine._dirLastModifiedTime = aDir.lastModifiedTime; + } + } catch (ex) { + LOG("_loadEnginesFromDir: Failed to load " + file.path + "!\n" + ex); + continue; + } + + this._addEngineToStore(addedEngine); + } + }, + + /** + * Loads engines from a given directory asynchronously. + * + * @param aDir the directory. + * + * @returns {Promise} A promise, resolved successfully if retrieveing data + * succeeds. + */ + _asyncLoadEnginesFromDir: Task.async(function* (aDir) { + LOG("_asyncLoadEnginesFromDir: Searching in " + aDir.path + " for search engines."); + + // Check whether aDir is the user profile dir + let isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR)); + let dirPath = aDir.path; + let iterator = new OS.File.DirectoryIterator(dirPath); + + let osfiles = yield iterator.nextBatch(); + iterator.close(); + + let engines = []; + for (let osfile of osfiles) { + if (osfile.isDir || osfile.isSymLink) + continue; + + let fileInfo = yield OS.File.stat(osfile.path); + if (fileInfo.size == 0) + continue; + + let parts = osfile.path.split("."); + if (parts.length <= 1 || (parts.pop()).toLowerCase() != "xml") { + // Not an engine + continue; + } + + let addedEngine = null; + try { + let file = new FileUtils.File(osfile.path); + addedEngine = new Engine(file, !isInProfile); + yield checkForSyncCompletion(addedEngine._asyncInitFromFile(file)); + if (!isInProfile && !addedEngine._isDefault) { + addedEngine._dirPath = dirPath; + let info = yield OS.File.stat(dirPath); + addedEngine._dirLastModifiedTime = + info.lastModificationDate.getTime(); + } + engines.push(addedEngine); + } catch (ex) { + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + throw ex; + } + LOG("_asyncLoadEnginesFromDir: Failed to load " + osfile.path + "!\n" + ex); + } + } + return engines; + }), + + _loadFromChromeURLs: function SRCH_SVC_loadFromChromeURLs(aURLs) { + aURLs.forEach(function (url) { + try { + LOG("_loadFromChromeURLs: loading engine from chrome url: " + url); + + let uri = makeURI(url); + let engine = new Engine(uri, true); + + engine._initFromURISync(uri); + + this._addEngineToStore(engine); + } catch (ex) { + LOG("_loadFromChromeURLs: failed to load engine: " + ex); + } + }, this); + }, + + /** + * Loads engines from Chrome URLs asynchronously. + * + * @param aURLs a list of URLs. + * + * @returns {Promise} A promise, resolved successfully if loading data + * succeeds. + */ + _asyncLoadFromChromeURLs: Task.async(function* (aURLs) { + let engines = []; + for (let url of aURLs) { + try { + LOG("_asyncLoadFromChromeURLs: loading engine from chrome url: " + url); + let uri = NetUtil.newURI(url); + let engine = new Engine(uri, true); + yield checkForSyncCompletion(engine._asyncInitFromURI(uri)); + engines.push(engine); + } catch (ex) { + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + throw ex; + } + LOG("_asyncLoadFromChromeURLs: failed to load engine: " + ex); + } + } + return engines; + }), + + _convertChannelToFile: function(chan) { + let fileURI = chan.URI; + while (fileURI instanceof Ci.nsIJARURI) + fileURI = fileURI.JARFile; + fileURI.QueryInterface(Ci.nsIFileURL); + + return fileURI.file; + }, + + _findJAREngines: function SRCH_SVC_findJAREngines() { + LOG("_findJAREngines: looking for engines in JARs") + + let chan = makeChannel(APP_SEARCH_PREFIX + "list.json"); + if (!chan) { + LOG("_findJAREngines: " + APP_SEARCH_PREFIX + " isn't registered"); + return []; + } + + let uris = []; + + let sis = Cc["@mozilla.org/scriptableinputstream;1"]. + createInstance(Ci.nsIScriptableInputStream); + try { + sis.init(chan.open2()); + this._parseListJSON(sis.read(sis.available()), uris); + // parseListJSON will catch its own errors, so we + // should only go into this catch if list.json + // doesn't exist + } catch (e) { + chan = makeChannel(APP_SEARCH_PREFIX + "list.txt"); + sis.init(chan.open2()); + this._parseListTxt(sis.read(sis.available()), uris); + } + return uris; + }, + + /** + * Loads jar engines asynchronously. + * + * @returns {Promise} A promise, resolved successfully if finding jar engines + * succeeds. + */ + _asyncFindJAREngines: Task.async(function* () { + LOG("_asyncFindJAREngines: looking for engines in JARs") + + let listURL = APP_SEARCH_PREFIX + "list.json"; + let chan = makeChannel(listURL); + if (!chan) { + LOG("_asyncFindJAREngines: " + APP_SEARCH_PREFIX + " isn't registered"); + return []; + } + + let uris = []; + + // Read list.json to find the engines we need to load. + let deferred = Promise.defer(); + let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. + createInstance(Ci.nsIXMLHttpRequest); + request.overrideMimeType("text/plain"); + request.onload = function(aEvent) { + deferred.resolve(aEvent.target.responseText); + }; + request.onerror = function(aEvent) { + LOG("_asyncFindJAREngines: failed to read " + listURL); + // Couldn't find list.json, try list.txt + request.onerror = function(aEvent) { + LOG("_asyncFindJAREngines: failed to read " + APP_SEARCH_PREFIX + "list.txt"); + deferred.resolve(""); + } + request.open("GET", NetUtil.newURI(APP_SEARCH_PREFIX + "list.txt").spec, true); + request.send(); + }; + request.open("GET", NetUtil.newURI(listURL).spec, true); + request.send(); + let list = yield deferred.promise; + + if (request.responseURL.endsWith(".txt")) { + this._parseListTxt(list, uris); + } else { + this._parseListJSON(list, uris); + } + return uris; + }), + + _parseListJSON: function SRCH_SVC_parseListJSON(list, uris) { + let searchSettings; + try { + searchSettings = JSON.parse(list); + } catch (e) { + LOG("failing to parse list.json: " + e); + return; + } + + let jarNames = new Set(); + for (let region in searchSettings) { + // Artifact builds use the full list.json which parses + // slightly differently + if (!("visibleDefaultEngines" in searchSettings[region])) { + continue; + } + for (let engine of searchSettings[region]["visibleDefaultEngines"]) { + jarNames.add(engine); + } + } + + // Check if we have a useable country specific list of visible default engines. + let engineNames; + let visibleDefaultEngines = this.getVerifiedGlobalAttr("visibleDefaultEngines"); + if (visibleDefaultEngines) { + engineNames = visibleDefaultEngines.split(","); + for (let engineName of engineNames) { + // If all engineName values are part of jarNames, + // then we can use the country specific list, otherwise ignore it. + // The visibleDefaultEngines string containing the name of an engine we + // don't ship indicates the server is misconfigured to answer requests + // from the specific Firefox version we are running, so ignoring the + // value altogether is safer. + if (!jarNames.has(engineName)) { + LOG("_parseListJSON: ignoring visibleDefaultEngines value because " + + engineName + " is not in the jar engines we have found"); + engineNames = null; + break; + } + } + } + + // Fallback to building a list based on the regions in the JSON + if (!engineNames || !engineNames.length) { + engineNames = searchSettings["default"]["visibleDefaultEngines"]; + } + + for (let name of engineNames) { + uris.push(APP_SEARCH_PREFIX + name + ".xml"); + } + + // Store this so that it can be used while writing the cache file. + this._visibleDefaultEngines = engineNames; + }, + + _parseListTxt: function SRCH_SVC_parseListTxt(list, uris) { + let names = list.split("\n").filter(n => !!n); + // This maps the names of our built-in engines to a boolean + // indicating whether it should be hidden by default. + let jarNames = new Map(); + for (let name of names) { + if (name.endsWith(":hidden")) { + name = name.split(":")[0]; + jarNames.set(name, true); + } else { + jarNames.set(name, false); + } + } + + // Check if we have a useable country specific list of visible default engines. + let engineNames; + let visibleDefaultEngines = this.getVerifiedGlobalAttr("visibleDefaultEngines"); + if (visibleDefaultEngines) { + engineNames = visibleDefaultEngines.split(","); + + for (let engineName of engineNames) { + // If all engineName values are part of jarNames, + // then we can use the country specific list, otherwise ignore it. + // The visibleDefaultEngines string containing the name of an engine we + // don't ship indicates the server is misconfigured to answer requests + // from the specific Firefox version we are running, so ignoring the + // value altogether is safer. + if (!jarNames.has(engineName)) { + LOG("_parseListTxt: ignoring visibleDefaultEngines value because " + + engineName + " is not in the jar engines we have found"); + engineNames = null; + break; + } + } + } + + // Fallback to building a list based on the :hidden suffixes found in list.txt. + if (!engineNames) { + engineNames = []; + for (let [name, hidden] of jarNames) { + if (!hidden) + engineNames.push(name); + } + } + + for (let name of engineNames) { + uris.push(APP_SEARCH_PREFIX + name + ".xml"); + } + + // Store this so that it can be used while writing the cache file. + this._visibleDefaultEngines = engineNames; + }, + + + _saveSortedEngineList: function SRCH_SVC_saveSortedEngineList() { + LOG("SRCH_SVC_saveSortedEngineList: starting"); + + // Set the useDB pref to indicate that from now on we should use the order + // information stored in the database. + Services.prefs.setBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", true); + + var engines = this._getSortedEngines(true); + + for (var i = 0; i < engines.length; ++i) { + engines[i].setAttr("order", i + 1); + } + + LOG("SRCH_SVC_saveSortedEngineList: done"); + }, + + _buildSortedEngineList: function SRCH_SVC_buildSortedEngineList() { + LOG("_buildSortedEngineList: building list"); + var addedEngines = { }; + this.__sortedEngines = []; + var engine; + + // If the user has specified a custom engine order, read the order + // information from the metadata instead of the default prefs. + if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", false)) { + LOG("_buildSortedEngineList: using db for order"); + + // Flag to keep track of whether or not we need to call _saveSortedEngineList. + let needToSaveEngineList = false; + + for (let name in this._engines) { + let engine = this._engines[name]; + var orderNumber = engine.getAttr("order"); + + // Since the DB isn't regularly cleared, and engine files may disappear + // without us knowing, we may already have an engine in this slot. If + // that happens, we just skip it - it will be added later on as an + // unsorted engine. + if (orderNumber && !this.__sortedEngines[orderNumber-1]) { + this.__sortedEngines[orderNumber-1] = engine; + addedEngines[engine.name] = engine; + } else { + // We need to call _saveSortedEngineList so this gets sorted out. + needToSaveEngineList = true; + } + } + + // Filter out any nulls for engines that may have been removed + var filteredEngines = this.__sortedEngines.filter(function(a) { return !!a; }); + if (this.__sortedEngines.length != filteredEngines.length) + needToSaveEngineList = true; + this.__sortedEngines = filteredEngines; + + if (needToSaveEngineList) + this._saveSortedEngineList(); + } else { + // The DB isn't being used, so just read the engine order from the prefs + var i = 0; + var engineName; + var prefName; + + try { + var extras = + Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); + + for (prefName of extras) { + engineName = Services.prefs.getCharPref(prefName); + + engine = this._engines[engineName]; + if (!engine || engine.name in addedEngines) + continue; + + this.__sortedEngines.push(engine); + addedEngines[engine.name] = engine; + } + } + catch (e) { } + + while (true) { + engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i)); + if (!engineName) + break; + + engine = this._engines[engineName]; + if (!engine || engine.name in addedEngines) + continue; + + this.__sortedEngines.push(engine); + addedEngines[engine.name] = engine; + } + } + + // Array for the remaining engines, alphabetically sorted. + let alphaEngines = []; + + for (let name in this._engines) { + let engine = this._engines[name]; + if (!(engine.name in addedEngines)) + alphaEngines.push(this._engines[engine.name]); + } + + let locale = Cc["@mozilla.org/intl/nslocaleservice;1"] + .getService(Ci.nsILocaleService) + .newLocale(getLocale()); + let collation = Cc["@mozilla.org/intl/collation-factory;1"] + .createInstance(Ci.nsICollationFactory) + .CreateCollation(locale); + const strength = Ci.nsICollation.kCollationCaseInsensitiveAscii; + let comparator = (a, b) => collation.compareString(strength, a.name, b.name); + alphaEngines.sort(comparator); + return this.__sortedEngines = this.__sortedEngines.concat(alphaEngines); + }, + + /** + * Get a sorted array of engines. + * @param aWithHidden + * True if hidden plugins should be included in the result. + */ + _getSortedEngines: function SRCH_SVC_getSorted(aWithHidden) { + if (aWithHidden) + return this._sortedEngines; + + return this._sortedEngines.filter(function (engine) { + return !engine.hidden; + }); + }, + + // nsIBrowserSearchService + init: function SRCH_SVC_init(observer) { + LOG("SearchService.init"); + let self = this; + if (!this._initStarted) { + this._initStarted = true; + Task.spawn(function* task() { + try { + // Complete initialization by calling asynchronous initializer. + yield self._asyncInit(); + } catch (ex) { + if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { + // No need to pursue asynchronous because synchronous fallback was + // called and has finished. + } else { + self._initObservers.reject(ex); + } + } + }); + } + if (observer) { + this._initObservers.promise.then( + function onSuccess() { + try { + observer.onInitComplete(self._initRV); + } catch (e) { + Cu.reportError(e); + } + }, + function onError(aReason) { + Cu.reportError("Internal error while initializing SearchService: " + aReason); + observer.onInitComplete(Components.results.NS_ERROR_UNEXPECTED); + } + ); + } + }, + + get isInitialized() { + return gInitialized; + }, + + getEngines: function SRCH_SVC_getEngines(aCount) { + this._ensureInitialized(); + LOG("getEngines: getting all engines"); + var engines = this._getSortedEngines(true); + aCount.value = engines.length; + return engines; + }, + + getVisibleEngines: function SRCH_SVC_getVisible(aCount) { + this._ensureInitialized(); + LOG("getVisibleEngines: getting all visible engines"); + var engines = this._getSortedEngines(false); + aCount.value = engines.length; + return engines; + }, + + getDefaultEngines: function SRCH_SVC_getDefault(aCount) { + this._ensureInitialized(); + function isDefault(engine) { + return engine._isDefault; + } + var engines = this._sortedEngines.filter(isDefault); + var engineOrder = {}; + var engineName; + var i = 1; + + // Build a list of engines which we have ordering information for. + // We're rebuilding the list here because _sortedEngines contain the + // current order, but we want the original order. + + // First, look at the "browser.search.order.extra" branch. + try { + var extras = Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); + + for (var prefName of extras) { + engineName = Services.prefs.getCharPref(prefName); + + if (!(engineName in engineOrder)) + engineOrder[engineName] = i++; + } + } catch (e) { + LOG("Getting extra order prefs failed: " + e); + } + + // Now look through the "browser.search.order" branch. + for (var j = 1; ; j++) { + engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + j); + if (!engineName) + break; + + if (!(engineName in engineOrder)) + engineOrder[engineName] = i++; + } + + LOG("getDefaultEngines: engineOrder: " + engineOrder.toSource()); + + function compareEngines (a, b) { + var aIdx = engineOrder[a.name]; + var bIdx = engineOrder[b.name]; + + if (aIdx && bIdx) + return aIdx - bIdx; + if (aIdx) + return -1; + if (bIdx) + return 1; + + return a.name.localeCompare(b.name); + } + engines.sort(compareEngines); + + aCount.value = engines.length; + return engines; + }, + + getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) { + this._ensureInitialized(); + return this._engines[aEngineName] || null; + }, + + getEngineByAlias: function SRCH_SVC_getEngineByAlias(aAlias) { + this._ensureInitialized(); + for (var engineName in this._engines) { + var engine = this._engines[engineName]; + if (engine && engine.alias == aAlias) + return engine; + } + return null; + }, + + addEngineWithDetails: function SRCH_SVC_addEWD(aName, aIconURL, aAlias, + aDescription, aMethod, + aTemplate, aExtensionID) { + this._ensureInitialized(); + if (!aName) + FAIL("Invalid name passed to addEngineWithDetails!"); + if (!aMethod) + FAIL("Invalid method passed to addEngineWithDetails!"); + if (!aTemplate) + FAIL("Invalid template passed to addEngineWithDetails!"); + if (this._engines[aName]) + FAIL("An engine with that name already exists!", Cr.NS_ERROR_FILE_ALREADY_EXISTS); + + var engine = new Engine(sanitizeName(aName), false); + engine._initFromMetadata(aName, aIconURL, aAlias, aDescription, + aMethod, aTemplate, aExtensionID); + engine._loadPath = "[other]addEngineWithDetails"; + this._addEngineToStore(engine); + }, + + addEngine: function SRCH_SVC_addEngine(aEngineURL, aDataType, aIconURL, + aConfirm, aCallback) { + LOG("addEngine: Adding \"" + aEngineURL + "\"."); + this._ensureInitialized(); + try { + var uri = makeURI(aEngineURL); + var engine = new Engine(uri, false); + if (aCallback) { + engine._installCallback = function (errorCode) { + try { + if (errorCode == null) + aCallback.onSuccess(engine); + else + aCallback.onError(errorCode); + } catch (ex) { + Cu.reportError("Error invoking addEngine install callback: " + ex); + } + // Clear the reference to the callback now that it's been invoked. + engine._installCallback = null; + }; + } + engine._initFromURIAndLoad(uri); + } catch (ex) { + // Drop the reference to the callback, if set + if (engine) + engine._installCallback = null; + FAIL("addEngine: Error adding engine:\n" + ex, Cr.NS_ERROR_FAILURE); + } + engine._setIcon(aIconURL, false); + engine._confirm = aConfirm; + }, + + removeEngine: function SRCH_SVC_removeEngine(aEngine) { + this._ensureInitialized(); + if (!aEngine) + FAIL("no engine passed to removeEngine!"); + + var engineToRemove = null; + for (var e in this._engines) { + if (aEngine.wrappedJSObject == this._engines[e]) + engineToRemove = this._engines[e]; + } + + if (!engineToRemove) + FAIL("removeEngine: Can't find engine to remove!", Cr.NS_ERROR_FILE_NOT_FOUND); + + if (engineToRemove == this.currentEngine) { + this._currentEngine = null; + } + + if (engineToRemove._readOnly) { + // Just hide it (the "hidden" setter will notify) and remove its alias to + // avoid future conflicts with other engines. + engineToRemove.hidden = true; + engineToRemove.alias = null; + } else { + // Remove the engine file from disk if we had a legacy file in the profile. + if (engineToRemove._filePath) { + let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); + file.persistentDescriptor = engineToRemove._filePath; + if (file.exists()) { + file.remove(false); + } + engineToRemove._filePath = null; + } + + // Remove the engine from _sortedEngines + var index = this._sortedEngines.indexOf(engineToRemove); + if (index == -1) + FAIL("Can't find engine to remove in _sortedEngines!", Cr.NS_ERROR_FAILURE); + this.__sortedEngines.splice(index, 1); + + // Remove the engine from the internal store + delete this._engines[engineToRemove.name]; + + // Since we removed an engine, we need to update the preferences. + this._saveSortedEngineList(); + } + notifyAction(engineToRemove, SEARCH_ENGINE_REMOVED); + }, + + moveEngine: function SRCH_SVC_moveEngine(aEngine, aNewIndex) { + this._ensureInitialized(); + if ((aNewIndex > this._sortedEngines.length) || (aNewIndex < 0)) + FAIL("SRCH_SVC_moveEngine: Index out of bounds!"); + if (!(aEngine instanceof Ci.nsISearchEngine)) + FAIL("SRCH_SVC_moveEngine: Invalid engine passed to moveEngine!"); + if (aEngine.hidden) + FAIL("moveEngine: Can't move a hidden engine!", Cr.NS_ERROR_FAILURE); + + var engine = aEngine.wrappedJSObject; + + var currentIndex = this._sortedEngines.indexOf(engine); + if (currentIndex == -1) + FAIL("moveEngine: Can't find engine to move!", Cr.NS_ERROR_UNEXPECTED); + + // Our callers only take into account non-hidden engines when calculating + // aNewIndex, but we need to move it in the array of all engines, so we + // need to adjust aNewIndex accordingly. To do this, we count the number + // of hidden engines in the list before the engine that we're taking the + // place of. We do this by first finding newIndexEngine (the engine that + // we were supposed to replace) and then iterating through the complete + // engine list until we reach it, increasing aNewIndex for each hidden + // engine we find on our way there. + // + // This could be further simplified by having our caller pass in + // newIndexEngine directly instead of aNewIndex. + var newIndexEngine = this._getSortedEngines(false)[aNewIndex]; + if (!newIndexEngine) + FAIL("moveEngine: Can't find engine to replace!", Cr.NS_ERROR_UNEXPECTED); + + for (var i = 0; i < this._sortedEngines.length; ++i) { + if (newIndexEngine == this._sortedEngines[i]) + break; + if (this._sortedEngines[i].hidden) + aNewIndex++; + } + + if (currentIndex == aNewIndex) + return; // nothing to do! + + // Move the engine + var movedEngine = this.__sortedEngines.splice(currentIndex, 1)[0]; + this.__sortedEngines.splice(aNewIndex, 0, movedEngine); + + notifyAction(engine, SEARCH_ENGINE_CHANGED); + + // Since we moved an engine, we need to update the preferences. + this._saveSortedEngineList(); + }, + + restoreDefaultEngines: function SRCH_SVC_resetDefaultEngines() { + this._ensureInitialized(); + for (let name in this._engines) { + let e = this._engines[name]; + // Unhide all default engines + if (e.hidden && e._isDefault) + e.hidden = false; + } + }, + + get defaultEngine() { return this.currentEngine; }, + + set defaultEngine(val) { + this.currentEngine = val; + }, + + get currentEngine() { + this._ensureInitialized(); + if (!this._currentEngine) { + let name = this.getGlobalAttr("current"); + let engine = this.getEngineByName(name); + if (engine && (this.getGlobalAttr("hash") == getVerificationHash(name) || + engine._isDefault)) { + // If the current engine is a default one, we can relax the + // verification hash check to reduce the annoyance for users who + // backup/sync their profile in custom ways. + this._currentEngine = engine; + } + if (!name) + this._currentEngine = this.originalDefaultEngine; + } + + // If the current engine is not set or hidden, we fallback... + if (!this._currentEngine || this._currentEngine.hidden) { + // first to the original default engine + let originalDefault = this.originalDefaultEngine; + if (!originalDefault || originalDefault.hidden) { + // then to the first visible engine + let firstVisible = this._getSortedEngines(false)[0]; + if (firstVisible && !firstVisible.hidden) { + this.currentEngine = firstVisible; + return firstVisible; + } + // and finally as a last resort we unhide the original default engine. + if (originalDefault) + originalDefault.hidden = false; + } + if (!originalDefault) + return null; + + // If the current engine wasn't set or was hidden, we used a fallback + // to pick a new current engine. As soon as we return it, this new + // current engine will become user-visible, so we should persist it. + // by calling the setter. + this.currentEngine = originalDefault; + } + + return this._currentEngine; + }, + + set currentEngine(val) { + this._ensureInitialized(); + // Sometimes we get wrapped nsISearchEngine objects (external XPCOM callers), + // and sometimes we get raw Engine JS objects (callers in this file), so + // handle both. + if (!(val instanceof Ci.nsISearchEngine) && !(val instanceof Engine)) + FAIL("Invalid argument passed to currentEngine setter"); + + var newCurrentEngine = this.getEngineByName(val.name); + if (!newCurrentEngine) + FAIL("Can't find engine in store!", Cr.NS_ERROR_UNEXPECTED); + + if (!newCurrentEngine._isDefault) { + // If a non default engine is being set as the current engine, ensure + // its loadPath has a verification hash. + if (!newCurrentEngine._loadPath) + newCurrentEngine._loadPath = "[other]unknown"; + let loadPathHash = getVerificationHash(newCurrentEngine._loadPath); + let currentHash = newCurrentEngine.getAttr("loadPathHash"); + if (!currentHash || currentHash != loadPathHash) { + newCurrentEngine.setAttr("loadPathHash", loadPathHash); + notifyAction(newCurrentEngine, SEARCH_ENGINE_CHANGED); + } + } + + if (newCurrentEngine == this._currentEngine) + return; + + this._currentEngine = newCurrentEngine; + + // If we change the default engine in the future, that change should impact + // users who have switched away from and then back to the build's "default" + // engine. So clear the user pref when the currentEngine is set to the + // build's default engine, so that the currentEngine getter falls back to + // whatever the default is. + let newName = this._currentEngine.name; + if (this._currentEngine == this.originalDefaultEngine) { + newName = ""; + } + + this.setGlobalAttr("current", newName); + this.setGlobalAttr("hash", getVerificationHash(newName)); + + notifyAction(this._currentEngine, SEARCH_ENGINE_DEFAULT); + notifyAction(this._currentEngine, SEARCH_ENGINE_CURRENT); + }, + + getDefaultEngineInfo() { + let result = {}; + + let engine; + try { + engine = this.defaultEngine; + } catch (e) { + // The defaultEngine getter will throw if there's no engine at all, + // which shouldn't happen unless an add-on or a test deleted all of them. + // Our preferences UI doesn't let users do that. + Cu.reportError("getDefaultEngineInfo: No default engine"); + } + + if (!engine) { + result.name = "NONE"; + } else { + if (engine.name) + result.name = engine.name; + + result.loadPath = engine._loadPath; + + let origin; + if (engine._isDefault) + origin = "default"; + else { + let currentHash = engine.getAttr("loadPathHash"); + if (!currentHash) + origin = "unverified"; + else { + let loadPathHash = getVerificationHash(engine._loadPath); + origin = currentHash == loadPathHash ? "verified" : "invalid"; + } + } + result.origin = origin; + + // For privacy, we only collect the submission URL for default engines... + let sendSubmissionURL = engine._isDefault; + + // ... or engines sorted by default near the top of the list. + if (!sendSubmissionURL) { + let extras = + Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); + + for (let prefName of extras) { + try { + if (result.name == Services.prefs.getCharPref(prefName)) { + sendSubmissionURL = true; + break; + } + } catch (e) {} + } + + let i = 0; + while (!sendSubmissionURL) { + let engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i)); + if (!engineName) + break; + if (result.name == engineName) { + sendSubmissionURL = true; + break; + } + } + } + + if (sendSubmissionURL) { + let uri = engine._getURLOfType("text/html") + .getSubmission("", engine, "searchbar").uri; + uri.userPass = ""; // Avoid reporting a username or password. + result.submissionURL = uri.spec; + } + } + + return result; + }, + + _recordEngineTelemetry: function() { + Services.telemetry.getHistogramById("SEARCH_SERVICE_ENGINE_COUNT") + .add(Object.keys(this._engines).length); + let hasUpdates = false; + let hasIconUpdates = false; + for (let name in this._engines) { + let engine = this._engines[name]; + if (engine._hasUpdates) { + hasUpdates = true; + if (engine._iconUpdateURL) { + hasIconUpdates = true; + break; + } + } + } + Services.telemetry.getHistogramById("SEARCH_SERVICE_HAS_UPDATES").add(hasUpdates); + Services.telemetry.getHistogramById("SEARCH_SERVICE_HAS_ICON_UPDATES").add(hasIconUpdates); + }, + + /** + * This map is built lazily after the available search engines change. It + * allows quick parsing of an URL representing a search submission into the + * search engine name and original terms. + * + * The keys are strings containing the domain name and lowercase path of the + * engine submission, for example "www.google.com/search". + * + * The values are objects with these properties: + * { + * engine: The associated nsISearchEngine. + * termsParameterName: Name of the URL parameter containing the search + * terms, for example "q". + * } + */ + _parseSubmissionMap: null, + + _buildParseSubmissionMap: function SRCH_SVC__buildParseSubmissionMap() { + LOG("_buildParseSubmissionMap"); + this._parseSubmissionMap = new Map(); + + // Used only while building the map, indicates which entries do not refer to + // the main domain of the engine but to an alternate domain, for example + // "www.google.fr" for the "www.google.com" search engine. + let keysOfAlternates = new Set(); + + for (let engine of this._sortedEngines) { + LOG("Processing engine: " + engine.name); + + if (engine.hidden) { + LOG("Engine is hidden."); + continue; + } + + let urlParsingInfo = engine.getURLParsingInfo(); + if (!urlParsingInfo) { + LOG("Engine does not support URL parsing."); + continue; + } + + // Store the same object on each matching map key, as an optimization. + let mapValueForEngine = { + engine: engine, + termsParameterName: urlParsingInfo.termsParameterName, + }; + + let processDomain = (domain, isAlternate) => { + let key = domain + urlParsingInfo.path; + + // Apply the logic for which main domains take priority over alternate + // domains, even if they are found later in the ordered engine list. + let existingEntry = this._parseSubmissionMap.get(key); + if (!existingEntry) { + LOG("Adding new entry: " + key); + if (isAlternate) { + keysOfAlternates.add(key); + } + } else if (!isAlternate && keysOfAlternates.has(key)) { + LOG("Overriding alternate entry: " + key + + " (" + existingEntry.engine.name + ")"); + keysOfAlternates.delete(key); + } else { + LOG("Keeping existing entry: " + key + + " (" + existingEntry.engine.name + ")"); + return; + } + + this._parseSubmissionMap.set(key, mapValueForEngine); + }; + + processDomain(urlParsingInfo.mainDomain, false); + SearchStaticData.getAlternateDomains(urlParsingInfo.mainDomain) + .forEach(d => processDomain(d, true)); + } + }, + + /** + * Checks to see if any engine has an EngineURL of type URLTYPE_SEARCH_HTML + * for this request-method, template URL, and query params. + */ + hasEngineWithURL: function(method, template, formData) { + this._ensureInitialized(); + + // Quick helper method to ensure formData filtered/sorted for compares. + let getSortedFormData = data => { + return data.filter(a => a.name && a.value).sort((a, b) => { + if (a.name > b.name) { + return 1; + } else if (b.name > a.name) { + return -1; + } else if (a.value > b.value) { + return 1; + } + return (b.value > a.value) ? -1 : 0; + }); + }; + + // Sanitize method, ensure formData is pre-sorted. + let methodUpper = method.toUpperCase(); + let sortedFormData = getSortedFormData(formData); + let sortedFormLength = sortedFormData.length; + + return this._getSortedEngines(false).some(engine => { + return engine._urls.some(url => { + // Not an engineURL match if type, method, url, #params don't match. + if (url.type != URLTYPE_SEARCH_HTML || + url.method != methodUpper || + url.template != template || + url.params.length != sortedFormLength) { + return false; + } + + // Ensure engineURL formData is pre-sorted. Then, we're + // not an engineURL match if any queryParam doesn't compare. + let sortedParams = getSortedFormData(url.params); + for (let i = 0; i < sortedFormLength; i++) { + let formData = sortedFormData[i]; + let param = sortedParams[i]; + if (param.name != formData.name || + param.value != formData.value || + param.purpose != formData.purpose) { + return false; + } + } + // Else we're a match. + return true; + }); + }); + }, + + parseSubmissionURL: function SRCH_SVC_parseSubmissionURL(aURL) { + this._ensureInitialized(); + LOG("parseSubmissionURL: Parsing \"" + aURL + "\"."); + + if (!this._parseSubmissionMap) { + this._buildParseSubmissionMap(); + } + + // Extract the elements of the provided URL first. + let soughtKey, soughtQuery; + try { + let soughtUrl = NetUtil.newURI(aURL).QueryInterface(Ci.nsIURL); + + // Exclude any URL that is not HTTP or HTTPS from the beginning. + if (soughtUrl.scheme != "http" && soughtUrl.scheme != "https") { + LOG("The URL scheme is not HTTP or HTTPS."); + return gEmptyParseSubmissionResult; + } + + // Reading these URL properties may fail and raise an exception. + soughtKey = soughtUrl.host + soughtUrl.filePath.toLowerCase(); + soughtQuery = soughtUrl.query; + } catch (ex) { + // Errors while parsing the URL or accessing the properties are not fatal. + LOG("The value does not look like a structured URL."); + return gEmptyParseSubmissionResult; + } + + // Look up the domain and path in the map to identify the search engine. + let mapEntry = this._parseSubmissionMap.get(soughtKey); + if (!mapEntry) { + LOG("No engine associated with domain and path: " + soughtKey); + return gEmptyParseSubmissionResult; + } + + // Extract the search terms from the parameter, for example "caff%C3%A8" + // from the URL "https://www.google.com/search?q=caff%C3%A8&client=firefox". + let encodedTerms = null; + for (let param of soughtQuery.split("&")) { + let equalPos = param.indexOf("="); + if (equalPos != -1 && + param.substr(0, equalPos) == mapEntry.termsParameterName) { + // This is the parameter we are looking for. + encodedTerms = param.substr(equalPos + 1); + break; + } + } + if (encodedTerms === null) { + LOG("Missing terms parameter: " + mapEntry.termsParameterName); + return gEmptyParseSubmissionResult; + } + + let length = 0; + let offset = aURL.indexOf("?") + 1; + let query = aURL.slice(offset); + // Iterate a second time over the original input string to determine the + // correct search term offset and length in the original encoding. + for (let param of query.split("&")) { + let equalPos = param.indexOf("="); + if (equalPos != -1 && + param.substr(0, equalPos) == mapEntry.termsParameterName) { + // This is the parameter we are looking for. + offset += equalPos + 1; + length = param.length - equalPos - 1; + break; + } + offset += param.length + 1; + } + + // Decode the terms using the charset defined in the search engine. + let terms; + try { + terms = gTextToSubURI.UnEscapeAndConvert( + mapEntry.engine.queryCharset, + encodedTerms.replace(/\+/g, " ")); + } catch (ex) { + // Decoding errors will cause this match to be ignored. + LOG("Parameter decoding failed. Charset: " + + mapEntry.engine.queryCharset); + return gEmptyParseSubmissionResult; + } + + LOG("Match found. Terms: " + terms); + return new ParseSubmissionResult(mapEntry.engine, terms, offset, length); + }, + + // nsIObserver + observe: function SRCH_SVC_observe(aEngine, aTopic, aVerb) { + switch (aTopic) { + case SEARCH_ENGINE_TOPIC: + switch (aVerb) { + case SEARCH_ENGINE_LOADED: + var engine = aEngine.QueryInterface(Ci.nsISearchEngine); + LOG("nsSearchService::observe: Done installation of " + engine.name + + "."); + this._addEngineToStore(engine.wrappedJSObject); + if (engine.wrappedJSObject._useNow) { + LOG("nsSearchService::observe: setting current"); + this.currentEngine = aEngine; + } + // The addition of the engine to the store always triggers an ADDED + // or a CHANGED notification, that will trigger the task below. + break; + case SEARCH_ENGINE_ADDED: + case SEARCH_ENGINE_CHANGED: + case SEARCH_ENGINE_REMOVED: + this.batchTask.disarm(); + this.batchTask.arm(); + // Invalidate the map used to parse URLs to search engines. + this._parseSubmissionMap = null; + break; + } + break; + + case QUIT_APPLICATION_TOPIC: + this._removeObservers(); + break; + + case "nsPref:changed": + if (aVerb == LOCALE_PREF) { + // Locale changed. Re-init. We rely on observers, because we can't + // return this promise to anyone. + this._asyncReInit(); + break; + } + } + }, + + // nsITimerCallback + notify: function SRCH_SVC_notify(aTimer) { + LOG("_notify: checking for updates"); + + if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update", true)) + return; + + // Our timer has expired, but unfortunately, we can't get any data from it. + // Therefore, we need to walk our engine-list, looking for expired engines + var currentTime = Date.now(); + LOG("currentTime: " + currentTime); + for (let name in this._engines) { + let engine = this._engines[name].wrappedJSObject; + if (!engine._hasUpdates) + continue; + + LOG("checking " + engine.name); + + var expirTime = engine.getAttr("updateexpir"); + LOG("expirTime: " + expirTime + "\nupdateURL: " + engine._updateURL + + "\niconUpdateURL: " + engine._iconUpdateURL); + + var engineExpired = expirTime <= currentTime; + + if (!expirTime || !engineExpired) { + LOG("skipping engine"); + continue; + } + + LOG(engine.name + " has expired"); + + engineUpdateService.update(engine); + + // Schedule the next update + engineUpdateService.scheduleNextUpdate(engine); + + } // end engine iteration + }, + + _addObservers: function SRCH_SVC_addObservers() { + if (this._observersAdded) { + // There might be a race between synchronous and asynchronous + // initialization for which we try to register the observers twice. + return; + } + this._observersAdded = true; + + Services.obs.addObserver(this, SEARCH_ENGINE_TOPIC, false); + Services.obs.addObserver(this, QUIT_APPLICATION_TOPIC, false); + + // The current stage of shutdown. Used to help analyze crash + // signatures in case of shutdown timeout. + let shutdownState = { + step: "Not started", + latestError: { + message: undefined, + stack: undefined + } + }; + OS.File.profileBeforeChange.addBlocker( + "Search service: shutting down", + () => Task.spawn(function* () { + if (this._batchTask) { + shutdownState.step = "Finalizing batched task"; + try { + yield this._batchTask.finalize(); + shutdownState.step = "Batched task finalized"; + } catch (ex) { + shutdownState.step = "Batched task failed to finalize"; + + shutdownState.latestError.message = "" + ex; + if (ex && typeof ex == "object") { + shutdownState.latestError.stack = ex.stack || undefined; + } + + // Ensure that error is reported and that it causes tests + // to fail. + Promise.reject(ex); + } + } + }.bind(this)), + + () => shutdownState + ); + }, + _observersAdded: false, + + _removeObservers: function SRCH_SVC_removeObservers() { + Services.obs.removeObserver(this, SEARCH_ENGINE_TOPIC); + Services.obs.removeObserver(this, QUIT_APPLICATION_TOPIC); + }, + + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIBrowserSearchService, + Ci.nsIObserver, + Ci.nsITimerCallback + ]) +}; + + +const SEARCH_UPDATE_LOG_PREFIX = "*** Search update: "; + +/** + * Outputs aText to the JavaScript console as well as to stdout, if the search + * logging pref (browser.search.update.log) is set to true. + */ +function ULOG(aText) { + if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update.log", false)) { + dump(SEARCH_UPDATE_LOG_PREFIX + aText + "\n"); + Services.console.logStringMessage(aText); + } +} + +var engineUpdateService = { + scheduleNextUpdate: function eus_scheduleNextUpdate(aEngine) { + var interval = aEngine._updateInterval || SEARCH_DEFAULT_UPDATE_INTERVAL; + var milliseconds = interval * 86400000; // |interval| is in days + aEngine.setAttr("updateexpir", Date.now() + milliseconds); + }, + + update: function eus_Update(aEngine) { + let engine = aEngine.wrappedJSObject; + ULOG("update called for " + aEngine._name); + if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update", true) || !engine._hasUpdates) + return; + + let testEngine = null; + let updateURL = engine._getURLOfType(URLTYPE_OPENSEARCH); + let updateURI = (updateURL && updateURL._hasRelation("self")) ? + updateURL.getSubmission("", engine).uri : + makeURI(engine._updateURL); + if (updateURI) { + if (engine._isDefault && !updateURI.schemeIs("https")) { + ULOG("Invalid scheme for default engine update"); + return; + } + + ULOG("updating " + engine.name + " from " + updateURI.spec); + testEngine = new Engine(updateURI, false); + testEngine._engineToUpdate = engine; + testEngine._initFromURIAndLoad(updateURI); + } else + ULOG("invalid updateURI"); + + if (engine._iconUpdateURL) { + // If we're updating the engine too, use the new engine object, + // otherwise use the existing engine object. + (testEngine || engine)._setIcon(engine._iconUpdateURL, true); + } + } +}; + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SearchService]); diff --git a/application/basilisk/components/search/service/nsSearchSuggestions.js b/application/basilisk/components/search/service/nsSearchSuggestions.js new file mode 100644 index 000000000..a05d8b4b4 --- /dev/null +++ b/application/basilisk/components/search/service/nsSearchSuggestions.js @@ -0,0 +1,197 @@ +/* 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/. */ + +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "SearchSuggestionController", + "resource://gre/modules/SearchSuggestionController.jsm"); + +/** + * SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch + * and can collect results for a given search by using this._suggestionController. + * We do it this way since the AutoCompleteController in Mozilla requires a + * unique XPCOM Service for every search provider, even if the logic for two + * providers is identical. + * @constructor + */ +function SuggestAutoComplete() { + this._init(); +} +SuggestAutoComplete.prototype = { + + _init: function() { + this._suggestionController = new SearchSuggestionController(obj => this.onResultsReturned(obj)); + this._suggestionController.maxLocalResults = this._historyLimit; + }, + + get _suggestionLabel() { + let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); + let label = bundle.GetStringFromName("suggestion_label"); + Object.defineProperty(SuggestAutoComplete.prototype, "_suggestionLabel", {value: label}); + return label; + }, + + /** + * The object implementing nsIAutoCompleteObserver that we notify when + * we have found results + * @private + */ + _listener: null, + + /** + * Maximum number of history items displayed. This is capped at 7 + * because the primary consumer (Firefox search bar) displays 10 rows + * by default, and so we want to leave some space for suggestions + * to be visible. + */ + _historyLimit: 7, + + /** + * Callback for handling results from SearchSuggestionController.jsm + * @private + */ + onResultsReturned: function(results) { + let finalResults = []; + let finalComments = []; + + // If form history has results, add them to the list. + for (let i = 0; i < results.local.length; ++i) { + finalResults.push(results.local[i]); + finalComments.push(""); + } + + // If there are remote matches, add them. + if (results.remote.length) { + // "comments" column values for suggestions starts as empty strings + let comments = new Array(results.remote.length).fill("", 1); + comments[0] = this._suggestionLabel; + // now put the history results above the suggestions + finalResults = finalResults.concat(results.remote); + finalComments = finalComments.concat(comments); + } + + // Notify the FE of our new results + this.onResultsReady(results.term, finalResults, finalComments, results.formHistoryResult); + }, + + /** + * Notifies the front end of new results. + * @param searchString the user's query string + * @param results an array of results to the search + * @param comments an array of metadata corresponding to the results + * @private + */ + onResultsReady: function(searchString, results, comments, formHistoryResult) { + if (this._listener) { + // Create a copy of the results array to use as labels, since + // FormAutoCompleteResult doesn't like being passed the same array + // for both. + let labels = results.slice(); + let result = new FormAutoCompleteResult( + searchString, + Ci.nsIAutoCompleteResult.RESULT_SUCCESS, + 0, + "", + results, + labels, + comments, + formHistoryResult); + + this._listener.onSearchResult(this, result); + + // Null out listener to make sure we don't notify it twice + this._listener = null; + } + }, + + /** + * Initiates the search result gathering process. Part of + * nsIAutoCompleteSearch implementation. + * + * @param searchString the user's query string + * @param searchParam unused, "an extra parameter"; even though + * this parameter and the next are unused, pass + * them through in case the form history + * service wants them + * @param previousResult unused, a client-cached store of the previous + * generated resultset for faster searching. + * @param listener object implementing nsIAutoCompleteObserver which + * we notify when results are ready. + */ + startSearch: function(searchString, searchParam, previousResult, listener) { + // Don't reuse a previous form history result when it no longer applies. + if (!previousResult) + this._formHistoryResult = null; + + var formHistorySearchParam = searchParam.split("|")[0]; + + // Receive the information about the privacy mode of the window to which + // this search box belongs. The front-end's search.xml bindings passes this + // information in the searchParam parameter. The alternative would have + // been to modify nsIAutoCompleteSearch to add an argument to startSearch + // and patch all of autocomplete to be aware of this, but the searchParam + // argument is already an opaque argument, so this solution is hopefully + // less hackish (although still gross.) + var privacyMode = (searchParam.split("|")[1] == "private"); + + // Start search immediately if possible, otherwise once the search + // service is initialized + if (Services.search.isInitialized) { + this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode); + return; + } + + Services.search.init((function startSearch_cb(aResult) { + if (!Components.isSuccessCode(aResult)) { + Cu.reportError("Could not initialize search service, bailing out: " + aResult); + return; + } + this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode); + }).bind(this)); + }, + + /** + * Actual implementation of search. + */ + _triggerSearch: function(searchString, searchParam, listener, privacyMode) { + this._listener = listener; + this._suggestionController.fetch(searchString, + privacyMode, + Services.search.currentEngine); + }, + + /** + * Ends the search result gathering process. Part of nsIAutoCompleteSearch + * implementation. + */ + stopSearch: function() { + this._suggestionController.stop(); + }, + + // nsISupports + QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch, + Ci.nsIAutoCompleteObserver]) +}; + +/** + * SearchSuggestAutoComplete is a service implementation that handles suggest + * results specific to web searches. + * @constructor + */ +function SearchSuggestAutoComplete() { + // This calls _init() in the parent class (SuggestAutoComplete) via the + // prototype, below. + this._init(); +} +SearchSuggestAutoComplete.prototype = { + classID: Components.ID("{aa892eb4-ffbf-477d-9f9a-06c995ae9f27}"), + __proto__: SuggestAutoComplete.prototype, + serviceURL: "" +}; + +var component = [SearchSuggestAutoComplete]; +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component); diff --git a/application/basilisk/components/search/service/nsSidebar.js b/application/basilisk/components/search/service/nsSidebar.js new file mode 100644 index 000000000..63976cba7 --- /dev/null +++ b/application/basilisk/components/search/service/nsSidebar.js @@ -0,0 +1,66 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */ +/* 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/. */ + +const { interfaces: Ci, utils: Cu } = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +// File extension for Sherlock search plugin description files +const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i; + +function nsSidebar() { +} + +nsSidebar.prototype = { + init: function(window) { + this.window = window; + try { + this.mm = window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDocShell) + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIContentFrameMessageManager); + } catch (e) { + Cu.reportError(e); + } + }, + + // Deprecated, only left here to avoid breaking old browser-detection scripts. + addSearchEngine: function(engineURL, iconURL, suggestedTitle, suggestedCategory) { + if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) { + Cu.reportError("Installing Sherlock search plugins is no longer supported."); + return; + } + + this.AddSearchProvider(engineURL); + }, + + // This function implements window.external.AddSearchProvider(). + // The capitalization, although nonstandard here, is to match other browsers' + // APIs and is therefore important. + AddSearchProvider: function(engineURL) { + if (!this.mm) { + Cu.reportError(`Installing a search provider from this context is not currently supported: ${Error().stack}.`); + return; + } + + this.mm.sendAsyncMessage("Search:AddEngine", { + pageURL: this.window.document.documentURIObject.spec, + engineURL + }); + }, + + // This function exists to implement window.external.IsSearchProviderInstalled(), + // for compatibility with other browsers. The function has been deprecated + // and so will not be implemented. + IsSearchProviderInstalled: function(engineURL) { + return 0; + }, + + classID: Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"), + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, + Ci.nsIDOMGlobalPropertyInitializer]) +} + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]); diff --git a/application/basilisk/components/search/service/toolkitsearch.manifest b/application/basilisk/components/search/service/toolkitsearch.manifest new file mode 100644 index 000000000..b7c55da0e --- /dev/null +++ b/application/basilisk/components/search/service/toolkitsearch.manifest @@ -0,0 +1,10 @@ +component {7319788a-fe93-4db3-9f39-818cf08f4256} nsSearchService.js process=main +contract @mozilla.org/browser/search-service;1 {7319788a-fe93-4db3-9f39-818cf08f4256} process=main +# 21600 == 6 hours +category update-timer nsSearchService @mozilla.org/browser/search-service;1,getService,search-engine-update-timer,browser.search.update.interval,21600 +component {aa892eb4-ffbf-477d-9f9a-06c995ae9f27} nsSearchSuggestions.js +contract @mozilla.org/autocomplete/search;1?name=search-autocomplete {aa892eb4-ffbf-477d-9f9a-06c995ae9f27} +#ifdef HAVE_SIDEBAR +component {22117140-9c6e-11d3-aaf1-00805f8a4905} nsSidebar.js +contract @mozilla.org/sidebar;1 {22117140-9c6e-11d3-aaf1-00805f8a4905} +#endif diff --git a/application/basilisk/components/sessionstore/ContentRestore.jsm b/application/basilisk/components/sessionstore/ContentRestore.jsm index d4972bcaf..8b3867624 100644 --- a/application/basilisk/components/sessionstore/ContentRestore.jsm +++ b/application/basilisk/components/sessionstore/ContentRestore.jsm @@ -208,10 +208,6 @@ ContentRestoreInternal.prototype = { ? Utils.deserializePrincipal(loadArguments.triggeringPrincipal) : null; - if (loadArguments.userContextId) { - webNavigation.setOriginAttributesBeforeLoading({ userContextId: loadArguments.userContextId }); - } - webNavigation.loadURIWithOptions(loadArguments.uri, loadArguments.flags, referrer, referrerPolicy, postData, null, null, triggeringPrincipal); diff --git a/application/basilisk/components/sessionstore/SessionHistory.jsm b/application/basilisk/components/sessionstore/SessionHistory.jsm index 3d28d87db..907a60839 100644 --- a/application/basilisk/components/sessionstore/SessionHistory.jsm +++ b/application/basilisk/components/sessionstore/SessionHistory.jsm @@ -64,11 +64,10 @@ var SessionHistoryInternal = { * The docShell that owns the session history. */ collect: function (docShell) { - let loadContext = docShell.QueryInterface(Ci.nsILoadContext); let webNavigation = docShell.QueryInterface(Ci.nsIWebNavigation); let history = webNavigation.sessionHistory.QueryInterface(Ci.nsISHistoryInternal); - let data = {entries: [], userContextId: loadContext.originAttributes.userContextId }; + let data = {entries: []}; if (history && history.count > 0) { // Loop over the transaction linked list directly so we can get the diff --git a/application/basilisk/components/sessionstore/SessionStore.jsm b/application/basilisk/components/sessionstore/SessionStore.jsm index b599bc162..086bb914a 100644 --- a/application/basilisk/components/sessionstore/SessionStore.jsm +++ b/application/basilisk/components/sessionstore/SessionStore.jsm @@ -2204,10 +2204,9 @@ var SessionStoreInternal = { } // Create a new tab. - let userContextId = aTab.getAttribute("usercontextid"); let newTab = aTab == aWindow.gBrowser.selectedTab ? - aWindow.gBrowser.addTab(null, {relatedToCurrent: true, ownerTab: aTab, userContextId}) : - aWindow.gBrowser.addTab(null, {userContextId}); + aWindow.gBrowser.addTab(null, {relatedToCurrent: true, ownerTab: aTab}) : + aWindow.gBrowser.addTab(); // Set tab title to "Connecting..." and start the throbber to pretend we're // doing something while actually waiting for data from the frame script. @@ -2296,7 +2295,7 @@ var SessionStoreInternal = { // create a new tab let tabbrowser = aWindow.gBrowser; - let tab = tabbrowser.selectedTab = tabbrowser.addTab(null, state); + let tab = tabbrowser.selectedTab = tabbrowser.addTab(); // restore tab content this.restoreTab(tab, state); @@ -3100,31 +3099,13 @@ var SessionStoreInternal = { let numVisibleTabs = 0; for (var t = 0; t < newTabCount; t++) { - // When trying to restore into existing tab, we also take the userContextId - // into account if present. - let userContextId = winData.tabs[t].userContextId; - let reuseExisting = t < openTabCount && - (tabbrowser.tabs[t].getAttribute("usercontextid") == (userContextId || "")); - // If the tab is pinned, then we'll be loading it right away, and - // there's no need to cause a remoteness flip by loading it initially - // non-remote. - let forceNotRemote = !winData.tabs[t].pinned; - let tab = reuseExisting ? tabbrowser.tabs[t] : - tabbrowser.addTab("about:blank", - {skipAnimation: true, - forceNotRemote, - userContextId, - skipBackgroundNotify: true}); - - // If we inserted a new tab because the userContextId didn't match with the - // open tab, even though `t < openTabCount`, we need to remove that open tab - // and put the newly added tab in its place. - if (!reuseExisting && t < openTabCount) { - tabbrowser.removeTab(tabbrowser.tabs[t]); - tabbrowser.moveTabTo(tab, t); - } - - tabs.push(tab); + tabs.push(t < openTabCount ? + tabbrowser.tabs[t] : + tabbrowser.addTab("about:blank", { + skipAnimation: true, + forceNotRemote: true, + skipBackgroundNotify: true + })); if (winData.tabs[t].pinned) tabbrowser.pinTab(tabs[t]); @@ -3531,9 +3512,6 @@ var SessionStoreInternal = { let uri = activePageData ? activePageData.url || null : null; if (aLoadArguments) { uri = aLoadArguments.uri; - if (aLoadArguments.userContextId) { - browser.setAttribute("usercontextid", aLoadArguments.userContextId); - } } // We have to mark this tab as restoring first, otherwise diff --git a/application/basilisk/components/sessionstore/TabState.jsm b/application/basilisk/components/sessionstore/TabState.jsm index f22c52fe3..ac846031b 100644 --- a/application/basilisk/components/sessionstore/TabState.jsm +++ b/application/basilisk/components/sessionstore/TabState.jsm @@ -181,10 +181,6 @@ var TabStateInternal = { if (key === "history") { tabData.entries = value.entries; - if (value.hasOwnProperty("userContextId")) { - tabData.userContextId = value.userContextId; - } - if (value.hasOwnProperty("index")) { tabData.index = value.index; } diff --git a/application/basilisk/components/sync/aboutSyncTabs-bindings.xml b/application/basilisk/components/sync/aboutSyncTabs-bindings.xml new file mode 100644 index 000000000..e6108209a --- /dev/null +++ b/application/basilisk/components/sync/aboutSyncTabs-bindings.xml @@ -0,0 +1,46 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<bindings id="tabBindings" + xmlns="http://www.mozilla.org/xbl" + xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:xbl="http://www.mozilla.org/xbl"> + + <binding id="tab-listing" extends="chrome://global/content/bindings/richlistbox.xml#richlistitem"> + <content> + <xul:hbox flex="1"> + <xul:vbox pack="start"> + <xul:image class="tabIcon" + xbl:inherits="src=icon"/> + </xul:vbox> + <xul:vbox pack="start" flex="1"> + <xul:label xbl:inherits="value=title,selected" + crop="end" flex="1" class="title"/> + <xul:label xbl:inherits="value=url,selected" + crop="end" flex="1" class="url"/> + </xul:vbox> + </xul:hbox> + </content> + <handlers> + <handler event="dblclick" button="0"> + <![CDATA[ + RemoteTabViewer.openSelected(); + ]]> + </handler> + </handlers> + </binding> + + <binding id="client-listing" extends="chrome://global/content/bindings/richlistbox.xml#richlistitem"> + <content> + <xul:hbox pack="start" align="center" onfocus="event.target.blur()" onselect="return false;"> + <xul:image/> + <xul:label xbl:inherits="value=clientName" + class="clientName" + crop="center" flex="1"/> + </xul:hbox> + </content> + </binding> +</bindings> diff --git a/application/basilisk/components/sync/aboutSyncTabs.css b/application/basilisk/components/sync/aboutSyncTabs.css new file mode 100644 index 000000000..5a353175b --- /dev/null +++ b/application/basilisk/components/sync/aboutSyncTabs.css @@ -0,0 +1,11 @@ +/* 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/. */ + +richlistitem[type="tab"] { + -moz-binding: url(chrome://browser/content/sync/aboutSyncTabs-bindings.xml#tab-listing); +} + +richlistitem[type="client"] { + -moz-binding: url(chrome://browser/content/sync/aboutSyncTabs-bindings.xml#client-listing); +} diff --git a/application/basilisk/components/sync/aboutSyncTabs.js b/application/basilisk/components/sync/aboutSyncTabs.js new file mode 100644 index 000000000..4808c052f --- /dev/null +++ b/application/basilisk/components/sync/aboutSyncTabs.js @@ -0,0 +1,307 @@ +/* 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/. */ + +var Cu = Components.utils; + +Cu.import("resource://services-common/utils.js"); +Cu.import("resource://services-sync/main.js"); +Cu.import("resource:///modules/PlacesUIUtils.jsm"); +Cu.import("resource://gre/modules/PlacesUtils.jsm", this); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +var RemoteTabViewer = { + _tabsList: null, + + init: function () { + Services.obs.addObserver(this, "weave:service:login:finish", false); + Services.obs.addObserver(this, "weave:engine:sync:finish", false); + + this._tabsList = document.getElementById("tabsList"); + + this.buildList(true); + }, + + uninit: function () { + Services.obs.removeObserver(this, "weave:service:login:finish"); + Services.obs.removeObserver(this, "weave:engine:sync:finish"); + }, + + createItem: function(attrs) { + let item = document.createElement("richlistitem"); + + // Copy the attributes from the argument into the item + for (let attr in attrs) { + item.setAttribute(attr, attrs[attr]); + } + + if (attrs["type"] == "tab") { + item.label = attrs.title != "" ? attrs.title : attrs.url; + } + + return item; + }, + + filterTabs: function(event) { + let val = event.target.value.toLowerCase(); + let numTabs = this._tabsList.getRowCount(); + let clientTabs = 0; + let currentClient = null; + + for (let i = 0; i < numTabs; i++) { + let item = this._tabsList.getItemAtIndex(i); + let hide = false; + if (item.getAttribute("type") == "tab") { + if (!item.getAttribute("url").toLowerCase().includes(val) && + !item.getAttribute("title").toLowerCase().includes(val)) { + hide = true; + } else { + clientTabs++; + } + } + else if (item.getAttribute("type") == "client") { + if (currentClient) { + if (clientTabs == 0) { + currentClient.hidden = true; + } + } + currentClient = item; + clientTabs = 0; + } + item.hidden = hide; + } + if (clientTabs == 0) { + currentClient.hidden = true; + } + }, + + openSelected: function() { + let items = this._tabsList.selectedItems; + let urls = []; + for (let i = 0;i < items.length;i++) { + if (items[i].getAttribute("type") == "tab") { + urls.push(items[i].getAttribute("url")); + let index = this._tabsList.getIndexOfItem(items[i]); + this._tabsList.removeItemAt(index); + } + } + if (urls.length) { + getTopWin().gBrowser.loadTabs(urls); + this._tabsList.clearSelection(); + } + }, + + bookmarkSingleTab: function() { + let item = this._tabsList.selectedItems[0]; + let uri = Weave.Utils.makeURI(item.getAttribute("url")); + let title = item.getAttribute("title"); + PlacesUIUtils.showBookmarkDialog({ action: "add" + , type: "bookmark" + , uri: uri + , title: title + , hiddenRows: [ "description" + , "location" + , "loadInSidebar" + , "keyword" ] + }, window.top); + }, + + bookmarkSelectedTabs: function() { + let items = this._tabsList.selectedItems; + let URIs = []; + for (let i = 0;i < items.length;i++) { + if (items[i].getAttribute("type") == "tab") { + let uri = Weave.Utils.makeURI(items[i].getAttribute("url")); + if (!uri) { + continue; + } + + URIs.push(uri); + } + } + if (URIs.length) { + PlacesUIUtils.showBookmarkDialog({ action: "add" + , type: "folder" + , URIList: URIs + , hiddenRows: [ "description" ] + }, window.top); + } + }, + + getIcon: function (iconUri, defaultIcon) { + try { + let iconURI = Weave.Utils.makeURI(iconUri); + return PlacesUtils.favicons.getFaviconLinkForIcon(iconURI).spec; + } catch (ex) { + // Do nothing. + } + + // Just give the provided default icon or the system's default. + return defaultIcon || PlacesUtils.favicons.defaultFavicon.spec; + }, + + _waitingForBuildList: false, + + _buildListRequested: false, + + buildList: function (force) { + if (this._waitingForBuildList) { + this._buildListRequested = true; + return; + } + + this._waitingForBuildList = true; + this._buildListRequested = false; + + this._clearTabList(); + + if (Weave.Service.isLoggedIn && this._refetchTabs(force)) { + this._generateWeaveTabList(); + } else { + //XXXzpao We should say something about not being logged in & not having data + // or tell the appropriate condition. (bug 583344) + } + + function complete() { + this._waitingForBuildList = false; + if (this._buildListRequested) { + CommonUtils.nextTick(this.buildList, this); + } + } + + complete(); + }, + + _clearTabList: function () { + let list = this._tabsList; + + // Clear out existing richlistitems + let count = list.getRowCount(); + if (count > 0) { + for (let i = count - 1; i >= 0; i--) { + list.removeItemAt(i); + } + } + }, + + _generateWeaveTabList: function () { + let engine = Weave.Service.engineManager.get("tabs"); + let list = this._tabsList; + + let seenURLs = new Set(); + let localURLs = engine.getOpenURLs(); + + for (let [guid, client] in Iterator(engine.getAllClients())) { + // Create the client node, but don't add it in-case we don't show any tabs + let appendClient = true; + + client.tabs.forEach(function({title, urlHistory, icon}) { + let url = urlHistory[0]; + if (!url || localURLs.has(url) || seenURLs.has(url)) { + return; + } + seenURLs.add(url); + + if (appendClient) { + let attrs = { + type: "client", + clientName: client.clientName, + class: Weave.Service.clientsEngine.isMobile(client.id) ? "mobile" : "desktop" + }; + let clientEnt = this.createItem(attrs); + list.appendChild(clientEnt); + appendClient = false; + clientEnt.disabled = true; + } + let attrs = { + type: "tab", + title: title || url, + url: url, + icon: this.getIcon(icon), + } + let tab = this.createItem(attrs); + list.appendChild(tab); + }, this); + } + }, + + adjustContextMenu: function(event) { + let mode = "all"; + switch (this._tabsList.selectedItems.length) { + case 0: + break; + case 1: + mode = "single" + break; + default: + mode = "multiple"; + break; + } + + let menu = document.getElementById("tabListContext"); + let el = menu.firstChild; + while (el) { + let showFor = el.getAttribute("showFor"); + if (showFor) { + el.hidden = showFor != mode && showFor != "all"; + } + + el = el.nextSibling; + } + }, + + _refetchTabs: function(force) { + if (!force) { + // Don't bother refetching tabs if we already did so recently + lastFetch = Services.prefs.getIntPref("services.sync.lastTabFetch", 0); + + let now = Math.floor(Date.now() / 1000); + if (now - lastFetch < 30) { + return false; + } + } + + // if Clients hasn't synced yet this session, we need to sync it as well. + if (Weave.Service.clientsEngine.lastSync == 0) { + Weave.Service.clientsEngine.sync(); + } + + // Force a sync only for the tabs engine + let engine = Weave.Service.engineManager.get("tabs"); + engine.lastModified = null; + engine.sync(); + Services.prefs.setIntPref("services.sync.lastTabFetch", + Math.floor(Date.now() / 1000)); + + return true; + }, + + observe: function(subject, topic, data) { + switch (topic) { + case "weave:service:login:finish": + this.buildList(true); + break; + case "weave:engine:sync:finish": + if (subject == "tabs") { + this.buildList(false); + } + break; + } + }, + + handleClick: function(event) { + if (event.target.getAttribute("type") != "tab") { + return; + } + + + if (event.button == 1) { + let url = event.target.getAttribute("url"); + openUILink(url, event); + let index = this._tabsList.getIndexOfItem(event.target); + this._tabsList.removeItemAt(index); + } + } +} + diff --git a/application/basilisk/components/sync/aboutSyncTabs.xul b/application/basilisk/components/sync/aboutSyncTabs.xul new file mode 100644 index 000000000..a4aa0032f --- /dev/null +++ b/application/basilisk/components/sync/aboutSyncTabs.xul @@ -0,0 +1,68 @@ +<?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/. --> + +<?xml-stylesheet href="chrome://browser/skin/" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/aboutSyncTabs.css" type="text/css"?> +<?xml-stylesheet href="chrome://browser/content/sync/aboutSyncTabs.css" type="text/css"?> + +<!DOCTYPE window [ + <!ENTITY % aboutSyncTabsDTD SYSTEM "chrome://browser/locale/aboutSyncTabs.dtd"> + %aboutSyncTabsDTD; +]> + +<window id="tabs-display" + onload="RemoteTabViewer.init()" + onunload="RemoteTabViewer.uninit()" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:html="http://www.w3.org/1999/xhtml" + title="&tabs.otherDevices.label;"> + <script type="application/javascript;version=1.8" src="chrome://browser/content/sync/aboutSyncTabs.js"/> + <script type="application/javascript" src="chrome://browser/content/utilityOverlay.js"/> + <html:head> + <html:link rel="icon" href="chrome://browser/skin/sync-16.png"/> + </html:head> + + <popupset id="contextmenus"> + <menupopup id="tabListContext"> + <menuitem label="&tabs.context.openTab.label;" + accesskey="&tabs.context.openTab.accesskey;" + oncommand="RemoteTabViewer.openSelected()" + showFor="single"/> + <menuitem label="&tabs.context.bookmarkSingleTab.label;" + accesskey="&tabs.context.bookmarkSingleTab.accesskey;" + oncommand="RemoteTabViewer.bookmarkSingleTab(event)" + showFor="single"/> + <menuitem label="&tabs.context.openMultipleTabs.label;" + accesskey="&tabs.context.openMultipleTabs.accesskey;" + oncommand="RemoteTabViewer.openSelected()" + showFor="multiple"/> + <menuitem label="&tabs.context.bookmarkMultipleTabs.label;" + accesskey="&tabs.context.bookmarkMultipleTabs.accesskey;" + oncommand="RemoteTabViewer.bookmarkSelectedTabs()" + showFor="multiple"/> + <menuseparator/> + <menuitem label="&tabs.context.refreshList.label;" + accesskey="&tabs.context.refreshList.accesskey;" + oncommand="RemoteTabViewer.buildList()" + showFor="all"/> + </menupopup> + </popupset> + <richlistbox context="tabListContext" id="tabsList" seltype="multiple" + align="center" flex="1" + onclick="RemoteTabViewer.handleClick(event)" + oncontextmenu="RemoteTabViewer.adjustContextMenu(event)"> + <hbox id="headers" align="center"> + <label id="tabsListHeading" + value="&tabs.otherDevices.label;"/> + <spacer flex="1"/> + <textbox type="search" + emptytext="&tabs.searchText.label;" + oncommand="RemoteTabViewer.filterTabs(event)"/> + </hbox> + + </richlistbox> +</window> + diff --git a/application/basilisk/components/sync/addDevice.js b/application/basilisk/components/sync/addDevice.js new file mode 100644 index 000000000..0390d4397 --- /dev/null +++ b/application/basilisk/components/sync/addDevice.js @@ -0,0 +1,157 @@ +/* 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/. */ + +var Ci = Components.interfaces; +var Cc = Components.classes; +var Cu = Components.utils; + +Cu.import("resource://services-sync/main.js"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +const PIN_PART_LENGTH = 4; + +const ADD_DEVICE_PAGE = 0; +const SYNC_KEY_PAGE = 1; +const DEVICE_CONNECTED_PAGE = 2; + +var gSyncAddDevice = { + + init: function init() { + this.pin1.setAttribute("maxlength", PIN_PART_LENGTH); + this.pin2.setAttribute("maxlength", PIN_PART_LENGTH); + this.pin3.setAttribute("maxlength", PIN_PART_LENGTH); + + this.nextFocusEl = {pin1: this.pin2, + pin2: this.pin3, + pin3: this.wizard.getButton("next")}; + + this.throbber = document.getElementById("pairDeviceThrobber"); + this.errorRow = document.getElementById("errorRow"); + + // Kick off a sync. That way the server will have the most recent data from + // this computer and it will show up immediately on the new device. + Weave.Service.scheduler.scheduleNextSync(0); + }, + + onPageShow: function onPageShow() { + this.wizard.getButton("back").hidden = true; + + switch (this.wizard.pageIndex) { + case ADD_DEVICE_PAGE: + this.onTextBoxInput(); + this.wizard.canRewind = false; + this.wizard.getButton("next").hidden = false; + this.pin1.focus(); + break; + case SYNC_KEY_PAGE: + this.wizard.canAdvance = false; + this.wizard.canRewind = true; + this.wizard.getButton("back").hidden = false; + this.wizard.getButton("next").hidden = true; + document.getElementById("weavePassphrase").value = + Weave.Utils.hyphenatePassphrase(Weave.Service.identity.syncKey); + break; + case DEVICE_CONNECTED_PAGE: + this.wizard.canAdvance = true; + this.wizard.canRewind = false; + this.wizard.getButton("cancel").hidden = true; + break; + } + }, + + onWizardAdvance: function onWizardAdvance() { + switch (this.wizard.pageIndex) { + case ADD_DEVICE_PAGE: + this.startTransfer(); + return false; + case DEVICE_CONNECTED_PAGE: + window.close(); + return false; + } + return true; + }, + + startTransfer: function startTransfer() { + this.errorRow.hidden = true; + // When onAbort is called, Weave may already be gone. + const JPAKE_ERROR_USERABORT = Weave.JPAKE_ERROR_USERABORT; + + let self = this; + let jpakeclient = this._jpakeclient = new Weave.JPAKEClient({ + onPaired: function onPaired() { + let credentials = {account: Weave.Service.identity.account, + password: Weave.Service.identity.basicPassword, + synckey: Weave.Service.identity.syncKey, + serverURL: Weave.Service.serverURL}; + jpakeclient.sendAndComplete(credentials); + }, + onComplete: function onComplete() { + delete self._jpakeclient; + self.wizard.pageIndex = DEVICE_CONNECTED_PAGE; + + // Schedule a Sync for soonish to fetch the data uploaded by the + // device with which we just paired. + Weave.Service.scheduler.scheduleNextSync(Weave.Service.scheduler.activeInterval); + }, + onAbort: function onAbort(error) { + delete self._jpakeclient; + + // Aborted by user, ignore. + if (error == JPAKE_ERROR_USERABORT) { + return; + } + + self.errorRow.hidden = false; + self.throbber.hidden = true; + self.pin1.value = self.pin2.value = self.pin3.value = ""; + self.pin1.disabled = self.pin2.disabled = self.pin3.disabled = false; + self.pin1.focus(); + } + }); + this.throbber.hidden = false; + this.pin1.disabled = this.pin2.disabled = this.pin3.disabled = true; + this.wizard.canAdvance = false; + + let pin = this.pin1.value + this.pin2.value + this.pin3.value; + let expectDelay = false; + jpakeclient.pairWithPIN(pin, expectDelay); + }, + + onWizardBack: function onWizardBack() { + if (this.wizard.pageIndex != SYNC_KEY_PAGE) + return true; + + this.wizard.pageIndex = ADD_DEVICE_PAGE; + return false; + }, + + onWizardCancel: function onWizardCancel() { + if (this._jpakeclient) { + this._jpakeclient.abort(); + delete this._jpakeclient; + } + return true; + }, + + onTextBoxInput: function onTextBoxInput(textbox) { + if (textbox && textbox.value.length == PIN_PART_LENGTH) + this.nextFocusEl[textbox.id].focus(); + + this.wizard.canAdvance = (this.pin1.value.length == PIN_PART_LENGTH + && this.pin2.value.length == PIN_PART_LENGTH + && this.pin3.value.length == PIN_PART_LENGTH); + }, + + goToSyncKeyPage: function goToSyncKeyPage() { + this.wizard.pageIndex = SYNC_KEY_PAGE; + } + +}; +// onWizardAdvance() and onPageShow() are run before init() so we'll set +// these up as lazy getters. +["wizard", "pin1", "pin2", "pin3"].forEach(function (id) { + XPCOMUtils.defineLazyGetter(gSyncAddDevice, id, function() { + return document.getElementById(id); + }); +}); diff --git a/application/basilisk/components/sync/addDevice.xul b/application/basilisk/components/sync/addDevice.xul new file mode 100644 index 000000000..f2371aad0 --- /dev/null +++ b/application/basilisk/components/sync/addDevice.xul @@ -0,0 +1,129 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncSetup.css" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncCommon.css" type="text/css"?> + +<!DOCTYPE window [ +<!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> +<!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> +<!ENTITY % syncSetupDTD SYSTEM "chrome://browser/locale/syncSetup.dtd"> +%brandDTD; +%syncBrandDTD; +%syncSetupDTD; +]> +<wizard xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:html="http://www.w3.org/1999/xhtml" + id="wizard" + title="&pairDevice.title.label;" + windowtype="Sync:AddDevice" + persist="screenX screenY" + onwizardnext="return gSyncAddDevice.onWizardAdvance();" + onwizardback="return gSyncAddDevice.onWizardBack();" + onwizardcancel="gSyncAddDevice.onWizardCancel();" + onload="gSyncAddDevice.init();"> + + <script type="application/javascript" + src="chrome://browser/content/sync/addDevice.js"/> + <script type="application/javascript" + src="chrome://browser/content/sync/utils.js"/> + <script type="application/javascript" + src="chrome://browser/content/utilityOverlay.js"/> + <script type="application/javascript" + src="chrome://global/content/printUtils.js"/> + + <wizardpage id="addDevicePage" + label="&pairDevice.title.label;" + onpageshow="gSyncAddDevice.onPageShow();"> + <description> + &pairDevice.dialog.description.label; + <label class="text-link" + value="&addDevice.showMeHow.label;" + href="http://www.palemoon.org/sync/help/easy-setup.shtml"/> + </description> + <separator class="groove-thin"/> + <description> + &addDevice.dialog.enterCode.label; + </description> + <separator class="groove-thin"/> + <vbox align="center"> + <textbox id="pin1" + class="pin" + oninput="gSyncAddDevice.onTextBoxInput(this);" + onfocus="this.select();" + /> + <textbox id="pin2" + class="pin" + oninput="gSyncAddDevice.onTextBoxInput(this);" + onfocus="this.select();" + /> + <textbox id="pin3" + class="pin" + oninput="gSyncAddDevice.onTextBoxInput(this);" + onfocus="this.select();" + /> + </vbox> + <separator class="groove-thin"/> + <vbox id="pairDeviceThrobber" align="center" hidden="true"> + <image/> + </vbox> + <hbox id="errorRow" pack="center" hidden="true"> + <image class="statusIcon" status="error"/> + <label class="status" + value="&addDevice.dialog.tryAgain.label;"/> + </hbox> + <spacer flex="3"/> + <label class="text-link" + value="&addDevice.dontHaveDevice.label;" + onclick="gSyncAddDevice.goToSyncKeyPage();"/> + </wizardpage> + + <!-- Need a non-empty label here, otherwise we get a default label on Mac --> + <wizardpage id="syncKeyPage" + label=" " + onpageshow="gSyncAddDevice.onPageShow();"> + <description> + &addDevice.dialog.recoveryKey.label; + </description> + <spacer/> + + <groupbox> + <label value="&recoveryKeyEntry.label;" + accesskey="&recoveryKeyEntry.accesskey;" + control="weavePassphrase"/> + <textbox id="weavePassphrase" + readonly="true"/> + </groupbox> + + <groupbox align="center"> + <description>&recoveryKeyBackup.description;</description> + <hbox> + <button id="printSyncKeyButton" + label="&button.syncKeyBackup.print.label;" + accesskey="&button.syncKeyBackup.print.accesskey;" + oncommand="gSyncUtils.passphrasePrint('weavePassphrase');"/> + <button id="saveSyncKeyButton" + label="&button.syncKeyBackup.save.label;" + accesskey="&button.syncKeyBackup.save.accesskey;" + oncommand="gSyncUtils.passphraseSave('weavePassphrase');"/> + </hbox> + </groupbox> + </wizardpage> + + <wizardpage id="deviceConnectedPage" + label="&addDevice.dialog.connected.label;" + onpageshow="gSyncAddDevice.onPageShow();"> + <vbox align="center"> + <image id="successPageIcon"/> + </vbox> + <separator/> + <description class="normal"> + &addDevice.dialog.successful.label; + </description> + </wizardpage> + +</wizard> diff --git a/application/basilisk/components/sync/genericChange.js b/application/basilisk/components/sync/genericChange.js new file mode 100644 index 000000000..df6639178 --- /dev/null +++ b/application/basilisk/components/sync/genericChange.js @@ -0,0 +1,234 @@ +/* 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/. */ + +var Ci = Components.interfaces; +var Cc = Components.classes; + +Components.utils.import("resource://services-sync/main.js"); +Components.utils.import("resource://gre/modules/Services.jsm"); + +var Change = { + _dialog: null, + _dialogType: null, + _status: null, + _statusIcon: null, + _firstBox: null, + _secondBox: null, + + get _passphraseBox() { + delete this._passphraseBox; + return this._passphraseBox = document.getElementById("passphraseBox"); + }, + + get _currentPasswordInvalid() { + return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED; + }, + + get _updatingPassphrase() { + return this._dialogType == "UpdatePassphrase"; + }, + + onLoad: function Change_onLoad() { + /* Load labels */ + let introText = document.getElementById("introText"); + let introText2 = document.getElementById("introText2"); + let warningText = document.getElementById("warningText"); + + // load some other elements & info from the window + this._dialog = document.getElementById("change-dialog"); + this._dialogType = window.arguments[0]; + this._duringSetup = window.arguments[1]; + this._status = document.getElementById("status"); + this._statusIcon = document.getElementById("statusIcon"); + this._statusRow = document.getElementById("statusRow"); + this._firstBox = document.getElementById("textBox1"); + this._secondBox = document.getElementById("textBox2"); + + this._dialog.getButton("finish").disabled = true; + this._dialog.getButton("back").hidden = true; + + this._stringBundle = + Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties"); + + switch (this._dialogType) { + case "UpdatePassphrase": + case "ResetPassphrase": + document.getElementById("textBox1Row").hidden = true; + document.getElementById("textBox2Row").hidden = true; + document.getElementById("passphraseLabel").value + = this._str("new.recoverykey.label"); + document.getElementById("passphraseSpacer").hidden = false; + + if (this._updatingPassphrase) { + document.getElementById("passphraseHelpBox").hidden = false; + document.title = this._str("new.recoverykey.title"); + introText.textContent = this._str("new.recoverykey.introText"); + this._dialog.getButton("finish").label + = this._str("new.recoverykey.acceptButton"); + } + else { + document.getElementById("generatePassphraseButton").hidden = false; + document.getElementById("passphraseBackupButtons").hidden = false; + let pp = Weave.Service.identity.syncKey; + if (Weave.Utils.isPassphrase(pp)) + pp = Weave.Utils.hyphenatePassphrase(pp); + this._passphraseBox.value = pp; + this._passphraseBox.focus(); + document.title = this._str("change.recoverykey.title"); + introText.textContent = this._str("change.synckey.introText2"); + warningText.textContent = this._str("change.recoverykey.warningText"); + this._dialog.getButton("finish").label + = this._str("change.recoverykey.acceptButton"); + if (this._duringSetup) { + this._dialog.getButton("finish").disabled = false; + } + } + break; + case "ChangePassword": + document.getElementById("passphraseRow").hidden = true; + let box1label = document.getElementById("textBox1Label"); + let box2label = document.getElementById("textBox2Label"); + box1label.value = this._str("new.password.label"); + + if (this._currentPasswordInvalid) { + document.title = this._str("new.password.title"); + introText.textContent = this._str("new.password.introText"); + this._dialog.getButton("finish").label + = this._str("new.password.acceptButton"); + document.getElementById("textBox2Row").hidden = true; + } + else { + document.title = this._str("change.password.title"); + box2label.value = this._str("new.password.confirm"); + introText.textContent = this._str("change.password3.introText"); + warningText.textContent = this._str("change.password.warningText"); + this._dialog.getButton("finish").label + = this._str("change.password.acceptButton"); + } + break; + } + document.getElementById("change-page") + .setAttribute("label", document.title); + }, + + _clearStatus: function _clearStatus() { + this._status.value = ""; + this._statusIcon.removeAttribute("status"); + }, + + _updateStatus: function Change__updateStatus(str, state) { + this._updateStatusWithString(this._str(str), state); + }, + + _updateStatusWithString: function Change__updateStatusWithString(string, state) { + this._statusRow.hidden = false; + this._status.value = string; + this._statusIcon.setAttribute("status", state); + + let error = state == "error"; + this._dialog.getButton("cancel").disabled = !error; + this._dialog.getButton("finish").disabled = !error; + document.getElementById("printSyncKeyButton").disabled = !error; + document.getElementById("saveSyncKeyButton").disabled = !error; + + if (state == "success") + window.setTimeout(window.close, 1500); + }, + + onDialogAccept: function() { + switch (this._dialogType) { + case "UpdatePassphrase": + case "ResetPassphrase": + return this.doChangePassphrase(); + break; + case "ChangePassword": + return this.doChangePassword(); + break; + } + }, + + doGeneratePassphrase: function () { + let passphrase = Weave.Utils.generatePassphrase(); + this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase); + this._dialog.getButton("finish").disabled = false; + }, + + doChangePassphrase: function Change_doChangePassphrase() { + let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value); + if (this._updatingPassphrase) { + Weave.Service.identity.syncKey = pp; + if (Weave.Service.login()) { + this._updateStatus("change.recoverykey.success", "success"); + Weave.Service.persistLogin(); + Weave.Service.scheduler.delayedAutoConnect(0); + } + else { + this._updateStatus("new.passphrase.status.incorrect", "error"); + } + } + else { + this._updateStatus("change.recoverykey.label", "active"); + + if (Weave.Service.changePassphrase(pp)) + this._updateStatus("change.recoverykey.success", "success"); + else + this._updateStatus("change.recoverykey.error", "error"); + } + + return false; + }, + + doChangePassword: function Change_doChangePassword() { + if (this._currentPasswordInvalid) { + Weave.Service.identity.basicPassword = this._firstBox.value; + if (Weave.Service.login()) { + this._updateStatus("change.password.status.success", "success"); + Weave.Service.persistLogin(); + } + else { + this._updateStatus("new.password.status.incorrect", "error"); + } + } + else { + this._updateStatus("change.password.status.active", "active"); + + if (Weave.Service.changePassword(this._firstBox.value)) + this._updateStatus("change.password.status.success", "success"); + else + this._updateStatus("change.password.status.error", "error"); + } + + return false; + }, + + validate: function (event) { + let valid = false; + let errorString = ""; + + if (this._dialogType == "ChangePassword") { + if (this._currentPasswordInvalid) + [valid, errorString] = gSyncUtils.validatePassword(this._firstBox); + else + [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox); + } + else { + //Pale Moon: Enforce minimum length of 8 for allowed custom passphrase + //and don't restrict it to "out of sync" situations only. People who + //go to this page generally know what they are doing ;) + valid = this._passphraseBox.value.length >= 8; + } + + if (errorString == "") + this._clearStatus(); + else + this._updateStatusWithString(errorString, "error"); + + this._statusRow.hidden = valid; + this._dialog.getButton("finish").disabled = !valid; + }, + + _str: function Change__string(str) { + return this._stringBundle.GetStringFromName(str); + } +}; diff --git a/application/basilisk/components/sync/genericChange.xul b/application/basilisk/components/sync/genericChange.xul new file mode 100644 index 000000000..3c0b2cd6c --- /dev/null +++ b/application/basilisk/components/sync/genericChange.xul @@ -0,0 +1,123 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncSetup.css" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncCommon.css" type="text/css"?> + +<!DOCTYPE window [ +<!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> +<!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> +<!ENTITY % syncSetupDTD SYSTEM "chrome://browser/locale/syncSetup.dtd"> +%brandDTD; +%syncBrandDTD; +%syncSetupDTD; +]> +<wizard xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:html="http://www.w3.org/1999/xhtml" + id="change-dialog" + windowtype="Weave:ChangeSomething" + persist="screenX screenY" + onwizardnext="Change.onLoad()" + onwizardfinish="return Change.onDialogAccept();"> + + <script type="application/javascript" + src="chrome://browser/content/sync/genericChange.js"/> + <script type="application/javascript" + src="chrome://browser/content/sync/utils.js"/> + <script type="application/javascript" + src="chrome://global/content/printUtils.js"/> + + <wizardpage id="change-page" + label=""> + + <description id="introText"> + </description> + + <separator class="thin"/> + + <groupbox> + <grid> + <columns> + <column align="right"/> + <column flex="3"/> + <column flex="1"/> + </columns> + <rows> + <row id="textBox1Row" align="center"> + <label id="textBox1Label" control="textBox1"/> + <textbox id="textBox1" type="password" oninput="Change.validate()"/> + <spacer/> + </row> + <row id="textBox2Row" align="center"> + <label id="textBox2Label" control="textBox2"/> + <textbox id="textBox2" type="password" oninput="Change.validate()"/> + <spacer/> + </row> + </rows> + </grid> + + <vbox id="passphraseRow"> + <hbox flex="1"> + <label id="passphraseLabel" control="passphraseBox"/> + <spacer flex="1"/> + <label id="generatePassphraseButton" + hidden="true" + value="&syncGenerateNewKey.label;" + class="text-link inline-link" + onclick="event.stopPropagation(); + Change.doGeneratePassphrase();"/> + </hbox> + <textbox id="passphraseBox" + flex="1" + onfocus="this.select()" + oninput="Change.validate()"/> + </vbox> + + <vbox id="feedback" pack="center"> + <hbox id="statusRow" align="center"> + <image id="statusIcon" class="statusIcon"/> + <label id="status" class="status" value=" "/> + </hbox> + </vbox> + </groupbox> + + <separator class="thin"/> + + <hbox id="passphraseBackupButtons" + hidden="true" + pack="center"> + <button id="printSyncKeyButton" + label="&button.syncKeyBackup.print.label;" + accesskey="&button.syncKeyBackup.print.accesskey;" + oncommand="gSyncUtils.passphrasePrint('passphraseBox');"/> + <button id="saveSyncKeyButton" + label="&button.syncKeyBackup.save.label;" + accesskey="&button.syncKeyBackup.save.accesskey;" + oncommand="gSyncUtils.passphraseSave('passphraseBox');"/> + </hbox> + + <vbox id="passphraseHelpBox" + hidden="true"> + <description> + &existingRecoveryKey.description; + <label class="text-link" + href="http://www.palemoon.org/sync/help/recoverykey.shtml"> + &addDevice.showMeHow.label; + </label> + </description> + </vbox> + + <spacer id="passphraseSpacer" + flex="1" + hidden="true"/> + + <description id="warningText" class="data"> + </description> + + <spacer flex="1"/> + </wizardpage> +</wizard> diff --git a/application/basilisk/components/sync/jar.mn b/application/basilisk/components/sync/jar.mn new file mode 100644 index 000000000..3782038cd --- /dev/null +++ b/application/basilisk/components/sync/jar.mn @@ -0,0 +1,22 @@ +# 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/sync/aboutSyncTabs.xul + content/browser/sync/aboutSyncTabs.js + content/browser/sync/aboutSyncTabs.css + content/browser/sync/aboutSyncTabs-bindings.xml + content/browser/sync/setup.xul + content/browser/sync/addDevice.js + content/browser/sync/addDevice.xul + content/browser/sync/setup.js + content/browser/sync/genericChange.xul + content/browser/sync/genericChange.js + content/browser/sync/key.xhtml + content/browser/sync/notification.xml + content/browser/sync/quota.xul + content/browser/sync/quota.js + content/browser/sync/utils.js + content/browser/sync/progress.js + content/browser/sync/progress.xhtml
\ No newline at end of file diff --git a/application/basilisk/components/sync/key.xhtml b/application/basilisk/components/sync/key.xhtml new file mode 100644 index 000000000..92abf0ee6 --- /dev/null +++ b/application/basilisk/components/sync/key.xhtml @@ -0,0 +1,54 @@ +<?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/. --> + +<!DOCTYPE html [ + <!ENTITY % htmlDTD PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> + %htmlDTD; + <!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> + %syncBrandDTD; + <!ENTITY % syncKeyDTD SYSTEM "chrome://browser/locale/syncKey.dtd"> + %syncKeyDTD; + <!ENTITY % globalDTD SYSTEM "chrome://global/locale/global.dtd" > + %globalDTD; +]> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>&syncKey.page.title;</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> + <meta name="robots" content="noindex"/> + <style type="text/css"> + #synckey { font-size: 150% } + footer { font-size: 70% } + /* Bug 575675: Need to have an a:visited rule in a chrome document. */ + a:visited { color: purple; } + </style> +</head> + +<body dir="&locale.dir;"> +<h1>&syncKey.page.title;</h1> + +<p id="synckey" dir="ltr">SYNCKEY</p> + +<p>&syncKey.page.description2;</p> + +<div id="column1"> + <h2>&syncKey.keepItSecret.heading;</h2> + <p>&syncKey.keepItSecret.description;</p> +</div> + +<div id="column2"> + <h2>&syncKey.keepItSafe.heading;</h2> + <p><em>&syncKey.keepItSafe1.description;</em>&syncKey.keepItSafe2.description;<em>&syncKey.keepItSafe3.description;</em>&syncKey.keepItSafe4a.description;</p> +</div> + +<p>&syncKey.findOutMore1.label;<a href="http://www.palemoon.org/sync/">http://www.palemoon.org/sync/</a>&syncKey.findOutMore2.label;</p> + +<footer> + &syncKey.footer1.label;<a id="tosLink" href="termsURL">termsURL</a>&syncKey.footer2.label;<a id="ppLink" href="privacyURL">privacyURL</a>&syncKey.footer3.label; +</footer> + +</body> +</html> diff --git a/application/basilisk/components/contextualidentity/moz.build b/application/basilisk/components/sync/moz.build index aac3a838c..2d64d506c 100644 --- a/application/basilisk/components/contextualidentity/moz.build +++ b/application/basilisk/components/sync/moz.build @@ -1,7 +1,8 @@ -# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# -*- Mode: python; c-basic-offset: 4; 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/application/basilisk/components/sync/notification.xml b/application/basilisk/components/sync/notification.xml new file mode 100644 index 000000000..8ac881e08 --- /dev/null +++ b/application/basilisk/components/sync/notification.xml @@ -0,0 +1,129 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<!DOCTYPE bindings [ +<!ENTITY % notificationDTD SYSTEM "chrome://global/locale/notification.dtd"> +%notificationDTD; +]> + +<bindings id="notificationBindings" + xmlns="http://www.mozilla.org/xbl" + xmlns:xbl="http://www.mozilla.org/xbl" + xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + + <binding id="notificationbox" extends="chrome://global/content/bindings/notification.xml#notificationbox"> + <content> + <xul:vbox xbl:inherits="hidden=notificationshidden"> + <xul:spacer/> + <children includes="notification"/> + </xul:vbox> + <children/> + </content> + + <implementation> + <constructor><![CDATA[ + let temp = {}; + Cu.import("resource://services-common/observers.js", temp); + temp.Observers.add("weave:notification:added", this.onNotificationAdded, this); + temp.Observers.add("weave:notification:removed", this.onNotificationRemoved, this); + + for each (var notification in Weave.Notifications.notifications) + this._appendNotification(notification); + ]]></constructor> + + <destructor><![CDATA[ + let temp = {}; + Cu.import("resource://services-common/observers.js", temp); + temp.Observers.remove("weave:notification:added", this.onNotificationAdded, this); + temp.Observers.remove("weave:notification:removed", this.onNotificationRemoved, this); + ]]></destructor> + + <method name="onNotificationAdded"> + <parameter name="subject"/> + <parameter name="data"/> + <body><![CDATA[ + this._appendNotification(subject); + ]]></body> + </method> + + <method name="onNotificationRemoved"> + <parameter name="subject"/> + <parameter name="data"/> + <body><![CDATA[ + // If the view of the notification hasn't been removed yet, remove it. + var notifications = this.allNotifications; + for each (var notification in notifications) { + if (notification.notification == subject) { + notification.close(); + break; + } + } + ]]></body> + </method> + + <method name="_appendNotification"> + <parameter name="notification"/> + <body><![CDATA[ + var node = this.appendNotification(notification.description, + notification.title, + notification.iconURL, + notification.priority, + notification.buttons); + node.notification = notification; + ]]></body> + </method> + + </implementation> + </binding> + + <binding id="notification" extends="chrome://global/content/bindings/notification.xml#notification"> + <content> + <xul:hbox class="notification-inner outset" flex="1" xbl:inherits="type"> + <xul:toolbarbutton ondblclick="event.stopPropagation();" + class="messageCloseButton close-icon tabbable" + xbl:inherits="hidden=hideclose" + tooltiptext="&closeNotification.tooltip;" + oncommand="document.getBindingParent(this).close()"/> + <xul:hbox anonid="details" align="center" flex="1"> + <xul:image anonid="messageImage" class="messageImage" xbl:inherits="src=image,type"/> + <xul:description anonid="messageText" class="messageText" xbl:inherits="xbl:text=label"/> + + <!-- The children are the buttons defined by the notification. --> + <xul:hbox oncommand="document.getBindingParent(this)._doButtonCommand(event);"> + <children/> + </xul:hbox> + </xul:hbox> + </xul:hbox> + </content> + <implementation> + <!-- Note: this used to be a field, but for some reason it kept getting + - reset to its default value for TabNotification elements. + - As a property, that doesn't happen, even though the property stores + - its value in a JS property |_notification| that is not defined + - in XBL as a field or property. Maybe this is wrong, but it works. + --> + <property name="notification" + onget="return this._notification" + onset="this._notification = val; return val;"/> + <method name="close"> + <body><![CDATA[ + Weave.Notifications.remove(this.notification); + + // We should be able to call the base class's close method here + // to remove the notification element from the notification box, + // but we can't because of bug 373652, so instead we copied its code + // and execute it below. + var control = this.control; + if (control) + control.removeNotification(this); + else + this.hidden = true; + ]]></body> + </method> + </implementation> + </binding> + +</bindings> diff --git a/application/basilisk/components/sync/progress.js b/application/basilisk/components/sync/progress.js new file mode 100644 index 000000000..101160fa8 --- /dev/null +++ b/application/basilisk/components/sync/progress.js @@ -0,0 +1,71 @@ +/* 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/. */ + +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://services-sync/main.js"); + +var gProgressBar; +var gCounter = 0; + +function onLoad(event) { + Services.obs.addObserver(onEngineSync, "weave:engine:sync:finish", false); + Services.obs.addObserver(onEngineSync, "weave:engine:sync:error", false); + Services.obs.addObserver(onServiceSync, "weave:service:sync:finish", false); + Services.obs.addObserver(onServiceSync, "weave:service:sync:error", false); + + gProgressBar = document.getElementById('uploadProgressBar'); + + if (Services.prefs.getPrefType("services.sync.firstSync") != Ci.nsIPrefBranch.PREF_INVALID) { + gProgressBar.hidden = false; + } + else { + gProgressBar.hidden = true; + } +} + +function onUnload(event) { + cleanUpObservers(); +} + +function cleanUpObservers() { + try { + Services.obs.removeObserver(onEngineSync, "weave:engine:sync:finish"); + Services.obs.removeObserver(onEngineSync, "weave:engine:sync:error"); + Services.obs.removeObserver(onServiceSync, "weave:service:sync:finish"); + Services.obs.removeObserver(onServiceSync, "weave:service:sync:error"); + } + catch (e) { + // may be double called by unload & exit. Ignore. + } +} + +function onEngineSync(subject, topic, data) { + // The Clients engine syncs first. At this point we don't necessarily know + // yet how many engines will be enabled, so we'll ignore the Clients engine + // and evaluate how many engines are enabled when the first "real" engine + // syncs. + if (data == "clients") { + return; + } + + if (!gCounter && + Services.prefs.getPrefType("services.sync.firstSync") != Ci.nsIPrefBranch.PREF_INVALID) { + gProgressBar.max = Weave.Service.engineManager.getEnabled().length; + } + + gCounter += 1; + gProgressBar.setAttribute("value", gCounter); +} + +function onServiceSync(subject, topic, data) { + // To address the case where 0 engines are synced, we will fill the + // progress bar so the user knows that the sync has finished. + gProgressBar.setAttribute("value", gProgressBar.max); + cleanUpObservers(); +} + +function closeTab() { + window.close(); +} diff --git a/application/basilisk/components/sync/progress.xhtml b/application/basilisk/components/sync/progress.xhtml new file mode 100644 index 000000000..d403cb20d --- /dev/null +++ b/application/basilisk/components/sync/progress.xhtml @@ -0,0 +1,55 @@ +<?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/. --> + +<!DOCTYPE html [ + <!ENTITY % htmlDTD + PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "DTD/xhtml1-strict.dtd"> + %htmlDTD; + <!ENTITY % syncProgressDTD + SYSTEM "chrome://browser/locale/syncProgress.dtd"> + %syncProgressDTD; + <!ENTITY % syncSetupDTD + SYSTEM "chrome://browser/locale/syncSetup.dtd"> + %syncSetupDTD; + <!ENTITY % globalDTD + SYSTEM "chrome://global/locale/global.dtd"> + %globalDTD; +]> + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>&syncProgress.pageTitle;</title> + + <link rel="stylesheet" type="text/css" media="all" + href="chrome://browser/skin/syncProgress.css"/> + + <link rel="icon" type="image/png" id="favicon" + href="chrome://browser/skin/sync-16.png"/> + + <script type="text/javascript;version=1.8" + src="chrome://browser/content/sync/progress.js"/> + </head> + <body onload="onLoad(event)" onunload="onUnload(event)" dir="&locale.dir;"> + <title>&setup.successPage.title;</title> + <div id="floatingBox" class="main-content"> + <div id="title"> + <h1>&setup.successPage.title;</h1> + </div> + <div id="successLogo"> + <img id="brandSyncLogo" src="chrome://browser/skin/sync-128.png" alt="&syncProgress.logoAltText;" /> + </div> + <div id="loadingText"> + <p id="blurb">&syncProgress.textBlurb; </p> + </div> + <div id="progressBar"> + <progress id="uploadProgressBar" value="0"/> + </div> + <div id="bottomRow"> + <button id="closeButton" onclick="closeTab()">&syncProgress.closeButton; </button> + </div> + </div> + </body> +</html> diff --git a/application/basilisk/components/sync/quota.js b/application/basilisk/components/sync/quota.js new file mode 100644 index 000000000..b416a44cc --- /dev/null +++ b/application/basilisk/components/sync/quota.js @@ -0,0 +1,247 @@ +/* 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/. */ + +const Ci = Components.interfaces; +const Cc = Components.classes; +const Cr = Components.results; +const Cu = Components.utils; + +Cu.import("resource://services-sync/main.js"); +Cu.import("resource://gre/modules/DownloadUtils.jsm"); + +var gSyncQuota = { + + init: function init() { + this.bundle = document.getElementById("quotaStrings"); + let caption = document.getElementById("treeCaption"); + caption.firstChild.nodeValue = this.bundle.getString("quota.treeCaption.label"); + + gUsageTreeView.init(); + this.tree = document.getElementById("usageTree"); + this.tree.view = gUsageTreeView; + + this.loadData(); + }, + + loadData: function loadData() { + this._usage_req = Weave.Service.getStorageInfo(Weave.INFO_COLLECTION_USAGE, + function (error, usage) { + delete gSyncQuota._usage_req; + // displayUsageData handles null values, so no need to check 'error'. + gUsageTreeView.displayUsageData(usage); + }); + + let usageLabel = document.getElementById("usageLabel"); + let bundle = this.bundle; + + this._quota_req = Weave.Service.getStorageInfo(Weave.INFO_QUOTA, + function (error, quota) { + delete gSyncQuota._quota_req; + + if (error) { + usageLabel.value = bundle.getString("quota.usageError.label"); + return; + } + let used = gSyncQuota.convertKB(quota[0]); + if (!quota[1]) { + // No quota on the server. + usageLabel.value = bundle.getFormattedString( + "quota.usageNoQuota.label", used); + return; + } + let percent = Math.round(100 * quota[0] / quota[1]); + let total = gSyncQuota.convertKB(quota[1]); + usageLabel.value = bundle.getFormattedString( + "quota.usagePercentage.label", [percent].concat(used).concat(total)); + }); + }, + + onCancel: function onCancel() { + if (this._usage_req) { + this._usage_req.abort(); + } + if (this._quota_req) { + this._quota_req.abort(); + } + return true; + }, + + onAccept: function onAccept() { + let engines = gUsageTreeView.getEnginesToDisable(); + for each (let engine in engines) { + Weave.Service.engineManager.get(engine).enabled = false; + } + if (engines.length) { + // The 'Weave' object will disappear once the window closes. + let Service = Weave.Service; + Weave.Utils.nextTick(function() { Service.sync(); }); + } + return this.onCancel(); + }, + + convertKB: function convertKB(value) { + return DownloadUtils.convertByteUnits(value * 1024); + } + +}; + +var gUsageTreeView = { + + _ignored: {keys: true, + meta: true, + clients: true}, + + /* + * Internal data structures underlaying the tree. + */ + _collections: [], + _byname: {}, + + init: function init() { + let retrievingLabel = gSyncQuota.bundle.getString("quota.retrieving.label"); + for each (let engine in Weave.Service.engineManager.getEnabled()) { + if (this._ignored[engine.name]) + continue; + + // Some engines use the same pref, which means they can only be turned on + // and off together. We need to combine them here as well. + let existing = this._byname[engine.prefName]; + if (existing) { + existing.engines.push(engine.name); + continue; + } + + let obj = {name: engine.prefName, + title: this._collectionTitle(engine), + engines: [engine.name], + enabled: true, + sizeLabel: retrievingLabel}; + this._collections.push(obj); + this._byname[engine.prefName] = obj; + } + }, + + _collectionTitle: function _collectionTitle(engine) { + try { + return gSyncQuota.bundle.getString( + "collection." + engine.prefName + ".label"); + } catch (ex) { + return engine.Name; + } + }, + + /* + * Process the quota information as returned by info/collection_usage. + */ + displayUsageData: function displayUsageData(data) { + for each (let coll in this._collections) { + coll.size = 0; + // If we couldn't retrieve any data, just blank out the label. + if (!data) { + coll.sizeLabel = ""; + continue; + } + + for each (let engineName in coll.engines) + coll.size += data[engineName] || 0; + let sizeLabel = ""; + sizeLabel = gSyncQuota.bundle.getFormattedString( + "quota.sizeValueUnit.label", gSyncQuota.convertKB(coll.size)); + coll.sizeLabel = sizeLabel; + } + let sizeColumn = this.treeBox.columns.getNamedColumn("size"); + this.treeBox.invalidateColumn(sizeColumn); + }, + + /* + * Handle click events on the tree. + */ + onTreeClick: function onTreeClick(event) { + if (event.button == 2) + return; + + let cell = this.treeBox.getCellAt(event.clientX, event.clientY); + if (cell.col && cell.col.id == "enabled") + this.toggle(cell.row); + }, + + /* + * Toggle enabled state of an engine. + */ + toggle: function toggle(row) { + // Update the tree + let collection = this._collections[row]; + collection.enabled = !collection.enabled; + this.treeBox.invalidateRow(row); + }, + + /* + * Return a list of engines (or rather their pref names) that should be + * disabled. + */ + getEnginesToDisable: function getEnginesToDisable() { + // Tycho: return [coll.name for each (coll in this._collections) if (!coll.enabled)]; + let engines = []; + for each (let coll in this._collections) { + if (!coll.enabled) { + engines.push(coll.name); + } + } + return engines; + }, + + // nsITreeView + + get rowCount() { + return this._collections.length; + }, + + getRowProperties: function(index) { return ""; }, + getCellProperties: function(row, col) { return ""; }, + getColumnProperties: function(col) { return ""; }, + isContainer: function(index) { return false; }, + isContainerOpen: function(index) { return false; }, + isContainerEmpty: function(index) { return false; }, + isSeparator: function(index) { return false; }, + isSorted: function() { return false; }, + canDrop: function(index, orientation, dataTransfer) { return false; }, + drop: function(row, orientation, dataTransfer) {}, + getParentIndex: function(rowIndex) {}, + hasNextSibling: function(rowIndex, afterIndex) { return false; }, + getLevel: function(index) { return 0; }, + getImageSrc: function(row, col) {}, + + getCellValue: function(row, col) { + return this._collections[row].enabled; + }, + + getCellText: function getCellText(row, col) { + let collection = this._collections[row]; + switch (col.id) { + case "collection": + return collection.title; + case "size": + return collection.sizeLabel; + default: + return ""; + } + }, + + setTree: function setTree(tree) { + this.treeBox = tree; + }, + + toggleOpenState: function(index) {}, + cycleHeader: function(col) {}, + selectionChanged: function() {}, + cycleCell: function(row, col) {}, + isEditable: function(row, col) { return false; }, + isSelectable: function (row, col) { return false; }, + setCellValue: function(row, col, value) {}, + setCellText: function(row, col, value) {}, + performAction: function(action) {}, + performActionOnRow: function(action, row) {}, + performActionOnCell: function(action, row, col) {} + +}; diff --git a/application/basilisk/components/sync/quota.xul b/application/basilisk/components/sync/quota.xul new file mode 100644 index 000000000..99e6ed78b --- /dev/null +++ b/application/basilisk/components/sync/quota.xul @@ -0,0 +1,65 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncQuota.css"?> + +<!DOCTYPE dialog [ +<!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> +<!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> +<!ENTITY % syncQuotaDTD SYSTEM "chrome://browser/locale/syncQuota.dtd"> +%brandDTD; +%syncBrandDTD; +%syncQuotaDTD; +]> +<dialog id="quotaDialog" + windowtype="Sync:ViewQuota" + persist="screenX screenY width height" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:html="http://www.w3.org/1999/xhtml" + onload="gSyncQuota.init()" + buttons="accept,cancel" + title=""a.dialogTitle.label;" + ondialogcancel="return gSyncQuota.onCancel();" + ondialogaccept="return gSyncQuota.onAccept();"> + + <script type="application/javascript" + src="chrome://browser/content/sync/quota.js"/> + + <stringbundleset id="stringbundleset"> + <stringbundle id="quotaStrings" + src="chrome://browser/locale/syncQuota.properties"/> + </stringbundleset> + + <vbox flex="1"> + <label id="usageLabel" + value=""a.retrievingInfo.label;"/> + <separator/> + <tree id="usageTree" + seltype="single" + hidecolumnpicker="true" + onclick="gUsageTreeView.onTreeClick(event);" + flex="1"> + <treecols> + <treecol id="enabled" + type="checkbox" + fixed="true"/> + <splitter class="tree-splitter"/> + <treecol id="collection" + label=""a.typeColumn.label;" + flex="1"/> + <splitter class="tree-splitter"/> + <treecol id="size" + label=""a.sizeColumn.label;" + flex="1"/> + </treecols> + <treechildren flex="1"/> + </tree> + <separator/> + <description id="treeCaption"> </description> + </vbox> + +</dialog> diff --git a/application/basilisk/components/sync/setup.js b/application/basilisk/components/sync/setup.js new file mode 100644 index 000000000..e8d67a5f6 --- /dev/null +++ b/application/basilisk/components/sync/setup.js @@ -0,0 +1,1071 @@ +/* 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/. */ + +var Ci = Components.interfaces; +var Cc = Components.classes; +var Cr = Components.results; +var Cu = Components.utils; + +// page consts + +const PAIR_PAGE = 0; +const INTRO_PAGE = 1; +const NEW_ACCOUNT_START_PAGE = 2; +const EXISTING_ACCOUNT_CONNECT_PAGE = 3; +const EXISTING_ACCOUNT_LOGIN_PAGE = 4; +const OPTIONS_PAGE = 5; +const OPTIONS_CONFIRM_PAGE = 6; + +// Broader than we'd like, but after this changed from api-secure.recaptcha.net +// we had no choice. At least we only do this for the duration of setup. +// See discussion in Bugs 508112 and 653307. +const RECAPTCHA_DOMAIN = "https://www.google.com"; + +const PIN_PART_LENGTH = 4; + +Cu.import("resource://services-sync/main.js"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/PlacesUtils.jsm"); +Cu.import("resource://gre/modules/PluralForm.jsm"); + + +function setVisibility(element, visible) { + element.style.visibility = visible ? "visible" : "hidden"; +} + +var gSyncSetup = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, + Ci.nsIWebProgressListener, + Ci.nsISupportsWeakReference]), + + captchaBrowser: null, + wizard: null, + _disabledSites: [], + + status: { + password: false, + email: false, + server: false + }, + + get _remoteSites() [Weave.Service.serverURL, RECAPTCHA_DOMAIN], + + get _usingMainServers() { + if (this._settingUpNew) + return document.getElementById("server").selectedIndex == 0; + return document.getElementById("existingServer").selectedIndex == 0; + }, + + init: function () { + let obs = [ + ["weave:service:change-passphrase", "onResetPassphrase"], + ["weave:service:login:start", "onLoginStart"], + ["weave:service:login:error", "onLoginEnd"], + ["weave:service:login:finish", "onLoginEnd"]]; + + // Add the observers now and remove them on unload + let self = this; + let addRem = function(add) { + obs.forEach(function([topic, func]) { + //XXXzpao This should use Services.obs.* but Weave's Obs does nice handling + // of `this`. Fix in a followup. (bug 583347) + if (add) + Weave.Svc.Obs.add(topic, self[func], self); + else + Weave.Svc.Obs.remove(topic, self[func], self); + }); + }; + addRem(true); + window.addEventListener("unload", function() addRem(false), false); + + window.setTimeout(function () { + // Force Service to be loaded so that engines are registered. + // See Bug 670082. + Weave.Service; + }, 0); + + this.captchaBrowser = document.getElementById("captcha"); + + this.wizardType = null; + if (window.arguments && window.arguments[0]) { + this.wizardType = window.arguments[0]; + } + switch (this.wizardType) { + case null: + this.wizard.pageIndex = INTRO_PAGE; + // Fall through! + case "pair": + this.captchaBrowser.addProgressListener(this); + Weave.Svc.Prefs.set("firstSync", "notReady"); + break; + case "reset": + this._resettingSync = true; + this.wizard.pageIndex = OPTIONS_PAGE; + break; + } + + this.wizard.getButton("extra1").label = + this._stringBundle.GetStringFromName("button.syncOptions.label"); + + // Remember these values because the options pages change them temporarily. + this._nextButtonLabel = this.wizard.getButton("next").label; + this._nextButtonAccesskey = this.wizard.getButton("next") + .getAttribute("accesskey"); + this._backButtonLabel = this.wizard.getButton("back").label; + this._backButtonAccesskey = this.wizard.getButton("back") + .getAttribute("accesskey"); + }, + + startNewAccountSetup: function () { + if (!Weave.Utils.ensureMPUnlocked()) + return false; + this._settingUpNew = true; + this.wizard.pageIndex = NEW_ACCOUNT_START_PAGE; + }, + + useExistingAccount: function () { + if (!Weave.Utils.ensureMPUnlocked()) + return false; + this._settingUpNew = false; + if (this.wizardType == "pair") { + // We're already pairing, so there's no point in pairing again. + // Go straight to the manual login page. + this.wizard.pageIndex = EXISTING_ACCOUNT_LOGIN_PAGE; + } else { + this.wizard.pageIndex = EXISTING_ACCOUNT_CONNECT_PAGE; + } + }, + + resetPassphrase: function resetPassphrase() { + // Apply the existing form fields so that + // Weave.Service.changePassphrase() has the necessary credentials. + Weave.Service.identity.account = document.getElementById("existingAccountName").value; + Weave.Service.identity.basicPassword = document.getElementById("existingPassword").value; + + // Generate a new passphrase so that Weave.Service.login() will + // actually do something. + let passphrase = Weave.Utils.generatePassphrase(); + Weave.Service.identity.syncKey = passphrase; + + // Only open the dialog if username + password are actually correct. + Weave.Service.login(); + if ([Weave.LOGIN_FAILED_INVALID_PASSPHRASE, + Weave.LOGIN_FAILED_NO_PASSPHRASE, + Weave.LOGIN_SUCCEEDED].indexOf(Weave.Status.login) == -1) { + return; + } + + // Hide any errors about the passphrase, we know it's not right. + let feedback = document.getElementById("existingPassphraseFeedbackRow"); + feedback.hidden = true; + let el = document.getElementById("existingPassphrase"); + el.value = Weave.Utils.hyphenatePassphrase(passphrase); + + // changePassphrase() will sync, make sure we set the "firstSync" pref + // according to the user's pref. + Weave.Svc.Prefs.reset("firstSync"); + this.setupInitialSync(); + gSyncUtils.resetPassphrase(true); + }, + + onResetPassphrase: function () { + document.getElementById("existingPassphrase").value = + Weave.Utils.hyphenatePassphrase(Weave.Service.identity.syncKey); + this.checkFields(); + this.wizard.advance(); + }, + + onLoginStart: function () { + this.toggleLoginFeedback(false); + }, + + onLoginEnd: function () { + this.toggleLoginFeedback(true); + }, + + sendCredentialsAfterSync: function () { + let send = function() { + Services.obs.removeObserver("weave:service:sync:finish", send); + Services.obs.removeObserver("weave:service:sync:error", send); + let credentials = {account: Weave.Service.identity.account, + password: Weave.Service.identity.basicPassword, + synckey: Weave.Service.identity.syncKey, + serverURL: Weave.Service.serverURL}; + this._jpakeclient.sendAndComplete(credentials); + }.bind(this); + Services.obs.addObserver("weave:service:sync:finish", send, false); + Services.obs.addObserver("weave:service:sync:error", send, false); + }, + + toggleLoginFeedback: function (stop) { + document.getElementById("login-throbber").hidden = stop; + let password = document.getElementById("existingPasswordFeedbackRow"); + let server = document.getElementById("existingServerFeedbackRow"); + let passphrase = document.getElementById("existingPassphraseFeedbackRow"); + + if (!stop || (Weave.Status.login == Weave.LOGIN_SUCCEEDED)) { + password.hidden = server.hidden = passphrase.hidden = true; + return; + } + + let feedback; + switch (Weave.Status.login) { + case Weave.LOGIN_FAILED_NETWORK_ERROR: + case Weave.LOGIN_FAILED_SERVER_ERROR: + feedback = server; + break; + case Weave.LOGIN_FAILED_LOGIN_REJECTED: + case Weave.LOGIN_FAILED_NO_USERNAME: + case Weave.LOGIN_FAILED_NO_PASSWORD: + feedback = password; + break; + case Weave.LOGIN_FAILED_INVALID_PASSPHRASE: + feedback = passphrase; + break; + } + this._setFeedbackMessage(feedback, false, Weave.Status.login); + }, + + setupInitialSync: function () { + let action = document.getElementById("mergeChoiceRadio").selectedItem.id; + switch (action) { + case "resetClient": + // if we're not resetting sync, we don't need to explicitly + // call resetClient + if (!this._resettingSync) + return; + // otherwise, fall through + case "wipeClient": + case "wipeRemote": + Weave.Svc.Prefs.set("firstSync", action); + break; + } + }, + + // fun with validation! + checkFields: function () { + this.wizard.canAdvance = this.readyToAdvance(); + }, + + readyToAdvance: function () { + switch (this.wizard.pageIndex) { + case INTRO_PAGE: + return false; + case NEW_ACCOUNT_START_PAGE: + for (let i in this.status) { + if (!this.status[i]) + return false; + } + if (this._usingMainServers) + return document.getElementById("tos").checked; + + return true; + case EXISTING_ACCOUNT_LOGIN_PAGE: + let hasUser = document.getElementById("existingAccountName").value != ""; + let hasPass = document.getElementById("existingPassword").value != ""; + let hasKey = document.getElementById("existingPassphrase").value != ""; + + if (hasUser && hasPass && hasKey) { + if (this._usingMainServers) + return true; + + if (this._validateServer(document.getElementById("existingServer"))) { + return true; + } + } + return false; + } + // Default, e.g. wizard's special page -1 etc. + return true; + }, + + onPINInput: function onPINInput(textbox) { + if (textbox && textbox.value.length == PIN_PART_LENGTH) { + this.nextFocusEl[textbox.id].focus(); + } + this.wizard.canAdvance = (this.pin1.value.length == PIN_PART_LENGTH && + this.pin2.value.length == PIN_PART_LENGTH && + this.pin3.value.length == PIN_PART_LENGTH); + }, + + onEmailInput: function () { + // Check account validity when the user stops typing for 1 second. + if (this._checkAccountTimer) + window.clearTimeout(this._checkAccountTimer); + this._checkAccountTimer = window.setTimeout(function () { + gSyncSetup.checkAccount(); + }, 1000); + }, + + checkAccount: function() { + delete this._checkAccountTimer; + let value = Weave.Utils.normalizeAccount( + document.getElementById("weaveEmail").value); + if (!value) { + this.status.email = false; + this.checkFields(); + return; + } + + let re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + let feedback = document.getElementById("emailFeedbackRow"); + let valid = re.test(value); + + let str = ""; + if (!valid) { + str = "invalidEmail.label"; + } else { + let availCheck = Weave.Service.checkAccount(value); + valid = availCheck == "available"; + if (!valid) { + if (availCheck == "notAvailable") + str = "usernameNotAvailable.label"; + else + str = availCheck; + } + } + + this._setFeedbackMessage(feedback, valid, str); + this.status.email = valid; + if (valid) + Weave.Service.identity.account = value; + this.checkFields(); + }, + + onPasswordChange: function () { + let password = document.getElementById("weavePassword"); + let pwconfirm = document.getElementById("weavePasswordConfirm"); + let [valid, errorString] = gSyncUtils.validatePassword(password, pwconfirm); + + let feedback = document.getElementById("passwordFeedbackRow"); + this._setFeedback(feedback, valid, errorString); + + this.status.password = valid; + this.checkFields(); + }, + + onPageShow: function() { + switch (this.wizard.pageIndex) { + case PAIR_PAGE: + this.wizard.getButton("back").hidden = true; + this.wizard.getButton("extra1").hidden = true; + this.onPINInput(); + this.pin1.focus(); + break; + case INTRO_PAGE: + // We may not need the captcha in the Existing Account branch of the + // wizard. However, we want to preload it to avoid any flickering while + // the Create Account page is shown. + this.loadCaptcha(); + this.wizard.getButton("next").hidden = true; + this.wizard.getButton("back").hidden = true; + this.wizard.getButton("extra1").hidden = true; + this.checkFields(); + break; + case NEW_ACCOUNT_START_PAGE: + this.wizard.getButton("extra1").hidden = false; + this.wizard.getButton("next").hidden = false; + this.wizard.getButton("back").hidden = false; + this.onServerCommand(); + this.wizard.canRewind = true; + this.checkFields(); + break; + case EXISTING_ACCOUNT_CONNECT_PAGE: + Weave.Svc.Prefs.set("firstSync", "existingAccount"); + this.wizard.getButton("next").hidden = false; + this.wizard.getButton("back").hidden = false; + this.wizard.getButton("extra1").hidden = false; + this.wizard.canAdvance = false; + this.wizard.canRewind = true; + this.startEasySetup(); + break; + case EXISTING_ACCOUNT_LOGIN_PAGE: + this.wizard.getButton("next").hidden = false; + this.wizard.getButton("back").hidden = false; + this.wizard.getButton("extra1").hidden = false; + this.wizard.canRewind = true; + this.checkFields(); + break; + case OPTIONS_PAGE: + this.wizard.canRewind = false; + this.wizard.canAdvance = true; + if (!this._resettingSync) { + this.wizard.getButton("next").label = + this._stringBundle.GetStringFromName("button.syncOptionsDone.label"); + this.wizard.getButton("next").removeAttribute("accesskey"); + } + this.wizard.getButton("next").hidden = false; + this.wizard.getButton("back").hidden = true; + this.wizard.getButton("cancel").hidden = !this._resettingSync; + this.wizard.getButton("extra1").hidden = true; + document.getElementById("syncComputerName").value = Weave.Service.clientsEngine.localName; + document.getElementById("syncOptions").collapsed = this._resettingSync; + document.getElementById("mergeOptions").collapsed = this._settingUpNew; + break; + case OPTIONS_CONFIRM_PAGE: + this.wizard.canRewind = true; + this.wizard.canAdvance = true; + this.wizard.getButton("back").label = + this._stringBundle.GetStringFromName("button.syncOptionsCancel.label"); + this.wizard.getButton("back").removeAttribute("accesskey"); + this.wizard.getButton("back").hidden = this._resettingSync; + this.wizard.getButton("next").hidden = false; + this.wizard.getButton("finish").hidden = true; + break; + } + }, + + onWizardAdvance: function () { + // Check pageIndex so we don't prompt before the Sync setup wizard appears. + // This is a fallback in case the Master Password gets locked mid-wizard. + if ((this.wizard.pageIndex >= 0) && + !Weave.Utils.ensureMPUnlocked()) { + return false; + } + + switch (this.wizard.pageIndex) { + case PAIR_PAGE: + this.startPairing(); + return false; + case NEW_ACCOUNT_START_PAGE: + // If the user selects Next (e.g. by hitting enter) when we haven't + // executed the delayed checks yet, execute them immediately. + if (this._checkAccountTimer) { + this.checkAccount(); + } + if (this._checkServerTimer) { + this.checkServer(); + } + if (!this.wizard.canAdvance) { + return false; + } + + let doc = this.captchaBrowser.contentDocument; + let getField = function getField(field) { + let node = doc.getElementById("recaptcha_" + field + "_field"); + return node && node.value; + }; + + // Display throbber + let feedback = document.getElementById("captchaFeedback"); + let image = feedback.firstChild; + let label = image.nextSibling; + image.setAttribute("status", "active"); + label.value = this._stringBundle.GetStringFromName("verifying.label"); + setVisibility(feedback, true); + + let password = document.getElementById("weavePassword").value; + let email = Weave.Utils.normalizeAccount( + document.getElementById("weaveEmail").value); + let challenge = getField("challenge"); + let response = getField("response"); + + let error = Weave.Service.createAccount(email, password, + challenge, response); + + if (error == null) { + Weave.Service.identity.account = email; + Weave.Service.identity.basicPassword = password; + Weave.Service.identity.syncKey = Weave.Utils.generatePassphrase(); + this._handleNoScript(false); + Weave.Svc.Prefs.set("firstSync", "newAccount"); + this.wizardFinish(); + return false; + } + + image.setAttribute("status", "error"); + label.value = Weave.Utils.getErrorString(error); + return false; + case EXISTING_ACCOUNT_LOGIN_PAGE: + Weave.Service.identity.account = Weave.Utils.normalizeAccount( + document.getElementById("existingAccountName").value); + Weave.Service.identity.basicPassword = + document.getElementById("existingPassword").value; + let pp = document.getElementById("existingPassphrase").value; + Weave.Service.identity.syncKey = Weave.Utils.normalizePassphrase(pp); + if (Weave.Service.login()) { + this.wizardFinish(); + } + return false; + case OPTIONS_PAGE: + let desc = document.getElementById("mergeChoiceRadio").selectedIndex; + // No confirmation needed on new account setup or merge option + // with existing account. + if (this._settingUpNew || (!this._resettingSync && desc == 0)) + return this.returnFromOptions(); + return this._handleChoice(); + case OPTIONS_CONFIRM_PAGE: + if (this._resettingSync) { + this.wizardFinish(); + return false; + } + return this.returnFromOptions(); + } + return true; + }, + + onWizardBack: function () { + switch (this.wizard.pageIndex) { + case NEW_ACCOUNT_START_PAGE: + case EXISTING_ACCOUNT_LOGIN_PAGE: + this.wizard.pageIndex = INTRO_PAGE; + return false; + case EXISTING_ACCOUNT_CONNECT_PAGE: + this.abortEasySetup(); + this.wizard.pageIndex = INTRO_PAGE; + return false; + case EXISTING_ACCOUNT_LOGIN_PAGE: + // If we were already pairing on entry, we went straight to the manual + // login page. If subsequently we go back, return to the page that lets + // us choose whether we already have an account. + if (this.wizardType == "pair") { + this.wizard.pageIndex = INTRO_PAGE; + return false; + } + return true; + case OPTIONS_CONFIRM_PAGE: + // Backing up from the confirmation page = resetting first sync to merge. + document.getElementById("mergeChoiceRadio").selectedIndex = 0; + return this.returnFromOptions(); + } + return true; + }, + + wizardFinish: function () { + this.setupInitialSync(); + + if (this.wizardType == "pair") { + this.completePairing(); + } + + if (!this._resettingSync) { + function isChecked(element) { + return document.getElementById(element).hasAttribute("checked"); + } + + let prefs = ["engine.bookmarks", "engine.passwords", "engine.history", + "engine.tabs", "engine.prefs", "engine.addons"]; + for (let i = 0;i < prefs.length;i++) { + Weave.Svc.Prefs.set(prefs[i], isChecked(prefs[i])); + } + + // XXX: Addons syncing is currently not operational; + // Make doubly-sure to always disable addons syncing pref + Weave.Svc.Prefs.set("engine.addons", false); + + this._handleNoScript(false); + if (Weave.Svc.Prefs.get("firstSync", "") == "notReady") + Weave.Svc.Prefs.reset("firstSync"); + + Weave.Service.persistLogin(); + Weave.Svc.Obs.notify("weave:service:setup-complete"); + + gSyncUtils.openFirstSyncProgressPage(); + } + Weave.Utils.nextTick(Weave.Service.sync, Weave.Service); + window.close(); + }, + + onWizardCancel: function () { + if (this._resettingSync) + return; + + this.abortEasySetup(); + this._handleNoScript(false); + Weave.Service.startOver(); + }, + + onSyncOptions: function () { + this._beforeOptionsPage = this.wizard.pageIndex; + this.wizard.pageIndex = OPTIONS_PAGE; + }, + + returnFromOptions: function() { + this.wizard.getButton("next").label = this._nextButtonLabel; + this.wizard.getButton("next").setAttribute("accesskey", + this._nextButtonAccesskey); + this.wizard.getButton("back").label = this._backButtonLabel; + this.wizard.getButton("back").setAttribute("accesskey", + this._backButtonAccesskey); + this.wizard.getButton("cancel").hidden = false; + this.wizard.getButton("extra1").hidden = false; + this.wizard.pageIndex = this._beforeOptionsPage; + return false; + }, + + startPairing: function startPairing() { + this.pairDeviceErrorRow.hidden = true; + // When onAbort is called, Weave may already be gone. + const JPAKE_ERROR_USERABORT = Weave.JPAKE_ERROR_USERABORT; + + let self = this; + let jpakeclient = this._jpakeclient = new Weave.JPAKEClient({ + onPaired: function onPaired() { + self.wizard.pageIndex = INTRO_PAGE; + }, + onComplete: function onComplete() { + // This method will never be called since SendCredentialsController + // will take over after the wizard completes. + }, + onAbort: function onAbort(error) { + delete self._jpakeclient; + + // Aborted by user, ignore. The window is almost certainly going to close + // or is already closed. + if (error == JPAKE_ERROR_USERABORT) { + return; + } + + self.pairDeviceErrorRow.hidden = false; + self.pairDeviceThrobber.hidden = true; + self.pin1.value = self.pin2.value = self.pin3.value = ""; + self.pin1.disabled = self.pin2.disabled = self.pin3.disabled = false; + if (self.wizard.pageIndex == PAIR_PAGE) { + self.pin1.focus(); + } + } + }); + this.pairDeviceThrobber.hidden = false; + this.pin1.disabled = this.pin2.disabled = this.pin3.disabled = true; + this.wizard.canAdvance = false; + + let pin = this.pin1.value + this.pin2.value + this.pin3.value; + let expectDelay = true; + jpakeclient.pairWithPIN(pin, expectDelay); + }, + + completePairing: function completePairing() { + if (!this._jpakeclient) { + // The channel was aborted while we were setting up the account + // locally. XXX TODO should we do anything here, e.g. tell + // the user on the last wizard page that it's ok, they just + // have to pair again? + return; + } + let controller = new Weave.SendCredentialsController(this._jpakeclient, + Weave.Service); + this._jpakeclient.controller = controller; + }, + + startEasySetup: function () { + // Don't do anything if we have a client already (e.g. we went to + // Sync Options and just came back). + if (this._jpakeclient) + return; + + // When onAbort is called, Weave may already be gone + const JPAKE_ERROR_USERABORT = Weave.JPAKE_ERROR_USERABORT; + + let self = this; + this._jpakeclient = new Weave.JPAKEClient({ + displayPIN: function displayPIN(pin) { + document.getElementById("easySetupPIN1").value = pin.slice(0, 4); + document.getElementById("easySetupPIN2").value = pin.slice(4, 8); + document.getElementById("easySetupPIN3").value = pin.slice(8); + }, + + onPairingStart: function onPairingStart() {}, + + onComplete: function onComplete(credentials) { + Weave.Service.identity.account = credentials.account; + Weave.Service.identity.basicPassword = credentials.password; + Weave.Service.identity.syncKey = credentials.synckey; + Weave.Service.serverURL = credentials.serverURL; + gSyncSetup.wizardFinish(); + }, + + onAbort: function onAbort(error) { + delete self._jpakeclient; + + // Ignore if wizard is aborted. + if (error == JPAKE_ERROR_USERABORT) + return; + + // Automatically go to manual setup if we couldn't acquire a channel. + if (error == Weave.JPAKE_ERROR_CHANNEL) { + self.wizard.pageIndex = EXISTING_ACCOUNT_LOGIN_PAGE; + return; + } + + // Restart on all other errors. + self.startEasySetup(); + } + }); + this._jpakeclient.receiveNoPIN(); + }, + + abortEasySetup: function () { + document.getElementById("easySetupPIN1").value = ""; + document.getElementById("easySetupPIN2").value = ""; + document.getElementById("easySetupPIN3").value = ""; + if (!this._jpakeclient) + return; + + this._jpakeclient.abort(); + delete this._jpakeclient; + }, + + manualSetup: function () { + this.abortEasySetup(); + this.wizard.pageIndex = EXISTING_ACCOUNT_LOGIN_PAGE; + }, + + // _handleNoScript is needed because it blocks the captcha. So we temporarily + // allow the necessary sites so that we can verify the user is in fact a human. + // This was done with the help of Giorgio (NoScript author). See bug 508112. + _handleNoScript: function (addExceptions) { + // if NoScript isn't installed, or is disabled, bail out. + let ns = Cc["@maone.net/noscript-service;1"]; + if (ns == null) + return; + + ns = ns.getService().wrappedJSObject; + if (addExceptions) { + this._remoteSites.forEach(function(site) { + site = ns.getSite(site); + if (!ns.isJSEnabled(site)) { + this._disabledSites.push(site); // save status + ns.setJSEnabled(site, true); // allow site + } + }, this); + } + else { + this._disabledSites.forEach(function(site) { + ns.setJSEnabled(site, false); + }); + this._disabledSites = []; + } + }, + + onExistingServerCommand: function () { + let control = document.getElementById("existingServer"); + if (control.selectedIndex == 0) { + control.removeAttribute("editable"); + Weave.Svc.Prefs.reset("serverURL"); + } else { + control.setAttribute("editable", "true"); + // Force a style flush to ensure that the binding is attached. + control.clientTop; + control.value = ""; + control.inputField.focus(); + } + document.getElementById("existingServerFeedbackRow").hidden = true; + this.checkFields(); + }, + + onExistingServerInput: function () { + // Check custom server validity when the user stops typing for 1 second. + if (this._existingServerTimer) + window.clearTimeout(this._existingServerTimer); + this._existingServerTimer = window.setTimeout(function () { + gSyncSetup.checkFields(); + }, 1000); + }, + + onServerCommand: function () { + setVisibility(document.getElementById("TOSRow"), this._usingMainServers); + let control = document.getElementById("server"); + if (!this._usingMainServers) { + control.setAttribute("editable", "true"); + // Force a style flush to ensure that the binding is attached. + control.clientTop; + control.value = ""; + control.inputField.focus(); + // checkServer() will call checkAccount() and checkFields(). + this.checkServer(); + return; + } + control.removeAttribute("editable"); + Weave.Svc.Prefs.reset("serverURL"); + if (this._settingUpNew) { + this.loadCaptcha(); + } + this.checkAccount(); + this.status.server = true; + document.getElementById("serverFeedbackRow").hidden = true; + this.checkFields(); + }, + + onServerInput: function () { + // Check custom server validity when the user stops typing for 1 second. + if (this._checkServerTimer) + window.clearTimeout(this._checkServerTimer); + this._checkServerTimer = window.setTimeout(function () { + gSyncSetup.checkServer(); + }, 1000); + }, + + checkServer: function () { + delete this._checkServerTimer; + let el = document.getElementById("server"); + let valid = false; + let feedback = document.getElementById("serverFeedbackRow"); + let str = ""; + if (el.value) { + valid = this._validateServer(el); + let str = valid ? "" : "serverInvalid.label"; + this._setFeedbackMessage(feedback, valid, str); + } + else + this._setFeedbackMessage(feedback, true); + + // Recheck account against the new server. + if (valid) + this.checkAccount(); + + this.status.server = valid; + this.checkFields(); + }, + + _validateServer: function (element) { + let valid = false; + let val = element.value; + if (!val) + return false; + + let uri = Weave.Utils.makeURI(val); + + if (!uri) + uri = Weave.Utils.makeURI("https://" + val); + + if (uri && this._settingUpNew) { + function isValid(uri) { + Weave.Service.serverURL = uri.spec; + let check = Weave.Service.checkAccount("a"); + return (check == "available" || check == "notAvailable"); + } + + if (uri.schemeIs("http")) { + uri.scheme = "https"; + if (isValid(uri)) + valid = true; + else + // setting the scheme back to http + uri.scheme = "http"; + } + if (!valid) + valid = isValid(uri); + + if (valid) { + this.loadCaptcha(); + } + } + else if (uri) { + valid = true; + Weave.Service.serverURL = uri.spec; + } + + if (valid) + element.value = Weave.Service.serverURL; + else + Weave.Svc.Prefs.reset("serverURL"); + + return valid; + }, + + _handleChoice: function () { + let desc = document.getElementById("mergeChoiceRadio").selectedIndex; + document.getElementById("chosenActionDeck").selectedIndex = desc; + switch (desc) { + case 1: + if (this._case1Setup) + break; + + let places_db = PlacesUtils.history + .QueryInterface(Ci.nsPIPlacesDatabase) + .DBConnection; + if (Weave.Service.engineManager.get("history").enabled) { + let daysOfHistory = 0; + let stm = places_db.createStatement( + "SELECT ROUND(( " + + "strftime('%s','now','localtime','utc') - " + + "( " + + "SELECT visit_date FROM moz_historyvisits " + + "ORDER BY visit_date ASC LIMIT 1 " + + ")/1000000 " + + ")/86400) AS daysOfHistory "); + + if (stm.step()) + daysOfHistory = stm.getInt32(0); + // Support %S for historical reasons (see bug 600141) + document.getElementById("historyCount").value = + PluralForm.get(daysOfHistory, + this._stringBundle.GetStringFromName("historyDaysCount.label")) + .replace("%S", daysOfHistory) + .replace("#1", daysOfHistory); + } else { + document.getElementById("historyCount").hidden = true; + } + + if (Weave.Service.engineManager.get("bookmarks").enabled) { + let bookmarks = 0; + let stm = places_db.createStatement( + "SELECT count(*) AS bookmarks " + + "FROM moz_bookmarks b " + + "LEFT JOIN moz_bookmarks t ON " + + "b.parent = t.id WHERE b.type = 1 AND t.parent <> :tag"); + stm.params.tag = PlacesUtils.tagsFolderId; + if (stm.executeStep()) + bookmarks = stm.row.bookmarks; + // Support %S for historical reasons (see bug 600141) + document.getElementById("bookmarkCount").value = + PluralForm.get(bookmarks, + this._stringBundle.GetStringFromName("bookmarksCount.label")) + .replace("%S", bookmarks) + .replace("#1", bookmarks); + } else { + document.getElementById("bookmarkCount").hidden = true; + } + + if (Weave.Service.engineManager.get("passwords").enabled) { + let logins = Services.logins.getAllLogins({}); + // Support %S for historical reasons (see bug 600141) + document.getElementById("passwordCount").value = + PluralForm.get(logins.length, + this._stringBundle.GetStringFromName("passwordsCount.label")) + .replace("%S", logins.length) + .replace("#1", logins.length); + } else { + document.getElementById("passwordCount").hidden = true; + } + + if (!Weave.Service.engineManager.get("prefs").enabled) { + document.getElementById("prefsWipe").hidden = true; + } + + let addonsEngine = Weave.Service.engineManager.get("addons"); + if (addonsEngine.enabled) { + let ids = addonsEngine._store.getAllIDs(); + let blessedcount = 0; + for each (let i in ids) { + if (i) { + blessedcount++; + } + } + // bug 600141 does not apply, as this does not have to support existing strings + document.getElementById("addonCount").value = + PluralForm.get(blessedcount, + this._stringBundle.GetStringFromName("addonsCount.label")) + .replace("#1", blessedcount); + } else { + document.getElementById("addonCount").hidden = true; + } + + this._case1Setup = true; + break; + case 2: + if (this._case2Setup) + break; + let count = 0; + function appendNode(label) { + let box = document.getElementById("clientList"); + let node = document.createElement("label"); + node.setAttribute("value", label); + node.setAttribute("class", "data indent"); + box.appendChild(node); + } + + for each (let name in Weave.Service.clientsEngine.stats.names) { + // Don't list the current client + if (name == Weave.Service.clientsEngine.localName) + continue; + + // Only show the first several client names + if (++count <= 5) + appendNode(name); + } + if (count > 5) { + // Support %S for historical reasons (see bug 600141) + let label = + PluralForm.get(count - 5, + this._stringBundle.GetStringFromName("additionalClientCount.label")) + .replace("%S", count - 5) + .replace("#1", count - 5); + appendNode(label); + } + this._case2Setup = true; + break; + } + + return true; + }, + + // sets class and string on a feedback element + // if no property string is passed in, we clear label/style + _setFeedback: function (element, success, string) { + element.hidden = success || !string; + let classname = success ? "success" : "error"; + let image = element.getElementsByAttribute("class", "statusIcon")[0]; + image.setAttribute("status", classname); + let label = element.getElementsByAttribute("class", "status")[0]; + label.value = string; + }, + + // shim + _setFeedbackMessage: function (element, success, string) { + let str = ""; + if (string) { + try { + str = this._stringBundle.GetStringFromName(string); + } catch(e) {} + + if (!str) + str = Weave.Utils.getErrorString(string); + } + this._setFeedback(element, success, str); + }, + + loadCaptcha: function loadCaptcha() { + let captchaURI = Weave.Service.miscAPI + "captcha_html"; + // First check for NoScript and whitelist the right sites. + this._handleNoScript(true); + if (this.captchaBrowser.currentURI.spec != captchaURI) { + this.captchaBrowser.loadURI(captchaURI); + } + }, + + onStateChange: function(webProgress, request, stateFlags, status) { + // We're only looking for the end of the frame load + if ((stateFlags & Ci.nsIWebProgressListener.STATE_STOP) == 0) + return; + if ((stateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) == 0) + return; + if ((stateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW) == 0) + return; + + // If we didn't find a captcha, assume it's not needed and don't show it. + let responseStatus = request.QueryInterface(Ci.nsIHttpChannel).responseStatus; + setVisibility(this.captchaBrowser, responseStatus != 404); + //XXX TODO we should really log any responseStatus other than 200 + }, + onProgressChange: function() {}, + onStatusChange: function() {}, + onSecurityChange: function() {}, + onLocationChange: function () {} +}; + +// Define lazy getters for various XUL elements. +// +// onWizardAdvance() and onPageShow() are run before init(), so we'll even +// define things that will almost certainly be used (like 'wizard') as a lazy +// getter here. +["wizard", + "pin1", + "pin2", + "pin3", + "pairDeviceErrorRow", + "pairDeviceThrobber"].forEach(function (id) { + XPCOMUtils.defineLazyGetter(gSyncSetup, id, function() { + return document.getElementById(id); + }); +}); +XPCOMUtils.defineLazyGetter(gSyncSetup, "nextFocusEl", function () { + return {pin1: this.pin2, + pin2: this.pin3, + pin3: this.wizard.getButton("next")}; +}); +XPCOMUtils.defineLazyGetter(gSyncSetup, "_stringBundle", function() { + return Services.strings.createBundle("chrome://browser/locale/syncSetup.properties"); +}); diff --git a/application/basilisk/components/sync/setup.xul b/application/basilisk/components/sync/setup.xul new file mode 100644 index 000000000..cf2cc77e4 --- /dev/null +++ b/application/basilisk/components/sync/setup.xul @@ -0,0 +1,491 @@ +<?xml version="1.0"?> + +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncSetup.css" type="text/css"?> +<?xml-stylesheet href="chrome://browser/skin/syncCommon.css" type="text/css"?> + +<!DOCTYPE window [ +<!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> +<!ENTITY % syncBrandDTD SYSTEM "chrome://browser/locale/syncBrand.dtd"> +<!ENTITY % syncSetupDTD SYSTEM "chrome://browser/locale/syncSetup.dtd"> +%brandDTD; +%syncBrandDTD; +%syncSetupDTD; +]> +<wizard id="wizard" + title="&accountSetupTitle.label;" + windowtype="Weave:AccountSetup" + persist="screenX screenY" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:html="http://www.w3.org/1999/xhtml" + onwizardnext="return gSyncSetup.onWizardAdvance()" + onwizardback="return gSyncSetup.onWizardBack()" + onwizardcancel="gSyncSetup.onWizardCancel()" + onload="gSyncSetup.init()"> + + <script type="application/javascript" + src="chrome://browser/content/sync/setup.js"/> + <script type="application/javascript" + src="chrome://browser/content/sync/utils.js"/> + <script type="application/javascript" + src="chrome://browser/content/utilityOverlay.js"/> + <script type="application/javascript" + src="chrome://global/content/printUtils.js"/> + + <wizardpage id="addDevicePage" + label="&pairDevice.title.label;" + onpageshow="gSyncSetup.onPageShow()"> + <description> + &pairDevice.dialog.description.label; + <label class="text-link" + value="&addDevice.showMeHow.label;" + href="http://www.palemoon.org/sync/help/easy-setup.shtml"/> + </description> + <separator class="groove-thin"/> + <description> + &addDevice.dialog.enterCode.label; + </description> + <separator class="groove-thin"/> + <vbox align="center"> + <textbox id="pin1" + class="pin" + oninput="gSyncSetup.onPINInput(this);" + onfocus="this.select();" + /> + <textbox id="pin2" + class="pin" + oninput="gSyncSetup.onPINInput(this);" + onfocus="this.select();" + /> + <textbox id="pin3" + class="pin" + oninput="gSyncSetup.onPINInput(this);" + onfocus="this.select();" + /> + </vbox> + <separator class="groove-thin"/> + <vbox id="pairDeviceThrobber" align="center" hidden="true"> + <image/> + </vbox> + <hbox id="pairDeviceErrorRow" pack="center" hidden="true"> + <image class="statusIcon" status="error"/> + <label class="status" + value="&addDevice.dialog.tryAgain.label;"/> + </hbox> + </wizardpage> + + <wizardpage id="pickSetupType" + label="&syncBrand.fullName.label;" + onpageshow="gSyncSetup.onPageShow()"> + <vbox align="center" flex="1"> + <description style="padding: 0 7em;"> + &setup.pickSetupType.description2; + </description> + <spacer flex="3"/> + <button id="newAccount" + class="accountChoiceButton" + label="&button.createNewAccount.label;" + oncommand="gSyncSetup.startNewAccountSetup()" + align="center"/> + <spacer flex="1"/> + </vbox> + <separator class="groove"/> + <vbox align="center" flex="1"> + <spacer flex="1"/> + <button id="existingAccount" + class="accountChoiceButton" + label="&button.haveAccount.label;" + oncommand="gSyncSetup.useExistingAccount()"/> + <spacer flex="3"/> + </vbox> + </wizardpage> + + <wizardpage label="&setup.newAccountDetailsPage.title.label;" + id="newAccountStart" + onextra1="gSyncSetup.onSyncOptions()" + onpageshow="gSyncSetup.onPageShow();"> + <grid> + <columns> + <column/> + <column class="inputColumn" flex="1"/> + </columns> + <rows> + <row id="emailRow" align="center"> + <label value="&setup.emailAddress.label;" + accesskey="&setup.emailAddress.accesskey;" + control="weaveEmail"/> + <textbox id="weaveEmail" + oninput="gSyncSetup.onEmailInput()"/> + </row> + <row id="emailFeedbackRow" align="center" hidden="true"> + <spacer/> + <hbox> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </row> + <row id="passwordRow" align="center"> + <label value="&setup.choosePassword.label;" + accesskey="&setup.choosePassword.accesskey;" + control="weavePassword"/> + <textbox id="weavePassword" + type="password" + onchange="gSyncSetup.onPasswordChange()"/> + </row> + <row id="confirmRow" align="center"> + <label value="&setup.confirmPassword.label;" + accesskey="&setup.confirmPassword.accesskey;" + control="weavePasswordConfirm"/> + <textbox id="weavePasswordConfirm" + type="password" + onchange="gSyncSetup.onPasswordChange()"/> + </row> + <row id="passwordFeedbackRow" align="center" hidden="true"> + <spacer/> + <hbox> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </row> + <row align="center"> + <label control="server" + value="&server.label;"/> + <menulist id="server" + oncommand="gSyncSetup.onServerCommand()" + oninput="gSyncSetup.onServerInput()"> + <menupopup> + <menuitem label="&serverType.default.label;" + value="main"/> + <menuitem label="&serverType.custom2.label;" + value="custom"/> + </menupopup> + </menulist> + </row> + <row id="serverFeedbackRow" align="center" hidden="true"> + <spacer/> + <hbox> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </row> + <row id="TOSRow" align="center"> + <spacer/> + <hbox align="center"> + <checkbox id="tos" + accesskey="&setup.tosAgree1.accesskey;" + oncommand="this.focus(); gSyncSetup.checkFields();"/> + <description id="tosDesc" + flex="1" + onclick="document.getElementById('tos').focus(); + document.getElementById('tos').click()"> + &setup.tosAgree1.label; + <label class="text-link inline-link" + onclick="event.stopPropagation();gSyncUtils.openToS();"> + &setup.tosLink.label; + </label> + &setup.tosAgree2.label; + <label class="text-link inline-link" + onclick="event.stopPropagation();gSyncUtils.openPrivacyPolicy();"> + &setup.ppLink.label; + </label> + &setup.tosAgree3.label; + </description> + </hbox> + </row> + </rows> + </grid> + <spacer flex="1"/> + <vbox flex="1" align="center"> + <browser height="150" + width="500" + id="captcha" + type="content" + disablehistory="true"/> + <spacer flex="1"/> + <hbox id="captchaFeedback"> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </vbox> + </wizardpage> + + <wizardpage id="addDevice" + label="&pairDevice.title.label;" + onextra1="gSyncSetup.onSyncOptions()" + onpageshow="gSyncSetup.onPageShow()"> + <description> + &pairDevice.setup.description.label; + <label class="text-link" + value="&addDevice.showMeHow.label;" + href="http://www.palemoon.org/sync/help/easy-setup.shtml"/> + </description> + <label value="&addDevice.setup.enterCode.label;" + control="easySetupPIN1"/> + <spacer flex="1"/> + <vbox align="center" flex="1"> + <textbox id="easySetupPIN1" + class="pin" + value="" + readonly="true" + /> + <textbox id="easySetupPIN2" + class="pin" + value="" + readonly="true" + /> + <textbox id="easySetupPIN3" + class="pin" + value="" + readonly="true" + /> + </vbox> + <spacer flex="3"/> + <label class="text-link" + value="&addDevice.dontHaveDevice.label;" + onclick="gSyncSetup.manualSetup();"/> + </wizardpage> + + <wizardpage id="existingAccount" + label="&setup.signInPage.title.label;" + onextra1="gSyncSetup.onSyncOptions()" + onpageshow="gSyncSetup.onPageShow()"> + <grid> + <columns> + <column/> + <column class="inputColumn" flex="1"/> + </columns> + <rows> + <row id="existingAccountRow" align="center"> + <label id="existingAccountLabel" + value="&signIn.account2.label;" + accesskey="&signIn.account2.accesskey;" + control="existingAccount"/> + <textbox id="existingAccountName" + oninput="gSyncSetup.checkFields(event)" + onchange="gSyncSetup.checkFields(event)"/> + </row> + <row id="existingPasswordRow" align="center"> + <label id="existingPasswordLabel" + value="&signIn.password.label;" + accesskey="&signIn.password.accesskey;" + control="existingPassword"/> + <textbox id="existingPassword" + type="password" + onkeyup="gSyncSetup.checkFields(event)" + onchange="gSyncSetup.checkFields(event)"/> + </row> + <row id="existingPasswordFeedbackRow" align="center" hidden="true"> + <spacer/> + <hbox> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </row> + <row align="center"> + <spacer/> + <label class="text-link" + value="&resetPassword.label;" + onclick="gSyncUtils.resetPassword(); return false;"/> + </row> + <row align="center"> + <label control="existingServer" + value="&server.label;"/> + <menulist id="existingServer" + oncommand="gSyncSetup.onExistingServerCommand()" + oninput="gSyncSetup.onExistingServerInput()"> + <menupopup> + <menuitem label="&serverType.default.label;" + value="main"/> + <menuitem label="&serverType.custom2.label;" + value="custom"/> + </menupopup> + </menulist> + </row> + <row id="existingServerFeedbackRow" align="center" hidden="true"> + <spacer/> + <hbox> + <image class="statusIcon"/> + <vbox> + <label class="status" value=" "/> + </vbox> + </hbox> + </row> + </rows> + </grid> + + <groupbox> + <label id="existingPassphraseLabel" + value="&signIn.recoveryKey.label;" + accesskey="&signIn.recoveryKey.accesskey;" + control="existingPassphrase"/> + <textbox id="existingPassphrase" + oninput="gSyncSetup.checkFields()"/> + <hbox id="login-throbber" hidden="true"> + <image/> + <label value="&verifying.label;"/> + </hbox> + <vbox align="left" id="existingPassphraseFeedbackRow" hidden="true"> + <hbox> + <image class="statusIcon"/> + <label class="status" value=" "/> + </hbox> + </vbox> + </groupbox> + + <vbox id="passphraseHelpBox"> + <description> + &existingRecoveryKey.description; + <label class="text-link" + href="http://www.palemoon.org/sync/help/recoverykey.shtml"> + &addDevice.showMeHow.label; + </label> + <spacer id="passphraseHelpSpacer"/> + <label class="text-link" + onclick="gSyncSetup.resetPassphrase(); return false;"> + &resetSyncKey.label; + </label> + </description> + </vbox> + </wizardpage> + + <wizardpage id="syncOptionsPage" + label="&setup.optionsPage.title;" + onpageshow="gSyncSetup.onPageShow()"> + <groupbox id="syncOptions"> + <grid> + <columns> + <column/> + <column flex="1" style="-moz-margin-end: 2px"/> + </columns> + <rows> + <row align="center"> + <label value="&syncDeviceName.label;" + accesskey="&syncDeviceName.accesskey;" + control="syncComputerName"/> + <textbox id="syncComputerName" flex="1" + onchange="gSyncUtils.changeName(this)"/> + </row> + <row> + <label value="&syncMy.label;" /> + <vbox> + <checkbox label="&engine.addons.label;" + accesskey="&engine.addons.accesskey;" + id="engine.addons" + checked="false" + hidden="true"/> + <checkbox label="&engine.bookmarks.label;" + accesskey="&engine.bookmarks.accesskey;" + id="engine.bookmarks" + checked="true"/> + <checkbox label="&engine.passwords.label;" + accesskey="&engine.passwords.accesskey;" + id="engine.passwords" + checked="true"/> + <checkbox label="&engine.prefs.label;" + accesskey="&engine.prefs.accesskey;" + id="engine.prefs" + checked="true"/> + <checkbox label="&engine.history.label;" + accesskey="&engine.history.accesskey;" + id="engine.history" + checked="true"/> + <checkbox label="&engine.tabs.label;" + accesskey="&engine.tabs.accesskey;" + id="engine.tabs" + checked="true"/> + </vbox> + </row> + </rows> + </grid> + </groupbox> + + <groupbox id="mergeOptions"> + <radiogroup id="mergeChoiceRadio" pack="start"> + <grid> + <columns> + <column/> + <column flex="1"/> + </columns> + <rows flex="1"> + <row align="center"> + <radio id="resetClient" + class="mergeChoiceButton" + aria-labelledby="resetClientLabel"/> + <label id="resetClientLabel" control="resetClient"> + <html:strong>&choice2.merge.recommended.label;</html:strong> + &choice2a.merge.main.label; + </label> + </row> + <row align="center"> + <radio id="wipeClient" + class="mergeChoiceButton" + aria-labelledby="wipeClientLabel"/> + <label id="wipeClientLabel" + control="wipeClient"> + &choice2a.client.main.label; + </label> + </row> + <row align="center"> + <radio id="wipeRemote" + class="mergeChoiceButton" + aria-labelledby="wipeRemoteLabel"/> + <label id="wipeRemoteLabel" + control="wipeRemote"> + &choice2a.server.main.label; + </label> + </row> + </rows> + </grid> + </radiogroup> + </groupbox> + </wizardpage> + + <wizardpage id="syncOptionsConfirm" + label="&setup.optionsConfirmPage.title;" + onpageshow="gSyncSetup.onPageShow()"> + <deck id="chosenActionDeck"> + <vbox id="chosenActionMerge" class="confirm"> + <description class="normal"> + &confirm.merge2.label; + </description> + </vbox> + <vbox id="chosenActionWipeClient" class="confirm"> + <description class="normal"> + &confirm.client3.label; + </description> + <separator class="thin"/> + <vbox id="dataList"> + <label class="data indent" id="bookmarkCount"/> + <label class="data indent" id="historyCount"/> + <label class="data indent" id="passwordCount"/> + <label class="data indent" id="addonCount"/> + <label class="data indent" id="prefsWipe" + value="&engine.prefs.label;"/> + </vbox> + <separator class="thin"/> + <description class="normal"> + &confirm.client2.moreinfo.label; + </description> + </vbox> + <vbox id="chosenActionWipeServer" class="confirm"> + <description class="normal"> + &confirm.server2.label; + </description> + <separator class="thin"/> + <vbox id="clientList"> + </vbox> + </vbox> + </deck> + </wizardpage> + <!-- In terms of the wizard flow shown to the user, the 'syncOptionsConfirm' + page above is not the last wizard page. To prevent the wizard binding from + assuming that it is, we're inserting this dummy page here. This also means + that the wizard needs to always be closed manually via wizardFinish(). --> + <wizardpage> + </wizardpage> +</wizard> + diff --git a/application/basilisk/components/sync/utils.js b/application/basilisk/components/sync/utils.js new file mode 100644 index 000000000..d41ecf18a --- /dev/null +++ b/application/basilisk/components/sync/utils.js @@ -0,0 +1,218 @@ +/* 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/. */ + +// Equivalent to 0o600 permissions; used for saved Sync Recovery Key. +// This constant can be replaced when the equivalent values are available to +// chrome JS; see Bug 433295 and Bug 757351. +const PERMISSIONS_RWUSR = 0x180; + +// Weave should always exist before before this file gets included. +var gSyncUtils = { + get bundle() { + delete this.bundle; + return this.bundle = Services.strings.createBundle("chrome://browser/locale/syncSetup.properties"); + }, + + // opens in a new window if we're in a modal prefwindow world, in a new tab otherwise + _openLink: function (url) { + let thisDocEl = document.documentElement, + openerDocEl = window.opener && window.opener.document.documentElement; + if (thisDocEl.id == "accountSetup" && window.opener && + openerDocEl.id == "BrowserPreferences" && !openerDocEl.instantApply) + openUILinkIn(url, "window"); + else if (thisDocEl.id == "BrowserPreferences" && !thisDocEl.instantApply) + openUILinkIn(url, "window"); + else if (document.documentElement.id == "change-dialog") + Services.wm.getMostRecentWindow("navigator:browser") + .openUILinkIn(url, "tab"); + else + openUILinkIn(url, "tab"); + }, + + changeName: function changeName(input) { + // Make sure to update to a modified name, e.g., empty-string -> default + Weave.Service.clientsEngine.localName = input.value; + input.value = Weave.Service.clientsEngine.localName; + }, + + openChange: function openChange(type, duringSetup) { + // Just re-show the dialog if it's already open + let openedDialog = Services.wm.getMostRecentWindow("Sync:" + type); + if (openedDialog != null) { + openedDialog.focus(); + return; + } + + // Open up the change dialog + let changeXUL = "chrome://browser/content/sync/genericChange.xul"; + let changeOpt = "centerscreen,chrome,resizable=no"; + Services.ww.activeWindow.openDialog(changeXUL, "", changeOpt, + type, duringSetup); + }, + + changePassword: function () { + if (Weave.Utils.ensureMPUnlocked()) + this.openChange("ChangePassword"); + }, + + resetPassphrase: function (duringSetup) { + if (Weave.Utils.ensureMPUnlocked()) + this.openChange("ResetPassphrase", duringSetup); + }, + + updatePassphrase: function () { + if (Weave.Utils.ensureMPUnlocked()) + this.openChange("UpdatePassphrase"); + }, + + resetPassword: function () { + this._openLink(Weave.Service.pwResetURL); + }, + + openToS: function () { + this._openLink(Weave.Svc.Prefs.get("termsURL")); + }, + + openPrivacyPolicy: function () { + this._openLink(Weave.Svc.Prefs.get("privacyURL")); + }, + + openFirstSyncProgressPage: function () { + this._openLink("about:sync-progress"); + }, + + /** + * Prepare an invisible iframe with the passphrase backup document. + * Used by both the print and saving methods. + * + * @param elid : ID of the form element containing the passphrase. + * @param callback : Function called once the iframe has loaded. + */ + _preparePPiframe: function(elid, callback) { + let pp = document.getElementById(elid).value; + + // Create an invisible iframe whose contents we can print. + let iframe = document.createElement("iframe"); + iframe.setAttribute("src", "chrome://browser/content/sync/key.xhtml"); + iframe.collapsed = true; + document.documentElement.appendChild(iframe); + iframe.contentWindow.addEventListener("load", function() { + iframe.contentWindow.removeEventListener("load", arguments.callee, false); + + // Insert the Sync Key into the page. + let el = iframe.contentDocument.getElementById("synckey"); + el.firstChild.nodeValue = pp; + + // Insert the TOS and Privacy Policy URLs into the page. + let termsURL = Weave.Svc.Prefs.get("termsURL"); + el = iframe.contentDocument.getElementById("tosLink"); + el.setAttribute("href", termsURL); + el.firstChild.nodeValue = termsURL; + + let privacyURL = Weave.Svc.Prefs.get("privacyURL"); + el = iframe.contentDocument.getElementById("ppLink"); + el.setAttribute("href", privacyURL); + el.firstChild.nodeValue = privacyURL; + + callback(iframe); + }, false); + }, + + /** + * Print passphrase backup document. + * + * @param elid : ID of the form element containing the passphrase. + */ + passphrasePrint: function(elid) { + this._preparePPiframe(elid, function(iframe) { + let webBrowserPrint = iframe.contentWindow + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebBrowserPrint); + let printSettings = PrintUtils.getPrintSettings(); + + // Display no header/footer decoration except for the date. + printSettings.headerStrLeft + = printSettings.headerStrCenter + = printSettings.headerStrRight + = printSettings.footerStrLeft + = printSettings.footerStrCenter = ""; + printSettings.footerStrRight = "&D"; + + try { + webBrowserPrint.print(printSettings, null); + } catch (ex) { + // print()'s return codes are expressed as exceptions. Ignore. + } + }); + }, + + /** + * Save passphrase backup document to disk as HTML file. + * + * @param elid : ID of the form element containing the passphrase. + */ + passphraseSave: function(elid) { + let dialogTitle = this.bundle.GetStringFromName("save.recoverykey.title"); + let defaultSaveName = this.bundle.GetStringFromName("save.recoverykey.defaultfilename"); + this._preparePPiframe(elid, function(iframe) { + let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); + let fpCallback = function fpCallback_done(aResult) { + if (aResult == Ci.nsIFilePicker.returnOK || + aResult == Ci.nsIFilePicker.returnReplace) { + let stream = Cc["@mozilla.org/network/file-output-stream;1"]. + createInstance(Ci.nsIFileOutputStream); + stream.init(fp.file, -1, PERMISSIONS_RWUSR, 0); + + let serializer = new XMLSerializer(); + let output = serializer.serializeToString(iframe.contentDocument); + output = output.replace(/<!DOCTYPE (.|\n)*?]>/, + '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' + + '"DTD/xhtml1-strict.dtd">'); + output = Weave.Utils.encodeUTF8(output); + stream.write(output, output.length); + } + }; + + fp.init(window, dialogTitle, Ci.nsIFilePicker.modeSave); + fp.appendFilters(Ci.nsIFilePicker.filterHTML); + fp.defaultString = defaultSaveName; + fp.open(fpCallback); + return false; + }); + }, + + /** + * validatePassword + * + * @param el1 : the first textbox element in the form + * @param el2 : the second textbox element, if omitted it's an update form + * + * returns [valid, errorString] + */ + validatePassword: function (el1, el2) { + let valid = false; + let val1 = el1.value; + let val2 = el2 ? el2.value : ""; + let error = ""; + + if (!el2) + valid = val1.length >= Weave.MIN_PASS_LENGTH; + else if (val1 && val1 == Weave.Service.identity.username) + error = "change.password.pwSameAsUsername"; + else if (val1 && val1 == Weave.Service.identity.account) + error = "change.password.pwSameAsEmail"; + else if (val1 && val1 == Weave.Service.identity.basicPassword) + error = "change.password.pwSameAsPassword"; + else if (val1 && val2) { + if (val1 == val2 && val1.length >= Weave.MIN_PASS_LENGTH) + valid = true; + else if (val1.length < Weave.MIN_PASS_LENGTH) + error = "change.password.tooShort"; + else if (val1 != val2) + error = "change.password.mismatch"; + } + let errorString = error ? Weave.Utils.getErrorString(error) : ""; + return [valid, errorString]; + } +}; diff --git a/application/basilisk/components/syncedtabs/EventEmitter.jsm b/application/basilisk/components/syncedtabs/EventEmitter.jsm deleted file mode 100644 index ec3225f0f..000000000 --- a/application/basilisk/components/syncedtabs/EventEmitter.jsm +++ /dev/null @@ -1,45 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -this.EXPORTED_SYMBOLS = [ - "EventEmitter" -]; - -// Simple event emitter abstraction for storage objects to use. -function EventEmitter () { - this._events = new Map(); -} - -EventEmitter.prototype = { - on(event, listener) { - if (this._events.has(event)) { - this._events.get(event).add(listener); - } else { - this._events.set(event, new Set([listener])); - } - }, - off(event, listener) { - if (!this._events.has(event)) { - return; - } - this._events.get(event).delete(listener); - }, - emit(event, ...args) { - if (!this._events.has(event)) { - return; - } - for (let listener of this._events.get(event).values()) { - try { - listener.apply(this, args); - } catch (e) { - Cu.reportError(e); - } - } - }, -}; - diff --git a/application/basilisk/components/syncedtabs/SyncedTabsDeckComponent.js b/application/basilisk/components/syncedtabs/SyncedTabsDeckComponent.js deleted file mode 100644 index c35277795..000000000 --- a/application/basilisk/components/syncedtabs/SyncedTabsDeckComponent.js +++ /dev/null @@ -1,169 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); - -Cu.import("resource:///modules/syncedtabs/SyncedTabsDeckStore.js"); -Cu.import("resource:///modules/syncedtabs/SyncedTabsDeckView.js"); -Cu.import("resource:///modules/syncedtabs/SyncedTabsListStore.js"); -Cu.import("resource:///modules/syncedtabs/TabListComponent.js"); -Cu.import("resource:///modules/syncedtabs/TabListView.js"); -let { getChromeWindow } = Cu.import("resource:///modules/syncedtabs/util.js", {}); - -XPCOMUtils.defineLazyGetter(this, "FxAccountsCommon", function () { - return Components.utils.import("resource://gre/modules/FxAccountsCommon.js", {}); -}); - -let log = Cu.import("resource://gre/modules/Log.jsm", {}) - .Log.repository.getLogger("Sync.RemoteTabs"); - -this.EXPORTED_SYMBOLS = [ - "SyncedTabsDeckComponent" -]; - -/* SyncedTabsDeckComponent - * This component instantiates views and storage objects as well as defines - * behaviors that will be passed down to the views. This helps keep the views - * isolated and easier to test. - */ - -function SyncedTabsDeckComponent({ - window, SyncedTabs, fxAccounts, deckStore, listStore, listComponent, DeckView, getChromeWindowMock, -}) { - this._window = window; - this._SyncedTabs = SyncedTabs; - this._fxAccounts = fxAccounts; - this._DeckView = DeckView || SyncedTabsDeckView; - // used to stub during tests - this._getChromeWindow = getChromeWindowMock || getChromeWindow; - - this._deckStore = deckStore || new SyncedTabsDeckStore(); - this._syncedTabsListStore = listStore || new SyncedTabsListStore(SyncedTabs); - this.tabListComponent = listComponent || new TabListComponent({ - window: this._window, - store: this._syncedTabsListStore, - View: TabListView, - SyncedTabs: SyncedTabs, - clipboardHelper: Cc["@mozilla.org/widget/clipboardhelper;1"] - .getService(Ci.nsIClipboardHelper), - getChromeWindow: this._getChromeWindow, - }); -} - -SyncedTabsDeckComponent.prototype = { - PANELS: { - TABS_CONTAINER: "tabs-container", - TABS_FETCHING: "tabs-fetching", - NOT_AUTHED_INFO: "notAuthedInfo", - SINGLE_DEVICE_INFO: "singleDeviceInfo", - TABS_DISABLED: "tabs-disabled", - }, - - get container() { - return this._deckView ? this._deckView.container : null; - }, - - init() { - Services.obs.addObserver(this, this._SyncedTabs.TOPIC_TABS_CHANGED, false); - Services.obs.addObserver(this, FxAccountsCommon.ONLOGIN_NOTIFICATION, false); - - // Go ahead and trigger sync - this._SyncedTabs.syncTabs() - .catch(Cu.reportError); - - this._deckView = new this._DeckView(this._window, this.tabListComponent, { - onAndroidClick: event => this.openAndroidLink(event), - oniOSClick: event => this.openiOSLink(event), - onSyncPrefClick: event => this.openSyncPrefs(event) - }); - - this._deckStore.on("change", state => this._deckView.render(state)); - // Trigger the initial rendering of the deck view - // Object.values only in nightly - this._deckStore.setPanels(Object.keys(this.PANELS).map(k => this.PANELS[k])); - // Set the initial panel to display - this.updatePanel(); - }, - - uninit() { - Services.obs.removeObserver(this, this._SyncedTabs.TOPIC_TABS_CHANGED); - Services.obs.removeObserver(this, FxAccountsCommon.ONLOGIN_NOTIFICATION); - this._deckView.destroy(); - }, - - observe(subject, topic, data) { - switch (topic) { - case this._SyncedTabs.TOPIC_TABS_CHANGED: - this._syncedTabsListStore.getData(); - this.updatePanel(); - break; - case FxAccountsCommon.ONLOGIN_NOTIFICATION: - this.updatePanel(); - break; - default: - break; - } - }, - - // There's no good way to mock fxAccounts in browser tests where it's already - // been instantiated, so we have this method for stubbing. - _accountStatus() { - return this._fxAccounts.accountStatus(); - }, - - getPanelStatus() { - return this._accountStatus().then(exists => { - if (!exists) { - return this.PANELS.NOT_AUTHED_INFO; - } - if (!this._SyncedTabs.isConfiguredToSyncTabs) { - return this.PANELS.TABS_DISABLED; - } - if (!this._SyncedTabs.hasSyncedThisSession) { - return this.PANELS.TABS_FETCHING; - } - return this._SyncedTabs.getTabClients().then(clients => { - if (clients.length) { - return this.PANELS.TABS_CONTAINER; - } - return this.PANELS.SINGLE_DEVICE_INFO; - }); - }) - .catch(err => { - Cu.reportError(err); - return this.PANELS.NOT_AUTHED_INFO; - }); - }, - - updatePanel() { - // return promise for tests - return this.getPanelStatus() - .then(panelId => this._deckStore.selectPanel(panelId)) - .catch(Cu.reportError); - }, - - openAndroidLink(event) { - let href = Services.prefs.getCharPref("identity.mobilepromo.android") + "synced-tabs-sidebar"; - this._openUrl(href, event); - }, - - openiOSLink(event) { - let href = Services.prefs.getCharPref("identity.mobilepromo.ios") + "synced-tabs-sidebar"; - this._openUrl(href, event); - }, - - _openUrl(url, event) { - this._window.openUILink(url, event); - }, - - openSyncPrefs() { - this._getChromeWindow(this._window).gSyncUI.openSetup(null, "tabs-sidebar"); - } -}; - diff --git a/application/basilisk/components/syncedtabs/SyncedTabsDeckStore.js b/application/basilisk/components/syncedtabs/SyncedTabsDeckStore.js deleted file mode 100644 index ede6914c8..000000000 --- a/application/basilisk/components/syncedtabs/SyncedTabsDeckStore.js +++ /dev/null @@ -1,60 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -let { EventEmitter } = Cu.import("resource:///modules/syncedtabs/EventEmitter.jsm", {}); - -this.EXPORTED_SYMBOLS = [ - "SyncedTabsDeckStore" -]; - -/** - * SyncedTabsDeckStore - * - * This store keeps track of the deck view state, including the panels and which - * one is selected. The view listens for change events on the store, which are - * triggered whenever the state changes. If it's a small change, the state - * will have `isUpdatable` set to true so the view can skip rerendering the whole - * DOM. - */ -function SyncedTabsDeckStore() { - EventEmitter.call(this); - this._panels = []; -} - -Object.assign(SyncedTabsDeckStore.prototype, EventEmitter.prototype, { - _change(isUpdatable = false) { - let panels = this._panels.map(panel => { - return {id: panel, selected: panel === this._selectedPanel}; - }); - this.emit("change", {panels, isUpdatable: isUpdatable}); - }, - - /** - * Sets the selected panelId and triggers a change event. - * @param {String} panelId - ID of the panel to select. - */ - selectPanel(panelId) { - if (this._panels.indexOf(panelId) === -1 || this._selectedPanel === panelId) { - return; - } - this._selectedPanel = panelId; - this._change(true); - }, - - /** - * Update the set of panels in the deck and trigger a change event. - * @param {Array} panels - an array of IDs for each panel in the deck. - */ - setPanels(panels) { - if (panels === this._panels) { - return; - } - this._panels = panels || []; - this._change(); - } -}); diff --git a/application/basilisk/components/syncedtabs/SyncedTabsDeckView.js b/application/basilisk/components/syncedtabs/SyncedTabsDeckView.js deleted file mode 100644 index e9efff323..000000000 --- a/application/basilisk/components/syncedtabs/SyncedTabsDeckView.js +++ /dev/null @@ -1,116 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -let { getChromeWindow } = Cu.import("resource:///modules/syncedtabs/util.js", {}); - -let log = Cu.import("resource://gre/modules/Log.jsm", {}) - .Log.repository.getLogger("Sync.RemoteTabs"); - -this.EXPORTED_SYMBOLS = [ - "SyncedTabsDeckView" -]; - -/** - * SyncedTabsDeckView - * - * Instances of SyncedTabsDeckView render DOM nodes from a given state. - * No state is kept internaly and the DOM will completely - * rerender unless the state flags `isUpdatable`, which helps - * make small changes without the overhead of a full rerender. - */ -const SyncedTabsDeckView = function (window, tabListComponent, props) { - this.props = props; - - this._window = window; - this._doc = window.document; - - this._tabListComponent = tabListComponent; - this._deckTemplate = this._doc.getElementById("deck-template"); - this.container = this._doc.createElement("div"); -}; - -SyncedTabsDeckView.prototype = { - render(state) { - if (state.isUpdatable) { - this.update(state); - } else { - this.create(state); - } - }, - - create(state) { - let deck = this._doc.importNode(this._deckTemplate.content, true).firstElementChild; - this._clearChilden(); - - let tabListWrapper = this._doc.createElement("div"); - tabListWrapper.className = "tabs-container sync-state"; - this._tabListComponent.init(); - tabListWrapper.appendChild(this._tabListComponent.container); - deck.appendChild(tabListWrapper); - this.container.appendChild(deck); - - this._generateDevicePromo(); - - this._attachListeners(); - this.update(state); - }, - - _getBrowserBundle() { - return getChromeWindow(this._window).document.getElementById("bundle_browser"); - }, - - _generateDevicePromo() { - let bundle = this._getBrowserBundle(); - let formatArgs = ["android", "ios"].map(os => { - let link = this._doc.createElement("a"); - link.textContent = bundle.getString(`appMenuRemoteTabs.mobilePromo.${os}`); - link.className = `${os}-link text-link`; - link.setAttribute("href", "#"); - return link.outerHTML; - }); - // Put it all together... - let contents = bundle.getFormattedString("appMenuRemoteTabs.mobilePromo.text2", formatArgs); - this.container.querySelector(".device-promo").innerHTML = contents; - }, - - destroy() { - this._tabListComponent.uninit(); - this.container.remove(); - }, - - update(state) { - // Note that we may also want to update elements that are outside of the - // deck, so use the document to find the class names rather than our - // container. - for (let panel of state.panels) { - if (panel.selected) { - Array.prototype.map.call(this._doc.getElementsByClassName(panel.id), - item => item.classList.add("selected")); - } else { - Array.prototype.map.call(this._doc.getElementsByClassName(panel.id), - item => item.classList.remove("selected")); - } - } - }, - - _clearChilden() { - while (this.container.firstChild) { - this.container.removeChild(this.container.firstChild); - } - }, - - _attachListeners() { - this.container.querySelector(".android-link").addEventListener("click", this.props.onAndroidClick); - this.container.querySelector(".ios-link").addEventListener("click", this.props.oniOSClick); - let syncPrefLinks = this.container.querySelectorAll(".sync-prefs"); - for (let link of syncPrefLinks) { - link.addEventListener("click", this.props.onSyncPrefClick); - } - }, -}; - diff --git a/application/basilisk/components/syncedtabs/SyncedTabsListStore.js b/application/basilisk/components/syncedtabs/SyncedTabsListStore.js deleted file mode 100644 index 8f03d9a89..000000000 --- a/application/basilisk/components/syncedtabs/SyncedTabsListStore.js +++ /dev/null @@ -1,235 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -let { EventEmitter } = Cu.import("resource:///modules/syncedtabs/EventEmitter.jsm", {}); - -this.EXPORTED_SYMBOLS = [ - "SyncedTabsListStore" -]; - -/** - * SyncedTabsListStore - * - * Instances of this store encapsulate all of the state associated with a synced tabs list view. - * The state includes the clients, their tabs, the row that is currently selected, - * and the filtered query. - */ -function SyncedTabsListStore(SyncedTabs) { - EventEmitter.call(this); - this._SyncedTabs = SyncedTabs; - this.data = []; - this._closedClients = {}; - this._selectedRow = [-1, -1]; - this.filter = ""; - this.inputFocused = false; -} - -Object.assign(SyncedTabsListStore.prototype, EventEmitter.prototype, { - // This internal method triggers the "change" event that views - // listen for. It denormalizes the state so that it's easier for - // the view to deal with. updateType hints to the view what - // actually needs to be rerendered or just updated, and can be - // empty (to (re)render everything), "searchbox" (to rerender just the tab list), - // or "all" (to skip rendering and just update all attributes of existing nodes). - _change(updateType) { - let selectedParent = this._selectedRow[0]; - let selectedChild = this._selectedRow[1]; - let rowSelected = false; - // clone the data so that consumers can't mutate internal storage - let data = Cu.cloneInto(this.data, {}); - let tabCount = 0; - - data.forEach((client, index) => { - client.closed = !!this._closedClients[client.id]; - - if (rowSelected || selectedParent < 0) { - return; - } - if (this.filter) { - if (selectedParent < tabCount + client.tabs.length) { - client.tabs[selectedParent - tabCount].selected = true; - client.tabs[selectedParent - tabCount].focused = !this.inputFocused; - rowSelected = true; - } else { - tabCount += client.tabs.length; - } - return; - } - if (selectedParent === index && selectedChild === -1) { - client.selected = true; - client.focused = !this.inputFocused; - rowSelected = true; - } else if (selectedParent === index) { - client.tabs[selectedChild].selected = true; - client.tabs[selectedChild].focused = !this.inputFocused; - rowSelected = true; - } - }); - - // If this were React the view would be smart enough - // to not re-render the whole list unless necessary. But it's - // not, so updateType is a hint to the view of what actually - // needs to be rerendered. - this.emit("change", { - clients: data, - canUpdateAll: updateType === "all", - canUpdateInput: updateType === "searchbox", - filter: this.filter, - inputFocused: this.inputFocused - }); - }, - - /** - * Moves the row selection from a child to its parent, - * which occurs when the parent of a selected row closes. - */ - _selectParentRow() { - this._selectedRow[1] = -1; - }, - - _toggleBranch(id, closed) { - this._closedClients[id] = closed; - if (this._closedClients[id]) { - this._selectParentRow(); - } - this._change("all"); - }, - - _isOpen(client) { - return !this._closedClients[client.id]; - }, - - moveSelectionDown() { - let branchRow = this._selectedRow[0]; - let childRow = this._selectedRow[1]; - let branch = this.data[branchRow]; - - if (this.filter) { - this.selectRow(branchRow + 1); - return; - } - - if (branchRow < 0) { - this.selectRow(0, -1); - } else if ((!branch.tabs.length || childRow >= branch.tabs.length - 1 || !this._isOpen(branch)) && branchRow < this.data.length) { - this.selectRow(branchRow + 1, -1); - } else if (childRow < branch.tabs.length) { - this.selectRow(branchRow, childRow + 1); - } - }, - - moveSelectionUp() { - let branchRow = this._selectedRow[0]; - let childRow = this._selectedRow[1]; - - if (this.filter) { - this.selectRow(branchRow - 1); - return; - } - - if (branchRow < 0) { - this.selectRow(0, -1); - } else if (childRow < 0 && branchRow > 0) { - let prevBranch = this.data[branchRow - 1]; - let newChildRow = this._isOpen(prevBranch) ? prevBranch.tabs.length - 1 : -1; - this.selectRow(branchRow - 1, newChildRow); - } else if (childRow >= 0) { - this.selectRow(branchRow, childRow - 1); - } - }, - - // Selects a row and makes sure the selection is within bounds - selectRow(parent, child) { - let maxParentRow = this.filter ? this._tabCount() : this.data.length; - let parentRow = parent; - if (parent <= -1) { - parentRow = 0; - } else if (parent >= maxParentRow) { - return; - } - - let childRow = child; - if (parentRow === -1 || this.filter || typeof child === "undefined" || child < -1) { - childRow = -1; - } else if (child >= this.data[parentRow].tabs.length) { - childRow = this.data[parentRow].tabs.length - 1; - } - - if (this._selectedRow[0] === parentRow && this._selectedRow[1] === childRow) { - return; - } - - this._selectedRow = [parentRow, childRow]; - this.inputFocused = false; - this._change("all"); - }, - - _tabCount() { - return this.data.reduce((prev, curr) => curr.tabs.length + prev, 0); - }, - - toggleBranch(id) { - this._toggleBranch(id, !this._closedClients[id]); - }, - - closeBranch(id) { - this._toggleBranch(id, true); - }, - - openBranch(id) { - this._toggleBranch(id, false); - }, - - focusInput() { - this.inputFocused = true; - // A change type of "all" updates rather than rebuilds, which is what we - // want here - only the selection/focus has changed. - this._change("all"); - }, - - blurInput() { - this.inputFocused = false; - // A change type of "all" updates rather than rebuilds, which is what we - // want here - only the selection/focus has changed. - this._change("all"); - }, - - clearFilter() { - this.filter = ""; - this._selectedRow = [-1, -1]; - return this.getData(); - }, - - // Fetches data from the SyncedTabs module and triggers - // and update - getData(filter) { - let updateType; - let hasFilter = typeof filter !== "undefined"; - if (hasFilter) { - this.filter = filter; - this._selectedRow = [-1, -1]; - - // When a filter is specified we tell the view that only the list - // needs to be rerendered so that it doesn't disrupt the input - // field's focus. - updateType = "searchbox"; - } - - // return promise for tests - return this._SyncedTabs.getTabClients(this.filter) - .then(result => { - if (!hasFilter) { - // Only sort clients and tabs if we're rendering the whole list. - this._SyncedTabs.sortTabClientsByLastUsed(result); - } - this.data = result; - this._change(updateType); - }) - .catch(Cu.reportError); - } -}); diff --git a/application/basilisk/components/syncedtabs/TabListComponent.js b/application/basilisk/components/syncedtabs/TabListComponent.js deleted file mode 100644 index aa60e4769..000000000 --- a/application/basilisk/components/syncedtabs/TabListComponent.js +++ /dev/null @@ -1,138 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -let log = Cu.import("resource://gre/modules/Log.jsm", {}) - .Log.repository.getLogger("Sync.RemoteTabs"); - -XPCOMUtils.defineLazyModuleGetter(this, "PlacesUIUtils", - "resource:///modules/PlacesUIUtils.jsm"); - -this.EXPORTED_SYMBOLS = [ - "TabListComponent" -]; - -/** - * TabListComponent - * - * The purpose of this component is to compose the view, state, and actions. - * It defines high level actions that act on the state and passes them to the - * view for it to trigger during user interaction. It also subscribes the view - * to state changes so it can rerender. - */ - -function TabListComponent({window, store, View, SyncedTabs, clipboardHelper, - getChromeWindow}) { - this._window = window; - this._store = store; - this._View = View; - this._clipboardHelper = clipboardHelper; - this._getChromeWindow = getChromeWindow; - // used to trigger Sync from context menu - this._SyncedTabs = SyncedTabs; -} - -TabListComponent.prototype = { - get container() { - return this._view.container; - }, - - init() { - log.debug("Initializing TabListComponent"); - - this._view = new this._View(this._window, { - onSelectRow: (...args) => this.onSelectRow(...args), - onOpenTab: (...args) => this.onOpenTab(...args), - onOpenTabs: (...args) => this.onOpenTabs(...args), - onMoveSelectionDown: (...args) => this.onMoveSelectionDown(...args), - onMoveSelectionUp: (...args) => this.onMoveSelectionUp(...args), - onToggleBranch: (...args) => this.onToggleBranch(...args), - onBookmarkTab: (...args) => this.onBookmarkTab(...args), - onCopyTabLocation: (...args) => this.onCopyTabLocation(...args), - onSyncRefresh: (...args) => this.onSyncRefresh(...args), - onFilter: (...args) => this.onFilter(...args), - onClearFilter: (...args) => this.onClearFilter(...args), - onFilterFocus: (...args) => this.onFilterFocus(...args), - onFilterBlur: (...args) => this.onFilterBlur(...args) - }); - - this._store.on("change", state => this._view.render(state)); - this._view.render({clients: []}); - // get what's already available... - this._store.getData(); - this._store.focusInput(); - }, - - uninit() { - this._view.destroy(); - }, - - onFilter(query) { - this._store.getData(query); - }, - - onClearFilter() { - this._store.clearFilter(); - }, - - onFilterFocus() { - this._store.focusInput(); - }, - - onFilterBlur() { - this._store.blurInput(); - }, - - onSelectRow(position) { - this._store.selectRow(position[0], position[1]); - }, - - onMoveSelectionDown() { - this._store.moveSelectionDown(); - }, - - onMoveSelectionUp() { - this._store.moveSelectionUp(); - }, - - onToggleBranch(id) { - this._store.toggleBranch(id); - }, - - onBookmarkTab(uri, title) { - this._window.top.PlacesCommandHook - .bookmarkLink(this._window.top.PlacesUtils.bookmarksMenuFolderId, uri, title) - .catch(Cu.reportError); - }, - - onOpenTab(url, where, params) { - this._window.openUILinkIn(url, where, params); - }, - - onOpenTabs(urls, where) { - if (!PlacesUIUtils.confirmOpenInTabs(urls.length, this._window)) { - return; - } - if (where == "window") { - this._window.openDialog(this._window.getBrowserURL(), "_blank", - "chrome,dialog=no,all", urls.join("|")); - } else { - let loadInBackground = where == "tabshifted" ? true : false; - this._getChromeWindow(this._window).gBrowser.loadTabs(urls, loadInBackground, false); - } - }, - - onCopyTabLocation(url) { - this._clipboardHelper.copyString(url); - }, - - onSyncRefresh() { - this._SyncedTabs.syncTabs(true); - } -}; diff --git a/application/basilisk/components/syncedtabs/TabListView.js b/application/basilisk/components/syncedtabs/TabListView.js deleted file mode 100644 index dab15101b..000000000 --- a/application/basilisk/components/syncedtabs/TabListView.js +++ /dev/null @@ -1,568 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -Cu.import("resource://gre/modules/Services.jsm"); - -let { getChromeWindow } = Cu.import("resource:///modules/syncedtabs/util.js", {}); - -let log = Cu.import("resource://gre/modules/Log.jsm", {}) - .Log.repository.getLogger("Sync.RemoteTabs"); - -this.EXPORTED_SYMBOLS = [ - "TabListView" -]; - -function getContextMenu(window) { - return getChromeWindow(window).document.getElementById("SyncedTabsSidebarContext"); -} - -function getTabsFilterContextMenu(window) { - return getChromeWindow(window).document.getElementById("SyncedTabsSidebarTabsFilterContext"); -} - -/* - * TabListView - * - * Given a state, this object will render the corresponding DOM. - * It maintains no state of it's own. It listens for DOM events - * and triggers actions that may cause the state to change and - * ultimately the view to rerender. - */ -function TabListView(window, props) { - this.props = props; - - this._window = window; - this._doc = this._window.document; - - this._tabsContainerTemplate = this._doc.getElementById("tabs-container-template"); - this._clientTemplate = this._doc.getElementById("client-template"); - this._emptyClientTemplate = this._doc.getElementById("empty-client-template"); - this._tabTemplate = this._doc.getElementById("tab-template"); - this.tabsFilter = this._doc.querySelector(".tabsFilter"); - this.clearFilter = this._doc.querySelector(".textbox-search-clear"); - this.searchBox = this._doc.querySelector(".search-box"); - this.searchIcon = this._doc.querySelector(".textbox-search-icon"); - - this.container = this._doc.createElement("div"); - - this._attachFixedListeners(); - - this._setupContextMenu(); -} - -TabListView.prototype = { - render(state) { - // Don't rerender anything; just update attributes, e.g. selection - if (state.canUpdateAll) { - this._update(state); - return; - } - // Rerender the tab list - if (state.canUpdateInput) { - this._updateSearchBox(state); - this._createList(state); - return; - } - // Create the world anew - this._create(state); - }, - - // Create the initial DOM from templates - _create(state) { - let wrapper = this._doc.importNode(this._tabsContainerTemplate.content, true).firstElementChild; - this._clearChilden(); - this.container.appendChild(wrapper); - - this.list = this.container.querySelector(".list"); - - this._createList(state); - this._updateSearchBox(state); - - this._attachListListeners(); - }, - - _createList(state) { - this._clearChilden(this.list); - for (let client of state.clients) { - if (state.filter) { - this._renderFilteredClient(client); - } else { - this._renderClient(client); - } - } - if (this.list.firstChild) { - const firstTab = this.list.firstChild.querySelector(".item.tab:first-child .item-title"); - if (firstTab) { - firstTab.setAttribute("tabindex", 2); - } - } - }, - - destroy() { - this._teardownContextMenu(); - this.container.remove(); - }, - - _update(state) { - this._updateSearchBox(state); - for (let client of state.clients) { - let clientNode = this._doc.getElementById("item-" + client.id); - if (clientNode) { - this._updateClient(client, clientNode); - } - - client.tabs.forEach((tab, index) => { - let tabNode = this._doc.getElementById('tab-' + client.id + '-' + index); - this._updateTab(tab, tabNode, index); - }); - } - }, - - // Client rows are hidden when the list is filtered - _renderFilteredClient(client, filter) { - client.tabs.forEach((tab, index) => { - let node = this._renderTab(client, tab, index); - this.list.appendChild(node); - }); - }, - - _renderClient(client) { - let itemNode = client.tabs.length ? - this._createClient(client) : - this._createEmptyClient(client); - - this._updateClient(client, itemNode); - - let tabsList = itemNode.querySelector(".item-tabs-list"); - client.tabs.forEach((tab, index) => { - let node = this._renderTab(client, tab, index); - tabsList.appendChild(node); - }); - - this.list.appendChild(itemNode); - return itemNode; - }, - - _renderTab(client, tab, index) { - let itemNode = this._createTab(tab); - this._updateTab(tab, itemNode, index); - return itemNode; - }, - - _createClient(item) { - return this._doc.importNode(this._clientTemplate.content, true).firstElementChild; - }, - - _createEmptyClient(item) { - return this._doc.importNode(this._emptyClientTemplate.content, true).firstElementChild; - }, - - _createTab(item) { - return this._doc.importNode(this._tabTemplate.content, true).firstElementChild; - }, - - _clearChilden(node) { - let parent = node || this.container; - while (parent.firstChild) { - parent.removeChild(parent.firstChild); - } - }, - - // These listeners are attached only once, when we initialize the view - _attachFixedListeners() { - this.tabsFilter.addEventListener("input", this.onFilter.bind(this)); - this.tabsFilter.addEventListener("focus", this.onFilterFocus.bind(this)); - this.tabsFilter.addEventListener("blur", this.onFilterBlur.bind(this)); - this.clearFilter.addEventListener("click", this.onClearFilter.bind(this)); - this.searchIcon.addEventListener("click", this.onFilterFocus.bind(this)); - }, - - // These listeners have to be re-created every time since we re-create the list - _attachListListeners() { - this.list.addEventListener("click", this.onClick.bind(this)); - this.list.addEventListener("mouseup", this.onMouseUp.bind(this)); - this.list.addEventListener("keydown", this.onKeyDown.bind(this)); - }, - - _updateSearchBox(state) { - if (state.filter) { - this.searchBox.classList.add("filtered"); - } else { - this.searchBox.classList.remove("filtered"); - } - this.tabsFilter.value = state.filter; - if (state.inputFocused) { - this.searchBox.setAttribute("focused", true); - this.tabsFilter.focus(); - } else { - this.searchBox.removeAttribute("focused"); - } - }, - - /** - * Update the element representing an item, ensuring it's in sync with the - * underlying data. - * @param {client} item - Item to use as a source. - * @param {Element} itemNode - Element to update. - */ - _updateClient(item, itemNode) { - itemNode.setAttribute("id", "item-" + item.id); - let lastSync = new Date(item.lastModified); - let lastSyncTitle = getChromeWindow(this._window).gSyncUI.formatLastSyncDate(lastSync); - itemNode.setAttribute("title", lastSyncTitle); - if (item.closed) { - itemNode.classList.add("closed"); - } else { - itemNode.classList.remove("closed"); - } - if (item.selected) { - itemNode.classList.add("selected"); - } else { - itemNode.classList.remove("selected"); - } - if (item.isMobile) { - itemNode.classList.add("device-image-mobile"); - } else { - itemNode.classList.add("device-image-desktop"); - } - if (item.focused) { - itemNode.focus(); - } - itemNode.dataset.id = item.id; - itemNode.querySelector(".item-title").textContent = item.name; - }, - - /** - * Update the element representing a tab, ensuring it's in sync with the - * underlying data. - * @param {tab} item - Item to use as a source. - * @param {Element} itemNode - Element to update. - */ - _updateTab(item, itemNode, index) { - itemNode.setAttribute("title", `${item.title}\n${item.url}`); - itemNode.setAttribute("id", "tab-" + item.client + '-' + index); - if (item.selected) { - itemNode.classList.add("selected"); - } else { - itemNode.classList.remove("selected"); - } - if (item.focused) { - itemNode.focus(); - } - itemNode.dataset.url = item.url; - - itemNode.querySelector(".item-title").textContent = item.title; - - if (item.icon) { - let icon = itemNode.querySelector(".item-icon-container"); - icon.style.backgroundImage = "url(" + item.icon + ")"; - } - }, - - onMouseUp(event) { - if (event.which == 2) { // Middle click - this.onClick(event); - } - }, - - onClick(event) { - let itemNode = this._findParentItemNode(event.target); - if (!itemNode) { - return; - } - - if (itemNode.classList.contains("tab")) { - let url = itemNode.dataset.url; - if (url) { - this.onOpenSelected(url, event); - } - } - - // Middle click on a client - if (itemNode.classList.contains("client")) { - let where = getChromeWindow(this._window).whereToOpenLink(event); - if (where != "current") { - const tabs = itemNode.querySelector(".item-tabs-list").childNodes; - const urls = [...tabs].map(tab => tab.dataset.url); - this.props.onOpenTabs(urls, where); - } - } - - if (event.target.classList.contains("item-twisty-container") - && event.which != 2) { - this.props.onToggleBranch(itemNode.dataset.id); - return; - } - - let position = this._getSelectionPosition(itemNode); - this.props.onSelectRow(position); - }, - - /** - * Handle a keydown event on the list box. - * @param {Event} event - Triggering event. - */ - onKeyDown(event) { - if (event.keyCode == this._window.KeyEvent.DOM_VK_DOWN) { - event.preventDefault(); - this.props.onMoveSelectionDown(); - } else if (event.keyCode == this._window.KeyEvent.DOM_VK_UP) { - event.preventDefault(); - this.props.onMoveSelectionUp(); - } else if (event.keyCode == this._window.KeyEvent.DOM_VK_RETURN) { - let selectedNode = this.container.querySelector('.item.selected'); - if (selectedNode.dataset.url) { - this.onOpenSelected(selectedNode.dataset.url, event); - } else if (selectedNode) { - this.props.onToggleBranch(selectedNode.dataset.id); - } - } - }, - - onBookmarkTab() { - let item = this._getSelectedTabNode(); - if (item) { - let title = item.querySelector(".item-title").textContent; - this.props.onBookmarkTab(item.dataset.url, title); - } - }, - - onCopyTabLocation() { - let item = this._getSelectedTabNode(); - if (item) { - this.props.onCopyTabLocation(item.dataset.url); - } - }, - - onOpenSelected(url, event) { - let where = getChromeWindow(this._window).whereToOpenLink(event); - this.props.onOpenTab(url, where, {}); - }, - - onOpenSelectedFromContextMenu(event) { - let item = this._getSelectedTabNode(); - if (item) { - let where = event.target.getAttribute("where"); - let params = { - private: event.target.hasAttribute("private"), - }; - this.props.onOpenTab(item.dataset.url, where, params); - } - }, - - onFilter(event) { - let query = event.target.value; - if (query) { - this.props.onFilter(query); - } else { - this.props.onClearFilter(); - } - }, - - onClearFilter() { - this.props.onClearFilter(); - }, - - onFilterFocus() { - this.props.onFilterFocus(); - }, - onFilterBlur() { - this.props.onFilterBlur(); - }, - - _getSelectedTabNode() { - let item = this.container.querySelector('.item.selected'); - if (this._isTab(item) && item.dataset.url) { - return item; - } - return null; - }, - - // Set up the custom context menu - _setupContextMenu() { - Services.els.addSystemEventListener(this._window, "contextmenu", this, false); - for (let getMenu of [getContextMenu, getTabsFilterContextMenu]) { - let menu = getMenu(this._window); - menu.addEventListener("popupshowing", this, true); - menu.addEventListener("command", this, true); - } - }, - - _teardownContextMenu() { - // Tear down context menu - Services.els.removeSystemEventListener(this._window, "contextmenu", this, false); - for (let getMenu of [getContextMenu, getTabsFilterContextMenu]) { - let menu = getMenu(this._window); - menu.removeEventListener("popupshowing", this, true); - menu.removeEventListener("command", this, true); - } - }, - - handleEvent(event) { - switch (event.type) { - case "contextmenu": - this.handleContextMenu(event); - break; - - case "popupshowing": { - if (event.target.getAttribute("id") == "SyncedTabsSidebarTabsFilterContext") { - this.handleTabsFilterContextMenuShown(event); - } - break; - } - - case "command": { - let menu = event.target.closest("menupopup"); - switch (menu.getAttribute("id")) { - case "SyncedTabsSidebarContext": - this.handleContentContextMenuCommand(event); - break; - - case "SyncedTabsSidebarTabsFilterContext": - this.handleTabsFilterContextMenuCommand(event); - break; - } - break; - } - } - }, - - handleTabsFilterContextMenuShown(event) { - let document = event.target.ownerDocument; - let focusedElement = document.commandDispatcher.focusedElement; - if (focusedElement != this.tabsFilter) { - this.tabsFilter.focus(); - } - for (let item of event.target.children) { - if (!item.hasAttribute("cmd")) { - continue; - } - let command = item.getAttribute("cmd"); - let controller = document.commandDispatcher.getControllerForCommand(command); - if (controller.isCommandEnabled(command)) { - item.removeAttribute("disabled"); - } else { - item.setAttribute("disabled", "true"); - } - } - }, - - handleContentContextMenuCommand(event) { - let id = event.target.getAttribute("id"); - switch (id) { - case "syncedTabsOpenSelected": - case "syncedTabsOpenSelectedInTab": - case "syncedTabsOpenSelectedInWindow": - case "syncedTabsOpenSelectedInPrivateWindow": - this.onOpenSelectedFromContextMenu(event); - break; - case "syncedTabsBookmarkSelected": - this.onBookmarkTab(); - break; - case "syncedTabsCopySelected": - this.onCopyTabLocation(); - break; - case "syncedTabsRefresh": - case "syncedTabsRefreshFilter": - this.props.onSyncRefresh(); - break; - } - }, - - handleTabsFilterContextMenuCommand(event) { - let command = event.target.getAttribute("cmd"); - let dispatcher = getChromeWindow(this._window).document.commandDispatcher; - let controller = dispatcher.focusedElement.controllers.getControllerForCommand(command); - controller.doCommand(command); - }, - - handleContextMenu(event) { - let menu; - - if (event.target == this.tabsFilter) { - menu = getTabsFilterContextMenu(this._window); - } else { - let itemNode = this._findParentItemNode(event.target); - if (itemNode) { - let position = this._getSelectionPosition(itemNode); - this.props.onSelectRow(position); - } - menu = getContextMenu(this._window); - this.adjustContextMenu(menu); - } - - menu.openPopupAtScreen(event.screenX, event.screenY, true, event); - }, - - adjustContextMenu(menu) { - let item = this.container.querySelector('.item.selected'); - let showTabOptions = this._isTab(item); - - let el = menu.firstChild; - - while (el) { - if (showTabOptions || el.getAttribute("id") === "syncedTabsRefresh") { - el.hidden = false; - } else { - el.hidden = true; - } - - el = el.nextSibling; - } - }, - - /** - * Find the parent item element, from a given child element. - * @param {Element} node - Child element. - * @return {Element} Element for the item, or null if not found. - */ - _findParentItemNode(node) { - while (node && node !== this.list && node !== this._doc.documentElement && - !node.classList.contains("item")) { - node = node.parentNode; - } - - if (node !== this.list && node !== this._doc.documentElement) { - return node; - } - - return null; - }, - - _findParentBranchNode(node) { - while (node && !node.classList.contains("list") && node !== this._doc.documentElement && - !node.parentNode.classList.contains("list")) { - node = node.parentNode; - } - - if (node !== this.list && node !== this._doc.documentElement) { - return node; - } - - return null; - }, - - _getSelectionPosition(itemNode) { - let parent = this._findParentBranchNode(itemNode); - let parentPosition = this._indexOfNode(parent.parentNode, parent); - let childPosition = -1; - // if the node is not a client, find its position within the parent - if (parent !== itemNode) { - childPosition = this._indexOfNode(itemNode.parentNode, itemNode); - } - return [parentPosition, childPosition]; - }, - - _indexOfNode(parent, child) { - return Array.prototype.indexOf.call(parent.childNodes, child); - }, - - _isTab(item) { - return item && item.classList.contains("tab"); - } -}; diff --git a/application/basilisk/components/syncedtabs/jar.mn b/application/basilisk/components/syncedtabs/jar.mn deleted file mode 100644 index ba2b105a1..000000000 --- a/application/basilisk/components/syncedtabs/jar.mn +++ /dev/null @@ -1,7 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -browser.jar: - content/browser/syncedtabs/sidebar.xhtml - content/browser/syncedtabs/sidebar.js diff --git a/application/basilisk/components/syncedtabs/moz.build b/application/basilisk/components/syncedtabs/moz.build deleted file mode 100644 index a6515d6a1..000000000 --- a/application/basilisk/components/syncedtabs/moz.build +++ /dev/null @@ -1,17 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -JAR_MANIFESTS += ['jar.mn'] - -EXTRA_JS_MODULES.syncedtabs += [ - 'EventEmitter.jsm', - 'SyncedTabsDeckComponent.js', - 'SyncedTabsDeckStore.js', - 'SyncedTabsDeckView.js', - 'SyncedTabsListStore.js', - 'TabListComponent.js', - 'TabListView.js', - 'util.js', -] - diff --git a/application/basilisk/components/syncedtabs/sidebar.js b/application/basilisk/components/syncedtabs/sidebar.js deleted file mode 100644 index 84df95e9d..000000000 --- a/application/basilisk/components/syncedtabs/sidebar.js +++ /dev/null @@ -1,30 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://services-sync/SyncedTabs.jsm"); -Cu.import("resource:///modules/syncedtabs/SyncedTabsDeckComponent.js"); - -XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", - "resource://gre/modules/FxAccounts.jsm"); - -this.syncedTabsDeckComponent = new SyncedTabsDeckComponent({window, SyncedTabs, fxAccounts}); - -let onLoaded = () => { - syncedTabsDeckComponent.init(); - document.getElementById("template-container").appendChild(syncedTabsDeckComponent.container); -}; - -let onUnloaded = () => { - removeEventListener("DOMContentLoaded", onLoaded); - removeEventListener("unload", onUnloaded); - syncedTabsDeckComponent.uninit(); -}; - -addEventListener("DOMContentLoaded", onLoaded); -addEventListener("unload", onUnloaded); diff --git a/application/basilisk/components/syncedtabs/sidebar.xhtml b/application/basilisk/components/syncedtabs/sidebar.xhtml deleted file mode 100644 index 3efcbea0e..000000000 --- a/application/basilisk/components/syncedtabs/sidebar.xhtml +++ /dev/null @@ -1,114 +0,0 @@ -<?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/. --> - -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" [ - <!ENTITY % browserDTD SYSTEM "chrome://browser/locale/browser.dtd"> - %browserDTD; - <!ENTITY % globalDTD - SYSTEM "chrome://global/locale/global.dtd"> - %globalDTD; - <!ENTITY % syncBrandDTD - SYSTEM "chrome://browser/locale/syncBrand.dtd"> - %syncBrandDTD; -]> -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> - <head> - <script src="chrome://browser/content/syncedtabs/sidebar.js" type="application/javascript;version=1.8"></script> - <script type="application/javascript" src="chrome://browser/content/utilityOverlay.js"/> - - <link rel="stylesheet" type="text/css" media="all" href="chrome://browser/skin/syncedtabs/sidebar.css"/> - <link rel="stylesheet" type="text/css" media="all" href="chrome://global/skin/"/> - <link rel="stylesheet" type="text/css" media="all" href="chrome://global/skin/textbox.css"/> - <link rel="stylesheet" type="text/css" media="all" href="chrome://browser/content/browser.css"/> - <title>&syncedTabs.sidebar.label;</title> - </head> - - <body dir="&locale.dir;" role="application"> - <template id="client-template"> - <div class="item client" role="option" tabindex="-1"> - <div class="item-title-container"> - <div class="item-twisty-container"></div> - <div class="item-icon-container"></div> - <p class="item-title"></p> - </div> - <div class="item-tabs-list"></div> - </div> - </template> - <template id="empty-client-template"> - <div class="item empty client" role="option" tabindex="-1"> - <div class="item-title-container"> - <div class="item-twisty-container"></div> - <div class="item-icon-container"></div> - <p class="item-title"></p> - </div> - <div class="item-tabs-list"> - <div class="item empty" role="option" tabindex="-1"> - <div class="item-title-container"> - <div class="item-icon-container"></div> - <p class="item-title">&syncedTabs.sidebar.notabs.label;</p> - </div> - </div> - </div> - </div> - </template> - <template id="tab-template"> - <div class="item tab" role="option" tabindex="-1"> - <div class="item-title-container"> - <div class="item-icon-container"></div> - <p class="item-title"></p> - </div> - </div> - </template> - - <template id="tabs-container-template"> - <div class="tabs-container"> - <div class="list" role="listbox"></div> - </div> - </template> - - <template id="deck-template"> - <div class="deck"> - <div class="tabs-fetching sync-state"> - <!-- Show intentionally blank panel, see bug 1239845 --> - </div> - <div class="notAuthedInfo sync-state"> - <p>&syncedTabs.sidebar.notsignedin.label;</p> - <p><a href="#" class="sync-prefs text-link">&fxaSignIn.label;</a></p> - </div> - <div class="singleDeviceInfo sync-state"> - <p>&syncedTabs.sidebar.noclients.title;</p> - <p>&syncedTabs.sidebar.noclients.subtitle;</p> - <p class="device-promo" fxAccountsBrand="&syncBrand.fxAccount.label;"></p> - </div> - <div class="tabs-disabled sync-state"> - <p>&syncedTabs.sidebar.tabsnotsyncing.label;</p> - <p><a href="#" class="sync-prefs text-link">&syncedTabs.sidebar.openprefs.label;</a></p> - </div> - </div> - </template> - - <div class="content-container"> - <!-- the non-scrollable header --> - <div class="content-header"> - <div class="sidebar-search-container tabs-container sync-state"> - <div class="search-box compact"> - <div class="textbox-input-box"> - <input type="text" class="tabsFilter textbox-input" tabindex="1"/> - <div class="textbox-search-icons"> - <a class="textbox-search-clear"></a> - <a class="textbox-search-icon"></a> - </div> - </div> - </div> - </div> - </div> - <!-- the scrollable content area where our templates are inserted --> - <div id="template-container" class="content-scrollable" tabindex="-1"> - </div> - </div> - </body> -</html> diff --git a/application/basilisk/components/syncedtabs/util.js b/application/basilisk/components/syncedtabs/util.js deleted file mode 100644 index e09a1a528..000000000 --- a/application/basilisk/components/syncedtabs/util.js +++ /dev/null @@ -1,23 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; - -this.EXPORTED_SYMBOLS = [ - "getChromeWindow" -]; - -// Get the chrome (ie, browser) window hosting this content. -function getChromeWindow(window) { - return window - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIWebNavigation) - .QueryInterface(Ci.nsIDocShellTreeItem) - .rootTreeItem - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow) - .wrappedJSObject; -} |