1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/PlacesCategoriesStarter.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 sts=2 expandtab 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +//////////////////////////////////////////////////////////////////////////////// 1.11 +//// Constants 1.12 + 1.13 +const Cc = Components.classes; 1.14 +const Ci = Components.interfaces; 1.15 +const Cu = Components.utils; 1.16 + 1.17 +// Fired by TelemetryPing when async telemetry data should be collected. 1.18 +const TOPIC_GATHER_TELEMETRY = "gather-telemetry"; 1.19 + 1.20 +// Seconds between maintenance runs. 1.21 +const MAINTENANCE_INTERVAL_SECONDS = 7 * 86400; 1.22 + 1.23 +//////////////////////////////////////////////////////////////////////////////// 1.24 +//// Imports 1.25 + 1.26 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.27 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.28 +Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); 1.29 +XPCOMUtils.defineLazyModuleGetter(this, "PlacesDBUtils", 1.30 + "resource://gre/modules/PlacesDBUtils.jsm"); 1.31 + 1.32 +/** 1.33 + * This component can be used as a starter for modules that have to run when 1.34 + * certain categories are invoked. 1.35 + */ 1.36 +function PlacesCategoriesStarter() 1.37 +{ 1.38 + Services.obs.addObserver(this, TOPIC_GATHER_TELEMETRY, false); 1.39 + Services.obs.addObserver(this, PlacesUtils.TOPIC_SHUTDOWN, false); 1.40 + 1.41 + // nsINavBookmarkObserver implementation. 1.42 + let notify = (function () { 1.43 + if (!this._notifiedBookmarksSvcReady) { 1.44 + // For perf reasons unregister from the category, since no further 1.45 + // notifications are needed. 1.46 + Cc["@mozilla.org/categorymanager;1"] 1.47 + .getService(Ci.nsICategoryManager) 1.48 + .deleteCategoryEntry("bookmarks-observer", this, false); 1.49 + // Directly notify PlacesUtils, to ensure it catches the notification. 1.50 + PlacesUtils.observe(null, "bookmarks-service-ready", null); 1.51 + } 1.52 + }).bind(this); 1.53 + [ "onItemAdded", "onItemRemoved", "onItemChanged", "onBeginUpdateBatch", 1.54 + "onEndUpdateBatch", "onItemVisited", 1.55 + "onItemMoved" ].forEach(function(aMethod) { 1.56 + this[aMethod] = notify; 1.57 + }, this); 1.58 +} 1.59 + 1.60 +PlacesCategoriesStarter.prototype = { 1.61 + ////////////////////////////////////////////////////////////////////////////// 1.62 + //// nsIObserver 1.63 + 1.64 + observe: function PCS_observe(aSubject, aTopic, aData) 1.65 + { 1.66 + switch (aTopic) { 1.67 + case PlacesUtils.TOPIC_SHUTDOWN: 1.68 + Services.obs.removeObserver(this, PlacesUtils.TOPIC_SHUTDOWN); 1.69 + Services.obs.removeObserver(this, TOPIC_GATHER_TELEMETRY); 1.70 + let globalObj = 1.71 + Cu.getGlobalForObject(PlacesCategoriesStarter.prototype); 1.72 + let descriptor = 1.73 + Object.getOwnPropertyDescriptor(globalObj, "PlacesDBUtils"); 1.74 + if (descriptor.value !== undefined) { 1.75 + PlacesDBUtils.shutdown(); 1.76 + } 1.77 + break; 1.78 + case TOPIC_GATHER_TELEMETRY: 1.79 + PlacesDBUtils.telemetry(); 1.80 + break; 1.81 + case "idle-daily": 1.82 + // Once a week run places.sqlite maintenance tasks. 1.83 + let lastMaintenance = 0; 1.84 + try { 1.85 + lastMaintenance = 1.86 + Services.prefs.getIntPref("places.database.lastMaintenance"); 1.87 + } catch (ex) {} 1.88 + let nowSeconds = parseInt(Date.now() / 1000); 1.89 + if (lastMaintenance < nowSeconds - MAINTENANCE_INTERVAL_SECONDS) { 1.90 + PlacesDBUtils.maintenanceOnIdle(); 1.91 + } 1.92 + break; 1.93 + default: 1.94 + throw new Error("Trying to handle an unknown category."); 1.95 + } 1.96 + }, 1.97 + 1.98 + ////////////////////////////////////////////////////////////////////////////// 1.99 + //// nsISupports 1.100 + 1.101 + classID: Components.ID("803938d5-e26d-4453-bf46-ad4b26e41114"), 1.102 + 1.103 + _xpcom_factory: XPCOMUtils.generateSingletonFactory(PlacesCategoriesStarter), 1.104 + 1.105 + QueryInterface: XPCOMUtils.generateQI([ 1.106 + Ci.nsIObserver 1.107 + , Ci.nsINavBookmarkObserver 1.108 + ]) 1.109 +}; 1.110 + 1.111 +//////////////////////////////////////////////////////////////////////////////// 1.112 +//// Module Registration 1.113 + 1.114 +let components = [PlacesCategoriesStarter]; 1.115 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);