| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/plugins/ppapi/ppapi_webplugin_impl.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 #include "base/debug/crash_logging.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 12 #include "ppapi/shared_impl/var_tracker.h" | |
| 13 #include "third_party/WebKit/public/platform/WebPoint.h" | |
| 14 #include "third_party/WebKit/public/platform/WebRect.h" | |
| 15 #include "third_party/WebKit/public/platform/WebSize.h" | |
| 16 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | |
| 17 #include "third_party/WebKit/public/web/WebBindings.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/WebPluginContainer.h" | |
| 22 #include "third_party/WebKit/public/web/WebPluginParams.h" | |
| 23 #include "third_party/WebKit/public/web/WebPrintParams.h" | |
| 24 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" | |
| 25 #include "third_party/WebKit/public/web/WebView.h" | |
| 26 #include "url/gurl.h" | |
| 27 #include "webkit/plugins/ppapi/message_channel.h" | |
| 28 #include "webkit/plugins/ppapi/npobject_var.h" | |
| 29 #include "webkit/plugins/ppapi/plugin_module.h" | |
| 30 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h" | |
| 31 | |
| 32 using ppapi::NPObjectVar; | |
| 33 using WebKit::WebCanvas; | |
| 34 using WebKit::WebPlugin; | |
| 35 using WebKit::WebPluginContainer; | |
| 36 using WebKit::WebPluginParams; | |
| 37 using WebKit::WebPoint; | |
| 38 using WebKit::WebPrintParams; | |
| 39 using WebKit::WebRect; | |
| 40 using WebKit::WebSize; | |
| 41 using WebKit::WebString; | |
| 42 using WebKit::WebURL; | |
| 43 using WebKit::WebVector; | |
| 44 using WebKit::WebView; | |
| 45 | |
| 46 namespace webkit { | |
| 47 namespace ppapi { | |
| 48 | |
| 49 struct WebPluginImpl::InitData { | |
| 50 scoped_refptr<PluginModule> module; | |
| 51 base::WeakPtr<PluginDelegate> delegate; | |
| 52 base::WeakPtr<content::RenderView> render_view; | |
| 53 std::vector<std::string> arg_names; | |
| 54 std::vector<std::string> arg_values; | |
| 55 GURL url; | |
| 56 }; | |
| 57 | |
| 58 WebPluginImpl::WebPluginImpl( | |
| 59 PluginModule* plugin_module, | |
| 60 const WebPluginParams& params, | |
| 61 const base::WeakPtr<PluginDelegate>& plugin_delegate, | |
| 62 const base::WeakPtr<content::RenderView>& render_view) | |
| 63 : init_data_(new InitData()), | |
| 64 full_frame_(params.loadManually), | |
| 65 instance_object_(PP_MakeUndefined()), | |
| 66 container_(NULL) { | |
| 67 DCHECK(plugin_module); | |
| 68 init_data_->module = plugin_module; | |
| 69 init_data_->delegate = plugin_delegate; | |
| 70 init_data_->render_view = render_view; | |
| 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()); | |
| 74 } | |
| 75 init_data_->url = params.url; | |
| 76 | |
| 77 // Set subresource URL for crash reporting. | |
| 78 base::debug::SetCrashKeyValue("subresource_url", init_data_->url.spec()); | |
| 79 } | |
| 80 | |
| 81 WebPluginImpl::~WebPluginImpl() { | |
| 82 } | |
| 83 | |
| 84 WebKit::WebPluginContainer* WebPluginImpl::container() const { | |
| 85 return container_; | |
| 86 } | |
| 87 | |
| 88 bool WebPluginImpl::initialize(WebPluginContainer* container) { | |
| 89 // The plugin delegate may have gone away. | |
| 90 if (!init_data_->delegate.get()) | |
| 91 return false; | |
| 92 | |
| 93 instance_ = init_data_->module->CreateInstance( | |
| 94 init_data_->delegate.get(), init_data_->render_view.get(), container, | |
| 95 init_data_->url); | |
| 96 if (!instance_.get()) | |
| 97 return false; | |
| 98 | |
| 99 // Enable script objects for this plugin. | |
| 100 container->allowScriptObjects(); | |
| 101 | |
| 102 bool success = instance_->Initialize(init_data_->arg_names, | |
| 103 init_data_->arg_values, | |
| 104 full_frame_); | |
| 105 if (!success) { | |
| 106 instance_->Delete(); | |
| 107 instance_ = NULL; | |
| 108 | |
| 109 WebKit::WebPlugin* replacement_plugin = | |
| 110 init_data_->delegate->CreatePluginReplacement( | |
| 111 init_data_->module->path()); | |
| 112 if (!replacement_plugin || !replacement_plugin->initialize(container)) | |
| 113 return false; | |
| 114 | |
| 115 container->setPlugin(replacement_plugin); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 init_data_.reset(); | |
| 120 container_ = container; | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 void WebPluginImpl::destroy() { | |
| 125 // Tell |container_| to clear references to this plugin's script objects. | |
| 126 if (container_) | |
| 127 container_->clearScriptObjects(); | |
| 128 | |
| 129 if (instance_.get()) { | |
| 130 ::ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(instance_object_); | |
| 131 instance_object_ = PP_MakeUndefined(); | |
| 132 instance_->Delete(); | |
| 133 instance_ = NULL; | |
| 134 } | |
| 135 | |
| 136 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 137 } | |
| 138 | |
| 139 NPObject* WebPluginImpl::scriptableObject() { | |
| 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(); | |
| 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 NULL; | |
| 148 | |
| 149 scoped_refptr<NPObjectVar> object(NPObjectVar::FromPPVar(instance_object_)); | |
| 150 // If there's an InstanceObject, tell the Instance's MessageChannel to pass | |
| 151 // any non-postMessage calls to it. | |
| 152 if (object.get()) { | |
| 153 instance_->message_channel().SetPassthroughObject(object->np_object()); | |
| 154 } | |
| 155 NPObject* message_channel_np_object(instance_->message_channel().np_object()); | |
| 156 // The object is expected to be retained before it is returned. | |
| 157 WebKit::WebBindings::retainObject(message_channel_np_object); | |
| 158 return message_channel_np_object; | |
| 159 } | |
| 160 | |
| 161 NPP WebPluginImpl::pluginNPP() { | |
| 162 return instance_->instanceNPP(); | |
| 163 } | |
| 164 | |
| 165 bool WebPluginImpl::getFormValue(WebString& value) { | |
| 166 return false; | |
| 167 } | |
| 168 | |
| 169 void WebPluginImpl::paint(WebCanvas* canvas, const WebRect& rect) { | |
| 170 if (!instance_->FlashIsFullscreenOrPending()) | |
| 171 instance_->Paint(canvas, plugin_rect_, rect); | |
| 172 } | |
| 173 | |
| 174 void WebPluginImpl::updateGeometry( | |
| 175 const WebRect& window_rect, | |
| 176 const WebRect& clip_rect, | |
| 177 const WebVector<WebRect>& cut_outs_rects, | |
| 178 bool is_visible) { | |
| 179 plugin_rect_ = window_rect; | |
| 180 if (!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, cut_outs); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 void WebPluginImpl::updateFocus(bool focused) { | |
| 189 instance_->SetWebKitFocus(focused); | |
| 190 } | |
| 191 | |
| 192 void WebPluginImpl::updateVisibility(bool visible) { | |
| 193 } | |
| 194 | |
| 195 bool WebPluginImpl::acceptsInputEvents() { | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 199 bool WebPluginImpl::handleInputEvent(const WebKit::WebInputEvent& event, | |
| 200 WebKit::WebCursorInfo& cursor_info) { | |
| 201 if (instance_->FlashIsFullscreenOrPending()) | |
| 202 return false; | |
| 203 return instance_->HandleInputEvent(event, &cursor_info); | |
| 204 } | |
| 205 | |
| 206 void WebPluginImpl::didReceiveResponse( | |
| 207 const WebKit::WebURLResponse& response) { | |
| 208 DCHECK(!instance_->document_loader()); | |
| 209 instance_->HandleDocumentLoad(response); | |
| 210 } | |
| 211 | |
| 212 void WebPluginImpl::didReceiveData(const char* data, int data_length) { | |
| 213 WebKit::WebURLLoaderClient* document_loader = instance_->document_loader(); | |
| 214 if (document_loader) | |
| 215 document_loader->didReceiveData(NULL, data, data_length, 0); | |
| 216 } | |
| 217 | |
| 218 void WebPluginImpl::didFinishLoading() { | |
| 219 WebKit::WebURLLoaderClient* document_loader = instance_->document_loader(); | |
| 220 if (document_loader) | |
| 221 document_loader->didFinishLoading(NULL, 0.0); | |
| 222 } | |
| 223 | |
| 224 void WebPluginImpl::didFailLoading(const WebKit::WebURLError& error) { | |
| 225 WebKit::WebURLLoaderClient* document_loader = instance_->document_loader(); | |
| 226 if (document_loader) | |
| 227 document_loader->didFail(NULL, error); | |
| 228 } | |
| 229 | |
| 230 void WebPluginImpl::didFinishLoadingFrameRequest(const WebKit::WebURL& url, | |
| 231 void* notify_data) { | |
| 232 } | |
| 233 | |
| 234 void WebPluginImpl::didFailLoadingFrameRequest( | |
| 235 const WebKit::WebURL& url, | |
| 236 void* notify_data, | |
| 237 const WebKit::WebURLError& error) { | |
| 238 } | |
| 239 | |
| 240 bool WebPluginImpl::hasSelection() const { | |
| 241 return !selectionAsText().isEmpty(); | |
| 242 } | |
| 243 | |
| 244 WebString WebPluginImpl::selectionAsText() const { | |
| 245 return instance_->GetSelectedText(false); | |
| 246 } | |
| 247 | |
| 248 WebString WebPluginImpl::selectionAsMarkup() const { | |
| 249 return instance_->GetSelectedText(true); | |
| 250 } | |
| 251 | |
| 252 WebURL WebPluginImpl::linkAtPosition(const WebPoint& position) const { | |
| 253 return GURL(instance_->GetLinkAtPosition(position)); | |
| 254 } | |
| 255 | |
| 256 void WebPluginImpl::setZoomLevel(double level, bool text_only) { | |
| 257 instance_->Zoom(WebView::zoomLevelToZoomFactor(level), text_only); | |
| 258 } | |
| 259 | |
| 260 bool WebPluginImpl::startFind(const WebKit::WebString& search_text, | |
| 261 bool case_sensitive, | |
| 262 int identifier) { | |
| 263 return instance_->StartFind(search_text, case_sensitive, identifier); | |
| 264 } | |
| 265 | |
| 266 void WebPluginImpl::selectFindResult(bool forward) { | |
| 267 instance_->SelectFindResult(forward); | |
| 268 } | |
| 269 | |
| 270 void WebPluginImpl::stopFind() { | |
| 271 instance_->StopFind(); | |
| 272 } | |
| 273 | |
| 274 bool WebPluginImpl::supportsPaginatedPrint() { | |
| 275 return instance_->SupportsPrintInterface(); | |
| 276 } | |
| 277 | |
| 278 bool WebPluginImpl::isPrintScalingDisabled() { | |
| 279 return instance_->IsPrintScalingDisabled(); | |
| 280 } | |
| 281 | |
| 282 int WebPluginImpl::printBegin(const WebPrintParams& print_params) { | |
| 283 return instance_->PrintBegin(print_params); | |
| 284 } | |
| 285 | |
| 286 bool WebPluginImpl::printPage(int page_number, | |
| 287 WebKit::WebCanvas* canvas) { | |
| 288 return instance_->PrintPage(page_number, canvas); | |
| 289 } | |
| 290 | |
| 291 void WebPluginImpl::printEnd() { | |
| 292 return instance_->PrintEnd(); | |
| 293 } | |
| 294 | |
| 295 bool WebPluginImpl::canRotateView() { | |
| 296 return instance_->CanRotateView(); | |
| 297 } | |
| 298 | |
| 299 void WebPluginImpl::rotateView(RotationType type) { | |
| 300 instance_->RotateView(type); | |
| 301 } | |
| 302 | |
| 303 bool WebPluginImpl::isPlaceholder() { | |
| 304 return false; | |
| 305 } | |
| 306 | |
| 307 } // namespace ppapi | |
| 308 } // namespace webkit | |
| OLD | NEW |