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.

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