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

Side by Side Diff: chrome/renderer/plugins/blocked_plugin.cc

Issue 8461011: Clean up plug-in placeholders: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/plugins/blocked_plugin.h ('k') | chrome/renderer/plugins/missing_plugin.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "chrome/renderer/blocked_plugin.h" 5 #include "chrome/renderer/plugins/blocked_plugin.h"
6 6
7 #include "base/string_piece.h" 7 #include "base/string_piece.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/common/jstemplate_builder.h" 10 #include "chrome/common/jstemplate_builder.h"
11 #include "chrome/common/render_messages.h" 11 #include "chrome/common/render_messages.h"
12 #include "chrome/renderer/plugin_uma.h" 12 #include "chrome/renderer/custom_menu_commands.h"
13 #include "content/public/renderer/render_thread.h" 13 #include "content/public/renderer/render_thread.h"
14 #include "content/public/renderer/render_view.h" 14 #include "content/public/renderer/render_view.h"
15 #include "grit/generated_resources.h" 15 #include "grit/generated_resources.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMenuItemInfo.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMenuItemInfo.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPoint.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPoint.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression. h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCaseSensitivit y.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
29 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h" 23 #include "ui/base/resource/resource_bundle.h"
31 #include "webkit/glue/webpreferences.h" 24 #include "webkit/glue/webpreferences.h"
32 #include "webkit/plugins/npapi/plugin_group.h" 25 #include "webkit/plugins/npapi/plugin_group.h"
33 #include "webkit/plugins/npapi/webview_plugin.h" 26 #include "webkit/plugins/webview_plugin.h"
34 27
35 using WebKit::WebContextMenuData; 28 using WebKit::WebContextMenuData;
36 using WebKit::WebElement;
37 using WebKit::WebFrame; 29 using WebKit::WebFrame;
38 using WebKit::WebMenuItemInfo; 30 using WebKit::WebMenuItemInfo;
39 using WebKit::WebNode;
40 using WebKit::WebPlugin; 31 using WebKit::WebPlugin;
41 using WebKit::WebPluginContainer;
42 using WebKit::WebPluginParams; 32 using WebKit::WebPluginParams;
43 using WebKit::WebPoint; 33 using WebKit::WebPoint;
44 using WebKit::WebRegularExpression;
45 using WebKit::WebString; 34 using WebKit::WebString;
46 using WebKit::WebURLRequest; 35 using WebKit::WebURLRequest;
47 using WebKit::WebVector; 36 using WebKit::WebVector;
48 using content::RenderThread; 37 using content::RenderThread;
38 using webkit::WebViewPlugin;
49 39
50 static const char* const kBlockedPluginDataURL = "chrome://blockedplugindata/"; 40 namespace {
51 // TODO(cevans) - move these to a shared header file so that there are no 41 const BlockedPlugin* g_last_active_menu = NULL;
52 // collisions in the longer term. Currently, blocked_plugin.cc is the only 42 }
53 // user of custom menu commands (extension menu items have their own range).
54 static const unsigned kMenuActionLoad = 1;
55 static const unsigned kMenuActionRemove = 2;
56 43
57 static const BlockedPlugin* g_last_active_menu; 44 // static
45 WebViewPlugin* BlockedPlugin::Create(content::RenderView* render_view,
46 WebFrame* frame,
47 const WebPluginParams& params,
48 const webkit::WebPluginInfo& plugin,
49 const webkit::npapi::PluginGroup* group,
50 int template_id,
51 int message_id,
52 bool is_blocked_for_prerendering,
53 bool allow_loading) {
54 string16 name = group->GetGroupName();
55 string16 message = l10n_util::GetStringFUTF16(message_id, name);
58 56
59 BlockedPlugin::BlockedPlugin(content::RenderView* render_view, 57 DictionaryValue values;
60 WebFrame* frame, 58 values.SetString("message", message);
61 const webkit::WebPluginInfo& info, 59 values.SetString("name", name);
62 const WebPluginParams& params, 60 values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
63 const WebPreferences& preferences, 61
64 int template_id,
65 const string16& name,
66 const string16& message,
67 bool is_blocked_for_prerendering,
68 bool allow_loading)
69 : content::RenderViewObserver(render_view),
70 frame_(frame),
71 plugin_info_(info),
72 plugin_params_(params),
73 name_(name),
74 is_blocked_for_prerendering_(is_blocked_for_prerendering),
75 hidden_(false),
76 allow_loading_(allow_loading) {
77 const base::StringPiece template_html( 62 const base::StringPiece template_html(
78 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id)); 63 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
79 64
80 DCHECK(!template_html.empty()) << "unable to load template. ID: " 65 DCHECK(!template_html.empty()) << "unable to load template. ID: "
81 << template_id; 66 << template_id;
82
83 DictionaryValue values;
84 values.SetString("message", message);
85 values.SetString("name", name_);
86 values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
87
88 // "t" is the id of the templates root node. 67 // "t" is the id of the templates root node.
89 std::string html_data = jstemplate_builder::GetI18nTemplateHtml( 68 std::string html_data = jstemplate_builder::GetI18nTemplateHtml(
90 template_html, &values); 69 template_html, &values);
91 70
92 plugin_ = webkit::npapi::WebViewPlugin::Create(this, 71 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
93 preferences, 72 BlockedPlugin* blocked_plugin = new BlockedPlugin(
94 html_data, 73 render_view, frame, params, html_data, plugin, name,
95 GURL(kBlockedPluginDataURL)); 74 is_blocked_for_prerendering, allow_loading);
75 return blocked_plugin->plugin();
76 }
77
78 BlockedPlugin::BlockedPlugin(content::RenderView* render_view,
79 WebFrame* frame,
80 const WebPluginParams& params,
81 const std::string& html_data,
82 const webkit::WebPluginInfo& info,
83 const string16& name,
84 bool is_blocked_for_prerendering,
85 bool allow_loading)
86 : PluginPlaceholder(render_view, frame, params, html_data),
87 plugin_info_(info),
88 name_(name),
89 is_blocked_for_prerendering_(is_blocked_for_prerendering),
90 hidden_(false),
91 allow_loading_(allow_loading) {
96 } 92 }
97 93
98 BlockedPlugin::~BlockedPlugin() { 94 BlockedPlugin::~BlockedPlugin() {
99 } 95 }
100 96
101 void BlockedPlugin::BindWebFrame(WebFrame* frame) { 97 void BlockedPlugin::BindWebFrame(WebFrame* frame) {
102 BindToJavascript(frame, "plugin"); 98 PluginPlaceholder::BindWebFrame(frame);
103 BindMethod("load", &BlockedPlugin::LoadCallback); 99 BindMethod("load", &BlockedPlugin::LoadCallback);
104 BindMethod("hide", &BlockedPlugin::HideCallback); 100 BindMethod("hide", &BlockedPlugin::HideCallback);
105 BindMethod("openURL", &BlockedPlugin::OpenUrlCallback); 101 BindMethod("openURL", &BlockedPlugin::OpenUrlCallback);
106 } 102 }
107 103
108 void BlockedPlugin::WillDestroyPlugin() {
109 delete this;
110 }
111
112 void BlockedPlugin::ShowContextMenu(const WebKit::WebMouseEvent& event) { 104 void BlockedPlugin::ShowContextMenu(const WebKit::WebMouseEvent& event) {
113 WebContextMenuData menu_data; 105 WebContextMenuData menu_data;
114 106
115 size_t num_items = name_.empty() ? 2u : 4u; 107 size_t num_items = name_.empty() ? 2u : 4u;
116 WebVector<WebMenuItemInfo> custom_items(num_items); 108 WebVector<WebMenuItemInfo> custom_items(num_items);
117 109
118 size_t i = 0; 110 size_t i = 0;
119 if (!name_.empty()) { 111 if (!name_.empty()) {
120 WebMenuItemInfo name_item; 112 WebMenuItemInfo name_item;
121 name_item.label = name_; 113 name_item.label = name_;
122 name_item.hasTextDirectionOverride = false; 114 name_item.hasTextDirectionOverride = false;
123 name_item.textDirection = WebKit::WebTextDirectionDefault; 115 name_item.textDirection = WebKit::WebTextDirectionDefault;
124 custom_items[i++] = name_item; 116 custom_items[i++] = name_item;
125 117
126 WebMenuItemInfo separator_item; 118 WebMenuItemInfo separator_item;
127 separator_item.type = WebMenuItemInfo::Separator; 119 separator_item.type = WebMenuItemInfo::Separator;
128 custom_items[i++] = separator_item; 120 custom_items[i++] = separator_item;
129 } 121 }
130 122
131 WebMenuItemInfo run_item; 123 WebMenuItemInfo run_item;
132 run_item.action = kMenuActionLoad; 124 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
133 // Disable this menu item if the plugin is blocked by policy. 125 // Disable this menu item if the plugin is blocked by policy.
134 run_item.enabled = allow_loading_; 126 run_item.enabled = allow_loading_;
135 run_item.label = WebString::fromUTF8( 127 run_item.label = WebString::fromUTF8(
136 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_RUN).c_str()); 128 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_RUN).c_str());
137 run_item.hasTextDirectionOverride = false; 129 run_item.hasTextDirectionOverride = false;
138 run_item.textDirection = WebKit::WebTextDirectionDefault; 130 run_item.textDirection = WebKit::WebTextDirectionDefault;
139 custom_items[i++] = run_item; 131 custom_items[i++] = run_item;
140 132
141 WebMenuItemInfo hide_item; 133 WebMenuItemInfo hide_item;
142 hide_item.action = kMenuActionRemove; 134 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
143 hide_item.enabled = true; 135 hide_item.enabled = true;
144 hide_item.label = WebString::fromUTF8( 136 hide_item.label = WebString::fromUTF8(
145 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_HIDE).c_str()); 137 l10n_util::GetStringUTF8(IDS_CONTENT_CONTEXT_PLUGIN_HIDE).c_str());
146 hide_item.hasTextDirectionOverride = false; 138 hide_item.hasTextDirectionOverride = false;
147 hide_item.textDirection = WebKit::WebTextDirectionDefault; 139 hide_item.textDirection = WebKit::WebTextDirectionDefault;
148 custom_items[i++] = hide_item; 140 custom_items[i++] = hide_item;
149 141
150 menu_data.customItems.swap(custom_items); 142 menu_data.customItems.swap(custom_items);
151 menu_data.mousePosition = WebPoint(event.windowX, event.windowY); 143 menu_data.mousePosition = WebPoint(event.windowX, event.windowY);
152 render_view()->ShowContextMenu(NULL, menu_data); 144 render_view()->ShowContextMenu(NULL, menu_data);
153 g_last_active_menu = this; 145 g_last_active_menu = this;
154 } 146 }
155 147
156 bool BlockedPlugin::OnMessageReceived(const IPC::Message& message) { 148 bool BlockedPlugin::OnMessageReceived(const IPC::Message& message) {
157 // We don't swallow ViewMsg_CustomContextMenuAction because we listen for all 149 // We don't swallow ViewMsg_CustomContextMenuAction because we listen for all
158 // custom menu IDs, and not just our own. We don't swallow 150 // custom menu IDs, and not just our own. We don't swallow
159 // ViewMsg_LoadBlockedPlugins because multiple blocked plugins have an 151 // ViewMsg_LoadBlockedPlugins because multiple blocked plugins have an
160 // interest in it. 152 // interest in it.
161 IPC_BEGIN_MESSAGE_MAP(BlockedPlugin, message) 153 IPC_BEGIN_MESSAGE_MAP(BlockedPlugin, message)
162 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins) 154 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
163 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsPrerendering, OnSetIsPrerendering) 155 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsPrerendering, OnSetIsPrerendering)
164 IPC_END_MESSAGE_MAP() 156 IPC_END_MESSAGE_MAP()
165 157
166 return false; 158 return false;
167 } 159 }
168 160
169 void BlockedPlugin::ContextMenuAction(unsigned id) { 161 void BlockedPlugin::ContextMenuAction(unsigned id) {
170 if (g_last_active_menu != this) 162 if (g_last_active_menu != this)
171 return; 163 return;
172 if (id == kMenuActionLoad) { 164 switch (id) {
173 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu"); 165 case chrome::MENU_COMMAND_PLUGIN_RUN: {
174 LoadPlugin(); 166 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu");
175 } else if (id == kMenuActionRemove) { 167 LoadPlugin();
176 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu"); 168 break;
177 HidePlugin(); 169 }
170 case chrome::MENU_COMMAND_PLUGIN_HIDE: {
171 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu");
172 HidePlugin();
173 break;
174 }
175 default:
176 NOTREACHED();
178 } 177 }
179 } 178 }
180 179
181 void BlockedPlugin::OnLoadBlockedPlugins() { 180 void BlockedPlugin::OnLoadBlockedPlugins() {
182 RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI"); 181 RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI");
183 LoadPlugin(); 182 LoadPlugin();
184 } 183 }
185 184
186 void BlockedPlugin::OnSetIsPrerendering(bool is_prerendering) { 185 void BlockedPlugin::OnSetIsPrerendering(bool is_prerendering) {
187 // Prerendering can only be enabled prior to a RenderView's first navigation, 186 // Prerendering can only be enabled prior to a RenderView's first navigation,
188 // so no BlockedPlugin should see the notification that enables prerendering. 187 // so no BlockedPlugin should see the notification that enables prerendering.
189 DCHECK(!is_prerendering); 188 DCHECK(!is_prerendering);
190 if (is_blocked_for_prerendering_ && !is_prerendering) 189 if (is_blocked_for_prerendering_ && !is_prerendering)
191 LoadPlugin(); 190 LoadPlugin();
192 } 191 }
193 192
194 void BlockedPlugin::LoadPlugin() { 193 void BlockedPlugin::LoadPlugin() {
195 CHECK(plugin_);
196 // This is not strictly necessary but is an important defense in case the 194 // This is not strictly necessary but is an important defense in case the
197 // event propagation changes between "close" vs. "click-to-play". 195 // event propagation changes between "close" vs. "click-to-play".
198 if (hidden_) 196 if (hidden_)
199 return; 197 return;
200 if (!allow_loading_) 198 if (!allow_loading_)
201 return; 199 return;
202 WebPluginContainer* container = plugin_->container(); 200 LoadPluginInternal(plugin_info_);
203 WebPlugin* new_plugin =
204 render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_);
205 if (new_plugin && new_plugin->initialize(container)) {
206 plugin_->RestoreTitleText();
207 container->setPlugin(new_plugin);
208 container->invalidate();
209 container->reportGeometry();
210 plugin_->ReplayReceivedData(new_plugin);
211 plugin_->destroy();
212 } else {
213 MissingPluginReporter::GetInstance()->ReportPluginMissing(
214 plugin_params_.mimeType.utf8(),
215 plugin_params_.url);
216 }
217 } 201 }
218 202
219 void BlockedPlugin::LoadCallback(const CppArgumentList& args, 203 void BlockedPlugin::LoadCallback(const CppArgumentList& args,
220 CppVariant* result) { 204 CppVariant* result) {
221 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click"); 205 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click");
222 LoadPlugin(); 206 LoadPlugin();
223 } 207 }
224 208
225 void BlockedPlugin::HideCallback(const CppArgumentList& args, 209 void BlockedPlugin::HideCallback(const CppArgumentList& args,
226 CppVariant* result) { 210 CppVariant* result) {
(...skipping 10 matching lines...) Expand all
237 if (!args[0].isString()) { 221 if (!args[0].isString()) {
238 NOTREACHED(); 222 NOTREACHED();
239 return; 223 return;
240 } 224 }
241 225
242 GURL url(args[0].ToString()); 226 GURL url(args[0].ToString());
243 WebURLRequest request; 227 WebURLRequest request;
244 request.initialize(); 228 request.initialize();
245 request.setURL(url); 229 request.setURL(url);
246 render_view()->LoadURLExternally( 230 render_view()->LoadURLExternally(
247 frame_, request, WebKit::WebNavigationPolicyNewForegroundTab); 231 frame(), request, WebKit::WebNavigationPolicyNewForegroundTab);
248 } 232 }
249 233
250 void BlockedPlugin::HidePlugin() { 234 void BlockedPlugin::HidePlugin() {
251 CHECK(plugin_);
252 hidden_ = true; 235 hidden_ = true;
253 WebPluginContainer* container = plugin_->container(); 236 HidePluginInternal();
254 WebElement element = container->element();
255 element.setAttribute("style", "display: none;");
256 // If we have a width and height, search for a parent (often <div>) with the
257 // same dimensions. If we find such a parent, hide that as well.
258 // This makes much more uncovered page content usable (including clickable)
259 // as opposed to merely visible.
260 // TODO(cevans) -- it's a foul heurisitc but we're going to tolerate it for
261 // now for these reasons:
262 // 1) Makes the user experience better.
263 // 2) Foulness is encapsulated within this single function.
264 // 3) Confidence in no fasle positives.
265 // 4) Seems to have a good / low false negative rate at this time.
266 if (element.hasAttribute("width") && element.hasAttribute("height")) {
267 std::string width_str("width:[\\s]*");
268 width_str += element.getAttribute("width").utf8().data();
269 if (EndsWith(width_str, "px", false)) {
270 width_str = width_str.substr(0, width_str.length() - 2);
271 }
272 TrimWhitespace(width_str, TRIM_TRAILING, &width_str);
273 width_str += "[\\s]*px";
274 WebRegularExpression width_regex(WebString::fromUTF8(width_str.c_str()),
275 WebKit::WebTextCaseSensitive);
276 std::string height_str("height:[\\s]*");
277 height_str += element.getAttribute("height").utf8().data();
278 if (EndsWith(height_str, "px", false)) {
279 height_str = height_str.substr(0, height_str.length() - 2);
280 }
281 TrimWhitespace(height_str, TRIM_TRAILING, &height_str);
282 height_str += "[\\s]*px";
283 WebRegularExpression height_regex(WebString::fromUTF8(height_str.c_str()),
284 WebKit::WebTextCaseSensitive);
285 WebNode parent = element;
286 while (!parent.parentNode().isNull()) {
287 parent = parent.parentNode();
288 if (!parent.isElementNode())
289 continue;
290 element = parent.toConst<WebElement>();
291 if (element.hasAttribute("style")) {
292 WebString style_str = element.getAttribute("style");
293 if (width_regex.match(style_str) >= 0 &&
294 height_regex.match(style_str) >= 0)
295 element.setAttribute("style", "display: none;");
296 }
297 }
298 }
299 } 237 }
OLDNEW
« no previous file with comments | « chrome/renderer/plugins/blocked_plugin.h ('k') | chrome/renderer/plugins/missing_plugin.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698