| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/plugin/chrome_plugin_host.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/process_util.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "base/string_split.h" | |
| 14 #include "chrome/common/chrome_constants.h" | |
| 15 #include "chrome/common/chrome_plugin_lib.h" | |
| 16 #include "chrome/common/chrome_plugin_util.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/plugin_messages.h" | |
| 19 #include "chrome/plugin/plugin_thread.h" | |
| 20 #include "chrome/plugin/webplugin_proxy.h" | |
| 21 #include "content/common/child_process.h" | |
| 22 #include "content/common/resource_dispatcher.h" | |
| 23 #include "net/base/data_url.h" | |
| 24 #include "net/base/io_buffer.h" | |
| 25 #include "net/base/upload_data.h" | |
| 26 #include "net/http/http_response_headers.h" | |
| 27 #include "webkit/appcache/appcache_interfaces.h" | |
| 28 #include "webkit/plugins/npapi/plugin_instance.h" | |
| 29 #include "webkit/glue/resource_loader_bridge.h" | |
| 30 #include "webkit/glue/resource_type.h" | |
| 31 #include "webkit/glue/webkit_glue.h" | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 using webkit_glue::ResourceLoaderBridge; | |
| 36 using webkit_glue::ResourceResponseInfo; | |
| 37 | |
| 38 static MessageLoop* g_plugin_thread_message_loop; | |
| 39 | |
| 40 // This class manages a network request made by the plugin, handling the | |
| 41 // data as it comes in from the ResourceLoaderBridge and is requested by the | |
| 42 // plugin. | |
| 43 // NOTE: All methods must be called on the Plugin thread. | |
| 44 class PluginRequestHandlerProxy | |
| 45 : public PluginHelper, public ResourceLoaderBridge::Peer { | |
| 46 public: | |
| 47 static PluginRequestHandlerProxy* FromCPRequest(CPRequest* request) { | |
| 48 return ScopableCPRequest::GetData<PluginRequestHandlerProxy*>(request); | |
| 49 } | |
| 50 | |
| 51 PluginRequestHandlerProxy(ChromePluginLib* plugin, | |
| 52 ScopableCPRequest* cprequest) | |
| 53 : PluginHelper(plugin), | |
| 54 cprequest_(cprequest), | |
| 55 sync_(false), | |
| 56 response_data_offset_(0), | |
| 57 completed_(false), | |
| 58 read_buffer_(NULL), | |
| 59 read_buffer_size_(0) { | |
| 60 load_flags_ = PluginResponseUtils::CPLoadFlagsToNetFlags(0); | |
| 61 cprequest_->data = this; // see FromCPRequest(). | |
| 62 } | |
| 63 | |
| 64 ~PluginRequestHandlerProxy() { | |
| 65 if (bridge_.get() && !completed_) { | |
| 66 bridge_->Cancel(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 // ResourceLoaderBridge::Peer | |
| 71 virtual void OnUploadProgress(uint64 position, uint64 size) { | |
| 72 CPRR_UploadProgressFunc upload_progress = | |
| 73 plugin_->functions().response_funcs->upload_progress; | |
| 74 if (upload_progress) | |
| 75 upload_progress(cprequest_.get(), position, size); | |
| 76 } | |
| 77 | |
| 78 virtual bool OnReceivedRedirect( | |
| 79 const GURL& new_url, | |
| 80 const ResourceResponseInfo& info, | |
| 81 bool* has_new_first_party_for_cookies, | |
| 82 GURL* new_first_party_for_cookies) { | |
| 83 plugin_->functions().response_funcs->received_redirect( | |
| 84 cprequest_.get(), new_url.spec().c_str()); | |
| 85 // TODO(wtc): should we return a new first party for cookies URL? | |
| 86 *has_new_first_party_for_cookies = false; | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 virtual void OnReceivedResponse(const ResourceResponseInfo& info) { | |
| 91 response_headers_ = info.headers; | |
| 92 plugin_->functions().response_funcs->start_completed( | |
| 93 cprequest_.get(), CPERR_SUCCESS); | |
| 94 } | |
| 95 | |
| 96 virtual void OnDownloadedData(int len) { | |
| 97 } | |
| 98 | |
| 99 virtual void OnReceivedData(const char* data, int len) { | |
| 100 response_data_.append(data, len); | |
| 101 if (read_buffer_) { | |
| 102 // If we had an asynchronous operation pending, read into that buffer | |
| 103 // and inform the plugin. | |
| 104 int rv = Read(read_buffer_, read_buffer_size_); | |
| 105 DCHECK(rv != CPERR_IO_PENDING); | |
| 106 read_buffer_ = NULL; | |
| 107 plugin_->functions().response_funcs->read_completed( | |
| 108 cprequest_.get(), rv); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | |
| 113 const std::string& security_info, | |
| 114 const base::Time& completion_time) { | |
| 115 completed_ = true; | |
| 116 | |
| 117 if (!status.is_success()) { | |
| 118 // TODO(mpcomplete): better error codes | |
| 119 // Inform the plugin, calling the right function depending on whether | |
| 120 // we got the start_completed event or not. | |
| 121 if (response_headers_) { | |
| 122 plugin_->functions().response_funcs->start_completed( | |
| 123 cprequest_.get(), CPERR_FAILURE); | |
| 124 } else { | |
| 125 plugin_->functions().response_funcs->read_completed( | |
| 126 cprequest_.get(), CPERR_FAILURE); | |
| 127 } | |
| 128 } else if (read_buffer_) { | |
| 129 // The plugin was waiting for more data. Inform him we're done. | |
| 130 read_buffer_ = NULL; | |
| 131 plugin_->functions().response_funcs->read_completed( | |
| 132 cprequest_.get(), CPERR_SUCCESS); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void set_extra_headers(const std::string& headers) { | |
| 137 extra_headers_ = headers; | |
| 138 } | |
| 139 void set_load_flags(uint32 flags) { | |
| 140 load_flags_ = flags; | |
| 141 } | |
| 142 void set_sync(bool sync) { | |
| 143 sync_ = sync; | |
| 144 } | |
| 145 void AppendDataToUpload(const char* bytes, int bytes_len) { | |
| 146 upload_content_.push_back(net::UploadData::Element()); | |
| 147 upload_content_.back().SetToBytes(bytes, bytes_len); | |
| 148 } | |
| 149 | |
| 150 void AppendFileToUpload(const FilePath &filepath) { | |
| 151 AppendFileRangeToUpload(filepath, 0, kuint64max); | |
| 152 } | |
| 153 | |
| 154 void AppendFileRangeToUpload(const FilePath &filepath, | |
| 155 uint64 offset, uint64 length) { | |
| 156 upload_content_.push_back(net::UploadData::Element()); | |
| 157 upload_content_.back().SetToFilePathRange(filepath, offset, length, | |
| 158 base::Time()); | |
| 159 } | |
| 160 | |
| 161 CPError Start(int renderer_id, int render_view_id) { | |
| 162 webkit_glue::ResourceLoaderBridge::RequestInfo request_info; | |
| 163 request_info.method = cprequest_->method; | |
| 164 request_info.url = GURL(cprequest_->url); | |
| 165 request_info.first_party_for_cookies = | |
| 166 GURL(cprequest_->url); // TODO(jackson): policy url? | |
| 167 request_info.referrer = GURL(); // TODO(mpcomplete): referrer? | |
| 168 request_info.headers = extra_headers_; | |
| 169 request_info.load_flags = load_flags_; | |
| 170 request_info.requestor_pid = base::GetCurrentProcId(); | |
| 171 request_info.request_type = ResourceType::OBJECT; | |
| 172 request_info.request_context = cprequest_->context; | |
| 173 request_info.appcache_host_id = appcache::kNoHostId; | |
| 174 request_info.routing_id = MSG_ROUTING_CONTROL; | |
| 175 bridge_.reset( | |
| 176 PluginThread::current()->resource_dispatcher()->CreateBridge( | |
| 177 request_info, | |
| 178 renderer_id, | |
| 179 render_view_id)); | |
| 180 if (!bridge_.get()) | |
| 181 return CPERR_FAILURE; | |
| 182 | |
| 183 for (size_t i = 0; i < upload_content_.size(); ++i) { | |
| 184 switch (upload_content_[i].type()) { | |
| 185 case net::UploadData::TYPE_BYTES: { | |
| 186 const std::vector<char>& bytes = upload_content_[i].bytes(); | |
| 187 bridge_->AppendDataToUpload(&bytes[0], | |
| 188 static_cast<int>(bytes.size())); | |
| 189 break; | |
| 190 } | |
| 191 case net::UploadData::TYPE_FILE: { | |
| 192 bridge_->AppendFileRangeToUpload( | |
| 193 upload_content_[i].file_path(), | |
| 194 upload_content_[i].file_range_offset(), | |
| 195 upload_content_[i].file_range_length(), | |
| 196 upload_content_[i].expected_file_modification_time()); | |
| 197 break; | |
| 198 } | |
| 199 default: { | |
| 200 NOTREACHED() << "Unknown UploadData::Element type"; | |
| 201 } | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 if (sync_) { | |
| 206 ResourceLoaderBridge::SyncLoadResponse response; | |
| 207 bridge_->SyncLoad(&response); | |
| 208 response_headers_ = response.headers; | |
| 209 response_data_ = response.data; | |
| 210 completed_ = true; | |
| 211 return response.status.is_success() ? CPERR_SUCCESS : CPERR_FAILURE; | |
| 212 } else { | |
| 213 if (!bridge_->Start(this)) { | |
| 214 bridge_.reset(); | |
| 215 return CPERR_FAILURE; | |
| 216 } | |
| 217 return CPERR_IO_PENDING; | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 int GetResponseInfo(CPResponseInfoType type, void* buf, uint32 buf_size) { | |
| 222 return PluginResponseUtils::GetResponseInfo( | |
| 223 response_headers_, type, buf, buf_size); | |
| 224 } | |
| 225 | |
| 226 int Read(void* buf, uint32 buf_size) { | |
| 227 uint32 avail = | |
| 228 static_cast<uint32>(response_data_.size()) - response_data_offset_; | |
| 229 uint32 count = buf_size; | |
| 230 if (count > avail) | |
| 231 count = avail; | |
| 232 | |
| 233 if (count) { | |
| 234 // Data is ready now. | |
| 235 memcpy(buf, &response_data_[0] + response_data_offset_, count); | |
| 236 response_data_offset_ += count; | |
| 237 } else if (!completed_) { | |
| 238 read_buffer_ = buf; | |
| 239 read_buffer_size_ = buf_size; | |
| 240 DCHECK(!sync_); | |
| 241 return CPERR_IO_PENDING; | |
| 242 } | |
| 243 | |
| 244 if (response_data_.size() == response_data_offset_) { | |
| 245 // Simple optimization for large requests. Generally the consumer will | |
| 246 // read the data faster than it comes in, so we can clear our buffer | |
| 247 // any time it has all been read. | |
| 248 response_data_.clear(); | |
| 249 response_data_offset_ = 0; | |
| 250 } | |
| 251 | |
| 252 read_buffer_ = NULL; | |
| 253 return count; | |
| 254 } | |
| 255 | |
| 256 private: | |
| 257 scoped_ptr<ScopableCPRequest> cprequest_; | |
| 258 scoped_ptr<ResourceLoaderBridge> bridge_; | |
| 259 std::vector<net::UploadData::Element> upload_content_; | |
| 260 std::string extra_headers_; | |
| 261 uint32 load_flags_; | |
| 262 bool sync_; | |
| 263 | |
| 264 scoped_refptr<net::HttpResponseHeaders> response_headers_; | |
| 265 std::string response_data_; | |
| 266 size_t response_data_offset_; | |
| 267 bool completed_; | |
| 268 void* read_buffer_; | |
| 269 uint32 read_buffer_size_; | |
| 270 }; | |
| 271 | |
| 272 // | |
| 273 // Generic functions | |
| 274 // | |
| 275 | |
| 276 void STDCALL CPB_SetKeepProcessAlive(CPID id, CPBool keep_alive) { | |
| 277 CHECK(ChromePluginLib::IsPluginThread()); | |
| 278 static bool g_keep_process_alive = false; | |
| 279 bool desired_value = keep_alive ? true : false; // smash to bool | |
| 280 if (desired_value != g_keep_process_alive) { | |
| 281 g_keep_process_alive = desired_value; | |
| 282 if (g_keep_process_alive) | |
| 283 ChildProcess::current()->AddRefProcess(); | |
| 284 else | |
| 285 ChildProcess::current()->ReleaseProcess(); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 CPError STDCALL CPB_GetCookies(CPID id, CPBrowsingContext context, | |
| 290 const char* url, char** cookies) { | |
| 291 CHECK(ChromePluginLib::IsPluginThread()); | |
| 292 std::string cookies_str; | |
| 293 | |
| 294 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext(context); | |
| 295 // There are two contexts in which we can be asked for cookies: | |
| 296 // 1. From a script context. webplugin will be non-NULL. | |
| 297 // 2. From a global browser context (think: Gears UpdateTask). webplugin will | |
| 298 // be NULL and context will (loosely) represent a browser Profile. | |
| 299 // In case 1, we *must* route through the renderer process, otherwise we race | |
| 300 // with renderer script that may have set cookies. In case 2, we are running | |
| 301 // out-of-band with script, so we don't need to stay in sync with any | |
| 302 // particular renderer. | |
| 303 // See http://b/issue?id=1487502. | |
| 304 if (webplugin) { | |
| 305 cookies_str = webplugin->GetCookies(GURL(url), GURL(url)); | |
| 306 } else { | |
| 307 PluginThread::current()->Send( | |
| 308 new PluginProcessHostMsg_GetCookies(context, GURL(url), &cookies_str)); | |
| 309 } | |
| 310 | |
| 311 *cookies = CPB_StringDup(CPB_Alloc, cookies_str); | |
| 312 return CPERR_SUCCESS; | |
| 313 } | |
| 314 | |
| 315 CPError STDCALL CPB_ShowHtmlDialogModal( | |
| 316 CPID id, CPBrowsingContext context, const char* url, int width, int height, | |
| 317 const char* json_arguments, char** json_retval) { | |
| 318 CHECK(ChromePluginLib::IsPluginThread()); | |
| 319 | |
| 320 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext(context); | |
| 321 if (!webplugin) | |
| 322 return CPERR_INVALID_PARAMETER; | |
| 323 | |
| 324 std::string retval_str; | |
| 325 webplugin->ShowModalHTMLDialog( | |
| 326 GURL(url), width, height, json_arguments, &retval_str); | |
| 327 *json_retval = CPB_StringDup(CPB_Alloc, retval_str); | |
| 328 return CPERR_SUCCESS; | |
| 329 } | |
| 330 | |
| 331 CPError STDCALL CPB_ShowHtmlDialog( | |
| 332 CPID id, CPBrowsingContext context, const char* url, int width, int height, | |
| 333 const char* json_arguments, void* plugin_context) { | |
| 334 // TODO(mpcomplete): support non-modal dialogs. | |
| 335 return CPERR_FAILURE; | |
| 336 } | |
| 337 | |
| 338 CPError STDCALL CPB_GetDragData( | |
| 339 CPID id, CPBrowsingContext context, struct NPObject* event, bool add_data, | |
| 340 int32 *identity, int32 *event_id, char **drag_type, char **drag_data) { | |
| 341 CHECK(ChromePluginLib::IsPluginThread()); | |
| 342 | |
| 343 *identity = *event_id = 0; | |
| 344 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext(context); | |
| 345 if (!event || !webplugin) | |
| 346 return CPERR_INVALID_PARAMETER; | |
| 347 | |
| 348 std::string type_str, data_str; | |
| 349 if (!webplugin->GetDragData(event, add_data, | |
| 350 identity, event_id, &type_str, &data_str)) { | |
| 351 return CPERR_FAILURE; | |
| 352 } | |
| 353 | |
| 354 if (add_data) | |
| 355 *drag_data = CPB_StringDup(CPB_Alloc, data_str); | |
| 356 *drag_type = CPB_StringDup(CPB_Alloc, type_str); | |
| 357 return CPERR_SUCCESS; | |
| 358 } | |
| 359 | |
| 360 CPError STDCALL CPB_SetDropEffect( | |
| 361 CPID id, CPBrowsingContext context, struct NPObject* event, int effect) { | |
| 362 CHECK(ChromePluginLib::IsPluginThread()); | |
| 363 | |
| 364 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext(context); | |
| 365 if (!event || !webplugin) | |
| 366 return CPERR_INVALID_PARAMETER; | |
| 367 | |
| 368 if (webplugin->SetDropEffect(event, effect)) | |
| 369 return CPERR_SUCCESS; | |
| 370 return CPERR_FAILURE; | |
| 371 } | |
| 372 | |
| 373 CPError STDCALL CPB_AllowFileDrop( | |
| 374 CPID id, CPBrowsingContext context, const char* file_drag_data) { | |
| 375 CHECK(ChromePluginLib::IsPluginThread()); | |
| 376 | |
| 377 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext(context); | |
| 378 if (!webplugin || !file_drag_data) | |
| 379 return CPERR_INVALID_PARAMETER; | |
| 380 | |
| 381 const int renderer = webplugin->GetRendererId(); | |
| 382 if (renderer == -1) | |
| 383 return CPERR_FAILURE; | |
| 384 | |
| 385 static const char kDelimiter('\b'); | |
| 386 std::vector<std::string> files; | |
| 387 base::SplitStringDontTrim(file_drag_data, kDelimiter, &files); | |
| 388 | |
| 389 bool allowed = false; | |
| 390 if (!PluginThread::current()->Send( | |
| 391 new PluginProcessHostMsg_AccessFiles(renderer, files, &allowed))) { | |
| 392 return CPERR_FAILURE; | |
| 393 } | |
| 394 | |
| 395 if (allowed) | |
| 396 return CPERR_SUCCESS; | |
| 397 return CPERR_FAILURE; | |
| 398 } | |
| 399 | |
| 400 CPError STDCALL CPB_GetCommandLineArguments( | |
| 401 CPID id, CPBrowsingContext context, const char* url, char** arguments) { | |
| 402 CHECK(ChromePluginLib::IsPluginThread()); | |
| 403 std::string arguments_str; | |
| 404 CPError rv = CPB_GetCommandLineArgumentsCommon(url, &arguments_str); | |
| 405 if (rv == CPERR_SUCCESS) | |
| 406 *arguments = CPB_StringDup(CPB_Alloc, arguments_str); | |
| 407 return rv; | |
| 408 } | |
| 409 | |
| 410 CPBrowsingContext STDCALL CPB_GetBrowsingContextFromNPP(NPP npp) { | |
| 411 if (!npp) | |
| 412 return CPERR_INVALID_PARAMETER; | |
| 413 | |
| 414 webkit::npapi::PluginInstance* instance = | |
| 415 static_cast<webkit::npapi::PluginInstance *>(npp->ndata); | |
| 416 WebPluginProxy* webplugin = | |
| 417 static_cast<WebPluginProxy*>(instance->webplugin()); | |
| 418 | |
| 419 return webplugin->GetCPBrowsingContext(); | |
| 420 } | |
| 421 | |
| 422 int STDCALL CPB_GetBrowsingContextInfo( | |
| 423 CPID id, CPBrowsingContext context, CPBrowsingContextInfoType type, | |
| 424 void* buf, uint32 buf_size) { | |
| 425 CHECK(ChromePluginLib::IsPluginThread()); | |
| 426 | |
| 427 #if defined(OS_WIN) | |
| 428 switch (type) { | |
| 429 case CPBROWSINGCONTEXT_DATA_DIR_PTR: { | |
| 430 if (buf_size < sizeof(char*)) | |
| 431 return sizeof(char*); | |
| 432 | |
| 433 FilePath path = CommandLine::ForCurrentProcess()-> | |
| 434 GetSwitchValuePath(switches::kPluginDataDir); | |
| 435 DCHECK(!path.empty()); | |
| 436 std::string retval = WideToUTF8( | |
| 437 path.Append(chrome::kChromePluginDataDirname).value()); | |
| 438 *static_cast<char**>(buf) = CPB_StringDup(CPB_Alloc, retval); | |
| 439 | |
| 440 return CPERR_SUCCESS; | |
| 441 } | |
| 442 case CPBROWSINGCONTEXT_UI_LOCALE_PTR: { | |
| 443 if (buf_size < sizeof(char*)) | |
| 444 return sizeof(char*); | |
| 445 | |
| 446 std::string retval = webkit_glue::GetWebKitLocale(); | |
| 447 *static_cast<char**>(buf) = CPB_StringDup(CPB_Alloc, retval); | |
| 448 return CPERR_SUCCESS; | |
| 449 } | |
| 450 } | |
| 451 #else | |
| 452 // TODO(aa): this code is only used by Gears, which we are removing. | |
| 453 NOTREACHED(); | |
| 454 #endif | |
| 455 | |
| 456 return CPERR_FAILURE; | |
| 457 } | |
| 458 | |
| 459 CPError STDCALL CPB_AddUICommand(CPID id, int command) { | |
| 460 // Not implemented in the plugin process | |
| 461 return CPERR_FAILURE; | |
| 462 } | |
| 463 | |
| 464 CPError STDCALL CPB_HandleCommand( | |
| 465 CPID id, CPBrowsingContext context, int command, void *data) { | |
| 466 // Not implemented in the plugin process | |
| 467 return CPERR_FAILURE; | |
| 468 } | |
| 469 | |
| 470 // | |
| 471 // Functions related to network interception | |
| 472 // | |
| 473 | |
| 474 void STDCALL CPB_EnableRequestIntercept( | |
| 475 CPID id, const char** schemes, uint32 num_schemes) { | |
| 476 // We ignore requests by the plugin to intercept from this process. That's | |
| 477 // handled in the browser process. | |
| 478 } | |
| 479 | |
| 480 void STDCALL CPRR_ReceivedRedirect(CPRequest* request, const char* new_url) { | |
| 481 NOTREACHED() << "Network interception should not happen in plugin process."; | |
| 482 } | |
| 483 | |
| 484 void STDCALL CPRR_StartCompleted(CPRequest* request, CPError result) { | |
| 485 NOTREACHED() << "Network interception should not happen in plugin process."; | |
| 486 } | |
| 487 | |
| 488 void STDCALL CPRR_ReadCompleted(CPRequest* request, int bytes_read) { | |
| 489 NOTREACHED() << "Network interception should not happen in plugin process."; | |
| 490 } | |
| 491 | |
| 492 void STDCALL CPRR_UploadProgress(CPRequest* request, uint64 pos, uint64 size) { | |
| 493 NOTREACHED() << "Network interception should not happen in plugin process."; | |
| 494 } | |
| 495 | |
| 496 // | |
| 497 // Functions related to serving network requests to the plugin | |
| 498 // | |
| 499 | |
| 500 CPError STDCALL CPB_CreateRequest(CPID id, CPBrowsingContext context, | |
| 501 const char* method, const char* url, | |
| 502 CPRequest** request) { | |
| 503 CHECK(ChromePluginLib::IsPluginThread()); | |
| 504 ChromePluginLib* plugin = ChromePluginLib::FromCPID(id); | |
| 505 CHECK(plugin); | |
| 506 | |
| 507 ScopableCPRequest* cprequest = new ScopableCPRequest(url, method, context); | |
| 508 new PluginRequestHandlerProxy(plugin, cprequest); | |
| 509 | |
| 510 *request = cprequest; | |
| 511 return CPERR_SUCCESS; | |
| 512 } | |
| 513 | |
| 514 CPError STDCALL CPR_StartRequest(CPRequest* request) { | |
| 515 CHECK(ChromePluginLib::IsPluginThread()); | |
| 516 PluginRequestHandlerProxy* handler = | |
| 517 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 518 CHECK(handler); | |
| 519 | |
| 520 int renderer_id = -1; | |
| 521 int render_view_id = -1; | |
| 522 | |
| 523 WebPluginProxy* webplugin = WebPluginProxy::FromCPBrowsingContext( | |
| 524 request->context); | |
| 525 if (webplugin) { | |
| 526 renderer_id = webplugin->GetRendererId(); | |
| 527 if (renderer_id == -1) | |
| 528 return CPERR_FAILURE; | |
| 529 | |
| 530 render_view_id = webplugin->host_render_view_routing_id(); | |
| 531 if (render_view_id == -1) | |
| 532 return CPERR_FAILURE; | |
| 533 } | |
| 534 | |
| 535 return handler->Start(renderer_id, render_view_id); | |
| 536 } | |
| 537 | |
| 538 void STDCALL CPR_EndRequest(CPRequest* request, CPError reason) { | |
| 539 CHECK(ChromePluginLib::IsPluginThread()); | |
| 540 PluginRequestHandlerProxy* handler = | |
| 541 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 542 delete handler; | |
| 543 } | |
| 544 | |
| 545 void STDCALL CPR_SetExtraRequestHeaders(CPRequest* request, | |
| 546 const char* headers) { | |
| 547 CHECK(ChromePluginLib::IsPluginThread()); | |
| 548 PluginRequestHandlerProxy* handler = | |
| 549 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 550 CHECK(handler); | |
| 551 handler->set_extra_headers(headers); | |
| 552 } | |
| 553 | |
| 554 void STDCALL CPR_SetRequestLoadFlags(CPRequest* request, uint32 flags) { | |
| 555 CHECK(ChromePluginLib::IsPluginThread()); | |
| 556 PluginRequestHandlerProxy* handler = | |
| 557 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 558 CHECK(handler); | |
| 559 | |
| 560 if (flags & CPREQUESTLOAD_SYNCHRONOUS) { | |
| 561 handler->set_sync(true); | |
| 562 } | |
| 563 | |
| 564 uint32 net_flags = PluginResponseUtils::CPLoadFlagsToNetFlags(flags); | |
| 565 handler->set_load_flags(net_flags); | |
| 566 } | |
| 567 | |
| 568 void STDCALL CPR_AppendDataToUpload(CPRequest* request, const char* bytes, | |
| 569 int bytes_len) { | |
| 570 CHECK(ChromePluginLib::IsPluginThread()); | |
| 571 PluginRequestHandlerProxy* handler = | |
| 572 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 573 CHECK(handler); | |
| 574 handler->AppendDataToUpload(bytes, bytes_len); | |
| 575 } | |
| 576 | |
| 577 CPError STDCALL CPR_AppendFileToUpload(CPRequest* request, const char* filepath, | |
| 578 uint64 offset, uint64 length) { | |
| 579 CHECK(ChromePluginLib::IsPluginThread()); | |
| 580 PluginRequestHandlerProxy* handler = | |
| 581 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 582 CHECK(handler); | |
| 583 | |
| 584 if (!length) length = kuint64max; | |
| 585 std::wstring wfilepath(UTF8ToWide(filepath)); | |
| 586 handler->AppendFileRangeToUpload(FilePath::FromWStringHack(wfilepath), offset, | |
| 587 length); | |
| 588 return CPERR_SUCCESS; | |
| 589 } | |
| 590 | |
| 591 int STDCALL CPR_GetResponseInfo(CPRequest* request, CPResponseInfoType type, | |
| 592 void* buf, uint32 buf_size) { | |
| 593 CHECK(ChromePluginLib::IsPluginThread()); | |
| 594 PluginRequestHandlerProxy* handler = | |
| 595 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 596 CHECK(handler); | |
| 597 return handler->GetResponseInfo(type, buf, buf_size); | |
| 598 } | |
| 599 | |
| 600 int STDCALL CPR_Read(CPRequest* request, void* buf, uint32 buf_size) { | |
| 601 CHECK(ChromePluginLib::IsPluginThread()); | |
| 602 PluginRequestHandlerProxy* handler = | |
| 603 PluginRequestHandlerProxy::FromCPRequest(request); | |
| 604 CHECK(handler); | |
| 605 return handler->Read(buf, buf_size); | |
| 606 } | |
| 607 | |
| 608 | |
| 609 CPBool STDCALL CPB_IsPluginProcessRunning(CPID id) { | |
| 610 CHECK(ChromePluginLib::IsPluginThread()); | |
| 611 return true; | |
| 612 } | |
| 613 | |
| 614 CPProcessType STDCALL CPB_GetProcessType(CPID id) { | |
| 615 CHECK(ChromePluginLib::IsPluginThread()); | |
| 616 return CP_PROCESS_PLUGIN; | |
| 617 } | |
| 618 | |
| 619 CPError STDCALL CPB_SendMessage(CPID id, const void *data, uint32 data_len) { | |
| 620 CHECK(ChromePluginLib::IsPluginThread()); | |
| 621 const uint8* data_ptr = static_cast<const uint8*>(data); | |
| 622 std::vector<uint8> v(data_ptr, data_ptr + data_len); | |
| 623 if (!PluginThread::current()->Send(new PluginProcessHostMsg_PluginMessage(v))) | |
| 624 return CPERR_FAILURE; | |
| 625 | |
| 626 return CPERR_SUCCESS; | |
| 627 } | |
| 628 | |
| 629 CPError STDCALL CPB_SendSyncMessage(CPID id, const void *data, uint32 data_len, | |
| 630 void **retval, uint32 *retval_len) { | |
| 631 CHECK(ChromePluginLib::IsPluginThread()); | |
| 632 const uint8* data_ptr = static_cast<const uint8*>(data); | |
| 633 std::vector<uint8> v(data_ptr, data_ptr + data_len); | |
| 634 std::vector<uint8> r; | |
| 635 if (!PluginThread::current()->Send( | |
| 636 new PluginProcessHostMsg_PluginSyncMessage(v, &r))) { | |
| 637 return CPERR_FAILURE; | |
| 638 } | |
| 639 | |
| 640 if (r.size()) { | |
| 641 *retval_len = static_cast<uint32>(r.size()); | |
| 642 *retval = CPB_Alloc(*retval_len); | |
| 643 memcpy(*retval, &(r.at(0)), r.size()); | |
| 644 } else { | |
| 645 *retval = NULL; | |
| 646 *retval_len = 0; | |
| 647 } | |
| 648 | |
| 649 return CPERR_SUCCESS; | |
| 650 } | |
| 651 | |
| 652 CPError STDCALL CPB_PluginThreadAsyncCall(CPID id, | |
| 653 void (*func)(void *), | |
| 654 void *user_data) { | |
| 655 g_plugin_thread_message_loop->PostTask( | |
| 656 FROM_HERE, NewRunnableFunction(func, user_data)); | |
| 657 | |
| 658 return CPERR_SUCCESS; | |
| 659 } | |
| 660 | |
| 661 CPError STDCALL CPB_OpenFileDialog(CPID id, | |
| 662 CPBrowsingContext context, | |
| 663 bool multiple_files, | |
| 664 const char *title, | |
| 665 const char *filter, | |
| 666 void *user_data) { | |
| 667 NOTREACHED() << | |
| 668 "Open file dialog should only be called from the renderer process."; | |
| 669 | |
| 670 return CPERR_FAILURE; | |
| 671 } | |
| 672 | |
| 673 } // namespace | |
| 674 | |
| 675 CPBrowserFuncs* GetCPBrowserFuncsForPlugin() { | |
| 676 static CPBrowserFuncs browser_funcs; | |
| 677 static CPRequestFuncs request_funcs; | |
| 678 static CPResponseFuncs response_funcs; | |
| 679 static bool initialized = false; | |
| 680 if (!initialized) { | |
| 681 initialized = true; | |
| 682 | |
| 683 g_plugin_thread_message_loop = PluginThread::current()->message_loop(); | |
| 684 | |
| 685 browser_funcs.size = sizeof(browser_funcs); | |
| 686 browser_funcs.version = CP_VERSION; | |
| 687 browser_funcs.enable_request_intercept = CPB_EnableRequestIntercept; | |
| 688 browser_funcs.create_request = CPB_CreateRequest; | |
| 689 browser_funcs.get_cookies = CPB_GetCookies; | |
| 690 browser_funcs.alloc = CPB_Alloc; | |
| 691 browser_funcs.free = CPB_Free; | |
| 692 browser_funcs.set_keep_process_alive = CPB_SetKeepProcessAlive; | |
| 693 browser_funcs.show_html_dialog = CPB_ShowHtmlDialog; | |
| 694 browser_funcs.show_html_dialog_modal = CPB_ShowHtmlDialogModal; | |
| 695 browser_funcs.is_plugin_process_running = CPB_IsPluginProcessRunning; | |
| 696 browser_funcs.get_process_type = CPB_GetProcessType; | |
| 697 browser_funcs.send_message = CPB_SendMessage; | |
| 698 browser_funcs.get_browsing_context_from_npp = CPB_GetBrowsingContextFromNPP; | |
| 699 browser_funcs.get_browsing_context_info = CPB_GetBrowsingContextInfo; | |
| 700 browser_funcs.get_command_line_arguments = CPB_GetCommandLineArguments; | |
| 701 browser_funcs.add_ui_command = CPB_AddUICommand; | |
| 702 browser_funcs.handle_command = CPB_HandleCommand; | |
| 703 browser_funcs.send_sync_message = CPB_SendSyncMessage; | |
| 704 browser_funcs.plugin_thread_async_call = CPB_PluginThreadAsyncCall; | |
| 705 browser_funcs.open_file_dialog = CPB_OpenFileDialog; | |
| 706 browser_funcs.get_drag_data = CPB_GetDragData; | |
| 707 browser_funcs.set_drop_effect = CPB_SetDropEffect; | |
| 708 browser_funcs.allow_file_drop = CPB_AllowFileDrop; | |
| 709 | |
| 710 browser_funcs.request_funcs = &request_funcs; | |
| 711 browser_funcs.response_funcs = &response_funcs; | |
| 712 | |
| 713 request_funcs.size = sizeof(request_funcs); | |
| 714 request_funcs.start_request = CPR_StartRequest; | |
| 715 request_funcs.end_request = CPR_EndRequest; | |
| 716 request_funcs.set_extra_request_headers = CPR_SetExtraRequestHeaders; | |
| 717 request_funcs.set_request_load_flags = CPR_SetRequestLoadFlags; | |
| 718 request_funcs.append_data_to_upload = CPR_AppendDataToUpload; | |
| 719 request_funcs.get_response_info = CPR_GetResponseInfo; | |
| 720 request_funcs.read = CPR_Read; | |
| 721 request_funcs.append_file_to_upload = CPR_AppendFileToUpload; | |
| 722 | |
| 723 response_funcs.size = sizeof(response_funcs); | |
| 724 response_funcs.received_redirect = CPRR_ReceivedRedirect; | |
| 725 response_funcs.start_completed = CPRR_StartCompleted; | |
| 726 response_funcs.read_completed = CPRR_ReadCompleted; | |
| 727 response_funcs.upload_progress = CPRR_UploadProgress; | |
| 728 } | |
| 729 | |
| 730 return &browser_funcs; | |
| 731 } | |
| OLD | NEW |