Various webview cleanup.

**********

- Optimized onSizeChanged().
- Got rid of throwing errors.
- Removed some unused or unnecessary helper functions.
- Removed some other unnecessary code.
- executeScript() and insertCSS() now share their code (since it was nearly identical in the first place).

BUG=431002

Review URL: https://codereview.chromium.org/715583007

Cr-Commit-Position: refs/heads/master@{#303713}
diff --git a/chrome/test/data/extensions/platform_apps/web_view/shim/main.js b/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
index 8a98077..332a85ea 100644
--- a/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
+++ b/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
@@ -750,15 +750,14 @@
   var webview = document.createElement('webview');
   document.body.appendChild(webview);
   setTimeout(function() {
-    try {
     webview.executeScript(
-      {code:'document.body.style.backgroundColor = "red";'},
-      function(results) {
-        embedder.test.fail();
-      });
-    } catch (e) {
+        {code:'document.body.style.backgroundColor = "red";'},
+        function(results) {
+          embedder.test.fail();
+        });
+    setTimeout(function() {
       embedder.test.succeed();
-    }
+    }, 0);
   }, 0);
 }
 
diff --git a/extensions/renderer/resources/guest_view/web_view.js b/extensions/renderer/resources/guest_view/web_view.js
index 32eda06..7d11ca7 100644
--- a/extensions/renderer/resources/guest_view/web_view.js
+++ b/extensions/renderer/resources/guest_view/web_view.js
@@ -50,10 +50,6 @@
   return browserPluginNode;
 };
 
-WebViewImpl.prototype.getGuestInstanceId = function() {
-  return this.guestInstanceId;
-};
-
 // Resets some state upon reattaching <webview> element to the DOM.
 WebViewImpl.prototype.reset = function() {
   // If guestInstanceId is defined then the <webview> has navigated and has
@@ -103,13 +99,6 @@
   }.bind(this));
 };
 
-// Validation helper function for executeScript() and insertCSS().
-WebViewImpl.prototype.validateExecuteCodeCall  = function() {
-  if (!this.guestInstanceId) {
-    throw new Error(WebViewConstants.ERROR_MSG_CANNOT_INJECT_SCRIPT);
-  }
-};
-
 WebViewImpl.prototype.setupWebviewNodeProperties = function() {
   // We cannot use {writable: true} property descriptor because we want a
   // dynamic getter value.
@@ -175,49 +164,23 @@
 
   // Check the current bounds to make sure we do not resize <webview>
   // outside of current constraints.
-  var maxWidth;
-  if (node.hasAttribute(WebViewConstants.ATTRIBUTE_MAXWIDTH) &&
-      node[WebViewConstants.ATTRIBUTE_MAXWIDTH]) {
-    maxWidth = node[WebViewConstants.ATTRIBUTE_MAXWIDTH];
-  } else {
-    maxWidth = width;
-  }
+  var maxWidth = this.attributes[
+    WebViewConstants.ATTRIBUTE_MAXWIDTH].getValue() || width;
+  var minWidth = this.attributes[
+    WebViewConstants.ATTRIBUTE_MINWIDTH].getValue() || width;
+  var maxHeight = this.attributes[
+    WebViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || height;
+  var minHeight = this.attributes[
+    WebViewConstants.ATTRIBUTE_MINHEIGHT].getValue() || height;
 
-  var minWidth;
-  if (node.hasAttribute(WebViewConstants.ATTRIBUTE_MINWIDTH) &&
-      node[WebViewConstants.ATTRIBUTE_MINWIDTH]) {
-    minWidth = node[WebViewConstants.ATTRIBUTE_MINWIDTH];
-  } else {
-    minWidth = width;
-  }
-  if (minWidth > maxWidth) {
-    minWidth = maxWidth;
-  }
-
-  var maxHeight;
-  if (node.hasAttribute(WebViewConstants.ATTRIBUTE_MAXHEIGHT) &&
-      node[WebViewConstants.ATTRIBUTE_MAXHEIGHT]) {
-    maxHeight = node[WebViewConstants.ATTRIBUTE_MAXHEIGHT];
-  } else {
-    maxHeight = height;
-  }
-
-  var minHeight;
-  if (node.hasAttribute(WebViewConstants.ATTRIBUTE_MINHEIGHT) &&
-      node[WebViewConstants.ATTRIBUTE_MINHEIGHT]) {
-    minHeight = node[WebViewConstants.ATTRIBUTE_MINHEIGHT];
-  } else {
-    minHeight = height;
-  }
-  if (minHeight > maxHeight) {
-    minHeight = maxHeight;
-  }
+  minWidth = Math.min(minWidth, maxWidth);
+  minHeight = Math.min(minHeight, maxHeight);
 
   if (!this.attributes[WebViewConstants.ATTRIBUTE_AUTOSIZE].getValue() ||
       (newWidth >= minWidth &&
-       newWidth <= maxWidth &&
-       newHeight >= minHeight &&
-       newHeight <= maxHeight)) {
+      newWidth <= maxWidth &&
+      newHeight >= minHeight &&
+      newHeight <= maxHeight)) {
     node.style.width = newWidth + 'px';
     node.style.height = newHeight + 'px';
     // Only fire the DOM event if the size of the <webview> has actually
@@ -226,15 +189,6 @@
   }
 };
 
