- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / pepper / pepper_extensions_common_host.cc
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.
4
5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h"
6
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"
24
25 PepperExtensionsCommonHost::PepperExtensionsCommonHost(
26     content::RendererPpapiHost* host,
27     PP_Instance instance,
28     PP_Resource resource,
29     extensions::Dispatcher* dispatcher)
30     : ResourceHost(host->GetPpapiHost(), instance, resource),
31       renderer_ppapi_host_(host),
32       dispatcher_(dispatcher) {
33 }
34
35 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() {
36   dispatcher_->request_sender()->InvalidateSource(this);
37 }
38
39 // static
40 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create(
41     content::RendererPpapiHost* host,
42     PP_Instance instance,
43     PP_Resource resource) {
44   content::RenderView* render_view = host->GetRenderViewForInstance(instance);
45   if (!render_view)
46     return NULL;
47   extensions::ExtensionHelper* extension_helper =
48       extensions::ExtensionHelper::Get(render_view);
49   if (!extension_helper)
50     return NULL;
51   extensions::Dispatcher* dispatcher = extension_helper->dispatcher();
52   if (!dispatcher)
53     return NULL;
54
55   return new PepperExtensionsCommonHost(host, instance, resource, dispatcher);
56 }
57
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,
63                                       OnPost)
64     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call,
65                                       OnCall)
66   IPC_END_MESSAGE_MAP()
67   return PP_ERROR_FAILED;
68 }
69
70 extensions::ChromeV8Context* PepperExtensionsCommonHost::GetContext() {
71   WebKit::WebPluginContainer* container =
72       renderer_ppapi_host_->GetContainerForInstance(pp_instance());
73   if (!container)
74     return NULL;
75
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());
80 }
81
82 void PepperExtensionsCommonHost::OnResponseReceived(
83     const std::string& /* name */,
84     int request_id,
85     bool success,
86     const base::ListValue& response,
87     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);
100   SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response));
101 }
102
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
108   // bindings.
109   int request_id = dispatcher_->request_sender()->GetNextRequestId();
110   dispatcher_->request_sender()->StartRequest(this, request_name, request_id,
111                                               false, false, &args);
112   return PP_OK;
113 }
114
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
120   // bindings.
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()));
126
127   dispatcher_->request_sender()->StartRequest(this, request_name, request_id,
128                                               true, false, &args);
129   return PP_OK_COMPLETIONPENDING;
130 }