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