Browser Plugin: Remove dependency on NPAPI

The CL moves the final attribute on BrowserPlugin 'allowtransparency' out to the extensions module! No new content APIs had to be introduced! Transparency can be enabled from RenderWidgetHostView::SetBackgroundOpaque in the content embedder.

This means that BrowserPlugin no longer depends on NPAPI and so this patch removes a lot of unnecessary code.

[email protected] for histograms.xml, extension_function_histogram_value.h

[email protected] for histograms.xml, extension
BUG=330264

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

Cr-Commit-Position: refs/heads/master@{#295879}
diff --git a/extensions/browser/api/web_view/web_view_internal_api.cc b/extensions/browser/api/web_view/web_view_internal_api.cc
index 0bffc5a7..cbacf7e 100644
--- a/extensions/browser/api/web_view/web_view_internal_api.cc
+++ b/extensions/browser/api/web_view/web_view_internal_api.cc
@@ -180,12 +180,6 @@
 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
 }
 
-WebViewInternalSetZoomFunction::WebViewInternalSetZoomFunction() {
-}
-
-WebViewInternalSetZoomFunction::~WebViewInternalSetZoomFunction() {
-}
-
 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
   scoped_ptr<webview::SetName::Params> params(
       webview::SetName::Params::Create(*args_));
@@ -195,6 +189,30 @@
   return true;
 }
 
+WebViewInternalSetAllowTransparencyFunction::
+WebViewInternalSetAllowTransparencyFunction() {
+}
+
+WebViewInternalSetAllowTransparencyFunction::
+~WebViewInternalSetAllowTransparencyFunction() {
+}
+
+bool WebViewInternalSetAllowTransparencyFunction::RunAsyncSafe(
+    WebViewGuest* guest) {
+  scoped_ptr<webview::SetAllowTransparency::Params> params(
+      webview::SetAllowTransparency::Params::Create(*args_));
+  EXTENSION_FUNCTION_VALIDATE(params.get());
+  guest->SetAllowTransparency(params->allow);
+  SendResponse(true);
+  return true;
+}
+
+WebViewInternalSetZoomFunction::WebViewInternalSetZoomFunction() {
+}
+
+WebViewInternalSetZoomFunction::~WebViewInternalSetZoomFunction() {
+}
+
 bool WebViewInternalSetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
   scoped_ptr<webview::SetZoom::Params> params(
       webview::SetZoom::Params::Create(*args_));
diff --git a/extensions/browser/api/web_view/web_view_internal_api.h b/extensions/browser/api/web_view/web_view_internal_api.h
index 6eae99fd..ecb54c1 100644
--- a/extensions/browser/api/web_view/web_view_internal_api.h
+++ b/extensions/browser/api/web_view/web_view_internal_api.h
@@ -150,6 +150,23 @@
   DISALLOW_COPY_AND_ASSIGN(WebViewInternalSetNameFunction);
 };
 
