1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/metro/components/BrowserStartup.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 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 +const Cc = Components.classes; 1.8 +const Ci = Components.interfaces; 1.9 +const Cu = Components.utils; 1.10 + 1.11 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.12 +Cu.import("resource://gre/modules/Services.jsm"); 1.13 + 1.14 +XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", 1.15 + "resource://gre/modules/PlacesUtils.jsm"); 1.16 +XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", 1.17 + "resource://gre/modules/NetUtil.jsm"); 1.18 +XPCOMUtils.defineLazyModuleGetter(this, "Task", 1.19 + "resource://gre/modules/Task.jsm"); 1.20 + 1.21 +// Custom factory object to ensure that we're a singleton 1.22 +const BrowserStartupServiceFactory = { 1.23 + _instance: null, 1.24 + createInstance: function (outer, iid) { 1.25 + if (outer != null) 1.26 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.27 + return this._instance || (this._instance = new BrowserStartup()); 1.28 + } 1.29 +}; 1.30 + 1.31 +function BrowserStartup() { 1.32 + this._init(); 1.33 +} 1.34 + 1.35 +BrowserStartup.prototype = { 1.36 + // for XPCOM 1.37 + classID: Components.ID("{1d542abc-c88b-4636-a4ef-075b49806317}"), 1.38 + 1.39 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]), 1.40 + 1.41 + _xpcom_factory: BrowserStartupServiceFactory, 1.42 + 1.43 + _init: function() { 1.44 + Services.obs.addObserver(this, "places-init-complete", false); 1.45 + Services.obs.addObserver(this, "final-ui-startup", false); 1.46 + }, 1.47 + 1.48 + _initDefaultBookmarks: function() { 1.49 + // We must instantiate the history service since it will tell us if we 1.50 + // need to import or restore bookmarks due to first-run, corruption or 1.51 + // forced migration (due to a major schema change). 1.52 + let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.53 + getService(Ci.nsINavHistoryService); 1.54 + 1.55 + // If the database is corrupt or has been newly created we should 1.56 + // import bookmarks. 1.57 + let databaseStatus = histsvc.databaseStatus; 1.58 + let importBookmarks = databaseStatus == histsvc.DATABASE_STATUS_CREATE || 1.59 + databaseStatus == histsvc.DATABASE_STATUS_CORRUPT; 1.60 + 1.61 + if (!importBookmarks) { 1.62 + // Check to see whether "mobile" root already exists. This is to handle 1.63 + // existing profiles created with pre-1.0 builds (which won't have mobile 1.64 + // bookmarks root). We can remove this eventually when we stop 1.65 + // caring about users migrating to current builds with pre-1.0 profiles. 1.66 + let annos = Cc["@mozilla.org/browser/annotation-service;1"]. 1.67 + getService(Ci.nsIAnnotationService); 1.68 + let metroRootItems = annos.getItemsWithAnnotation("metro/bookmarksRoot", {}); 1.69 + if (metroRootItems.length > 0) 1.70 + return; // no need to do initial import 1.71 + } 1.72 + 1.73 + Cu.import("resource://gre/modules/BookmarkJSONUtils.jsm"); 1.74 + 1.75 + Task.spawn(function() { 1.76 + yield BookmarkJSONUtils.importFromURL("chrome://browser/locale/bookmarks.json", false); 1.77 + 1.78 + // Create the new smart bookmark. 1.79 + const MAX_RESULTS = 10; 1.80 + const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark"; 1.81 + 1.82 + // Place the Metro folder at the end of the smart bookmarks list. 1.83 + let maxIndex = Math.max.apply(null, 1.84 + PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO).map(id => { 1.85 + return PlacesUtils.bookmarks.getItemIndex(id); 1.86 + })); 1.87 + let smartBookmarkId = 1.88 + PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId, 1.89 + NetUtil.newURI("place:folder=" + 1.90 + PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0] + 1.91 + "&queryType=" + 1.92 + Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS + 1.93 + "&sort=" + 1.94 + Ci.nsINavHistoryQueryOptions.SORT_BY_DATEADDED_DESCENDING + 1.95 + "&maxResults=" + MAX_RESULTS + 1.96 + "&excludeQueries=1"), 1.97 + maxIndex + 1, 1.98 + PlacesUtils.getString("windows8TouchTitle")); 1.99 + PlacesUtils.annotations.setItemAnnotation(smartBookmarkId, 1.100 + SMART_BOOKMARKS_ANNO, 1.101 + "Windows8Touch", 0, 1.102 + PlacesUtils.annotations.EXPIRE_NEVER); 1.103 + }); 1.104 + }, 1.105 + 1.106 + _startupActions: function() { 1.107 + }, 1.108 + 1.109 + // nsIObserver 1.110 + observe: function(aSubject, aTopic, aData) { 1.111 + switch (aTopic) { 1.112 + case "places-init-complete": 1.113 + Services.obs.removeObserver(this, "places-init-complete"); 1.114 + this._initDefaultBookmarks(); 1.115 + break; 1.116 + case "final-ui-startup": 1.117 + Services.obs.removeObserver(this, "final-ui-startup"); 1.118 + this._startupActions(); 1.119 + break; 1.120 + } 1.121 + } 1.122 +}; 1.123 + 1.124 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([BrowserStartup]);