1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is the Browser Search Service.
16 # The Initial Developer of the Original Code is
18 # Portions created by the Initial Developer are Copyright (C) 2005
19 # the Initial Developer. All Rights Reserved.
22 # Giorgio Maone <g.maone@informaction.com>
23 # Seth Spitzer <sspitzer@mozilla.com>
24 # Asaf Romano <mano@mozilla.com>
25 # Marco Bonardo <mak77@bonardo.net>
26 # Dietrich Ayala <dietrich@mozilla.com>
27 # Ehsan Akhgari <ehsan.akhgari@gmail.com>
28 # Nils Maier <maierman@web.de>
29 # Robert Strong <robert.bugzilla@gmail.com>
31 # Alternatively, the contents of this file may be used under the terms of
32 # either the GNU General Public License Version 2 or later (the "GPL"), or
33 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34 # in which case the provisions of the GPL or the LGPL are applicable instead
35 # of those above. If you wish to allow use of your version of this file only
36 # under the terms of either the GPL or the LGPL, and not to allow others to
37 # use your version of this file under the terms of the MPL, indicate your
38 # decision by deleting the provisions above and replace them with the notice
39 # and other provisions required by the GPL or the LGPL. If you do not delete
40 # the provisions above, a recipient may use your version of this file under
41 # the terms of any one of the MPL, the GPL or the LGPL.
43 # ***** END LICENSE BLOCK *****
45 const Ci = Components.interfaces;
46 const Cc = Components.classes;
47 const Cr = Components.results;
48 const Cu = Components.utils;
50 const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
52 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
53 Cu.import("resource://gre/modules/Services.jsm");
55 XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
56 Cu.import("resource://gre/modules/NetUtil.jsm");
60 XPCOMUtils.defineLazyGetter(this, "PlacesUtils", function() {
61 Cu.import("resource://gre/modules/PlacesUtils.jsm");
65 const PREF_EM_NEW_ADDONS_LIST = "extensions.newAddons";
66 const PREF_PLUGINS_NOTIFYUSER = "plugins.update.notifyUser";
67 const PREF_PLUGINS_UPDATEURL = "plugins.update.url";
69 // We try to backup bookmarks at idle times, to avoid doing that at shutdown.
70 // Number of idle seconds before trying to backup bookmarks. 15 minutes.
71 const BOOKMARKS_BACKUP_IDLE_TIME = 15 * 60;
72 // Minimum interval in milliseconds between backups.
73 const BOOKMARKS_BACKUP_INTERVAL = 86400 * 1000;
74 // Maximum number of backups to create. Old ones will be purged.
75 const BOOKMARKS_BACKUP_MAX_BACKUPS = 10;
78 const BrowserGlueServiceFactory = {
80 createInstance: function BGSF_createInstance(outer, iid) {
82 throw Components.results.NS_ERROR_NO_AGGREGATION;
83 return this._instance == null ?
84 this._instance = new BrowserGlue() : this._instance;
90 function BrowserGlue() {
91 XPCOMUtils.defineLazyServiceGetter(this, "_idleService",
92 "@mozilla.org/widget/idleservice;1",
95 XPCOMUtils.defineLazyGetter(this, "_distributionCustomizer", function() {
96 Cu.import("resource:///modules/distribution.js");
97 return new DistributionCustomizer();
100 XPCOMUtils.defineLazyGetter(this, "_sanitizer",
102 let sanitizerScope = {};
103 Services.scriptloader.loadSubScript("chrome://browser/content/sanitize.js", sanitizerScope);
104 return sanitizerScope.Sanitizer;
111 # OS X has the concept of zero-window sessions and therefore ignores the
112 # browser-lastwindow-close-* topics.
113 #define OBSERVE_LASTWINDOW_CLOSE_TOPICS 1
116 BrowserGlue.prototype = {
118 _isIdleObserver: false,
119 _isPlacesInitObserver: false,
120 _isPlacesLockedObserver: false,
121 _isPlacesShutdownObserver: false,
122 _isPlacesDatabaseLocked: false,
124 _setPrefToSaveSession: function BG__setPrefToSaveSession(aForce) {
125 if (!this._saveSession && !aForce)
128 Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
130 // This method can be called via [NSApplication terminate:] on Mac, which
131 // ends up causing prefs not to be flushed to disk, so we need to do that
132 // explicitly here. See bug 497652.
133 Services.prefs.savePrefFile(null);
136 #ifdef MOZ_SERVICES_SYNC
137 _setSyncAutoconnectDelay: function BG__setSyncAutoconnectDelay() {
138 // Assume that a non-zero value for services.sync.autoconnectDelay should override
139 if (Services.prefs.prefHasUserValue("services.sync.autoconnectDelay")) {
140 let prefDelay = Services.prefs.getIntPref("services.sync.autoconnectDelay");
146 // delays are in seconds
147 const MAX_DELAY = 300;
149 let browserEnum = Services.wm.getEnumerator("navigator:browser");
150 while (browserEnum.hasMoreElements()) {
151 delay += browserEnum.getNext().gBrowser.tabs.length;
153 delay = delay <= MAX_DELAY ? delay : MAX_DELAY;
156 Cu.import("resource://services-sync/service.js", syncTemp);
157 syncTemp.Weave.Service.delayedAutoConnect(delay);
161 // nsIObserver implementation
162 observe: function BG_observe(subject, topic, data) {
164 case "xpcom-shutdown":
167 case "prefservice:after-app-defaults":
168 this._onAppDefaults();
170 case "final-ui-startup":
171 this._onProfileStartup();
173 case "browser-delayed-startup-finished":
174 this._onFirstWindowLoaded();
175 Services.obs.removeObserver(this, "browser-delayed-startup-finished");
177 case "sessionstore-windows-restored":
178 this._onBrowserStartup();
180 case "browser:purge-session-history":
181 // reset the console service's error buffer
182 Services.console.logStringMessage(null); // clear the console (in case it's open)
183 Services.console.reset();
185 case "quit-application-requested":
186 this._onQuitRequest(subject, data);
188 case "quit-application-granted":
189 // This pref must be set here because SessionStore will use its value
190 // on quit-application.
191 this._setPrefToSaveSession();
193 #ifdef OBSERVE_LASTWINDOW_CLOSE_TOPICS
194 case "browser-lastwindow-close-requested":
195 // The application is not actually quitting, but the last full browser
196 // window is about to be closed.
197 this._onQuitRequest(subject, "lastwindow");
199 case "browser-lastwindow-close-granted":
200 this._setPrefToSaveSession();
203 #ifdef MOZ_SERVICES_SYNC
204 case "weave:service:ready":
205 this._setSyncAutoconnectDelay();
209 this._setPrefToSaveSession(true);
210 subject.QueryInterface(Ci.nsISupportsPRBool);
213 case "places-init-complete":
215 Services.obs.removeObserver(this, "places-init-complete");
216 this._isPlacesInitObserver = false;
217 // no longer needed, since history was initialized completely.
218 Services.obs.removeObserver(this, "places-database-locked");
219 this._isPlacesLockedObserver = false;
221 // Now apply distribution customized bookmarks.
222 // This should always run after Places initialization.
223 this._distributionCustomizer.applyBookmarks();
225 case "places-database-locked":
226 this._isPlacesDatabaseLocked = true;
227 // Stop observing, so further attempts to load history service
228 // will not show the prompt.
229 Services.obs.removeObserver(this, "places-database-locked");
230 this._isPlacesLockedObserver = false;
232 case "places-shutdown":
233 if (this._isPlacesShutdownObserver) {
234 Services.obs.removeObserver(this, "places-shutdown");
235 this._isPlacesShutdownObserver = false;
237 // places-shutdown is fired when the profile is about to disappear.
238 this._onProfileShutdown();
241 if (this._idleService.idleTime > BOOKMARKS_BACKUP_IDLE_TIME * 1000)
242 this._backupBookmarks();
244 case "distribution-customization-complete":
245 Services.obs.removeObserver(this, "distribution-customization-complete");
246 // Customization has finished, we don't need the customizer anymore.
247 delete this._distributionCustomizer;
249 case "bookmarks-restore-success":
250 case "bookmarks-restore-failed":
251 Services.obs.removeObserver(this, "bookmarks-restore-success");
252 Services.obs.removeObserver(this, "bookmarks-restore-failed");
253 if (topic == "bookmarks-restore-success" && data == "html-initial")
254 this.ensurePlacesDefaultQueriesInitialized();
256 case "browser-glue-test": // used by tests
257 if (data == "post-update-notification") {
258 if (Services.prefs.prefHasUserValue("app.update.postupdate"))
259 this._showUpdateNotification();
261 else if (data == "force-ui-migration") {
268 // initialization (called on application startup)
269 _init: function BG__init() {
270 let os = Services.obs;
271 os.addObserver(this, "xpcom-shutdown", false);
272 os.addObserver(this, "prefservice:after-app-defaults", false);
273 os.addObserver(this, "final-ui-startup", false);
274 os.addObserver(this, "browser-delayed-startup-finished", false);
275 os.addObserver(this, "sessionstore-windows-restored", false);
276 os.addObserver(this, "browser:purge-session-history", false);
277 os.addObserver(this, "quit-application-requested", false);
278 os.addObserver(this, "quit-application-granted", false);
279 #ifdef OBSERVE_LASTWINDOW_CLOSE_TOPICS
280 os.addObserver(this, "browser-lastwindow-close-requested", false);
281 os.addObserver(this, "browser-lastwindow-close-granted", false);
283 #ifdef MOZ_SERVICES_SYNC
284 os.addObserver(this, "weave:service:ready", false);
286 os.addObserver(this, "session-save", false);
287 os.addObserver(this, "places-init-complete", false);
288 this._isPlacesInitObserver = true;
289 os.addObserver(this, "places-database-locked", false);
290 this._isPlacesLockedObserver = true;
291 os.addObserver(this, "distribution-customization-complete", false);
292 os.addObserver(this, "places-shutdown", false);
293 this._isPlacesShutdownObserver = true;
296 // cleanup (called on application shutdown)
297 _dispose: function BG__dispose() {
298 let os = Services.obs;
299 os.removeObserver(this, "xpcom-shutdown");
300 os.removeObserver(this, "prefservice:after-app-defaults");
301 os.removeObserver(this, "final-ui-startup");
302 os.removeObserver(this, "sessionstore-windows-restored");
303 os.removeObserver(this, "browser:purge-session-history");
304 os.removeObserver(this, "quit-application-requested");
305 os.removeObserver(this, "quit-application-granted");
306 #ifdef OBSERVE_LASTWINDOW_CLOSE_TOPICS
307 os.removeObserver(this, "browser-lastwindow-close-requested");
308 os.removeObserver(this, "browser-lastwindow-close-granted");
310 #ifdef MOZ_SERVICES_SYNC
311 os.removeObserver(this, "weave:service:ready", false);
313 os.removeObserver(this, "session-save");
314 if (this._isIdleObserver)
315 this._idleService.removeIdleObserver(this, BOOKMARKS_BACKUP_IDLE_TIME);
316 if (this._isPlacesInitObserver)
317 os.removeObserver(this, "places-init-complete");
318 if (this._isPlacesLockedObserver)
319 os.removeObserver(this, "places-database-locked");
320 if (this._isPlacesShutdownObserver)
321 os.removeObserver(this, "places-shutdown");
324 _onAppDefaults: function BG__onAppDefaults() {
325 // apply distribution customizations (prefs)
326 // other customizations are applied in _onProfileStartup()
327 this._distributionCustomizer.applyPrefDefaults();
330 // profile startup handler (contains profile initialization routines)
331 _onProfileStartup: function BG__onProfileStartup() {
332 this._sanitizer.onStartup();
333 // check if we're in safe mode
334 if (Services.appinfo.inSafeMode) {
335 Services.ww.openWindow(null, "chrome://browser/content/safeMode.xul",
336 "_blank", "chrome,centerscreen,modal,resizable=no", null);
339 // apply distribution customizations
340 // prefs are applied in _onAppDefaults()
341 this._distributionCustomizer.applyCustomizations();
343 // handle any UI migration
346 // if ioService is managing the offline status, then ioservice.offline
347 // is already set correctly. We will continue to allow the ioService
348 // to manage its offline state until the user uses the "Work Offline" UI.
349 if (!Services.io.manageOfflineStatus) {
350 // set the initial state
352 Services.io.offline = Services.prefs.getBoolPref("browser.offline");
355 Services.io.offline = false;
359 Services.obs.notifyObservers(null, "browser-ui-startup-complete", "");
362 // the first browser window has finished initializing
363 _onFirstWindowLoaded: function BG__onFirstWindowLoaded() {
366 // For windows seven, initialize the jump list module.
367 const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1";
368 if (WINTASKBAR_CONTRACTID in Cc &&
369 Cc[WINTASKBAR_CONTRACTID].getService(Ci.nsIWinTaskbar).available) {
371 Cu.import("resource://gre/modules/WindowsJumpLists.jsm", temp);
372 temp.WinTaskbarJumpList.startup();
378 // profile shutdown handler (contains profile cleanup routines)
379 _onProfileShutdown: function BG__onProfileShutdown() {
382 // If there's a pending update, clear cache to free up disk space.
384 let um = Cc["@mozilla.org/updates/update-manager;1"].
385 getService(Ci.nsIUpdateManager);
386 if (um.activeUpdate && um.activeUpdate.state == "pending") {
387 let cacheService = Cc["@mozilla.org/network/cache-service;1"].
388 getService(Ci.nsICacheService);
389 cacheService.evictEntries(Ci.nsICache.STORE_ANYWHERE);
394 this._shutdownPlaces();
395 this._sanitizer.onShutdown();
398 // Browser startup complete. All initial windows have opened.
399 _onBrowserStartup: function BG__onBrowserStartup() {
400 // Show about:rights notification, if needed.
401 if (this._shouldShowRights())
402 this._showRightsNotification();
404 // Show update notification, if needed.
405 if (Services.prefs.prefHasUserValue("app.update.postupdate"))
406 this._showUpdateNotification();
408 // If new add-ons were installed during startup open the add-ons manager.
409 if (Services.prefs.prefHasUserValue(PREF_EM_NEW_ADDONS_LIST)) {
410 var args = Cc["@mozilla.org/supports-array;1"].
411 createInstance(Ci.nsISupportsArray);
412 var str = Cc["@mozilla.org/supports-string;1"].
413 createInstance(Ci.nsISupportsString);
415 args.AppendElement(str);
416 var str = Cc["@mozilla.org/supports-string;1"].
417 createInstance(Ci.nsISupportsString);
418 str.data = Services.prefs.getCharPref(PREF_EM_NEW_ADDONS_LIST);
419 args.AppendElement(str);
420 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
421 const EMFEATURES = "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable";
422 Services.ww.openWindow(null, EMURL, "_blank", EMFEATURES, args);
423 Services.prefs.clearUserPref(PREF_EM_NEW_ADDONS_LIST);
426 // Load the "more info" page for a locked places.sqlite
427 // This property is set earlier by places-database-locked topic.
428 if (this._isPlacesDatabaseLocked) {
429 this._showPlacesLockedNotificationBox();
432 // If there are plugins installed that are outdated, and the user hasn't
433 // been warned about them yet, open the plugins update page.
434 if (Services.prefs.getBoolPref(PREF_PLUGINS_NOTIFYUSER))
435 this._showPluginUpdatePage();
438 _onQuitRequest: function BG__onQuitRequest(aCancelQuit, aQuitType) {
439 // If user has already dismissed quit request, then do nothing
440 if ((aCancelQuit instanceof Ci.nsISupportsPRBool) && aCancelQuit.data)
443 // There are several cases where we won't show a dialog here:
444 // 1. There is only 1 tab open in 1 window
445 // 2. The session will be restored at startup, indicated by
446 // browser.startup.page == 3 or browser.sessionstore.resume_session_once == true
447 // 3. browser.warnOnQuit == false
448 // 4. The browser is currently in Private Browsing mode
450 // Otherwise these are the conditions and the associated dialogs that will be shown:
451 // 1. aQuitType == "lastwindow" or "quit" and browser.showQuitWarning == true
452 // - The quit dialog will be shown
453 // 2. aQuitType == "restart" && browser.warnOnRestart == true
454 // - The restart dialog will be shown
455 // 3. aQuitType == "lastwindow" && browser.tabs.warnOnClose == true
456 // - The "closing multiple tabs" dialog will be shown
458 // aQuitType == "lastwindow" is overloaded. "lastwindow" is used to indicate
459 // "the last window is closing but we're not quitting (a non-browser window is open)"
460 // and also "we're quitting by closing the last window".
464 var browserEnum = Services.wm.getEnumerator("navigator:browser");
465 while (browserEnum.hasMoreElements()) {
468 var browser = browserEnum.getNext();
469 var tabbrowser = browser.document.getElementById("content");
471 pagecount += tabbrowser.browsers.length - tabbrowser._numPinnedTabs;
474 this._saveSession = false;
481 // Never show a prompt inside private browsing mode
482 var inPrivateBrowsing = Cc["@mozilla.org/privatebrowsing;1"].
483 getService(Ci.nsIPrivateBrowsingService).
484 privateBrowsingEnabled;
485 if (inPrivateBrowsing)
488 var showPrompt = false;
489 var mostRecentBrowserWindow;
491 // browser.warnOnQuit is a hidden global boolean to override all quit prompts
492 // browser.showQuitWarning specifically covers quitting
493 // browser.warnOnRestart specifically covers app-initiated restarts where we restart the app
494 // browser.tabs.warnOnClose is the global "warn when closing multiple tabs" pref
496 var sessionWillBeRestored = Services.prefs.getIntPref("browser.startup.page") == 3 ||
497 Services.prefs.getBoolPref("browser.sessionstore.resume_session_once");
498 if (sessionWillBeRestored || !Services.prefs.getBoolPref("browser.warnOnQuit"))
501 // On last window close or quit && showQuitWarning, we want to show the
503 if (aQuitType != "restart" && Services.prefs.getBoolPref("browser.showQuitWarning")) {
506 else if (aQuitType == "restart" && Services.prefs.getBoolPref("browser.warnOnRestart")) {
509 else if (aQuitType == "lastwindow") {
510 // If aQuitType is "lastwindow" and we aren't showing the quit warning,
511 // we should show the window closing warning instead. warnAboutClosing
512 // tabs checks browser.tabs.warnOnClose and returns if it's ok to close
513 // the window. It doesn't actually close the window.
514 mostRecentBrowserWindow = Services.wm.getMostRecentWindow("navigator:browser");
515 aCancelQuit.data = !mostRecentBrowserWindow.gBrowser.warnAboutClosingTabs(true);
522 var quitBundle = Services.strings.createBundle("chrome://browser/locale/quitDialog.properties");
523 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
525 var appName = brandBundle.GetStringFromName("brandShortName");
526 var quitTitleString = (aQuitType == "restart" ? "restart" : "quit") + "DialogTitle";
527 var quitDialogTitle = quitBundle.formatStringFromName(quitTitleString, [appName], 1);
530 if (aQuitType == "restart")
531 message = quitBundle.formatStringFromName("messageRestart",
533 else if (windowcount == 1)
534 message = quitBundle.formatStringFromName("messageNoWindows",
537 message = quitBundle.formatStringFromName("message",
540 var promptService = Services.prompt;
542 var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
543 promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
544 promptService.BUTTON_POS_0_DEFAULT;
546 var neverAsk = {value:false};
547 var button0Title, button2Title;
548 var button1Title = quitBundle.GetStringFromName("cancelTitle");
549 var neverAskText = quitBundle.GetStringFromName("neverAsk");
551 if (aQuitType == "restart")
552 button0Title = quitBundle.GetStringFromName("restartTitle");
554 flags += promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_2;
555 button0Title = quitBundle.GetStringFromName("saveTitle");
556 button2Title = quitBundle.GetStringFromName("quitTitle");
559 // This wouldn't have been set above since we shouldn't be here for
560 // aQuitType == "lastwindow"
561 mostRecentBrowserWindow = Services.wm.getMostRecentWindow("navigator:browser");
564 promptService.confirmEx(mostRecentBrowserWindow, quitDialogTitle, message,
565 flags, button0Title, button1Title, button2Title,
566 neverAskText, neverAsk);
568 switch (buttonChoice) {
571 Services.prefs.setBoolPref("browser.showQuitWarning", false);
574 aCancelQuit.QueryInterface(Ci.nsISupportsPRBool);
575 aCancelQuit.data = true;
577 case 0: // Save & Quit
578 this._saveSession = true;
579 if (neverAsk.value) {
580 if (aQuitType == "restart")
581 Services.prefs.setBoolPref("browser.warnOnRestart", false);
583 // always save state when shutting down
584 Services.prefs.setIntPref("browser.startup.page", 3);
592 * _shouldShowRights - Determines if the user should be shown the
593 * about:rights notification. The notification should *not* be shown if
594 * we've already shown the current version, or if the override pref says to
595 * never show it. The notification *should* be shown if it's never been seen
596 * before, if a newer version is available, or if the override pref says to
599 _shouldShowRights: function BG__shouldShowRights() {
600 // Look for an unconditional override pref. If set, do what it says.
601 // (true --> never show, false --> always show)
603 return !Services.prefs.getBoolPref("browser.rights.override");
605 // Ditto, for the legacy EULA pref.
607 return !Services.prefs.getBoolPref("browser.EULA.override");
610 #ifndef OFFICIAL_BUILD
611 // Non-official builds shouldn't shouldn't show the notification.
615 // Look to see if the user has seen the current version or not.
616 var currentVersion = Services.prefs.getIntPref("browser.rights.version");
618 return !Services.prefs.getBoolPref("browser.rights." + currentVersion + ".shown");
621 // Legacy: If the user accepted a EULA, we won't annoy them with the
622 // equivalent about:rights page until the version changes.
624 return !Services.prefs.getBoolPref("browser.EULA." + currentVersion + ".accepted");
627 // We haven't shown the notification before, so do so now.
631 _showRightsNotification: function BG__showRightsNotification() {
632 // Stick the notification onto the selected tab of the active browser window.
633 var win = this.getMostRecentBrowserWindow();
634 var browser = win.gBrowser; // for closure in notification bar callback
635 var notifyBox = browser.getNotificationBox();
637 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
638 var rightsBundle = Services.strings.createBundle("chrome://global/locale/aboutRights.properties");
640 var buttonLabel = rightsBundle.GetStringFromName("buttonLabel");
641 var buttonAccessKey = rightsBundle.GetStringFromName("buttonAccessKey");
642 var productName = brandBundle.GetStringFromName("brandFullName");
643 var notifyRightsText = rightsBundle.formatStringFromName("notifyRightsText", [productName], 1);
648 accessKey: buttonAccessKey,
650 callback: function(aNotificationBar, aButton) {
651 browser.selectedTab = browser.addTab("about:rights");
656 // Set pref to indicate we've shown the notification.
657 var currentVersion = Services.prefs.getIntPref("browser.rights.version");
658 Services.prefs.setBoolPref("browser.rights." + currentVersion + ".shown", true);
660 var box = notifyBox.appendNotification(notifyRightsText, "about-rights", null, notifyBox.PRIORITY_INFO_LOW, buttons);
661 box.persistence = 3; // arbitrary number, just so bar sticks around for a bit
664 _showUpdateNotification: function BG__showUpdateNotification() {
665 Services.prefs.clearUserPref("app.update.postupdate");
667 var um = Cc["@mozilla.org/updates/update-manager;1"].
668 getService(Ci.nsIUpdateManager);
670 // If the updates.xml file is deleted then getUpdateAt will throw.
671 var update = um.getUpdateAt(0).QueryInterface(Ci.nsIPropertyBag);
674 // This should never happen.
675 Cu.reportError("Unable to find update: " + e);
679 var actions = update.getProperty("actions");
680 if (!actions || actions.indexOf("silent") != -1)
683 var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
684 getService(Ci.nsIURLFormatter);
685 var browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
686 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
687 var appName = brandBundle.GetStringFromName("brandShortName");
689 function getNotifyString(aPropData) {
690 var propValue = update.getProperty(aPropData.propName);
692 if (aPropData.prefName)
693 propValue = formatter.formatURLPref(aPropData.prefName);
694 else if (aPropData.stringParams)
695 propValue = browserBundle.formatStringFromName(aPropData.stringName,
696 aPropData.stringParams,
697 aPropData.stringParams.length);
699 propValue = browserBundle.GetStringFromName(aPropData.stringName);
704 if (actions.indexOf("showNotification") != -1) {
705 let text = getNotifyString({propName: "notificationText",
706 stringName: "puNotifyText",
707 stringParams: [appName]});
708 let url = getNotifyString({propName: "notificationURL",
709 prefName: "startup.homepage_override_url"});
710 let label = getNotifyString({propName: "notificationButtonLabel",
711 stringName: "pu.notifyButton.label"});
712 let key = getNotifyString({propName: "notificationButtonAccessKey",
713 stringName: "pu.notifyButton.accesskey"});
715 let win = this.getMostRecentBrowserWindow();
716 let browser = win.gBrowser; // for closure in notification bar callback
717 let notifyBox = browser.getNotificationBox();
724 callback: function(aNotificationBar, aButton) {
725 browser.selectedTab = browser.addTab(url);
730 let box = notifyBox.appendNotification(text, "post-update-notification",
731 null, notifyBox.PRIORITY_INFO_LOW,
736 if (actions.indexOf("showAlert") == -1)
741 notifier = Cc["@mozilla.org/alerts-service;1"].
742 getService(Ci.nsIAlertsService);
745 // nsIAlertsService is not available for this platform
749 let title = getNotifyString({propName: "alertTitle",
750 stringName: "puAlertTitle",
751 stringParams: [appName]});
752 let text = getNotifyString({propName: "alertText",
753 stringName: "puAlertText",
754 stringParams: [appName]});
755 let url = getNotifyString({propName: "alertURL",
756 prefName: "startup.homepage_override_url"});
759 function clickCallback(subject, topic, data) {
760 // This callback will be called twice but only once with this topic
761 if (topic != "alertclickcallback")
763 let win = self.getMostRecentBrowserWindow();
764 let browser = win.gBrowser;
765 browser.selectedTab = browser.addTab(data);
769 // This will throw NS_ERROR_NOT_AVAILABLE if the notification cannot
770 // be displayed per the idl.
771 notifier.showAlertNotification("post-update-notification", title, text,
772 true, url, clickCallback);
778 _showPluginUpdatePage: function BG__showPluginUpdatePage() {
779 Services.prefs.setBoolPref(PREF_PLUGINS_NOTIFYUSER, false);
781 var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
782 getService(Ci.nsIURLFormatter);
783 var updateUrl = formatter.formatURLPref(PREF_PLUGINS_UPDATEURL);
785 var win = this.getMostRecentBrowserWindow();
786 var browser = win.gBrowser;
787 browser.selectedTab = browser.addTab(updateUrl);
792 * - imports the bookmarks html file if bookmarks database is empty, try to
793 * restore bookmarks from a JSON backup if the backend indicates that the
794 * database was corrupt.
796 * These prefs can be set up by the frontend:
798 * WARNING: setting these preferences to true will overwite existing bookmarks
800 * - browser.places.importBookmarksHTML
801 * Set to true will import the bookmarks.html file from the profile folder.
802 * - browser.places.smartBookmarksVersion
803 * Set during HTML import to indicate that Smart Bookmarks were created.
804 * Set to -1 to disable Smart Bookmarks creation.
805 * Set to 0 to restore current Smart Bookmarks.
806 * - browser.bookmarks.restore_default_bookmarks
807 * Set to true by safe-mode dialog to indicate we must restore default
810 _initPlaces: function BG__initPlaces() {
811 // We must instantiate the history service since it will tell us if we
812 // need to import or restore bookmarks due to first-run, corruption or
813 // forced migration (due to a major schema change).
814 // If the database is corrupt or has been newly created we should
816 var dbStatus = PlacesUtils.history.databaseStatus;
817 var importBookmarks = dbStatus == PlacesUtils.history.DATABASE_STATUS_CREATE ||
818 dbStatus == PlacesUtils.history.DATABASE_STATUS_CORRUPT;
820 if (dbStatus == PlacesUtils.history.DATABASE_STATUS_CREATE) {
821 // If the database has just been created, but we already have any
822 // bookmark, this is not the initial import. This can happen after a
823 // migration from a different browser since migrators run before us.
824 // In such a case we should not import, unless some pref has been set.
825 if (PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0) != -1 ||
826 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0) != -1)
827 importBookmarks = false;
830 // Check if user or an extension has required to import bookmarks.html
831 var importBookmarksHTML = false;
833 importBookmarksHTML =
834 Services.prefs.getBoolPref("browser.places.importBookmarksHTML");
835 if (importBookmarksHTML)
836 importBookmarks = true;
839 // Check if Safe Mode or the user has required to restore bookmarks from
840 // default profile's bookmarks.html
841 var restoreDefaultBookmarks = false;
843 restoreDefaultBookmarks =
844 Services.prefs.getBoolPref("browser.bookmarks.restore_default_bookmarks");
845 if (restoreDefaultBookmarks) {
846 // Ensure that we already have a bookmarks backup for today.
847 this._backupBookmarks();
848 importBookmarks = true;
852 // If the user did not require to restore default bookmarks, or import
853 // from bookmarks.html, we will try to restore from JSON
854 if (importBookmarks && !restoreDefaultBookmarks && !importBookmarksHTML) {
855 // get latest JSON backup
856 var bookmarksBackupFile = PlacesUtils.backups.getMostRecent("json");
857 if (bookmarksBackupFile) {
858 // restore from JSON backup
859 PlacesUtils.restoreBookmarksFromJSONFile(bookmarksBackupFile);
860 importBookmarks = false;
863 // We have created a new database but we don't have any backup available
864 importBookmarks = true;
865 var dirService = Cc["@mozilla.org/file/directory_service;1"].
866 getService(Ci.nsIProperties);
867 var bookmarksHTMLFile = dirService.get("BMarks", Ci.nsILocalFile);
868 if (bookmarksHTMLFile.exists()) {
869 // If bookmarks.html is available in current profile import it...
870 importBookmarksHTML = true;
873 // ...otherwise we will restore defaults
874 restoreDefaultBookmarks = true;
879 // If bookmarks are not imported, then initialize smart bookmarks. This
880 // happens during a common startup.
881 // Otherwise, if any kind of import runs, smart bookmarks creation should be
882 // delayed till the import operations has finished. Not doing so would
883 // cause them to be overwritten by the newly imported bookmarks.
884 if (!importBookmarks) {
885 this.ensurePlacesDefaultQueriesInitialized();
888 // An import operation is about to run.
889 // Don't try to recreate smart bookmarks if autoExportHTML is true or
890 // smart bookmarks are disabled.
891 var autoExportHTML = false;
893 autoExportHTML = Services.prefs.getBoolPref("browser.bookmarks.autoExportHTML");
895 var smartBookmarksVersion = 0;
897 smartBookmarksVersion = Services.prefs.getIntPref("browser.places.smartBookmarksVersion");
899 if (!autoExportHTML && smartBookmarksVersion != -1)
900 Services.prefs.setIntPref("browser.places.smartBookmarksVersion", 0);
902 // Get bookmarks.html file location
903 var dirService = Cc["@mozilla.org/file/directory_service;1"].
904 getService(Ci.nsIProperties);
906 var bookmarksURI = null;
907 if (restoreDefaultBookmarks) {
908 // User wants to restore bookmarks.html file from default profile folder
909 bookmarksURI = NetUtil.newURI("resource:///defaults/profile/bookmarks.html");
912 var bookmarksFile = dirService.get("BMarks", Ci.nsILocalFile);
913 if (bookmarksFile.exists())
914 bookmarksURI = NetUtil.newURI(bookmarksFile);
918 // Add an import observer. It will ensure that smart bookmarks are
919 // created once the operation is complete.
920 Services.obs.addObserver(this, "bookmarks-restore-success", false);
921 Services.obs.addObserver(this, "bookmarks-restore-failed", false);
923 // Import from bookmarks.html file.
925 var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
926 getService(Ci.nsIPlacesImportExportService);
927 importer.importHTMLFromURI(bookmarksURI, true /* overwrite existing */);
929 // Report the error, but ignore it.
930 Cu.reportError("Bookmarks.html file could be corrupt. " + err);
931 Services.obs.removeObserver(this, "bookmarks-restore-success");
932 Services.obs.removeObserver(this, "bookmarks-restore-failed");
936 Cu.reportError("Unable to find bookmarks.html file.");
938 // Reset preferences, so we won't try to import again at next run
939 if (importBookmarksHTML)
940 Services.prefs.setBoolPref("browser.places.importBookmarksHTML", false);
941 if (restoreDefaultBookmarks)
942 Services.prefs.setBoolPref("browser.bookmarks.restore_default_bookmarks",
946 // Initialize bookmark archiving on idle.
947 // Once a day, either on idle or shutdown, bookmarks are backed up.
948 if (!this._isIdleObserver) {
949 this._idleService.addIdleObserver(this, BOOKMARKS_BACKUP_IDLE_TIME);
950 this._isIdleObserver = true;
955 * Places shut-down tasks
956 * - back up bookmarks if needed.
957 * - export bookmarks as HTML, if so configured.
959 * Note: quit-application-granted notification is received twice
960 * so replace this method with a no-op when first called.
962 _shutdownPlaces: function BG__shutdownPlaces() {
963 if (this._isIdleObserver) {
964 this._idleService.removeIdleObserver(this, BOOKMARKS_BACKUP_IDLE_TIME);
965 this._isIdleObserver = false;
967 this._backupBookmarks();
969 // Backup bookmarks to bookmarks.html to support apps that depend
970 // on the legacy format.
971 var autoExportHTML = false;
973 autoExportHTML = Services.prefs.getBoolPref("browser.bookmarks.autoExportHTML");
974 } catch(ex) { /* Don't export */ }
976 if (autoExportHTML) {
977 Cc["@mozilla.org/browser/places/import-export-service;1"].
978 getService(Ci.nsIPlacesImportExportService).
979 backupBookmarksFile();
984 * Backup bookmarks if needed.
986 _backupBookmarks: function BG__backupBookmarks() {
987 let lastBackupFile = PlacesUtils.backups.getMostRecent();
989 // Backup bookmarks if there are no backups or the maximum interval between
991 if (!lastBackupFile ||
992 new Date() - PlacesUtils.backups.getDateForFile(lastBackupFile) > BOOKMARKS_BACKUP_INTERVAL) {
993 let maxBackups = BOOKMARKS_BACKUP_MAX_BACKUPS;
995 maxBackups = Services.prefs.getIntPref("browser.bookmarks.max_backups");
997 catch(ex) { /* Use default. */ }
999 PlacesUtils.backups.create(maxBackups); // Don't force creation.
1004 * Show the notificationBox for a locked places database.
1006 _showPlacesLockedNotificationBox: function BG__showPlacesLockedNotificationBox() {
1007 var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
1008 var applicationName = brandBundle.GetStringFromName("brandShortName");
1009 var placesBundle = Services.strings.createBundle("chrome://browser/locale/places/places.properties");
1010 var title = placesBundle.GetStringFromName("lockPrompt.title");
1011 var text = placesBundle.formatStringFromName("lockPrompt.text", [applicationName], 1);
1012 var buttonText = placesBundle.GetStringFromName("lockPromptInfoButton.label");
1013 var accessKey = placesBundle.GetStringFromName("lockPromptInfoButton.accessKey");
1015 var helpTopic = "places-locked";
1016 var url = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
1017 getService(Components.interfaces.nsIURLFormatter).
1018 formatURLPref("app.support.baseURL");
1021 var browser = this.getMostRecentBrowserWindow().gBrowser;
1026 accessKey: accessKey,
1028 callback: function(aNotificationBar, aButton) {
1029 browser.selectedTab = browser.addTab(url);
1034 var notifyBox = browser.getNotificationBox();
1035 var box = notifyBox.appendNotification(text, title, null,
1036 notifyBox.PRIORITY_CRITICAL_MEDIUM,
1038 box.persistence = -1; // Until user closes it
1041 _migrateUI: function BG__migrateUI() {
1042 const UI_VERSION = 5;
1043 const BROWSER_DOCURL = "chrome://browser/content/browser.xul#";
1044 let currentUIVersion = 0;
1046 currentUIVersion = Services.prefs.getIntPref("browser.migration.version");
1048 if (currentUIVersion >= UI_VERSION)
1051 this._rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
1052 this._dataSource = this._rdf.GetDataSource("rdf:local-store");
1053 this._dirty = false;
1055 if (currentUIVersion < 1) {
1056 // this code should always migrate pre-FF3 profiles to the current UI state
1057 let currentsetResource = this._rdf.GetResource("currentset");
1058 let toolbars = ["nav-bar", "toolbar-menubar", "PersonalToolbar"];
1059 for (let i = 0; i < toolbars.length; i++) {
1060 let toolbar = this._rdf.GetResource(BROWSER_DOCURL + toolbars[i]);
1061 let currentset = this._getPersist(toolbar, currentsetResource);
1063 // toolbar isn't customized
1065 // new button is in the defaultset, nothing to migrate
1069 if (/(?:^|,)unified-back-forward-button(?:$|,)/.test(currentset))
1070 // new button is already there, nothing to migrate
1072 if (/(?:^|,)back-button(?:$|,)/.test(currentset)) {
1073 let newset = currentset.replace(/(^|,)back-button($|,)/,
1074 "$1unified-back-forward-button,back-button$2")
1075 this._setPersist(toolbar, currentsetResource, newset);
1082 if (currentUIVersion < 2) {
1083 // This code adds the customizable bookmarks button.
1084 let currentsetResource = this._rdf.GetResource("currentset");
1085 let toolbarResource = this._rdf.GetResource(BROWSER_DOCURL + "nav-bar");
1086 let currentset = this._getPersist(toolbarResource, currentsetResource);
1087 // Need to migrate only if toolbar is customized and the element is not found.
1089 currentset.indexOf("bookmarks-menu-button-container") == -1) {
1090 if (currentset.indexOf("fullscreenflex") != -1) {
1091 currentset = currentset.replace(/(^|,)fullscreenflex($|,)/,
1092 "$1bookmarks-menu-button-container,fullscreenflex$2")
1095 currentset += ",bookmarks-menu-button-container";
1097 this._setPersist(toolbarResource, currentsetResource, currentset);
1101 if (currentUIVersion < 3) {
1102 // This code merges the reload/stop/go button into the url bar.
1103 let currentsetResource = this._rdf.GetResource("currentset");
1104 let toolbarResource = this._rdf.GetResource(BROWSER_DOCURL + "nav-bar");
1105 let currentset = this._getPersist(toolbarResource, currentsetResource);
1106 // Need to migrate only if toolbar is customized and all 3 elements are found.
1108 currentset.indexOf("reload-button") != -1 &&
1109 currentset.indexOf("stop-button") != -1 &&
1110 currentset.indexOf("urlbar-container") != -1 &&
1111 currentset.indexOf("urlbar-container,reload-button,stop-button") == -1) {
1112 currentset = currentset.replace(/(^|,)reload-button($|,)/, "$1$2")
1113 .replace(/(^|,)stop-button($|,)/, "$1$2")
1114 .replace(/(^|,)urlbar-container($|,)/,
1115 "$1urlbar-container,reload-button,stop-button$2");
1116 this._setPersist(toolbarResource, currentsetResource, currentset);
1120 if (currentUIVersion < 4) {
1121 // This code moves the home button to the immediate left of the bookmarks menu button.
1122 let currentsetResource = this._rdf.GetResource("currentset");
1123 let toolbarResource = this._rdf.GetResource(BROWSER_DOCURL + "nav-bar");
1124 let currentset = this._getPersist(toolbarResource, currentsetResource);
1125 // Need to migrate only if toolbar is customized and the elements are found.
1127 currentset.indexOf("home-button") != -1 &&
1128 currentset.indexOf("bookmarks-menu-button-container") != -1) {
1129 currentset = currentset.replace(/(^|,)home-button($|,)/, "$1$2")
1130 .replace(/(^|,)bookmarks-menu-button-container($|,)/,
1131 "$1home-button,bookmarks-menu-button-container$2");
1132 this._setPersist(toolbarResource, currentsetResource, currentset);
1136 if (currentUIVersion < 5) {
1137 // This code uncollapses PersonalToolbar if its collapsed status is not
1138 // persisted, and user customized it or changed default bookmarks.
1139 let toolbarResource = this._rdf.GetResource(BROWSER_DOCURL + "PersonalToolbar");
1140 let collapsedResource = this._rdf.GetResource("collapsed");
1141 let collapsed = this._getPersist(toolbarResource, collapsedResource);
1142 // If the user does not have a persisted value for the toolbar's
1143 // "collapsed" attribute, try to determine whether it's customized.
1144 if (collapsed === null) {
1145 // We consider the toolbar customized if it has more than
1146 // 3 children, or if it has a persisted currentset value.
1147 let currentsetResource = this._rdf.GetResource("currentset");
1148 let toolbarIsCustomized = !!this._getPersist(toolbarResource,
1149 currentsetResource);
1150 function getToolbarFolderCount() {
1152 PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId).root;
1153 let toolbarChildCount = toolbarFolder.childCount;
1154 toolbarFolder.containerOpen = false;
1155 return toolbarChildCount;
1158 if (toolbarIsCustomized || getToolbarFolderCount() > 3) {
1159 this._setPersist(toolbarResource, collapsedResource, "false");
1165 this._dataSource.QueryInterface(Ci.nsIRDFRemoteDataSource).Flush();
1168 delete this._dataSource;
1170 // Update the migration version.
1171 Services.prefs.setIntPref("browser.migration.version", UI_VERSION);
1174 _getPersist: function BG__getPersist(aSource, aProperty) {
1175 var target = this._dataSource.GetTarget(aSource, aProperty, true);
1176 if (target instanceof Ci.nsIRDFLiteral)
1177 return target.Value;
1181 _setPersist: function BG__setPersist(aSource, aProperty, aTarget) {
1184 var oldTarget = this._dataSource.GetTarget(aSource, aProperty, true);
1187 this._dataSource.Change(aSource, aProperty, oldTarget, this._rdf.GetLiteral(aTarget));
1189 this._dataSource.Unassert(aSource, aProperty, oldTarget);
1192 this._dataSource.Assert(aSource, aProperty, this._rdf.GetLiteral(aTarget), true);
1195 // Add the entry to the persisted set for this document if it's not there.
1196 // This code is mostly borrowed from nsXULDocument::Persist.
1197 let docURL = aSource.ValueUTF8.split("#")[0];
1198 let docResource = this._rdf.GetResource(docURL);
1199 let persistResource = this._rdf.GetResource("http://home.netscape.com/NC-rdf#persist");
1200 if (!this._dataSource.HasAssertion(docResource, persistResource, aSource, true)) {
1201 this._dataSource.Assert(docResource, persistResource, aSource, true);
1207 // ------------------------------
1208 // public nsIBrowserGlue members
1209 // ------------------------------
1211 sanitize: function BG_sanitize(aParentWindow) {
1212 this._sanitizer.sanitize(aParentWindow);
1215 ensurePlacesDefaultQueriesInitialized:
1216 function BG_ensurePlacesDefaultQueriesInitialized() {
1217 // This is actual version of the smart bookmarks, must be increased every
1218 // time smart bookmarks change.
1219 // When adding a new smart bookmark below, its newInVersion property must
1220 // be set to the version it has been added in, we will compare its value
1221 // to users' smartBookmarksVersion and add new smart bookmarks without
1222 // recreating old deleted ones.
1223 const SMART_BOOKMARKS_VERSION = 2;
1224 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
1225 const SMART_BOOKMARKS_PREF = "browser.places.smartBookmarksVersion";
1227 // TODO bug 399268: should this be a pref?
1228 const MAX_RESULTS = 10;
1230 // Get current smart bookmarks version. If not set, create them.
1231 let smartBookmarksCurrentVersion = 0;
1233 smartBookmarksCurrentVersion = Services.prefs.getIntPref(SMART_BOOKMARKS_PREF);
1236 // If version is current or smart bookmarks are disabled, just bail out.
1237 if (smartBookmarksCurrentVersion == -1 ||
1238 smartBookmarksCurrentVersion >= SMART_BOOKMARKS_VERSION) {
1243 runBatched: function BG_EPDQI_runBatched() {
1245 let toolbarIndex = 0;
1246 let bundle = Services.strings.createBundle("chrome://browser/locale/places/places.properties");
1248 let smartBookmarks = {
1250 title: bundle.GetStringFromName("mostVisitedTitle"),
1251 uri: NetUtil.newURI("place:redirectsMode=" +
1252 Ci.nsINavHistoryQueryOptions.REDIRECTS_MODE_TARGET +
1254 Ci.nsINavHistoryQueryOptions.SORT_BY_VISITCOUNT_DESCENDING +
1255 "&maxResults=" + MAX_RESULTS),
1256 parent: PlacesUtils.toolbarFolderId,
1257 position: toolbarIndex++,
1260 RecentlyBookmarked: {
1261 title: bundle.GetStringFromName("recentlyBookmarkedTitle"),
1262 uri: NetUtil.newURI("place:folder=BOOKMARKS_MENU" +
1263 "&folder=UNFILED_BOOKMARKS" +
1266 Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS +
1268 Ci.nsINavHistoryQueryOptions.SORT_BY_DATEADDED_DESCENDING +
1269 "&excludeItemIfParentHasAnnotation=livemark%2FfeedURI" +
1270 "&maxResults=" + MAX_RESULTS +
1271 "&excludeQueries=1"),
1272 parent: PlacesUtils.bookmarksMenuFolderId,
1273 position: menuIndex++,
1277 title: bundle.GetStringFromName("recentTagsTitle"),
1278 uri: NetUtil.newURI("place:"+
1280 Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_QUERY +
1282 Ci.nsINavHistoryQueryOptions.SORT_BY_LASTMODIFIED_DESCENDING +
1283 "&maxResults=" + MAX_RESULTS),
1284 parent: PlacesUtils.bookmarksMenuFolderId,
1285 position: menuIndex++,
1290 // Set current itemId, parent and position if Smart Bookmark exists,
1291 // we will use these informations to create the new version at the same
1293 let smartBookmarkItemIds = PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
1294 smartBookmarkItemIds.forEach(function (itemId) {
1295 let queryId = PlacesUtils.annotations.getItemAnnotation(itemId, SMART_BOOKMARKS_ANNO);
1296 if (queryId in smartBookmarks) {
1297 let smartBookmark = smartBookmarks[queryId];
1298 smartBookmarks[queryId].itemId = itemId;
1299 smartBookmarks[queryId].parent = PlacesUtils.bookmarks.getFolderIdForItem(itemId);
1300 smartBookmarks[queryId].position = PlacesUtils.bookmarks.getItemIndex(itemId);
1303 // We don't remove old Smart Bookmarks because user could still
1304 // find them useful, or could have personalized them.
1305 // Instead we remove the Smart Bookmark annotation.
1306 PlacesUtils.annotations.removeItemAnnotation(itemId, SMART_BOOKMARKS_ANNO);
1310 for (let queryId in smartBookmarks) {
1311 let smartBookmark = smartBookmarks[queryId];
1313 // We update or create only changed or new smart bookmarks.
1314 // Also we respect user choices, so we won't try to create a smart
1315 // bookmark if it has been removed.
1316 if (smartBookmarksCurrentVersion > 0 &&
1317 smartBookmark.newInVersion <= smartBookmarksCurrentVersion &&
1318 !smartBookmark.itemId)
1321 // Remove old version of the smart bookmark if it exists, since it
1322 // will be replaced in place.
1323 if (smartBookmark.itemId) {
1324 PlacesUtils.bookmarks.removeItem(smartBookmark.itemId);
1327 // Create the new smart bookmark and store its updated itemId.
1328 smartBookmark.itemId =
1329 PlacesUtils.bookmarks.insertBookmark(smartBookmark.parent,
1331 smartBookmark.position,
1332 smartBookmark.title);
1333 PlacesUtils.annotations.setItemAnnotation(smartBookmark.itemId,
1334 SMART_BOOKMARKS_ANNO,
1336 PlacesUtils.annotations.EXPIRE_NEVER);
1339 // If we are creating all Smart Bookmarks from ground up, add a
1340 // separator below them in the bookmarks menu.
1341 if (smartBookmarksCurrentVersion == 0 &&
1342 smartBookmarkItemIds.length == 0) {
1343 let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
1345 // Don't add a separator if the menu was empty or there is one already.
1347 PlacesUtils.bookmarks.getItemType(id) != PlacesUtils.bookmarks.TYPE_SEPARATOR) {
1348 PlacesUtils.bookmarks.insertSeparator(PlacesUtils.bookmarksMenuFolderId,
1356 PlacesUtils.bookmarks.runInBatchMode(batch, null);
1359 Components.utils.reportError(ex);
1362 Services.prefs.setIntPref(SMART_BOOKMARKS_PREF, SMART_BOOKMARKS_VERSION);
1363 Services.prefs.savePrefFile(null);
1368 #define BROKEN_WM_Z_ORDER
1371 // this returns the most recent non-popup browser window
1372 getMostRecentBrowserWindow: function BG_getMostRecentBrowserWindow() {
1373 function isFullBrowserWindow(win) {
1374 return !win.closed &&
1375 !win.document.documentElement.getAttribute("chromehidden");
1378 #ifdef BROKEN_WM_Z_ORDER
1379 var win = Services.wm.getMostRecentWindow("navigator:browser");
1381 // if we're lucky, this isn't a popup, and we can just return this
1382 if (win && !isFullBrowserWindow(win)) {
1384 let windowList = Services.wm.getEnumerator("navigator:browser");
1385 // this is oldest to newest, so this gets a bit ugly
1386 while (windowList.hasMoreElements()) {
1387 let nextWin = windowList.getNext();
1388 if (isFullBrowserWindow(nextWin))
1394 var windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
1395 while (windowList.hasMoreElements()) {
1396 let win = windowList.getNext();
1397 if (isFullBrowserWindow(win))
1406 classID: Components.ID("{eab9012e-5f74-4cbc-b2b5-a590235513cc}"),
1408 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
1409 Ci.nsISupportsWeakReference,
1410 Ci.nsIBrowserGlue]),
1412 // redefine the default factory for XPCOMUtils
1413 _xpcom_factory: BrowserGlueServiceFactory,
1416 function ContentPermissionPrompt() {}
1418 ContentPermissionPrompt.prototype = {
1419 classID: Components.ID("{d8903bf6-68d5-4e97-bcd1-e4d3012f721a}"),
1421 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
1423 prompt: function CPP_prompt(request) {
1425 if (request.type != "geolocation") {
1429 var requestingURI = request.uri;
1431 // Ignore requests from non-nsIStandardURLs
1432 if (!(requestingURI instanceof Ci.nsIStandardURL))
1435 var result = Services.perms.testExactPermission(requestingURI, "geo");
1437 if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
1442 if (result == Ci.nsIPermissionManager.DENY_ACTION) {
1447 function getChromeWindow(aWindow) {
1448 var chromeWin = aWindow
1449 .QueryInterface(Ci.nsIInterfaceRequestor)
1450 .getInterface(Ci.nsIWebNavigation)
1451 .QueryInterface(Ci.nsIDocShellTreeItem)
1453 .QueryInterface(Ci.nsIInterfaceRequestor)
1454 .getInterface(Ci.nsIDOMWindow)
1455 .QueryInterface(Ci.nsIDOMChromeWindow);
1459 var browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
1462 label: browserBundle.GetStringFromName("geolocation.shareLocation"),
1463 accessKey: browserBundle.GetStringFromName("geolocation.shareLocation.accesskey"),
1464 callback: function(notification) {
1470 // browserBundle.GetStringFromName("geolocation.learnMore")
1471 //var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter);
1472 //link.href = formatter.formatURLPref("browser.geolocation.warning.infoURL");
1475 var secondaryActions = [];
1477 // Different message/options if it is a local file
1478 if (requestingURI.schemeIs("file")) {
1479 message = browserBundle.formatStringFromName("geolocation.fileWantsToKnow",
1480 [requestingURI.path], 1);
1482 message = browserBundle.formatStringFromName("geolocation.siteWantsToKnow",
1483 [requestingURI.host], 1);
1485 // Don't offer to "always/never share" in PB mode
1486 var inPrivateBrowsing = Cc["@mozilla.org/privatebrowsing;1"].
1487 getService(Ci.nsIPrivateBrowsingService).
1488 privateBrowsingEnabled;
1490 if (!inPrivateBrowsing) {
1491 secondaryActions.push({
1492 label: browserBundle.GetStringFromName("geolocation.alwaysShare"),
1493 accessKey: browserBundle.GetStringFromName("geolocation.alwaysShare.accesskey"),
1494 callback: function () {
1495 Services.perms.add(requestingURI, "geo", Ci.nsIPermissionManager.ALLOW_ACTION);
1499 secondaryActions.push({
1500 label: browserBundle.GetStringFromName("geolocation.neverShare"),
1501 accessKey: browserBundle.GetStringFromName("geolocation.neverShare.accesskey"),
1502 callback: function () {
1503 Services.perms.add(requestingURI, "geo", Ci.nsIPermissionManager.DENY_ACTION);
1510 var requestingWindow = request.window.top;
1511 var chromeWin = getChromeWindow(requestingWindow).wrappedJSObject;
1512 var browser = chromeWin.gBrowser.getBrowserForDocument(requestingWindow.document);
1514 chromeWin.PopupNotifications.show(browser, "geolocation", message, "geo-notification-icon",
1515 mainAction, secondaryActions);
1519 var components = [BrowserGlue, ContentPermissionPrompt];
1520 var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);