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
3 // found in the LICENSE file.
5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.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"
14 #include "content/public/renderer/renderer_ppapi_host.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/host_message_context.h"
18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebPluginContainer.h"
25 PepperExtensionsCommonHost::PepperExtensionsCommonHost(
26 content::RendererPpapiHost* host,
29 extensions::Dispatcher* dispatcher)
30 : ResourceHost(host->GetPpapiHost(), instance, resource),
31 renderer_ppapi_host_(host),
32 dispatcher_(dispatcher) {
35 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() {
36 dispatcher_->request_sender()->InvalidateSource(this);
40 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create(
41 content::RendererPpapiHost* host,
43 PP_Resource resource) {
44 content::RenderView* render_view = host->GetRenderViewForInstance(instance);
47 extensions::ExtensionHelper* extension_helper =
48 extensions::ExtensionHelper::Get(render_view);
49 if (!extension_helper)
51 extensions::Dispatcher* dispatcher = extension_helper->dispatcher();
55 return new PepperExtensionsCommonHost(host, instance, resource, dispatcher);
58 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived(
59 const IPC::Message& msg,
60 ppapi::host::HostMessageContext* context) {
61 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg)
62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post,
64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call,
67 return PP_ERROR_FAILED;
70 extensions::ChromeV8Context* PepperExtensionsCommonHost::GetContext() {
71 WebKit::WebPluginContainer* container =
72 renderer_ppapi_host_->GetContainerForInstance(pp_instance());
76 WebKit::WebFrame* frame = container->element().document().frame();
77 v8::HandleScope scope(v8::Isolate::GetCurrent());
78 return dispatcher_->v8_context_set().GetByV8Context(
79 frame->mainWorldScriptContext());
82 void PepperExtensionsCommonHost::OnResponseReceived(
83 const std::string& /* name */,
86 const base::ListValue& response,
87 const std::string& /* error */) {
88 PendingRequestMap::iterator iter = pending_request_map_.find(request_id);
90 // Ignore responses resulted from calls to OnPost().
91 if (iter == pending_request_map_.end()) {
92 DCHECK_EQ(0u, response.GetSize());
96 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second;
97 pending_request_map_.erase(iter);
99 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED);
100 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response));
103 int32_t PepperExtensionsCommonHost::OnPost(
104 ppapi::host::HostMessageContext* context,
105 const std::string& request_name,
106 base::ListValue& args) {
107 // TODO(yzshen): Add support for calling into JS for APIs that have custom
109 int request_id = dispatcher_->request_sender()->GetNextRequestId();
110 dispatcher_->request_sender()->StartRequest(this, request_name, request_id,
111 false, false, &args);
115 int32_t PepperExtensionsCommonHost::OnCall(
116 ppapi::host::HostMessageContext* context,
117 const std::string& request_name,
118 base::ListValue& args) {
119 // TODO(yzshen): Add support for calling into JS for APIs that have custom
121 int request_id = dispatcher_->request_sender()->GetNextRequestId();
122 pending_request_map_[request_id] =
123 linked_ptr<ppapi::host::ReplyMessageContext>(
124 new ppapi::host::ReplyMessageContext(
125 context->MakeReplyMessageContext()));
127 dispatcher_->request_sender()->StartRequest(this, request_name, request_id,
129 return PP_OK_COMPLETIONPENDING;