Index: remoting/host/setup/me2me_native_messaging_host.cc |
diff --git a/remoting/host/setup/me2me_native_messaging_host.cc b/remoting/host/setup/me2me_native_messaging_host.cc |
index f3bf180e3094a0086325055a5ba56d5a9e6f467a..b641483889517b857b341dd214daf1e645595230 100644 |
--- a/remoting/host/setup/me2me_native_messaging_host.cc |
+++ b/remoting/host/setup/me2me_native_messaging_host.cc |
@@ -9,6 +9,7 @@ |
#include "base/basictypes.h" |
#include "base/bind.h" |
#include "base/callback.h" |
+#include "base/callback_helpers.h" |
#include "base/logging.h" |
#include "base/strings/stringize_macros.h" |
#include "base/threading/thread.h" |
@@ -63,15 +64,37 @@ NativeMessagingHost::NativeMessagingHost( |
} |
NativeMessagingHost::~NativeMessagingHost() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
} |
-void NativeMessagingHost::SetSendMessageCallback( |
- const SendMessageCallback& send_message) { |
- send_message_ = send_message; |
+void NativeMessagingHost::Start( |
+ base::PlatformFile input, |
+ base::PlatformFile output, |
+ const base::Closure& quit_closure) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ // Set up the native messaging channel. |
+ channel_.reset( |
+ new NativeMessagingChannel( |
+ base::Bind(&NativeMessagingHost::ProcessMessage, weak_ptr_), |
+ input, |
+ output)); |
+ |
+ quit_closure_ = quit_closure; |
+ channel_->Start(base::Bind(&NativeMessagingHost::ShutDown, weak_ptr_)); |
+} |
+ |
+void NativeMessagingHost::ShutDown() { |
Sergey Ulanov
2013/12/12 19:10:28
Don't need this method. Just pass quit_closure_ to
weitao
2013/12/12 23:33:48
Done.
|
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ if (!quit_closure_.is_null()) |
+ base::ResetAndReturn(&quit_closure_).Run(); |
} |
void NativeMessagingHost::ProcessMessage( |
scoped_ptr<base::DictionaryValue> message) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
// If the client supplies an ID, it will expect it in the response. This |
@@ -83,7 +106,7 @@ void NativeMessagingHost::ProcessMessage( |
std::string type; |
if (!message->GetString("type", &type)) { |
LOG(ERROR) << "'type' not found"; |
- send_message_.Run(scoped_ptr<base::DictionaryValue>()); |
+ channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); |
return; |
} |
@@ -125,24 +148,28 @@ void NativeMessagingHost::ProcessMessage( |
} |
if (!success) |
- send_message_.Run(scoped_ptr<base::DictionaryValue>()); |
+ channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); |
} |
bool NativeMessagingHost::ProcessHello( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetString("version", STRINGIZE(VERSION)); |
scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
supported_features_list->AppendStrings(std::vector<std::string>( |
kSupportedFeatures, kSupportedFeatures + arraysize(kSupportedFeatures))); |
response->Set("supportedFeatures", supported_features_list.release()); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessClearPairedClients( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
if (pairing_registry_) { |
pairing_registry_->ClearAllPairings( |
base::Bind(&NativeMessagingHost::SendBooleanResult, weak_ptr_, |
@@ -156,6 +183,8 @@ bool NativeMessagingHost::ProcessClearPairedClients( |
bool NativeMessagingHost::ProcessDeletePairedClient( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
std::string client_id; |
if (!message.GetString(protocol::PairingRegistry::kClientIdKey, &client_id)) { |
LOG(ERROR) << "'" << protocol::PairingRegistry::kClientIdKey |
@@ -176,14 +205,18 @@ bool NativeMessagingHost::ProcessDeletePairedClient( |
bool NativeMessagingHost::ProcessGetHostName( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetString("hostname", net::GetHostName()); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessGetPinHash( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
std::string host_id; |
if (!message.GetString("hostId", &host_id)) { |
LOG(ERROR) << "'hostId' not found: " << message; |
@@ -195,23 +228,27 @@ bool NativeMessagingHost::ProcessGetPinHash( |
return false; |
} |
response->SetString("hash", MakeHostPinHash(host_id, pin)); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessGenerateKeyPair( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::Generate(); |
response->SetString("privateKey", key_pair->ToString()); |
response->SetString("publicKey", key_pair->GetPublicKey()); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessUpdateDaemonConfig( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
scoped_ptr<base::DictionaryValue> config_dict = |
ConfigDictionaryFromMessage(message); |
if (!config_dict) |
@@ -227,6 +264,8 @@ bool NativeMessagingHost::ProcessUpdateDaemonConfig( |
bool NativeMessagingHost::ProcessGetDaemonConfig( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
daemon_controller_->GetConfig( |
base::Bind(&NativeMessagingHost::SendConfigResponse, weak_ptr_, |
base::Passed(&response))); |
@@ -236,6 +275,8 @@ bool NativeMessagingHost::ProcessGetDaemonConfig( |
bool NativeMessagingHost::ProcessGetPairedClients( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
if (pairing_registry_) { |
pairing_registry_->GetAllPairings( |
base::Bind(&NativeMessagingHost::SendPairedClientsResponse, weak_ptr_, |
@@ -250,6 +291,8 @@ bool NativeMessagingHost::ProcessGetPairedClients( |
bool NativeMessagingHost::ProcessGetUsageStatsConsent( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
daemon_controller_->GetUsageStatsConsent( |
base::Bind(&NativeMessagingHost::SendUsageStatsConsentResponse, |
weak_ptr_, base::Passed(&response))); |
@@ -259,6 +302,8 @@ bool NativeMessagingHost::ProcessGetUsageStatsConsent( |
bool NativeMessagingHost::ProcessStartDaemon( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
bool consent; |
if (!message.GetBoolean("consent", &consent)) { |
LOG(ERROR) << "'consent' not found."; |
@@ -280,6 +325,8 @@ bool NativeMessagingHost::ProcessStartDaemon( |
bool NativeMessagingHost::ProcessStopDaemon( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
daemon_controller_->Stop( |
base::Bind(&NativeMessagingHost::SendAsyncResult, weak_ptr_, |
base::Passed(&response))); |
@@ -289,6 +336,8 @@ bool NativeMessagingHost::ProcessStopDaemon( |
bool NativeMessagingHost::ProcessGetDaemonState( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
DaemonController::State state = daemon_controller_->GetState(); |
switch (state) { |
case DaemonController::STATE_NOT_IMPLEMENTED: |
@@ -316,22 +365,26 @@ bool NativeMessagingHost::ProcessGetDaemonState( |
response->SetString("state", "UNKNOWN"); |
break; |
} |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessGetHostClientId( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetString("clientId", google_apis::GetOAuth2ClientID( |
google_apis::CLIENT_REMOTING_HOST)); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
return true; |
} |
bool NativeMessagingHost::ProcessGetCredentialsFromAuthCode( |
const base::DictionaryValue& message, |
scoped_ptr<base::DictionaryValue> response) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
std::string auth_code; |
if (!message.GetString("authorizationCode", &auth_code)) { |
LOG(ERROR) << "'authorizationCode' string not found."; |
@@ -355,33 +408,41 @@ bool NativeMessagingHost::ProcessGetCredentialsFromAuthCode( |
void NativeMessagingHost::SendConfigResponse( |
scoped_ptr<base::DictionaryValue> response, |
scoped_ptr<base::DictionaryValue> config) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
if (config) { |
response->Set("config", config.release()); |
} else { |
response->Set("config", Value::CreateNullValue()); |
} |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
void NativeMessagingHost::SendPairedClientsResponse( |
scoped_ptr<base::DictionaryValue> response, |
scoped_ptr<base::ListValue> pairings) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->Set("pairedClients", pairings.release()); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
void NativeMessagingHost::SendUsageStatsConsentResponse( |
scoped_ptr<base::DictionaryValue> response, |
const DaemonController::UsageStatsConsent& consent) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetBoolean("supported", consent.supported); |
response->SetBoolean("allowed", consent.allowed); |
response->SetBoolean("setByPolicy", consent.set_by_policy); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
void NativeMessagingHost::SendAsyncResult( |
scoped_ptr<base::DictionaryValue> response, |
DaemonController::AsyncResult result) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
switch (result) { |
case DaemonController::RESULT_OK: |
response->SetString("result", "OK"); |
@@ -396,22 +457,26 @@ void NativeMessagingHost::SendAsyncResult( |
response->SetString("result", "FAILED_DIRECTORY"); |
break; |
} |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
void NativeMessagingHost::SendBooleanResult( |
scoped_ptr<base::DictionaryValue> response, bool result) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetBoolean("result", result); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
void NativeMessagingHost::SendCredentialsResponse( |
scoped_ptr<base::DictionaryValue> response, |
const std::string& user_email, |
const std::string& refresh_token) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
response->SetString("userEmail", user_email); |
response->SetString("refreshToken", refresh_token); |
- send_message_.Run(response.Pass()); |
+ channel_->SendMessage(response.Pass()); |
} |
} // namespace remoting |