Use atom for webview ref instead of storing it in re-frame's db

Signed-off-by: Vitaliy Vlasov <siphiuel@gmail.com>
This commit is contained in:
Vitaliy Vlasov 2020-03-23 17:52:42 +02:00
parent b6e0bb0a0e
commit 86d85df5d3
No known key found for this signature in database
GPG Key ID: A7D57C347F2B2964
7 changed files with 48 additions and 52 deletions

View File

@ -23,7 +23,8 @@
[taoensso.timbre :as log]
[status-im.signing.core :as signing]
[status-im.multiaccounts.update.core :as multiaccounts.update]
[status-im.ui.components.bottom-sheet.core :as bottom-sheet]))
[status-im.ui.components.bottom-sheet.core :as bottom-sheet]
[status-im.browser.webview-ref :as webview-ref]))
(fx/defn update-browser-option
[{:keys [db]} option-key option-value]
@ -283,24 +284,22 @@
(fx/defn web3-error-callback
{:events [:browser.dapp/transaction-on-error]}
[{{:keys [webview-bridge]} :db} message-id message]
[cofx message-id message]
{:browser/send-to-bridge
{:message {:type constants/web3-send-async-callback
{:type constants/web3-send-async-callback
:messageId message-id
:error message}
:webview webview-bridge}})
:error message}})
(fx/defn dapp-complete-transaction
{:events [:browser.dapp/transaction-on-result]}
[{{:keys [webview-bridge]} :db} message-id id result]
[cofx message-id id result]
;;TODO check and test id
{:browser/send-to-bridge
{:message {:type constants/web3-send-async-callback
{:type constants/web3-send-async-callback
:messageId message-id
:result {:jsonrpc "2.0"
:id (int id)
:result result}}
:webview webview-bridge}})
:result result}}})
(defn normalize-sign-message-params
"NOTE (andrey) we need this function, because params may be mixed up"
@ -315,8 +314,7 @@
(fx/defn send-to-bridge
[cofx message]
{:browser/send-to-bridge {:message message
:webview (get-in cofx [:db :webview-bridge])}})
{:browser/send-to-bridge message})
(fx/defn web3-send-async
[cofx {:keys [method params id] :as payload} message-id]
@ -418,9 +416,10 @@
(re-frame/reg-fx
:browser/send-to-bridge
(fn [{:keys [message webview]}]
(fn [message]
(let [webview @webview-ref/webview-ref]
(when (and message webview)
(.sendToBridge webview (types/clj->json message)))))
(.sendToBridge webview (types/clj->json message))))))
(re-frame/reg-fx
:browser/call-rpc

View File

@ -4,7 +4,8 @@
[status-im.i18n :as i18n]
[status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.fx :as fx]))
[status-im.utils.fx :as fx]
[status-im.browser.webview-ref :as webview-ref]))
(declare process-next-permission)
(declare send-response-to-bridge)
@ -50,13 +51,12 @@
"Send response to the bridge. If the permission is allowed, send data associated
with the permission"
[{:keys [db] :as cofx} permission message-id allowed? data]
{:browser/send-to-bridge {:message (cond-> {:type constants/api-response
{:browser/send-to-bridge (cond-> {:type constants/api-response
:isAllowed allowed?
:permission permission
:messageId message-id}
allowed?
(assoc :data data))
:webview (:webview-bridge db)}})
(assoc :data data))})
(fx/defn update-dapp-permissions
[{:keys [db]} dapp-name permission allowed?]

View File

@ -0,0 +1,3 @@
(ns status-im.browser.webview-ref)
(def webview-ref (atom nil))

View File

@ -21,12 +21,13 @@
[status-im.utils.js-resources :as js-res]
[status-im.ui.components.chat-icon.screen :as chat-icon]
[status-im.ui.screens.browser.accounts :as accounts]
[status-im.utils.debounce :as debounce])
[status-im.utils.debounce :as debounce]
[status-im.browser.webview-ref :as webview-ref])
(:require-macros
[status-im.utils.slurp :refer [slurp]]
[status-im.utils.views :as views]))
(defn toolbar-content [url url-original {:keys [secure?]} url-editing? webview]
(defn toolbar-content [url url-original {:keys [secure?]} url-editing?]
(let [url-text (atom url)]
[react/view styles/toolbar-content
[react/touchable-highlight {:on-press #(re-frame/dispatch [:browser.ui/lock-pressed secure?])}
@ -47,11 +48,11 @@
[react/touchable-highlight {:style styles/url-text-container
:on-press #(re-frame/dispatch [:browser.ui/url-input-pressed])}
[react/text (http/url-host url-original)]])
[react/touchable-highlight {:on-press #(.reload @webview)
[react/touchable-highlight {:on-press #(.reload @webview-ref/webview-ref)
:accessibility-label :refresh-page-button}
[icons/icon :main-icons/refresh]]]))
(defn toolbar [error? url url-original browser browser-id url-editing? webview]
(defn toolbar [error? url url-original browser browser-id url-editing?]
[toolbar.view/toolbar
{:browser? true}
[toolbar.view/nav-button
@ -60,7 +61,7 @@
(re-frame/dispatch [:navigate-back])
(when error?
(re-frame/dispatch [:browser.ui/remove-browser-pressed browser-id]))))]
[toolbar-content url url-original browser url-editing? webview]])
[toolbar-content url url-original browser url-editing?]])
(defn- web-view-error [_ code desc]
(reagent/as-element
@ -120,9 +121,7 @@
[components.webview-bridge/webview-bridge
{:dapp? dapp?
:dapp-name name
:ref #(do
(reset! webview %)
(re-frame/dispatch [:set :webview-bridge %]))
:ref #(reset! webview-ref/webview-ref %)
:source (when (and url (not resolving?)) {:uri url})
:java-script-enabled true
:bounces false
@ -148,8 +147,7 @@
#(re-frame/dispatch [:browser.ui/close-tooltip-pressed])])])
(views/defview browser []
(views/letsubs [webview (atom nil)
window-width [:dimensions/window-width]
(views/letsubs [window-width [:dimensions/window-width]
{:keys [browser-id dapp? name unsafe?] :as browser} [:get-current-browser]
{:keys [url error? loading? url-editing? show-tooltip show-permission resolving?]} [:browser/options]
dapps-account [:dapps-account]
@ -158,12 +156,11 @@
can-go-forward? (browser/can-go-forward? browser)
url-original (browser/get-current-url browser)]
[react/view {:style styles/browser}
[toolbar error? url url-original browser browser-id url-editing? webview]
[toolbar error? url url-original browser browser-id url-editing?]
[react/view
(when loading?
[connectivity/loading-indicator window-width])]
[browser-component {:webview webview
:dapp? dapp?
[browser-component {:dapp? dapp?
:error? error?
:url url
:url-original url-original

View File

@ -68,7 +68,6 @@
(spec/def ::web3 (spec/nilable any?))
(spec/def ::web3-node-version (spec/nilable string?))
;;object?
(spec/def ::webview-bridge (spec/nilable any?))
;;height of native keyboard if shown
(spec/def ::keyboard-height (spec/nilable number?))
@ -252,7 +251,6 @@
::initial-props
::web3
::web3-node-version
::webview-bridge
::keyboard-height
::keyboard-max-height
::network-status

View File

@ -11,4 +11,6 @@
[{:name :open-dapp
:component open-dapp/open-dapp}
{:name :browser
:back-handler :noop
:options {:animationEnabled false}
:component browser/browser}]])

View File

@ -21,7 +21,7 @@
:host dapp-name
:messageId 0
:permission "FAKE_PERMISSION"}))]
(is (not (get-in result-ask [:browser/send-to-bridge :message :isAllowed])))))
(is (not (get-in result-ask [:browser/send-to-bridge :isAllowed])))))
(testing "receiving a supported permission"
(let [result-ask (browser/process-bridge-message cofx
@ -35,7 +35,7 @@
(testing "then user accepts the supported permission"
(let [accept-result (permissions/allow-permission {:db (:db result-ask)})]
(is (= (get-in accept-result [:browser/send-to-bridge :message])
(is (= (get accept-result :browser/send-to-bridge)
{:type "api-response"
:messageId 1
:isAllowed true
@ -52,8 +52,7 @@
:host dapp-name
:messageId 2
:permission "contact-code"}))]
(is (= (get-in result-ask-again
[:browser/send-to-bridge :message])
(is (= (get result-ask-again :browser/send-to-bridge)
{:type "api-response"
:isAllowed true
:messageId 2
@ -71,8 +70,7 @@
(is (= (get-in result-ask2 [:db :dapps/permissions])
{"test.com" {:dapp "test.com", :permissions ["contact-code"]}})
"there should only be permissions for dapp-name at that point")
(is (nil? (get-in result-ask2
[:browser/send-to-bridge :message]))
(is (nil? (get result-ask2 :browser/send-to-bridge))
"no message should be sent to the bridge")
(testing "then user accepts permission for dapp-name2"
@ -81,8 +79,7 @@
{"test.com" {:dapp "test.com" :permissions ["contact-code"]}
"test2.org" {:dapp "test2.org" :permissions ["contact-code"]}})
"there should be permissions for both dapps now")
(is (= (get-in accept-result2
[:browser/send-to-bridge :message])
(is (= (get accept-result2 :browser/send-to-bridge)
{:type "api-response"
:isAllowed true
:messageId 3