summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/plugins/blocked_plugin.cc
blob: 2438de6a41eb298b27da74fc53dbb8902c4f2113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/plugins/blocked_plugin.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/custom_menu_commands.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "grit/generated_resources.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebMenuItemInfo.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/webpreferences.h"
#include "webkit/plugins/npapi/plugin_group.h"
#include "webkit/plugins/webview_plugin.h"

using WebKit::WebContextMenuData;
using WebKit::WebFrame;
using WebKit::WebMenuItemInfo;
using WebKit::WebPlugin;
using WebKit::WebPluginParams;
using WebKit::WebPoint;
using WebKit::WebString;
using WebKit::WebURLRequest;
using WebKit::WebVector;
using content::RenderThread;
using webkit::WebViewPlugin;

namespace {
const BlockedPlugin* g_last_active_menu = NULL;
}

// static
WebViewPlugin* BlockedPlugin::Create(content::RenderView* render_view,
                                     WebFrame* frame,
                                     const WebPluginParams& params,
                                     const webkit::WebPluginInfo& plugin,
                                     const webkit::npapi::PluginGroup* group,
                                     int template_id,
                                     int message_id,
                                     bool is_blocked_for_prerendering,
                                     bool allow_loading) {
  string16 name = group->GetGroupName();
  string16 message = l10n_util::GetStringFUTF16(message_id, name);

  DictionaryValue values;
  values.SetString("message", message);
  values.SetString("name", name);
  values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));

  const base::StringPiece template_html(
      ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));

  DCHECK(!template_html.empty()) << "unable to load template. ID: "
                                 << template_id;
  // "t" is the id of the templates root node.
  std::string html_data = jstemplate_builder::GetI18nTemplateHtml(
      template_html, &values);

  // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
  BlockedPlugin* blocked_plugin = new BlockedPlugin(
      render_view, frame, params, html_data, plugin, name,
      is_blocked_for_prerendering, allow_loading);
  return blocked_plugin->plugin();
}

BlockedPlugin::BlockedPlugin(content::RenderView* render_view,
                             WebFrame* frame,
                             const WebPluginParams& params,
                             const std::string& html_data,
                             const webkit::WebPluginInfo& info,
                             const string16& name,
                             bool is_blocked_for_prerendering,
                             bool allow_loading)
    : PluginPlaceholder(render_view, frame, params, html_data),
      plugin_info_(info),
      name_(name),
      is_blocked_for_prerendering_(is_blocked_for_prerendering),
      hidden_(false),
      allow_loading_(allow_loading) {
}

BlockedPlugin::~BlockedPlugin() {
}

void BlockedPlugin::BindWebFrame(WebFrame* frame) {
  PluginPlaceholder::BindWebFrame(frame);
  BindCallback("load", base::Bind(&BlockedPlugin::LoadCallback,
                                  base::Unretained(this)));
  BindCallback("hide", base::Bind(&BlockedPlugin::HideCallback,
                                  base::Unretained(this)));
  BindCallback("openURL", base::Bind(&BlockedPlugin::OpenUrlCallback,
                                     base::Unretained(this)));
}

void BlockedPlugin::ShowContextMenu(const WebKit::WebMouseEvent& event) {
  WebContextMenuData menu_data;

  size_t num_items = name_.empty() ? 2u : 4u;
  WebVector<WebMenuItemInfo> custom_items(num_items);

  size_t i = 0;
  if (!name_.empty()) {
    WebMenuItemInfo name_item;
    name_item.label = name_;
    name_item.hasTextDirectionOverride = false;
    name_item.textDirection =  WebKit::WebTextDirectionDefault;
    custom_items[i++] = name_item;

    WebMenuItemInfo separator_item;
    separator_item.type = WebMenuItemInfo::Separator;
    custom_items[i++] = separator_item;
  }

  WebMenuItemInfo run_item;
  run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
  // Disable this menu item if the plugin is blocked by policy.
  run_item.enabled = allow_loading_;
  run_item.label = WebString::fromUTF8(
      l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_RUN).c_str());
  run_item.hasTextDirectionOverride = false;
  run_item.textDirection =  WebKit::WebTextDirectionDefault;
  custom_items[i++] = run_item;

  WebMenuItemInfo hide_item;
  hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
  hide_item.enabled = true;
  hide_item.label = WebString::fromUTF8(
      l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_HIDE).c_str());
  hide_item.hasTextDirectionOverride = false;
  hide_item.textDirection =  WebKit::WebTextDirectionDefault;
  custom_items[i++] = hide_item;

  menu_data.customItems.swap(custom_items);
  menu_data.mousePosition = WebPoint(event.windowX, event.windowY);
  render_view()->ShowContextMenu(NULL, menu_data);
  g_last_active_menu = this;
}

bool BlockedPlugin::OnMessageReceived(const IPC::Message& message) {
  // We don't swallow ViewMsg_CustomContextMenuAction because we listen for all
  // custom menu IDs, and not just our own. We don't swallow
  // ViewMsg_LoadBlockedPlugins because multiple blocked plugins have an
  // interest in it.
  IPC_BEGIN_MESSAGE_MAP(BlockedPlugin, message)
    IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
    IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsPrerendering, OnSetIsPrerendering)
  IPC_END_MESSAGE_MAP()

  return false;
}

void BlockedPlugin::ContextMenuAction(unsigned id) {
  if (g_last_active_menu != this)
    return;
  switch (id) {
    case chrome::MENU_COMMAND_PLUGIN_RUN: {
      RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu");
      LoadPlugin();
      break;
    }
    case chrome::MENU_COMMAND_PLUGIN_HIDE: {
      RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu");
      HidePlugin();
      break;
    }
    default:
      NOTREACHED();
  }
}

void BlockedPlugin::OnLoadBlockedPlugins() {
  RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI");
  LoadPlugin();
}

void BlockedPlugin::OnSetIsPrerendering(bool is_prerendering) {
  // Prerendering can only be enabled prior to a RenderView's first navigation,
  // so no BlockedPlugin should see the notification that enables prerendering.
  DCHECK(!is_prerendering);
  if (is_blocked_for_prerendering_ && !is_prerendering)
    LoadPlugin();
}

void BlockedPlugin::LoadPlugin() {
  // This is not strictly necessary but is an important defense in case the
  // event propagation changes between "close" vs. "click-to-play".
  if (hidden_)
    return;
  if (!allow_loading_)
    return;
  LoadPluginInternal(plugin_info_);
}

void BlockedPlugin::LoadCallback(const CppArgumentList& args,
                                 CppVariant* result) {
  RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click");
  LoadPlugin();
}

void BlockedPlugin::HideCallback(const CppArgumentList& args,
                                 CppVariant* result) {
  RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click");
  HidePlugin();
}

void BlockedPlugin::OpenUrlCallback(const CppArgumentList& args,
                                    CppVariant* result) {
  if (args.size() < 1) {
    NOTREACHED();
    return;
  }
  if (!args[0].isString()) {
    NOTREACHED();
    return;
  }

  GURL url(args[0].ToString());
  WebURLRequest request;
  request.initialize();
  request.setURL(url);
  render_view()->LoadURLExternally(
      frame(), request, WebKit::WebNavigationPolicyNewForegroundTab);
}

void BlockedPlugin::HidePlugin() {
  hidden_ = true;
  HidePluginInternal();
}