aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/start.js
blob: 45d20d996a07976e3f23c4a477552bfe8c89a83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************

    µBlock - a Chromium browser extension to block requests.
    Copyright (C) 2014-2015 Raymond Hill

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

/* global vAPI, µBlock */

/******************************************************************************/

// Load all: executed once.

(function() {

/******************************************************************************/

// Final initialization steps after all needed assets are in memory.
// - Initialize internal state with maybe already existing tabs.
// - Schedule next update operation.

var onAllReady = function() {
    var µb = µBlock;

    // https://github.com/gorhill/uBlock/issues/184
    // Check for updates not too far in the future.
    µb.assetUpdater.onStart.addEventListenerb.updateStartHandler.bindb));
    µb.assetUpdater.onCompleted.addEventListenerb.updateCompleteHandler.bindb));

    // Important: remove barrier to remote fetching, this was useful only
    // for launch time.
    µb.assets.allowRemoteFetch = true;

    vAPI.onLoadAllCompleted();
};

/******************************************************************************/

// To bring older versions up to date

var onVersionReady = function(bin) {
    var µb = µBlock;
    var lastVersion = bin.version || '0.0.0.0';

    // Whitelist some key scopes by default
    if ( lastVersion.localeCompare('0.8.6.0') < 0 ) {
        µb.netWhitelist = µb.whitelistFromString(
            µb.stringFromWhitelistb.netWhitelist) + 
            '\n' + 
            µb.netWhitelistDefault
        );
        µb.saveWhitelist();
    }

    vAPI.storage.set({ version: vAPI.app.version });
    onAllReady();
};

/******************************************************************************/

// Filter lists
// Whitelist

var countdown = 2;
var doCountdown = function() {
    countdown -= 1;
    if ( countdown !== 0 ) {
        return;
    }
    // Last step: do whatever is necessary when version changes
    vAPI.storage.get('version', onVersionReady);
};

/******************************************************************************/

// Filters are in memory.
// Filter engines need PSL to be ready.

var onFiltersReady = function() {
    doCountdown();
};

/******************************************************************************/

// https://github.com/gorhill/uBlock/issues/226
// Whitelist in memory.
// Whitelist parser needs PSL to be ready.
// gorhill 2014-12-15: not anymore

var onWhitelistReady = function() {
    doCountdown();
};

/******************************************************************************/

// Load order because dependencies:
// User settings -> PSL -> [filter lists]

var onPSLReady = function() {
    µBlock.loadFilterLists(onFiltersReady);
};

/******************************************************************************/

// If no selfie available, take the long way, i.e. load and parse
// raw data.

var onSelfieReady = function(success) {
    if ( success === true ) {
        onFiltersReady();
        return;
    }
    µBlock.loadPublicSuffixList(onPSLReady);
};

/******************************************************************************/

// User settings are in memory

var onUserSettingsReady = function(userSettings) {
    var µb = µBlock;

    // https://github.com/gorhill/uBlock/issues/426
    // Important: block remote fetching for when loading assets at launch
    // time.
    µb.assets.allowRemoteFetch = false;
    µb.assets.autoUpdate = userSettings.autoUpdate;
    µb.fromSelfie(onSelfieReady);

    // https://github.com/gorhill/uBlock/issues/540
    // Disabling local mirroring for the time being
    userSettings.experimentalEnabled = false;
    µb.mirrors.toggle(false /* userSettings.experimentalEnabled */);

    µb.contextMenu.toggle(userSettings.contextMenuEnabled);
    µb.permanentFirewall.fromString(userSettings.dynamicFilteringString);
    µb.sessionFirewall.assignb.permanentFirewall);

    // Remove obsolete setting
    delete userSettings.logRequests;
    µb.XAL.keyvalRemoveOne('logRequests');
};

µBlock.loadUserSettings(onUserSettingsReady);
µBlock.loadWhitelist(onWhitelistReady);
µBlock.loadLocalSettings();

/******************************************************************************/

})();

/******************************************************************************/