diff options
author | simonmorris@chromium.org <simonmorris@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 00:50:10 +0000 |
---|---|---|
committer | simonmorris@chromium.org <simonmorris@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 00:50:10 +0000 |
commit | d215b65bbbfa5800297788cfec4ba6e750541aa4 (patch) | |
tree | 87b125257a42847f25fc37e0ad024f00e56d3f02 /remoting | |
parent | 1bfc7eb0207c3f0fc09915f088e1af8b991c697c (diff) | |
download | chromium_src-d215b65bbbfa5800297788cfec4ba6e750541aa4.zip chromium_src-d215b65bbbfa5800297788cfec4ba6e750541aa4.tar.gz chromium_src-d215b65bbbfa5800297788cfec4ba6e750541aa4.tar.bz2 |
[Chromoting] Make the HostStarter unregister a new host if it couldn't start it.
BUG=153453
Review URL: https://chromiumcodereview.appspot.com/11193045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164468 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/host/service_client.cc | 64 | ||||
-rw-r--r-- | remoting/host/service_client.h | 8 | ||||
-rw-r--r-- | remoting/host/setup/host_starter.cc | 16 | ||||
-rw-r--r-- | remoting/host/setup/host_starter.h | 1 |
4 files changed, 80 insertions, 9 deletions
diff --git a/remoting/host/service_client.cc b/remoting/host/service_client.cc index 56d7935..26e4eef 100644 --- a/remoting/host/service_client.cc +++ b/remoting/host/service_client.cc @@ -26,7 +26,9 @@ class ServiceClient::Core public: Core(net::URLRequestContextGetter* request_context_getter) : request_context_getter_(request_context_getter), - delegate_(NULL) {} + delegate_(NULL), + pending_request_type_(PENDING_REQUEST_NONE) { + } void RegisterHost(const std::string& host_id, const std::string& host_name, @@ -34,6 +36,10 @@ class ServiceClient::Core const std::string& oauth_access_token, ServiceClient::Delegate* delegate); + void UnregisterHost(const std::string& host_id, + const std::string& oauth_access_token, + ServiceClient::Delegate* delegate); + // net::URLFetcherDelegate implementation. virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; @@ -41,7 +47,15 @@ class ServiceClient::Core friend class base::RefCountedThreadSafe<Core>; virtual ~Core() {} - void MakeGaiaRequest(const std::string& post_body, + enum PendingRequestType { + PENDING_REQUEST_NONE, + PENDING_REQUEST_REGISTER_HOST, + PENDING_REQUEST_UNREGISTER_HOST + }; + + void MakeGaiaRequest(net::URLFetcher::RequestType request_type, + const std::string& post_body, + const std::string& url_suffix, const std::string& oauth_access_token, ServiceClient::Delegate* delegate); void HandleResponse(const net::URLFetcher* source); @@ -49,6 +63,7 @@ class ServiceClient::Core scoped_refptr<net::URLRequestContextGetter> request_context_getter_; ServiceClient::Delegate* delegate_; scoped_ptr<net::URLFetcher> request_; + PendingRequestType pending_request_type_; }; void ServiceClient::Core::RegisterHost( @@ -57,25 +72,39 @@ void ServiceClient::Core::RegisterHost( const std::string& public_key, const std::string& oauth_access_token, Delegate* delegate) { + DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); + pending_request_type_ = PENDING_REQUEST_REGISTER_HOST; base::DictionaryValue post_body; post_body.SetString("data.hostId", host_id); post_body.SetString("data.hostName", host_name); post_body.SetString("data.publicKey", public_key); std::string post_body_str; base::JSONWriter::Write(&post_body, &post_body_str); - MakeGaiaRequest(post_body_str, oauth_access_token, delegate); + MakeGaiaRequest(net::URLFetcher::POST, "", post_body_str, oauth_access_token, + delegate); +} + +void ServiceClient::Core::UnregisterHost( + const std::string& host_id, + const std::string& oauth_access_token, + Delegate* delegate) { + DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); + pending_request_type_ = PENDING_REQUEST_UNREGISTER_HOST; + MakeGaiaRequest(net::URLFetcher::DELETE_REQUEST, host_id, "", + oauth_access_token, delegate); } void ServiceClient::Core::MakeGaiaRequest( - const std::string& post_body, + net::URLFetcher::RequestType request_type, + const std::string& url_suffix, + const std::string& request_body, const std::string& oauth_access_token, ServiceClient::Delegate* delegate) { - DCHECK(!request_.get()) << "Tried to fetch two things at once!"; delegate_ = delegate; request_.reset(net::URLFetcher::Create( - 0, GURL(kServiceUrl), net::URLFetcher::POST, this)); + 0, GURL(kServiceUrl + url_suffix), request_type, this)); request_->SetRequestContext(request_context_getter_); - request_->SetUploadData("application/json; charset=UTF-8", post_body); + request_->SetUploadData("application/json; charset=UTF-8", request_body); request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); request_->Start(); } @@ -88,12 +117,24 @@ void ServiceClient::Core::OnURLFetchComplete( } void ServiceClient::Core::HandleResponse(const net::URLFetcher* source) { + DCHECK(pending_request_type_ != PENDING_REQUEST_NONE); + PendingRequestType old_type = pending_request_type_; + pending_request_type_ = PENDING_REQUEST_NONE; if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { delegate_->OnOAuthError(); return; } if (source->GetResponseCode() == net::HTTP_OK) { - delegate_->OnHostRegistered(); + switch (old_type) { + case PENDING_REQUEST_NONE: + break; + case PENDING_REQUEST_REGISTER_HOST: + delegate_->OnHostRegistered(); + break; + case PENDING_REQUEST_UNREGISTER_HOST: + delegate_->OnHostUnregistered(); + break; + } return; } delegate_->OnNetworkError(source->GetResponseCode()); @@ -116,4 +157,11 @@ void ServiceClient::RegisterHost( delegate); } +void ServiceClient::UnregisterHost( + const std::string& host_id, + const std::string& oauth_access_token, + Delegate* delegate) { + return core_->UnregisterHost(host_id, oauth_access_token, delegate); +} + } // namespace gaia diff --git a/remoting/host/service_client.h b/remoting/host/service_client.h index 8c65df91..cb878d8 100644 --- a/remoting/host/service_client.h +++ b/remoting/host/service_client.h @@ -18,10 +18,13 @@ namespace remoting { class ServiceClient { public: + // TODO(simonmorris): Consider using a Callback instead of a delegate. class Delegate { public: // Invoked when a host has been registered. virtual void OnHostRegistered() = 0; + // Invoked when a host has been unregistered. + virtual void OnHostUnregistered() = 0; // Invoked when there is an OAuth error. virtual void OnOAuthError() = 0; // Invoked when there is a network error or upon receiving an invalid @@ -34,11 +37,16 @@ class ServiceClient { ServiceClient(net::URLRequestContextGetter* context_getter); ~ServiceClient(); + // Register a host. void RegisterHost(const std::string& host_id, const std::string& host_name, const std::string& public_key, const std::string& oauth_access_token, Delegate* delegate); + // Unregister a host. + void UnregisterHost(const std::string& host_id, + const std::string& oauth_access_token, + Delegate* delegate); private: // The guts of the implementation live in this class. diff --git a/remoting/host/setup/host_starter.cc b/remoting/host/setup/host_starter.cc index 2f2e757..4021ccb 100644 --- a/remoting/host/setup/host_starter.cc +++ b/remoting/host/setup/host_starter.cc @@ -134,7 +134,10 @@ void HostStarter::OnHostStarted(DaemonController::AsyncResult result) { &HostStarter::OnHostStarted, weak_ptr_, result)); return; } - // TODO(simonmorris): Unregister the host if we didn't start it. + if (result != DaemonController::RESULT_OK) { + service_client_->UnregisterHost(host_id_, access_token_, this); + return; + } Result done_result = (result == DaemonController::RESULT_OK) ? START_COMPLETE : START_ERROR; CompletionCallback cb = on_done_; @@ -164,4 +167,15 @@ void HostStarter::OnNetworkError(int response_code) { cb.Run(NETWORK_ERROR); } +void HostStarter::OnHostUnregistered() { + if (!main_task_runner_->BelongsToCurrentThread()) { + main_task_runner_->PostTask(FROM_HERE, base::Bind( + &HostStarter::OnHostUnregistered, weak_ptr_)); + return; + } + CompletionCallback cb = on_done_; + on_done_.Reset(); + cb.Run(START_ERROR); +} + } // namespace remoting diff --git a/remoting/host/setup/host_starter.h b/remoting/host/setup/host_starter.h index 456befc..b15e874 100644 --- a/remoting/host/setup/host_starter.h +++ b/remoting/host/setup/host_starter.h @@ -55,6 +55,7 @@ class HostStarter : public gaia::GaiaOAuthClient::Delegate, // remoting::ServiceClient::Delegate virtual void OnHostRegistered() OVERRIDE; + virtual void OnHostUnregistered() OVERRIDE; // TODO(sergeyu): Following methods are members of all three delegate // interfaces implemented in this class. Fix ServiceClient and |