Chromium Code Reviews| 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 d2957fdf8c5a0c276be641e503aec965714f72f1..d46d03ffc347df186a44a75c16b6061b34920c4c 100644 |
| --- a/remoting/host/setup/me2me_native_messaging_host.cc |
| +++ b/remoting/host/setup/me2me_native_messaging_host.cc |
| @@ -3,6 +3,7 @@ |
| // found in the LICENSE file. |
| #include "remoting/host/setup/me2me_native_messaging_host.h" |
| +#include <sstream> |
| #include <string> |
| #include "base/basictypes.h" |
| @@ -167,8 +168,10 @@ void Me2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) { |
| ProcessGetCredentialsFromAuthCode( |
| message_dict.Pass(), response.Pass(), false); |
| } else { |
| - LOG(ERROR) << "Unsupported request type: " << type; |
| - OnError(); |
| + std::ostringstream error_message; |
| + error_message << "Unsupported request type: " << type; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message.str(), FROM_HERE); |
| } |
| } |
| @@ -224,9 +227,11 @@ void Me2MeNativeMessagingHost::ProcessDeletePairedClient( |
| std::string client_id; |
| if (!message->GetString(protocol::PairingRegistry::kClientIdKey, |
| &client_id)) { |
| - LOG(ERROR) << "'" << protocol::PairingRegistry::kClientIdKey |
| - << "' string not found."; |
| - OnError(); |
| + std::ostringstream error_message; |
|
Sergey Ulanov
2015/08/06 22:40:57
This code may be simpler with base::StringPrintf()
Jamie
2015/08/06 23:06:17
I'm not a fan of printf, but the +-operator would
|
| + error_message << "'" << protocol::PairingRegistry::kClientIdKey |
| + << "' string not found"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message.str(), FROM_HERE); |
| return; |
| } |
| @@ -255,14 +260,16 @@ void Me2MeNativeMessagingHost::ProcessGetPinHash( |
| std::string host_id; |
| if (!message->GetString("hostId", &host_id)) { |
| - LOG(ERROR) << "'hostId' not found: " << message; |
| - OnError(); |
| + std::string error_message = "'hostId' not found"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| std::string pin; |
| if (!message->GetString("pin", &pin)) { |
| - LOG(ERROR) << "'pin' not found: " << message; |
| - OnError(); |
| + std::string error_message = "'pin' not found"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| response->SetString("hash", MakeHostPinHash(host_id, pin)); |
| @@ -286,22 +293,30 @@ void Me2MeNativeMessagingHost::ProcessUpdateDaemonConfig( |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| if (needs_elevation_) { |
| - if (!DelegateToElevatedHost(message.Pass())) |
| - SendAsyncResult(response.Pass(), DaemonController::RESULT_FAILED); |
| + if (!DelegateToElevatedHost(message.Pass())) { |
| + std::string error_message = "DelegateToElevatedHost failed"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| + } |
| return; |
| } |
| scoped_ptr<base::DictionaryValue> config_dict = |
| ConfigDictionaryFromMessage(message.Pass()); |
| if (!config_dict) { |
| - OnError(); |
| + std::string error_message = "No config dictionary in message"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| + scoped_ptr<base::DictionaryValue> response_copy(response->CreateDeepCopy()); |
| daemon_controller_->UpdateConfig( |
| config_dict.Pass(), |
| - base::Bind(&Me2MeNativeMessagingHost::SendAsyncResult, weak_ptr_, |
| - base::Passed(&response))); |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncSuccess, weak_ptr_, |
| + base::Passed(&response)), |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncFailure, weak_ptr_, |
| + base::Passed(&response_copy))); |
| } |
| void Me2MeNativeMessagingHost::ProcessGetDaemonConfig( |
| @@ -345,29 +360,38 @@ void Me2MeNativeMessagingHost::ProcessStartDaemon( |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| if (needs_elevation_) { |
| - if (!DelegateToElevatedHost(message.Pass())) |
| - SendAsyncResult(response.Pass(), DaemonController::RESULT_FAILED); |
| + if (!DelegateToElevatedHost(message.Pass())) { |
| + std::string error_message = "DelegateToElevatedHost failed"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| + } |
| return; |
| } |
| bool consent; |
| if (!message->GetBoolean("consent", &consent)) { |
| - LOG(ERROR) << "'consent' not found."; |
| - OnError(); |
| + std::string error_message = "'consent' not found"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| scoped_ptr<base::DictionaryValue> config_dict = |
| ConfigDictionaryFromMessage(message.Pass()); |
| if (!config_dict) { |
| - OnError(); |
| + std::string error_message = "No config dictionary in message"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| + scoped_ptr<base::DictionaryValue> response_copy(response->CreateDeepCopy()); |
| daemon_controller_->SetConfigAndStart( |
| config_dict.Pass(), consent, |
| - base::Bind(&Me2MeNativeMessagingHost::SendAsyncResult, weak_ptr_, |
| - base::Passed(&response))); |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncSuccess, weak_ptr_, |
| + base::Passed(&response)), |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncFailure, weak_ptr_, |
| + base::Passed(&response_copy))); |
| } |
| void Me2MeNativeMessagingHost::ProcessStopDaemon( |
| @@ -376,14 +400,20 @@ void Me2MeNativeMessagingHost::ProcessStopDaemon( |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| if (needs_elevation_) { |
| - if (!DelegateToElevatedHost(message.Pass())) |
| - SendAsyncResult(response.Pass(), DaemonController::RESULT_FAILED); |
| + if (!DelegateToElevatedHost(message.Pass())) { |
| + std::string error_message = "DelegateToElevatedHost failed"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| + } |
| return; |
| } |
| + scoped_ptr<base::DictionaryValue> response_copy(response->CreateDeepCopy()); |
| daemon_controller_->Stop( |
| - base::Bind(&Me2MeNativeMessagingHost::SendAsyncResult, weak_ptr_, |
| - base::Passed(&response))); |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncSuccess, weak_ptr_, |
| + base::Passed(&response)), |
| + base::Bind(&Me2MeNativeMessagingHost::SendAsyncFailure, weak_ptr_, |
| + base::Passed(&response_copy))); |
| } |
| void Me2MeNativeMessagingHost::ProcessGetDaemonState( |
| @@ -433,8 +463,9 @@ void Me2MeNativeMessagingHost::ProcessGetCredentialsFromAuthCode( |
| std::string auth_code; |
| if (!message->GetString("authorizationCode", &auth_code)) { |
| - LOG(ERROR) << "'authorizationCode' string not found."; |
| - OnError(); |
| + std::string error_message = "'authorizationCode' string not found"; |
| + LOG(ERROR) << error_message; |
| + SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
| return; |
| } |
| @@ -483,25 +514,23 @@ void Me2MeNativeMessagingHost::SendUsageStatsConsentResponse( |
| channel_->SendMessage(response.Pass()); |
| } |
| -void Me2MeNativeMessagingHost::SendAsyncResult( |
| +void Me2MeNativeMessagingHost::SendAsyncSuccess( |
| + scoped_ptr<base::DictionaryValue> response) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + response->SetString("result", "OK"); |
| + channel_->SendMessage(response.Pass()); |
| +} |
| + |
| +void Me2MeNativeMessagingHost::SendAsyncFailure( |
| scoped_ptr<base::DictionaryValue> response, |
| - DaemonController::AsyncResult result) { |
| + const std::string& error_message, |
| + const tracked_objects::Location& location) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - switch (result) { |
| - case DaemonController::RESULT_OK: |
| - response->SetString("result", "OK"); |
| - break; |
| - case DaemonController::RESULT_FAILED: |
| - response->SetString("result", "FAILED"); |
| - break; |
| - case DaemonController::RESULT_CANCELLED: |
| - response->SetString("result", "CANCELLED"); |
| - break; |
| - case DaemonController::RESULT_FAILED_DIRECTORY: |
| - response->SetString("result", "FAILED_DIRECTORY"); |
| - break; |
| - } |
| + response->SetString("result", "FAILED"); |
| + response->SetString("error_message", error_message); |
| + response->SetString("error_location", location.ToString()); |
| channel_->SendMessage(response.Pass()); |
| } |
| @@ -526,11 +555,6 @@ void Me2MeNativeMessagingHost::SendCredentialsResponse( |
| channel_->SendMessage(response.Pass()); |
| } |
| -void Me2MeNativeMessagingHost::OnError() { |
| - // Trigger a host shutdown by sending a nullptr message. |
| - channel_->SendMessage(nullptr); |
| -} |
| - |
| void Me2MeNativeMessagingHost::Stop() { |
| DCHECK(thread_checker_.CalledOnValidThread()); |