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.

1649 lines
57 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
7 years ago
10 years ago
10 years ago
10 years ago
10 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. {
  21. // >>>>> start of local scope
  22. /******************************************************************************/
  23. /******************************************************************************/
  24. const browser = self.browser;
  25. const manifest = browser.runtime.getManifest();
  26. vAPI.cantWebsocket =
  27. browser.webRequest.ResourceType instanceof Object === false ||
  28. browser.webRequest.ResourceType.WEBSOCKET !== 'websocket';
  29. vAPI.canWASM = vAPI.webextFlavor.soup.has('chromium') === false;
  30. if ( vAPI.canWASM === false ) {
  31. const csp = manifest.content_security_policy;
  32. vAPI.canWASM = csp !== undefined && csp.indexOf("'wasm-eval'") !== -1;
  33. }
  34. vAPI.supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
  35. // The real actual webextFlavor value may not be set in stone, so listen
  36. // for possible future changes.
  37. window.addEventListener('webextFlavor', function() {
  38. vAPI.supportsUserStylesheets =
  39. vAPI.webextFlavor.soup.has('user_stylesheet');
  40. }, { once: true });
  41. /******************************************************************************/
  42. vAPI.app = {
  43. name: manifest.name.replace(/ dev\w+ build/, ''),
  44. version: (( ) => {
  45. let version = manifest.version;
  46. const match = /(\d+\.\d+\.\d+)(?:\.(\d+))?/.exec(version);
  47. if ( match && match[2] ) {
  48. const v = parseInt(match[2], 10);
  49. version = match[1] + (v < 100 ? 'b' + v : 'rc' + (v - 100));
  50. }
  51. return version;
  52. })(),
  53. intFromVersion: function(s) {
  54. const parts = s.match(/(?:^|\.|b|rc)\d+/g);
  55. if ( parts === null ) { return 0; }
  56. let vint = 0;
  57. for ( let i = 0; i < 4; i++ ) {
  58. const pstr = parts[i] || '';
  59. let pint;
  60. if ( pstr === '' ) {
  61. pint = 0;
  62. } else if ( pstr.startsWith('.') || pstr.startsWith('b') ) {
  63. pint = parseInt(pstr.slice(1), 10);
  64. } else if ( pstr.startsWith('rc') ) {
  65. pint = parseInt(pstr.slice(2), 10) + 100;
  66. } else {
  67. pint = parseInt(pstr, 10);
  68. }
  69. vint = vint * 1000 + pint;
  70. }
  71. return vint;
  72. },
  73. restart: function() {
  74. browser.runtime.reload();
  75. },
  76. };
  77. /******************************************************************************/
  78. /******************************************************************************/
  79. vAPI.storage = webext.storage.local;
  80. /******************************************************************************/
  81. /******************************************************************************/
  82. // https://github.com/gorhill/uMatrix/issues/234
  83. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network
  84. // https://github.com/gorhill/uBlock/issues/2048
  85. // Do not mess up with existing settings if not assigning them stricter
  86. // values.
  87. vAPI.browserSettings = (( ) => {
  88. // Not all platforms support `browser.privacy`.
  89. const bp = webext.privacy;
  90. if ( bp instanceof Object === false ) { return; }
  91. return {
  92. // Whether the WebRTC-related privacy API is crashy is an open question
  93. // only for Chromium proper (because it can be compiled without the
  94. // WebRTC feature): hence avoid overhead of the evaluation (which uses
  95. // an iframe) for platforms where it's a non-issue.
  96. // https://github.com/uBlockOrigin/uBlock-issues/issues/9
  97. // Some Chromium builds are made to look like a Chrome build.
  98. webRTCSupported: vAPI.webextFlavor.soup.has('chromium') === false || undefined,
  99. // Calling with `true` means IP address leak is not prevented.
  100. // https://github.com/gorhill/uBlock/issues/533
  101. // We must first check wether this Chromium-based browser was compiled
  102. // with WebRTC support. To do this, we use an iframe, this way the
  103. // empty RTCPeerConnection object we create to test for support will
  104. // be properly garbage collected. This prevents issues such as
  105. // a computer unable to enter into sleep mode, as reported in the
  106. // Chrome store:
  107. // https://github.com/gorhill/uBlock/issues/533#issuecomment-167931681
  108. setWebrtcIPAddress: function(setting) {
  109. // We don't know yet whether this browser supports WebRTC: find out.
  110. if ( this.webRTCSupported === undefined ) {
  111. // If asked to leave WebRTC setting alone at this point in the
  112. // code, this means we never grabbed the setting in the first
  113. // place.
  114. if ( setting ) { return; }
  115. this.webRTCSupported = { setting: setting };
  116. let iframe = document.createElement('iframe');
  117. const messageHandler = ev => {
  118. if ( ev.origin !== self.location.origin ) { return; }
  119. window.removeEventListener('message', messageHandler);
  120. const setting = this.webRTCSupported.setting;
  121. this.webRTCSupported = ev.data === 'webRTCSupported';
  122. this.setWebrtcIPAddress(setting);
  123. iframe.parentNode.removeChild(iframe);
  124. iframe = null;
  125. };
  126. window.addEventListener('message', messageHandler);
  127. iframe.src = 'is-webrtc-supported.html';
  128. document.body.appendChild(iframe);
  129. return;
  130. }
  131. // We are waiting for a response from our iframe. This makes the code
  132. // safe to re-entrancy.
  133. if ( typeof this.webRTCSupported === 'object' ) {
  134. this.webRTCSupported.setting = setting;
  135. return;
  136. }
  137. // https://github.com/gorhill/uBlock/issues/533
  138. // WebRTC not supported: `webRTCMultipleRoutesEnabled` can NOT be
  139. // safely accessed. Accessing the property will cause full browser
  140. // crash.
  141. if ( this.webRTCSupported !== true ) { return; }
  142. const bpn = bp.network;
  143. if ( setting ) {
  144. bpn.webRTCIPHandlingPolicy.clear({
  145. scope: 'regular',
  146. });
  147. } else {
  148. // https://github.com/uBlockOrigin/uAssets/issues/333#issuecomment-289426678
  149. // Leverage virtuous side-effect of strictest setting.
  150. // https://github.com/gorhill/uBlock/issues/3009
  151. // Firefox currently works differently, use
  152. // `default_public_interface_only` for now.
  153. bpn.webRTCIPHandlingPolicy.set({
  154. value: vAPI.webextFlavor.soup.has('chromium')
  155. ? 'disable_non_proxied_udp'
  156. : 'default_public_interface_only',
  157. scope: 'regular',
  158. });
  159. }
  160. },
  161. set: function(details) {
  162. for ( const setting in details ) {
  163. if ( details.hasOwnProperty(setting) === false ) { continue; }
  164. switch ( setting ) {
  165. case 'prefetching':
  166. const enabled = !!details[setting];
  167. if ( enabled ) {
  168. bp.network.networkPredictionEnabled.clear({
  169. scope: 'regular',
  170. });
  171. } else {
  172. bp.network.networkPredictionEnabled.set({
  173. value: false,
  174. scope: 'regular',
  175. });
  176. }
  177. if ( vAPI.prefetching instanceof Function ) {
  178. vAPI.prefetching(enabled);
  179. }
  180. break;
  181. case 'hyperlinkAuditing':
  182. if ( !!details[setting] ) {
  183. bp.websites.hyperlinkAuditingEnabled.clear({
  184. scope: 'regular',
  185. });
  186. } else {
  187. bp.websites.hyperlinkAuditingEnabled.set({
  188. value: false,
  189. scope: 'regular',
  190. });
  191. }
  192. break;
  193. case 'webrtcIPAddress':
  194. this.setWebrtcIPAddress(!!details[setting]);
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. }
  201. };
  202. })();
  203. /******************************************************************************/
  204. /******************************************************************************/
  205. vAPI.isBehindTheSceneTabId = function(tabId) {
  206. return tabId < 0;
  207. };
  208. vAPI.unsetTabId = 0;
  209. vAPI.noTabId = -1; // definitely not any existing tab
  210. // To ensure we always use a good tab id
  211. const toTabId = function(tabId) {
  212. return typeof tabId === 'number' && isNaN(tabId) === false
  213. ? tabId
  214. : 0;
  215. };
  216. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation
  217. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs
  218. vAPI.Tabs = class {
  219. constructor() {
  220. browser.webNavigation.onCreatedNavigationTarget.addListener(details => {
  221. if ( typeof details.url !== 'string' ) {
  222. details.url = '';
  223. }
  224. if ( /^https?:\/\//.test(details.url) === false ) {
  225. details.frameId = 0;
  226. details.url = this.sanitizeURL(details.url);
  227. this.onNavigation(details);
  228. }
  229. this.onCreated(details);
  230. });
  231. // Ensure the tab id is a valid one before propagating event to
  232. // client code. "Invalid" yab id can occur when a browser chrome
  233. // window is opened; for example, a detached dev tool window.
  234. browser.webNavigation.onCommitted.addListener(async details => {
  235. const tab = await this.get(details.tabId);
  236. if ( tab.id === -1 ) { return; }
  237. details.url = this.sanitizeURL(details.url);
  238. this.onNavigation(details);
  239. });
  240. // https://github.com/gorhill/uBlock/issues/3073
  241. // Fall back to `tab.url` when `changeInfo.url` is not set.
  242. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  243. if ( typeof changeInfo.url !== 'string' ) {
  244. changeInfo.url = tab && tab.url;
  245. }
  246. if ( changeInfo.url ) {
  247. changeInfo.url = this.sanitizeURL(changeInfo.url);
  248. }
  249. this.onUpdated(tabId, changeInfo, tab);
  250. });
  251. browser.tabs.onActivated.addListener(details => {
  252. this.onActivated(details);
  253. });
  254. // https://github.com/uBlockOrigin/uBlock-issues/issues/151
  255. // https://github.com/uBlockOrigin/uBlock-issues/issues/680#issuecomment-515215220
  256. if ( browser.windows instanceof Object ) {
  257. browser.windows.onFocusChanged.addListener(async windowId => {
  258. if ( windowId === browser.windows.WINDOW_ID_NONE ) { return; }
  259. const tabs = await vAPI.tabs.query({ active: true, windowId });
  260. if ( tabs.length === 0 ) { return; }
  261. const tab = tabs[0];
  262. this.onActivated({ tabId: tab.id, windowId: tab.windowId });
  263. });
  264. }
  265. browser.tabs.onRemoved.addListener((tabId, details) => {
  266. this.onClosed(tabId, details);
  267. });
  268. }
  269. async executeScript() {
  270. let result;
  271. try {
  272. result = await webext.tabs.executeScript(...arguments);
  273. }
  274. catch(reason) {
  275. }
  276. return Array.isArray(result) ? result : [];
  277. }
  278. async get(tabId) {
  279. if ( tabId === null ) {
  280. return this.getCurrent();
  281. }
  282. if ( tabId <= 0 ) { return null; }
  283. let tab;
  284. try {
  285. tab = await webext.tabs.get(tabId);
  286. }
  287. catch(reason) {
  288. }
  289. return tab instanceof Object ? tab : null;
  290. }
  291. async getCurrent() {
  292. const tabs = await this.query({ active: true, currentWindow: true });
  293. return tabs.length !== 0 ? tabs[0] : null;
  294. }
  295. async insertCSS() {
  296. try {
  297. await webext.tabs.insertCSS(...arguments);
  298. }
  299. catch(reason) {
  300. }
  301. }
  302. async query(queryInfo) {
  303. let tabs;
  304. try {
  305. tabs = await webext.tabs.query(queryInfo);
  306. }
  307. catch(reason) {
  308. }
  309. return Array.isArray(tabs) ? tabs : [];
  310. }
  311. async removeCSS() {
  312. try {
  313. await webext.tabs.removeCSS(...arguments);
  314. }
  315. catch(reason) {
  316. }
  317. }
  318. // Properties of the details object:
  319. // - url: 'URL', => the address that will be opened
  320. // - index: -1, => undefined: end of the list, -1: following tab,
  321. // or after index
  322. // - active: false, => opens the tab... in background: true,
  323. // foreground: undefined
  324. // - popup: 'popup' => open in a new window
  325. async create(url, details) {
  326. if ( details.active === undefined ) {
  327. details.active = true;
  328. }
  329. const subWrapper = async ( ) => {
  330. const updateDetails = {
  331. url: url,
  332. active: !!details.active
  333. };
  334. // Opening a tab from incognito window won't focus the window
  335. // in which the tab was opened
  336. const focusWindow = tab => {
  337. if ( tab.active && vAPI.windows instanceof Object ) {
  338. vAPI.windows.update(tab.windowId, { focused: true });
  339. }
  340. };
  341. if ( !details.tabId ) {
  342. if ( details.index !== undefined ) {
  343. updateDetails.index = details.index;
  344. }
  345. browser.tabs.create(updateDetails, focusWindow);
  346. return;
  347. }
  348. // update doesn't accept index, must use move
  349. const tab = await vAPI.tabs.update(
  350. toTabId(details.tabId),
  351. updateDetails
  352. );
  353. // if the tab doesn't exist
  354. if ( tab === null ) {
  355. browser.tabs.create(updateDetails, focusWindow);
  356. } else if ( details.index !== undefined ) {
  357. browser.tabs.move(tab.id, { index: details.index });
  358. }
  359. };
  360. // Open in a standalone window
  361. //
  362. // https://github.com/uBlockOrigin/uBlock-issues/issues/168#issuecomment-413038191
  363. // Not all platforms support vAPI.windows.
  364. //
  365. // For some reasons, some platforms do not honor the left,top
  366. // position when specified. I found that further calling
  367. // windows.update again with the same position _may_ help.
  368. if ( details.popup !== undefined && vAPI.windows instanceof Object ) {
  369. const createDetails = { url, type: details.popup };
  370. if ( details.box instanceof Object ) {
  371. Object.assign(createDetails, details.box);
  372. }
  373. const win = await vAPI.windows.create(createDetails);
  374. if ( win === null ) { return; }
  375. if ( details.box instanceof Object === false ) { return; }
  376. if (
  377. win.left === details.box.left &&
  378. win.top === details.box.top
  379. ) {
  380. return;
  381. }
  382. vAPI.windows.update(win.id, {
  383. left: details.box.left,
  384. top: details.box.top
  385. });
  386. return;
  387. }
  388. if ( details.index !== -1 ) {
  389. subWrapper();
  390. return;
  391. }
  392. const tab = await vAPI.tabs.getCurrent();
  393. if ( tab !== null ) {
  394. details.index = tab.index + 1;
  395. } else {
  396. details.index = undefined;
  397. }
  398. subWrapper();
  399. }
  400. // Properties of the details object:
  401. // - url: 'URL', => the address that will be opened
  402. // - tabId: 1, => the tab is used if set, instead of creating a new one
  403. // - index: -1, => undefined: end of the list, -1: following tab, or
  404. // after index
  405. // - active: false, => opens the tab in background - true and undefined:
  406. // foreground
  407. // - select: true, => if a tab is already opened with that url, then select
  408. // it instead of opening a new one
  409. // - popup: true => open in a new window
  410. async open(details) {
  411. let targetURL = details.url;
  412. if ( typeof targetURL !== 'string' || targetURL === '' ) {
  413. return null;
  414. }
  415. // extension pages
  416. if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
  417. targetURL = vAPI.getURL(targetURL);
  418. }
  419. if ( !details.select ) {
  420. this.create(targetURL, details);
  421. return;
  422. }
  423. // https://github.com/gorhill/uBlock/issues/3053#issuecomment-332276818
  424. // Do not try to lookup uBO's own pages with FF 55 or less.
  425. if (
  426. vAPI.webextFlavor.soup.has('firefox') &&
  427. vAPI.webextFlavor.major < 56
  428. ) {
  429. this.create(targetURL, details);
  430. return;
  431. }
  432. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/query#Parameters
  433. // "Note that fragment identifiers are not matched."
  434. // Fragment identifiers ARE matched -- we need to remove the fragment.
  435. const pos = targetURL.indexOf('#');
  436. const targetURLWithoutHash = pos === -1
  437. ? targetURL
  438. : targetURL.slice(0, pos);
  439. const tabs = await vAPI.tabs.query({ url: targetURLWithoutHash });
  440. if ( tabs.length === 0 ) {
  441. this.create(targetURL, details);
  442. return;
  443. }
  444. let tab = tabs[0];
  445. const updateDetails = { active: true };
  446. // https://github.com/uBlockOrigin/uBlock-issues/issues/592
  447. if ( tab.url.startsWith(targetURL) === false ) {
  448. updateDetails.url = targetURL;
  449. }
  450. tab = await vAPI.tabs.update(tab.id, updateDetails);
  451. if ( vAPI.windows instanceof Object === false ) { return; }
  452. vAPI.windows.update(tab.windowId, { focused: true });
  453. }
  454. async update() {
  455. let tab;
  456. try {
  457. tab = await webext.tabs.update(...arguments);
  458. }
  459. catch (reason) {
  460. }
  461. return tab instanceof Object ? tab : null;
  462. }
  463. // Replace the URL of a tab. Noop if the tab does not exist.
  464. replace(tabId, url) {
  465. tabId = toTabId(tabId);
  466. if ( tabId === 0 ) { return; }
  467. let targetURL = url;
  468. // extension pages
  469. if ( /^[\w-]{2,}:/.test(targetURL) !== true ) {
  470. targetURL = vAPI.getURL(targetURL);
  471. }
  472. vAPI.tabs.update(tabId, { url: targetURL });
  473. }
  474. async remove(tabId) {
  475. tabId = toTabId(tabId);
  476. if ( tabId === 0 ) { return; }
  477. try {
  478. await webext.tabs.remove(tabId);
  479. }
  480. catch (reason) {
  481. }
  482. }
  483. async reload(tabId, bypassCache = false) {
  484. tabId = toTabId(tabId);
  485. if ( tabId === 0 ) { return; }
  486. try {
  487. await webext.tabs.reload(
  488. tabId,
  489. { bypassCache: bypassCache === true }
  490. );
  491. }
  492. catch (reason) {
  493. }
  494. }
  495. async select(tabId) {
  496. tabId = toTabId(tabId);
  497. if ( tabId === 0 ) { return; }
  498. const tab = await vAPI.tabs.update(tabId, { active: true });
  499. if ( tab === null ) { return; }
  500. if ( vAPI.windows instanceof Object === false ) { return; }
  501. vAPI.windows.update(tab.windowId, { focused: true });
  502. }
  503. // https://forums.lanik.us/viewtopic.php?f=62&t=32826
  504. // Chromium-based browsers: sanitize target URL. I've seen data: URI with
  505. // newline characters in standard fields, possibly as a way of evading
  506. // filters. As per spec, there should be no whitespaces in a data: URI's
  507. // standard fields.
  508. sanitizeURL(url) {
  509. if ( url.startsWith('data:') === false ) { return url; }
  510. const pos = url.indexOf(',');
  511. if ( pos === -1 ) { return url; }
  512. const s = url.slice(0, pos);
  513. if ( s.search(/\s/) === -1 ) { return url; }
  514. return s.replace(/\s+/, '') + url.slice(pos);
  515. }
  516. onActivated(/* details */) {
  517. }
  518. onClosed(/* tabId, details */) {
  519. }
  520. onCreated(/* details */) {
  521. }
  522. onNavigation(/* details */) {
  523. }
  524. onUpdated(/* tabId, changeInfo, tab */) {
  525. }
  526. };
  527. /******************************************************************************/
  528. /******************************************************************************/
  529. if ( webext.windows instanceof Object ) {
  530. vAPI.windows = {
  531. get: async function() {
  532. let win;
  533. try {
  534. win = await webext.windows.get(...arguments);
  535. }
  536. catch (reason) {
  537. }
  538. return win instanceof Object ? win : null;
  539. },
  540. create: async function() {
  541. let win;
  542. try {
  543. win = await webext.windows.create(...arguments);
  544. }
  545. catch (reason) {
  546. }
  547. return win instanceof Object ? win : null;
  548. },
  549. update: async function() {
  550. let win;
  551. try {
  552. win = await webext.windows.update(...arguments);
  553. }
  554. catch (reason) {
  555. }
  556. return win instanceof Object ? win : null;
  557. },
  558. };
  559. }
  560. /******************************************************************************/
  561. /******************************************************************************/
  562. if ( webext.browserAction instanceof Object ) {
  563. vAPI.browserAction = {
  564. setTitle: async function() {
  565. try {
  566. await webext.browserAction.setTitle(...arguments);
  567. }
  568. catch (reason) {
  569. }
  570. },
  571. };
  572. // Not supported on Firefox for Android
  573. if ( webext.browserAction.setIcon ) {
  574. vAPI.browserAction.setBadgeTextColor = async function() {
  575. try {
  576. await webext.browserAction.setBadgeTextColor(...arguments);
  577. }
  578. catch (reason) {
  579. }
  580. };
  581. vAPI.browserAction.setBadgeBackgroundColor = async function() {
  582. try {
  583. await webext.browserAction.setBadgeBackgroundColor(...arguments);
  584. }
  585. catch (reason) {
  586. }
  587. };
  588. vAPI.browserAction.setBadgeText = async function() {
  589. try {
  590. await webext.browserAction.setBadgeText(...arguments);
  591. }
  592. catch (reason) {
  593. }
  594. };
  595. vAPI.browserAction.setIcon = async function() {
  596. try {
  597. await webext.browserAction.setIcon(...arguments);
  598. }
  599. catch (reason) {
  600. }
  601. };
  602. }
  603. }
  604. /******************************************************************************/
  605. /******************************************************************************/
  606. // Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
  607. // https://github.com/chrisaljoudi/uBlock/issues/19
  608. // https://github.com/chrisaljoudi/uBlock/issues/207
  609. // Since we may be called asynchronously, the tab id may not exist
  610. // anymore, so this ensures it does still exist.
  611. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/browserAction#Browser_compatibility
  612. // Firefox for Android does no support browser.browserAction.setIcon().
  613. // Performance: use ImageData for platforms supporting it.
  614. // https://github.com/uBlockOrigin/uBlock-issues/issues/32
  615. // Ensure ImageData for toolbar icon is valid before use.
  616. vAPI.setIcon = (( ) => {
  617. const browserAction = vAPI.browserAction;
  618. const titleTemplate =
  619. browser.runtime.getManifest().browser_action.default_title +
  620. ' ({badge})';
  621. const icons = [
  622. { path: { '16': 'img/icon_16-off.png', '32': 'img/icon_32-off.png' } },
  623. { path: { '16': 'img/icon_16.png', '32': 'img/icon_32.png' } },
  624. ];
  625. (( ) => {
  626. if ( browserAction.setIcon === undefined ) { return; }
  627. // The global badge text and background color.
  628. if ( browserAction.setBadgeBackgroundColor !== undefined ) {
  629. browserAction.setBadgeBackgroundColor({ color: '#666666' });
  630. }
  631. if ( browserAction.setBadgeTextColor !== undefined ) {
  632. browserAction.setBadgeTextColor({ color: '#FFFFFF' });
  633. }
  634. // As of 2018-05, benchmarks show that only Chromium benefits for sure
  635. // from using ImageData.
  636. //
  637. // Chromium creates a new ImageData instance every call to setIcon
  638. // with paths:
  639. // https://cs.chromium.org/chromium/src/extensions/renderer/resources/set_icon.js?l=56&rcl=99be185c25738437ecfa0dafba72a26114196631
  640. //
  641. // Firefox uses an internal cache for each setIcon's paths:
  642. // https://searchfox.org/mozilla-central/rev/5ff2d7683078c96e4b11b8a13674daded935aa44/browser/components/extensions/parent/ext-browserAction.js#631
  643. if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; }
  644. const imgs = [];
  645. for ( let i = 0; i < icons.length; i++ ) {
  646. const path = icons[i].path;
  647. for ( const key in path ) {
  648. if ( path.hasOwnProperty(key) === false ) { continue; }
  649. imgs.push({ i: i, p: key });
  650. }
  651. }
  652. // https://github.com/uBlockOrigin/uBlock-issues/issues/296
  653. const safeGetImageData = function(ctx, w, h) {
  654. let data;
  655. try {
  656. data = ctx.getImageData(0, 0, w, h);
  657. } catch(ex) {
  658. }
  659. return data;
  660. };
  661. const onLoaded = function() {
  662. for ( const img of imgs ) {
  663. if ( img.r.complete === false ) { return; }
  664. if ( img.r.naturalWidth === 0 ) { return; }
  665. if ( img.r.naturalHeight === 0 ) { return; }
  666. }
  667. const ctx = document.createElement('canvas').getContext('2d');
  668. const iconData = [ null, null ];
  669. for ( const img of imgs ) {
  670. const w = img.r.naturalWidth, h = img.r.naturalHeight;
  671. ctx.width = w; ctx.height = h;
  672. ctx.clearRect(0, 0, w, h);
  673. ctx.drawImage(img.r, 0, 0);
  674. if ( iconData[img.i] === null ) { iconData[img.i] = {}; }
  675. const imgData = safeGetImageData(ctx, w, h);
  676. if (
  677. imgData instanceof Object === false ||
  678. imgData.data instanceof Uint8ClampedArray === false ||
  679. imgData.data[0] !== 0 ||
  680. imgData.data[1] !== 0 ||
  681. imgData.data[2] !== 0 ||
  682. imgData.data[3] !== 0
  683. ) {
  684. return;
  685. }
  686. iconData[img.i][img.p] = imgData;
  687. }
  688. for ( let i = 0; i < iconData.length; i++ ) {
  689. if ( iconData[i] ) {
  690. icons[i] = { imageData: iconData[i] };
  691. }
  692. }
  693. };
  694. for ( const img of imgs ) {
  695. img.r = new Image();
  696. img.r.addEventListener('load', onLoaded, { once: true });
  697. img.r.src = icons[img.i].path[img.p];
  698. }
  699. })();
  700. // parts: bit 0 = icon
  701. // bit 1 = badge text
  702. // bit 2 = badge color
  703. // bit 3 = hide badge
  704. return async function(tabId, details) {
  705. tabId = toTabId(tabId);
  706. if ( tabId === 0 ) { return; }
  707. const tab = await vAPI.tabs.get(tabId);
  708. if ( tab === null ) { return; }
  709. const { parts, src, badge, color } = details;
  710. if ( browserAction.setIcon !== undefined ) {
  711. if ( parts === undefined || (parts & 0b0001) !== 0 ) {
  712. browserAction.setIcon(
  713. Object.assign({ tabId: tab.id, path: src })
  714. );
  715. }
  716. if ( (parts & 0b0010) !== 0 ) {
  717. browserAction.setBadgeText({
  718. tabId: tab.id,
  719. text: (parts & 0b1000) === 0 ? badge : ''
  720. });
  721. }
  722. if ( (parts & 0b0100) !== 0 ) {
  723. browserAction.setBadgeBackgroundColor({ tabId: tab.id, color });
  724. }
  725. }
  726. if ( browserAction.setTitle !== undefined ) {
  727. browserAction.setTitle({
  728. tabId: tab.id,
  729. title: titleTemplate.replace(
  730. '{badge}',
  731. badge !== '' ? badge : '0'
  732. )
  733. });
  734. }
  735. if ( vAPI.contextMenu instanceof Object ) {
  736. vAPI.contextMenu.onMustUpdate(tabId);
  737. }
  738. };
  739. })();
  740. browser.browserAction.onClicked.addListener(tab => {
  741. vAPI.tabs.open({
  742. select: true,
  743. url: 'popup.html?tabId=' + tab.id + '&responsive=1'
  744. });
  745. });
  746. /******************************************************************************/
  747. /******************************************************************************/
  748. // https://github.com/uBlockOrigin/uBlock-issues/issues/710
  749. // uBO uses only ports to communicate with its auxiliary pages and
  750. // content scripts. Whether a message can trigger a privileged operation is
  751. // decided based on whether the port from which a message is received is
  752. // privileged, which is a status evaluated once, at port connection time.
  753. vAPI.messaging = {
  754. ports: new Map(),
  755. listeners: new Map(),
  756. defaultHandler: null,
  757. PRIVILEGED_URL: vAPI.getURL(''),
  758. NOOPFUNC: function(){},
  759. UNHANDLED: 'vAPI.messaging.notHandled',
  760. listen: function(details) {
  761. this.listeners.set(details.name, {
  762. fn: details.listener,
  763. privileged: details.privileged === true
  764. });
  765. },
  766. onPortDisconnect: function(port) {
  767. this.ports.delete(port.name);
  768. },
  769. onPortConnect: function(port) {
  770. port.onDisconnect.addListener(
  771. port => this.onPortDisconnect(port)
  772. );
  773. port.onMessage.addListener(
  774. (request, port) => this.onPortMessage(request, port)
  775. );
  776. this.ports.set(port.name, {
  777. port,
  778. privileged: port.sender.url.startsWith(this.PRIVILEGED_URL)
  779. });
  780. },
  781. setup: function(defaultHandler) {
  782. if ( this.defaultHandler !== null ) { return; }
  783. if ( typeof defaultHandler !== 'function' ) {
  784. defaultHandler = function() {
  785. return this.UNHANDLED;
  786. };
  787. }
  788. this.defaultHandler = defaultHandler;
  789. browser.runtime.onConnect.addListener(
  790. port => this.onPortConnect(port)
  791. );
  792. // https://bugzilla.mozilla.org/show_bug.cgi?id=1392067
  793. // Workaround: manually remove ports matching removed tab.
  794. if (
  795. vAPI.webextFlavor.soup.has('firefox') &&
  796. vAPI.webextFlavor.major < 61
  797. ) {
  798. browser.tabs.onRemoved.addListener(tabId => {
  799. for ( const { port } of this.ports.values() ) {
  800. const tab = port.sender && port.sender.tab;
  801. if ( !tab ) { continue; }
  802. if ( tab.id === tabId ) {
  803. this.onPortDisconnect(port);
  804. }
  805. }
  806. });
  807. }
  808. },
  809. broadcast: function(message) {
  810. const messageWrapper = { broadcast: true, msg: message };
  811. for ( const { port } of this.ports.values() ) {
  812. try {
  813. port.postMessage(messageWrapper);
  814. } catch(ex) {
  815. this.ports.delete(port.name);
  816. }
  817. }
  818. },
  819. onFrameworkMessage: function(request, port, callback) {
  820. const sender = port && port.sender;
  821. if ( !sender ) { return; }
  822. const tabId = sender.tab && sender.tab.id || undefined;
  823. const msg = request.msg;
  824. switch ( msg.what ) {
  825. case 'connectionAccepted':
  826. case 'connectionRefused': {
  827. const toPort = this.ports.get(msg.fromToken);
  828. if ( toPort !== undefined ) {
  829. msg.tabId = tabId;
  830. toPort.port.postMessage(request);
  831. } else {
  832. msg.what = 'connectionBroken';
  833. port.postMessage(request);
  834. }
  835. break;
  836. }
  837. case 'connectionRequested':
  838. msg.tabId = tabId;
  839. for ( const { port: toPort } of this.ports.values() ) {
  840. if ( toPort === port ) { continue; }
  841. toPort.postMessage(request);
  842. }
  843. break;
  844. case 'connectionBroken':
  845. case 'connectionCheck':
  846. case 'connectionMessage': {
  847. const toPort = this.ports.get(
  848. port.name === msg.fromToken ? msg.toToken : msg.fromToken
  849. );
  850. if ( toPort !== undefined ) {
  851. msg.tabId = tabId;
  852. toPort.port.postMessage(request);
  853. } else {
  854. msg.what = 'connectionBroken';
  855. port.postMessage(request);
  856. }
  857. break;
  858. }
  859. case 'extendClient':
  860. vAPI.tabs.executeScript(tabId, {
  861. file: '/js/vapi-client-extra.js',
  862. }).then(( ) => {
  863. callback();
  864. });
  865. break;
  866. case 'userCSS':
  867. if ( tabId === undefined ) { break; }
  868. const details = {
  869. code: undefined,
  870. frameId: sender.frameId,
  871. matchAboutBlank: true
  872. };
  873. if ( vAPI.supportsUserStylesheets ) {
  874. details.cssOrigin = 'user';
  875. }
  876. if ( msg.add ) {
  877. details.runAt = 'document_start';
  878. }
  879. const promises = [];
  880. for ( const cssText of msg.add ) {
  881. details.code = cssText;
  882. promises.push(vAPI.tabs.insertCSS(tabId, details));
  883. }
  884. if ( typeof webext.tabs.removeCSS === 'function' ) {
  885. for ( const cssText of msg.remove ) {
  886. details.code = cssText;
  887. promises.push(vAPI.tabs.removeCSS(tabId, details));
  888. }
  889. }
  890. Promise.all(promises).then(( ) => {
  891. callback();
  892. });
  893. break;
  894. }
  895. },
  896. // Use a wrapper to avoid closure and to allow reuse.
  897. CallbackWrapper: class {
  898. constructor(messaging, port, msgId) {
  899. this.messaging = messaging;
  900. this.callback = this.proxy.bind(this); // bind once
  901. this.init(port, msgId);
  902. }
  903. init(port, msgId) {
  904. this.port = port;
  905. this.msgId = msgId;
  906. return this;
  907. }
  908. proxy(response) {
  909. // https://github.com/chrisaljoudi/uBlock/issues/383
  910. if ( this.messaging.ports.has(this.port.name) ) {
  911. this.port.postMessage({
  912. msgId: this.msgId,
  913. msg: response !== undefined ? response : null,
  914. });
  915. }
  916. // Store for reuse
  917. this.port = null;
  918. this.messaging.callbackWrapperJunkyard.push(this);
  919. }
  920. },
  921. callbackWrapperJunkyard: [],
  922. callbackWrapperFactory: function(port, msgId) {
  923. return this.callbackWrapperJunkyard.length !== 0
  924. ? this.callbackWrapperJunkyard.pop().init(port, msgId)
  925. : new this.CallbackWrapper(this, port, msgId);
  926. },
  927. onPortMessage: function(request, port) {
  928. // prepare response
  929. let callback = this.NOOPFUNC;
  930. if ( request.msgId !== undefined ) {
  931. callback = this.callbackWrapperFactory(port, request.msgId).callback;
  932. }
  933. // Content process to main process: framework handler.
  934. if ( request.channel === 'vapi' ) {
  935. this.onFrameworkMessage(request, port, callback);
  936. return;
  937. }
  938. // Auxiliary process to main process: specific handler
  939. const fromDetails = this.ports.get(port.name);
  940. if ( fromDetails === undefined ) { return; }
  941. const listenerDetails = this.listeners.get(request.channel);
  942. let r = this.UNHANDLED;
  943. if (
  944. (listenerDetails !== undefined) &&
  945. (listenerDetails.privileged === false || fromDetails.privileged)
  946. ) {
  947. r = listenerDetails.fn(request.msg, port.sender, callback);
  948. }
  949. if ( r !== this.UNHANDLED ) { return; }
  950. // Auxiliary process to main process: default handler
  951. if ( fromDetails.privileged ) {
  952. r = this.defaultHandler(request.msg, port.sender, callback);
  953. if ( r !== this.UNHANDLED ) { return; }
  954. }
  955. // Auxiliary process to main process: no handler
  956. log.info(
  957. `vAPI.messaging.onPortMessage > unhandled request: ${JSON.stringify(request.msg)}`,
  958. request
  959. );
  960. // Need to callback anyways in case caller expected an answer, or
  961. // else there is a memory leak on caller's side
  962. callback();
  963. },
  964. };
  965. /******************************************************************************/
  966. /******************************************************************************/
  967. // https://github.com/gorhill/uBlock/issues/3474
  968. // https://github.com/gorhill/uBlock/issues/2823
  969. // Foil ability of web pages to identify uBO through
  970. // its web accessible resources.
  971. // https://github.com/gorhill/uBlock/issues/3497
  972. // Prevent web pages from interfering with uBO's element picker
  973. // https://github.com/uBlockOrigin/uBlock-issues/issues/550
  974. // Support using a new secret for every network request.
  975. vAPI.warSecret = (( ) => {
  976. const generateSecret = ( ) => {
  977. return Math.floor(Math.random() * 982451653 + 982451653).toString(36);
  978. };
  979. const root = vAPI.getURL('/');
  980. const secrets = [];
  981. let lastSecretTime = 0;
  982. const guard = function(details) {
  983. const url = details.url;
  984. const pos = secrets.findIndex(secret =>
  985. url.lastIndexOf(`?secret=${secret}`) !== -1
  986. );
  987. if ( pos === -1 ) {
  988. return { redirectUrl: root };
  989. }
  990. secrets.splice(pos, 1);
  991. };
  992. browser.webRequest.onBeforeRequest.addListener(
  993. guard,
  994. {
  995. urls: [ root + 'web_accessible_resources/*' ]
  996. },
  997. [ 'blocking' ]
  998. );
  999. return ( ) => {
  1000. if ( secrets.length !== 0 ) {
  1001. if ( (Date.now() - lastSecretTime) > 5000 ) {
  1002. secrets.splice(0);
  1003. } else if ( secrets.length > 256 ) {
  1004. secrets.splice(0, secrets.length - 192);
  1005. }
  1006. }
  1007. lastSecretTime = Date.now();
  1008. const secret = generateSecret();
  1009. secrets.push(secret);
  1010. return `?secret=${secret}`;
  1011. };
  1012. })();
  1013. /******************************************************************************/
  1014. vAPI.Net = class {
  1015. constructor() {
  1016. this.validTypes = new Set();
  1017. {
  1018. const wrrt = browser.webRequest.ResourceType;
  1019. for ( const typeKey in wrrt ) {
  1020. if ( wrrt.hasOwnProperty(typeKey) ) {
  1021. this.validTypes.add(wrrt[typeKey]);
  1022. }
  1023. }
  1024. }
  1025. this.suspendableListener = undefined;
  1026. this.listenerMap = new WeakMap();
  1027. this.suspendDepth = 0;
  1028. browser.webRequest.onBeforeRequest.addListener(
  1029. details => {
  1030. this.normalizeDetails(details);
  1031. if ( this.suspendDepth !== 0 && details.tabId >= 0 ) {
  1032. return this.suspendOneRequest(details);
  1033. }
  1034. return this.onBeforeSuspendableRequest(details);
  1035. },
  1036. this.denormalizeFilters({ urls: [ 'http://*/*', 'https://*/*' ] }),
  1037. [ 'blocking' ]
  1038. );
  1039. }
  1040. setOptions(/* options */) {
  1041. }
  1042. normalizeDetails(/* details */) {
  1043. }
  1044. denormalizeFilters(filters) {
  1045. const urls = filters.urls || [ '<all_urls>' ];
  1046. let types = filters.types;
  1047. if ( Array.isArray(types) ) {
  1048. types = this.denormalizeTypes(types);
  1049. }
  1050. if (
  1051. (this.validTypes.has('websocket')) &&
  1052. (types === undefined || types.indexOf('websocket') !== -1) &&
  1053. (urls.indexOf('<all_urls>') === -1)
  1054. ) {
  1055. if ( urls.indexOf('ws://*/*') === -1 ) {
  1056. urls.push('ws://*/*');
  1057. }
  1058. if ( urls.indexOf('wss://*/*') === -1 ) {
  1059. urls.push('wss://*/*');
  1060. }
  1061. }
  1062. return { types, urls };
  1063. }
  1064. denormalizeTypes(types) {
  1065. return types;
  1066. }
  1067. addListener(which, clientListener, filters, options) {
  1068. const actualFilters = this.denormalizeFilters(filters);
  1069. const actualListener = this.makeNewListenerProxy(clientListener);
  1070. browser.webRequest[which].addListener(
  1071. actualListener,
  1072. actualFilters,
  1073. options
  1074. );
  1075. }
  1076. onBeforeSuspendableRequest(details) {
  1077. if ( this.suspendableListener === undefined ) { return; }
  1078. return this.suspendableListener(details);
  1079. }
  1080. setSuspendableListener(listener) {
  1081. this.suspendableListener = listener;
  1082. }
  1083. removeListener(which, clientListener) {
  1084. const actualListener = this.listenerMap.get(clientListener);
  1085. if ( actualListener === undefined ) { return; }
  1086. this.listenerMap.delete(clientListener);
  1087. browser.webRequest[which].removeListener(actualListener);
  1088. }
  1089. makeNewListenerProxy(clientListener) {
  1090. const actualListener = details => {
  1091. this.normalizeDetails(details);
  1092. return clientListener(details);
  1093. };
  1094. this.listenerMap.set(clientListener, actualListener);
  1095. return actualListener;
  1096. }
  1097. suspendOneRequest() {
  1098. }
  1099. unsuspendAllRequests() {
  1100. }
  1101. suspend(force = false) {
  1102. if ( this.canSuspend() || force ) {
  1103. this.suspendDepth += 1;
  1104. }
  1105. }
  1106. unsuspend(all = false) {
  1107. if ( this.suspendDepth === 0 ) { return; }
  1108. if ( all ) {
  1109. this.suspendDepth = 0;
  1110. } else {
  1111. this.suspendDepth -= 1;
  1112. }
  1113. if ( this.suspendDepth !== 0 ) { return; }
  1114. this.unsuspendAllRequests();
  1115. }
  1116. canSuspend() {
  1117. return false;
  1118. }
  1119. async benchmark() {
  1120. if ( typeof µMatrix !== 'object' ) { return; }
  1121. const requests = await µMatrix.loadBenchmarkDataset();
  1122. if ( Array.isArray(requests) === false || requests.length === 0 ) {
  1123. console.info('No requests found to benchmark');
  1124. return;
  1125. }
  1126. const mappedTypes = new Map([
  1127. [ 'document', 'main_frame' ],
  1128. [ 'subdocument', 'sub_frame' ],
  1129. ]);
  1130. console.info('vAPI.net.onBeforeSuspendableRequest()...');
  1131. const t0 = self.performance.now();
  1132. const promises = [];
  1133. const details = {
  1134. documentUrl: '',
  1135. tabId: -1,
  1136. parentFrameId: -1,
  1137. frameId: 0,
  1138. type: '',
  1139. url: '',
  1140. };
  1141. for ( const request of requests ) {
  1142. details.documentUrl = request.frameUrl;
  1143. details.tabId = -1;
  1144. details.parentFrameId = -1;
  1145. details.frameId = 0;
  1146. details.type = mappedTypes.get(request.cpt) || request.cpt;
  1147. details.url = request.url;
  1148. if ( details.type === 'main_frame' ) { continue; }
  1149. promises.push(this.onBeforeSuspendableRequest(details));
  1150. }
  1151. return Promise.all(promises).then(results => {
  1152. let blockCount = 0;
  1153. for ( const r of results ) {
  1154. if ( r !== undefined ) { blockCount += 1; }
  1155. }
  1156. const t1 = self.performance.now();
  1157. const dur = t1 - t0;
  1158. console.info(`Evaluated ${requests.length} requests in ${dur.toFixed(0)} ms`);
  1159. console.info(`\tBlocked ${blockCount} requests`);
  1160. console.info(`\tAverage: ${(dur / requests.length).toFixed(3)} ms per request`);
  1161. });
  1162. }
  1163. };
  1164. /******************************************************************************/
  1165. /******************************************************************************/
  1166. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus#Browser_compatibility
  1167. // Firefox for Android does no support browser.contextMenus.
  1168. vAPI.contextMenu = webext.menus && {
  1169. _callback: null,
  1170. _entries: [],
  1171. _createEntry: function(entry) {
  1172. webext.menus.create(JSON.parse(JSON.stringify(entry)));
  1173. },
  1174. onMustUpdate: function() {},
  1175. setEntries: function(entries, callback) {
  1176. entries = entries || [];
  1177. let n = Math.max(this._entries.length, entries.length);
  1178. for ( let i = 0; i < n; i++ ) {
  1179. const oldEntryId = this._entries[i];
  1180. const newEntry = entries[i];
  1181. if ( oldEntryId && newEntry ) {
  1182. if ( newEntry.id !== oldEntryId ) {
  1183. webext.menus.remove(oldEntryId);
  1184. this._createEntry(newEntry);
  1185. this._entries[i] = newEntry.id;
  1186. }
  1187. } else if ( oldEntryId && !newEntry ) {
  1188. webext.menus.remove(oldEntryId);
  1189. } else if ( !oldEntryId && newEntry ) {
  1190. this._createEntry(newEntry);
  1191. this._entries[i] = newEntry.id;
  1192. }
  1193. }
  1194. n = this._entries.length = entries.length;
  1195. callback = callback || null;
  1196. if ( callback === this._callback ) {
  1197. return;
  1198. }
  1199. if ( n !== 0 && callback !== null ) {
  1200. webext.menus.onClicked.addListener(callback);
  1201. this._callback = callback;
  1202. } else if ( n === 0 && this._callback !== null ) {
  1203. webext.menus.onClicked.removeListener(this._callback);
  1204. this._callback = null;
  1205. }
  1206. }
  1207. };
  1208. /******************************************************************************/
  1209. /******************************************************************************/
  1210. vAPI.commands = browser.commands;
  1211. /******************************************************************************/
  1212. /******************************************************************************/
  1213. // https://github.com/gorhill/uBlock/issues/531
  1214. // Storage area dedicated to admin settings. Read-only.
  1215. // https://github.com/gorhill/uBlock/commit/43a5ed735b95a575a9339b6e71a1fcb27a99663b#commitcomment-13965030
  1216. // Not all Chromium-based browsers support managed storage. Merely testing or
  1217. // exception handling in this case does NOT work: I don't know why. The
  1218. // extension on Opera ends up in a non-sensical state, whereas vAPI become
  1219. // undefined out of nowhere. So only solution left is to test explicitly for
  1220. // Opera.
  1221. // https://github.com/gorhill/uBlock/issues/900
  1222. // Also, UC Browser: http://www.upsieutoc.com/image/WXuH
  1223. vAPI.adminStorage = (( ) => {
  1224. if ( webext.storage.managed instanceof Object === false ) {
  1225. return {
  1226. getItem: function() {
  1227. return Promise.resolve();
  1228. },
  1229. };
  1230. }
  1231. return {
  1232. getItem: async function(key) {
  1233. let bin;
  1234. try {
  1235. bin = await webext.storage.managed.get(key);
  1236. } catch(ex) {
  1237. }
  1238. if ( bin instanceof Object ) {
  1239. return bin[key];
  1240. }
  1241. }
  1242. };
  1243. })();
  1244. /******************************************************************************/
  1245. /******************************************************************************/
  1246. // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/sync
  1247. vAPI.cloud = (( ) => {
  1248. // Not all platforms support `webext.storage.sync`.
  1249. if ( webext.storage.sync instanceof Object === false ) { return; }
  1250. // Currently, only Chromium supports the following constants -- these
  1251. // values will be assumed for platforms which do not define them.
  1252. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/sync
  1253. // > You can store up to 100KB of data using this API
  1254. const MAX_ITEMS =
  1255. webext.storage.sync.MAX_ITEMS || 512;
  1256. const QUOTA_BYTES =
  1257. webext.storage.sync.QUOTA_BYTES || 102400;
  1258. const QUOTA_BYTES_PER_ITEM =
  1259. webext.storage.sync.QUOTA_BYTES_PER_ITEM || 8192;
  1260. const chunkCountPerFetch = 16; // Must be a power of 2
  1261. const maxChunkCountPerItem = Math.floor(MAX_ITEMS * 0.75) & ~(chunkCountPerFetch - 1);
  1262. // https://github.com/gorhill/uBlock/issues/3006
  1263. // For Firefox, we will use a lower ratio to allow for more overhead for
  1264. // the infrastructure. Unfortunately this leads to less usable space for
  1265. // actual data, but all of this is provided for free by browser vendors,
  1266. // so we need to accept and deal with these limitations.
  1267. const evalMaxChunkSize = function() {
  1268. return Math.floor(
  1269. QUOTA_BYTES_PER_ITEM *
  1270. (vAPI.webextFlavor.soup.has('firefox') ? 0.6 : 0.75)
  1271. );
  1272. };
  1273. let maxChunkSize = evalMaxChunkSize();
  1274. // The real actual webextFlavor value may not be set in stone, so listen
  1275. // for possible future changes.
  1276. window.addEventListener('webextFlavor', function() {
  1277. maxChunkSize = evalMaxChunkSize();
  1278. }, { once: true });
  1279. const maxStorageSize = QUOTA_BYTES;
  1280. const options = {
  1281. defaultDeviceName: window.navigator.platform,
  1282. deviceName: vAPI.localStorage.getItem('deviceName') || ''
  1283. };
  1284. // This is used to find out a rough count of how many chunks exists:
  1285. // We "poll" at specific index in order to get a rough idea of how
  1286. // large is the stored string.
  1287. // This allows reading a single item with only 2 sync operations -- a
  1288. // good thing given chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE
  1289. // and chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR.
  1290. const getCoarseChunkCount = async function(dataKey) {
  1291. const keys = {};
  1292. for ( let i = 0; i < maxChunkCountPerItem; i += 16 ) {
  1293. keys[dataKey + i.toString()] = '';
  1294. }
  1295. let bin;
  1296. try {
  1297. bin = await webext.storage.sync.get(keys);
  1298. } catch (reason) {
  1299. return reason;
  1300. }
  1301. let chunkCount = 0;
  1302. for ( let i = 0; i < maxChunkCountPerItem; i += 16 ) {
  1303. if ( bin[dataKey + i.toString()] === '' ) { break; }
  1304. chunkCount = i + 16;
  1305. }
  1306. return chunkCount;
  1307. };
  1308. const deleteChunks = function(dataKey, start) {
  1309. const keys = [];
  1310. // No point in deleting more than:
  1311. // - The max number of chunks per item
  1312. // - The max number of chunks per storage limit
  1313. const n = Math.min(
  1314. maxChunkCountPerItem,
  1315. Math.ceil(maxStorageSize / maxChunkSize)
  1316. );
  1317. for ( let i = start; i < n; i++ ) {
  1318. keys.push(dataKey + i.toString());
  1319. }
  1320. if ( keys.length !== 0 ) {
  1321. webext.storage.sync.remove(keys);
  1322. }
  1323. };
  1324. const push = async function(dataKey, data) {
  1325. let bin = {
  1326. 'source': options.deviceName || options.defaultDeviceName,
  1327. 'tstamp': Date.now(),
  1328. 'data': data,
  1329. 'size': 0
  1330. };
  1331. bin.size = JSON.stringify(bin).length;
  1332. const item = JSON.stringify(bin);
  1333. // Chunkify taking into account QUOTA_BYTES_PER_ITEM:
  1334. // https://developer.chrome.com/extensions/storage#property-sync
  1335. // "The maximum size (in bytes) of each individual item in sync
  1336. // "storage, as measured by the JSON stringification of its value
  1337. // "plus its key length."
  1338. bin = {};
  1339. let chunkCount = Math.ceil(item.length / maxChunkSize);
  1340. for ( let i = 0; i < chunkCount; i++ ) {
  1341. bin[dataKey + i.toString()] = item.substr(i * maxChunkSize, maxChunkSize);
  1342. }
  1343. bin[dataKey + chunkCount.toString()] = ''; // Sentinel
  1344. let result;
  1345. let errorStr;
  1346. try {
  1347. result = await webext.storage.sync.set(bin);
  1348. } catch (reason) {
  1349. errorStr = reason;
  1350. }
  1351. // https://github.com/gorhill/uBlock/issues/3006#issuecomment-332597677
  1352. // - Delete all that was pushed in case of failure.
  1353. // - It's unknown whether such issue applies only to Firefox:
  1354. // until such cases are reported for other browsers, we will
  1355. // reset the (now corrupted) content of the cloud storage
  1356. // only on Firefox.
  1357. if ( errorStr !== undefined && vAPI.webextFlavor.soup.has('firefox') ) {
  1358. chunkCount = 0;
  1359. }
  1360. // Remove potentially unused trailing chunks
  1361. deleteChunks(dataKey, chunkCount);
  1362. return errorStr;
  1363. };
  1364. const pull = async function(dataKey) {
  1365. const result = await getCoarseChunkCount(dataKey);
  1366. if ( typeof result !== 'number' ) {
  1367. return result;
  1368. }
  1369. const chunkKeys = {};
  1370. for ( let i = 0; i < result; i++ ) {
  1371. chunkKeys[dataKey + i.toString()] = '';
  1372. }
  1373. let bin;
  1374. try {
  1375. bin = await webext.storage.sync.get(chunkKeys);
  1376. } catch (reason) {
  1377. return reason;
  1378. }
  1379. // Assemble chunks into a single string.
  1380. // https://www.reddit.com/r/uMatrix/comments/8lc9ia/my_rules_tab_hangs_with_cloud_storage_support/
  1381. // Explicit sentinel is not necessarily present: this can
  1382. // happen when the number of chunks is a multiple of
  1383. // chunkCountPerFetch. Hence why we must also test against
  1384. // undefined.
  1385. let json = [], jsonSlice;
  1386. let i = 0;
  1387. for (;;) {
  1388. jsonSlice = bin[dataKey + i.toString()];
  1389. if ( jsonSlice === '' || jsonSlice === undefined ) { break; }
  1390. json.push(jsonSlice);
  1391. i += 1;
  1392. }
  1393. let entry = null;
  1394. try {
  1395. entry = JSON.parse(json.join(''));
  1396. } catch(ex) {
  1397. }
  1398. return entry;
  1399. };
  1400. const getOptions = function(callback) {
  1401. if ( typeof callback !== 'function' ) { return; }
  1402. callback(options);
  1403. };
  1404. const setOptions = function(details, callback) {
  1405. if ( typeof details !== 'object' || details === null ) { return; }
  1406. if ( typeof details.deviceName === 'string' ) {
  1407. vAPI.localStorage.setItem('deviceName', details.deviceName);
  1408. options.deviceName = details.deviceName;
  1409. }
  1410. getOptions(callback);
  1411. };
  1412. return { push, pull, getOptions, setOptions };
  1413. })();
  1414. /******************************************************************************/
  1415. /******************************************************************************/
  1416. vAPI.browserData = {};
  1417. // https://developer.chrome.com/extensions/browsingData
  1418. vAPI.browserData.clearCache = function(callback) {
  1419. browser.browsingData.removeCache({ since: 0 }, callback);
  1420. };
  1421. /******************************************************************************/
  1422. /******************************************************************************/
  1423. // https://developer.chrome.com/extensions/cookies
  1424. vAPI.cookies = {
  1425. start: function() {
  1426. const reallyRemoved = {
  1427. 'evicted': true,
  1428. 'expired': true,
  1429. 'explicit': true,
  1430. };
  1431. browser.cookies.onChanged.addListener(changeInfo => {
  1432. if ( changeInfo.removed ) {
  1433. if (
  1434. reallyRemoved[changeInfo.cause] &&
  1435. typeof this.onRemoved === 'function'
  1436. ) {
  1437. this.onRemoved(changeInfo.cookie);
  1438. }
  1439. return;
  1440. }
  1441. if ( typeof this.onChanged === 'function' ) {
  1442. this.onChanged(changeInfo.cookie);
  1443. }
  1444. });
  1445. },
  1446. getAll: function() {
  1447. return webext.cookies.getAll({});
  1448. },
  1449. remove: function(details) {
  1450. return webext.cookies.remove(details);
  1451. },
  1452. };
  1453. /******************************************************************************/
  1454. /******************************************************************************/
  1455. // <<<<< end of local scope
  1456. }
  1457. /******************************************************************************/