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..ad0c15ed549ae8fc6aa7fb8e70fc2ae56dcdf865 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> |
Sergey Ulanov
2015/08/08 00:57:09
don't need this include.
nit: add an empty line he
|
#include <string> |
#include "base/basictypes.h" |
@@ -107,31 +108,34 @@ void Me2MeNativeMessagingHost::Start( |
void Me2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
+ |
if (!message->IsType(base::Value::TYPE_DICTIONARY)) { |
- LOG(ERROR) << "Received a message that's not a dictionary."; |
- channel_->SendMessage(nullptr); |
+ std::string error_message = "Received a message that's not a dictionary."; |
+ LOG(ERROR) << error_message; |
+ SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
return; |
} |
scoped_ptr<base::DictionaryValue> message_dict( |
static_cast<base::DictionaryValue*>(message.release())); |
- scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
- |
- // 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. |
- const base::Value* id; |
- if (message_dict->Get("id", &id)) |
- response->Set("id", id->DeepCopy()); |
- |
std::string type; |
if (!message_dict->GetString("type", &type)) { |
- LOG(ERROR) << "'type' not found"; |
- channel_->SendMessage(nullptr); |
+ std::string error_message = "'type' not found"; |
+ LOG(ERROR) << error_message; |
+ SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
return; |
} |
+ // Message response types are just the message type with "Response" appended. |
response->SetString("type", type + "Response"); |
+ // 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. |
+ const base::Value* id; |
+ if (message_dict->Get("id", &id)) |
+ response->Set("id", id->DeepCopy()); |
+ |
if (type == "hello") { |
ProcessHello(message_dict.Pass(), response.Pass()); |
} else if (type == "clearPairedClients") { |
@@ -167,8 +171,9 @@ void Me2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) { |
ProcessGetCredentialsFromAuthCode( |
message_dict.Pass(), response.Pass(), false); |
} else { |
- LOG(ERROR) << "Unsupported request type: " << type; |
- OnError(); |
+ std::string error_message = "Unsupported request type: " + type; |
+ LOG(ERROR) << error_message; |
+ SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
} |
} |
@@ -224,9 +229,10 @@ 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::string error_message = std::string("'") + |
+ protocol::PairingRegistry::kClientIdKey + "' string not found"; |
+ LOG(ERROR) << error_message; |
+ SendAsyncFailure(response.Pass(), error_message, FROM_HERE); |
return; |
} |
@@ -255,14 +261,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 +294,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 +361,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 +401,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 +464,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 +515,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"); |
Sergey Ulanov
2015/08/08 00:57:09
Do we still send "CANCELLED" response? I think it
Jamie
2015/08/11 21:23:13
I've reverted this change, but FWIW I don't think
|
- 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 +556,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()); |