| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Shim that simulates a <webview> tag via Mutation Observers. | 5 // Shim that simulates a <webview> tag via Mutation Observers. |
| 6 // | 6 // |
| 7 // The actual tag is implemented via the browser plugin. The internals of this | 7 // The actual tag is implemented via the browser plugin. The internals of this |
| 8 // are hidden via Shadow DOM. | 8 // are hidden via Shadow DOM. |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 var DocumentNatives = requireNative('document_natives'); | 12 var DocumentNatives = requireNative('document_natives'); |
| 13 var EventBindings = require('event_bindings'); | 13 var EventBindings = require('event_bindings'); |
| 14 var MessagingNatives = requireNative('messaging_natives'); | 14 var MessagingNatives = requireNative('messaging_natives'); |
| 15 var WebRequestEvent = require('webRequestInternal').WebRequestEvent; | 15 var WebRequestEvent = require('webRequestInternal').WebRequestEvent; |
| 16 var WebRequestSchema = | 16 var WebRequestSchema = |
| 17 requireNative('schema_registry').GetSchema('webRequest'); | 17 requireNative('schema_registry').GetSchema('webRequest'); |
| 18 var WebView = require('binding').Binding.create('webview').generate(); | 18 var WebView = require('binding').Binding.create('webview').generate(); |
| 19 | 19 |
| 20 // This secret enables hiding <webview> private members from the outside scope. | 20 // This secret enables hiding <webview> private members from the outside scope. |
| 21 // Outside of this file, |secret| is inaccessible. The only way to access the | 21 // Outside of this file, |secret| is inaccessible. The only way to access the |
| 22 // <webview> element's internal members is via the |secret|. Since it's only | 22 // <webview> element's internal members is via the |secret|. Since it's only |
| 23 // accessible by code here (and in web_view_experimental), only <webview>'s | 23 // accessible by code here (and in web_view_experimental), only <webview>'s |
| 24 // API can access it and not external developers. | 24 // API can access it and not external developers. |
| 25 var secret = {}; | 25 var secret = {}; |
| 26 | 26 |
| 27 var WEB_VIEW_ATTRIBUTE_MAXHEIGHT = 'maxheight'; | |
| 28 var WEB_VIEW_ATTRIBUTE_MAXWIDTH = 'maxwidth'; | |
| 29 var WEB_VIEW_ATTRIBUTE_MINHEIGHT = 'minheight'; | |
| 30 var WEB_VIEW_ATTRIBUTE_MINWIDTH = 'minwidth'; | |
| 31 | |
| 32 /** @type {Array.<string>} */ | 27 /** @type {Array.<string>} */ |
| 33 var WEB_VIEW_ATTRIBUTES = [ | 28 var WEB_VIEW_ATTRIBUTES = ['name', 'partition', 'autosize', 'minheight', |
| 34 'name', | 29 'minwidth', 'maxheight', 'maxwidth']; |
| 35 'partition', | |
| 36 'autosize', | |
| 37 WEB_VIEW_ATTRIBUTE_MINHEIGHT, | |
| 38 WEB_VIEW_ATTRIBUTE_MINWIDTH, | |
| 39 WEB_VIEW_ATTRIBUTE_MAXHEIGHT, | |
| 40 WEB_VIEW_ATTRIBUTE_MAXWIDTH | |
| 41 ]; | |
| 42 | 30 |
| 43 var webViewInstanceIdCounter = 0; | 31 var webViewInstanceIdCounter = 0; |
| 44 | 32 |
| 45 var CreateEvent = function(name) { | 33 var CreateEvent = function(name) { |
| 46 var eventOpts = {supportsListeners: true, supportsFilters: true}; | 34 var eventOpts = {supportsListeners: true, supportsFilters: true}; |
| 47 return new EventBindings.Event(name, undefined, eventOpts); | 35 return new EventBindings.Event(name, undefined, eventOpts); |
| 48 }; | 36 }; |
| 49 | 37 |
| 50 var WEB_VIEW_EVENTS = { | 38 var WEB_VIEW_EVENTS = { |
| 51 'close': { | 39 'close': { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 'url', | 102 'url', |
| 115 'userGesture' | 103 'userGesture' |
| 116 ] | 104 ] |
| 117 }, | 105 }, |
| 118 'responsive': { | 106 'responsive': { |
| 119 evt: CreateEvent('webview.onResponsive'), | 107 evt: CreateEvent('webview.onResponsive'), |
| 120 fields: ['processId'] | 108 fields: ['processId'] |
| 121 }, | 109 }, |
| 122 'sizechanged': { | 110 'sizechanged': { |
| 123 evt: CreateEvent('webview.onSizeChanged'), | 111 evt: CreateEvent('webview.onSizeChanged'), |
| 124 customHandler: function(webViewInternal, event, webViewEvent) { | |
| 125 webViewInternal.handleSizeChangedEvent_(event, webViewEvent); | |
| 126 }, | |
| 127 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'] | 112 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'] |
| 128 }, | 113 }, |
| 129 'unresponsive': { | 114 'unresponsive': { |
| 130 evt: CreateEvent('webview.onUnresponsive'), | 115 evt: CreateEvent('webview.onUnresponsive'), |
| 131 fields: ['processId'] | 116 fields: ['processId'] |
| 132 } | 117 } |
| 133 }; | 118 }; |
| 134 | 119 |
| 135 // Implemented when the experimental API is available. | 120 // Implemented when the experimental API is available. |
| 136 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {} | 121 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {} |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 * @private | 464 * @private |
| 480 */ | 465 */ |
| 481 WebViewInternal.prototype.getEvents_ = function() { | 466 WebViewInternal.prototype.getEvents_ = function() { |
| 482 var experimentalEvents = this.maybeGetExperimentalEvents_(); | 467 var experimentalEvents = this.maybeGetExperimentalEvents_(); |
| 483 for (var eventName in experimentalEvents) { | 468 for (var eventName in experimentalEvents) { |
| 484 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName]; | 469 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName]; |
| 485 } | 470 } |
| 486 return WEB_VIEW_EVENTS; | 471 return WEB_VIEW_EVENTS; |
| 487 }; | 472 }; |
| 488 | 473 |
| 489 WebViewInternal.prototype.handleSizeChangedEvent_ = | |
| 490 function(event, webViewEvent) { | |
| 491 var node = this.webviewNode_; | |
| 492 | |
| 493 // Check the current bounds to make sure we do not resize <webview> | |
| 494 // outside of current constraints. | |
| 495 var minWidth = 0; | |
| 496 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINWIDTH) && | |
| 497 node[WEB_VIEW_ATTRIBUTE_MINWIDTH]) { | |
| 498 minWidth = node[WEB_VIEW_ATTRIBUTE_MINWIDTH]; | |
| 499 } | |
| 500 var maxWidth; | |
| 501 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXWIDTH) && | |
| 502 node[WEB_VIEW_ATTRIBUTE_MAXWIDTH]) { | |
| 503 maxWidth = node[WEB_VIEW_ATTRIBUTE_MAXWIDTH]; | |
| 504 } else { | |
| 505 maxWidth = node.offsetWidth; | |
| 506 } | |
| 507 if (minWidth > maxWidth) { | |
| 508 minWidth = maxWidth; | |
| 509 } | |
| 510 | |
| 511 var minHeight = 0; | |
| 512 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINHEIGHT) && | |
| 513 node[WEB_VIEW_ATTRIBUTE_MINHEIGHT]) { | |
| 514 minHeight = node[WEB_VIEW_ATTRIBUTE_MINHEIGHT]; | |
| 515 } | |
| 516 var maxHeight; | |
| 517 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXHEIGHT) && | |
| 518 node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT]) { | |
| 519 maxHeight = node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT]; | |
| 520 } else { | |
| 521 maxHeight = node.offsetHeight; | |
| 522 } | |
| 523 if (minHeight > maxHeight) { | |
| 524 minHeight = maxHeight; | |
| 525 } | |
| 526 | |
| 527 if (webViewEvent.newWidth >= minWidth && | |
| 528 webViewEvent.newWidth <= maxWidth && | |
| 529 webViewEvent.newHeight >= minHeight && | |
| 530 webViewEvent.newHeight <= maxHeight) { | |
| 531 node.style.width = webViewEvent.newWidth + 'px'; | |
| 532 node.style.height = webViewEvent.newHeight + 'px'; | |
| 533 } | |
| 534 node.dispatchEvent(webViewEvent); | |
| 535 }; | |
| 536 | |
| 537 /** | 474 /** |
| 538 * @private | 475 * @private |
| 539 */ | 476 */ |
| 540 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() { | 477 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() { |
| 541 var self = this; | 478 var self = this; |
| 542 this.viewInstanceId_ = ++webViewInstanceIdCounter; | 479 this.viewInstanceId_ = ++webViewInstanceIdCounter; |
| 543 var onInstanceIdAllocated = function(e) { | 480 var onInstanceIdAllocated = function(e) { |
| 544 var detail = e.detail ? JSON.parse(e.detail) : {}; | 481 var detail = e.detail ? JSON.parse(e.detail) : {}; |
| 545 self.instanceId_ = detail.windowId; | 482 self.instanceId_ = detail.windowId; |
| 546 var params = { | 483 var params = { |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 | 857 |
| 921 /** | 858 /** |
| 922 * Implemented when the experimental API is available. | 859 * Implemented when the experimental API is available. |
| 923 * @private | 860 * @private |
| 924 */ | 861 */ |
| 925 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {}; | 862 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {}; |
| 926 | 863 |
| 927 exports.WebView = WebView; | 864 exports.WebView = WebView; |
| 928 exports.WebViewInternal = WebViewInternal; | 865 exports.WebViewInternal = WebViewInternal; |
| 929 exports.CreateEvent = CreateEvent; | 866 exports.CreateEvent = CreateEvent; |
| OLD | NEW |