init-webext.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* global Util, chrome, Config, UI, Broker, Snowflake, WS */
  2. /* eslint no-unused-vars: 0 */
  3. /*
  4. UI
  5. */
  6. class WebExtUI extends UI {
  7. constructor() {
  8. super();
  9. this.onConnect = this.onConnect.bind(this);
  10. this.onMessage = this.onMessage.bind(this);
  11. this.onDisconnect = this.onDisconnect.bind(this);
  12. this.initStats();
  13. chrome.runtime.onConnect.addListener(this.onConnect);
  14. }
  15. initStats() {
  16. this.stats = [0];
  17. setInterval((() => {
  18. this.stats.unshift(0);
  19. this.stats.splice(24);
  20. this.postActive();
  21. }), 60 * 60 * 1000);
  22. }
  23. initToggle() {
  24. // First, check if we have our status stored
  25. (new Promise((resolve) => {
  26. chrome.storage.local.get(["snowflake-enabled"], resolve);
  27. }))
  28. .then((result) => {
  29. let enabled = this.enabled;
  30. if (result['snowflake-enabled'] !== void 0) {
  31. enabled = result['snowflake-enabled'];
  32. } else {
  33. log("Toggle state not yet saved");
  34. }
  35. // If it isn't enabled, stop
  36. if (!enabled) {
  37. this.setEnabled(enabled);
  38. return;
  39. }
  40. // Otherwise, do feature checks
  41. if (!Util.hasWebRTC()) {
  42. this.missingFeature = 'popupWebRTCOff';
  43. this.setEnabled(false);
  44. return;
  45. }
  46. WS.probeWebsocket(config.relayAddr)
  47. .then(
  48. () => {
  49. this.setEnabled(true);
  50. },
  51. () => {
  52. log('Could not connect to bridge.');
  53. this.missingFeature = 'popupBridgeUnreachable';
  54. this.setEnabled(false);
  55. }
  56. );
  57. });
  58. }
  59. postActive() {
  60. this.setIcon();
  61. if (!this.port) { return; }
  62. this.port.postMessage({
  63. active: this.active,
  64. total: this.stats.reduce((function(t, c) {
  65. return t + c;
  66. }), 0),
  67. enabled: this.enabled,
  68. missingFeature: this.missingFeature,
  69. });
  70. }
  71. onConnect(port) {
  72. this.port = port;
  73. port.onDisconnect.addListener(this.onDisconnect);
  74. port.onMessage.addListener(this.onMessage);
  75. this.postActive();
  76. }
  77. onMessage(m) {
  78. (new Promise((resolve) => {
  79. chrome.storage.local.set({ "snowflake-enabled": m.enabled }, resolve);
  80. }))
  81. .then(() => {
  82. log("Stored toggle state");
  83. this.initToggle();
  84. });
  85. }
  86. onDisconnect() {
  87. this.port = null;
  88. }
  89. setActive(connected) {
  90. super.setActive(connected);
  91. if (connected) {
  92. this.stats[0] += 1;
  93. }
  94. this.postActive();
  95. }
  96. setEnabled(enabled) {
  97. this.enabled = enabled;
  98. this.postActive();
  99. update();
  100. }
  101. setIcon() {
  102. let path = null;
  103. if (!this.enabled) {
  104. path = {
  105. 48: "assets/toolbar-off-48.png",
  106. 96: "assets/toolbar-off-96.png"
  107. };
  108. } else if (this.active) {
  109. path = {
  110. 48: "assets/toolbar-running-48.png",
  111. 96: "assets/toolbar-running-96.png"
  112. };
  113. } else {
  114. path = {
  115. 48: "assets/toolbar-on-48.png",
  116. 96: "assets/toolbar-on-96.png"
  117. };
  118. }
  119. chrome.browserAction.setIcon({
  120. path: path,
  121. });
  122. }
  123. }
  124. WebExtUI.prototype.port = null;
  125. WebExtUI.prototype.stats = null;
  126. WebExtUI.prototype.enabled = true;
  127. /*
  128. Entry point.
  129. */
  130. var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications;
  131. (function () {
  132. silenceNotifications = false;
  133. debug = false;
  134. snowflake = null;
  135. config = null;
  136. broker = null;
  137. ui = null;
  138. // Log to both console and UI if applicable.
  139. // Requires that the snowflake and UI objects are hooked up in order to
  140. // log to console.
  141. log = function(msg) {
  142. console.log('Snowflake: ' + msg);
  143. return snowflake != null ? snowflake.ui.log(msg) : void 0;
  144. };
  145. dbg = function(msg) {
  146. if (debug) {
  147. return log(msg);
  148. }
  149. };
  150. init = function() {
  151. config = new Config;
  152. config.proxyType = "webext";
  153. ui = new WebExtUI();
  154. broker = new Broker(config);
  155. snowflake = new Snowflake(config, ui, broker);
  156. log('== snowflake proxy ==');
  157. ui.initToggle();
  158. };
  159. update = function() {
  160. if (!ui.enabled) {
  161. // Do not activate the proxy if any number of conditions are true.
  162. snowflake.disable();
  163. log('Currently not active.');
  164. return;
  165. }
  166. // Otherwise, begin setting up WebRTC and acting as a proxy.
  167. dbg('Contacting Broker at ' + broker.url);
  168. log('Starting snowflake');
  169. snowflake.setRelayAddr(config.relayAddr);
  170. return snowflake.beginWebRTC();
  171. };
  172. window.onunload = function() {
  173. if (snowflake !== null) { snowflake.disable(); }
  174. return null;
  175. };
  176. window.onload = init;
  177. }());