-// Returns if <object> is in the render tree.
-WebViewImpl.prototype.isPluginInRenderTree = function() {
-  return !!this.internalInstanceId && this.internalInstanceId != 0;
-};
-
-WebViewImpl.prototype.hasNavigated = function() {
-  return !this.beforeFirstNavigation;
-};
-
 WebViewImpl.prototype.parseSrcAttribute = function() {
   if (!this.attributes[WebViewConstants.ATTRIBUTE_PARTITION].validPartitionId ||
       !this.attributes[WebViewConstants.ATTRIBUTE_SRC].getValue()) {
@@ -255,13 +209,6 @@
       this.attributes[WebViewConstants.ATTRIBUTE_SRC].getValue());
 };
 
-WebViewImpl.prototype.parseAttributes = function() {
-  if (!this.elementAttached) {
-    return;
-  }
-  this.parseSrcAttribute();
-};
-
 WebViewImpl.prototype.createGuest = function() {
   if (this.pendingGuestCreation) {
     return;
@@ -356,7 +303,7 @@
   this.guestInstanceId = guestInstanceId;
   var params = this.buildAttachParams();
 
-  if (!this.isPluginInRenderTree()) {
+  if (!this.internalInstanceId) {
     return true;
   }
 
@@ -398,16 +345,26 @@
   $Function.apply(WebViewInternal.clearData, null, args);
 };
 
-// Injects JavaScript code into the guest page.
-WebViewImpl.prototype.executeScript = function(var_args) {
-  this.validateExecuteCodeCall();
+// Shared implementation of executeScript() and insertCSS().
+WebViewImpl.prototype.executeCode = function(func, args) {
+  if (!this.guestInstanceId) {
+    window.console.error(WebViewConstants.ERROR_MSG_CANNOT_INJECT_SCRIPT);
+    return;
+  }
+
   var webviewSrc = this.attributes[WebViewConstants.ATTRIBUTE_SRC].getValue();
   if (this.baseUrlForDataUrl != '') {
     webviewSrc = this.baseUrlForDataUrl;
   }
-  var args = $Array.concat([this.guestInstanceId, webviewSrc],
-                           $Array.slice(arguments));
-  $Function.apply(WebViewInternal.executeScript, null, args);
+
+  args = $Array.concat([this.guestInstanceId, webviewSrc],
+                       $Array.slice(args));
+  $Function.apply(func, null, args);
+}
+
+// Injects JavaScript code into the guest page.
+WebViewImpl.prototype.executeScript = function(var_args) {
+  this.executeCode(WebViewInternal.executeScript, $Array.slice(arguments));
 };
 
 // Initiates a find-in-page request.
@@ -453,14 +410,7 @@
 
 // Injects CSS into the guest page.
 WebViewImpl.prototype.insertCSS = function(var_args) {
-  this.validateExecuteCodeCall();
-  var webviewSrc = this.attributes[WebViewConstants.ATTRIBUTE_SRC].getValue();
-  if (this.baseUrlForDataUrl != '') {
-    webviewSrc = this.baseUrlForDataUrl;
-  }
-  var args = $Array.concat([this.guestInstanceId, webviewSrc],
-                           $Array.slice(arguments));
-  $Function.apply(WebViewInternal.insertCSS, null, args);
+  this.executeCode(WebViewInternal.insertCSS, $Array.slice(arguments));
 };
 
 // Indicates whether or not the webview's user agent string has been overridden.
@@ -594,7 +544,7 @@
     }
     if (!internal.elementAttached) {
       internal.elementAttached = true;
-      internal.parseAttributes();
+      internal.parseSrcAttribute();
     }
   };
 
@@ -664,7 +614,6 @@
 
 // Implemented when the experimental WebView API is available.
 WebViewImpl.maybeGetExperimentalAPIs = function() {};
-WebViewImpl.prototype.maybeGetExperimentalEvents = function() {};
 WebViewImpl.prototype.setupExperimentalContextMenus = function() {};
 
 // Exports.
diff --git a/extensions/renderer/resources/guest_view/web_view_events.js b/extensions/renderer/resources/guest_view/web_view_events.js
index 73249c4..1f05be6 100644
--- a/extensions/renderer/resources/guest_view/web_view_events.js
+++ b/extensions/renderer/resources/guest_view/web_view_events.js
@@ -269,10 +269,6 @@
 };
 
 WebViewEvents.prototype.getEvents = function() {
-  var experimentalEvents = this.webViewImpl.maybeGetExperimentalEvents();
-  for (var eventName in experimentalEvents) {
-    WEB_VIEW_EVENTS[eventName] = experimentalEvents[eventName];
-  }
   var chromeEvents = this.webViewImpl.maybeGetChromeWebViewEvents();
   for (var eventName in chromeEvents) {
     WEB_VIEW_EVENTS[eventName] = chromeEvents[eventName];
@@ -327,7 +323,7 @@
   };
 
   var getGuestInstanceId = function() {
-    return this.webViewImpl.getGuestInstanceId();
+    return this.webViewImpl.guestInstanceId;
   }.bind(this);
 
   var dialog = {
@@ -414,7 +410,7 @@
   var requestId = event.requestId;
   var actionTaken = false;
   var getGuestInstanceId = function() {
-    return this.webViewImpl.getGuestInstanceId();
+    return this.webViewImpl.guestInstanceId;
   }.bind(this);
 
   var validateCall = function() {
@@ -530,7 +526,7 @@
 
   var requestId = event.requestId;
   var getGuestInstanceId = function() {
-    return this.webViewImpl.getGuestInstanceId();
+    return this.webViewImpl.guestInstanceId;
   }.bind(this);
 
   if (this.permissionTypes.indexOf(event.permission) < 0) {
diff --git a/extensions/renderer/resources/guest_view/web_view_experimental.js b/extensions/renderer/resources/guest_view/web_view_experimental.js
index a64e87a5..be118b3 100644
--- a/extensions/renderer/resources/guest_view/web_view_experimental.js
+++ b/extensions/renderer/resources/guest_view/web_view_experimental.js
@@ -11,14 +11,6 @@
 var WebViewImpl = require('webView').WebViewImpl;
 var WebViewInternal = require('webViewInternal').WebViewInternal;
 
-// Returns a map of experimental <webview> DOM event names to their associated
-// extension event descriptor objects. See |WEB_VIEW_EVENTS| in
-// web_view_events.js for more information and details on how this map should
-// be formatted.
-WebViewImpl.prototype.maybeGetExperimentalEvents = function() {
-  return {};
-};
-
 // Loads a data URL with a specified base URL used for relative links.
 // Optionally, a virtual URL can be provided to be shown to the user instead
 // of the data URL.
diff --git a/extensions/test/data/web_view/apitest/main.js b/extensions/test/data/web_view/apitest/main.js
index 2c8d15f..78c56b7 100644
--- a/extensions/test/data/web_view/apitest/main.js
+++ b/extensions/test/data/web_view/apitest/main.js
@@ -671,14 +671,17 @@
 
 function testExecuteScriptFail() {
   var webview = document.createElement('webview');
-  try {
+  document.body.appendChild(webview);
+  setTimeout(function() {
     webview.executeScript(
-        {code: 'document.body.style.backgroundColor = "red";'},
-        function(results) { embedder.test.fail(); });
-  }
-  catch (e) {
-    embedder.test.succeed();
-  }
+        {code:'document.body.style.backgroundColor = "red";'},
+        function(results) {
+          embedder.test.fail();
+        });
+    setTimeout(function() {
+      embedder.test.succeed();
+    }, 0);
+  }, 0);
 }
 
 // This test verifies that the call of executeScript will fail and return null