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.

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