|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 sts=2 expandtab |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 //////////////////////////////////////////////////////////////////////////////// |
|
8 //// Constants |
|
9 |
|
10 const Cc = Components.classes; |
|
11 const Ci = Components.interfaces; |
|
12 const Cu = Components.utils; |
|
13 |
|
14 // Fired by TelemetryPing when async telemetry data should be collected. |
|
15 const TOPIC_GATHER_TELEMETRY = "gather-telemetry"; |
|
16 |
|
17 // Seconds between maintenance runs. |
|
18 const MAINTENANCE_INTERVAL_SECONDS = 7 * 86400; |
|
19 |
|
20 //////////////////////////////////////////////////////////////////////////////// |
|
21 //// Imports |
|
22 |
|
23 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
24 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
25 Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); |
|
26 XPCOMUtils.defineLazyModuleGetter(this, "PlacesDBUtils", |
|
27 "resource://gre/modules/PlacesDBUtils.jsm"); |
|
28 |
|
29 /** |
|
30 * This component can be used as a starter for modules that have to run when |
|
31 * certain categories are invoked. |
|
32 */ |
|
33 function PlacesCategoriesStarter() |
|
34 { |
|
35 Services.obs.addObserver(this, TOPIC_GATHER_TELEMETRY, false); |
|
36 Services.obs.addObserver(this, PlacesUtils.TOPIC_SHUTDOWN, false); |
|
37 |
|
38 // nsINavBookmarkObserver implementation. |
|
39 let notify = (function () { |
|
40 if (!this._notifiedBookmarksSvcReady) { |
|
41 // For perf reasons unregister from the category, since no further |
|
42 // notifications are needed. |
|
43 Cc["@mozilla.org/categorymanager;1"] |
|
44 .getService(Ci.nsICategoryManager) |
|
45 .deleteCategoryEntry("bookmarks-observer", this, false); |
|
46 // Directly notify PlacesUtils, to ensure it catches the notification. |
|
47 PlacesUtils.observe(null, "bookmarks-service-ready", null); |
|
48 } |
|
49 }).bind(this); |
|
50 [ "onItemAdded", "onItemRemoved", "onItemChanged", "onBeginUpdateBatch", |
|
51 "onEndUpdateBatch", "onItemVisited", |
|
52 "onItemMoved" ].forEach(function(aMethod) { |
|
53 this[aMethod] = notify; |
|
54 }, this); |
|
55 } |
|
56 |
|
57 PlacesCategoriesStarter.prototype = { |
|
58 ////////////////////////////////////////////////////////////////////////////// |
|
59 //// nsIObserver |
|
60 |
|
61 observe: function PCS_observe(aSubject, aTopic, aData) |
|
62 { |
|
63 switch (aTopic) { |
|
64 case PlacesUtils.TOPIC_SHUTDOWN: |
|
65 Services.obs.removeObserver(this, PlacesUtils.TOPIC_SHUTDOWN); |
|
66 Services.obs.removeObserver(this, TOPIC_GATHER_TELEMETRY); |
|
67 let globalObj = |
|
68 Cu.getGlobalForObject(PlacesCategoriesStarter.prototype); |
|
69 let descriptor = |
|
70 Object.getOwnPropertyDescriptor(globalObj, "PlacesDBUtils"); |
|
71 if (descriptor.value !== undefined) { |
|
72 PlacesDBUtils.shutdown(); |
|
73 } |
|
74 break; |
|
75 case TOPIC_GATHER_TELEMETRY: |
|
76 PlacesDBUtils.telemetry(); |
|
77 break; |
|
78 case "idle-daily": |
|
79 // Once a week run places.sqlite maintenance tasks. |
|
80 let lastMaintenance = 0; |
|
81 try { |
|
82 lastMaintenance = |
|
83 Services.prefs.getIntPref("places.database.lastMaintenance"); |
|
84 } catch (ex) {} |
|
85 let nowSeconds = parseInt(Date.now() / 1000); |
|
86 if (lastMaintenance < nowSeconds - MAINTENANCE_INTERVAL_SECONDS) { |
|
87 PlacesDBUtils.maintenanceOnIdle(); |
|
88 } |
|
89 break; |
|
90 default: |
|
91 throw new Error("Trying to handle an unknown category."); |
|
92 } |
|
93 }, |
|
94 |
|
95 ////////////////////////////////////////////////////////////////////////////// |
|
96 //// nsISupports |
|
97 |
|
98 classID: Components.ID("803938d5-e26d-4453-bf46-ad4b26e41114"), |
|
99 |
|
100 _xpcom_factory: XPCOMUtils.generateSingletonFactory(PlacesCategoriesStarter), |
|
101 |
|
102 QueryInterface: XPCOMUtils.generateQI([ |
|
103 Ci.nsIObserver |
|
104 , Ci.nsINavBookmarkObserver |
|
105 ]) |
|
106 }; |
|
107 |
|
108 //////////////////////////////////////////////////////////////////////////////// |
|
109 //// Module Registration |
|
110 |
|
111 let components = [PlacesCategoriesStarter]; |
|
112 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components); |