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.

958 lines
30 KiB

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