[tor-commits] [meek/chrome] Avoid hardcoded extension ID.

dcf at torproject.org dcf at torproject.org
Wed Apr 16 04:11:27 UTC 2014


commit f3361a55c543a89441824fe55304d4d2e60ebcbb
Author: Chang Lan <clan at eecs.berkeley.edu>
Date:   Tue Apr 15 20:04:18 2014 -0700

    Avoid hardcoded extension ID.
    
    The Extension send heartbeat information containing its extension ID to the
    App periodically.
---
 chrome/app/background.js       |   14 ++++++++++----
 chrome/extension/background.js |   15 +++++++++++++--
 chrome/extension/manifest.json |    1 +
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/chrome/app/background.js b/chrome/app/background.js
index 1ce4d32..77ad0da 100644
--- a/chrome/app/background.js
+++ b/chrome/app/background.js
@@ -5,7 +5,6 @@ chrome.alarms.onAlarm.addListener(function(alarm) { console.info("alarm name = "
 
 const IP = "127.0.0.1";
 const PORT = 7000;
-const EXTENSION_ID = "epmfkpbifhkdhcedgfppmeeoonjenkee"; //FIXME: Hardcoded extension ID
 
 const STATE_READING_LENGTH = 1;
 const STATE_READING_OBJECT = 2;
@@ -15,9 +14,16 @@ var state = STATE_READING_LENGTH;
 var buf = new Uint8Array(4);
 var bytesToRead = buf.length;
 
-chrome.sockets.tcpServer.create({}, function(createInfo) {
-  listenAndAccept(createInfo.socketId);
-});
+chrome.runtime.onMessageExternal.addListener(
+  function onHeartbeat(id, sender, sendResponse) {
+    console.assert(id === sender.id, "Sender's ID is incorrect.");
+    EXTENSION_ID = id;
+    chrome.runtime.onMessageExternal.removeListener(onHeartbeat);
+    chrome.sockets.tcpServer.create({}, function(createInfo) {
+      listenAndAccept(createInfo.socketId);
+    });
+  }
+);
 
 function listenAndAccept(socketId) {
   console.log("listenAndAccept " + socketId);
diff --git a/chrome/extension/background.js b/chrome/extension/background.js
index ecadac5..8acd9e8 100644
--- a/chrome/extension/background.js
+++ b/chrome/extension/background.js
@@ -1,7 +1,18 @@
 // attempt to keep app from going inactive
 
-chrome.alarms.create("ping", {when: 5000, periodInMinutes: 1 });
-chrome.alarms.onAlarm.addListener(function(alarm) { console.info("alarm name = " + alarm.name); });
+chrome.alarms.create("heartbeat", {when: 5000, periodInMinutes: 1 });
+chrome.alarms.onAlarm.addListener(function(alarm) { 
+  chrome.management.getAll(function(extensions) {
+    var app_id;
+    for (var i = 0; i < extensions.length; i++) {
+      if (extensions[i].name === "meek-browser-app") {
+        app_id = extensions[i].id;
+        break;
+      }
+    }
+    chrome.runtime.sendMessage(app_id, chrome.runtime.id);
+  });
+});
 
 var host = 'meek-reflect.appspot.com';
 
diff --git a/chrome/extension/manifest.json b/chrome/extension/manifest.json
index 0e245db..c6bf6a3 100644
--- a/chrome/extension/manifest.json
+++ b/chrome/extension/manifest.json
@@ -7,6 +7,7 @@
   "permissions": [
     "notifications",
     "alarms",
+    "management",
     "webRequest",
     "webRequestBlocking",
     "<all_urls>"



More information about the tor-commits mailing list