Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: chrome/renderer/resources/extensions/web_view.js

Issue 23503023: Reland after fix: Improve <webview> autosize: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for re-land. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
27 /** @type {Array.<string>} */ 32 /** @type {Array.<string>} */
28 var WEB_VIEW_ATTRIBUTES = ['name', 'partition', 'autosize', 'minheight', 33 var WEB_VIEW_ATTRIBUTES = [
29 'minwidth', 'maxheight', 'maxwidth']; 34 'name',
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 ];
30 42
31 var webViewInstanceIdCounter = 0; 43 var webViewInstanceIdCounter = 0;
32 44
33 var CreateEvent = function(name) { 45 var CreateEvent = function(name) {
34 var eventOpts = {supportsListeners: true, supportsFilters: true}; 46 var eventOpts = {supportsListeners: true, supportsFilters: true};
35 return new EventBindings.Event(name, undefined, eventOpts); 47 return new EventBindings.Event(name, undefined, eventOpts);
36 }; 48 };
37 49
38 var WEB_VIEW_EVENTS = { 50 var WEB_VIEW_EVENTS = {
39 'close': { 51 'close': {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 'url', 114 'url',
103 'userGesture' 115 'userGesture'
104 ] 116 ]
105 }, 117 },
106 'responsive': { 118 'responsive': {
107 evt: CreateEvent('webview.onResponsive'), 119 evt: CreateEvent('webview.onResponsive'),
108 fields: ['processId'] 120 fields: ['processId']
109 }, 121 },
110 'sizechanged': { 122 'sizechanged': {
111 evt: CreateEvent('webview.onSizeChanged'), 123 evt: CreateEvent('webview.onSizeChanged'),
124 customHandler: function(webViewInternal, event, webViewEvent) {
125 webViewInternal.handleSizeChangedEvent_(event, webViewEvent);
126 },
112 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'] 127 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth']
113 }, 128 },
114 'unresponsive': { 129 'unresponsive': {
115 evt: CreateEvent('webview.onUnresponsive'), 130 evt: CreateEvent('webview.onUnresponsive'),
116 fields: ['processId'] 131 fields: ['processId']
117 } 132 }
118 }; 133 };
119 134
120 // Implemented when the experimental API is available. 135 // Implemented when the experimental API is available.
121 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {} 136 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {}
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 * @private 479 * @private
465 */ 480 */
466 WebViewInternal.prototype.getEvents_ = function() { 481 WebViewInternal.prototype.getEvents_ = function() {
467 var experimentalEvents = this.maybeGetExperimentalEvents_(); 482 var experimentalEvents = this.maybeGetExperimentalEvents_();
468 for (var eventName in experimentalEvents) { 483 for (var eventName in experimentalEvents) {
469 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName]; 484 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName];
470 } 485 }
471 return WEB_VIEW_EVENTS; 486 return WEB_VIEW_EVENTS;
472 }; 487 };
473 488
489 WebViewInternal.prototype.handleSizeChangedEvent_ =
490 function(event, webViewEvent) {
491 var node = this.webviewNode_;
492
493 var width = node.offsetWidth;
494 var height = node.offsetHeight;
495
496 // Check the current bounds to make sure we do not resize <webview>
497 // outside of current constraints.
498 var maxWidth;
499 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXWIDTH) &&
500 node[WEB_VIEW_ATTRIBUTE_MAXWIDTH]) {
501 maxWidth = node[WEB_VIEW_ATTRIBUTE_MAXWIDTH];
502 } else {
503 maxWidth = width;
504 }
505
506 var minWidth;
507 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINWIDTH) &&
508 node[WEB_VIEW_ATTRIBUTE_MINWIDTH]) {
509 minWidth = node[WEB_VIEW_ATTRIBUTE_MINWIDTH];
510 } else {
511 minWidth = width;
512 }
513 if (minWidth > maxWidth) {
514 minWidth = maxWidth;
515 }
516
517 var maxHeight;
518 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXHEIGHT) &&
519 node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT]) {
520 maxHeight = node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT];
521 } else {
522 maxHeight = height;
523 }
524 var minHeight;
525 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINHEIGHT) &&
526 node[WEB_VIEW_ATTRIBUTE_MINHEIGHT]) {
527 minHeight = node[WEB_VIEW_ATTRIBUTE_MINHEIGHT];
528 } else {
529 minHeight = height;
530 }
531 if (minHeight > maxHeight) {
532 minHeight = maxHeight;
533 }
534
535 if (webViewEvent.newWidth >= minWidth &&
536 webViewEvent.newWidth <= maxWidth &&
537 webViewEvent.newHeight >= minHeight &&
538 webViewEvent.newHeight <= maxHeight) {
539 node.style.width = webViewEvent.newWidth + 'px';
540 node.style.height = webViewEvent.newHeight + 'px';
541 }
542 node.dispatchEvent(webViewEvent);
543 };
544
474 /** 545 /**
475 * @private 546 * @private
476 */ 547 */
477 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() { 548 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() {
478 var self = this; 549 var self = this;
479 this.viewInstanceId_ = ++webViewInstanceIdCounter; 550 this.viewInstanceId_ = ++webViewInstanceIdCounter;
480 var onInstanceIdAllocated = function(e) { 551 var onInstanceIdAllocated = function(e) {
481 var detail = e.detail ? JSON.parse(e.detail) : {}; 552 var detail = e.detail ? JSON.parse(e.detail) : {};
482 self.instanceId_ = detail.windowId; 553 self.instanceId_ = detail.windowId;
483 var params = { 554 var params = {
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 928
858 /** 929 /**
859 * Implemented when the experimental API is available. 930 * Implemented when the experimental API is available.
860 * @private 931 * @private
861 */ 932 */
862 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {}; 933 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {};
863 934
864 exports.WebView = WebView; 935 exports.WebView = WebView;
865 exports.WebViewInternal = WebViewInternal; 936 exports.WebViewInternal = WebViewInternal;
866 exports.CreateEvent = CreateEvent; 937 exports.CreateEvent = CreateEvent;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698