Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | let Cc = Components.classes; |
michael@0 | 8 | let Ci = Components.interfaces; |
michael@0 | 9 | let Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | this.EXPORTED_SYMBOLS = [ "AboutHomeUtils", "AboutHome" ]; |
michael@0 | 12 | |
michael@0 | 13 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 14 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 15 | |
michael@0 | 16 | XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", |
michael@0 | 17 | "resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 18 | XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts", |
michael@0 | 19 | "resource://gre/modules/FxAccounts.jsm"); |
michael@0 | 20 | |
michael@0 | 21 | // Url to fetch snippets, in the urlFormatter service format. |
michael@0 | 22 | const SNIPPETS_URL_PREF = "browser.aboutHomeSnippets.updateUrl"; |
michael@0 | 23 | |
michael@0 | 24 | // Should be bumped up if the snippets content format changes. |
michael@0 | 25 | const STARTPAGE_VERSION = 4; |
michael@0 | 26 | |
michael@0 | 27 | this.AboutHomeUtils = { |
michael@0 | 28 | get snippetsVersion() STARTPAGE_VERSION, |
michael@0 | 29 | |
michael@0 | 30 | /* |
michael@0 | 31 | * showKnowYourRights - Determines if the user should be shown the |
michael@0 | 32 | * about:rights notification. The notification should *not* be shown if |
michael@0 | 33 | * we've already shown the current version, or if the override pref says to |
michael@0 | 34 | * never show it. The notification *should* be shown if it's never been seen |
michael@0 | 35 | * before, if a newer version is available, or if the override pref says to |
michael@0 | 36 | * always show it. |
michael@0 | 37 | */ |
michael@0 | 38 | get showKnowYourRights() { |
michael@0 | 39 | // Look for an unconditional override pref. If set, do what it says. |
michael@0 | 40 | // (true --> never show, false --> always show) |
michael@0 | 41 | try { |
michael@0 | 42 | return !Services.prefs.getBoolPref("browser.rights.override"); |
michael@0 | 43 | } catch (e) { } |
michael@0 | 44 | // Ditto, for the legacy EULA pref. |
michael@0 | 45 | try { |
michael@0 | 46 | return !Services.prefs.getBoolPref("browser.EULA.override"); |
michael@0 | 47 | } catch (e) { } |
michael@0 | 48 | |
michael@0 | 49 | #ifndef MOZILLA_OFFICIAL |
michael@0 | 50 | // Non-official builds shouldn't show the notification. |
michael@0 | 51 | return false; |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | // Look to see if the user has seen the current version or not. |
michael@0 | 55 | var currentVersion = Services.prefs.getIntPref("browser.rights.version"); |
michael@0 | 56 | try { |
michael@0 | 57 | return !Services.prefs.getBoolPref("browser.rights." + currentVersion + ".shown"); |
michael@0 | 58 | } catch (e) { } |
michael@0 | 59 | |
michael@0 | 60 | // Legacy: If the user accepted a EULA, we won't annoy them with the |
michael@0 | 61 | // equivalent about:rights page until the version changes. |
michael@0 | 62 | try { |
michael@0 | 63 | return !Services.prefs.getBoolPref("browser.EULA." + currentVersion + ".accepted"); |
michael@0 | 64 | } catch (e) { } |
michael@0 | 65 | |
michael@0 | 66 | // We haven't shown the notification before, so do so now. |
michael@0 | 67 | return true; |
michael@0 | 68 | } |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Returns the URL to fetch snippets from, in the urlFormatter service format. |
michael@0 | 73 | */ |
michael@0 | 74 | XPCOMUtils.defineLazyGetter(AboutHomeUtils, "snippetsURL", function() { |
michael@0 | 75 | let updateURL = Services.prefs |
michael@0 | 76 | .getCharPref(SNIPPETS_URL_PREF) |
michael@0 | 77 | .replace("%STARTPAGE_VERSION%", STARTPAGE_VERSION); |
michael@0 | 78 | return Services.urlFormatter.formatURL(updateURL); |
michael@0 | 79 | }); |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * This code provides services to the about:home page. Whenever |
michael@0 | 83 | * about:home needs to do something chrome-privileged, it sends a |
michael@0 | 84 | * message that's handled here. |
michael@0 | 85 | */ |
michael@0 | 86 | let AboutHome = { |
michael@0 | 87 | MESSAGES: [ |
michael@0 | 88 | "AboutHome:RestorePreviousSession", |
michael@0 | 89 | "AboutHome:Downloads", |
michael@0 | 90 | "AboutHome:Bookmarks", |
michael@0 | 91 | "AboutHome:History", |
michael@0 | 92 | "AboutHome:Apps", |
michael@0 | 93 | "AboutHome:Addons", |
michael@0 | 94 | "AboutHome:Sync", |
michael@0 | 95 | "AboutHome:Settings", |
michael@0 | 96 | "AboutHome:RequestUpdate", |
michael@0 | 97 | "AboutHome:Search", |
michael@0 | 98 | ], |
michael@0 | 99 | |
michael@0 | 100 | init: function() { |
michael@0 | 101 | let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager); |
michael@0 | 102 | |
michael@0 | 103 | for (let msg of this.MESSAGES) { |
michael@0 | 104 | mm.addMessageListener(msg, this); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | Services.obs.addObserver(this, "browser-search-engine-modified", false); |
michael@0 | 108 | }, |
michael@0 | 109 | |
michael@0 | 110 | observe: function(aEngine, aTopic, aVerb) { |
michael@0 | 111 | switch (aTopic) { |
michael@0 | 112 | case "browser-search-engine-modified": |
michael@0 | 113 | this.sendAboutHomeData(null); |
michael@0 | 114 | break; |
michael@0 | 115 | } |
michael@0 | 116 | }, |
michael@0 | 117 | |
michael@0 | 118 | receiveMessage: function(aMessage) { |
michael@0 | 119 | let window = aMessage.target.ownerDocument.defaultView; |
michael@0 | 120 | |
michael@0 | 121 | switch (aMessage.name) { |
michael@0 | 122 | case "AboutHome:RestorePreviousSession": |
michael@0 | 123 | let ss = Cc["@mozilla.org/browser/sessionstore;1"]. |
michael@0 | 124 | getService(Ci.nsISessionStore); |
michael@0 | 125 | if (ss.canRestoreLastSession) { |
michael@0 | 126 | ss.restoreLastSession(); |
michael@0 | 127 | } |
michael@0 | 128 | break; |
michael@0 | 129 | |
michael@0 | 130 | case "AboutHome:Downloads": |
michael@0 | 131 | window.BrowserDownloadsUI(); |
michael@0 | 132 | break; |
michael@0 | 133 | |
michael@0 | 134 | case "AboutHome:Bookmarks": |
michael@0 | 135 | window.PlacesCommandHook.showPlacesOrganizer("AllBookmarks"); |
michael@0 | 136 | break; |
michael@0 | 137 | |
michael@0 | 138 | case "AboutHome:History": |
michael@0 | 139 | window.PlacesCommandHook.showPlacesOrganizer("History"); |
michael@0 | 140 | break; |
michael@0 | 141 | |
michael@0 | 142 | case "AboutHome:Apps": |
michael@0 | 143 | window.openUILinkIn("https://marketplace.mozilla.org/", "tab"); |
michael@0 | 144 | break; |
michael@0 | 145 | |
michael@0 | 146 | case "AboutHome:Addons": |
michael@0 | 147 | window.BrowserOpenAddonsMgr(); |
michael@0 | 148 | break; |
michael@0 | 149 | |
michael@0 | 150 | case "AboutHome:Sync": |
michael@0 | 151 | let weave = Cc["@mozilla.org/weave/service;1"] |
michael@0 | 152 | .getService(Ci.nsISupports) |
michael@0 | 153 | .wrappedJSObject; |
michael@0 | 154 | |
michael@0 | 155 | if (weave.fxAccountsEnabled) { |
michael@0 | 156 | fxAccounts.getSignedInUser().then(userData => { |
michael@0 | 157 | if (userData) { |
michael@0 | 158 | window.openPreferences("paneSync"); |
michael@0 | 159 | } else { |
michael@0 | 160 | window.loadURI("about:accounts"); |
michael@0 | 161 | } |
michael@0 | 162 | }); |
michael@0 | 163 | } else { |
michael@0 | 164 | window.openPreferences("paneSync"); |
michael@0 | 165 | } |
michael@0 | 166 | break; |
michael@0 | 167 | |
michael@0 | 168 | case "AboutHome:Settings": |
michael@0 | 169 | window.openPreferences(); |
michael@0 | 170 | break; |
michael@0 | 171 | |
michael@0 | 172 | case "AboutHome:RequestUpdate": |
michael@0 | 173 | this.sendAboutHomeData(aMessage.target); |
michael@0 | 174 | break; |
michael@0 | 175 | |
michael@0 | 176 | case "AboutHome:Search": |
michael@0 | 177 | let data; |
michael@0 | 178 | try { |
michael@0 | 179 | data = JSON.parse(aMessage.data.searchData); |
michael@0 | 180 | } catch(ex) { |
michael@0 | 181 | Cu.reportError(ex); |
michael@0 | 182 | break; |
michael@0 | 183 | } |
michael@0 | 184 | let engine = Services.search.currentEngine; |
michael@0 | 185 | #ifdef MOZ_SERVICES_HEALTHREPORT |
michael@0 | 186 | window.BrowserSearch.recordSearchInHealthReport(engine, "abouthome"); |
michael@0 | 187 | #endif |
michael@0 | 188 | // Trigger a search through nsISearchEngine.getSubmission() |
michael@0 | 189 | let submission = engine.getSubmission(data.searchTerms, null, "homepage"); |
michael@0 | 190 | window.loadURI(submission.uri.spec, null, submission.postData); |
michael@0 | 191 | break; |
michael@0 | 192 | } |
michael@0 | 193 | }, |
michael@0 | 194 | |
michael@0 | 195 | // Send all the chrome-privileged data needed by about:home. This |
michael@0 | 196 | // gets re-sent when the search engine changes. |
michael@0 | 197 | sendAboutHomeData: function(target) { |
michael@0 | 198 | let wrapper = {}; |
michael@0 | 199 | Components.utils.import("resource:///modules/sessionstore/SessionStore.jsm", |
michael@0 | 200 | wrapper); |
michael@0 | 201 | let ss = wrapper.SessionStore; |
michael@0 | 202 | ss.promiseInitialized.then(function() { |
michael@0 | 203 | let data = { |
michael@0 | 204 | showRestoreLastSession: ss.canRestoreLastSession, |
michael@0 | 205 | snippetsURL: AboutHomeUtils.snippetsURL, |
michael@0 | 206 | showKnowYourRights: AboutHomeUtils.showKnowYourRights, |
michael@0 | 207 | snippetsVersion: AboutHomeUtils.snippetsVersion, |
michael@0 | 208 | defaultEngineName: Services.search.defaultEngine.name |
michael@0 | 209 | }; |
michael@0 | 210 | |
michael@0 | 211 | if (AboutHomeUtils.showKnowYourRights) { |
michael@0 | 212 | // Set pref to indicate we've shown the notification. |
michael@0 | 213 | let currentVersion = Services.prefs.getIntPref("browser.rights.version"); |
michael@0 | 214 | Services.prefs.setBoolPref("browser.rights." + currentVersion + ".shown", true); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | if (target) { |
michael@0 | 218 | target.messageManager.sendAsyncMessage("AboutHome:Update", data); |
michael@0 | 219 | } else { |
michael@0 | 220 | let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager); |
michael@0 | 221 | mm.broadcastAsyncMessage("AboutHome:Update", data); |
michael@0 | 222 | } |
michael@0 | 223 | }).then(null, function onError(x) { |
michael@0 | 224 | Cu.reportError("Error in AboutHome.sendAboutHomeData: " + x); |
michael@0 | 225 | }); |
michael@0 | 226 | }, |
michael@0 | 227 | }; |