toolkit/components/places/PlacesCategoriesStarter.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial