Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: remoting/host/setup/host_starter.cc

Issue 11090063: [Chromoting] Add a simple Windows app that registers and starts a host. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix non-standard resource header file. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/host/setup/host_starter.h ('k') | remoting/host/setup/pin_validator.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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();
78 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
75 } 79 }
76 80
77 HostStarter::~HostStarter() { 81 HostStarter::~HostStarter() {
78 } 82 }
79 83
80 scoped_ptr<HostStarter> HostStarter::Create( 84 scoped_ptr<HostStarter> HostStarter::Create(
81 net::URLRequestContextGetter* url_request_context_getter) { 85 net::URLRequestContextGetter* url_request_context_getter) {
82 scoped_ptr<gaia::GaiaOAuthClient> oauth_client( 86 scoped_ptr<gaia::GaiaOAuthClient> oauth_client(
83 new gaia::GaiaOAuthClient(gaia::kGaiaOAuth2Url, 87 new gaia::GaiaOAuthClient(gaia::kGaiaOAuth2Url,
84 url_request_context_getter)); 88 url_request_context_getter));
85 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher( 89 scoped_ptr<remoting::GaiaUserEmailFetcher> user_email_fetcher(
86 new remoting::GaiaUserEmailFetcher(url_request_context_getter)); 90 new remoting::GaiaUserEmailFetcher(url_request_context_getter));
87 scoped_ptr<remoting::ServiceClient> service_client( 91 scoped_ptr<remoting::ServiceClient> service_client(
88 new remoting::ServiceClient(url_request_context_getter)); 92 new remoting::ServiceClient(url_request_context_getter));
89 scoped_ptr<remoting::DaemonController> daemon_controller( 93 scoped_ptr<remoting::DaemonController> daemon_controller(
90 remoting::DaemonController::Create()); 94 remoting::DaemonController::Create());
91 return scoped_ptr<HostStarter>( 95 return scoped_ptr<HostStarter>(
92 new HostStarter(oauth_client.Pass(), user_email_fetcher.Pass(), 96 new HostStarter(oauth_client.Pass(), user_email_fetcher.Pass(),
93 service_client.Pass(), daemon_controller.Pass())); 97 service_client.Pass(), daemon_controller.Pass()));
94 } 98 }
95 99
96 void HostStarter::StartHost(const std::string& host_name, 100 void HostStarter::StartHost(
97 const std::string& host_pin, 101 const std::string& host_name,
98 bool consent_to_data_collection, 102 const std::string& host_pin,
99 const std::string& auth_code, 103 bool consent_to_data_collection,
100 CompletionCallback on_done) { 104 const std::string& auth_code,
105 CompletionCallback on_done) {
106 DCHECK(main_task_runner_->BelongsToCurrentThread());
101 if (in_progress_) { 107 if (in_progress_) {
102 on_done.Run(START_IN_PROGRESS); 108 on_done.Run(START_IN_PROGRESS);
103 return; 109 return;
104 } 110 }
105 in_progress_ = true; 111 in_progress_ = true;
106 host_name_ = host_name; 112 host_name_ = host_name;
107 host_pin_ = host_pin; 113 host_pin_ = host_pin;
108 consent_to_data_collection_ = consent_to_data_collection; 114 consent_to_data_collection_ = consent_to_data_collection;
109 on_done_ = on_done; 115 on_done_ = on_done;
110 // Map the authorization code to refresh and access tokens. 116 // Map the authorization code to refresh and access tokens.
111 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code, 117 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, auth_code,
112 kMaxGetTokensRetries, this); 118 kMaxGetTokensRetries, this);
113 } 119 }
114 120
115 void HostStarter::OnGetTokensResponse( 121 void HostStarter::OnGetTokensResponse(
116 const std::string& refresh_token, 122 const std::string& refresh_token,
117 const std::string& access_token, 123 const std::string& access_token,
118 int expires_in_seconds) { 124 int expires_in_seconds) {
125 if (!main_task_runner_->BelongsToCurrentThread()) {
126 main_task_runner_->PostTask(FROM_HERE, base::Bind(
127 &HostStarter::OnGetTokensResponse, weak_ptr_,
128 refresh_token, access_token, expires_in_seconds));
129 return;
130 }
119 refresh_token_ = refresh_token; 131 refresh_token_ = refresh_token;
120 access_token_ = access_token; 132 access_token_ = access_token;
121 // Get the email corresponding to the access token. 133 // Get the email corresponding to the access token.
122 user_email_fetcher_->GetUserEmail(access_token_, this); 134 user_email_fetcher_->GetUserEmail(access_token_, this);
123 } 135 }
124 136
125 void HostStarter::OnGetUserEmailResponse(const std::string& user_email) { 137 void HostStarter::OnGetUserEmailResponse(const std::string& user_email) {
138 if (!main_task_runner_->BelongsToCurrentThread()) {
139 main_task_runner_->PostTask(FROM_HERE, base::Bind(
140 &HostStarter::OnGetUserEmailResponse, weak_ptr_, user_email));
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
OLDNEW
« no previous file with comments | « remoting/host/setup/host_starter.h ('k') | remoting/host/setup/pin_validator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698