From 86d85df5d3c0ab8bd1b8f508891cfc327a5a2fc8 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Mon, 23 Mar 2020 17:52:42 +0200 Subject: [PATCH] Use atom for webview ref instead of storing it in re-frame's db Signed-off-by: Vitaliy Vlasov --- src/status_im/browser/core.cljs | 35 +++++++++---------- src/status_im/browser/permissions.cljs | 16 ++++----- src/status_im/browser/webview_ref.cljs | 3 ++ src/status_im/ui/screens/browser/views.cljs | 25 ++++++------- src/status_im/ui/screens/db.cljs | 2 -- .../ui/screens/routing/browser_stack.cljs | 6 ++-- .../status_im/test/browser/permissions.cljs | 13 +++---- 7 files changed, 48 insertions(+), 52 deletions(-) create mode 100644 src/status_im/browser/webview_ref.cljs diff --git a/src/status_im/browser/core.cljs b/src/status_im/browser/core.cljs index 1a9b5eee8a..e4eadbb34c 100644 --- a/src/status_im/browser/core.cljs +++ b/src/status_im/browser/core.cljs @@ -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 - :messageId message-id - :error message} - :webview webview-bridge}}) + {:type constants/web3-send-async-callback + :messageId message-id + :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 - :messageId message-id - :result {:jsonrpc "2.0" - :id (int id) - :result result}} - :webview webview-bridge}}) + {:type constants/web3-send-async-callback + :messageId message-id + :result {:jsonrpc "2.0" + :id (int id) + :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]}] - (when (and message webview) - (.sendToBridge webview (types/clj->json message))))) + (fn [message] + (let [webview @webview-ref/webview-ref] + (when (and message webview) + (.sendToBridge webview (types/clj->json message)))))) (re-frame/reg-fx :browser/call-rpc diff --git a/src/status_im/browser/permissions.cljs b/src/status_im/browser/permissions.cljs index 2f0f11130f..6fd381acd2 100644 --- a/src/status_im/browser/permissions.cljs +++ b/src/status_im/browser/permissions.cljs @@ -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 - :isAllowed allowed? - :permission permission - :messageId message-id} - allowed? - (assoc :data data)) - :webview (:webview-bridge db)}}) + {:browser/send-to-bridge (cond-> {:type constants/api-response + :isAllowed allowed? + :permission permission + :messageId message-id} + allowed? + (assoc :data data))}) (fx/defn update-dapp-permissions [{:keys [db]} dapp-name permission allowed?] diff --git a/src/status_im/browser/webview_ref.cljs b/src/status_im/browser/webview_ref.cljs new file mode 100644 index 0000000000..9a07759c29 --- /dev/null +++ b/src/status_im/browser/webview_ref.cljs @@ -0,0 +1,3 @@ +(ns status-im.browser.webview-ref) + +(def webview-ref (atom nil)) diff --git a/src/status_im/ui/screens/browser/views.cljs b/src/status_im/ui/screens/browser/views.cljs index a51a3f22eb..99e97352e2 100644 --- a/src/status_im/ui/screens/browser/views.cljs +++ b/src/status_im/ui/screens/browser/views.cljs @@ -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 @@ -177,4 +174,4 @@ :show-permission show-permission :show-tooltip show-tooltip :name name - :dapps-account dapps-account}]]))) \ No newline at end of file + :dapps-account dapps-account}]]))) diff --git a/src/status_im/ui/screens/db.cljs b/src/status_im/ui/screens/db.cljs index 96153e5da1..0c15c5a96f 100644 --- a/src/status_im/ui/screens/db.cljs +++ b/src/status_im/ui/screens/db.cljs @@ -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 diff --git a/src/status_im/ui/screens/routing/browser_stack.cljs b/src/status_im/ui/screens/routing/browser_stack.cljs index 69eb71bdcd..5c8b7fbaae 100644 --- a/src/status_im/ui/screens/routing/browser_stack.cljs +++ b/src/status_im/ui/screens/routing/browser_stack.cljs @@ -10,5 +10,7 @@ :header-mode :none} [{:name :open-dapp :component open-dapp/open-dapp} - {:name :browser - :component browser/browser}]]) + {:name :browser + :back-handler :noop + :options {:animationEnabled false} + :component browser/browser}]]) diff --git a/test/cljs/status_im/test/browser/permissions.cljs b/test/cljs/status_im/test/browser/permissions.cljs index 1ec48e7a0a..7dd2315e83 100644 --- a/test/cljs/status_im/test/browser/permissions.cljs +++ b/test/cljs/status_im/test/browser/permissions.cljs @@ -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