Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/setup/host_starter.h" | 5 #include "remoting/host/setup/host_starter.h" |
| 6 | 6 |
| 7 #include "base/thread_task_runner_handle.h" | |
| 7 #include "base/values.h" | 8 #include "base/values.h" |
| 8 #include "crypto/random.h" | 9 #include "crypto/random.h" |
| 9 #include "google_apis/google_api_keys.h" | 10 #include "google_apis/google_api_keys.h" |
| 10 #include "remoting/host/pin_hash.h" | 11 #include "remoting/host/pin_hash.h" |
| 11 #include "remoting/host/setup/oauth_helper.h" | 12 #include "remoting/host/setup/oauth_helper.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 void AppendByte(std::string& s, uint8 byte) { | 16 void AppendByte(std::string& s, uint8 byte) { |
| 16 const char alphabet[] = "0123456789abcdef"; | 17 const char alphabet[] = "0123456789abcdef"; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 HostStarter::HostStarter( | 60 HostStarter::HostStarter( |
| 60 scoped_ptr<gaia::GaiaOAuthClient> oauth_client, | 61 scoped_ptr<gaia::GaiaOAuthClient> oauth_client, |
| 61 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher, | 62 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher, |
| 62 scoped_ptr<remoting::ServiceClient> service_client, | 63 scoped_ptr<remoting::ServiceClient> service_client, |
| 63 scoped_ptr<remoting::DaemonController> daemon_controller) | 64 scoped_ptr<remoting::DaemonController> daemon_controller) |
| 64 : oauth_client_(oauth_client.Pass()), | 65 : oauth_client_(oauth_client.Pass()), |
| 65 user_email_fetcher_(user_email_fetcher.Pass()), | 66 user_email_fetcher_(user_email_fetcher.Pass()), |
| 66 service_client_(service_client.Pass()), | 67 service_client_(service_client.Pass()), |
| 67 daemon_controller_(daemon_controller.Pass()), | 68 daemon_controller_(daemon_controller.Pass()), |
| 68 in_progress_(false), | 69 in_progress_(false), |
| 69 consent_to_data_collection_(false) { | 70 consent_to_data_collection_(false), |
| 71 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | |
| 72 weak_ptr_(weak_ptr_factory_.GetWeakPtr()) { | |
| 70 oauth_client_info_.client_id = | 73 oauth_client_info_.client_id = |
| 71 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING); | 74 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING); |
| 72 oauth_client_info_.client_secret = | 75 oauth_client_info_.client_secret = |
| 73 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING); | 76 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING); |
| 74 oauth_client_info_.redirect_uri = GetOauthRedirectUrl(); | 77 oauth_client_info_.redirect_uri = GetOauthRedirectUrl(); |
| 75 } | 78 } |
| 76 | 79 |
| 77 HostStarter::~HostStarter() { | 80 HostStarter::~HostStarter() { |
| 78 } | 81 } |
| 79 | 82 |
| 80 scoped_ptr<HostStarter> HostStarter::Create( | 83 scoped_ptr<HostStarter> HostStarter::Create( |
| 81 net::URLRequestContextGetter* url_request_context_getter) { | 84 net::URLRequestContextGetter* url_request_context_getter) { |
| 82 scoped_ptr<gaia::GaiaOAuthClient> oauth_client( | 85 scoped_ptr<gaia::GaiaOAuthClient> oauth_client( |
| 83 new gaia::GaiaOAuthClient(gaia::kGaiaOAuth2Url, | 86 new gaia::GaiaOAuthClient(gaia::kGaiaOAuth2Url, |
| 84 url_request_context_getter)); | 87 url_request_context_getter)); |
| 85 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher( | 88 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher( |
| 86 new remoting::GaiaUserEmailFetcher(url_request_context_getter)); | 89 new remoting::GaiaUserEmailFetcher(url_request_context_getter)); |
| 87 scoped_ptr<remoting::ServiceClient> service_client( | 90 scoped_ptr<remoting::ServiceClient> service_client( |
| 88 new remoting::ServiceClient(url_request_context_getter)); | 91 new remoting::ServiceClient(url_request_context_getter)); |
| 89 scoped_ptr<remoting::DaemonController> daemon_controller( | 92 scoped_ptr<remoting::DaemonController> daemon_controller( |
| 90 remoting::DaemonController::Create()); | 93 remoting::DaemonController::Create()); |
| 91 return scoped_ptr<HostStarter>( | 94 return scoped_ptr<HostStarter>( |
| 92 new HostStarter(oauth_client.Pass(), user_email_fetcher.Pass(), | 95 new HostStarter(oauth_client.Pass(), user_email_fetcher.Pass(), |
| 93 service_client.Pass(), daemon_controller.Pass())); | 96 service_client.Pass(), daemon_controller.Pass())); |
| 94 } | 97 } |
| 95 | 98 |
| 96 void HostStarter::StartHost(const std::string& host_name, | 99 void HostStarter::StartHost( |
| 97 const std::string& host_pin, | 100 const std::string& host_name, |
| 98 bool consent_to_data_collection, | 101 const std::string& host_pin, |
| 99 const std::string& auth_code, | 102 bool consent_to_data_collection, |
| 100 CompletionCallback on_done) { | 103 const std::string& auth_code, |
| 104 CompletionCallback on_done) { | |
| 101 if (in_progress_) { | 105 if (in_progress_) { |
| 102 on_done.Run(START_IN_PROGRESS); | 106 on_done.Run(START_IN_PROGRESS); |
| 103 return; | 107 return; |
| 104 } | 108 } |
| 105 in_progress_ = true; | 109 in_progress_ = true; |
| 106 host_name_ = host_name; | 110 host_name_ = host_name; |
| 107 host_pin_ = host_pin; | 111 host_pin_ = host_pin; |
| 108 consent_to_data_collection_ = consent_to_data_collection; | 112 consent_to_data_collection_ = consent_to_data_collection; |
| 109 on_done_ = on_done; | 113 on_done_ = on_done; |
| 114 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.
| |
| 110 // Map the authorization code to refresh and access tokens. | 115 // Map the authorization code to refresh and access tokens. |
| 111 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code, | 116 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code, |
| 112 kMaxGetTokensRetries, this); | 117 kMaxGetTokensRetries, this); |
| 113 } | 118 } |
| 114 | 119 |
| 115 void HostStarter::OnGetTokensResponse( | 120 void HostStarter::OnGetTokensResponse( |
| 116 const std::string& refresh_token, | 121 const std::string& refresh_token, |
| 117 const std::string& access_token, | 122 const std::string& access_token, |
| 118 int expires_in_seconds) { | 123 int expires_in_seconds) { |
| 124 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 125 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 126 &HostStarter::OnGetTokensResponse, weak_ptr_, | |
| 127 refresh_token, access_token, expires_in_seconds)); | |
| 128 return; | |
| 129 } | |
| 119 refresh_token_ = refresh_token; | 130 refresh_token_ = refresh_token; |
| 120 access_token_ = access_token; | 131 access_token_ = access_token; |
| 121 // Get the email corresponding to the access token. | 132 // Get the email corresponding to the access token. |
| 122 user_email_fetcher_->GetUserEmail(access_token_, this); | 133 user_email_fetcher_->GetUserEmail(access_token_, this); |
| 123 } | 134 } |
| 124 | 135 |
| 125 void HostStarter::OnGetUserEmailResponse(const std::string& user_email) { | 136 void HostStarter::OnGetUserEmailResponse(const std::string& user_email) { |
| 137 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 138 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 139 &HostStarter::OnGetUserEmailResponse, weak_ptr_, | |
| 140 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.
| |
| 141 return; | |
| 142 } | |
| 126 user_email_ = user_email; | 143 user_email_ = user_email; |
| 127 // Register the host. | 144 // Register the host. |
| 128 host_id_ = MakeHostId(); | 145 host_id_ = MakeHostId(); |
| 129 key_pair_.Generate(); | 146 key_pair_.Generate(); |
| 130 service_client_->RegisterHost( | 147 service_client_->RegisterHost( |
| 131 host_id_, host_name_, key_pair_.GetPublicKey(), access_token_, this); | 148 host_id_, host_name_, key_pair_.GetPublicKey(), access_token_, this); |
| 132 } | 149 } |
| 133 | 150 |
| 134 void HostStarter::OnHostRegistered() { | 151 void HostStarter::OnHostRegistered() { |
| 152 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 153 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 154 &HostStarter::OnHostRegistered, weak_ptr_)); | |
| 155 return; | |
| 156 } | |
| 135 // Start the host. | 157 // Start the host. |
| 136 std::string host_secret_hash = remoting::MakeHostPinHash(host_id_, host_pin_); | 158 std::string host_secret_hash = remoting::MakeHostPinHash(host_id_, host_pin_); |
| 137 scoped_ptr<base::DictionaryValue> config(new base::DictionaryValue()); | 159 scoped_ptr<base::DictionaryValue> config(new base::DictionaryValue()); |
| 138 config->SetString("xmpp_login", user_email_); | 160 config->SetString("xmpp_login", user_email_); |
| 139 config->SetString("oauth_refresh_token", refresh_token_); | 161 config->SetString("oauth_refresh_token", refresh_token_); |
| 140 config->SetString("host_id", host_id_); | 162 config->SetString("host_id", host_id_); |
| 141 config->SetString("host_name", host_name_); | 163 config->SetString("host_name", host_name_); |
| 142 config->SetString("private_key", key_pair_.GetAsString()); | 164 config->SetString("private_key", key_pair_.GetAsString()); |
| 143 config->SetString("host_secret_hash", host_secret_hash); | 165 config->SetString("host_secret_hash", host_secret_hash); |
| 144 daemon_controller_->SetConfigAndStart( | 166 daemon_controller_->SetConfigAndStart( |
| 145 config.Pass(), consent_to_data_collection_, | 167 config.Pass(), consent_to_data_collection_, |
| 146 base::Bind(&HostStarter::OnHostStarted, | 168 base::Bind(&HostStarter::OnHostStarted, base::Unretained(this))); |
| 147 base::Unretained(this))); | |
| 148 } | 169 } |
| 149 | 170 |
| 150 void HostStarter::OnHostStarted(DaemonController::AsyncResult result) { | 171 void HostStarter::OnHostStarted(DaemonController::AsyncResult result) { |
| 151 on_done_.Run( | 172 if (!main_task_runner_->BelongsToCurrentThread()) { |
| 152 (result == DaemonController::RESULT_OK) ? START_COMPLETE : START_ERROR); | 173 main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 174 &HostStarter::OnHostStarted, weak_ptr_, result)); | |
| 175 return; | |
| 176 } | |
| 177 Result done_result = START_ERROR; | |
| 178 if (result == DaemonController::RESULT_OK) | |
| 179 done_result = START_COMPLETE; | |
| 180 on_done_.Run(done_result); | |
| 153 // TODO(simonmorris): Unregister the host if we didn't start it. | 181 // TODO(simonmorris): Unregister the host if we didn't start it. |
| 154 in_progress_ = false; | 182 in_progress_ = false; |
| 155 } | 183 } |
| 156 | 184 |
| 157 void HostStarter::OnOAuthError() { | 185 void HostStarter::OnOAuthError() { |
| 186 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 187 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 188 &HostStarter::OnOAuthError, weak_ptr_)); | |
| 189 return; | |
| 190 } | |
| 158 on_done_.Run(OAUTH_ERROR); | 191 on_done_.Run(OAUTH_ERROR); |
| 159 in_progress_ = false; | 192 in_progress_ = false; |
| 160 } | 193 } |
| 161 | 194 |
| 162 void HostStarter::OnNetworkError(int response_code) { | 195 void HostStarter::OnNetworkError(int response_code) { |
| 196 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 197 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 198 &HostStarter::OnNetworkError, weak_ptr_, response_code)); | |
| 199 return; | |
| 200 } | |
| 163 on_done_.Run(NETWORK_ERROR); | 201 on_done_.Run(NETWORK_ERROR); |
| 164 in_progress_ = false; | 202 in_progress_ = false; |
| 165 } | 203 } |
| 166 | 204 |
| 167 void HostStarter::OnRefreshTokenResponse( | 205 void HostStarter::OnRefreshTokenResponse( |
| 168 const std::string& access_token, | 206 const std::string& access_token, |
| 169 int expires_in_seconds) { | 207 int expires_in_seconds) { |
| 170 NOTREACHED(); | 208 NOTREACHED(); |
| 209 if (!main_task_runner_->BelongsToCurrentThread()) { | |
| 210 main_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 211 &HostStarter::OnRefreshTokenResponse, weak_ptr_, | |
| 212 access_token, expires_in_seconds)); | |
| 213 return; | |
| 214 } | |
| 171 on_done_.Run(OAUTH_ERROR); | 215 on_done_.Run(OAUTH_ERROR); |
| 172 in_progress_ = false; | 216 in_progress_ = false; |
| 173 } | 217 } |
| 174 | 218 |
| 175 } // namespace remoting | 219 } // namespace remoting |
| OLD | NEW |