toolkit/components/places/PlacesCategoriesStarter.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     7 ////////////////////////////////////////////////////////////////////////////////
     8 //// Constants
    10 const Cc = Components.classes;
    11 const Ci = Components.interfaces;
    12 const Cu = Components.utils;
    14 // Fired by TelemetryPing when async telemetry data should be collected.
    15 const TOPIC_GATHER_TELEMETRY = "gather-telemetry";
    17 // Seconds between maintenance runs.
    18 const MAINTENANCE_INTERVAL_SECONDS = 7 * 86400;
    20 ////////////////////////////////////////////////////////////////////////////////
    21 //// Imports
    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");
    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);
    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 }
    57 PlacesCategoriesStarter.prototype = {
    58   //////////////////////////////////////////////////////////////////////////////
    59   //// nsIObserver
    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   },
    95   //////////////////////////////////////////////////////////////////////////////
    96   //// nsISupports
    98   classID: Components.ID("803938d5-e26d-4453-bf46-ad4b26e41114"),
   100   _xpcom_factory: XPCOMUtils.generateSingletonFactory(PlacesCategoriesStarter),
   102   QueryInterface: XPCOMUtils.generateQI([
   103     Ci.nsIObserver
   104   , Ci.nsINavBookmarkObserver
   105   ])
   106 };
   108 ////////////////////////////////////////////////////////////////////////////////
   109 //// Module Registration
   111 let components = [PlacesCategoriesStarter];
   112 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

mercurial