Chromium Code Reviews| Index: chrome/browser/local_discovery/privet_http_impl.cc |
| diff --git a/chrome/browser/local_discovery/privet_http_impl.cc b/chrome/browser/local_discovery/privet_http_impl.cc |
| index c76b106499bdc82d522ab986eff298b29e0f33f1..871b541d10c6abb1ee491c0d06972431a0f4d64e 100644 |
| --- a/chrome/browser/local_discovery/privet_http_impl.cc |
| +++ b/chrome/browser/local_discovery/privet_http_impl.cc |
| @@ -22,6 +22,8 @@ const char kPrivetInfoURLFormat[] = "http://%s:%d/privet/info"; |
| // (string) is the user name. |
| const char kPrivetRegisterURLFormat[] = |
| "http://%s:%d/privet/register?action=%s&user=%s"; |
| + |
| +const int kPrivetCancelationTimeoutSeconds = 3; |
| } // namespace |
| PrivetInfoOperationImpl::PrivetInfoOperationImpl( |
| @@ -92,7 +94,11 @@ void PrivetRegisterOperationImpl::Start() { |
| void PrivetRegisterOperationImpl::Cancel() { |
| url_fetcher_.reset(); |
| - // TODO(noamsml): Proper cancelation. |
| + |
| + if (ongoing_) { |
| + 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.
|
| + ongoing_ = false; |
| + } |
| } |
| void PrivetRegisterOperationImpl::CompleteRegistration() { |
| @@ -176,16 +182,11 @@ void PrivetRegisterOperationImpl::OnParsedJson( |
| } |
| void PrivetRegisterOperationImpl::SendRequest(const std::string& action) { |
| - std::string url = base::StringPrintf( |
| - kPrivetRegisterURLFormat, |
| - privet_client_->host_port().host().c_str(), |
| - privet_client_->host_port().port(), |
| - action.c_str(), |
| - user_.c_str()); |
| + GURL url = GetURLForActionAndUser(privet_client_, action, user_); |
| current_action_ = action; |
| url_fetcher_ = privet_client_->fetcher_factory().CreateURLFetcher( |
| - GURL(url), net::URLFetcher::POST, this); |
| + url, net::URLFetcher::POST, this); |
| url_fetcher_->Start(); |
| } |
| @@ -276,6 +277,55 @@ bool PrivetRegisterOperationImpl::PrivetErrorTransient( |
| (error == kPrivetErrorPendingUserAction); |
| } |
| +// static |
| +GURL PrivetRegisterOperationImpl::GetURLForActionAndUser( |
| + PrivetHTTPClientImpl* privet_client, |
| + const std::string& action, |
| + const std::string& user) { |
| + return GURL(base::StringPrintf(kPrivetRegisterURLFormat, |
| + privet_client->host_port().host().c_str(), |
| + privet_client->host_port().port(), |
| + action.c_str(), |
| + user.c_str())); |
| +} |
| + |
| +PrivetRegisterOperationImpl::Cancelation::Cancelation( |
| + PrivetHTTPClientImpl* privet_client, |
| + const std::string& user) { |
| + GURL url = GetURLForActionAndUser(privet_client, |
| + kPrivetActionCancel, |
| + user); |
| + url_fetcher_ = privet_client->fetcher_factory().CreateURLFetcher( |
| + url, net::URLFetcher::POST, this); |
| + url_fetcher_->Start(); |
| + |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&PrivetRegisterOperationImpl::Cancelation::Cleanup, |
| + base::Owned(this)), |
| + base::TimeDelta::FromSeconds(kPrivetCancelationTimeoutSeconds)); |
| +} |
| + |
| +PrivetRegisterOperationImpl::Cancelation::~Cancelation() { |
| +} |
| + |
| +void PrivetRegisterOperationImpl::Cancelation::OnError( |
| + PrivetURLFetcher* fetcher, |
| + PrivetURLFetcher::ErrorType error) { |
| +} |
| + |
| +void PrivetRegisterOperationImpl::Cancelation::OnParsedJson( |
| + PrivetURLFetcher* fetcher, |
| + const base::DictionaryValue* value, |
| + bool has_error) { |
| +} |
| + |
| +void PrivetRegisterOperationImpl::Cancelation::Cleanup() { |
| + // Nothing needs to be done, as base::Owned will delete this object, |
| + // this callback is just here to pass ownership of the Cancelation to |
| + // the message loop. |
| +} |
| + |
| PrivetHTTPClientImpl::PrivetHTTPClientImpl( |
| const std::string& name, |
| const net::HostPortPair& host_port, |