browser/modules/AboutHome.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 "use strict";
     7 let Cc = Components.classes;
     8 let Ci = Components.interfaces;
     9 let Cu = Components.utils;
    11 this.EXPORTED_SYMBOLS = [ "AboutHomeUtils", "AboutHome" ];
    13 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    14 Components.utils.import("resource://gre/modules/Services.jsm");
    16 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
    17   "resource://gre/modules/PrivateBrowsingUtils.jsm");
    18 XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
    19   "resource://gre/modules/FxAccounts.jsm");
    21 // Url to fetch snippets, in the urlFormatter service format.
    22 const SNIPPETS_URL_PREF = "browser.aboutHomeSnippets.updateUrl";
    24 // Should be bumped up if the snippets content format changes.
    25 const STARTPAGE_VERSION = 4;
    27 this.AboutHomeUtils = {
    28   get snippetsVersion() STARTPAGE_VERSION,
    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) { }
    49 #ifndef MOZILLA_OFFICIAL
    50     // Non-official builds shouldn't show the notification.
    51     return false;
    52 #endif
    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) { }
    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) { }
    66     // We haven't shown the notification before, so do so now.
    67     return true;
    68   }
    69 };
    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 });
    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   ],
   100   init: function() {
   101     let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
   103     for (let msg of this.MESSAGES) {
   104       mm.addMessageListener(msg, this);
   105     }
   107     Services.obs.addObserver(this, "browser-search-engine-modified", false);
   108   },
   110   observe: function(aEngine, aTopic, aVerb) {
   111     switch (aTopic) {
   112       case "browser-search-engine-modified":
   113         this.sendAboutHomeData(null);
   114         break;
   115     }
   116   },
   118   receiveMessage: function(aMessage) {
   119     let window = aMessage.target.ownerDocument.defaultView;
   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;
   130       case "AboutHome:Downloads":
   131         window.BrowserDownloadsUI();
   132         break;
   134       case "AboutHome:Bookmarks":
   135         window.PlacesCommandHook.showPlacesOrganizer("AllBookmarks");
   136         break;
   138       case "AboutHome:History":
   139         window.PlacesCommandHook.showPlacesOrganizer("History");
   140         break;
   142       case "AboutHome:Apps":
   143         window.openUILinkIn("https://marketplace.mozilla.org/", "tab");
   144         break;
   146       case "AboutHome:Addons":
   147         window.BrowserOpenAddonsMgr();
   148         break;
   150       case "AboutHome:Sync":
   151         let weave = Cc["@mozilla.org/weave/service;1"]
   152                       .getService(Ci.nsISupports)
   153                       .wrappedJSObject;
   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;
   168       case "AboutHome:Settings":
   169         window.openPreferences();
   170         break;
   172       case "AboutHome:RequestUpdate":
   173         this.sendAboutHomeData(aMessage.target);
   174         break;
   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   },
   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       };
   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       }
   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