browser/modules/AboutHome.jsm

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

mercurial