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/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/npobject_var.h"
15 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
16 #include "content/renderer/pepper/plugin_module.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/WebPrintScalingOption.h"
34 using ppapi::NPObjectVar;
35 using blink::WebCanvas;
36 using blink::WebPlugin;
37 using blink::WebPluginContainer;
38 using blink::WebPluginParams;
39 using blink::WebPoint;
40 using blink::WebPrintParams;
43 using blink::WebString;
45 using blink::WebVector;
49 struct PepperWebPluginImpl::InitData {
50 scoped_refptr<PluginModule> module;
51 RenderFrameImpl* render_frame;
52 std::vector<std::string> arg_names;
53 std::vector<std::string> arg_values;
57 PepperWebPluginImpl::PepperWebPluginImpl(
58 PluginModule* plugin_module,
59 const WebPluginParams& params,
60 RenderFrameImpl* render_frame)
61 : init_data_(new InitData()),
62 full_frame_(params.loadManually),
63 instance_object_(PP_MakeUndefined()),
65 DCHECK(plugin_module);
66 init_data_->module = plugin_module;
67 init_data_->render_frame = render_frame;
68 for (size_t i = 0; i < params.attributeNames.size(); ++i) {
69 init_data_->arg_names.push_back(params.attributeNames[i].utf8());
70 init_data_->arg_values.push_back(params.attributeValues[i].utf8());
72 init_data_->url = params.url;
74 // Set subresource URL for crash reporting.
75 base::debug::SetCrashKeyValue("subresource_url", init_data_->url.spec());
78 PepperWebPluginImpl::~PepperWebPluginImpl() {
81 blink::WebPluginContainer* PepperWebPluginImpl::container() const {
85 bool PepperWebPluginImpl::initialize(WebPluginContainer* container) {
86 // The plugin delegate may have gone away.
87 instance_ = init_data_->module->CreateInstance(
88 init_data_->render_frame, container, init_data_->url);
92 // Enable script objects for this plugin.
93 container->allowScriptObjects();
95 bool success = instance_->Initialize(init_data_->arg_names,
96 init_data_->arg_values,
102 blink::WebPlugin* replacement_plugin =
103 GetContentClient()->renderer()->CreatePluginReplacement(
104 init_data_->render_frame, init_data_->module->path());
105 if (!replacement_plugin || !replacement_plugin->initialize(container))
108 container->setPlugin(replacement_plugin);
113 container_ = container;
117 void PepperWebPluginImpl::destroy() {
118 // Tell |container_| to clear references to this plugin's script objects.
120 container_->clearScriptObjects();
122 if (instance_.get()) {
123 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(instance_object_);
124 instance_object_ = PP_MakeUndefined();
129 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
132 NPObject* PepperWebPluginImpl::scriptableObject() {
133 // Call through the plugin to get its instance object. The plugin should pass
134 // us a reference which we release in destroy().
135 if (instance_object_.type == PP_VARTYPE_UNDEFINED)
136 instance_object_ = instance_->GetInstanceObject();
137 // GetInstanceObject talked to the plugin which may have removed the instance
138 // from the DOM, in which case instance_ would be NULL now.
139 if (!instance_.get())
142 scoped_refptr<NPObjectVar> object(NPObjectVar::FromPPVar(instance_object_));
143 // If there's an InstanceObject, tell the Instance's MessageChannel to pass
144 // any non-postMessage calls to it.
146 instance_->message_channel().SetPassthroughObject(object->np_object());
148 NPObject* message_channel_np_object(instance_->message_channel().np_object());
149 // The object is expected to be retained before it is returned.
150 blink::WebBindings::retainObject(message_channel_np_object);
151 return message_channel_np_object;
154 NPP PepperWebPluginImpl::pluginNPP() {
155 return instance_->instanceNPP();
158 bool PepperWebPluginImpl::getFormValue(WebString& value) {
162 void PepperWebPluginImpl::paint(WebCanvas* canvas, const WebRect& rect) {
163 if (!instance_->FlashIsFullscreenOrPending())
164 instance_->Paint(canvas, plugin_rect_, rect);
167 void PepperWebPluginImpl::updateGeometry(
168 const WebRect& window_rect,
169 const WebRect& clip_rect,
170 const WebVector<WebRect>& cut_outs_rects,
172 plugin_rect_ = window_rect;
173 if (!instance_->FlashIsFullscreenOrPending()) {
174 std::vector<gfx::Rect> cut_outs;
175 for (size_t i = 0; i < cut_outs_rects.size(); ++i)
176 cut_outs.push_back(cut_outs_rects[i]);
177 instance_->ViewChanged(plugin_rect_, clip_rect, cut_outs);
181 void PepperWebPluginImpl::updateFocus(bool focused) {
182 instance_->SetWebKitFocus(focused);
185 void PepperWebPluginImpl::updateVisibility(bool visible) {
188 bool PepperWebPluginImpl::acceptsInputEvents() {
192 bool PepperWebPluginImpl::handleInputEvent(const blink::WebInputEvent& event,
193 blink::WebCursorInfo& cursor_info) {
194 if (instance_->FlashIsFullscreenOrPending())
196 return instance_->HandleInputEvent(event, &cursor_info);
199 void PepperWebPluginImpl::didReceiveResponse(
200 const blink::WebURLResponse& response) {
201 DCHECK(!instance_->document_loader());
202 instance_->HandleDocumentLoad(response);
205 void PepperWebPluginImpl::didReceiveData(const char* data, int data_length) {
206 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
208 document_loader->didReceiveData(NULL, data, data_length, 0);
211 void PepperWebPluginImpl::didFinishLoading() {
212 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
214 document_loader->didFinishLoading(NULL, 0.0,
215 blink::WebURLLoaderClient::kUnknownEncodedDataLength);
218 void PepperWebPluginImpl::didFailLoading(const blink::WebURLError& error) {
219 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
221 document_loader->didFail(NULL, error);
224 void PepperWebPluginImpl::didFinishLoadingFrameRequest(
225 const blink::WebURL& url,
229 void PepperWebPluginImpl::didFailLoadingFrameRequest(
230 const blink::WebURL& url,
232 const blink::WebURLError& error) {
235 bool PepperWebPluginImpl::hasSelection() const {
236 return !selectionAsText().isEmpty();
239 WebString PepperWebPluginImpl::selectionAsText() const {
240 return instance_->GetSelectedText(false);
243 WebString PepperWebPluginImpl::selectionAsMarkup() const {
244 return instance_->GetSelectedText(true);
247 WebURL PepperWebPluginImpl::linkAtPosition(const WebPoint& position) const {
248 return GURL(instance_->GetLinkAtPosition(position));
251 void PepperWebPluginImpl::setZoomLevel(double level, bool text_only) {
252 instance_->Zoom(content::ZoomLevelToZoomFactor(level), text_only);
255 bool PepperWebPluginImpl::startFind(const blink::WebString& search_text,
258 return instance_->StartFind(search_text, case_sensitive, identifier);
261 void PepperWebPluginImpl::selectFindResult(bool forward) {
262 instance_->SelectFindResult(forward);
265 void PepperWebPluginImpl::stopFind() {
266 instance_->StopFind();
269 bool PepperWebPluginImpl::supportsPaginatedPrint() {
270 return instance_->SupportsPrintInterface();
273 bool PepperWebPluginImpl::isPrintScalingDisabled() {
274 return instance_->IsPrintScalingDisabled();
277 int PepperWebPluginImpl::printBegin(const WebPrintParams& print_params) {
278 return instance_->PrintBegin(print_params);
281 bool PepperWebPluginImpl::printPage(int page_number,
282 blink::WebCanvas* canvas) {
283 return instance_->PrintPage(page_number, canvas);
286 void PepperWebPluginImpl::printEnd() {
287 return instance_->PrintEnd();
290 bool PepperWebPluginImpl::canRotateView() {
291 return instance_->CanRotateView();
294 void PepperWebPluginImpl::rotateView(RotationType type) {
295 instance_->RotateView(type);
298 bool PepperWebPluginImpl::isPlaceholder() {
302 } // namespace content