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"
9 #include "base/debug/crash_logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "content/public/renderer/content_renderer_client.h"
12 #include "content/renderer/pepper/message_channel.h"
13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
14 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
15 #include "content/renderer/pepper/plugin_module.h"
16 #include "content/renderer/pepper/v8object_var.h"
17 #include "content/renderer/render_frame_impl.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
19 #include "ppapi/shared_impl/var_tracker.h"
20 #include "third_party/WebKit/public/platform/WebPoint.h"
21 #include "third_party/WebKit/public/platform/WebRect.h"
22 #include "third_party/WebKit/public/platform/WebSize.h"
23 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
24 #include "third_party/WebKit/public/web/WebBindings.h"
25 #include "third_party/WebKit/public/web/WebDocument.h"
26 #include "third_party/WebKit/public/web/WebElement.h"
27 #include "third_party/WebKit/public/web/WebFrame.h"
28 #include "third_party/WebKit/public/web/WebPluginContainer.h"
29 #include "third_party/WebKit/public/web/WebPluginParams.h"
30 #include "third_party/WebKit/public/web/WebPrintParams.h"
31 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h"
32 #include "third_party/WebKit/public/web/WebPrintScalingOption.h"
35 using ppapi::V8ObjectVar
;
36 using blink::WebCanvas
;
37 using blink::WebPlugin
;
38 using blink::WebPluginContainer
;
39 using blink::WebPluginParams
;
40 using blink::WebPoint
;
41 using blink::WebPrintParams
;
44 using blink::WebString
;
46 using blink::WebVector
;
50 struct PepperWebPluginImpl::InitData
{
51 scoped_refptr
<PluginModule
> module
;
52 RenderFrameImpl
* render_frame
;
53 std::vector
<std::string
> arg_names
;
54 std::vector
<std::string
> arg_values
;
58 PepperWebPluginImpl::PepperWebPluginImpl(
59 PluginModule
* plugin_module
,
60 const WebPluginParams
& params
,
61 RenderFrameImpl
* render_frame
,
62 scoped_ptr
<PluginInstanceThrottlerImpl
> throttler
)
63 : init_data_(new InitData()),
64 full_frame_(params
.loadManually
),
65 throttler_(throttler
.Pass()),
66 instance_object_(PP_MakeUndefined()),
68 DCHECK(plugin_module
);
69 init_data_
->module
= plugin_module
;
70 init_data_
->render_frame
= render_frame
;
71 for (size_t i
= 0; i
< params
.attributeNames
.size(); ++i
) {
72 init_data_
->arg_names
.push_back(params
.attributeNames
[i
].utf8());
73 init_data_
->arg_values
.push_back(params
.attributeValues
[i
].utf8());
75 init_data_
->url
= params
.url
;
77 // Set subresource URL for crash reporting.
78 base::debug::SetCrashKeyValue("subresource_url", init_data_
->url
.spec());
81 throttler_
->SetWebPlugin(this);
84 PepperWebPluginImpl::~PepperWebPluginImpl() {}
86 blink::WebPluginContainer
* PepperWebPluginImpl::container() const {
90 bool PepperWebPluginImpl::initialize(WebPluginContainer
* container
) {
91 // The plugin delegate may have gone away.
92 instance_
= init_data_
->module
->CreateInstance(
93 init_data_
->render_frame
, container
, init_data_
->url
);
97 // Enable script objects for this plugin.
98 container
->allowScriptObjects();
101 instance_
->Initialize(init_data_
->arg_names
, init_data_
->arg_values
,
102 full_frame_
, throttler_
.Pass());
107 blink::WebPlugin
* replacement_plugin
=
108 GetContentClient()->renderer()->CreatePluginReplacement(
109 init_data_
->render_frame
, init_data_
->module
->path());
110 if (!replacement_plugin
|| !replacement_plugin
->initialize(container
))
113 container
->setPlugin(replacement_plugin
);
118 container_
= container
;
122 void PepperWebPluginImpl::destroy() {
123 // Tell |container_| to clear references to this plugin's script objects.
125 container_
->clearScriptObjects();
127 if (instance_
.get()) {
128 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(instance_object_
);
129 instance_object_
= PP_MakeUndefined();
134 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
137 v8::Local
<v8::Object
> PepperWebPluginImpl::v8ScriptableObject(
138 v8::Isolate
* isolate
) {
139 // Re-entrancy may cause JS to try to execute script on the plugin before it
140 // is fully initialized. See e.g. crbug.com/503401.
141 if (!instance_
.get())
142 return v8::Local
<v8::Object
>();
143 // Call through the plugin to get its instance object. The plugin should pass
144 // us a reference which we release in destroy().
145 if (instance_object_
.type
== PP_VARTYPE_UNDEFINED
)
146 instance_object_
= instance_
->GetInstanceObject(isolate
);
147 // GetInstanceObject talked to the plugin which may have removed the instance
148 // from the DOM, in which case instance_ would be NULL now.
149 if (!instance_
.get())
150 return v8::Local
<v8::Object
>();
152 scoped_refptr
<V8ObjectVar
> object_var(
153 V8ObjectVar::FromPPVar(instance_object_
));
154 // If there's an InstanceObject, tell the Instance's MessageChannel to pass
155 // any non-postMessage calls to it.
156 if (object_var
.get()) {
157 MessageChannel
* message_channel
= instance_
->message_channel();
159 message_channel
->SetPassthroughObject(object_var
->GetHandle());
162 v8::Local
<v8::Object
> result
= instance_
->GetMessageChannelObject();
166 bool PepperWebPluginImpl::getFormValue(WebString
& value
) { return false; }
168 void PepperWebPluginImpl::paint(WebCanvas
* canvas
, const WebRect
& rect
) {
169 if (!instance_
->FlashIsFullscreenOrPending())
170 instance_
->Paint(canvas
, plugin_rect_
, rect
);
173 void PepperWebPluginImpl::updateGeometry(
174 const WebRect
& window_rect
,
175 const WebRect
& clip_rect
,
176 const WebRect
& unobscured_rect
,
177 const WebVector
<WebRect
>& cut_outs_rects
,
179 plugin_rect_
= window_rect
;
180 if (instance_
&& !instance_
->FlashIsFullscreenOrPending()) {
181 std::vector
<gfx::Rect
> cut_outs
;
182 for (size_t i
= 0; i
< cut_outs_rects
.size(); ++i
)
183 cut_outs
.push_back(cut_outs_rects
[i
]);
184 instance_
->ViewChanged(plugin_rect_
, clip_rect
, unobscured_rect
, cut_outs
);
188 void PepperWebPluginImpl::updateFocus(bool focused
,
189 blink::WebFocusType focus_type
) {
190 instance_
->SetWebKitFocus(focused
);
193 void PepperWebPluginImpl::updateVisibility(bool visible
) {}
195 bool PepperWebPluginImpl::acceptsInputEvents() { return true; }
197 bool PepperWebPluginImpl::handleInputEvent(const blink::WebInputEvent
& event
,
198 blink::WebCursorInfo
& cursor_info
) {
199 if (instance_
->FlashIsFullscreenOrPending())
201 return instance_
->HandleInputEvent(event
, &cursor_info
);
204 void PepperWebPluginImpl::didReceiveResponse(
205 const blink::WebURLResponse
& response
) {
206 DCHECK(!instance_
->document_loader());
207 instance_
->HandleDocumentLoad(response
);
210 void PepperWebPluginImpl::didReceiveData(const char* data
, int data_length
) {
211 blink::WebURLLoaderClient
* document_loader
= instance_
->document_loader();
213 document_loader
->didReceiveData(NULL
, data
, data_length
, 0);
216 void PepperWebPluginImpl::didFinishLoading() {
217 blink::WebURLLoaderClient
* document_loader
= instance_
->document_loader();
219 document_loader
->didFinishLoading(
220 NULL
, 0.0, blink::WebURLLoaderClient::kUnknownEncodedDataLength
);
223 void PepperWebPluginImpl::didFailLoading(const blink::WebURLError
& error
) {
224 blink::WebURLLoaderClient
* document_loader
= instance_
->document_loader();
226 document_loader
->didFail(NULL
, error
);
229 void PepperWebPluginImpl::didFinishLoadingFrameRequest(const blink::WebURL
& url
,
230 void* notify_data
) {}
232 void PepperWebPluginImpl::didFailLoadingFrameRequest(
233 const blink::WebURL
& url
,
235 const blink::WebURLError
& error
) {}
237 bool PepperWebPluginImpl::hasSelection() const {
238 return !selectionAsText().isEmpty();
241 WebString
PepperWebPluginImpl::selectionAsText() const {
242 return instance_
->GetSelectedText(false);
245 WebString
PepperWebPluginImpl::selectionAsMarkup() const {
246 return instance_
->GetSelectedText(true);
249 WebURL
PepperWebPluginImpl::linkAtPosition(const WebPoint
& position
) const {
250 return GURL(instance_
->GetLinkAtPosition(position
));
253 bool PepperWebPluginImpl::startFind(const blink::WebString
& search_text
,
256 return instance_
->StartFind(search_text
, case_sensitive
, identifier
);
259 void PepperWebPluginImpl::selectFindResult(bool forward
) {
260 instance_
->SelectFindResult(forward
);
263 void PepperWebPluginImpl::stopFind() { instance_
->StopFind(); }
265 bool PepperWebPluginImpl::supportsPaginatedPrint() {
266 return instance_
->SupportsPrintInterface();
269 bool PepperWebPluginImpl::isPrintScalingDisabled() {
270 return instance_
->IsPrintScalingDisabled();
273 int PepperWebPluginImpl::printBegin(const WebPrintParams
& print_params
) {
274 return instance_
->PrintBegin(print_params
);
277 void PepperWebPluginImpl::printPage(int page_number
, blink::WebCanvas
* canvas
) {
278 instance_
->PrintPage(page_number
, canvas
);
281 void PepperWebPluginImpl::printEnd() { instance_
->PrintEnd(); }
283 bool PepperWebPluginImpl::getPrintPresetOptionsFromDocument(
284 blink::WebPrintPresetOptions
* preset_options
) {
285 return instance_
->GetPrintPresetOptionsFromDocument(preset_options
);
288 bool PepperWebPluginImpl::canRotateView() { return instance_
->CanRotateView(); }
290 void PepperWebPluginImpl::rotateView(RotationType type
) {
291 instance_
->RotateView(type
);
294 bool PepperWebPluginImpl::isPlaceholder() { return false; }
296 } // namespace content