browser/metro/components/BrowserStartup.js

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

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 const Cc = Components.classes;
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cu = Components.utils;
michael@0 7
michael@0 8 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 9 Cu.import("resource://gre/modules/Services.jsm");
michael@0 10
michael@0 11 XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
michael@0 12 "resource://gre/modules/PlacesUtils.jsm");
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
michael@0 14 "resource://gre/modules/NetUtil.jsm");
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 16 "resource://gre/modules/Task.jsm");
michael@0 17
michael@0 18 // Custom factory object to ensure that we're a singleton
michael@0 19 const BrowserStartupServiceFactory = {
michael@0 20 _instance: null,
michael@0 21 createInstance: function (outer, iid) {
michael@0 22 if (outer != null)
michael@0 23 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 24 return this._instance || (this._instance = new BrowserStartup());
michael@0 25 }
michael@0 26 };
michael@0 27
michael@0 28 function BrowserStartup() {
michael@0 29 this._init();
michael@0 30 }
michael@0 31
michael@0 32 BrowserStartup.prototype = {
michael@0 33 // for XPCOM
michael@0 34 classID: Components.ID("{1d542abc-c88b-4636-a4ef-075b49806317}"),
michael@0 35
michael@0 36 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
michael@0 37
michael@0 38 _xpcom_factory: BrowserStartupServiceFactory,
michael@0 39
michael@0 40 _init: function() {
michael@0 41 Services.obs.addObserver(this, "places-init-complete", false);
michael@0 42 Services.obs.addObserver(this, "final-ui-startup", false);
michael@0 43 },
michael@0 44
michael@0 45 _initDefaultBookmarks: function() {
michael@0 46 // We must instantiate the history service since it will tell us if we
michael@0 47 // need to import or restore bookmarks due to first-run, corruption or
michael@0 48 // forced migration (due to a major schema change).
michael@0 49 let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 50 getService(Ci.nsINavHistoryService);
michael@0 51
michael@0 52 // If the database is corrupt or has been newly created we should
michael@0 53 // import bookmarks.
michael@0 54 let databaseStatus = histsvc.databaseStatus;
michael@0 55 let importBookmarks = databaseStatus == histsvc.DATABASE_STATUS_CREATE ||
michael@0 56 databaseStatus == histsvc.DATABASE_STATUS_CORRUPT;
michael@0 57
michael@0 58 if (!importBookmarks) {
michael@0 59 // Check to see whether "mobile" root already exists. This is to handle
michael@0 60 // existing profiles created with pre-1.0 builds (which won't have mobile
michael@0 61 // bookmarks root). We can remove this eventually when we stop
michael@0 62 // caring about users migrating to current builds with pre-1.0 profiles.
michael@0 63 let annos = Cc["@mozilla.org/browser/annotation-service;1"].
michael@0 64 getService(Ci.nsIAnnotationService);
michael@0 65 let metroRootItems = annos.getItemsWithAnnotation("metro/bookmarksRoot", {});
michael@0 66 if (metroRootItems.length > 0)
michael@0 67 return; // no need to do initial import
michael@0 68 }
michael@0 69
michael@0 70 Cu.import("resource://gre/modules/BookmarkJSONUtils.jsm");
michael@0 71
michael@0 72 Task.spawn(function() {
michael@0 73 yield BookmarkJSONUtils.importFromURL("chrome://browser/locale/bookmarks.json", false);
michael@0 74
michael@0 75 // Create the new smart bookmark.
michael@0 76 const MAX_RESULTS = 10;
michael@0 77 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
michael@0 78
michael@0 79 // Place the Metro folder at the end of the smart bookmarks list.
michael@0 80 let maxIndex = Math.max.apply(null,
michael@0 81 PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO).map(id => {
michael@0 82 return PlacesUtils.bookmarks.getItemIndex(id);
michael@0 83 }));
michael@0 84 let smartBookmarkId =
michael@0 85 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId,
michael@0 86 NetUtil.newURI("place:folder=" +
michael@0 87 PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0] +
michael@0 88 "&queryType=" +
michael@0 89 Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS +
michael@0 90 "&sort=" +
michael@0 91 Ci.nsINavHistoryQueryOptions.SORT_BY_DATEADDED_DESCENDING +
michael@0 92 "&maxResults=" + MAX_RESULTS +
michael@0 93 "&excludeQueries=1"),
michael@0 94 maxIndex + 1,
michael@0 95 PlacesUtils.getString("windows8TouchTitle"));
michael@0 96 PlacesUtils.annotations.setItemAnnotation(smartBookmarkId,
michael@0 97 SMART_BOOKMARKS_ANNO,
michael@0 98 "Windows8Touch", 0,
michael@0 99 PlacesUtils.annotations.EXPIRE_NEVER);
michael@0 100 });
michael@0 101 },
michael@0 102
michael@0 103 _startupActions: function() {
michael@0 104 },
michael@0 105
michael@0 106 // nsIObserver
michael@0 107 observe: function(aSubject, aTopic, aData) {
michael@0 108 switch (aTopic) {
michael@0 109 case "places-init-complete":
michael@0 110 Services.obs.removeObserver(this, "places-init-complete");
michael@0 111 this._initDefaultBookmarks();
michael@0 112 break;
michael@0 113 case "final-ui-startup":
michael@0 114 Services.obs.removeObserver(this, "final-ui-startup");
michael@0 115 this._startupActions();
michael@0 116 break;
michael@0 117 }
michael@0 118 }
michael@0 119 };
michael@0 120
michael@0 121 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([BrowserStartup]);

mercurial