browser/modules/AboutHome.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/modules/AboutHome.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,227 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +let Cc = Components.classes;
    1.11 +let Ci = Components.interfaces;
    1.12 +let Cu = Components.utils;
    1.13 +
    1.14 +this.EXPORTED_SYMBOLS = [ "AboutHomeUtils", "AboutHome" ];
    1.15 +
    1.16 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.18 +
    1.19 +XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
    1.20 +  "resource://gre/modules/PrivateBrowsingUtils.jsm");
    1.21 +XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
    1.22 +  "resource://gre/modules/FxAccounts.jsm");
    1.23 +
    1.24 +// Url to fetch snippets, in the urlFormatter service format.
    1.25 +const SNIPPETS_URL_PREF = "browser.aboutHomeSnippets.updateUrl";
    1.26 +
    1.27 +// Should be bumped up if the snippets content format changes.
    1.28 +const STARTPAGE_VERSION = 4;
    1.29 +
    1.30 +this.AboutHomeUtils = {
    1.31 +  get snippetsVersion() STARTPAGE_VERSION,
    1.32 +
    1.33 +  /*
    1.34 +   * showKnowYourRights - Determines if the user should be shown the
    1.35 +   * about:rights notification. The notification should *not* be shown if
    1.36 +   * we've already shown the current version, or if the override pref says to
    1.37 +   * never show it. The notification *should* be shown if it's never been seen
    1.38 +   * before, if a newer version is available, or if the override pref says to
    1.39 +   * always show it.
    1.40 +   */
    1.41 +  get showKnowYourRights() {
    1.42 +    // Look for an unconditional override pref. If set, do what it says.
    1.43 +    // (true --> never show, false --> always show)
    1.44 +    try {
    1.45 +      return !Services.prefs.getBoolPref("browser.rights.override");
    1.46 +    } catch (e) { }
    1.47 +    // Ditto, for the legacy EULA pref.
    1.48 +    try {
    1.49 +      return !Services.prefs.getBoolPref("browser.EULA.override");
    1.50 +    } catch (e) { }
    1.51 +
    1.52 +#ifndef MOZILLA_OFFICIAL
    1.53 +    // Non-official builds shouldn't show the notification.
    1.54 +    return false;
    1.55 +#endif
    1.56 +
    1.57 +    // Look to see if the user has seen the current version or not.
    1.58 +    var currentVersion = Services.prefs.getIntPref("browser.rights.version");
    1.59 +    try {
    1.60 +      return !Services.prefs.getBoolPref("browser.rights." + currentVersion + ".shown");
    1.61 +    } catch (e) { }
    1.62 +
    1.63 +    // Legacy: If the user accepted a EULA, we won't annoy them with the
    1.64 +    // equivalent about:rights page until the version changes.
    1.65 +    try {
    1.66 +      return !Services.prefs.getBoolPref("browser.EULA." + currentVersion + ".accepted");
    1.67 +    } catch (e) { }
    1.68 +
    1.69 +    // We haven't shown the notification before, so do so now.
    1.70 +    return true;
    1.71 +  }
    1.72 +};
    1.73 +
    1.74 +/**
    1.75 + * Returns the URL to fetch snippets from, in the urlFormatter service format.
    1.76 + */
    1.77 +XPCOMUtils.defineLazyGetter(AboutHomeUtils, "snippetsURL", function() {
    1.78 +  let updateURL = Services.prefs
    1.79 +                          .getCharPref(SNIPPETS_URL_PREF)
    1.80 +                          .replace("%STARTPAGE_VERSION%", STARTPAGE_VERSION);
    1.81 +  return Services.urlFormatter.formatURL(updateURL);
    1.82 +});
    1.83 +
    1.84 +/**
    1.85 + * This code provides services to the about:home page. Whenever
    1.86 + * about:home needs to do something chrome-privileged, it sends a
    1.87 + * message that's handled here.
    1.88 + */
    1.89 +let AboutHome = {
    1.90 +  MESSAGES: [
    1.91 +    "AboutHome:RestorePreviousSession",
    1.92 +    "AboutHome:Downloads",
    1.93 +    "AboutHome:Bookmarks",
    1.94 +    "AboutHome:History",
    1.95 +    "AboutHome:Apps",
    1.96 +    "AboutHome:Addons",
    1.97 +    "AboutHome:Sync",
    1.98 +    "AboutHome:Settings",
    1.99 +    "AboutHome:RequestUpdate",
   1.100 +    "AboutHome:Search",
   1.101 +  ],
   1.102 +
   1.103 +  init: function() {
   1.104 +    let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
   1.105 +
   1.106 +    for (let msg of this.MESSAGES) {
   1.107 +      mm.addMessageListener(msg, this);
   1.108 +    }
   1.109 +
   1.110 +    Services.obs.addObserver(this, "browser-search-engine-modified", false);
   1.111 +  },
   1.112 +
   1.113 +  observe: function(aEngine, aTopic, aVerb) {
   1.114 +    switch (aTopic) {
   1.115 +      case "browser-search-engine-modified":
   1.116 +        this.sendAboutHomeData(null);
   1.117 +        break;
   1.118 +    }
   1.119 +  },
   1.120 +
   1.121 +  receiveMessage: function(aMessage) {
   1.122 +    let window = aMessage.target.ownerDocument.defaultView;
   1.123 +
   1.124 +    switch (aMessage.name) {
   1.125 +      case "AboutHome:RestorePreviousSession":
   1.126 +        let ss = Cc["@mozilla.org/browser/sessionstore;1"].
   1.127 +                 getService(Ci.nsISessionStore);
   1.128 +        if (ss.canRestoreLastSession) {
   1.129 +          ss.restoreLastSession();
   1.130 +        }
   1.131 +        break;
   1.132 +
   1.133 +      case "AboutHome:Downloads":
   1.134 +        window.BrowserDownloadsUI();
   1.135 +        break;
   1.136 +
   1.137 +      case "AboutHome:Bookmarks":
   1.138 +        window.PlacesCommandHook.showPlacesOrganizer("AllBookmarks");
   1.139 +        break;
   1.140 +
   1.141 +      case "AboutHome:History":
   1.142 +        window.PlacesCommandHook.showPlacesOrganizer("History");
   1.143 +        break;
   1.144 +
   1.145 +      case "AboutHome:Apps":
   1.146 +        window.openUILinkIn("https://marketplace.mozilla.org/", "tab");
   1.147 +        break;
   1.148 +
   1.149 +      case "AboutHome:Addons":
   1.150 +        window.BrowserOpenAddonsMgr();
   1.151 +        break;
   1.152 +
   1.153 +      case "AboutHome:Sync":
   1.154 +        let weave = Cc["@mozilla.org/weave/service;1"]
   1.155 +                      .getService(Ci.nsISupports)
   1.156 +                      .wrappedJSObject;
   1.157 +
   1.158 +        if (weave.fxAccountsEnabled) {
   1.159 +          fxAccounts.getSignedInUser().then(userData => {
   1.160 +            if (userData) {
   1.161 +              window.openPreferences("paneSync");
   1.162 +            } else {
   1.163 +              window.loadURI("about:accounts");
   1.164 +            }
   1.165 +          });
   1.166 +        } else {
   1.167 +          window.openPreferences("paneSync");
   1.168 +        }
   1.169 +        break;
   1.170 +
   1.171 +      case "AboutHome:Settings":
   1.172 +        window.openPreferences();
   1.173 +        break;
   1.174 +
   1.175 +      case "AboutHome:RequestUpdate":
   1.176 +        this.sendAboutHomeData(aMessage.target);
   1.177 +        break;
   1.178 +
   1.179 +      case "AboutHome:Search":
   1.180 +        let data;
   1.181 +        try {
   1.182 +          data = JSON.parse(aMessage.data.searchData);
   1.183 +        } catch(ex) {
   1.184 +          Cu.reportError(ex);
   1.185 +          break;
   1.186 +        }
   1.187 +        let engine = Services.search.currentEngine;
   1.188 +#ifdef MOZ_SERVICES_HEALTHREPORT
   1.189 +        window.BrowserSearch.recordSearchInHealthReport(engine, "abouthome");
   1.190 +#endif
   1.191 +        // Trigger a search through nsISearchEngine.getSubmission()
   1.192 +        let submission = engine.getSubmission(data.searchTerms, null, "homepage");
   1.193 +        window.loadURI(submission.uri.spec, null, submission.postData);
   1.194 +        break;
   1.195 +    }
   1.196 +  },
   1.197 +
   1.198 +  // Send all the chrome-privileged data needed by about:home. This
   1.199 +  // gets re-sent when the search engine changes.
   1.200 +  sendAboutHomeData: function(target) {
   1.201 +    let wrapper = {};
   1.202 +    Components.utils.import("resource:///modules/sessionstore/SessionStore.jsm",
   1.203 +      wrapper);
   1.204 +    let ss = wrapper.SessionStore;
   1.205 +    ss.promiseInitialized.then(function() {
   1.206 +      let data = {
   1.207 +        showRestoreLastSession: ss.canRestoreLastSession,
   1.208 +        snippetsURL: AboutHomeUtils.snippetsURL,
   1.209 +        showKnowYourRights: AboutHomeUtils.showKnowYourRights,
   1.210 +        snippetsVersion: AboutHomeUtils.snippetsVersion,
   1.211 +        defaultEngineName: Services.search.defaultEngine.name
   1.212 +      };
   1.213 +
   1.214 +      if (AboutHomeUtils.showKnowYourRights) {
   1.215 +        // Set pref to indicate we've shown the notification.
   1.216 +        let currentVersion = Services.prefs.getIntPref("browser.rights.version");
   1.217 +        Services.prefs.setBoolPref("browser.rights." + currentVersion + ".shown", true);
   1.218 +      }
   1.219 +
   1.220 +      if (target) {
   1.221 +        target.messageManager.sendAsyncMessage("AboutHome:Update", data);
   1.222 +      } else {
   1.223 +        let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
   1.224 +        mm.broadcastAsyncMessage("AboutHome:Update", data);
   1.225 +      }
   1.226 +    }).then(null, function onError(x) {
   1.227 +      Cu.reportError("Error in AboutHome.sendAboutHomeData: " + x);
   1.228 +    });
   1.229 +  },
   1.230 +};

mercurial