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.

917 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. // 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. /******************************************************************************/
  402. vAPI.net.registerListeners = function() {
  403. var µm = µMatrix;
  404. // Normalizing request types
  405. // >>>>>>>>
  406. var extToTypeMap = new Map([
  407. ['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
  408. ['mp3','media'],['mp4','media'],['webm','media'],
  409. ['gif','image'],['ico','image'],['jpeg','image'],['jpg','image'],['png','image'],['webp','image']
  410. ]);
  411. var normalizeRequestDetails = function(details) {
  412. if (
  413. details.tabId === -1 &&
  414. details.documentUrl === undefined &&
  415. details.initiator !== undefined
  416. ) {
  417. details.documentUrl = details.initiator;
  418. }
  419. // The rest of the function code is to normalize request type
  420. if ( details.type !== 'other' ) { return; }
  421. // Try to map known "extension" part of URL to request type.
  422. var path = µm.URI.pathFromURI(details.url),
  423. pos = path.indexOf('.', path.length - 6);
  424. if ( pos !== -1 ) {
  425. var type = extToTypeMap.get(path.slice(pos + 1));
  426. if ( type !== undefined ) {
  427. details.type = type;
  428. }
  429. }
  430. };
  431. // <<<<<<<<
  432. // End of: Normalizing request types
  433. // Network event handlers
  434. // >>>>>>>>
  435. var onBeforeRequestClient = this.onBeforeRequest.callback;
  436. chrome.webRequest.onBeforeRequest.addListener(
  437. function(details) {
  438. normalizeRequestDetails(details);
  439. return onBeforeRequestClient(details);
  440. },
  441. {
  442. 'urls': this.onBeforeRequest.urls || [ '<all_urls>' ],
  443. 'types': this.onBeforeRequest.types || undefined
  444. },
  445. this.onBeforeRequest.extra
  446. );
  447. var onBeforeSendHeadersClient = this.onBeforeSendHeaders.callback;
  448. var onBeforeSendHeaders = function(details) {
  449. normalizeRequestDetails(details);
  450. return onBeforeSendHeadersClient(details);
  451. };
  452. chrome.webRequest.onBeforeSendHeaders.addListener(
  453. onBeforeSendHeaders,
  454. {
  455. 'urls': this.onBeforeSendHeaders.urls || [ '<all_urls>' ],
  456. 'types': this.onBeforeSendHeaders.types || undefined
  457. },
  458. this.onBeforeSendHeaders.extra
  459. );
  460. var onHeadersReceivedClient = this.onHeadersReceived.callback;
  461. var onHeadersReceived = function(details) {
  462. normalizeRequestDetails(details);
  463. return onHeadersReceivedClient(details);
  464. };
  465. chrome.webRequest.onHeadersReceived.addListener(
  466. onHeadersReceived,
  467. {
  468. 'urls': this.onHeadersReceived.urls || [ '<all_urls>' ],
  469. 'types': this.onHeadersReceived.types || undefined
  470. },
  471. this.onHeadersReceived.extra
  472. );
  473. // <<<<<<<<
  474. // End of: Network event handlers
  475. };
  476. /******************************************************************************/
  477. /******************************************************************************/
  478. vAPI.contextMenu = {
  479. create: function(details, callback) {
  480. this.menuId = details.id;
  481. this.callback = callback;
  482. chrome.contextMenus.create(details);
  483. chrome.contextMenus.onClicked.addListener(this.callback);
  484. },
  485. remove: function() {
  486. chrome.contextMenus.onClicked.removeListener(this.callback);
  487. chrome.contextMenus.remove(this.menuId);
  488. }
  489. };
  490. /******************************************************************************/
  491. vAPI.lastError = function() {
  492. return chrome.runtime.lastError;
  493. };
  494. /******************************************************************************/
  495. /******************************************************************************/
  496. // This is called only once, when everything has been loaded in memory after
  497. // the extension was launched. It can be used to inject content scripts
  498. // in already opened web pages, to remove whatever nuisance could make it to
  499. // the web pages before uBlock was ready.
  500. vAPI.onLoadAllCompleted = function() {
  501. };
  502. /******************************************************************************/
  503. /******************************************************************************/
  504. vAPI.punycodeHostname = function(hostname) {
  505. return hostname;
  506. };
  507. vAPI.punycodeURL = function(url) {
  508. return url;
  509. };
  510. /******************************************************************************/
  511. /******************************************************************************/
  512. vAPI.browserData = {};
  513. /******************************************************************************/
  514. // https://developer.chrome.com/extensions/browsingData
  515. vAPI.browserData.clearCache = function(callback) {
  516. chrome.browsingData.removeCache({ since: 0 }, callback);
  517. };
  518. /******************************************************************************/
  519. // Not supported on Chromium
  520. vAPI.browserData.clearOrigin = function(domain, callback) {
  521. // unsupported on Chromium
  522. if ( typeof callback === 'function' ) {
  523. callback(undefined);
  524. }
  525. };
  526. /******************************************************************************/
  527. /******************************************************************************/
  528. // https://developer.chrome.com/extensions/cookies
  529. vAPI.cookies = {};
  530. /******************************************************************************/
  531. vAPI.cookies.start = function() {
  532. var reallyRemoved = {
  533. 'evicted': true,
  534. 'expired': true,
  535. 'explicit': true
  536. };
  537. var onChanged = function(changeInfo) {
  538. if ( changeInfo.removed ) {
  539. if ( reallyRemoved[changeInfo.cause] && typeof this.onRemoved === 'function' ) {
  540. this.onRemoved(changeInfo.cookie);
  541. }
  542. return;
  543. }
  544. if ( typeof this.onChanged === 'function' ) {
  545. this.onChanged(changeInfo.cookie);
  546. }
  547. };
  548. chrome.cookies.onChanged.addListener(onChanged.bind(this));
  549. };
  550. /******************************************************************************/
  551. vAPI.cookies.getAll = function(callback) {
  552. chrome.cookies.getAll({}, callback);
  553. };
  554. /******************************************************************************/
  555. vAPI.cookies.remove = function(details, callback) {
  556. chrome.cookies.remove(details, callback || noopFunc);
  557. };
  558. /******************************************************************************/
  559. /******************************************************************************/
  560. vAPI.cloud = (function() {
  561. // Not all platforms support `chrome.storage.sync`.
  562. if ( chrome.storage.sync instanceof Object === false ) {
  563. return;
  564. }
  565. var chunkCountPerFetch = 16; // Must be a power of 2
  566. // Mind chrome.storage.sync.MAX_ITEMS (512 at time of writing)
  567. var maxChunkCountPerItem = Math.floor(512 * 0.75) & ~(chunkCountPerFetch - 1);
  568. // Mind chrome.storage.sync.QUOTA_BYTES_PER_ITEM (8192 at time of writing)
  569. // https://github.com/gorhill/uBlock/issues/3006
  570. // For Firefox, we will use a lower ratio to allow for more overhead for
  571. // the infrastructure. Unfortunately this leads to less usable space for
  572. // actual data, but all of this is provided for free by browser vendors,
  573. // so we need to accept and deal with these limitations.
  574. var evalMaxChunkSize = function() {
  575. return Math.floor(
  576. (chrome.storage.sync.QUOTA_BYTES_PER_ITEM || 8192) *
  577. (vAPI.webextFlavor.soup.has('firefox') ? 0.6 : 0.75)
  578. );
  579. };
  580. var maxChunkSize = evalMaxChunkSize();
  581. // The real actual webextFlavor value may not be set in stone, so listen
  582. // for possible future changes.
  583. window.addEventListener('webextFlavor', function() {
  584. maxChunkSize = evalMaxChunkSize();
  585. }, { once: true });
  586. // Mind chrome.storage.sync.QUOTA_BYTES (128 kB at time of writing)
  587. // Firefox:
  588. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/sync
  589. // > You can store up to 100KB of data using this API/
  590. var maxStorageSize = chrome.storage.sync.QUOTA_BYTES || 102400;
  591. var options = {
  592. defaultDeviceName: window.navigator.platform,
  593. deviceName: vAPI.localStorage.getItem('deviceName') || ''
  594. };
  595. // This is used to find out a rough count of how many chunks exists:
  596. // We "poll" at specific index in order to get a rough idea of how
  597. // large is the stored string.
  598. // This allows reading a single item with only 2 sync operations -- a
  599. // good thing given chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE
  600. // and chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR.
  601. var getCoarseChunkCount = function(dataKey, callback) {
  602. var bin = {};
  603. for ( var i = 0; i < maxChunkCountPerItem; i += 16 ) {
  604. bin[dataKey + i.toString()] = '';
  605. }
  606. chrome.storage.sync.get(bin, function(bin) {
  607. if ( chrome.runtime.lastError ) {
  608. callback(0, chrome.runtime.lastError.message);
  609. return;
  610. }
  611. var chunkCount = 0;
  612. for ( var i = 0; i < maxChunkCountPerItem; i += 16 ) {
  613. if ( bin[dataKey + i.toString()] === '' ) {
  614. break;
  615. }
  616. chunkCount = i + 16;
  617. }
  618. callback(chunkCount);
  619. });
  620. };
  621. var deleteChunks = function(dataKey, start) {
  622. var keys = [];
  623. // No point in deleting more than:
  624. // - The max number of chunks per item
  625. // - The max number of chunks per storage limit
  626. var n = Math.min(
  627. maxChunkCountPerItem,
  628. Math.ceil(maxStorageSize / maxChunkSize)
  629. );
  630. for ( var i = start; i < n; i++ ) {
  631. keys.push(dataKey + i.toString());
  632. }
  633. chrome.storage.sync.remove(keys);
  634. };
  635. var start = function(/* dataKeys */) {
  636. };
  637. var push = function(dataKey, data, callback) {
  638. var bin = {
  639. 'source': options.deviceName || options.defaultDeviceName,
  640. 'tstamp': Date.now(),
  641. 'data': data,
  642. 'size': 0
  643. };
  644. bin.size = JSON.stringify(bin).length;
  645. var item = JSON.stringify(bin);
  646. // Chunkify taking into account QUOTA_BYTES_PER_ITEM:
  647. // https://developer.chrome.com/extensions/storage#property-sync
  648. // "The maximum size (in bytes) of each individual item in sync
  649. // "storage, as measured by the JSON stringification of its value
  650. // "plus its key length."
  651. bin = {};
  652. var chunkCount = Math.ceil(item.length / maxChunkSize);
  653. for ( var i = 0; i < chunkCount; i++ ) {
  654. bin[dataKey + i.toString()] = item.substr(i * maxChunkSize, maxChunkSize);
  655. }
  656. bin[dataKey + i.toString()] = ''; // Sentinel
  657. chrome.storage.sync.set(bin, function() {
  658. var errorStr;
  659. if ( chrome.runtime.lastError ) {
  660. errorStr = chrome.runtime.lastError.message;
  661. // https://github.com/gorhill/uBlock/issues/3006#issuecomment-332597677
  662. // - Delete all that was pushed in case of failure.
  663. chunkCount = 0;
  664. }
  665. callback(errorStr);
  666. // Remove potentially unused trailing chunks
  667. deleteChunks(dataKey, chunkCount);
  668. });
  669. };
  670. var pull = function(dataKey, callback) {
  671. var assembleChunks = function(bin) {
  672. if ( chrome.runtime.lastError ) {
  673. callback(null, chrome.runtime.lastError.message);
  674. return;
  675. }
  676. // Assemble chunks into a single string.
  677. var json = [], jsonSlice;
  678. var i = 0;
  679. for (;;) {
  680. jsonSlice = bin[dataKey + i.toString()];
  681. if ( jsonSlice === '' ) {
  682. break;
  683. }
  684. json.push(jsonSlice);
  685. i += 1;
  686. }
  687. var entry = null;
  688. try {
  689. entry = JSON.parse(json.join(''));
  690. } catch(ex) {
  691. }
  692. callback(entry);
  693. };
  694. var fetchChunks = function(coarseCount, errorStr) {
  695. if ( coarseCount === 0 || typeof errorStr === 'string' ) {
  696. callback(null, errorStr);
  697. return;
  698. }
  699. var bin = {};
  700. for ( var i = 0; i < coarseCount; i++ ) {
  701. bin[dataKey + i.toString()] = '';
  702. }
  703. chrome.storage.sync.get(bin, assembleChunks);
  704. };
  705. getCoarseChunkCount(dataKey, fetchChunks);
  706. };
  707. var getOptions = function(callback) {
  708. if ( typeof callback !== 'function' ) {
  709. return;
  710. }
  711. callback(options);
  712. };
  713. var setOptions = function(details, callback) {
  714. if ( typeof details !== 'object' || details === null ) {
  715. return;
  716. }
  717. if ( typeof details.deviceName === 'string' ) {
  718. vAPI.localStorage.setItem('deviceName', details.deviceName);
  719. options.deviceName = details.deviceName;
  720. }
  721. getOptions(callback);
  722. };
  723. return {
  724. start: start,
  725. push: push,
  726. pull: pull,
  727. getOptions: getOptions,
  728. setOptions: setOptions
  729. };
  730. })();
  731. /******************************************************************************/
  732. /******************************************************************************/
  733. })();
  734. /******************************************************************************/