From db776b5a02187d4545e3fb2b9dfd33d838145af7 Mon Sep 17 00:00:00 2001
From: Paul Bouchon <mail@bitpshr.net>
Date: Wed, 30 Jan 2019 13:27:33 -0500
Subject: EIP-1102 updates (#6006)

* Update privacy notice

* Respond to 1102 messages using tab ID
---
 app/scripts/controllers/provider-approval.js | 46 +++++++++++++++-------------
 1 file changed, 24 insertions(+), 22 deletions(-)

(limited to 'app/scripts/controllers/provider-approval.js')

diff --git a/app/scripts/controllers/provider-approval.js b/app/scripts/controllers/provider-approval.js
index 53172c069..b8cf04341 100644
--- a/app/scripts/controllers/provider-approval.js
+++ b/app/scripts/controllers/provider-approval.js
@@ -27,19 +27,19 @@ class ProviderApprovalController {
     })
 
     if (platform && platform.addMessageListener) {
-      platform.addMessageListener(({ action = '', force, origin, siteTitle, siteImage }) => {
+      platform.addMessageListener(({ action = '', force, origin, siteTitle, siteImage }, { tab }) => {
         switch (action) {
           case 'init-provider-request':
-            this._handleProviderRequest(origin, siteTitle, siteImage, force)
+            this._handleProviderRequest(origin, siteTitle, siteImage, force, tab.id)
             break
           case 'init-is-approved':
-            this._handleIsApproved(origin)
+            this._handleIsApproved(origin, tab.id)
             break
           case 'init-is-unlocked':
-            this._handleIsUnlocked()
+            this._handleIsUnlocked(tab.id)
             break
           case 'init-privacy-request':
-            this._handlePrivacyRequest()
+            this._handlePrivacyRequest(tab.id)
             break
         }
       })
@@ -53,11 +53,11 @@ class ProviderApprovalController {
    * @param {string} siteTitle - The title of the document requesting full provider access
    * @param {string} siteImage - The icon of the window requesting full provider access
    */
-  _handleProviderRequest (origin, siteTitle, siteImage, force) {
-    this.store.updateState({ providerRequests: [{ origin, siteTitle, siteImage }] })
+  _handleProviderRequest (origin, siteTitle, siteImage, force, tabID) {
+    this.store.updateState({ providerRequests: [{ origin, siteTitle, siteImage, tabID }] })
     const isUnlocked = this.keyringController.memStore.getState().isUnlocked
     if (!force && this.approvedOrigins[origin] && this.caching && isUnlocked) {
-      this.approveProviderRequest(origin)
+      this.approveProviderRequest(tabID)
       return
     }
     this.openPopup && this.openPopup()
@@ -68,32 +68,32 @@ class ProviderApprovalController {
    *
    * @param {string} origin - Origin of the window
    */
-  _handleIsApproved (origin) {
+  _handleIsApproved (origin, tabID) {
     this.platform && this.platform.sendMessage({
       action: 'answer-is-approved',
       isApproved: this.approvedOrigins[origin] && this.caching,
       caching: this.caching,
-    }, { active: true })
+    }, { id: tabID })
   }
 
   /**
    * Called by a tab to determine if MetaMask is currently locked or unlocked
    */
-  _handleIsUnlocked () {
+  _handleIsUnlocked (tabID) {
     const isUnlocked = this.keyringController.memStore.getState().isUnlocked
-    this.platform && this.platform.sendMessage({ action: 'answer-is-unlocked', isUnlocked }, { active: true })
+    this.platform && this.platform.sendMessage({ action: 'answer-is-unlocked', isUnlocked }, { id: tabID })
   }
 
   /**
    * Called to check privacy mode; if privacy mode is off, this will automatically enable the provider (legacy behavior)
    */
-  _handlePrivacyRequest () {
+  _handlePrivacyRequest (tabID) {
     const privacyMode = this.preferencesController.getFeatureFlags().privacyMode
     if (!privacyMode) {
       this.platform && this.platform.sendMessage({
         action: 'approve-legacy-provider-request',
         selectedAddress: this.publicConfigStore.getState().selectedAddress,
-      }, { active: true })
+      }, { id: tabID })
       this.publicConfigStore.emit('update', this.publicConfigStore.getState())
     }
   }
@@ -101,17 +101,18 @@ class ProviderApprovalController {
   /**
    * Called when a user approves access to a full Ethereum provider API
    *
-   * @param {string} origin - Origin of the target window to approve provider access
+   * @param {string} tabID - ID of the target window that approved provider access
    */
-  approveProviderRequest (origin) {
+  approveProviderRequest (tabID) {
     this.closePopup && this.closePopup()
     const requests = this.store.getState().providerRequests
+    const origin = requests.find(request => request.tabID === tabID).origin
     this.platform && this.platform.sendMessage({
       action: 'approve-provider-request',
       selectedAddress: this.publicConfigStore.getState().selectedAddress,
-    }, { active: true })
+    }, { id: tabID })
     this.publicConfigStore.emit('update', this.publicConfigStore.getState())
-    const providerRequests = requests.filter(request => request.origin !== origin)
+    const providerRequests = requests.filter(request => request.tabID !== tabID)
     this.store.updateState({ providerRequests })
     this.approvedOrigins[origin] = true
   }
@@ -119,13 +120,14 @@ class ProviderApprovalController {
   /**
    * Called when a tab rejects access to a full Ethereum provider API
    *
-   * @param {string} origin - Origin of the target window to reject provider access
+   * @param {string} tabID - ID of the target window that rejected provider access
    */
-  rejectProviderRequest (origin) {
+  rejectProviderRequest (tabID) {
     this.closePopup && this.closePopup()
     const requests = this.store.getState().providerRequests
-    this.platform && this.platform.sendMessage({ action: 'reject-provider-request' }, { active: true })
-    const providerRequests = requests.filter(request => request.origin !== origin)
+    const origin = requests.find(request => request.tabID === tabID).origin
+    this.platform && this.platform.sendMessage({ action: 'reject-provider-request' }, { id: tabID })
+    const providerRequests = requests.filter(request => request.tabID !== tabID)
     this.store.updateState({ providerRequests })
     delete this.approvedOrigins[origin]
   }
-- 
cgit