Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_webplugin_impl.cc
blob5e23fb07cbe52e22d32c1b9815053c38c46eea7e
1 // Copyright (c) 2012 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.
5 #include "content/renderer/pepper/pepper_webplugin_impl.h"
7 #include <cmath>
9 #include "base/debug/crash_logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "content/public/common/page_zoom.h"
12 #include "content/public/renderer/content_renderer_client.h"
13 #include "content/renderer/pepper/message_channel.h"
14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
15 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
16 #include "content/renderer/pepper/plugin_module.h"
17 #include "content/renderer/pepper/v8object_var.h"
18 #include "content/renderer/render_frame_impl.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
20 #include "ppapi/shared_impl/var_tracker.h"
21 #include "third_party/WebKit/public/platform/WebPoint.h"
22 #include "third_party/WebKit/public/platform/WebRect.h"
23 #include "third_party/WebKit/public/platform/WebSize.h"
24 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
25 #include "third_party/WebKit/public/web/WebBindings.h"
26 #include "third_party/WebKit/public/web/WebDocument.h"
27 #include "third_party/WebKit/public/web/WebElement.h"
28 #include "third_party/WebKit/public/web/WebFrame.h"
29 #include "third_party/WebKit/public/web/WebPluginContainer.h"
30 #include "third_party/WebKit/public/web/WebPluginParams.h"
31 #include "third_party/WebKit/public/web/WebPrintParams.h"
32 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h"
33 #include "third_party/WebKit/public/web/WebPrintScalingOption.h"
34 #include "url/gurl.h"
36 using ppapi::V8ObjectVar;
37 using blink::WebCanvas;
38 using blink::WebPlugin;
39 using blink::WebPluginContainer;
40 using blink::WebPluginParams;
41 using blink::WebPoint;
42 using blink::WebPrintParams;
43 using blink::WebRect;
44 using blink::WebSize;
45 using blink::WebString;
46 using blink::WebURL;
47 using blink::WebVector;
49 namespace content {
51 struct PepperWebPluginImpl::InitData {
52 scoped_refptr<PluginModule> module;
53 RenderFrameImpl* render_frame;
54 std::vector<std::string> arg_names;
55 std::vector<std::string> arg_values;
56 GURL url;
59 PepperWebPluginImpl::PepperWebPluginImpl(
60 PluginModule* plugin_module,
61 const WebPluginParams& params,
62 RenderFrameImpl* render_frame,
63 scoped_ptr<PluginInstanceThrottlerImpl> throttler)
64 : init_data_(new InitData()),
65 full_frame_(params.loadManually),
66 throttler_(throttler.Pass()),
67 instance_object_(PP_MakeUndefined()),
68 container_(NULL) {
69 DCHECK(plugin_module);
70 init_data_->module = plugin_module;
71 init_data_->render_frame = render_frame;
72 for (size_t i = 0; i < params.attributeNames.size(); ++i) {
73 init_data_->arg_names.push_back(params.attributeNames[i].utf8());
74 init_data_->arg_values.push_back(params.attributeValues[i].utf8());
76 init_data_->url = params.url;
78 // Set subresource URL for crash reporting.
79 base::debug::SetCrashKeyValue("subresource_url", init_data_->url.spec());
81 if (throttler_)
82 throttler_->SetWebPlugin(this);
85 PepperWebPluginImpl::~PepperWebPluginImpl() {}
87 blink::WebPluginContainer* PepperWebPluginImpl::container() const {
88 return container_;
91 bool PepperWebPluginImpl::initialize(WebPluginContainer* container) {
92 // The plugin delegate may have gone away.
93 instance_ = init_data_->module->CreateInstance(
94 init_data_->render_frame, container, init_data_->url);
95 if (!instance_.get())
96 return false;
98 // Enable script objects for this plugin.
99 container->allowScriptObjects();
101 bool success =
102 instance_->Initialize(init_data_->arg_names, init_data_->arg_values,
103 full_frame_, throttler_.Pass());
104 if (!success) {
105 instance_->Delete();
106 instance_ = NULL;
108 blink::WebPlugin* replacement_plugin =
109 GetContentClient()->renderer()->CreatePluginReplacement(
110 init_data_->render_frame, init_data_->module->path());
111 if (!replacement_plugin || !replacement_plugin->initialize(container))
112 return false;
114 container->setPlugin(replacement_plugin);
115 return true;
118 init_data_.reset();
119 container_ = container;
120 return true;
123 void PepperWebPluginImpl::destroy() {
124 // Tell |container_| to clear references to this plugin's script objects.
125 if (container_)
126 container_->clearScriptObjects();
128 if (instance_.get()) {
129 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(instance_object_);
130 instance_object_ = PP_MakeUndefined();
131 instance_->Delete();
132 instance_ = NULL;
135 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
138 v8::Local<v8::Object> PepperWebPluginImpl::v8ScriptableObject(
139 v8::Isolate* isolate) {
140 // Call through the plugin to get its instance object. The plugin should pass
141 // us a reference which we release in destroy().
142 if (instance_object_.type == PP_VARTYPE_UNDEFINED)
143 instance_object_ = instance_->GetInstanceObject(isolate);
144 // GetInstanceObject talked to the plugin which may have removed the instance
145 // from the DOM, in which case instance_ would be NULL now.
146 if (!instance_.get())
147 return v8::Local<v8::Object>();
149 scoped_refptr<V8ObjectVar> object_var(
150 V8ObjectVar::FromPPVar(instance_object_));
151 // If there's an InstanceObject, tell the Instance's MessageChannel to pass
152 // any non-postMessage calls to it.
153 if (object_var.get()) {
154 MessageChannel* message_channel = instance_->message_channel();
155 if (message_channel)
156 message_channel->SetPassthroughObject(object_var->GetHandle());
159 v8::Handle<v8::Object> result = instance_->GetMessageChannelObject();
160 return result;
163 bool PepperWebPluginImpl::getFormValue(WebString& value) { return false; }
165 void PepperWebPluginImpl::paint(WebCanvas* canvas, const WebRect& rect) {
166 if (!instance_->FlashIsFullscreenOrPending())
167 instance_->Paint(canvas, plugin_rect_, rect);
170 void PepperWebPluginImpl::updateGeometry(
171 const WebRect& window_rect,
172 const WebRect& clip_rect,
173 const WebRect& unobscured_rect,
174 const WebVector<WebRect>& cut_outs_rects,
175 bool is_visible) {
176 plugin_rect_ = window_rect;
177 if (!instance_->FlashIsFullscreenOrPending()) {
178 std::vector<gfx::Rect> cut_outs;
179 for (size_t i = 0; i < cut_outs_rects.size(); ++i)
180 cut_outs.push_back(cut_outs_rects[i]);
181 instance_->ViewChanged(plugin_rect_, clip_rect, unobscured_rect, cut_outs);
185 void PepperWebPluginImpl::updateFocus(bool focused,
186 blink::WebFocusType focus_type) {
187 instance_->SetWebKitFocus(focused);
190 void PepperWebPluginImpl::updateVisibility(bool visible) {}
192 bool PepperWebPluginImpl::acceptsInputEvents() { return true; }
194 bool PepperWebPluginImpl::handleInputEvent(const blink::WebInputEvent& event,
195 blink::WebCursorInfo& cursor_info) {
196 if (instance_->FlashIsFullscreenOrPending())
197 return false;
198 return instance_->HandleInputEvent(event, &cursor_info);
201 void PepperWebPluginImpl::didReceiveResponse(
202 const blink::WebURLResponse& response) {
203 DCHECK(!instance_->document_loader());
204 instance_->HandleDocumentLoad(response);
207 void PepperWebPluginImpl::didReceiveData(const char* data, int data_length) {
208 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
209 if (document_loader)
210 document_loader->didReceiveData(NULL, data, data_length, 0);
213 void PepperWebPluginImpl::didFinishLoading() {
214 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
215 if (document_loader)
216 document_loader->didFinishLoading(
217 NULL, 0.0, blink::WebURLLoaderClient::kUnknownEncodedDataLength);
220 void PepperWebPluginImpl::didFailLoading(const blink::WebURLError& error) {
221 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
222 if (document_loader)
223 document_loader->didFail(NULL, error);
226 void PepperWebPluginImpl::didFinishLoadingFrameRequest(const blink::WebURL& url,
227 void* notify_data) {}
229 void PepperWebPluginImpl::didFailLoadingFrameRequest(
230 const blink::WebURL& url,
231 void* notify_data,
232 const blink::WebURLError& error) {}
234 bool PepperWebPluginImpl::hasSelection() const {
235 return !selectionAsText().isEmpty();
238 WebString PepperWebPluginImpl::selectionAsText() const {
239 return instance_->GetSelectedText(false);
242 WebString PepperWebPluginImpl::selectionAsMarkup() const {
243 return instance_->GetSelectedText(true);
246 WebURL PepperWebPluginImpl::linkAtPosition(const WebPoint& position) const {
247 return GURL(instance_->GetLinkAtPosition(position));
250 void PepperWebPluginImpl::setZoomLevel(double level, bool text_only) {
251 instance_->Zoom(content::ZoomLevelToZoomFactor(level), text_only);
254 bool PepperWebPluginImpl::startFind(const blink::WebString& search_text,
255 bool case_sensitive,
256 int identifier) {
257 return instance_->StartFind(search_text, case_sensitive, identifier);
260 void PepperWebPluginImpl::selectFindResult(bool forward) {
261 instance_->SelectFindResult(forward);
264 void PepperWebPluginImpl::stopFind() { instance_->StopFind(); }
266 bool PepperWebPluginImpl::supportsPaginatedPrint() {
267 return instance_->SupportsPrintInterface();
270 bool PepperWebPluginImpl::isPrintScalingDisabled() {
271 return instance_->IsPrintScalingDisabled();
274 int PepperWebPluginImpl::printBegin(const WebPrintParams& print_params) {
275 return instance_->PrintBegin(print_params);
278 bool PepperWebPluginImpl::printPage(int page_number, blink::WebCanvas* canvas) {
279 return instance_->PrintPage(page_number, canvas);
282 void PepperWebPluginImpl::printEnd() { return instance_->PrintEnd(); }
284 bool PepperWebPluginImpl::getPrintPresetOptionsFromDocument(
285 blink::WebPrintPresetOptions* preset_options) {
286 return instance_->GetPrintPresetOptionsFromDocument(preset_options);
289 bool PepperWebPluginImpl::canRotateView() { return instance_->CanRotateView(); }
291 void PepperWebPluginImpl::rotateView(RotationType type) {
292 instance_->RotateView(type);
295 bool PepperWebPluginImpl::isPlaceholder() { return false; }
297 } // namespace content