Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / it2me / it2me_native_messaging_host.cc
index 6fb65df..93286b3 100644 (file)
@@ -9,15 +9,15 @@
 #include "base/basictypes.h"
 #include "base/bind.h"
 #include "base/callback.h"
-#include "base/callback_helpers.h"
-#include "base/message_loop/message_loop.h"
-#include "base/run_loop.h"
+#include "base/json/json_reader.h"
+#include "base/json/json_writer.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/stringize_macros.h"
 #include "base/threading/thread.h"
 #include "base/values.h"
+#include "media/base/media.h"
 #include "net/base/net_util.h"
-#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_request_context_getter.h"
 #include "remoting/base/auth_token_util.h"
 #include "remoting/base/service_urls.h"
 #include "remoting/host/chromoting_host_context.h"
@@ -36,24 +36,22 @@ const remoting::protocol::NameMapElement<It2MeHostState> kIt2MeHostStates[] = {
     {kConnected, "CONNECTED"},
     {kDisconnecting, "DISCONNECTING"},
     {kError, "ERROR"},
-    {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, };
+    {kInvalidDomainError, "INVALID_DOMAIN_ERROR"},
+};
 
 }  // namespace
 
 It2MeNativeMessagingHost::It2MeNativeMessagingHost(
-    scoped_refptr<AutoThreadTaskRunner> task_runner,
-    scoped_ptr<extensions::NativeMessagingChannel> channel,
+    scoped_ptr<ChromotingHostContext> context,
     scoped_ptr<It2MeHostFactory> factory)
-    : channel_(channel.Pass()),
+    : client_(NULL),
+      host_context_(context.Pass()),
       factory_(factory.Pass()),
       weak_factory_(this) {
   weak_ptr_ = weak_factory_.GetWeakPtr();
 
-  // Initialize the host context to manage the threads for the it2me host.
-  // The native messaging host, rather than the It2MeHost object, owns and
-  // maintains the lifetime of the host context.
-
-  host_context_.reset(ChromotingHostContext::Create(task_runner).release());
+  // Ensures runtime specific CPU features are initialized.
+  media::InitializeCPUSpecificMediaFeatures();
 
   const ServiceUrls* service_urls = ServiceUrls::GetInstance();
   const bool xmpp_server_valid =
@@ -75,21 +73,19 @@ It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {
   }
 }
 
-void It2MeNativeMessagingHost::Start(const base::Closure& quit_closure) {
+void It2MeNativeMessagingHost::OnMessage(const std::string& message) {
   DCHECK(task_runner()->BelongsToCurrentThread());
-  DCHECK(!quit_closure.is_null());
-
-  quit_closure_ = quit_closure;
-
-  channel_->Start(this);
-}
 
-void It2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) {
-  DCHECK(task_runner()->BelongsToCurrentThread());
+  scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
+  scoped_ptr<base::Value> message_value(base::JSONReader::Read(message));
+  if (!message_value->IsType(base::Value::TYPE_DICTIONARY)) {
+    LOG(ERROR) << "Received a message that's not a dictionary.";
+    client_->CloseChannel(std::string());
+    return;
+  }
 
   scoped_ptr<base::DictionaryValue> message_dict(
-      static_cast<base::DictionaryValue*>(message.release()));
-  scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
+      static_cast<base::DictionaryValue*>(message_value.release()));
 
   // If the client supplies an ID, it will expect it in the response. This
   // might be a string or a number, so cope with both.
@@ -116,9 +112,17 @@ void It2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) {
   }
 }
 
-void It2MeNativeMessagingHost::OnDisconnect() {
-  if (!quit_closure_.is_null())
-    base::ResetAndReturn(&quit_closure_).Run();
+void It2MeNativeMessagingHost::Start(Client* client) {
+  DCHECK(task_runner()->BelongsToCurrentThread());
+  client_ = client;
+}
+
+void It2MeNativeMessagingHost::SendMessageToClient(
+    scoped_ptr<base::DictionaryValue> message) const {
+  DCHECK(task_runner()->BelongsToCurrentThread());
+  std::string message_json;
+  base::JSONWriter::Write(message.get(), &message_json);
+  client_->PostMessageFromNativeHost(message_json);
 }
 
 void It2MeNativeMessagingHost::ProcessHello(
@@ -132,7 +136,7 @@ void It2MeNativeMessagingHost::ProcessHello(
   scoped_ptr<base::ListValue> supported_features_list(new base::ListValue());
   response->Set("supportedFeatures", supported_features_list.release());
 
-  channel_->SendMessage(response.PassAs<base::Value>());
+  SendMessageToClient(response.Pass());
 }
 
 void It2MeNativeMessagingHost::ProcessConnect(
@@ -199,14 +203,13 @@ void It2MeNativeMessagingHost::ProcessConnect(
 #endif  // !defined(NDEBUG)
 
   // Create the It2Me host and start connecting.
-  it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(),
-                                          host_context_->ui_task_runner(),
+  it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(),
                                           weak_ptr_,
                                           xmpp_config,
                                           directory_bot_jid_);
   it2me_host_->Connect();
 
-  channel_->SendMessage(response.PassAs<base::Value>());
+  SendMessageToClient(response.Pass());
 }
 
 void It2MeNativeMessagingHost::ProcessDisconnect(
@@ -218,7 +221,7 @@ void It2MeNativeMessagingHost::ProcessDisconnect(
     it2me_host_->Disconnect();
     it2me_host_ = NULL;
   }
-  channel_->SendMessage(response.PassAs<base::Value>());
+  SendMessageToClient(response.Pass());
 }
 
 void It2MeNativeMessagingHost::SendErrorAndExit(
@@ -230,10 +233,10 @@ void It2MeNativeMessagingHost::SendErrorAndExit(
 
   response->SetString("type", "error");
   response->SetString("description", description);
-  channel_->SendMessage(response.PassAs<base::Value>());
+  SendMessageToClient(response.Pass());
 
-  // Trigger a host shutdown by sending a NULL message.
-  channel_->SendMessage(scoped_ptr<base::Value>());
+  // Trigger a host shutdown by sending an empty message.
+  client_->CloseChannel(std::string());
 }
 
 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) {
@@ -266,7 +269,7 @@ void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) {
       ;
   }
 
-  channel_->SendMessage(message.PassAs<base::Value>());
+  SendMessageToClient(message.Pass());
 }
 
 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) {
@@ -276,7 +279,7 @@ void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) {
 
   message->SetString("type", "natPolicyChanged");
   message->SetBoolean("natTraversalEnabled", nat_traversal_enabled);
-  channel_->SendMessage(message.PassAs<base::Value>());
+  SendMessageToClient(message.Pass());
 }
 
 // Stores the Access Code for the web-app to query.
@@ -297,7 +300,7 @@ void It2MeNativeMessagingHost::OnClientAuthenticated(
   client_username_ = client_username;
 }
 
-scoped_refptr<AutoThreadTaskRunner>
+scoped_refptr<base::SingleThreadTaskRunner>
 It2MeNativeMessagingHost::task_runner() const {
   return host_context_->ui_task_runner();
 }