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 new Cancelation(privet_client_, user_); // Owned by the message loop. | |
Vitaly Buka (NO REVIEWS)
2013/08/29 20:47:31
please move base::MessageLoop::current()->PostDela
Noam Samuel
2013/08/30 17:44:26
Done.
| |
100 ongoing_ = false; | |
101 } | |
96 } | 102 } |
97 | 103 |
98 void PrivetRegisterOperationImpl::CompleteRegistration() { | 104 void PrivetRegisterOperationImpl::CompleteRegistration() { |
99 next_response_handler_ = | 105 next_response_handler_ = |
100 base::Bind(&PrivetRegisterOperationImpl::CompleteResponse, | 106 base::Bind(&PrivetRegisterOperationImpl::CompleteResponse, |
101 base::Unretained(this)); | 107 base::Unretained(this)); |
102 SendRequest(kPrivetActionComplete); | 108 SendRequest(kPrivetActionComplete); |
103 } | 109 } |
104 | 110 |
105 PrivetHTTPClient* PrivetRegisterOperationImpl::GetHTTPClient() { | 111 PrivetHTTPClient* PrivetRegisterOperationImpl::GetHTTPClient() { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 return; | 175 return; |
170 } | 176 } |
171 | 177 |
172 // TODO(noamsml): Match the user&action with the user&action in the object, | 178 // TODO(noamsml): Match the user&action with the user&action in the object, |
173 // and fail if different. | 179 // and fail if different. |
174 | 180 |
175 next_response_handler_.Run(*value); | 181 next_response_handler_.Run(*value); |
176 } | 182 } |
177 | 183 |
178 void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { | 184 void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { |
179 std::string url = base::StringPrintf( | 185 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 | 186 |
186 current_action_ = action; | 187 current_action_ = action; |
187 url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( | 188 url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( |
188 GURL(url), net::URLFetcher::POST, this); | 189 url, net::URLFetcher::POST, this); |
189 url_fetcher_->Start(); | 190 url_fetcher_->Start(); |
190 } | 191 } |
191 | 192 |
192 void PrivetRegisterOperationImpl::StartResponse( | 193 void PrivetRegisterOperationImpl::StartResponse( |
193 const base::DictionaryValue& value) { | 194 const base::DictionaryValue& value) { |
194 next_response_handler_ = | 195 next_response_handler_ = |
195 base::Bind(&PrivetRegisterOperationImpl::GetClaimTokenResponse, | 196 base::Bind(&PrivetRegisterOperationImpl::GetClaimTokenResponse, |
196 base::Unretained(this)); | 197 base::Unretained(this)); |
197 | 198 |
198 SendRequest(kPrivetActionGetClaimToken); | 199 SendRequest(kPrivetActionGetClaimToken); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 info_operation_ = privet_client_->CreateInfoOperation(this); | 270 info_operation_ = privet_client_->CreateInfoOperation(this); |
270 info_operation_->Start(); | 271 info_operation_->Start(); |
271 } | 272 } |
272 | 273 |
273 bool PrivetRegisterOperationImpl::PrivetErrorTransient( | 274 bool PrivetRegisterOperationImpl::PrivetErrorTransient( |
274 const std::string& error) { | 275 const std::string& error) { |
275 return (error == kPrivetErrorDeviceBusy) || | 276 return (error == kPrivetErrorDeviceBusy) || |
276 (error == kPrivetErrorPendingUserAction); | 277 (error == kPrivetErrorPendingUserAction); |
277 } | 278 } |
278 | 279 |
280 // static | |
281 GURL PrivetRegisterOperationImpl::GetURLForActionAndUser( | |
282 PrivetHTTPClientImpl* privet_client, | |
283 const std::string& action, | |
284 const std::string& user) { | |
285 return GURL(base::StringPrintf(kPrivetRegisterURLFormat, | |
286 privet_client->host_port().host().c_str(), | |
287 privet_client->host_port().port(), | |
288 action.c_str(), | |
289 user.c_str())); | |
290 } | |
291 | |
292 PrivetRegisterOperationImpl::Cancelation::Cancelation( | |
293 PrivetHTTPClientImpl* privet_client, | |
294 const std::string& user) { | |
295 GURL url = GetURLForActionAndUser(privet_client, | |
296 kPrivetActionCancel, | |
297 user); | |
298 url_fetcher_ = privet_client->fetcher_factory().CreateURLFetcher( | |
299 url, net::URLFetcher::POST, this); | |
300 url_fetcher_->Start(); | |
301 | |
302 base::MessageLoop::current()->PostDelayedTask( | |
303 FROM_HERE, | |
304 base::Bind(&PrivetRegisterOperationImpl::Cancelation::Cleanup, | |
305 base::Owned(this)), | |
306 base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds)); | |
307 } | |
308 | |
309 PrivetRegisterOperationImpl::Cancelation::~Cancelation() { | |
310 } | |
311 | |
312 void PrivetRegisterOperationImpl::Cancelation::OnError( | |
313 PrivetURLFetcher* fetcher, | |
314 PrivetURLFetcher::ErrorType error) { | |
315 } | |
316 | |
317 void PrivetRegisterOperationImpl::Cancelation::OnParsedJson( | |
318 PrivetURLFetcher* fetcher, | |
319 const base::DictionaryValue* value, | |
320 bool has_error) { | |
321 } | |
322 | |
323 void PrivetRegisterOperationImpl::Cancelation::Cleanup() { | |
324 // Nothing needs to be done, as base::Owned will delete this object, | |
325 // this callback is just here to pass ownership of the Cancelation to | |
326 // the message loop. | |
327 } | |
328 | |
279 PrivetHTTPClientImpl::PrivetHTTPClientImpl( | 329 PrivetHTTPClientImpl::PrivetHTTPClientImpl( |
280 const std::string& name, | 330 const std::string& name, |
281 const net::HostPortPair& host_port, | 331 const net::HostPortPair& host_port, |
282 net::URLRequestContextGetter* request_context) | 332 net::URLRequestContextGetter* request_context) |
283 : name_(name), | 333 : name_(name), |
284 fetcher_factory_(request_context), | 334 fetcher_factory_(request_context), |
285 host_port_(host_port) { | 335 host_port_(host_port) { |
286 } | 336 } |
287 | 337 |
288 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { | 338 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { |
(...skipping 23 matching lines...) Expand all Loading... | |
312 | 362 |
313 void PrivetHTTPClientImpl::CacheInfo(const base::DictionaryValue* cached_info) { | 363 void PrivetHTTPClientImpl::CacheInfo(const base::DictionaryValue* cached_info) { |
314 cached_info_.reset(cached_info->DeepCopy()); | 364 cached_info_.reset(cached_info->DeepCopy()); |
315 std::string token; | 365 std::string token; |
316 if (cached_info_->GetString(kPrivetInfoKeyToken, &token)) { | 366 if (cached_info_->GetString(kPrivetInfoKeyToken, &token)) { |
317 fetcher_factory_.set_token(token); | 367 fetcher_factory_.set_token(token); |
318 } | 368 } |
319 } | 369 } |
320 | 370 |
321 } // namespace local_discovery | 371 } // namespace local_discovery |
OLD | NEW |