[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / pepper / pepper_webplugin_impl.cc
blob3ed39200d3e695c1ef8e71e6628210b558bef06b
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());
82 PepperWebPluginImpl::~PepperWebPluginImpl() {}
84 blink::WebPluginContainer* PepperWebPluginImpl::container() const {
85 return container_;
88 bool PepperWebPluginImpl::initialize(WebPluginContainer* container) {
89 // The plugin delegate may have gone away.
90 instance_ = init_data_->module->CreateInstance(
91 init_data_->render_frame, container, init_data_->url);
92 if (!instance_.get())
93 return false;
95 // Enable script objects for this plugin.
96 container->allowScriptObjects();
98 bool success =
99 instance_->Initialize(init_data_->arg_names, init_data_->arg_values,
100 full_frame_, throttler_.Pass());
101 if (!success) {
102 instance_->Delete();
103 instance_ = NULL;
105 blink::WebPlugin* replacement_plugin =
106 GetContentClient()->renderer()->CreatePluginReplacement(
107 init_data_->render_frame, init_data_->module->path());
108 if (!replacement_plugin || !replacement_plugin->initialize(container))
109 return false;
111 container->setPlugin(replacement_plugin);
112 return true;
115 init_data_.reset();
116 container_ = container;
117 return true;
120 void PepperWebPluginImpl::destroy() {
121 // Tell |container_| to clear references to this plugin's script objects.
122 if (container_)
123 container_->clearScriptObjects();
125 if (instance_.get()) {
126 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(instance_object_);
127 instance_object_ = PP_MakeUndefined();
128 instance_->Delete();
129 instance_ = NULL;
132 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
135 v8::Local<v8::Object> PepperWebPluginImpl::v8ScriptableObject(
136 v8::Isolate* isolate) {
137 // Call through the plugin to get its instance object. The plugin should pass
138 // us a reference which we release in destroy().
139 if (instance_object_.type == PP_VARTYPE_UNDEFINED)
140 instance_object_ = instance_->GetInstanceObject(isolate);
141 // GetInstanceObject talked to the plugin which may have removed the instance
142 // from the DOM, in which case instance_ would be NULL now.
143 if (!instance_.get())
144 return v8::Local<v8::Object>();
146 scoped_refptr<V8ObjectVar> object_var(
147 V8ObjectVar::FromPPVar(instance_object_));
148 // If there's an InstanceObject, tell the Instance's MessageChannel to pass
149 // any non-postMessage calls to it.
150 if (object_var.get()) {
151 MessageChannel* message_channel = instance_->message_channel();
152 if (message_channel)
153 message_channel->SetPassthroughObject(object_var->GetHandle());
156 v8::Handle<v8::Object> result = instance_->GetMessageChannelObject();
157 return result;
160 bool PepperWebPluginImpl::getFormValue(WebString& value) { return false; }
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,
171 bool is_visible) {
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 blink::WebFocusType focus_type) {
183 instance_->SetWebKitFocus(focused);
186 void PepperWebPluginImpl::updateVisibility(bool visible) {}
188 bool PepperWebPluginImpl::acceptsInputEvents() { return true; }
190 bool PepperWebPluginImpl::handleInputEvent(const blink::WebInputEvent& event,
191 blink::WebCursorInfo& cursor_info) {
192 if (instance_->FlashIsFullscreenOrPending())
193 return false;
194 return instance_->HandleInputEvent(event, &cursor_info);
197 void PepperWebPluginImpl::didReceiveResponse(
198 const blink::WebURLResponse& response) {
199 DCHECK(!instance_->document_loader());
200 instance_->HandleDocumentLoad(response);
203 void PepperWebPluginImpl::didReceiveData(const char* data, int data_length) {
204 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
205 if (document_loader)
206 document_loader->didReceiveData(NULL, data, data_length, 0);
209 void PepperWebPluginImpl::didFinishLoading() {
210 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
211 if (document_loader)
212 document_loader->didFinishLoading(
213 NULL, 0.0, blink::WebURLLoaderClient::kUnknownEncodedDataLength);
216 void PepperWebPluginImpl::didFailLoading(const blink::WebURLError& error) {
217 blink::WebURLLoaderClient* document_loader = instance_->document_loader();
218 if (document_loader)
219 document_loader->didFail(NULL, error);
222 void PepperWebPluginImpl::didFinishLoadingFrameRequest(const blink::WebURL& url,
223 void* notify_data) {}
225 void PepperWebPluginImpl::didFailLoadingFrameRequest(
226 const blink::WebURL& url,
227 void* notify_data,
228 const blink::WebURLError& error) {}
230 bool PepperWebPluginImpl::hasSelection() const {
231 return !selectionAsText().isEmpty();
234 WebString PepperWebPluginImpl::selectionAsText() const {
235 return instance_->GetSelectedText(false);
238 WebString PepperWebPluginImpl::selectionAsMarkup() const {
239 return instance_->GetSelectedText(true);
242 WebURL PepperWebPluginImpl::linkAtPosition(const WebPoint& position) const {
243 return GURL(instance_->GetLinkAtPosition(position));
246 void PepperWebPluginImpl::setZoomLevel(double level, bool text_only) {
247 instance_->Zoom(content::ZoomLevelToZoomFactor(level), text_only);
250 bool PepperWebPluginImpl::startFind(const blink::WebString& search_text,
251 bool case_sensitive,
252 int identifier) {
253 return instance_->StartFind(search_text, case_sensitive, identifier);
256 void PepperWebPluginImpl::selectFindResult(bool forward) {
257 instance_->SelectFindResult(forward);
260 void PepperWebPluginImpl::stopFind() { instance_->StopFind(); }
262 bool PepperWebPluginImpl::supportsPaginatedPrint() {
263 return instance_->SupportsPrintInterface();
266 bool PepperWebPluginImpl::isPrintScalingDisabled() {
267 return instance_->IsPrintScalingDisabled();
270 int PepperWebPluginImpl::printBegin(const WebPrintParams& print_params) {
271 return instance_->PrintBegin(print_params);
274 bool PepperWebPluginImpl::printPage(int page_number, blink::WebCanvas* canvas) {
275 return instance_->PrintPage(page_number, canvas);
278 void PepperWebPluginImpl::printEnd() { return instance_->PrintEnd(); }
280 bool PepperWebPluginImpl::getPrintPresetOptionsFromDocument(
281 blink::WebPrintPresetOptions* preset_options) {
282 return instance_->GetPrintPresetOptionsFromDocument(preset_options);
285 bool PepperWebPluginImpl::canRotateView() { return instance_->CanRotateView(); }
287 void PepperWebPluginImpl::rotateView(RotationType type) {
288 instance_->RotateView(type);
291 bool PepperWebPluginImpl::isPlaceholder() { return false; }
293 } // namespace content