From d0fc352d3bd644cba6a563d068f482c5fa28b3d0 Mon Sep 17 00:00:00 2001
From: David Rowe <david@ctrlaltstudio.com>
Date: Mon, 27 Mar 2017 16:07:58 +1300
Subject: [PATCH] Address event bridge being shared with other scripts

---
 scripts/system/html/js/SnapshotReview.js | 45 ++++++++++++++++++------
 scripts/system/snapshot.js               | 16 ++++++---
 2 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/scripts/system/html/js/SnapshotReview.js b/scripts/system/html/js/SnapshotReview.js
index a1bb350789..efe30196ee 100644
--- a/scripts/system/html/js/SnapshotReview.js
+++ b/scripts/system/html/js/SnapshotReview.js
@@ -43,7 +43,13 @@ function addImage(data) {
 function handleShareButtons(shareMsg) {
     var openFeed = document.getElementById('openFeed');
     openFeed.checked = shareMsg.openFeedAfterShare;
-    openFeed.onchange = function () { EventBridge.emitWebEvent(openFeed.checked ? 'setOpenFeedTrue' : 'setOpenFeedFalse'); };
+    openFeed.onchange = function () {
+        EventBridge.emitWebEvent(JSON.stringify({
+            type: "snapshot",
+            action: (openFeed.checked ? "setOpenFeedTrue" : "setOpenFeedFalse")
+        }));
+    };
+
     if (!shareMsg.canShare) {
         // this means you may or may not be logged in, but can't share
         // because you are not in a public place.
@@ -57,25 +63,42 @@ window.onload = function () {
     openEventBridge(function () {
         // Set up a handler for receiving the data, and tell the .js we are ready to receive it.
         EventBridge.scriptEventReceived.connect(function (message) {
+            message = JSON.parse(message);
+            if (message.type !== "snapshot") {
+                return;
+            }
+
             // last element of list contains a bool for whether or not we can share stuff
-            var shareMsg = message.pop();
+            var shareMsg = message.action.pop();
             handleShareButtons(shareMsg);
             
             // rest are image paths which we add
-            useCheckboxes = message.length > 1;
-            message.forEach(addImage);
+            useCheckboxes = message.action.length > 1;
+            message.action.forEach(addImage);
         });
-        EventBridge.emitWebEvent('ready');
+        EventBridge.emitWebEvent(JSON.stringify({
+            type: "snapshot",
+            action: "ready"
+        }));
     });
 
 };
 // beware of bug: Cannot send objects at top level. (Nested in arrays is fine.)
 function shareSelected() {
-    EventBridge.emitWebEvent(paths);
-};
+    EventBridge.emitWebEvent(JSON.stringify({
+        type: "snapshot",
+        action: paths
+    }));
+}
 function doNotShare() {
-    EventBridge.emitWebEvent([]);
-};
+    EventBridge.emitWebEvent(JSON.stringify({
+        type: "snapshot",
+        action: []
+    }));
+}
 function snapshotSettings() {
-    EventBridge.emitWebEvent("openSettings");
-};
+    EventBridge.emitWebEvent(JSON.stringify({
+        type: "snapshot",
+        action: "openSettings"
+    }));
+}
diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js
index 8a48aeb0ff..d25a208417 100644
--- a/scripts/system/snapshot.js
+++ b/scripts/system/snapshot.js
@@ -47,11 +47,19 @@ function confirmShare(data) {
         // 2. Although we currently use a single image, we would like to take snapshot, a selfie, a 360 etc. all at the
         //    same time, show the user all of them, and have the user deselect any that they do not want to share.
         //    So we'll ultimately be receiving a set of objects, perhaps with different post processing for each.
+        message = JSON.parse(message);
+        if (message.type !== "snapshot") {
+            return;
+        }
+
         var isLoggedIn;
         var needsLogin = false;
-        switch (message) {
-        case 'ready':
-            tablet.emitScriptEvent(data); // Send it.
+        switch (message.action) {
+        case 'ready':  // Send it.
+            tablet.emitScriptEvent(JSON.stringify({
+                type: "snapshot",
+                action: data
+            })); 
             outstanding = 0;
             break;
         case 'openSettings':
@@ -67,7 +75,7 @@ function confirmShare(data) {
             tablet.webEventReceived.disconnect(onMessage);
             HMD.closeTablet();
             isLoggedIn = Account.isLoggedIn();
-            message.forEach(function (submessage) {
+            message.action.forEach(function (submessage) {
                 if (submessage.share && !isLoggedIn) {
                     needsLogin = true;
                     submessage.share = false;