| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Event management for WebView. | 5 // Event management for WebView. |
| 6 | 6 |
| 7 var CreateEvent = require('guestViewEvents').CreateEvent; | 7 var CreateEvent = require('guestViewEvents').CreateEvent; |
| 8 var DeclarativeWebRequestSchema = | 8 var DeclarativeWebRequestSchema = |
| 9 requireNative('schema_registry').GetSchema('declarativeWebRequest'); | 9 requireNative('schema_registry').GetSchema('declarativeWebRequest'); |
| 10 var EventBindings = require('event_bindings'); | |
| 11 var GuestViewEvents = require('guestViewEvents').GuestViewEvents; | 10 var GuestViewEvents = require('guestViewEvents').GuestViewEvents; |
| 12 var GuestViewInternalNatives = requireNative('guest_view_internal'); | 11 var GuestViewInternalNatives = requireNative('guest_view_internal'); |
| 13 var IdGenerator = requireNative('id_generator'); | 12 var IdGenerator = requireNative('id_generator'); |
| 14 var WebRequestEvent = require('webRequestEvent').WebRequestEvent; | 13 var WebRequestEvent = require('webRequestEvent').WebRequestEvent; |
| 15 var WebRequestSchema = | 14 var WebRequestSchema = |
| 16 requireNative('schema_registry').GetSchema('webRequest'); | 15 requireNative('schema_registry').GetSchema('webRequest'); |
| 17 var WebViewActionRequests = | 16 var WebViewActionRequests = |
| 18 require('webViewActionRequests').WebViewActionRequests; | 17 require('webViewActionRequests').WebViewActionRequests; |
| 19 | 18 |
| 20 var WebRequestMessageEvent = CreateEvent('webViewInternal.onMessage'); | 19 var WebRequestMessageEvent = CreateEvent('webViewInternal.onMessage'); |
| 21 | 20 |
| 22 function WebViewEvents(webViewImpl) { | 21 function WebViewEvents(webViewImpl) { |
| 23 GuestViewEvents.call(this, webViewImpl); | 22 GuestViewEvents.call(this, webViewImpl); |
| 24 | 23 |
| 25 this.setupWebRequestEvents(); | 24 this.setupWebRequestEvents(); |
| 26 this.view.maybeSetupContextMenus(); | 25 this.view.maybeSetupContextMenus(); |
| 27 } | 26 } |
| 28 | 27 |
| 28 var jsEvent; |
| 29 function createCustomDeclarativeEvent(name, schema, options, webviewId) { |
| 30 if (bindingUtil) { |
| 31 return bindingUtil.createCustomDeclarativeEvent( |
| 32 name, options.actions, options.conditions, webviewId || 0); |
| 33 } |
| 34 if (!jsEvent) |
| 35 jsEvent = require('event_bindings').Event; |
| 36 return new jsEvent(name, schema, options, webviewId); |
| 37 } |
| 38 |
| 39 function createOnMessageEvent(name, schema, options, webviewId) { |
| 40 var subEventName = name + '/' + IdGenerator.GetNextId(); |
| 41 var newEvent = createCustomDeclarativeEvent(subEventName, |
| 42 schema, |
| 43 options, |
| 44 webviewId); |
| 45 |
| 46 var view = GuestViewInternalNatives.GetViewFromID(webviewId || 0); |
| 47 if (view) { |
| 48 view.events.addScopedListener( |
| 49 WebRequestMessageEvent, |
| 50 $Function.bind(function() { |
| 51 // Re-dispatch to subEvent's listeners. |
| 52 $Function.apply(newEvent.dispatch, newEvent, $Array.slice(arguments)); |
| 53 }, newEvent), |
| 54 {instanceId: webviewId || 0}); |
| 55 } |
| 56 |
| 57 return newEvent; |
| 58 } |
| 59 |
| 29 WebViewEvents.prototype.__proto__ = GuestViewEvents.prototype; | 60 WebViewEvents.prototype.__proto__ = GuestViewEvents.prototype; |
| 30 | 61 |
| 31 // A dictionary of <webview> extension events to be listened for. This | 62 // A dictionary of <webview> extension events to be listened for. This |
| 32 // dictionary augments |GuestViewEvents.EVENTS| in guest_view_events.js. See the | 63 // dictionary augments |GuestViewEvents.EVENTS| in guest_view_events.js. See the |
| 33 // documentation there for details. | 64 // documentation there for details. |
| 34 WebViewEvents.EVENTS = { | 65 WebViewEvents.EVENTS = { |
| 35 'close': { | 66 'close': { |
| 36 evt: CreateEvent('webViewInternal.onClose') | 67 evt: CreateEvent('webViewInternal.onClose') |
| 37 }, | 68 }, |
| 38 'consolemessage': { | 69 'consolemessage': { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 this.view.viewInstanceId); | 193 this.view.viewInstanceId); |
| 163 } | 194 } |
| 164 return this[webRequestEvent.name]; | 195 return this[webRequestEvent.name]; |
| 165 }); | 196 }); |
| 166 }, this); | 197 }, this); |
| 167 | 198 |
| 168 var createDeclarativeWebRequestEvent = | 199 var createDeclarativeWebRequestEvent = |
| 169 $Function.bind(function(webRequestEvent) { | 200 $Function.bind(function(webRequestEvent) { |
| 170 return this.weakWrapper(function() { | 201 return this.weakWrapper(function() { |
| 171 if (!this[webRequestEvent.name]) { | 202 if (!this[webRequestEvent.name]) { |
| 172 // The onMessage event gets a special event type because we want the | 203 var newEvent; |
| 173 // listener to fire only for messages targeted for this particular | 204 var eventName = |
| 174 // <webview>. | 205 'webViewInternal.declarativeWebRequest.' + webRequestEvent.name; |
| 175 var EventClass = webRequestEvent.name === 'onMessage' ? | 206 if (webRequestEvent.name === 'onMessage') { |
| 176 DeclarativeWebRequestEvent : EventBindings.Event; | 207 // The onMessage event gets a special event type because we want the |
| 177 this[webRequestEvent.name] = | 208 // listener to fire only for messages targeted for this particular |
| 178 new EventClass( | 209 // <webview>. |
| 179 'webViewInternal.declarativeWebRequest.' + webRequestEvent.name, | 210 newEvent = createOnMessageEvent(eventName, |
| 180 webRequestEvent.parameters, | 211 webRequestEvent.parameters, |
| 181 webRequestEvent.options, | 212 webRequestEvent.options, |
| 182 this.view.viewInstanceId); | 213 this.view.viewInstanceId); |
| 214 } else { |
| 215 newEvent = |
| 216 createCustomDeclarativeEvent(eventName, |
| 217 webRequestEvent.parameters, |
| 218 webRequestEvent.options, |
| 219 this.view.viewInstanceId); |
| 220 } |
| 221 this[webRequestEvent.name] = newEvent; |
| 183 } | 222 } |
| 184 return this[webRequestEvent.name]; | 223 return this[webRequestEvent.name]; |
| 185 }); | 224 }); |
| 186 }, this); | 225 }, this); |
| 187 | 226 |
| 188 for (var i = 0; i < DeclarativeWebRequestSchema.events.length; ++i) { | 227 for (var i = 0; i < DeclarativeWebRequestSchema.events.length; ++i) { |
| 189 var eventSchema = DeclarativeWebRequestSchema.events[i]; | 228 var eventSchema = DeclarativeWebRequestSchema.events[i]; |
| 190 var webRequestEvent = createDeclarativeWebRequestEvent(eventSchema); | 229 var webRequestEvent = createDeclarativeWebRequestEvent(eventSchema); |
| 191 Object.defineProperty( | 230 Object.defineProperty( |
| 192 request, | 231 request, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } else { | 307 } else { |
| 269 new WebViewActionRequests.PermissionRequest(this.view, event, webViewEvent); | 308 new WebViewActionRequests.PermissionRequest(this.view, event, webViewEvent); |
| 270 } | 309 } |
| 271 }; | 310 }; |
| 272 | 311 |
| 273 WebViewEvents.prototype.handleSizeChangedEvent = function(event, eventName) { | 312 WebViewEvents.prototype.handleSizeChangedEvent = function(event, eventName) { |
| 274 var webViewEvent = this.makeDomEvent(event, eventName); | 313 var webViewEvent = this.makeDomEvent(event, eventName); |
| 275 this.view.onSizeChanged(webViewEvent); | 314 this.view.onSizeChanged(webViewEvent); |
| 276 }; | 315 }; |
| 277 | 316 |
| 278 function DeclarativeWebRequestEvent(opt_eventName, | |
| 279 opt_argSchemas, | |
| 280 opt_eventOptions, | |
| 281 opt_webViewInstanceId) { | |
| 282 var subEventName = opt_eventName + '/' + IdGenerator.GetNextId(); | |
| 283 EventBindings.Event.call(this, | |
| 284 subEventName, | |
| 285 opt_argSchemas, | |
| 286 opt_eventOptions, | |
| 287 opt_webViewInstanceId); | |
| 288 | |
| 289 var view = GuestViewInternalNatives.GetViewFromID(opt_webViewInstanceId || 0); | |
| 290 if (!view) { | |
| 291 return; | |
| 292 } | |
| 293 view.events.addScopedListener( | |
| 294 WebRequestMessageEvent, | |
| 295 $Function.bind(function() { | |
| 296 // Re-dispatch to subEvent's listeners. | |
| 297 $Function.apply(this.dispatch, this, $Array.slice(arguments)); | |
| 298 }, this), {instanceId: opt_webViewInstanceId || 0}); | |
| 299 } | |
| 300 | |
| 301 DeclarativeWebRequestEvent.prototype.__proto__ = EventBindings.Event.prototype; | |
| 302 | |
| 303 // Exports. | 317 // Exports. |
| 304 exports.$set('WebViewEvents', WebViewEvents); | 318 exports.$set('WebViewEvents', WebViewEvents); |
| OLD | NEW |