718a858919cb5e67292d71385fb7b4c0fc89526e
[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_InstanceDestroyed,
48         OnInstanceDestroyed)
49     IPC_MESSAGE_UNHANDLED(handled = false)
50   IPC_END_MESSAGE_MAP()
51
52   return handled;
53 }
54
55 XWalkExtensionClient::ExtensionCodePoints::ExtensionCodePoints() {
56 }
57
58 XWalkExtensionClient::ExtensionCodePoints::~ExtensionCodePoints() {
59 }
60
61 void XWalkExtensionClient::OnPostMessageToJS(int64_t instance_id,
62                                              const base::ListValue& msg) {
63   HandlerMap::const_iterator it = handlers_.find(instance_id);
64   if (it == handlers_.end()) {
65     LOG(WARNING) << "Can't PostMessage to invalid Extension instance id: "
66                  << instance_id;
67     return;
68   }
69
70   // See comment in DestroyInstance() about two step destruction.
71   if (!it->second)
72     return;
73
74   const base::Value* value;
75   msg.Get(0, &value);
76   it->second->HandleMessageFromNative(*value);
77 }
78
79 void XWalkExtensionClient::DestroyInstance(int64_t instance_id) {
80   HandlerMap::iterator it = handlers_.find(instance_id);
81   if (it == handlers_.end() || !it->second) {
82     LOG(WARNING) << "Can't Destroy invalid instance id: " << instance_id;
83     return;
84   }
85   Send(new XWalkExtensionServerMsg_DestroyInstance(instance_id));
86
87   // Destruction happens in two steps, first we nullify the handler in our map,
88   // to indicate that destruction message was sent. If we get a new message from
89   // this instance, we can silently ignore. Later, we get a confirmation message
90   // from the server, only then we remove the entry from the map.
91   it->second = NULL;
92 }
93
94 void XWalkExtensionClient::OnInstanceDestroyed(int64_t instance_id) {
95   HandlerMap::iterator it = handlers_.find(instance_id);
96   if (it == handlers_.end()) {
97     LOG(WARNING) << "Got InstanceDestroyed msg for invalid instance id: "
98                  << instance_id;
99     return;
100   }
101
102   // Second part of the two step destruction. See DestroyInstance() for details.
103   // The system currently assumes that we always control the destruction of
104   // instances.
105   DCHECK(!it->second);
106   handlers_.erase(it);
107 }
108
109 namespace {
110
111 // Regular base::Value doesn't have param traits, so can't be passed as is
112 // through IPC. We wrap it in a base::ListValue that have traits before
113 // exchanging.
114 //
115 // Implementing param traits for base::Value is not a viable option at the
116 // moment (would require fork base::Value and create a new empty type).
117 scoped_ptr<base::ListValue> WrapValueInList(scoped_ptr<base::Value> value) {
118   if (!value)
119     return scoped_ptr<base::ListValue>();
120   scoped_ptr<base::ListValue> list_value(new base::ListValue);
121   list_value->Append(value.release());
122   return list_value.Pass();
123 }
124
125 }  // namespace
126
127 void XWalkExtensionClient::PostMessageToNative(int64_t instance_id,
128     scoped_ptr<base::Value> msg) {
129   scoped_ptr<base::ListValue> list_msg = WrapValueInList(msg.Pass());
130   Send(new XWalkExtensionServerMsg_PostMessageToNative(instance_id, *list_msg));
131 }
132
133 scoped_ptr<base::Value> XWalkExtensionClient::SendSyncMessageToNative(
134     int64_t instance_id, scoped_ptr<base::Value> msg) {
135   scoped_ptr<base::ListValue> wrapped_msg = WrapValueInList(msg.Pass());
136   base::ListValue* wrapped_reply = new base::ListValue;
137   Send(new XWalkExtensionServerMsg_SendSyncMessageToNative(instance_id,
138       *wrapped_msg, wrapped_reply));
139
140   scoped_ptr<base::Value> reply;
141   wrapped_reply->Remove(0, &reply);
142   return reply.Pass();
143 }
144
145 void XWalkExtensionClient::Initialize(IPC::Sender* sender) {
146   sender_ = sender;
147
148   std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams> extensions;
149   Send(new XWalkExtensionServerMsg_GetExtensions(&extensions));
150
151   if (extensions.empty())
152     return;
153
154   std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams>::iterator it =
155       extensions.begin();
156   for (; it != extensions.end(); ++it) {
157     ExtensionCodePoints* codepoint = new ExtensionCodePoints;
158     codepoint->api = (*it).js_api;
159
160     codepoint->entry_points = (*it).entry_points;
161
162     std::string name = (*it).name;
163     extension_apis_[name] = codepoint;
164   }
165 }
166
167 }  // namespace extensions
168 }  // namespace xwalk