Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h" | 5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/renderer/extensions/chrome_v8_context.h" | 10 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 11 #include "chrome/renderer/extensions/dispatcher.h" | 11 #include "chrome/renderer/extensions/dispatcher.h" |
| 12 #include "chrome/renderer/extensions/extension_helper.h" | 12 #include "chrome/renderer/extensions/extension_helper.h" |
| 13 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
| 14 #include "content/public/renderer/renderer_ppapi_host.h" | 14 #include "content/public/renderer/renderer_ppapi_host.h" |
| 15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/host/dispatch_host_message.h" | 16 #include "ppapi/host/dispatch_host_message.h" |
| 17 #include "ppapi/host/host_message_context.h" | 17 #include "ppapi/host/host_message_context.h" |
| 18 #include "ppapi/host/ppapi_host.h" | 18 #include "ppapi/host/ppapi_host.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" | 19 #include "ppapi/proxy/ppapi_messages.h" |
| 20 #include "third_party/WebKit/public/web/WebDocument.h" | 20 #include "third_party/WebKit/public/web/WebDocument.h" |
| 21 #include "third_party/WebKit/public/web/WebElement.h" | 21 #include "third_party/WebKit/public/web/WebElement.h" |
| 22 #include "third_party/WebKit/public/web/WebFrame.h" | 22 #include "third_party/WebKit/public/web/WebFrame.h" |
| 23 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 23 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
| 24 | 24 |
| 25 PepperExtensionsCommonHost::PepperExtensionsCommonHost( | 25 PepperExtensionsCommonHost::PepperExtensionsCommonHost( |
| 26 content::RendererPpapiHost* host, | 26 content::RendererPpapiHost* host, |
| 27 PP_Instance instance, | 27 PP_Instance instance, |
| 28 PP_Resource resource, | 28 PP_Resource resource, |
| 29 extensions::Dispatcher* dispatcher) | 29 extensions::PepperRequestProxy* pepper_request_proxy) |
| 30 : ResourceHost(host->GetPpapiHost(), instance, resource), | 30 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 31 renderer_ppapi_host_(host), | 31 renderer_ppapi_host_(host), |
| 32 dispatcher_(dispatcher) { | 32 pepper_request_proxy_(pepper_request_proxy) {} |
| 33 } | |
| 34 | 33 |
| 35 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() { | 34 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() { |
| 36 dispatcher_->request_sender()->InvalidateSource(this); | 35 pepper_request_proxy_->InvalidateSource(this); |
| 37 } | 36 } |
| 38 | 37 |
| 39 // static | 38 // static |
| 40 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create( | 39 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create( |
| 41 content::RendererPpapiHost* host, | 40 content::RendererPpapiHost* host, |
| 42 PP_Instance instance, | 41 PP_Instance instance, |
| 43 PP_Resource resource) { | 42 PP_Resource resource) { |
| 44 content::RenderView* render_view = host->GetRenderViewForInstance(instance); | 43 content::RenderView* render_view = host->GetRenderViewForInstance(instance); |
| 45 if (!render_view) | 44 if (!render_view) |
| 46 return NULL; | 45 return NULL; |
| 47 extensions::ExtensionHelper* extension_helper = | 46 extensions::ExtensionHelper* extension_helper = |
| 48 extensions::ExtensionHelper::Get(render_view); | 47 extensions::ExtensionHelper::Get(render_view); |
| 49 if (!extension_helper) | 48 if (!extension_helper) |
| 50 return NULL; | 49 return NULL; |
| 50 blink::WebPluginContainer* container = | |
|
yzshen1
2013/11/26 19:33:11
It seems better to move line 54-56 to be right bel
Sam McNally
2013/11/27 23:40:26
Done.
| |
| 51 host->GetContainerForInstance(instance); | |
| 52 if (!container) | |
| 53 return NULL; | |
| 51 extensions::Dispatcher* dispatcher = extension_helper->dispatcher(); | 54 extensions::Dispatcher* dispatcher = extension_helper->dispatcher(); |
| 52 if (!dispatcher) | 55 if (!dispatcher) |
| 53 return NULL; | 56 return NULL; |
| 57 blink::WebFrame* frame = container->element().document().frame(); | |
| 58 if (!frame) | |
| 59 return NULL; | |
| 60 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
| 61 extensions::ChromeV8Context* context = | |
| 62 dispatcher->v8_context_set().GetByV8Context( | |
| 63 frame->mainWorldScriptContext()); | |
| 64 if (!context) | |
| 65 return NULL; | |
| 54 | 66 |
| 55 return new PepperExtensionsCommonHost(host, instance, resource, dispatcher); | 67 return new PepperExtensionsCommonHost( |
| 68 host, instance, resource, context->pepper_request_proxy()); | |
| 56 } | 69 } |
| 57 | 70 |
| 58 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived( | 71 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived( |
| 59 const IPC::Message& msg, | 72 const IPC::Message& msg, |
| 60 ppapi::host::HostMessageContext* context) { | 73 ppapi::host::HostMessageContext* context) { |
| 61 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg) | 74 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg) |
| 62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post, | 75 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post, |
| 63 OnPost) | 76 OnPost) |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call, | 77 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call, |
| 65 OnCall) | 78 OnCall) |
| 66 IPC_END_MESSAGE_MAP() | 79 IPC_END_MESSAGE_MAP() |
| 67 return PP_ERROR_FAILED; | 80 return PP_ERROR_FAILED; |
| 68 } | 81 } |
| 69 | 82 |
| 70 extensions::ChromeV8Context* PepperExtensionsCommonHost::GetContext() { | |
| 71 blink::WebPluginContainer* container = | |
| 72 renderer_ppapi_host_->GetContainerForInstance(pp_instance()); | |
| 73 if (!container) | |
| 74 return NULL; | |
| 75 | |
| 76 blink::WebFrame* frame = container->element().document().frame(); | |
| 77 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
| 78 return dispatcher_->v8_context_set().GetByV8Context( | |
| 79 frame->mainWorldScriptContext()); | |
| 80 } | |
| 81 | |
| 82 void PepperExtensionsCommonHost::OnResponseReceived( | 83 void PepperExtensionsCommonHost::OnResponseReceived( |
| 83 const std::string& /* name */, | |
| 84 int request_id, | 84 int request_id, |
| 85 bool success, | 85 bool success, |
| 86 const base::ListValue& response, | 86 const base::ListValue& response, |
| 87 const std::string& /* error */) { | 87 const std::string& /* error */) { |
| 88 PendingRequestMap::iterator iter = pending_request_map_.find(request_id); | 88 PendingRequestMap::iterator iter = pending_request_map_.find(request_id); |
| 89 | 89 |
| 90 // Ignore responses resulted from calls to OnPost(). | 90 DCHECK(iter != pending_request_map_.end()); |
| 91 if (iter == pending_request_map_.end()) { | |
| 92 DCHECK_EQ(0u, response.GetSize()); | |
| 93 return; | |
| 94 } | |
| 95 | 91 |
| 96 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second; | 92 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second; |
| 97 pending_request_map_.erase(iter); | 93 pending_request_map_.erase(iter); |
| 98 | 94 |
| 99 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED); | 95 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED); |
| 100 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response)); | 96 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response)); |
| 101 } | 97 } |
| 102 | 98 |
| 103 int32_t PepperExtensionsCommonHost::OnPost( | 99 int32_t PepperExtensionsCommonHost::OnPost( |
| 104 ppapi::host::HostMessageContext* context, | 100 ppapi::host::HostMessageContext* context, |
| 105 const std::string& request_name, | 101 const std::string& request_name, |
| 106 base::ListValue& args) { | 102 base::ListValue& args) { |
| 107 // TODO(yzshen): Add support for calling into JS for APIs that have custom | 103 int request_id; |
| 108 // bindings. | 104 std::string error; |
| 109 int request_id = dispatcher_->request_sender()->GetNextRequestId(); | 105 bool success = pepper_request_proxy_->StartRequest( |
|
yzshen1
2013/11/26 19:33:11
Please add a TODO (maybe in pepper_request_proxy f
Sam McNally
2013/11/27 23:40:26
Done.
| |
| 110 dispatcher_->request_sender()->StartRequest(this, request_name, request_id, | 106 this, request_name, false, args, &request_id, &error); |
| 111 false, false, &args); | 107 if (!success) |
| 108 return PP_ERROR_FAILED; | |
| 109 | |
| 112 return PP_OK; | 110 return PP_OK; |
| 113 } | 111 } |
| 114 | 112 |
| 115 int32_t PepperExtensionsCommonHost::OnCall( | 113 int32_t PepperExtensionsCommonHost::OnCall( |
| 116 ppapi::host::HostMessageContext* context, | 114 ppapi::host::HostMessageContext* context, |
| 117 const std::string& request_name, | 115 const std::string& request_name, |
| 118 base::ListValue& args) { | 116 base::ListValue& args) { |
| 119 // TODO(yzshen): Add support for calling into JS for APIs that have custom | 117 int request_id; |
| 120 // bindings. | 118 std::string error; |
| 121 int request_id = dispatcher_->request_sender()->GetNextRequestId(); | 119 bool success = pepper_request_proxy_->StartRequest( |
| 120 this, request_name, true, args, &request_id, &error); | |
| 121 if (!success) | |
| 122 return PP_ERROR_FAILED; | |
| 123 | |
| 122 pending_request_map_[request_id] = | 124 pending_request_map_[request_id] = |
| 123 linked_ptr<ppapi::host::ReplyMessageContext>( | 125 linked_ptr<ppapi::host::ReplyMessageContext>( |
| 124 new ppapi::host::ReplyMessageContext( | 126 new ppapi::host::ReplyMessageContext( |
| 125 context->MakeReplyMessageContext())); | 127 context->MakeReplyMessageContext())); |
| 126 | 128 |
| 127 dispatcher_->request_sender()->StartRequest(this, request_name, request_id, | |
| 128 true, false, &args); | |
| 129 return PP_OK_COMPLETIONPENDING; | 129 return PP_OK_COMPLETIONPENDING; |
| 130 } | 130 } |
| OLD | NEW |