Class: Turbo::WebViewBridge
- Inherits:
-
Object
- Object
- Turbo::WebViewBridge
- Defined in:
- lib/turbo/web_view/web_view_bridge.rb
Overview
The WebViewBridge is an internal class used for bi-directional communication with the web view/JavaScript
Instance Attribute Summary collapse
-
#delegate ⇒ Object
, :navigationDelegate.
-
#pageLoadDelegate ⇒ Object
, :navigationDelegate.
-
#visitDelegate ⇒ Object
, :navigationDelegate.
-
#webView ⇒ Object
, :navigationDelegate.
Instance Method Summary collapse
-
#callJavaScriptFunction(functionExpression, withArguments: arguments, completionHandler: completionHandler) ⇒ Object
JavaScript Evaluation.
- #cancelVisitWithIdentifier(identifier) ⇒ Object
- #clearSnapshotCache ⇒ Object
- #encodeJavaScriptArguments(arguments) ⇒ Object
- #initWithWebView(webView) ⇒ Object
- #scriptForCallingJavaScriptFunction(functionExpression, withArguments: arguments) ⇒ Object
- #scriptMessageHandlerDidReceiveMessage(message) ⇒ Object
- #visitLocation(location, withOptions: options, restorationIdentifier: restorationIdentifier) ⇒ Object
Instance Attribute Details
#delegate ⇒ Object
, :navigationDelegate
5 6 7 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5 def delegate @delegate end |
#pageLoadDelegate ⇒ Object
, :navigationDelegate
5 6 7 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5 def pageLoadDelegate @pageLoadDelegate end |
#visitDelegate ⇒ Object
, :navigationDelegate
5 6 7 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5 def visitDelegate @visitDelegate end |
#webView ⇒ Object
, :navigationDelegate
5 6 7 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5 def webView @webView end |
Instance Method Details
#callJavaScriptFunction(functionExpression, withArguments: arguments, completionHandler: completionHandler) ⇒ Object
JavaScript Evaluation
54 55 56 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 54 def callJavaScriptFunction(functionExpression, withArguments: arguments) callJavaScriptFunction(functionExpression, withArguments: arguments, completionHandler: nil) end |
#cancelVisitWithIdentifier(identifier) ⇒ Object
48 49 50 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 48 def cancelVisitWithIdentifier(identifier) callJavaScriptFunction("window.turboNative.cancelVisitWithIdentifier", withArguments: [identifier]) end |
#clearSnapshotCache ⇒ Object
44 45 46 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 44 def clearSnapshotCache callJavaScriptFunction("window.turboNative.clearSnapshotCache", withArguments: []) end |
#encodeJavaScriptArguments(arguments) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 99 def encodeJavaScriptArguments(arguments) arguments = arguments.map {|v| v.nil? ? NSNull.alloc.init() : v } data = NSJSONSerialization.dataWithJSONObject(arguments, options: 0, error: nil) if data dataString = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding) return dataString[1..-2] end return nil end |
#initWithWebView(webView) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 6 def initWithWebView(webView) @webView = webView setup self end |
#scriptForCallingJavaScriptFunction(functionExpression, withArguments: arguments) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 83 def scriptForCallingJavaScriptFunction(functionExpression, withArguments: arguments) encodedArguments = encodeJavaScriptArguments(arguments) return unless encodedArguments script = "(function(result) {\n" + " try {\n" + " result.value = " + functionExpression + "(" + encodedArguments + ")\n" + " } catch (error) {\n" + " result.error = error.toString()\n" + " result.stack = error.stack\n" + " }\n" + " return result\n" + "})({})" return script end |
#scriptMessageHandlerDidReceiveMessage(message) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 110 def scriptMessageHandlerDidReceiveMessage() = ScriptMessage.parse() return unless if .name.to_sym != :log Turbo.logger.debug("[Bridge] ← #{.name} #{.data}", :bright_black) end case .name.to_sym when :page_loaded pageLoadDelegate.webView(self, didLoadPageWithRestorationIdentifier: .restorationIdentifier) if pageLoadDelegate when :page_load_failed delegate.webView(self, didFailInitialPageLoadWithError: TurboError.pageLoadFailure) if delegate when :form_submission_started delegate.webView(self, didStartFormSubmissionToLocation: .location) if delegate when :form_submission_finished delegate.webView(self, didFinishFormSubmissionToLocation: .location) if delegate when :page_invalidated delegate.webViewDidInvalidatePage(self) if delegate when :visit_proposed delegate.webView(self, didProposeVisitToLocation: .location, withOptions: .) if delegate when :visit_started visitDelegate.webView(self, didStartVisitWithIdentifier: .identifier, hasCachedSnapshot: .data["hasCachedSnapshot"]) if visitDelegate when :visit_request_started visitDelegate.webView(self, didStartRequestForVisitWithIdentifier: .identifier, date: .date) if visitDelegate when :visit_request_completed visitDelegate.webView(self, didCompleteRequestForVisitWithIdentifier: .identifier) if visitDelegate when :visit_request_failed visitDelegate.webView(self, didFailRequestForVisitWithIdentifier: .identifier, statusCode: .data["statusCode"]) if visitDelegate when :visit_request_finished visitDelegate.webView(self, didFinishRequestForVisitWithIdentifier: .identifier, date: .date) if visitDelegate when :visit_rendered visitDelegate.webView(self, didRenderForVisitWithIdentifier: .identifier) if visitDelegate when :visit_completed visitDelegate.webView(self, didCompleteVisitWithIdentifier: .identifier, restorationIdentifier: .restorationIdentifier) if visitDelegate when :error_raised error = .data["error"] || "<unknown error>" Turbo.logger.debug("JavaScript error: #{error}", :red) when :log msg = .data["message"] Turbo.logger.debug("[Bridge] ← log: #{msg}", :bright_black) if msg.is_a?(String) end end |
#visitLocation(location, withOptions: options, restorationIdentifier: restorationIdentifier) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/turbo/web_view/web_view_bridge.rb', line 34 def visitLocation(location, withOptions: , restorationIdentifier: restorationIdentifier) raise unless .is_a? Turbo::VisitOptions callJavaScriptFunction("window.turboNative.visitLocationWithOptionsAndRestorationIdentifier", withArguments: [ location.absoluteString, .encode, restorationIdentifier] ) end |