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.
5 #include "xwalk/extensions/renderer/xwalk_extension_client.h"
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"
13 namespace extensions {
15 XWalkExtensionClient::XWalkExtensionClient()
17 next_instance_id_(1) { // Zero is never used for a valid instance.
20 XWalkExtensionClient::~XWalkExtensionClient() {
21 STLDeleteValues(&extension_apis_);
24 bool XWalkExtensionClient::Send(IPC::Message* msg) {
27 return sender_->Send(msg);
30 int64_t XWalkExtensionClient::CreateInstance(
31 const std::string& extension_name,
32 InstanceHandler* handler) {
34 if (!Send(new XWalkExtensionServerMsg_CreateInstance(next_instance_id_,
38 handlers_[next_instance_id_] = handler;
39 return next_instance_id_++;
42 bool XWalkExtensionClient::OnMessageReceived(const IPC::Message& message) {
44 IPC_BEGIN_MESSAGE_MAP(XWalkExtensionClient, message)
45 IPC_MESSAGE_HANDLER(XWalkExtensionClientMsg_PostMessageToJS,
47 IPC_MESSAGE_HANDLER(XWalkExtensionClientMsg_InstanceDestroyed,
49 IPC_MESSAGE_UNHANDLED(handled = false)
55 XWalkExtensionClient::ExtensionCodePoints::ExtensionCodePoints() {
58 XWalkExtensionClient::ExtensionCodePoints::~ExtensionCodePoints() {
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: "
70 // See comment in DestroyInstance() about two step destruction.
74 const base::Value* value;
76 it->second->HandleMessageFromNative(*value);
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;
85 Send(new XWalkExtensionServerMsg_DestroyInstance(instance_id));
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.
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: "
102 // Second part of the two step destruction. See DestroyInstance() for details.
103 // The system currently assumes that we always control the destruction of
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
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) {
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();
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));
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));
140 scoped_ptr<base::Value> reply;
141 wrapped_reply->Remove(0, &reply);
145 void XWalkExtensionClient::Initialize(IPC::Sender* sender) {
148 std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams> extensions;
149 Send(new XWalkExtensionServerMsg_GetExtensions(&extensions));
151 if (extensions.empty())
154 std::vector<XWalkExtensionServerMsg_ExtensionRegisterParams>::iterator it =
156 for (; it != extensions.end(); ++it) {
157 ExtensionCodePoints* codepoint = new ExtensionCodePoints;
158 codepoint->api = (*it).js_api;
160 codepoint->entry_points = (*it).entry_points;
162 std::string name = (*it).name;
163 extension_apis_[name] = codepoint;
167 } // namespace extensions