| 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 namespace { |
| 26 void DoNothing(bool success, |
| 27 const base::ListValue& response, |
| 28 const std::string& error) {} |
| 29 } |
| 30 |
| 25 PepperExtensionsCommonHost::PepperExtensionsCommonHost( | 31 PepperExtensionsCommonHost::PepperExtensionsCommonHost( |
| 26 content::RendererPpapiHost* host, | 32 content::RendererPpapiHost* host, |
| 27 PP_Instance instance, | 33 PP_Instance instance, |
| 28 PP_Resource resource, | 34 PP_Resource resource, |
| 29 extensions::Dispatcher* dispatcher) | 35 extensions::PepperRequestProxy* pepper_request_proxy) |
| 30 : ResourceHost(host->GetPpapiHost(), instance, resource), | 36 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 31 renderer_ppapi_host_(host), | 37 renderer_ppapi_host_(host), |
| 32 dispatcher_(dispatcher) { | 38 pepper_request_proxy_(pepper_request_proxy), |
| 33 } | 39 weak_factory_(this) {} |
| 34 | 40 |
| 35 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() { | 41 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() {} |
| 36 dispatcher_->request_sender()->InvalidateSource(this); | |
| 37 } | |
| 38 | 42 |
| 39 // static | 43 // static |
| 40 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create( | 44 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create( |
| 41 content::RendererPpapiHost* host, | 45 content::RendererPpapiHost* host, |
| 42 PP_Instance instance, | 46 PP_Instance instance, |
| 43 PP_Resource resource) { | 47 PP_Resource resource) { |
| 44 content::RenderView* render_view = host->GetRenderViewForInstance(instance); | 48 content::RenderView* render_view = host->GetRenderViewForInstance(instance); |
| 45 if (!render_view) | 49 if (!render_view) |
| 46 return NULL; | 50 return NULL; |
| 47 extensions::ExtensionHelper* extension_helper = | 51 extensions::ExtensionHelper* extension_helper = |
| 48 extensions::ExtensionHelper::Get(render_view); | 52 extensions::ExtensionHelper::Get(render_view); |
| 49 if (!extension_helper) | 53 if (!extension_helper) |
| 50 return NULL; | 54 return NULL; |
| 51 extensions::Dispatcher* dispatcher = extension_helper->dispatcher(); | 55 extensions::Dispatcher* dispatcher = extension_helper->dispatcher(); |
| 52 if (!dispatcher) | 56 if (!dispatcher) |
| 53 return NULL; | 57 return NULL; |
| 58 blink::WebPluginContainer* container = |
| 59 host->GetContainerForInstance(instance); |
| 60 if (!container) |
| 61 return NULL; |
| 62 blink::WebFrame* frame = container->element().document().frame(); |
| 63 if (!frame) |
| 64 return NULL; |
| 65 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 66 extensions::ChromeV8Context* context = |
| 67 dispatcher->v8_context_set().GetByV8Context( |
| 68 frame->mainWorldScriptContext()); |
| 69 if (!context) |
| 70 return NULL; |
| 54 | 71 |
| 55 return new PepperExtensionsCommonHost(host, instance, resource, dispatcher); | 72 return new PepperExtensionsCommonHost( |
| 73 host, instance, resource, context->pepper_request_proxy()); |
| 56 } | 74 } |
| 57 | 75 |
| 58 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived( | 76 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived( |
| 59 const IPC::Message& msg, | 77 const IPC::Message& msg, |
| 60 ppapi::host::HostMessageContext* context) { | 78 ppapi::host::HostMessageContext* context) { |
| 61 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg) | 79 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg) |
| 62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post, | 80 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post, |
| 63 OnPost) | 81 OnPost) |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call, | 82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call, |
| 65 OnCall) | 83 OnCall) |
| 66 IPC_END_MESSAGE_MAP() | 84 IPC_END_MESSAGE_MAP() |
| 67 return PP_ERROR_FAILED; | 85 return PP_ERROR_FAILED; |
| 68 } | 86 } |
| 69 | 87 |
| 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( | 88 void PepperExtensionsCommonHost::OnResponseReceived( |
| 83 const std::string& /* name */, | 89 scoped_ptr<ppapi::host::ReplyMessageContext> context, |
| 84 int request_id, | |
| 85 bool success, | 90 bool success, |
| 86 const base::ListValue& response, | 91 const base::ListValue& response, |
| 87 const std::string& /* error */) { | 92 const std::string& /* error */) { |
| 88 PendingRequestMap::iterator iter = pending_request_map_.find(request_id); | |
| 89 | |
| 90 // Ignore responses resulted from calls to OnPost(). | |
| 91 if (iter == pending_request_map_.end()) { | |
| 92 DCHECK_EQ(0u, response.GetSize()); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second; | |
| 97 pending_request_map_.erase(iter); | |
| 98 | |
| 99 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED); | 93 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED); |
| 100 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response)); | 94 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response)); |
| 101 } | 95 } |
| 102 | 96 |
| 103 int32_t PepperExtensionsCommonHost::OnPost( | 97 int32_t PepperExtensionsCommonHost::OnPost( |
| 104 ppapi::host::HostMessageContext* context, | 98 ppapi::host::HostMessageContext* context, |
| 105 const std::string& request_name, | 99 const std::string& request_name, |
| 106 base::ListValue& args) { | 100 const base::ListValue& args) { |
| 107 // TODO(yzshen): Add support for calling into JS for APIs that have custom | 101 std::string error; |
| 108 // bindings. | 102 bool success = pepper_request_proxy_->StartRequest( |
| 109 int request_id = dispatcher_->request_sender()->GetNextRequestId(); | 103 base::Bind(&DoNothing), request_name, args, &error); |
| 110 dispatcher_->request_sender()->StartRequest(this, request_name, request_id, | 104 return success ? PP_OK : PP_ERROR_FAILED; |
| 111 false, false, &args); | |
| 112 return PP_OK; | |
| 113 } | 105 } |
| 114 | 106 |
| 115 int32_t PepperExtensionsCommonHost::OnCall( | 107 int32_t PepperExtensionsCommonHost::OnCall( |
| 116 ppapi::host::HostMessageContext* context, | 108 ppapi::host::HostMessageContext* context, |
| 117 const std::string& request_name, | 109 const std::string& request_name, |
| 118 base::ListValue& args) { | 110 const base::ListValue& args) { |
| 119 // TODO(yzshen): Add support for calling into JS for APIs that have custom | 111 std::string error; |
| 120 // bindings. | 112 scoped_ptr<ppapi::host::ReplyMessageContext> message_context( |
| 121 int request_id = dispatcher_->request_sender()->GetNextRequestId(); | 113 new ppapi::host::ReplyMessageContext(context->MakeReplyMessageContext())); |
| 122 pending_request_map_[request_id] = | 114 bool success = pepper_request_proxy_->StartRequest( |
| 123 linked_ptr<ppapi::host::ReplyMessageContext>( | 115 base::Bind(&PepperExtensionsCommonHost::OnResponseReceived, |
| 124 new ppapi::host::ReplyMessageContext( | 116 weak_factory_.GetWeakPtr(), |
| 125 context->MakeReplyMessageContext())); | 117 base::Passed(&message_context)), |
| 126 | 118 request_name, |
| 127 dispatcher_->request_sender()->StartRequest(this, request_name, request_id, | 119 args, |
| 128 true, false, &args); | 120 &error); |
| 129 return PP_OK_COMPLETIONPENDING; | 121 return success ? PP_OK_COMPLETIONPENDING : PP_ERROR_FAILED; |
| 130 } | 122 } |
| OLD | NEW |