OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_http_impl.h" | 5 #include "chrome/browser/local_discovery/privet_http_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "chrome/browser/local_discovery/privet_constants.h" | 11 #include "chrome/browser/local_discovery/privet_constants.h" |
12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
13 | 13 |
14 namespace local_discovery { | 14 namespace local_discovery { |
15 | 15 |
16 namespace { | 16 namespace { |
17 // First format argument (string) is the host, second format argument (int) is | 17 // First format argument (string) is the host, second format argument (int) is |
18 // the port. | 18 // the port. |
19 const char kPrivetInfoURLFormat[] = "http://%s:%d/privet/info"; | 19 const char kPrivetInfoURLFormat[] = "http://%s:%d/privet/info"; |
20 // First format argument (string) is the host, second format argument (int) is | 20 // First format argument (string) is the host, second format argument (int) is |
21 // the port, third argument (string) is the action name, fourth argument | 21 // the port, third argument (string) is the action name, fourth argument |
22 // (string) is the user name. | 22 // (string) is the user name. |
23 const char kPrivetRegisterURLFormat[] = | 23 const char kPrivetRegisterURLFormat[] = |
24 "http://%s:%d/privet/register?action=%s&user=%s"; | 24 "http://%s:%d/privet/register?action=%s&user=%s"; |
| 25 |
| 26 const int kPrivetCancelationTimeoutSeconds = 3; |
25 } // namespace | 27 } // namespace |
26 | 28 |
27 PrivetInfoOperationImpl::PrivetInfoOperationImpl( | 29 PrivetInfoOperationImpl::PrivetInfoOperationImpl( |
28 PrivetHTTPClientImpl* privet_client, | 30 PrivetHTTPClientImpl* privet_client, |
29 PrivetInfoOperation::Delegate* delegate) | 31 PrivetInfoOperation::Delegate* delegate) |
30 : privet_client_(privet_client), delegate_(delegate) { | 32 : privet_client_(privet_client), delegate_(delegate) { |
31 } | 33 } |
32 | 34 |
33 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { | 35 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { |
34 } | 36 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 87 |
86 ongoing_ = true; | 88 ongoing_ = true; |
87 next_response_handler_ = | 89 next_response_handler_ = |
88 base::Bind(&PrivetRegisterOperationImpl::StartResponse, | 90 base::Bind(&PrivetRegisterOperationImpl::StartResponse, |
89 base::Unretained(this)); | 91 base::Unretained(this)); |
90 SendRequest(kPrivetActionStart); | 92 SendRequest(kPrivetActionStart); |
91 } | 93 } |
92 | 94 |
93 void PrivetRegisterOperationImpl::Cancel() { | 95 void PrivetRegisterOperationImpl::Cancel() { |
94 url_fetcher_.reset(); | 96 url_fetcher_.reset(); |
95 // TODO(noamsml): Proper cancelation. | 97 |
| 98 if (ongoing_) { |
| 99 // Owned by the message loop. |
| 100 Cancelation* cancelation = new Cancelation(privet_client_, user_); |
| 101 |
| 102 base::MessageLoop::current()->PostDelayedTask( |
| 103 FROM_HERE, |
| 104 base::Bind(&PrivetRegisterOperationImpl::Cancelation::Cleanup, |
| 105 base::Owned(cancelation)), |
| 106 base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds)); |
| 107 |
| 108 ongoing_ = false; |
| 109 } |
96 } | 110 } |
97 | 111 |
98 void PrivetRegisterOperationImpl::CompleteRegistration() { | 112 void PrivetRegisterOperationImpl::CompleteRegistration() { |
99 next_response_handler_ = | 113 next_response_handler_ = |
100 base::Bind(&PrivetRegisterOperationImpl::CompleteResponse, | 114 base::Bind(&PrivetRegisterOperationImpl::CompleteResponse, |
101 base::Unretained(this)); | 115 base::Unretained(this)); |
102 SendRequest(kPrivetActionComplete); | 116 SendRequest(kPrivetActionComplete); |
103 } | 117 } |
104 | 118 |
105 PrivetHTTPClient* PrivetRegisterOperationImpl::GetHTTPClient() { | 119 PrivetHTTPClient* PrivetRegisterOperationImpl::GetHTTPClient() { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 return; | 183 return; |
170 } | 184 } |
171 | 185 |
172 // TODO(noamsml): Match the user&action with the user&action in the object, | 186 // TODO(noamsml): Match the user&action with the user&action in the object, |
173 // and fail if different. | 187 // and fail if different. |
174 | 188 |
175 next_response_handler_.Run(*value); | 189 next_response_handler_.Run(*value); |
176 } | 190 } |
177 | 191 |
178 void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { | 192 void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { |
179 std::string url = base::StringPrintf( | 193 GURL url = GetURLForActionAndUser(privet_client_, action, user_); |
180 kPrivetRegisterURLFormat, | |
181 privet_client_->host_port().host().c_str(), | |
182 privet_client_->host_port().port(), | |
183 action.c_str(), | |
184 user_.c_str()); | |
185 | 194 |
186 current_action_ = action; | 195 current_action_ = action; |
187 url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( | 196 url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( |
188 GURL(url), net::URLFetcher::POST, this); | 197 url, net::URLFetcher::POST, this); |
189 url_fetcher_->Start(); | 198 url_fetcher_->Start(); |
190 } | 199 } |
191 | 200 |
192 void PrivetRegisterOperationImpl::StartResponse( | 201 void PrivetRegisterOperationImpl::StartResponse( |
193 const base::DictionaryValue& value) { | 202 const base::DictionaryValue& value) { |
194 next_response_handler_ = | 203 next_response_handler_ = |
195 base::Bind(&PrivetRegisterOperationImpl::GetClaimTokenResponse, | 204 base::Bind(&PrivetRegisterOperationImpl::GetClaimTokenResponse, |
196 base::Unretained(this)); | 205 base::Unretained(this)); |
197 | 206 |
198 SendRequest(kPrivetActionGetClaimToken); | 207 SendRequest(kPrivetActionGetClaimToken); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 info_operation_ = privet_client_->CreateInfoOperation(this); | 278 info_operation_ = privet_client_->CreateInfoOperation(this); |
270 info_operation_->Start(); | 279 info_operation_->Start(); |
271 } | 280 } |
272 | 281 |
273 bool PrivetRegisterOperationImpl::PrivetErrorTransient( | 282 bool PrivetRegisterOperationImpl::PrivetErrorTransient( |
274 const std::string& error) { | 283 const std::string& error) { |
275 return (error == kPrivetErrorDeviceBusy) || | 284 return (error == kPrivetErrorDeviceBusy) || |
276 (error == kPrivetErrorPendingUserAction); | 285 (error == kPrivetErrorPendingUserAction); |
277 } | 286 } |
278 | 287 |
| 288 // static |
| 289 GURL PrivetRegisterOperationImpl::GetURLForActionAndUser( |
| 290 PrivetHTTPClientImpl* privet_client, |
| 291 const std::string& action, |
| 292 const std::string& user) { |
| 293 return GURL(base::StringPrintf(kPrivetRegisterURLFormat, |
| 294 privet_client->host_port().host().c_str(), |
| 295 privet_client->host_port().port(), |
| 296 action.c_str(), |
| 297 user.c_str())); |
| 298 } |
| 299 |
| 300 PrivetRegisterOperationImpl::Cancelation::Cancelation( |
| 301 PrivetHTTPClientImpl* privet_client, |
| 302 const std::string& user) { |
| 303 GURL url = GetURLForActionAndUser(privet_client, |
| 304 kPrivetActionCancel, |
| 305 user); |
| 306 url_fetcher_ = privet_client->fetcher_factory().CreateURLFetcher( |
| 307 url, net::URLFetcher::POST, this); |
| 308 url_fetcher_->Start(); |
| 309 } |
| 310 |
| 311 PrivetRegisterOperationImpl::Cancelation::~Cancelation() { |
| 312 } |
| 313 |
| 314 void PrivetRegisterOperationImpl::Cancelation::OnError( |
| 315 PrivetURLFetcher* fetcher, |
| 316 PrivetURLFetcher::ErrorType error) { |
| 317 } |
| 318 |
| 319 void PrivetRegisterOperationImpl::Cancelation::OnParsedJson( |
| 320 PrivetURLFetcher* fetcher, |
| 321 const base::DictionaryValue* value, |
| 322 bool has_error) { |
| 323 } |
| 324 |
| 325 void PrivetRegisterOperationImpl::Cancelation::Cleanup() { |
| 326 // Nothing needs to be done, as base::Owned will delete this object, |
| 327 // this callback is just here to pass ownership of the Cancelation to |
| 328 // the message loop. |
| 329 } |
| 330 |
279 PrivetHTTPClientImpl::PrivetHTTPClientImpl( | 331 PrivetHTTPClientImpl::PrivetHTTPClientImpl( |
280 const std::string& name, | 332 const std::string& name, |
281 const net::HostPortPair& host_port, | 333 const net::HostPortPair& host_port, |
282 net::URLRequestContextGetter* request_context) | 334 net::URLRequestContextGetter* request_context) |
283 : name_(name), | 335 : name_(name), |
284 fetcher_factory_(request_context), | 336 fetcher_factory_(request_context), |
285 host_port_(host_port) { | 337 host_port_(host_port) { |
286 } | 338 } |
287 | 339 |
288 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { | 340 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { |
(...skipping 23 matching lines...) Expand all Loading... |
312 | 364 |
313 void PrivetHTTPClientImpl::CacheInfo(const base::DictionaryValue* cached_info) { | 365 void PrivetHTTPClientImpl::CacheInfo(const base::DictionaryValue* cached_info) { |
314 cached_info_.reset(cached_info->DeepCopy()); | 366 cached_info_.reset(cached_info->DeepCopy()); |
315 std::string token; | 367 std::string token; |
316 if (cached_info_->GetString(kPrivetInfoKeyToken, &token)) { | 368 if (cached_info_->GetString(kPrivetInfoKeyToken, &token)) { |
317 fetcher_factory_.set_token(token); | 369 fetcher_factory_.set_token(token); |
318 } | 370 } |
319 } | 371 } |
320 | 372 |
321 } // namespace local_discovery | 373 } // namespace local_discovery |
OLD | NEW |