You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

964 lines
31 KiB

7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
10 years ago
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. uMatrix - a browser extension to block requests.
  3. Copyright (C) 2014-2018 The uBlock Origin authors
  4. Copyright (C) 2017-2018 Raymond Hill
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://github.com/gorhill/uMatrix
  16. */
  17. /* global self, µMatrix */
  18. // For background page
  19. 'use strict';
  20. /******************************************************************************/
  21. (function() {
  22. /******************************************************************************/
  23. var vAPI = self.vAPI = self.vAPI || {};
  24. var chrome = self.chrome;
  25. var manifest = chrome.runtime.getManifest();
  26. vAPI.chrome = true;
  27. vAPI.webextFlavor = undefined;
  28. if (
  29. self.browser instanceof Object &&
  30. typeof self.browser.runtime.getBrowserInfo === 'function'
  31. ) {
  32. self.browser.runtime.getBrowserInfo().then(function(info) {
  33. vAPI.webextFlavor = info.vendor + '-' + info.name + '-' + info.version;
  34. });
  35. } else {
  36. vAPI.webextFlavor = '';
  37. }
  38. var noopFunc = function(){};
  39. /******************************************************************************/
  40. // https://github.com/gorhill/uMatrix/issues/234
  41. // https://developer.chrome.com/extensions/privacy#property-network
  42. chrome.privacy.network.networkPredictionEnabled.set({ value: false });
  43. /******************************************************************************/
  44. vAPI.app = {
  45. name: manifest.name,
  46. version: manifest.version
  47. };
  48. /******************************************************************************/
  49. vAPI.app.start = function() {
  50. };
  51. /******************************************************************************/
  52. vAPI.app.stop = function() {
  53. };
  54. /******************************************************************************/
  55. vAPI.app.restart = function() {
  56. chrome.runtime.reload();
  57. };
  58. /******************************************************************************/
  59. // chrome.storage.local.get(null, function(bin){ console.debug('%o', bin); });
  60. vAPI.storage = chrome.storage.local;
  61. vAPI.cacheStorage = chrome.storage.local;
  62. /******************************************************************************/
  63. vAPI.tabs = {};
  64. /******************************************************************************/
  65. vAPI.isBehindTheSceneTabId = function(tabId) {
  66. return tabId.toString() === '-1';
  67. };
  68. vAPI.noTabId = '-1';
  69. /******************************************************************************/
  70. vAPI.tabs.registerListeners = function() {
  71. var onNavigationClient = this.onNavigation || noopFunc;
  72. var onUpdatedClient = this.onUpdated || noopFunc;
  73. var onClosedClient = this.onClosed || noopFunc;
  74. // https://developer.chrome.com/extensions/webNavigation
  75. // [onCreatedNavigationTarget ->]
  76. // onBeforeNavigate ->
  77. // onCommitted ->
  78. // onDOMContentLoaded ->
  79. // onCompleted
  80. // The chrome.webRequest.onBeforeRequest() won't be called for everything
  81. // else than `http`/`https`. Thus, in such case, we will bind the tab as
  82. // early as possible in order to increase the likelihood of a context
  83. // properly setup if network requests are fired from within the tab.
  84. // Example: Chromium + case #6 at
  85. // http://raymondhill.net/ublock/popup.html
  86. var reGoodForWebRequestAPI = /^https?:\/\//;
  87. var onCreatedNavigationTarget = function(details) {
  88. //console.debug('onCreatedNavigationTarget: tab id %d = "%s"', details.tabId, details.url);
  89. if ( reGoodForWebRequestAPI.test(details.url) ) { return; }
  90. details.tabId = details.tabId.toString();
  91. onNavigationClient(details);
  92. };
  93. var onUpdated = function(tabId, changeInfo, tab) {
  94. tabId = tabId.toString();
  95. onUpdatedClient(tabId, changeInfo, tab);
  96. };
  97. var onCommitted = function(details) {
  98. // Important: do not call client if not top frame.
  99. if ( details.frameId !== 0 ) {
  100. return;
  101. }
  102. details.tabId = details.tabId.toString();
  103. onNavigationClient(details);
  104. //console.debug('onCommitted: tab id %d = "%s"', details.tabId, details.url);
  105. };
  106. var onClosed = function(tabId) {
  107. onClosedClient(tabId.toString());
  108. };
  109. chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
  110. chrome.webNavigation.onCommitted.addListener(onCommitted);
  111. chrome.tabs.onUpdated.addListener(onUpdated);
  112. chrome.tabs.onRemoved.addListener(onClosed);
  113. };
  114. /******************************************************************************/
  115. // tabId: null, // active tab
  116. vAPI.tabs.get = function(tabId, callback) {
  117. var onTabReady = function(tab) {
  118. // https://code.google.com/p/chromium/issues/detail?id=410868#c8
  119. if ( chrome.runtime.lastError ) {
  120. }
  121. if ( tab instanceof Object ) {
  122. tab.id = tab.id.toString();
  123. }
  124. callback(tab);
  125. };
  126. if ( tabId !== null ) {
  127. if ( typeof tabId === 'string' ) {
  128. tabId = parseInt(tabId, 10);
  129. }
  130. chrome.tabs.get(tabId, onTabReady);
  131. return;
  132. }
  133. var onTabReceived = function(tabs) {
  134. // https://code.google.com/p/chromium/issues/detail?id=410868#c8
  135. if ( chrome.runtime.lastError ) {
  136. }
  137. var tab = null;
  138. if ( Array.isArray(tabs) && tabs.length !== 0 ) {
  139. tab = tabs[0];
  140. tab.id = tab.id.toString();
  141. }
  142. callback(tab);
  143. };
  144. chrome.tabs.query({ active: true, currentWindow: true }, onTabReceived);
  145. };
  146. /******************************************************************************/
  147. vAPI.tabs.getAll = function(callback) {
  148. var onTabsReady = function(tabs) {
  149. if ( Array.isArray(tabs) ) {
  150. var i = tabs.length;
  151. while ( i-- ) {
  152. tabs[i].id = tabs[i].id.toString();
  153. }
  154. }
  155. callback(tabs);
  156. };
  157. chrome.tabs.query({ url: '<all_urls>' }, onTabsReady);
  158. };
  159. /******************************************************************************/
  160. // properties of the details object:
  161. // url: 'URL', // the address that will be opened
  162. // tabId: 1, // the tab is used if set, instead of creating a new one
  163. // index: -1, // undefined: end of the list, -1: following tab, or after index
  164. // active: false, // opens the tab in background - true and undefined: foreground
  165. // select: true // if a tab is already opened with that url, then select it instead of opening a new one
  166. vAPI.tabs.open = function(details) {
  167. var targetURL = details.url;
  168. if ( typeof targetURL !== 'string' || targetURL === '' ) {
  169. return null;
  170. }
  171. // extension pages
  172. if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
  173. targetURL = vAPI.getURL(targetURL);
  174. }
  175. // dealing with Chrome's asynchronous API
  176. var wrapper = function() {
  177. if ( details.active === undefined ) {
  178. details.active = true;
  179. }
  180. var subWrapper = function() {
  181. var _details = {
  182. url: targetURL,
  183. active: !!details.active
  184. };
  185. // Opening a tab from incognito window won't focus the window
  186. // in which the tab was opened
  187. var focusWindow = function(tab) {
  188. if ( tab.active ) {
  189. chrome.windows.update(tab.windowId, { focused: true });
  190. }
  191. };
  192. if ( !details.tabId ) {
  193. if ( details.index !== undefined ) {
  194. _details.index = details.index;
  195. }
  196. chrome.tabs.create(_details, focusWindow);
  197. return;
  198. }
  199. // update doesn't accept index, must use move
  200. chrome.tabs.update(parseInt(details.tabId, 10), _details, function(tab) {
  201. // if the tab doesn't exist
  202. if ( vAPI.lastError() ) {
  203. chrome.tabs.create(_details, focusWindow);
  204. } else if ( details.index !== undefined ) {
  205. chrome.tabs.move(tab.id, {index: details.index});
  206. }
  207. });
  208. };
  209. // Open in a standalone window
  210. if ( details.popup === true ) {
  211. chrome.windows.create({ url: details.url, type: 'popup' });
  212. return;
  213. }
  214. if ( details.index !== -1 ) {
  215. subWrapper();
  216. return;
  217. }
  218. vAPI.tabs.get(null, function(tab) {
  219. if ( tab ) {
  220. details.index = tab.index + 1;
  221. } else {
  222. delete details.index;
  223. }
  224. subWrapper();
  225. });
  226. };
  227. if ( !details.select ) {
  228. wrapper();
  229. return;
  230. }
  231. chrome.tabs.query({ url: targetURL }, function(tabs) {
  232. if ( chrome.runtime.lastError ) { /* noop */ }
  233. var tab = Array.isArray(tabs) && tabs[0];
  234. if ( tab ) {
  235. chrome.tabs.update(tab.id, { active: true }, function(tab) {
  236. chrome.windows.update(tab.windowId, { focused: true });
  237. });
  238. } else {
  239. wrapper();
  240. }
  241. });
  242. };
  243. /******************************************************************************/
  244. // Replace the URL of a tab. Noop if the tab does not exist.
  245. vAPI.tabs.replace = function(tabId, url) {
  246. var targetURL = url;
  247. // extension pages
  248. if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
  249. targetURL = vAPI.getURL(targetURL);
  250. }
  251. if ( typeof tabId !== 'number' ) {
  252. tabId = parseInt(tabId, 10);
  253. if ( isNaN(tabId) ) {
  254. return;
  255. }
  256. }
  257. chrome.tabs.update(tabId, { url: targetURL }, function() {
  258. // this prevent console error
  259. if ( chrome.runtime.lastError ) {
  260. return;
  261. }
  262. });
  263. };
  264. /******************************************************************************/
  265. vAPI.tabs.remove = function(tabId) {
  266. var onTabRemoved = function() {
  267. if ( vAPI.lastError() ) {
  268. }
  269. };
  270. chrome.tabs.remove(parseInt(tabId, 10), onTabRemoved);
  271. };
  272. /******************************************************************************/
  273. vAPI.tabs.reload = function(tabId, bypassCache) {
  274. if ( typeof tabId === 'string' ) {
  275. tabId = parseInt(tabId, 10);
  276. }
  277. if ( isNaN(tabId) ) {
  278. return;
  279. }
  280. chrome.tabs.reload(tabId, { bypassCache: bypassCache === true });
  281. };
  282. /******************************************************************************/
  283. vAPI.tabs.injectScript = function(tabId, details, callback) {
  284. var onScriptExecuted = function() {
  285. // https://code.google.com/p/chromium/issues/detail?id=410868#c8
  286. if ( chrome.runtime.lastError ) {
  287. }
  288. if ( typeof callback === 'function' ) {
  289. callback();
  290. }
  291. };
  292. if ( tabId ) {
  293. tabId = parseInt(tabId, 10);
  294. chrome.tabs.executeScript(tabId, details, onScriptExecuted);
  295. } else {
  296. chrome.tabs.executeScript(details, onScriptExecuted);
  297. }
  298. };
  299. /******************************************************************************/
  300. // Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
  301. // https://github.com/chrisaljoudi/uBlock/issues/19
  302. // https://github.com/chrisaljoudi/uBlock/issues/207
  303. // Since we may be called asynchronously, the tab id may not exist
  304. // anymore, so this ensures it does still exist.
  305. vAPI.setIcon = (function() {
  306. let onIconReady = function(tabId, badgeDetails) {
  307. if ( vAPI.lastError() ) { return; }
  308. if ( badgeDetails.text !== undefined ) {
  309. chrome.browserAction.setBadgeText({
  310. tabId: tabId,
  311. text: badgeDetails.text
  312. });
  313. }
  314. if ( badgeDetails.color !== undefined ) {
  315. chrome.browserAction.setBadgeBackgroundColor({
  316. tabId: tabId,
  317. color: badgeDetails.color
  318. });
  319. }
  320. };
  321. return function(tabId, iconDetails, badgeDetails) {
  322. tabId = parseInt(tabId, 10);
  323. if ( isNaN(tabId) || tabId === -1 ) { return; }
  324. chrome.browserAction.setIcon(
  325. { tabId: tabId, path: iconDetails },
  326. function() { onIconReady(tabId, badgeDetails); }
  327. );
  328. };
  329. })();
  330. /******************************************************************************/
  331. /******************************************************************************/
  332. vAPI.messaging = {
  333. ports: new Map(),
  334. listeners: {},
  335. defaultHandler: null,
  336. NOOPFUNC: noopFunc,
  337. UNHANDLED: 'vAPI.messaging.notHandled'
  338. };
  339. /******************************************************************************/
  340. vAPI.messaging.listen = function(listenerName, callback) {
  341. this.listeners[listenerName] = callback;
  342. };
  343. /******************************************************************************/
  344. vAPI.messaging.onPortMessage = (function() {
  345. var messaging = vAPI.messaging;
  346. // Use a wrapper to avoid closure and to allow reuse.
  347. var CallbackWrapper = function(port, request) {
  348. this.callback = this.proxy.bind(this); // bind once
  349. this.init(port, request);
  350. };
  351. CallbackWrapper.prototype = {
  352. init: function(port, request) {
  353. this.port = port;
  354. this.request = request;
  355. return this;
  356. },
  357. proxy: function(response) {
  358. // https://github.com/chrisaljoudi/uBlock/issues/383
  359. if ( messaging.ports.has(this.port.name) ) {
  360. this.port.postMessage({
  361. auxProcessId: this.request.auxProcessId,
  362. channelName: this.request.channelName,
  363. msg: response !== undefined ? response : null
  364. });
  365. }
  366. // Mark for reuse
  367. this.port = this.request = null;
  368. callbackWrapperJunkyard.push(this);
  369. }
  370. };
  371. var callbackWrapperJunkyard = [];
  372. var callbackWrapperFactory = function(port, request) {
  373. var wrapper = callbackWrapperJunkyard.pop();
  374. if ( wrapper ) {
  375. return wrapper.init(port, request);
  376. }
  377. return new CallbackWrapper(port, request);
  378. };
  379. // https://bugzilla.mozilla.org/show_bug.cgi?id=1392067
  380. // Workaround: manually remove ports matching removed tab.
  381. chrome.tabs.onRemoved.addListener(function(tabId) {
  382. for ( var port of messaging.ports.values() ) {
  383. var tab = port.sender && port.sender.tab;
  384. if ( !tab ) { continue; }
  385. if ( tab.id === tabId ) {
  386. vAPI.messaging.onPortDisconnect(port);
  387. }
  388. }
  389. });
  390. return function(request, port) {
  391. // prepare response
  392. var callback = this.NOOPFUNC;
  393. if ( request.auxProcessId !== undefined ) {
  394. callback = callbackWrapperFactory(port, request).callback;
  395. }
  396. // Auxiliary process to main process: specific handler
  397. var r = this.UNHANDLED,
  398. listener = this.listeners[request.channelName];
  399. if ( typeof listener === 'function' ) {
  400. r = listener(request.msg, port.sender, callback);
  401. }
  402. if ( r !== this.UNHANDLED ) { return; }
  403. // Auxiliary process to main process: default handler
  404. r = this.defaultHandler(request.msg, port.sender, callback);
  405. if ( r !== this.UNHANDLED ) { return; }
  406. // Auxiliary process to main process: no handler
  407. console.error(
  408. 'vAPI.messaging.onPortMessage > unhandled request: %o',
  409. request
  410. );
  411. // Need to callback anyways in case caller expected an answer, or
  412. // else there is a memory leak on caller's side
  413. callback();
  414. }.bind(vAPI.messaging);
  415. })();
  416. /******************************************************************************/
  417. vAPI.messaging.onPortDisconnect = function(port) {
  418. port.onDisconnect.removeListener(this.onPortDisconnect);
  419. port.onMessage.removeListener(this.onPortMessage);
  420. this.ports.delete(port.name);
  421. }.bind(vAPI.messaging);
  422. /******************************************************************************/
  423. vAPI.messaging.onPortConnect = function(port) {
  424. port.onDisconnect.addListener(this.onPortDisconnect);
  425. port.onMessage.addListener(this.onPortMessage);
  426. this.ports.set(port.name, port);
  427. }.bind(vAPI.messaging);
  428. /******************************************************************************/
  429. vAPI.messaging.setup = function(defaultHandler) {
  430. if ( this.defaultHandler !== null ) { return; }
  431. if ( typeof defaultHandler !== 'function' ) {
  432. defaultHandler = function(){
  433. return vAPI.messaging.UNHANDLED;
  434. };
  435. }
  436. this.defaultHandler = defaultHandler;
  437. chrome.runtime.onConnect.addListener(this.onPortConnect);
  438. };
  439. /******************************************************************************/
  440. vAPI.messaging.broadcast = function(message) {
  441. var messageWrapper = {
  442. broadcast: true,
  443. msg: message
  444. };
  445. for ( var port of this.ports.values() ) {
  446. port.postMessage(messageWrapper);
  447. }
  448. };
  449. /******************************************************************************/
  450. /******************************************************************************/
  451. vAPI.net = {};
  452. /******************************************************************************/
  453. vAPI.net.registerListeners = function() {
  454. var µm = µMatrix;
  455. // Normalizing request types
  456. // >>>>>>>>
  457. var extToTypeMap = new Map([
  458. ['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
  459. ['mp3','media'],['mp4','media'],['webm','media'],
  460. ['gif','image'],['ico','image'],['jpeg','image'],['jpg','image'],['png','image'],['webp','image']
  461. ]);
  462. var normalizeRequestDetails = function(details) {
  463. details.tabId = details.tabId.toString();
  464. // The rest of the function code is to normalize request type
  465. if ( details.type !== 'other' ) { return; }
  466. // Try to map known "extension" part of URL to request type.
  467. var path = µm.URI.pathFromURI(details.url),
  468. pos = path.indexOf('.', path.length - 6);
  469. if ( pos !== -1 ) {
  470. var type = extToTypeMap.get(path.slice(pos + 1));
  471. if ( type !== undefined ) {
  472. details.type = type;
  473. }
  474. }
  475. };
  476. // <<<<<<<<
  477. // End of: Normalizing request types
  478. // Network event handlers
  479. // >>>>>>>>
  480. var onBeforeRequestClient = this.onBeforeRequest.callback;
  481. chrome.webRequest.onBeforeRequest.addListener(
  482. function(details) {
  483. normalizeRequestDetails(details);
  484. return onBeforeRequestClient(details);
  485. },
  486. {
  487. 'urls': this.onBeforeRequest.urls || [ '<all_urls>' ],
  488. 'types': this.onBeforeRequest.types || undefined
  489. },
  490. this.onBeforeRequest.extra
  491. );
  492. var onBeforeSendHeadersClient = this.onBeforeSendHeaders.callback;
  493. var onBeforeSendHeaders = function(details) {
  494. normalizeRequestDetails(details);
  495. return onBeforeSendHeadersClient(details);
  496. };
  497. chrome.webRequest.onBeforeSendHeaders.addListener(
  498. onBeforeSendHeaders,
  499. {
  500. 'urls': this.onBeforeSendHeaders.urls || [ '<all_urls>' ],
  501. 'types': this.onBeforeSendHeaders.types || undefined
  502. },
  503. this.onBeforeSendHeaders.extra
  504. );
  505. var onHeadersReceivedClient = this.onHeadersReceived.callback;
  506. var onHeadersReceived = function(details) {
  507. normalizeRequestDetails(details);
  508. return onHeadersReceivedClient(details);
  509. };
  510. chrome.webRequest.onHeadersReceived.addListener(
  511. onHeadersReceived,
  512. {
  513. 'urls': this.onHeadersReceived.urls || [ '<all_urls>' ],
  514. 'types': this.onHeadersReceived.types || undefined
  515. },
  516. this.onHeadersReceived.extra
  517. );
  518. // <<<<<<<<
  519. // End of: Network event handlers
  520. };
  521. /******************************************************************************/
  522. /******************************************************************************/
  523. vAPI.contextMenu = {
  524. create: function(details, callback) {
  525. this.menuId = details.id;
  526. this.callback = callback;
  527. chrome.contextMenus.create(details);
  528. chrome.contextMenus.onClicked.addListener(this.callback);
  529. },
  530. remove: function() {
  531. chrome.contextMenus.onClicked.removeListener(this.callback);
  532. chrome.contextMenus.remove(this.menuId);
  533. }
  534. };
  535. /******************************************************************************/
  536. vAPI.lastError = function() {
  537. return chrome.runtime.lastError;
  538. };
  539. /******************************************************************************/
  540. /******************************************************************************/
  541. // This is called only once, when everything has been loaded in memory after
  542. // the extension was launched. It can be used to inject content scripts
  543. // in already opened web pages, to remove whatever nuisance could make it to
  544. // the web pages before uBlock was ready.
  545. vAPI.onLoadAllCompleted = function() {
  546. };
  547. /******************************************************************************/
  548. /******************************************************************************/
  549. vAPI.punycodeHostname = function(hostname) {
  550. return hostname;
  551. };
  552. vAPI.punycodeURL = function(url) {
  553. return url;
  554. };
  555. /******************************************************************************/
  556. /******************************************************************************/
  557. vAPI.browserData = {};
  558. /******************************************************************************/
  559. // https://developer.chrome.com/extensions/browsingData
  560. vAPI.browserData.clearCache = function(callback) {
  561. chrome.browsingData.removeCache({ since: 0 }, callback);
  562. };
  563. /******************************************************************************/
  564. // Not supported on Chromium
  565. vAPI.browserData.clearOrigin = function(domain, callback) {
  566. // unsupported on Chromium
  567. if ( typeof callback === 'function' ) {
  568. callback(undefined);
  569. }
  570. };
  571. /******************************************************************************/
  572. /******************************************************************************/
  573. // https://developer.chrome.com/extensions/cookies
  574. vAPI.cookies = {};
  575. /******************************************************************************/
  576. vAPI.cookies.start = function() {
  577. var reallyRemoved = {
  578. 'evicted': true,
  579. 'expired': true,
  580. 'explicit': true
  581. };
  582. var onChanged = function(changeInfo) {
  583. if ( changeInfo.removed ) {
  584. if ( reallyRemoved[changeInfo.cause] && typeof this.onRemoved === 'function' ) {
  585. this.onRemoved(changeInfo.cookie);
  586. }
  587. return;
  588. }
  589. if ( typeof this.onChanged === 'function' ) {
  590. this.onChanged(changeInfo.cookie);
  591. }
  592. };
  593. chrome.cookies.onChanged.addListener(onChanged.bind(this));
  594. };
  595. /******************************************************************************/
  596. vAPI.cookies.getAll = function(callback) {
  597. chrome.cookies.getAll({}, callback);
  598. };
  599. /******************************************************************************/
  600. vAPI.cookies.remove = function(details, callback) {
  601. chrome.cookies.remove(details, callback || noopFunc);
  602. };
  603. /******************************************************************************/
  604. /******************************************************************************/
  605. vAPI.cloud = (function() {
  606. // Not all platforms support `chrome.storage.sync`.
  607. if ( chrome.storage.sync instanceof Object === false ) {
  608. return;
  609. }
  610. var chunkCountPerFetch = 16; // Must be a power of 2
  611. // Mind chrome.storage.sync.MAX_ITEMS (512 at time of writing)
  612. var maxChunkCountPerItem = Math.floor(512 * 0.75) & ~(chunkCountPerFetch - 1);
  613. // Mind chrome.storage.sync.QUOTA_BYTES_PER_ITEM (8192 at time of writing)
  614. var maxChunkSize = chrome.storage.sync.QUOTA_BYTES_PER_ITEM || 8192;
  615. // Mind chrome.storage.sync.QUOTA_BYTES (128 kB at time of writing)
  616. // Firefox:
  617. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/sync
  618. // > You can store up to 100KB of data using this API/
  619. var maxStorageSize = chrome.storage.sync.QUOTA_BYTES || 102400;
  620. // Flavor-specific handling needs to be done here. Reason: to allow time
  621. // for vAPI.webextFlavor to be properly set.
  622. // https://github.com/gorhill/uBlock/issues/3006
  623. // For Firefox, we will use a lower ratio to allow for more overhead for
  624. // the infrastructure. Unfortunately this leads to less usable space for
  625. // actual data, but all of this is provided for free by browser vendors,
  626. // so we need to accept and deal with these limitations.
  627. var initialize = function() {
  628. var ratio =
  629. vAPI.webextFlavor === undefined ||
  630. vAPI.webextFlavor.startsWith('Mozilla-Firefox-') ?
  631. 0.6 :
  632. 0.75;
  633. maxChunkSize = Math.floor(maxChunkSize * ratio);
  634. initialize = function(){};
  635. };
  636. var options = {
  637. defaultDeviceName: window.navigator.platform,
  638. deviceName: vAPI.localStorage.getItem('deviceName') || ''
  639. };
  640. // This is used to find out a rough count of how many chunks exists:
  641. // We "poll" at specific index in order to get a rough idea of how
  642. // large is the stored string.
  643. // This allows reading a single item with only 2 sync operations -- a
  644. // good thing given chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE
  645. // and chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR.
  646. var getCoarseChunkCount = function(dataKey, callback) {
  647. var bin = {};
  648. for ( var i = 0; i < maxChunkCountPerItem; i += 16 ) {
  649. bin[dataKey + i.toString()] = '';
  650. }
  651. chrome.storage.sync.get(bin, function(bin) {
  652. if ( chrome.runtime.lastError ) {
  653. callback(0, chrome.runtime.lastError.message);
  654. return;
  655. }
  656. var chunkCount = 0;
  657. for ( var i = 0; i < maxChunkCountPerItem; i += 16 ) {
  658. if ( bin[dataKey + i.toString()] === '' ) {
  659. break;
  660. }
  661. chunkCount = i + 16;
  662. }
  663. callback(chunkCount);
  664. });
  665. };
  666. var deleteChunks = function(dataKey, start) {
  667. var keys = [];
  668. // No point in deleting more than:
  669. // - The max number of chunks per item
  670. // - The max number of chunks per storage limit
  671. var n = Math.min(
  672. maxChunkCountPerItem,
  673. Math.ceil(maxStorageSize / maxChunkSize)
  674. );
  675. for ( var i = start; i < n; i++ ) {
  676. keys.push(dataKey + i.toString());
  677. }
  678. chrome.storage.sync.remove(keys);
  679. };
  680. var start = function(/* dataKeys */) {
  681. };
  682. var push = function(dataKey, data, callback) {
  683. initialize();
  684. var bin = {
  685. 'source': options.deviceName || options.defaultDeviceName,
  686. 'tstamp': Date.now(),
  687. 'data': data,
  688. 'size': 0
  689. };
  690. bin.size = JSON.stringify(bin).length;
  691. var item = JSON.stringify(bin);
  692. // Chunkify taking into account QUOTA_BYTES_PER_ITEM:
  693. // https://developer.chrome.com/extensions/storage#property-sync
  694. // "The maximum size (in bytes) of each individual item in sync
  695. // "storage, as measured by the JSON stringification of its value
  696. // "plus its key length."
  697. bin = {};
  698. var chunkCount = Math.ceil(item.length / maxChunkSize);
  699. for ( var i = 0; i < chunkCount; i++ ) {
  700. bin[dataKey + i.toString()] = item.substr(i * maxChunkSize, maxChunkSize);
  701. }
  702. bin[dataKey + i.toString()] = ''; // Sentinel
  703. chrome.storage.sync.set(bin, function() {
  704. var errorStr;
  705. if ( chrome.runtime.lastError ) {
  706. errorStr = chrome.runtime.lastError.message;
  707. // https://github.com/gorhill/uBlock/issues/3006#issuecomment-332597677
  708. // - Delete all that was pushed in case of failure.
  709. chunkCount = 0;
  710. }
  711. callback(errorStr);
  712. // Remove potentially unused trailing chunks
  713. deleteChunks(dataKey, chunkCount);
  714. });
  715. };
  716. var pull = function(dataKey, callback) {
  717. initialize();
  718. var assembleChunks = function(bin) {
  719. if ( chrome.runtime.lastError ) {
  720. callback(null, chrome.runtime.lastError.message);
  721. return;
  722. }
  723. // Assemble chunks into a single string.
  724. var json = [], jsonSlice;
  725. var i = 0;
  726. for (;;) {
  727. jsonSlice = bin[dataKey + i.toString()];
  728. if ( jsonSlice === '' ) {
  729. break;
  730. }
  731. json.push(jsonSlice);
  732. i += 1;
  733. }
  734. var entry = null;
  735. try {
  736. entry = JSON.parse(json.join(''));
  737. } catch(ex) {
  738. }
  739. callback(entry);
  740. };
  741. var fetchChunks = function(coarseCount, errorStr) {
  742. if ( coarseCount === 0 || typeof errorStr === 'string' ) {
  743. callback(null, errorStr);
  744. return;
  745. }
  746. var bin = {};
  747. for ( var i = 0; i < coarseCount; i++ ) {
  748. bin[dataKey + i.toString()] = '';
  749. }
  750. chrome.storage.sync.get(bin, assembleChunks);
  751. };
  752. getCoarseChunkCount(dataKey, fetchChunks);
  753. };
  754. var getOptions = function(callback) {
  755. if ( typeof callback !== 'function' ) {
  756. return;
  757. }
  758. callback(options);
  759. };
  760. var setOptions = function(details, callback) {
  761. if ( typeof details !== 'object' || details === null ) {
  762. return;
  763. }
  764. if ( typeof details.deviceName === 'string' ) {
  765. vAPI.localStorage.setItem('deviceName', details.deviceName);
  766. options.deviceName = details.deviceName;
  767. }
  768. getOptions(callback);
  769. };
  770. return {
  771. start: start,
  772. push: push,
  773. pull: pull,
  774. getOptions: getOptions,
  775. setOptions: setOptions
  776. };
  777. })();
  778. /******************************************************************************/
  779. /******************************************************************************/
  780. })();
  781. /******************************************************************************/