Wed, 31 Dec 2014 06:09:35 +0100
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: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * singleton to provide data-level functionality to the views |
michael@0 | 9 | */ |
michael@0 | 10 | let TopSites = { |
michael@0 | 11 | prepareCache: function(aForce){ |
michael@0 | 12 | // front to the NewTabUtils' links cache |
michael@0 | 13 | // -ensure NewTabUtils.links links are pre-cached |
michael@0 | 14 | |
michael@0 | 15 | // avoid re-fetching links data while a fetch is in flight |
michael@0 | 16 | if (this._promisedCache && !aForce) { |
michael@0 | 17 | return this._promisedCache; |
michael@0 | 18 | } |
michael@0 | 19 | let deferred = Promise.defer(); |
michael@0 | 20 | this._promisedCache = deferred.promise; |
michael@0 | 21 | |
michael@0 | 22 | NewTabUtils.links.populateCache(function () { |
michael@0 | 23 | deferred.resolve(); |
michael@0 | 24 | this._promisedCache = null; |
michael@0 | 25 | this._sites = null; // reset our sites cache so they are built anew |
michael@0 | 26 | this._sitesDirty.clear(); |
michael@0 | 27 | }.bind(this), true); |
michael@0 | 28 | return this._promisedCache; |
michael@0 | 29 | }, |
michael@0 | 30 | |
michael@0 | 31 | _sites: null, |
michael@0 | 32 | _sitesDirty: new Set(), |
michael@0 | 33 | getSites: function() { |
michael@0 | 34 | if (this._sites) { |
michael@0 | 35 | return this._sites; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | let links = NewTabUtils.links.getLinks(); |
michael@0 | 39 | let sites = links.map(function(aLink){ |
michael@0 | 40 | let site = new Site(aLink); |
michael@0 | 41 | return site; |
michael@0 | 42 | }); |
michael@0 | 43 | |
michael@0 | 44 | // reset state |
michael@0 | 45 | this._sites = sites; |
michael@0 | 46 | this._sitesDirty.clear(); |
michael@0 | 47 | return this._sites; |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Get list of top site as in need of update/re-render |
michael@0 | 52 | * @param aSite Optionally add Site arguments to be refreshed/updated |
michael@0 | 53 | */ |
michael@0 | 54 | dirty: function() { |
michael@0 | 55 | // add any arguments for more fine-grained updates rather than invalidating the whole collection |
michael@0 | 56 | for (let i=0; i<arguments.length; i++) { |
michael@0 | 57 | this._sitesDirty.add(arguments[i]); |
michael@0 | 58 | } |
michael@0 | 59 | return this._sitesDirty; |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Cause update of top sites |
michael@0 | 64 | */ |
michael@0 | 65 | update: function() { |
michael@0 | 66 | NewTabUtils.allPages.update(); |
michael@0 | 67 | // then clear all the dirty flags |
michael@0 | 68 | this._sitesDirty.clear(); |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Pin top site at a given index |
michael@0 | 73 | * @param aSites array of sites to be pinned |
michael@0 | 74 | * @param aSlotIndices indices corresponding to the site to be pinned |
michael@0 | 75 | */ |
michael@0 | 76 | pinSites: function(aSites, aSlotIndices) { |
michael@0 | 77 | if (aSites.length !== aSlotIndices.length) |
michael@0 | 78 | throw new Error("TopSites.pinSites: Mismatched sites/indices arguments"); |
michael@0 | 79 | |
michael@0 | 80 | for (let i=0; i<aSites.length && i<aSlotIndices.length; i++){ |
michael@0 | 81 | let site = aSites[i], |
michael@0 | 82 | idx = aSlotIndices[i]; |
michael@0 | 83 | if (!(site && site.url)) { |
michael@0 | 84 | throw Cr.NS_ERROR_INVALID_ARG |
michael@0 | 85 | } |
michael@0 | 86 | // pinned state is a pref, using Storage apis therefore sync |
michael@0 | 87 | NewTabUtils.pinnedLinks.pin(site, idx); |
michael@0 | 88 | this.dirty(site); |
michael@0 | 89 | } |
michael@0 | 90 | this.update(); |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Unpin top sites |
michael@0 | 95 | * @param aSites array of sites to be unpinned |
michael@0 | 96 | */ |
michael@0 | 97 | unpinSites: function(aSites) { |
michael@0 | 98 | for (let site of aSites) { |
michael@0 | 99 | if (!(site && site.url)) { |
michael@0 | 100 | throw Cr.NS_ERROR_INVALID_ARG |
michael@0 | 101 | } |
michael@0 | 102 | // pinned state is a pref, using Storage apis therefore sync |
michael@0 | 103 | NewTabUtils.pinnedLinks.unpin(site); |
michael@0 | 104 | this.dirty(site); |
michael@0 | 105 | } |
michael@0 | 106 | this.update(); |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Hide (block) top sites |
michael@0 | 111 | * @param aSites array of sites to be unpinned |
michael@0 | 112 | */ |
michael@0 | 113 | hideSites: function(aSites) { |
michael@0 | 114 | for (let site of aSites) { |
michael@0 | 115 | if (!(site && site.url)) { |
michael@0 | 116 | throw Cr.NS_ERROR_INVALID_ARG |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | site._restorePinIndex = NewTabUtils.pinnedLinks._indexOfLink(site); |
michael@0 | 120 | // blocked state is a pref, using Storage apis therefore sync |
michael@0 | 121 | NewTabUtils.blockedLinks.block(site); |
michael@0 | 122 | } |
michael@0 | 123 | // clear out the cache, we'll fetch and re-render |
michael@0 | 124 | this._sites = null; |
michael@0 | 125 | this._sitesDirty.clear(); |
michael@0 | 126 | this.update(); |
michael@0 | 127 | }, |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Un-hide (restore) top sites |
michael@0 | 131 | * @param aSites array of sites to be restored |
michael@0 | 132 | */ |
michael@0 | 133 | restoreSites: function(aSites) { |
michael@0 | 134 | for (let site of aSites) { |
michael@0 | 135 | if (!(site && site.url)) { |
michael@0 | 136 | throw Cr.NS_ERROR_INVALID_ARG |
michael@0 | 137 | } |
michael@0 | 138 | NewTabUtils.blockedLinks.unblock(site); |
michael@0 | 139 | let pinIndex = site._restorePinIndex; |
michael@0 | 140 | |
michael@0 | 141 | if (!isNaN(pinIndex) && pinIndex > -1) { |
michael@0 | 142 | NewTabUtils.pinnedLinks.pin(site, pinIndex); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | // clear out the cache, we'll fetch and re-render |
michael@0 | 146 | this._sites = null; |
michael@0 | 147 | this._sitesDirty.clear(); |
michael@0 | 148 | this.update(); |
michael@0 | 149 | }, |
michael@0 | 150 | |
michael@0 | 151 | _linkFromNode: function _linkFromNode(aNode) { |
michael@0 | 152 | return { |
michael@0 | 153 | url: aNode.getAttribute("value"), |
michael@0 | 154 | title: aNode.getAttribute("label") |
michael@0 | 155 | }; |
michael@0 | 156 | } |
michael@0 | 157 | }; |
michael@0 | 158 |