- add sources.
[platform/framework/web/crosswalk.git] / src / components / plugins / renderer / plugin_placeholder.cc
1 // Copyright 2013 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 "components/plugins/renderer/plugin_placeholder.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/string_escape.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/public/common/content_constants.h"
15 #include "content/public/common/context_menu_params.h"
16 #include "content/public/renderer/render_thread.h"
17 #include "content/public/renderer/render_view.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebElement.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "third_party/WebKit/public/web/WebPluginContainer.h"
23 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 #include "third_party/WebKit/public/web/WebView.h"
25 #include "third_party/re2/re2/re2.h"
26
27 using content::RenderThread;
28 using WebKit::WebElement;
29 using WebKit::WebFrame;
30 using WebKit::WebMouseEvent;
31 using WebKit::WebNode;
32 using WebKit::WebPlugin;
33 using WebKit::WebPluginContainer;
34 using WebKit::WebPluginParams;
35 using WebKit::WebScriptSource;
36 using WebKit::WebURLRequest;
37 using webkit_glue::CppArgumentList;
38 using webkit_glue::CppVariant;
39
40 namespace plugins {
41
42 PluginPlaceholder::PluginPlaceholder(content::RenderView* render_view,
43                                      WebFrame* frame,
44                                      const WebPluginParams& params,
45                                      const std::string& html_data,
46                                      GURL placeholderDataUrl)
47     : content::RenderViewObserver(render_view),
48       frame_(frame),
49       plugin_params_(params),
50       plugin_(WebViewPlugin::Create(this,
51                                     render_view->GetWebkitPreferences(),
52                                     html_data,
53                                     placeholderDataUrl)),
54       is_blocked_for_prerendering_(false),
55       allow_loading_(false),
56       hidden_(false),
57       finished_loading_(false) {}
58
59 PluginPlaceholder::~PluginPlaceholder() {}
60
61 void PluginPlaceholder::BindWebFrame(WebFrame* frame) {
62   BindToJavascript(frame, "plugin");
63   BindCallback(
64       "load",
65       base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this)));
66   BindCallback(
67       "hide",
68       base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this)));
69   BindCallback("didFinishLoading",
70                base::Bind(&PluginPlaceholder::DidFinishLoadingCallback,
71                           base::Unretained(this)));
72 }
73
74 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) {
75   CHECK(plugin_);
76   if (!new_plugin) return;
77   WebPluginContainer* container = plugin_->container();
78   // Set the new plug-in on the container before initializing it.
79   container->setPlugin(new_plugin);
80   // Save the element in case the plug-in is removed from the page during
81   // initialization.
82   WebElement element = container->element();
83   if (!new_plugin->initialize(container)) {
84     // We couldn't initialize the new plug-in. Restore the old one and abort.
85     container->setPlugin(plugin_);
86     return;
87   }
88
89   // The plug-in has been removed from the page. Destroy the old plug-in
90   // (which will destroy us).
91   if (!element.pluginContainer()) {
92     plugin_->destroy();
93     return;
94   }
95
96   // During initialization, the new plug-in might have replaced itself in turn
97   // with another plug-in. Make sure not to use the passed in |new_plugin| after
98   // this point.
99   new_plugin = container->plugin();
100
101   plugin_->RestoreTitleText();
102   container->invalidate();
103   container->reportGeometry();
104   plugin_->ReplayReceivedData(new_plugin);
105   plugin_->destroy();
106 }
107
108 void PluginPlaceholder::HidePlugin() {
109   hidden_ = true;
110   WebPluginContainer* container = plugin_->container();
111   WebElement element = container->element();
112   element.setAttribute("style", "display: none;");
113   // If we have a width and height, search for a parent (often <div>) with the
114   // same dimensions. If we find such a parent, hide that as well.
115   // This makes much more uncovered page content usable (including clickable)
116   // as opposed to merely visible.
117   // TODO(cevans) -- it's a foul heurisitc but we're going to tolerate it for
118   // now for these reasons:
119   // 1) Makes the user experience better.
120   // 2) Foulness is encapsulated within this single function.
121   // 3) Confidence in no fasle positives.
122   // 4) Seems to have a good / low false negative rate at this time.
123   if (element.hasAttribute("width") && element.hasAttribute("height")) {
124     std::string width_str("width:[\\s]*");
125     width_str += element.getAttribute("width").utf8().data();
126     if (EndsWith(width_str, "px", false)) {
127       width_str = width_str.substr(0, width_str.length() - 2);
128     }
129     TrimWhitespace(width_str, TRIM_TRAILING, &width_str);
130     width_str += "[\\s]*px";
131     std::string height_str("height:[\\s]*");
132     height_str += element.getAttribute("height").utf8().data();
133     if (EndsWith(height_str, "px", false)) {
134       height_str = height_str.substr(0, height_str.length() - 2);
135     }
136     TrimWhitespace(height_str, TRIM_TRAILING, &height_str);
137     height_str += "[\\s]*px";
138     WebNode parent = element;
139     while (!parent.parentNode().isNull()) {
140       parent = parent.parentNode();
141       if (!parent.isElementNode())
142         continue;
143       element = parent.toConst<WebElement>();
144       if (element.hasAttribute("style")) {
145         std::string style_str = element.getAttribute("style").utf8();
146         if (RE2::PartialMatch(style_str, width_str) &&
147             RE2::PartialMatch(style_str, height_str))
148           element.setAttribute("style", "display: none;");
149       }
150     }
151   }
152 }
153
154 void PluginPlaceholder::WillDestroyPlugin() { delete this; }
155
156 void PluginPlaceholder::SetMessage(const string16& message) {
157   message_ = message;
158   if (finished_loading_)
159     UpdateMessage();
160 }
161
162 void PluginPlaceholder::UpdateMessage() {
163   std::string script =
164       "window.setMessage(" + base::GetDoubleQuotedJson(message_) + ")";
165   plugin_->web_view()->mainFrame()->executeScript(
166       WebScriptSource(ASCIIToUTF16(script)));
167 }
168
169 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
170   // Does nothing by default. Will be overridden if a specific browser wants
171   // a context menu.
172   return;
173 }
174
175 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) {
176   if (!identifier.empty() && identifier != identifier_)
177     return;
178
179   RenderThread::Get()->RecordUserMetrics("Plugin_Load_UI");
180   LoadPlugin();
181 }
182
183 void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
184   // Prerendering can only be enabled prior to a RenderView's first navigation,
185   // so no BlockedPlugin should see the notification that enables prerendering.
186   DCHECK(!is_prerendering);
187   if (is_blocked_for_prerendering_ && !is_prerendering)
188     LoadPlugin();
189 }
190
191 void PluginPlaceholder::LoadPlugin() {
192   // This is not strictly necessary but is an important defense in case the
193   // event propagation changes between "close" vs. "click-to-play".
194   if (hidden_)
195     return;
196   if (!allow_loading_) {
197     NOTREACHED();
198     return;
199   }
200
201   // TODO(mmenke):  In the case of prerendering, feed into
202   //                ChromeContentRendererClient::CreatePlugin instead, to
203   //                reduce the chance of future regressions.
204   WebPlugin* plugin =
205       render_view()->CreatePlugin(frame_, plugin_info_, plugin_params_);
206   ReplacePlugin(plugin);
207 }
208
209 void PluginPlaceholder::LoadCallback(const CppArgumentList& args,
210                                      CppVariant* result) {
211   RenderThread::Get()->RecordUserMetrics("Plugin_Load_Click");
212   LoadPlugin();
213 }
214
215 void PluginPlaceholder::HideCallback(const CppArgumentList& args,
216                                      CppVariant* result) {
217   RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Click");
218   HidePlugin();
219 }
220
221 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args,
222                                                  CppVariant* result) {
223   finished_loading_ = true;
224   if (message_.length() > 0)
225     UpdateMessage();
226 }
227
228 void PluginPlaceholder::SetPluginInfo(
229     const content::WebPluginInfo& plugin_info) {
230   plugin_info_ = plugin_info;
231 }
232
233 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const {
234   return plugin_info_;
235 }
236
237 void PluginPlaceholder::SetIdentifier(const std::string& identifier) {
238   identifier_ = identifier;
239 }
240
241 WebKit::WebFrame* PluginPlaceholder::GetFrame() { return frame_; }
242
243 const WebKit::WebPluginParams& PluginPlaceholder::GetPluginParams() const {
244   return plugin_params_;
245 }
246
247 }  // namespace plugins