telemetry.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2016 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /* eslint strict: ["error", "function"] */
  14. (function () {
  15. "use strict";
  16. // This module sends the browser and extension version to a server, to
  17. // determine whether it is safe to drop support for old Chrome versions in
  18. // future extension updates.
  19. //
  20. // The source code for the server is available at:
  21. // https://github.com/Rob--W/pdfjs-telemetry
  22. var LOG_URL = "https://pdfjs.robwu.nl/logpdfjs";
  23. // The minimum time to wait before sending a ping, so that we don't send too
  24. // many requests even if the user restarts their browser very often.
  25. // We want one ping a day, so a minimum delay of 12 hours should be OK.
  26. var MINIMUM_TIME_BETWEEN_PING = 12 * 36e5;
  27. if (chrome.extension.inIncognitoContext) {
  28. // The extension uses incognito split mode, so there are two background
  29. // pages. Only send telemetry when not in incognito mode.
  30. return;
  31. }
  32. if (chrome.runtime.id !== "oemmndcbldboiebfnladdacbdfmadadm") {
  33. // Only send telemetry for the official PDF.js extension.
  34. console.warn("Disabled telemetry because this is not an official build.");
  35. return;
  36. }
  37. maybeSendPing();
  38. setInterval(maybeSendPing, 36e5);
  39. function maybeSendPing() {
  40. getLoggingPref(function (didOptOut) {
  41. if (didOptOut) {
  42. // Respect the user's decision to not send statistics.
  43. return;
  44. }
  45. if (!navigator.onLine) {
  46. // No network available; Wait until the next scheduled ping opportunity.
  47. // Even if onLine is true, the server may still be unreachable. But
  48. // because it is impossible to tell whether a request failed due to the
  49. // inability to connect, or a deliberate connection termination by the
  50. // server, we don't validate the response and assume that the request
  51. // succeeded. This ensures that the server cannot ask the client to
  52. // send more pings.
  53. return;
  54. }
  55. var lastTime = parseInt(localStorage.telemetryLastTime) || 0;
  56. var wasUpdated = didUpdateSinceLastCheck();
  57. if (!wasUpdated && Date.now() - lastTime < MINIMUM_TIME_BETWEEN_PING) {
  58. return;
  59. }
  60. localStorage.telemetryLastTime = Date.now();
  61. var deduplication_id = getDeduplicationId(wasUpdated);
  62. var extension_version = chrome.runtime.getManifest().version;
  63. if (window.Request && "mode" in Request.prototype) {
  64. // fetch is supported in extensions since Chrome 42 (though the above
  65. // feature-detection method detects Chrome 43+).
  66. // Unlike XMLHttpRequest, fetch omits credentials such as cookies in the
  67. // requests, which guarantees that the server cannot track the client
  68. // via HTTP cookies.
  69. fetch(LOG_URL, {
  70. method: "POST",
  71. headers: new Headers({
  72. "Deduplication-Id": deduplication_id,
  73. "Extension-Version": extension_version,
  74. }),
  75. // Set mode=cors so that the above custom headers are included in the
  76. // request.
  77. mode: "cors",
  78. });
  79. return;
  80. }
  81. var x = new XMLHttpRequest();
  82. x.open("POST", LOG_URL);
  83. x.setRequestHeader("Deduplication-Id", deduplication_id);
  84. x.setRequestHeader("Extension-Version", extension_version);
  85. x.send();
  86. });
  87. }
  88. /**
  89. * Generate a 40-bit hexadecimal string (=10 letters, 1.1E12 possibilities).
  90. * This is used by the server to discard duplicate entries of the same browser
  91. * version when the log data is aggregated.
  92. */
  93. function getDeduplicationId(wasUpdated) {
  94. var id = localStorage.telemetryDeduplicationId;
  95. // The ID is only used to deduplicate reports for the same browser version,
  96. // so it is OK to change the ID if the browser is updated. By changing the
  97. // ID, the server cannot track users for a long period even if it wants to.
  98. if (!id || !/^[0-9a-f]{10}$/.test(id) || wasUpdated) {
  99. id = "";
  100. var buf = new Uint8Array(5);
  101. crypto.getRandomValues(buf);
  102. for (const c of buf) {
  103. id += (c >>> 4).toString(16) + (c & 0xf).toString(16);
  104. }
  105. localStorage.telemetryDeduplicationId = id;
  106. }
  107. return id;
  108. }
  109. /**
  110. * Returns whether the browser has received a major update since the last call
  111. * to this function.
  112. */
  113. function didUpdateSinceLastCheck() {
  114. var chromeVersion = /Chrome\/(\d+)\./.exec(navigator.userAgent);
  115. chromeVersion = chromeVersion && chromeVersion[1];
  116. if (!chromeVersion || localStorage.telemetryLastVersion === chromeVersion) {
  117. return false;
  118. }
  119. localStorage.telemetryLastVersion = chromeVersion;
  120. return true;
  121. }
  122. /**
  123. * Get the value of the telemetry preference. The callback is invoked with a
  124. * boolean if a preference is found, and with the undefined value otherwise.
  125. */
  126. function getLoggingPref(callback) {
  127. // Try to look up the preference in the storage, in the following order:
  128. var areas = ["sync", "local", "managed"];
  129. next();
  130. function next(result) {
  131. var storageAreaName = areas.shift();
  132. if (typeof result === "boolean" || !storageAreaName) {
  133. callback(result);
  134. return;
  135. }
  136. if (!chrome.storage[storageAreaName]) {
  137. next();
  138. return;
  139. }
  140. chrome.storage[storageAreaName].get("disableTelemetry", function (items) {
  141. next(items && items.disableTelemetry);
  142. });
  143. }
  144. }
  145. })();