+class WebViewInternalSetAllowTransparencyFunction :
+    public WebViewInternalExtensionFunction {
+ public:
+  DECLARE_EXTENSION_FUNCTION("webViewInternal.setAllowTransparency",
+                             WEBVIEWINTERNAL_SETALLOWTRANSPARENCY);
+
+  WebViewInternalSetAllowTransparencyFunction();
+
+ protected:
+  virtual ~WebViewInternalSetAllowTransparencyFunction();
+
+ private:
+  virtual bool RunAsyncSafe(WebViewGuest* guest) OVERRIDE;
+
+  DISALLOW_COPY_AND_ASSIGN(WebViewInternalSetAllowTransparencyFunction);
+};
+
 class WebViewInternalSetZoomFunction : public WebViewInternalExtensionFunction {
  public:
   DECLARE_EXTENSION_FUNCTION("webViewInternal.setZoom",
diff --git a/extensions/browser/extension_function_histogram_value.h b/extensions/browser/extension_function_histogram_value.h
index 3eaa577..67245e0 100644
--- a/extensions/browser/extension_function_histogram_value.h
+++ b/extensions/browser/extension_function_histogram_value.h
@@ -953,6 +953,7 @@
   EASYUNLOCKPRIVATE_TRYSIGNINSECRET,
   ACCESSIBILITY_PRIVATE_SETFOCUSRING,
   USB_GETCONFIGURATION,
+  WEBVIEWINTERNAL_SETALLOWTRANSPARENCY,
   // Last entry: Add new entries above and ensure to update
   // tools/metrics/histograms/histograms.xml.
   ENUM_BOUNDARY
diff --git a/extensions/browser/guest_view/web_view/web_view_constants.cc b/extensions/browser/guest_view/web_view/web_view_constants.cc
index 4e2ee905..9d61ec65 100644
--- a/extensions/browser/guest_view/web_view/web_view_constants.cc
+++ b/extensions/browser/guest_view/web_view/web_view_constants.cc
@@ -7,11 +7,14 @@
 namespace webview {
 
 // Attributes.
+const char kAttributeAllowTransparency[] = "allowtransparency";
 const char kAttributeAutoSize[] = "autosize";
 const char kAttributeMaxHeight[] = "maxheight";
 const char kAttributeMaxWidth[] = "maxwidth";
 const char kAttributeMinHeight[] = "minheight";
 const char kAttributeMinWidth[] = "minwidth";
+const char kAttributeName[] = "name";
+const char kAttributeSrc[] = "src";
 
 // API namespace.
 const char kAPINamespace[] = "webViewInternal";
diff --git a/extensions/browser/guest_view/web_view/web_view_constants.h b/extensions/browser/guest_view/web_view/web_view_constants.h
index d0a24e2..b186edb 100644
--- a/extensions/browser/guest_view/web_view/web_view_constants.h
+++ b/extensions/browser/guest_view/web_view/web_view_constants.h
@@ -10,11 +10,14 @@
 namespace webview {
 
 // Attributes.
+extern const char kAttributeAllowTransparency[];
 extern const char kAttributeAutoSize[];
 extern const char kAttributeMaxHeight[];
 extern const char kAttributeMaxWidth[];
 extern const char kAttributeMinHeight[];
 extern const char kAttributeMinWidth[];
+extern const char kAttributeName[];
+extern const char kAttributeSrc[];
 
 // API namespace.
 // TODO(kalman): Consolidate this with the other API constants.
diff --git a/extensions/browser/guest_view/web_view/web_view_guest.cc b/extensions/browser/guest_view/web_view/web_view_guest.cc
index d7afe16e..deb840e 100644
--- a/extensions/browser/guest_view/web_view/web_view_guest.cc
+++ b/extensions/browser/guest_view/web_view/web_view_guest.cc
@@ -18,6 +18,7 @@
 #include "content/public/browser/notification_types.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/render_view_host.h"
+#include "content/public/browser/render_widget_host_view.h"
 #include "content/public/browser/resource_request_details.h"
 #include "content/public/browser/site_instance.h"
 #include "content/public/browser/storage_partition.h"
@@ -244,7 +245,7 @@
   SetUpAutoSize();
 
   std::string name;
-  if (attach_params()->GetString(webview::kName, &name)) {
+  if (attach_params()->GetString(webview::kAttributeName, &name)) {
     // If the guest window's name is empty, then the WebView tag's name is
     // assigned. Otherwise, the guest window's name takes precedence over the
     // WebView tag's name.
@@ -262,7 +263,7 @@
   }
 
   std::string src;
-  if (attach_params()->GetString("src", &src) && !src.empty())
+  if (attach_params()->GetString(webview::kAttributeSrc, &src) && !src.empty())
     NavigateGuest(src);
 
   if (GetOpener()) {
@@ -284,6 +285,13 @@
     // lifetime of the new guest is no longer managed by the opener guest.
     GetOpener()->pending_new_windows_.erase(this);
   }
+
+  bool allow_transparency = false;
+  attach_params()->GetBoolean(webview::kAttributeAllowTransparency,
+                              &allow_transparency);
+  // We need to set the background opaque flag after navigation to ensure that
+  // there is a RenderWidgetHostView available.
+  SetAllowTransparency(allow_transparency);
 }
 
 void WebViewGuest::DidInitialize() {
@@ -331,6 +339,13 @@
   // The guest RenderView should always live in an isolated guest process.
   CHECK(web_contents()->GetRenderProcessHost()->IsIsolatedGuest());
   Send(new ExtensionMsg_SetFrameName(web_contents()->GetRoutingID(), name_));
+
+  // We don't want to accidentally set the opacity of an interstitial page.
+  // WebContents::GetRenderWidgetHostView will return the RWHV of an
+  // interstitial page if one is showing at this time. We only want opacity
+  // to apply to web pages.
+  web_contents()->GetRenderViewHost()->GetView()->
+      SetBackgroundOpaque(guest_opaque_);
 }
 
 void WebViewGuest::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
@@ -618,6 +633,7 @@
     : GuestView<WebViewGuest>(browser_context, guest_instance_id),
       find_helper_(this),
       is_overriding_user_agent_(false),
+      guest_opaque_(true),
       javascript_dialog_helper_(this) {
   web_view_guest_delegate_.reset(
       ExtensionsAPIClient::Get()->CreateWebViewGuestDelegate(this));
@@ -955,6 +971,17 @@
     web_view_guest_delegate_->OnSetZoom(zoom_factor);
 }
 
+void WebViewGuest::SetAllowTransparency(bool allow) {
+  if (guest_opaque_ != allow)
+    return;
+
+  guest_opaque_ = !allow;
+  if (!web_contents()->GetRenderViewHost()->GetView())
+    return;
+
+  web_contents()->GetRenderViewHost()->GetView()->SetBackgroundOpaque(!allow);
+}
+
 void WebViewGuest::AddNewContents(content::WebContents* source,
                                   content::WebContents* new_contents,
                                   WindowOpenDisposition disposition,
diff --git a/extensions/browser/guest_view/web_view/web_view_guest.h b/extensions/browser/guest_view/web_view/web_view_guest.h
index e62629b..1d9ba9a 100644
--- a/extensions/browser/guest_view/web_view/web_view_guest.h
+++ b/extensions/browser/guest_view/web_view/web_view_guest.h
@@ -74,6 +74,9 @@
   // Set the zoom factor.
   void SetZoom(double zoom_factor);
 
+  // Sets the transparency of the guest.
+  void SetAllowTransparency(bool allow);
+
   // GuestViewBase implementation.
   virtual const char* GetAPINamespace() const OVERRIDE;
   virtual int GetTaskPrefix() const OVERRIDE;
@@ -330,6 +333,9 @@
   // Stores the window name of the main frame of the guest.
   std::string name_;
 
+  // Stores whether the contents of the guest can be transparent.
+  bool guest_opaque_;
+
   // Handles the JavaScript dialog requests.
   JavaScriptDialogHelper javascript_dialog_helper_;