Chromium Code Reviews| Index: remoting/host/setup/host_starter.cc |
| diff --git a/remoting/host/setup/host_starter.cc b/remoting/host/setup/host_starter.cc |
| index 31847fe8eb069b7a70f15cce33e4f5bab139e197..d3347a185c288db286dff105945aa358f62b1a84 100644 |
| --- a/remoting/host/setup/host_starter.cc |
| +++ b/remoting/host/setup/host_starter.cc |
| @@ -4,6 +4,7 @@ |
| #include "remoting/host/setup/host_starter.h" |
| +#include "base/thread_task_runner_handle.h" |
| #include "base/values.h" |
| #include "crypto/random.h" |
| #include "google_apis/google_api_keys.h" |
| @@ -66,7 +67,9 @@ HostStarter::HostStarter( |
| service_client_(service_client.Pass()), |
| daemon_controller_(daemon_controller.Pass()), |
| in_progress_(false), |
| - consent_to_data_collection_(false) { |
| + consent_to_data_collection_(false), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| + weak_ptr_(weak_ptr_factory_.GetWeakPtr()) { |
| oauth_client_info_.client_id = |
| google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING); |
| oauth_client_info_.client_secret = |
| @@ -93,11 +96,12 @@ scoped_ptr<HostStarter> HostStarter::Create( |
| service_client.Pass(), daemon_controller.Pass())); |
| } |
| -void HostStarter::StartHost(const std::string& host_name, |
| - const std::string& host_pin, |
| - bool consent_to_data_collection, |
| - const std::string& auth_code, |
| - CompletionCallback on_done) { |
| +void HostStarter::StartHost( |
| + const std::string& host_name, |
| + const std::string& host_pin, |
| + bool consent_to_data_collection, |
| + const std::string& auth_code, |
| + CompletionCallback on_done) { |
| if (in_progress_) { |
| on_done.Run(START_IN_PROGRESS); |
| return; |
| @@ -107,6 +111,7 @@ void HostStarter::StartHost(const std::string& host_name, |
| host_pin_ = host_pin; |
| consent_to_data_collection_ = consent_to_data_collection; |
| on_done_ = on_done; |
| + main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
|
Sergey Ulanov
2012/10/17 18:34:14
Maybe do it in the constructor and add DCHECK here
simonmorris
2012/10/17 18:49:46
Done.
|
| // Map the authorization code to refresh and access tokens. |
| oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code, |
| kMaxGetTokensRetries, this); |
| @@ -116,6 +121,12 @@ void HostStarter::OnGetTokensResponse( |
| const std::string& refresh_token, |
| const std::string& access_token, |
| int expires_in_seconds) { |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnGetTokensResponse, weak_ptr_, |
| + refresh_token, access_token, expires_in_seconds)); |
| + return; |
| + } |
| refresh_token_ = refresh_token; |
| access_token_ = access_token; |
| // Get the email corresponding to the access token. |
| @@ -123,6 +134,12 @@ void HostStarter::OnGetTokensResponse( |
| } |
| void HostStarter::OnGetUserEmailResponse(const std::string& user_email) { |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnGetUserEmailResponse, weak_ptr_, |
| + user_email)); |
|
Sergey Ulanov
2012/10/17 18:34:14
nit: Will this fit on the previous line?
simonmorris
2012/10/17 18:49:46
Done.
|
| + return; |
| + } |
| user_email_ = user_email; |
| // Register the host. |
| host_id_ = MakeHostId(); |
| @@ -132,6 +149,11 @@ void HostStarter::OnGetUserEmailResponse(const std::string& user_email) { |
| } |
| void HostStarter::OnHostRegistered() { |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnHostRegistered, weak_ptr_)); |
| + return; |
| + } |
| // Start the host. |
| std::string host_secret_hash = remoting::MakeHostPinHash(host_id_, host_pin_); |
| scoped_ptr<base::DictionaryValue> config(new base::DictionaryValue()); |
| @@ -143,23 +165,39 @@ void HostStarter::OnHostRegistered() { |
| config->SetString("host_secret_hash", host_secret_hash); |
| daemon_controller_->SetConfigAndStart( |
| config.Pass(), consent_to_data_collection_, |
| - base::Bind(&HostStarter::OnHostStarted, |
| - base::Unretained(this))); |
| + base::Bind(&HostStarter::OnHostStarted, base::Unretained(this))); |
| } |
| void HostStarter::OnHostStarted(DaemonController::AsyncResult result) { |
| - on_done_.Run( |
| - (result == DaemonController::RESULT_OK) ? START_COMPLETE : START_ERROR); |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnHostStarted, weak_ptr_, result)); |
| + return; |
| + } |
| + Result done_result = START_ERROR; |
| + if (result == DaemonController::RESULT_OK) |
| + done_result = START_COMPLETE; |
| + on_done_.Run(done_result); |
| // TODO(simonmorris): Unregister the host if we didn't start it. |
| in_progress_ = false; |
| } |
| void HostStarter::OnOAuthError() { |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnOAuthError, weak_ptr_)); |
| + return; |
| + } |
| on_done_.Run(OAUTH_ERROR); |
| in_progress_ = false; |
| } |
| void HostStarter::OnNetworkError(int response_code) { |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnNetworkError, weak_ptr_, response_code)); |
| + return; |
| + } |
| on_done_.Run(NETWORK_ERROR); |
| in_progress_ = false; |
| } |
| @@ -168,6 +206,12 @@ void HostStarter::OnRefreshTokenResponse( |
| const std::string& access_token, |
| int expires_in_seconds) { |
| NOTREACHED(); |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &HostStarter::OnRefreshTokenResponse, weak_ptr_, |
| + access_token, expires_in_seconds)); |
| + return; |
| + } |
| on_done_.Run(OAUTH_ERROR); |
| in_progress_ = false; |
| } |