Home | History | Annotate | Download | only in web_view
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/guest_view/web_view/plugin_permission_helper.h"
      6 
      7 #include "chrome/browser/guest_view/web_view/web_view_guest.h"
      8 #include "chrome/browser/guest_view/web_view/web_view_permission_types.h"
      9 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
     10 #include "chrome/common/render_messages.h"
     11 #include "content/public/browser/render_process_host.h"
     12 #include "content/public/browser/render_view_host.h"
     13 #include "content/public/browser/user_metrics.h"
     14 
     15 using content::BrowserPluginGuestDelegate;
     16 using content::RenderViewHost;
     17 using content::WebContents;
     18 
     19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PluginPermissionHelper);
     20 
     21 PluginPermissionHelper::PluginPermissionHelper(WebContents* contents)
     22     : content::WebContentsObserver(contents),
     23       weak_factory_(this) {
     24 }
     25 
     26 PluginPermissionHelper::~PluginPermissionHelper() {
     27 }
     28 
     29 bool PluginPermissionHelper::OnMessageReceived(
     30     const IPC::Message& message,
     31     content::RenderFrameHost* render_frame_host) {
     32   IPC_BEGIN_MESSAGE_MAP(PluginPermissionHelper, message)
     33     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_BlockedOutdatedPlugin,
     34                         OnBlockedOutdatedPlugin)
     35     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_BlockedUnauthorizedPlugin,
     36                         OnBlockedUnauthorizedPlugin)
     37     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NPAPINotSupported,
     38                         OnNPAPINotSupported)
     39 #if defined(ENABLE_PLUGIN_INSTALLATION)
     40     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FindMissingPlugin,
     41                         OnFindMissingPlugin)
     42 #endif
     43     IPC_MESSAGE_UNHANDLED(return false)
     44   IPC_END_MESSAGE_MAP()
     45 
     46   return true;
     47 }
     48 
     49 bool PluginPermissionHelper::OnMessageReceived(const IPC::Message& message) {
     50   IPC_BEGIN_MESSAGE_MAP(PluginPermissionHelper, message)
     51     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CouldNotLoadPlugin,
     52                         OnCouldNotLoadPlugin)
     53     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_OpenAboutPlugins,
     54                         OnOpenAboutPlugins)
     55 #if defined(ENABLE_PLUGIN_INSTALLATION)
     56     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RemovePluginPlaceholderHost,
     57                         OnRemovePluginPlaceholderHost)
     58 #endif
     59     IPC_MESSAGE_UNHANDLED(return false)
     60   IPC_END_MESSAGE_MAP()
     61 
     62   return true;
     63 }
     64 
     65 void PluginPermissionHelper::OnBlockedUnauthorizedPlugin(
     66     const base::string16& name,
     67     const std::string& identifier) {
     68   const char kPluginName[] = "name";
     69   const char kPluginIdentifier[] = "identifier";
     70 
     71   WebViewGuest* webview = WebViewGuest::FromWebContents(web_contents());
     72   if (!webview)
     73     return;
     74 
     75   base::DictionaryValue info;
     76   info.SetString(std::string(kPluginName), name);
     77   info.SetString(std::string(kPluginIdentifier), identifier);
     78   webview->RequestPermission(
     79       WEB_VIEW_PERMISSION_TYPE_LOAD_PLUGIN,
     80       info,
     81       base::Bind(&PluginPermissionHelper::OnPermissionResponse,
     82                  weak_factory_.GetWeakPtr(),
     83                  identifier),
     84       true /* allowed_by_default */);
     85   content::RecordAction(
     86       base::UserMetricsAction("WebView.Guest.PluginLoadRequest"));
     87 }
     88 
     89 void PluginPermissionHelper::OnCouldNotLoadPlugin(
     90     const base::FilePath& plugin_path) {
     91 }
     92 
     93 void PluginPermissionHelper::OnBlockedOutdatedPlugin(
     94     int placeholder_id,
     95     const std::string& identifier) {
     96 }
     97 
     98 void PluginPermissionHelper::OnNPAPINotSupported(const std::string& id) {
     99 }
    100 
    101 void PluginPermissionHelper::OnOpenAboutPlugins() {
    102 }
    103 
    104 #if defined(ENABLE_PLUGIN_INSTALLATION)
    105 void PluginPermissionHelper::OnFindMissingPlugin(int placeholder_id,
    106                                                  const std::string& mime_type) {
    107   Send(new ChromeViewMsg_DidNotFindMissingPlugin(placeholder_id));
    108 }
    109 
    110 void PluginPermissionHelper::OnRemovePluginPlaceholderHost(int placeholder_id) {
    111 }
    112 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
    113 
    114 void PluginPermissionHelper::OnPermissionResponse(const std::string& identifier,
    115                                                   bool allow,
    116                                                   const std::string& input) {
    117   if (allow) {
    118     ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
    119         web_contents(), true, identifier);
    120   }
    121 }
    122