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

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

Issue 23691039: Reland attempt 2: Improve <webview> autosize: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Relax checking width/height since bunch of removeAttribute() and style.width assignments are not at… 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 'url', 118 'url',
107 'userGesture' 119 'userGesture'
108 ] 120 ]
109 }, 121 },
110 'responsive': { 122 'responsive': {
111 evt: CreateEvent('webview.onResponsive'), 123 evt: CreateEvent('webview.onResponsive'),
112 fields: ['processId'] 124 fields: ['processId']
113 }, 125 },
114 'sizechanged': { 126 'sizechanged': {
115 evt: CreateEvent('webview.onSizeChanged'), 127 evt: CreateEvent('webview.onSizeChanged'),
128 customHandler: function(webViewInternal, event, webViewEvent) {
129 webViewInternal.handleSizeChangedEvent_(event, webViewEvent);
130 },
116 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'] 131 fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth']
117 }, 132 },
118 'unresponsive': { 133 'unresponsive': {
119 evt: CreateEvent('webview.onUnresponsive'), 134 evt: CreateEvent('webview.onUnresponsive'),
120 fields: ['processId'] 135 fields: ['processId']
121 } 136 }
122 }; 137 };
123 138
124 // Implemented when the experimental API is available. 139 // Implemented when the experimental API is available.
125 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {} 140 WebViewInternal.maybeRegisterExperimentalAPIs = function(proto) {}
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 * @private 483 * @private
469 */ 484 */
470 WebViewInternal.prototype.getEvents_ = function() { 485 WebViewInternal.prototype.getEvents_ = function() {
471 var experimentalEvents = this.maybeGetExperimentalEvents_(); 486 var experimentalEvents = this.maybeGetExperimentalEvents_();
472 for (var eventName in experimentalEvents) { 487 for (var eventName in experimentalEvents) {
473 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName]; 488 WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName];
474 } 489 }
475 return WEB_VIEW_EVENTS; 490 return WEB_VIEW_EVENTS;
476 }; 491 };
477 492
493 WebViewInternal.prototype.handleSizeChangedEvent_ =
494 function(event, webViewEvent) {
495 var node = this.webviewNode_;
496
497 var width = node.offsetWidth;
498 var height = node.offsetHeight;
499
500 // Check the current bounds to make sure we do not resize <webview>
501 // outside of current constraints.
502 var maxWidth;
503 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXWIDTH) &&
504 node[WEB_VIEW_ATTRIBUTE_MAXWIDTH]) {
505 maxWidth = node[WEB_VIEW_ATTRIBUTE_MAXWIDTH];
506 } else {
507 maxWidth = width;
508 }
509
510 var minWidth;
511 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINWIDTH) &&
512 node[WEB_VIEW_ATTRIBUTE_MINWIDTH]) {
513 minWidth = node[WEB_VIEW_ATTRIBUTE_MINWIDTH];
514 } else {
515 minWidth = width;
516 }
517 if (minWidth > maxWidth) {
518 minWidth = maxWidth;
519 }
520
521 var maxHeight;
522 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MAXHEIGHT) &&
523 node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT]) {
524 maxHeight = node[WEB_VIEW_ATTRIBUTE_MAXHEIGHT];
525 } else {
526 maxHeight = height;
527 }
528 var minHeight;
529 if (node.hasAttribute(WEB_VIEW_ATTRIBUTE_MINHEIGHT) &&
530 node[WEB_VIEW_ATTRIBUTE_MINHEIGHT]) {
531 minHeight = node[WEB_VIEW_ATTRIBUTE_MINHEIGHT];
532 } else {
533 minHeight = height;
534 }
535 if (minHeight > maxHeight) {
536 minHeight = maxHeight;
537 }
538
539 if (webViewEvent.newWidth >= minWidth &&
540 webViewEvent.newWidth <= maxWidth &&
541 webViewEvent.newHeight >= minHeight &&
542 webViewEvent.newHeight <= maxHeight) {
543 node.style.width = webViewEvent.newWidth + 'px';
544 node.style.height = webViewEvent.newHeight + 'px';
545 }
546 node.dispatchEvent(webViewEvent);
547 };
548
478 /** 549 /**
479 * @private 550 * @private
480 */ 551 */
481 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() { 552 WebViewInternal.prototype.setupWebviewNodeEvents_ = function() {
482 var self = this; 553 var self = this;
483 this.viewInstanceId_ = ++webViewInstanceIdCounter; 554 this.viewInstanceId_ = ++webViewInstanceIdCounter;
484 var onInstanceIdAllocated = function(e) { 555 var onInstanceIdAllocated = function(e) {
485 var detail = e.detail ? JSON.parse(e.detail) : {}; 556 var detail = e.detail ? JSON.parse(e.detail) : {};
486 self.instanceId_ = detail.windowId; 557 self.instanceId_ = detail.windowId;
487 var params = { 558 var params = {
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 932
862 /** 933 /**
863 * Implemented when the experimental API is available. 934 * Implemented when the experimental API is available.
864 * @private 935 * @private
865 */ 936 */
866 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {}; 937 WebViewInternal.prototype.maybeAttachWebRequestEventToWebview_ = function() {};
867 938
868 exports.WebView = WebView; 939 exports.WebView = WebView;
869 exports.WebViewInternal = WebViewInternal; 940 exports.WebViewInternal = WebViewInternal;
870 exports.CreateEvent = CreateEvent; 941 exports.CreateEvent = CreateEvent;
OLDNEW
« no previous file with comments | « chrome/browser/apps/web_view_browsertest.cc ('k') | chrome/test/data/extensions/platform_apps/web_view/autosize/main.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698