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

View File

@ -4,7 +4,8 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.qr-scanner.core :as qr-scanner] [status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.screens.navigation :as navigation] [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 process-next-permission)
(declare send-response-to-bridge) (declare send-response-to-bridge)
@ -50,13 +51,12 @@
"Send response to the bridge. If the permission is allowed, send data associated "Send response to the bridge. If the permission is allowed, send data associated
with the permission" with the permission"
[{:keys [db] :as cofx} permission message-id allowed? data] [{: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? :isAllowed allowed?
:permission permission :permission permission
:messageId message-id} :messageId message-id}
allowed? allowed?
(assoc :data data)) (assoc :data data))})
:webview (:webview-bridge db)}})
(fx/defn update-dapp-permissions (fx/defn update-dapp-permissions
[{:keys [db]} dapp-name permission allowed?] [{: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.utils.js-resources :as js-res]
[status-im.ui.components.chat-icon.screen :as chat-icon] [status-im.ui.components.chat-icon.screen :as chat-icon]
[status-im.ui.screens.browser.accounts :as accounts] [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 (:require-macros
[status-im.utils.slurp :refer [slurp]] [status-im.utils.slurp :refer [slurp]]
[status-im.utils.views :as views])) [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)] (let [url-text (atom url)]
[react/view styles/toolbar-content [react/view styles/toolbar-content
[react/touchable-highlight {:on-press #(re-frame/dispatch [:browser.ui/lock-pressed secure?])} [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 [react/touchable-highlight {:style styles/url-text-container
:on-press #(re-frame/dispatch [:browser.ui/url-input-pressed])} :on-press #(re-frame/dispatch [:browser.ui/url-input-pressed])}
[react/text (http/url-host url-original)]]) [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} :accessibility-label :refresh-page-button}
[icons/icon :main-icons/refresh]]])) [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 [toolbar.view/toolbar
{:browser? true} {:browser? true}
[toolbar.view/nav-button [toolbar.view/nav-button
@ -60,7 +61,7 @@
(re-frame/dispatch [:navigate-back]) (re-frame/dispatch [:navigate-back])
(when error? (when error?
(re-frame/dispatch [:browser.ui/remove-browser-pressed browser-id]))))] (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] (defn- web-view-error [_ code desc]
(reagent/as-element (reagent/as-element
@ -120,9 +121,7 @@
[components.webview-bridge/webview-bridge [components.webview-bridge/webview-bridge
{:dapp? dapp? {:dapp? dapp?
:dapp-name name :dapp-name name
:ref #(do :ref #(reset! webview-ref/webview-ref %)
(reset! webview %)
(re-frame/dispatch [:set :webview-bridge %]))
:source (when (and url (not resolving?)) {:uri url}) :source (when (and url (not resolving?)) {:uri url})
:java-script-enabled true :java-script-enabled true
:bounces false :bounces false
@ -148,8 +147,7 @@
#(re-frame/dispatch [:browser.ui/close-tooltip-pressed])])]) #(re-frame/dispatch [:browser.ui/close-tooltip-pressed])])])
(views/defview browser [] (views/defview browser []
(views/letsubs [webview (atom nil) (views/letsubs [window-width [:dimensions/window-width]
window-width [:dimensions/window-width]
{:keys [browser-id dapp? name unsafe?] :as browser} [:get-current-browser] {:keys [browser-id dapp? name unsafe?] :as browser} [:get-current-browser]
{:keys [url error? loading? url-editing? show-tooltip show-permission resolving?]} [:browser/options] {:keys [url error? loading? url-editing? show-tooltip show-permission resolving?]} [:browser/options]
dapps-account [:dapps-account] dapps-account [:dapps-account]
@ -158,12 +156,11 @@
can-go-forward? (browser/can-go-forward? browser) can-go-forward? (browser/can-go-forward? browser)
url-original (browser/get-current-url browser)] url-original (browser/get-current-url browser)]
[react/view {:style styles/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 [react/view
(when loading? (when loading?
[connectivity/loading-indicator window-width])] [connectivity/loading-indicator window-width])]
[browser-component {:webview webview [browser-component {:dapp? dapp?
:dapp? dapp?
:error? error? :error? error?
:url url :url url
:url-original url-original :url-original url-original

View File

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

View File

@ -10,5 +10,7 @@
:header-mode :none} :header-mode :none}
[{:name :open-dapp [{:name :open-dapp
:component open-dapp/open-dapp} :component open-dapp/open-dapp}
{:name :browser {:name :browser
:component browser/browser}]]) :back-handler :noop
:options {:animationEnabled false}
:component browser/browser}]])

View File

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