Upstream version 11.39.252.0
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / renderer / xwalk_extension_client.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/extensions/renderer/xwalk_extension_client.h"
6
7 #include "base/values.h"
8 #include "base/stl_util.h"
9 #include "ipc/ipc_sender.h"
10 #include "xwalk/extensions/common/xwalk_extension_messages.h"
11
12 namespace xwalk {
13 namespace extensions {
14
15 XWalkExtensionClient::XWalkExtensionClient()
16     : sender_(0),
17       next_instance_id_(1) {  // Zero is never used for a valid instance.
18 }
19
20 XWalkExtensionClient::~XWalkExtensionClient() {
21   STLDeleteValues(&extension_apis_);
22 }
23
24 bool XWalkExtensionClient::Send(IPC::Message* msg) {
25   DCHECK(sender_);
26
27   return sender_->Send(msg);
28 }
29
30 int64_t XWalkExtensionClient::CreateInstance(
31     const std::string& extension_name,
32     InstanceHandler* handler) {
33   CHECK(handler);
34   if (!Send(new XWalkExtensionServerMsg_CreateInstance(next_instance_id_,
35                                                        extension_name))) {
36     return 0;
37   }
38   handlers_[next_instance_id_] = handler;
39   return next_instance_id_++;
40 }
41
42 bool XWalkExtensionClient::OnMessageReceived(const IPC::Message& message) {
43   bool handled = true;
44   IPC_BEGIN_MESSAGE_MAP(XWalkExtensionClient, message)
45     IPC_MESSAGE_HANDLER(XWalkExtensionClientMsg_PostMessageToJS,
46         OnPostMessageToJS)
47     IPC_MESSAGE_HANDLER(XWalkExtensionClientMsg_PostOutOfLineMessageToJS,
48         OnPostOutOfLineMessageToJS)
49     IPC_MESSAGE_HANDLER(XWalkExtensionClientMsg_InstanceDestroyed,
50         OnInstanceDestroyed)
51     IPC_MESSAGE_UNHANDLED(handled = false)
52   IPC_END_MESSAGE_MAP()
53
54   return handled;
55 }
56
57 XWalkExtensionClient::ExtensionCodePoints::ExtensionCodePoints() {
58 }
59
60 XWalkExtensionClient::ExtensionCodePoints::~ExtensionCodePoints() {
61 }
62
63 void XWalkExtensionClient::OnPostMessageToJS(int64_t instance_id,
64                                              const base::ListValue& msg) {
65   HandlerMap::const_iterator it = handlers_.find(instance_id);
66   if (it == handlers_.end()) {
67     LOG(WARNING) << "Can't PostMessage to invalid Extension instance id: "
68                  << instance_id;
69     return;
70   }
71
72   // See comment in DestroyInstance() about two step destruction.
73   if (!it->second)
74     return;
75
76   const base::Value* value;
77   if (!msg.Get(0, &value))
78     return;
79   it->second->HandleMessageFromNative(*value);
80 }
81
82 void XWalkExtensionClient::OnPostOutOfLineMessageToJS(
83     base::SharedMemoryHandle handle, size_t size) {
84   CHECK(base::SharedMemory::IsHandleValid(handle));
85
86   base::SharedMemory shared_memory(handle, true);
87   if (!shared_memory.Map(size))
88     return;
89
90   IPC::Message message(static_cast<char*>(shared_memory.memory()), size);
91   OnMessageReceived(message);
92 }
93
94 void XWalkExtensionClient::DestroyInstance(int64_t instance_id) {
95   HandlerMap::iterator it = handlers_.find(instance_id);
96   if (it == handlers_.end() || !it->second) {
97     LOG(WARNING) << "Can't Destroy invalid instance id: " << instance_id;
98     return;
99   }
100   Send(new XWalkExtensionServerMsg_DestroyInstance(instance_id));
101
102   // Destruction happens in two steps, first we nullify the handler in our map,
103   // to indicate that destruction message was sent. If we get a new message from
104   // this instance, we can silently ignore. Later, we get a confirmation message
105   // from the server, only then we remove the entry from the map.
106   it->second = NULL;
107 }
108
109 void XWalkExtensionClient::OnInstanceDestroyed(int64_t instance_id) {
110   HandlerMap::iterator it = handlers_.find(instance_id);
111   if (it == handlers_.end()) {
112     LOG(WARNING) << "Got InstanceDestroyed msg for invalid instance id: "
113                  << instance_id;
114     return;
115   }
116
117   // Second part of the two step destruction. See DestroyInstance() for details.
118   // The system currently assumes that we always control the destruction of
119   // instances.
120   DCHECK(!it->second);
121   handlers_.erase(it);
122 }
123
124 namespace {
125
126 // Regular base::Value doesn't have param traits, so can't be passed as is
127 // through IPC. We wrap it in a base::ListValue that have traits before
128 // exchanging.
129 //
130 // Implementing param traits for base::Value is not a viable option at the
131 // moment (would require fork base::Value and create a new empty type).
132 scoped_ptr<base::ListValue> WrapValueInList(scoped_ptr<base::Value> value) {
133   if (!value)
134     return scoped_ptr<base::ListValue>();
135   scoped_ptr<base::ListValue> list_value(new base::ListValue);
136   list_value->Append(value.release());
137   return list_value.Pass();
138 }
139
140 }  // namespace
141
142 void XWalkExtensionClient::PostMessageToNative(int64_t instance_id,
143     scoped_ptr<base::Value> msg) {
144   scoped_ptr<base::ListValue> list_msg = WrapValueInList(msg.Pass());
145   Send(new XWalkExtensionServerMsg_PostMessageToNative(instance_id, *list_msg));
146 }
147
148 scoped_ptr<base::Value> XWalkExtensionClient::SendSyncMessageToNative(
149     int64_t instance_id, scoped_ptr<base::Value> msg) {
150   scoped_ptr<base::ListValue> wrapped_msg = WrapValueInList(msg.Pass());
151   base::ListValue* wrapped_reply = new base::ListValue;
152   Send(new XWalkExtensionServerMsg_SendSyncMessageToNative(instance_id,
153       *wrapped_msg, wrapped_reply));
154
155   scoped_ptr<base::Value> reply;
156   wrapped_reply->Remove(0, &reply);
157   return reply.Pass();
158 }
159
160 void XWalkExtensionClient::Initialize(IPC::Sender* sender) {
161   sender_ = sender;
162
163   std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams> extensions;
164   Send(new XWalkExtensionServerMsg_GetExtensions(&extensions));
165
166   if (extensions.empty())
167     return;
168
169   std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams>::iterator it =
170       extensions.begin();
171   for (; it != extensions.end(); ++it) {
172     ExtensionCodePoints* codepoint = new ExtensionCodePoints;
173     codepoint->api = (*it).js_api;
174
175     codepoint->entry_points = (*it).entry_points;
176
177     std::string name = (*it).name;
178     extension_apis_[name] = codepoint;
179   }
180 }
181
182 }  // namespace extensions
183 }  // namespace xwalk