Class: Turbo::WebViewBridge

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#delegateObject

, :navigationDelegate



5
6
7
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5

def delegate
  @delegate
end

#pageLoadDelegateObject

, :navigationDelegate



5
6
7
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5

def pageLoadDelegate
  @pageLoadDelegate
end

#visitDelegateObject

, :navigationDelegate



5
6
7
# File 'lib/turbo/web_view/web_view_bridge.rb', line 5

def visitDelegate
  @visitDelegate
end

#webViewObject

, :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

#clearSnapshotCacheObject



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(message)
  message = ScriptMessage.parse(message)
  return unless message

  if message.name.to_sym != :log
    Turbo.logger.debug("[Bridge] ← #{message.name} #{message.data}", :bright_black)
  end

  case message.name.to_sym
  when :page_loaded
    pageLoadDelegate.webView(self, didLoadPageWithRestorationIdentifier: message.restorationIdentifier) if pageLoadDelegate
  when :page_load_failed
    delegate.webView(self, didFailInitialPageLoadWithError: TurboError.pageLoadFailure) if delegate
  when :form_submission_started
    delegate.webView(self, didStartFormSubmissionToLocation: message.location) if delegate
  when :form_submission_finished
    delegate.webView(self, didFinishFormSubmissionToLocation: message.location) if delegate
  when :page_invalidated
    delegate.webViewDidInvalidatePage(self) if delegate
  when :visit_proposed
    delegate.webView(self, didProposeVisitToLocation: message.location, withOptions: message.options) if delegate
  when :visit_started
    visitDelegate.webView(self, didStartVisitWithIdentifier: message.identifier, hasCachedSnapshot: message.data["hasCachedSnapshot"]) if visitDelegate
  when :visit_request_started
    visitDelegate.webView(self, didStartRequestForVisitWithIdentifier: message.identifier, date: message.date) if visitDelegate
  when :visit_request_completed
    visitDelegate.webView(self, didCompleteRequestForVisitWithIdentifier: message.identifier) if visitDelegate
  when :visit_request_failed
    visitDelegate.webView(self, didFailRequestForVisitWithIdentifier: message.identifier, statusCode: message.data["statusCode"]) if visitDelegate
  when :visit_request_finished
    visitDelegate.webView(self, didFinishRequestForVisitWithIdentifier: message.identifier, date: message.date) if visitDelegate
  when :visit_rendered
    visitDelegate.webView(self, didRenderForVisitWithIdentifier: message.identifier) if visitDelegate
  when :visit_completed
    visitDelegate.webView(self, didCompleteVisitWithIdentifier: message.identifier, restorationIdentifier: message.restorationIdentifier) if visitDelegate
  when :error_raised
    error = message.data["error"] || "<unknown error>"
    Turbo.logger.debug("JavaScript error: #{error}", :red)
  when :log
    msg = message.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: options, restorationIdentifier: restorationIdentifier)
  raise unless options.is_a? Turbo::VisitOptions
  callJavaScriptFunction("window.turboNative.visitLocationWithOptionsAndRestorationIdentifier",
    withArguments: [
      location.absoluteString,
      options.encode,
      restorationIdentifier]
  )
end