toolkit/components/places/tests/expiration/head_expiration.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.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     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 const Ci = Components.interfaces;
     8 const Cc = Components.classes;
     9 const Cr = Components.results;
    10 const Cu = Components.utils;
    12 Cu.import("resource://gre/modules/Services.jsm");
    14 // Import common head.
    15 let (commonFile = do_get_file("../head_common.js", false)) {
    16   let uri = Services.io.newFileURI(commonFile);
    17   Services.scriptloader.loadSubScript(uri.spec, this);
    18 }
    20 // Put any other stuff relative to this test folder below.
    23 // Simulates an expiration at shutdown.
    24 function shutdownExpiration()
    25 {
    26   let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
    27   expire.observe(null, "places-will-close-connection", null);
    28 }
    31 /**
    32  * Causes expiration component to start, otherwise it would wait for the first
    33  * history notification.
    34  */
    35 function force_expiration_start() {
    36   Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsISupports);
    37 }
    40 /**
    41  * Forces an expiration run.
    42  *
    43  * @param [optional] aLimit
    44  *        Limit for the expiration.  Pass -1 for unlimited.
    45  *        Any other non-positive value will just expire orphans.
    46  *
    47  * @return {Promise}
    48  * @resolves When expiration finishes.
    49  * @rejects Never.
    50  */
    51 function promiseForceExpirationStep(aLimit) {
    52   let promise = promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
    53   let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
    54   expire.observe(null, "places-debug-start-expiration", aLimit);
    55   return promise;
    56 }
    59 /**
    60  * Expiration preferences helpers.
    61  */
    63 function setInterval(aNewInterval) {
    64   Services.prefs.setIntPref("places.history.expiration.interval_seconds", aNewInterval);
    65 }
    66 function getInterval() {
    67   return Services.prefs.getIntPref("places.history.expiration.interval_seconds");
    68 }
    69 function clearInterval() {
    70   try {
    71     Services.prefs.clearUserPref("places.history.expiration.interval_seconds");
    72   }
    73   catch(ex) {}
    74 }
    77 function setMaxPages(aNewMaxPages) {
    78   Services.prefs.setIntPref("places.history.expiration.max_pages", aNewMaxPages);
    79 }
    80 function getMaxPages() {
    81   return Services.prefs.getIntPref("places.history.expiration.max_pages");
    82 }
    83 function clearMaxPages() {
    84   try {
    85     Services.prefs.clearUserPref("places.history.expiration.max_pages");
    86   }
    87   catch(ex) {}
    88 }
    91 function setHistoryEnabled(aHistoryEnabled) {
    92   Services.prefs.setBoolPref("places.history.enabled", aHistoryEnabled);
    93 }
    94 function getHistoryEnabled() {
    95   return Services.prefs.getBoolPref("places.history.enabled");
    96 }
    97 function clearHistoryEnabled() {
    98   try {
    99     Services.prefs.clearUserPref("places.history.enabled");
   100   }
   101   catch(ex) {}
   102 }
   104 /**
   105  * Returns a PRTime in the past usable to add expirable visits.
   106  *
   107  * @note Expiration ignores any visit added in the last 7 days, but it's
   108  *       better be safe against DST issues, by going back one day more.
   109  */
   110 function getExpirablePRTime() {
   111   let dateObj = new Date();
   112   // Normalize to midnight
   113   dateObj.setHours(0);
   114   dateObj.setMinutes(0);
   115   dateObj.setSeconds(0);
   116   dateObj.setMilliseconds(0);
   117   dateObj = new Date(dateObj.getTime() - 8 * 86400000);
   118   return dateObj.getTime() * 1000;
   119 }

mercurial