namespace extensions {
+namespace {
+
+const int kRoutingIdEmbedderDataIndex = 12;
+
+} // namespace
+
RuntimeIPCClient::JSCallback::JSCallback(v8::Isolate* isolate,
v8::Handle<v8::Function> callback) {
callback_.Reset(isolate, callback);
return &self;
}
-RuntimeIPCClient::RuntimeIPCClient() : routing_id_(0) {
+RuntimeIPCClient::RuntimeIPCClient() {
+}
+
+int RuntimeIPCClient::GetRoutingId(v8::Handle<v8::Context> context) {
+ v8::Handle<v8::Value> value =
+ context->GetEmbedderData(kRoutingIdEmbedderDataIndex);
+ int routing_id = 0;
+ if (value->IsNumber()) {
+ routing_id = value->IntegerValue();
+ } else {
+ LOGGER(WARN) << "Failed to get routing index from context.";
+ }
+
+ return routing_id;
+}
+
+void RuntimeIPCClient::SetRoutingId(v8::Handle<v8::Context> context,
+ int routing_id) {
+ context->SetEmbedderData(kRoutingIdEmbedderDataIndex,
+ v8::Integer::New(context->GetIsolate(), routing_id));
}
-void RuntimeIPCClient::SendMessage(const std::string& type,
+void RuntimeIPCClient::SendMessage(v8::Handle<v8::Context> context,
+ const std::string& type,
const std::string& value) {
Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
ewk_ipc_wrt_message_data_id_set(msg, "");
ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
- if (routing_id_ > 0) {
- if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
+ int routing_id = GetRoutingId(context);
+ if (routing_id > 0) {
+ if (!ewk_ipc_plugins_message_send(routing_id, msg)) {
LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
}
}
ewk_ipc_wrt_message_data_del(msg);
}
-std::string RuntimeIPCClient::SendSyncMessage(const std::string& type,
+std::string RuntimeIPCClient::SendSyncMessage(v8::Handle<v8::Context> context,
+ const std::string& type,
const std::string& value) {
Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
- if (routing_id_ > 0) {
- if (!ewk_ipc_plugins_sync_message_send(routing_id_, msg)) {
+ int routing_id = GetRoutingId(context);
+ if (routing_id > 0) {
+ if (!ewk_ipc_plugins_sync_message_send(routing_id, msg)) {
LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
ewk_ipc_wrt_message_data_del(msg);
return std::string();
return result;
}
-void RuntimeIPCClient::SendAsyncMessage(const std::string& type,
+void RuntimeIPCClient::SendAsyncMessage(v8::Handle<v8::Context> context,
+ const std::string& type,
const std::string& value,
ReplyCallback callback) {
std::string msg_id = common::utils::GenerateUUID();
ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
- if (routing_id_ > 0) {
- if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
+ int routing_id = GetRoutingId(context);
+ if (routing_id > 0) {
+ if (!ewk_ipc_plugins_message_send(routing_id, msg)) {
LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
ewk_ipc_wrt_message_data_del(msg);
return;
static RuntimeIPCClient* GetInstance();
// Send message to BrowserProcess without reply
- void SendMessage(const std::string& type, const std::string& value);
+ void SendMessage(v8::Handle<v8::Context> context,
+ const std::string& type, const std::string& value);
// Send message to BrowserProcess synchronous with reply
- std::string SendSyncMessage(const std::string& type,
+ std::string SendSyncMessage(v8::Handle<v8::Context> context,
+ const std::string& type,
const std::string& value);
// Send message to BrowserProcess asynchronous,
// reply message will be passed to callback function.
- void SendAsyncMessage(const std::string& type, const std::string& value,
+ void SendAsyncMessage(v8::Handle<v8::Context> context,
+ const std::string& type, const std::string& value,
ReplyCallback callback);
void HandleMessageFromRuntime(const Ewk_IPC_Wrt_Message_Data* msg);
- int routing_id() const { return routing_id_; }
- void set_routing_id(int routing_id) { routing_id_ = routing_id; }
+ int GetRoutingId(v8::Handle<v8::Context> context);
+
+ void SetRoutingId(v8::Handle<v8::Context> context, int routing_id);
private:
RuntimeIPCClient();
- int routing_id_;
std::map<std::string, ReplyCallback> callbacks_;
};
}
RuntimeIPCClient* rc = RuntimeIPCClient::GetInstance();
- rc->SendMessage(std::string(*type), data_str);
+ rc->SendMessage(module->module_system_->GetV8Context(),
+ std::string(*type), data_str);
result.Set(true);
}
}
RuntimeIPCClient* rc = RuntimeIPCClient::GetInstance();
- std::string reply = rc->SendSyncMessage(std::string(*type), data_str);
+ std::string reply = rc->SendSyncMessage(
+ module->module_system_->GetV8Context(),
+ std::string(*type), data_str);
result.Set(v8::String::NewFromUtf8(isolate, reply.c_str()));
}
};
RuntimeIPCClient* rc = RuntimeIPCClient::GetInstance();
- rc->SendAsyncMessage(std::string(*type), value_str, callback);
+ rc->SendAsyncMessage(module->module_system_->GetV8Context(),
+ std::string(*type), value_str, callback);
result.Set(true);
}