| 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 "remoting/host/setup/service_client.h" | 5 #include "remoting/host/setup/service_client.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "net/http/http_status_code.h" | 11 #include "net/http/http_status_code.h" |
| 11 #include "net/url_request/url_fetcher.h" | 12 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 namespace remoting { | 17 namespace remoting { |
| 17 | 18 |
| 18 class ServiceClient::Core | 19 class ServiceClient::Core |
| 19 : public base::RefCountedThreadSafe<ServiceClient::Core>, | 20 : public base::RefCountedThreadSafe<ServiceClient::Core>, |
| 20 public net::URLFetcherDelegate { | 21 public net::URLFetcherDelegate { |
| 21 public: | 22 public: |
| 22 Core(const std::string& chromoting_hosts_url, | 23 Core(const std::string& chromoting_hosts_url, |
| 23 net::URLRequestContextGetter* request_context_getter) | 24 net::URLRequestContextGetter* request_context_getter) |
| 24 : request_context_getter_(request_context_getter), | 25 : request_context_getter_(request_context_getter), |
| 25 delegate_(NULL), | 26 delegate_(NULL), |
| 26 pending_request_type_(PENDING_REQUEST_NONE), | 27 pending_request_type_(PENDING_REQUEST_NONE), |
| 27 chromoting_hosts_url_(chromoting_hosts_url) { | 28 chromoting_hosts_url_(chromoting_hosts_url) { |
| 28 } | 29 } |
| 29 | 30 |
| 30 void RegisterHost(const std::string& host_id, | 31 void RegisterHost(const std::string& host_id, |
| 31 const std::string& host_name, | 32 const std::string& host_name, |
| 32 const std::string& public_key, | 33 const std::string& public_key, |
| 34 const std::string& host_client_id, |
| 33 const std::string& oauth_access_token, | 35 const std::string& oauth_access_token, |
| 34 ServiceClient::Delegate* delegate); | 36 ServiceClient::Delegate* delegate); |
| 35 | 37 |
| 36 void UnregisterHost(const std::string& host_id, | 38 void UnregisterHost(const std::string& host_id, |
| 37 const std::string& oauth_access_token, | 39 const std::string& oauth_access_token, |
| 38 ServiceClient::Delegate* delegate); | 40 ServiceClient::Delegate* delegate); |
| 39 | 41 |
| 40 // net::URLFetcherDelegate implementation. | 42 // net::URLFetcherDelegate implementation. |
| 41 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 43 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 42 | 44 |
| 43 private: | 45 private: |
| 44 friend class base::RefCountedThreadSafe<Core>; | 46 friend class base::RefCountedThreadSafe<Core>; |
| 45 virtual ~Core() {} | 47 virtual ~Core() {} |
| 46 | 48 |
| 47 enum PendingRequestType { | 49 enum PendingRequestType { |
| 48 PENDING_REQUEST_NONE, | 50 PENDING_REQUEST_NONE, |
| 49 PENDING_REQUEST_REGISTER_HOST, | 51 PENDING_REQUEST_REGISTER_HOST, |
| 50 PENDING_REQUEST_UNREGISTER_HOST | 52 PENDING_REQUEST_UNREGISTER_HOST |
| 51 }; | 53 }; |
| 52 | 54 |
| 53 void MakeGaiaRequest(net::URLFetcher::RequestType request_type, | 55 void MakeChromotingRequest(net::URLFetcher::RequestType request_type, |
| 54 const std::string& post_body, | 56 const std::string& post_body, |
| 55 const std::string& url_suffix, | 57 const std::string& url_suffix, |
| 56 const std::string& oauth_access_token, | 58 const std::string& oauth_access_token, |
| 57 ServiceClient::Delegate* delegate); | 59 ServiceClient::Delegate* delegate); |
| 58 void HandleResponse(const net::URLFetcher* source); | 60 void HandleResponse(const net::URLFetcher* source); |
| 59 | 61 |
| 60 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 62 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 61 ServiceClient::Delegate* delegate_; | 63 ServiceClient::Delegate* delegate_; |
| 62 scoped_ptr<net::URLFetcher> request_; | 64 scoped_ptr<net::URLFetcher> request_; |
| 63 PendingRequestType pending_request_type_; | 65 PendingRequestType pending_request_type_; |
| 64 std::string chromoting_hosts_url_; | 66 std::string chromoting_hosts_url_; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 void ServiceClient::Core::RegisterHost( | 69 void ServiceClient::Core::RegisterHost( |
| 68 const std::string& host_id, | 70 const std::string& host_id, |
| 69 const std::string& host_name, | 71 const std::string& host_name, |
| 70 const std::string& public_key, | 72 const std::string& public_key, |
| 73 const std::string& host_client_id, |
| 71 const std::string& oauth_access_token, | 74 const std::string& oauth_access_token, |
| 72 Delegate* delegate) { | 75 Delegate* delegate) { |
| 73 DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); | 76 DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); |
| 74 pending_request_type_ = PENDING_REQUEST_REGISTER_HOST; | 77 pending_request_type_ = PENDING_REQUEST_REGISTER_HOST; |
| 75 base::DictionaryValue post_body; | 78 base::DictionaryValue post_body; |
| 76 post_body.SetString("data.hostId", host_id); | 79 post_body.SetString("data.hostId", host_id); |
| 77 post_body.SetString("data.hostName", host_name); | 80 post_body.SetString("data.hostName", host_name); |
| 78 post_body.SetString("data.publicKey", public_key); | 81 post_body.SetString("data.publicKey", public_key); |
| 82 std::string url_suffix; |
| 83 if (!host_client_id.empty()) |
| 84 url_suffix = "?hostClientId=" + host_client_id; |
| 79 std::string post_body_str; | 85 std::string post_body_str; |
| 80 base::JSONWriter::Write(&post_body, &post_body_str); | 86 base::JSONWriter::Write(&post_body, &post_body_str); |
| 81 MakeGaiaRequest(net::URLFetcher::POST, | 87 MakeChromotingRequest(net::URLFetcher::POST, |
| 82 std::string(), | 88 url_suffix, |
| 83 post_body_str, | 89 post_body_str, |
| 84 oauth_access_token, | 90 oauth_access_token, |
| 85 delegate); | 91 delegate); |
| 86 } | 92 } |
| 87 | 93 |
| 88 void ServiceClient::Core::UnregisterHost( | 94 void ServiceClient::Core::UnregisterHost( |
| 89 const std::string& host_id, | 95 const std::string& host_id, |
| 90 const std::string& oauth_access_token, | 96 const std::string& oauth_access_token, |
| 91 Delegate* delegate) { | 97 Delegate* delegate) { |
| 92 DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); | 98 DCHECK(pending_request_type_ == PENDING_REQUEST_NONE); |
| 93 pending_request_type_ = PENDING_REQUEST_UNREGISTER_HOST; | 99 pending_request_type_ = PENDING_REQUEST_UNREGISTER_HOST; |
| 94 MakeGaiaRequest(net::URLFetcher::DELETE_REQUEST, | 100 MakeChromotingRequest(net::URLFetcher::DELETE_REQUEST, |
| 95 host_id, | 101 host_id, |
| 96 std::string(), | 102 std::string(), |
| 97 oauth_access_token, | 103 oauth_access_token, |
| 98 delegate); | 104 delegate); |
| 99 } | 105 } |
| 100 | 106 |
| 101 void ServiceClient::Core::MakeGaiaRequest( | 107 void ServiceClient::Core::MakeChromotingRequest( |
| 102 net::URLFetcher::RequestType request_type, | 108 net::URLFetcher::RequestType request_type, |
| 103 const std::string& url_suffix, | 109 const std::string& url_suffix, |
| 104 const std::string& request_body, | 110 const std::string& request_body, |
| 105 const std::string& oauth_access_token, | 111 const std::string& oauth_access_token, |
| 106 ServiceClient::Delegate* delegate) { | 112 ServiceClient::Delegate* delegate) { |
| 107 delegate_ = delegate; | 113 delegate_ = delegate; |
| 108 request_.reset(net::URLFetcher::Create( | 114 request_.reset(net::URLFetcher::Create( |
| 109 0, GURL(chromoting_hosts_url_ + url_suffix), request_type, this)); | 115 0, GURL(chromoting_hosts_url_ + url_suffix), request_type, this)); |
| 110 request_->SetRequestContext(request_context_getter_.get()); | 116 request_->SetRequestContext(request_context_getter_.get()); |
| 111 request_->SetUploadData("application/json; charset=UTF-8", request_body); | 117 request_->SetUploadData("application/json; charset=UTF-8", request_body); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 126 pending_request_type_ = PENDING_REQUEST_NONE; | 132 pending_request_type_ = PENDING_REQUEST_NONE; |
| 127 if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { | 133 if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { |
| 128 delegate_->OnOAuthError(); | 134 delegate_->OnOAuthError(); |
| 129 return; | 135 return; |
| 130 } | 136 } |
| 131 if (source->GetResponseCode() == net::HTTP_OK) { | 137 if (source->GetResponseCode() == net::HTTP_OK) { |
| 132 switch (old_type) { | 138 switch (old_type) { |
| 133 case PENDING_REQUEST_NONE: | 139 case PENDING_REQUEST_NONE: |
| 134 break; | 140 break; |
| 135 case PENDING_REQUEST_REGISTER_HOST: | 141 case PENDING_REQUEST_REGISTER_HOST: |
| 136 delegate_->OnHostRegistered(); | 142 { |
| 143 std::string data; |
| 144 source->GetResponseAsString(&data); |
| 145 scoped_ptr<Value> message_value(base::JSONReader::Read(data)); |
| 146 DictionaryValue *dict; |
| 147 std::string code; |
| 148 if (message_value.get() && |
| 149 message_value->IsType(Value::TYPE_DICTIONARY) && |
| 150 message_value->GetAsDictionary(&dict) && |
| 151 dict->GetString("data.authorizationCode", &code)) { |
| 152 delegate_->OnHostRegistered(code); |
| 153 } else { |
| 154 delegate_->OnHostRegistered(std::string()); |
| 155 } |
| 156 } |
| 137 break; | 157 break; |
| 138 case PENDING_REQUEST_UNREGISTER_HOST: | 158 case PENDING_REQUEST_UNREGISTER_HOST: |
| 139 delegate_->OnHostUnregistered(); | 159 delegate_->OnHostUnregistered(); |
| 140 break; | 160 break; |
| 141 } | 161 } |
| 142 return; | 162 return; |
| 143 } | 163 } |
| 144 delegate_->OnNetworkError(source->GetResponseCode()); | 164 delegate_->OnNetworkError(source->GetResponseCode()); |
| 145 } | 165 } |
| 146 | 166 |
| 147 ServiceClient::ServiceClient(const std::string& chromoting_hosts_url, | 167 ServiceClient::ServiceClient(const std::string& chromoting_hosts_url, |
| 148 net::URLRequestContextGetter* context_getter) { | 168 net::URLRequestContextGetter* context_getter) { |
| 149 core_ = new Core(chromoting_hosts_url, context_getter); | 169 core_ = new Core(chromoting_hosts_url, context_getter); |
| 150 } | 170 } |
| 151 | 171 |
| 152 ServiceClient::~ServiceClient() { | 172 ServiceClient::~ServiceClient() { |
| 153 } | 173 } |
| 154 | 174 |
| 155 void ServiceClient::RegisterHost( | 175 void ServiceClient::RegisterHost( |
| 156 const std::string& host_id, | 176 const std::string& host_id, |
| 157 const std::string& host_name, | 177 const std::string& host_name, |
| 158 const std::string& public_key, | 178 const std::string& public_key, |
| 179 const std::string& host_client_id, |
| 159 const std::string& oauth_access_token, | 180 const std::string& oauth_access_token, |
| 160 Delegate* delegate) { | 181 Delegate* delegate) { |
| 161 return core_->RegisterHost(host_id, host_name, public_key, oauth_access_token, | 182 return core_->RegisterHost(host_id, host_name, public_key, host_client_id, |
| 162 delegate); | 183 oauth_access_token, delegate); |
| 163 } | 184 } |
| 164 | 185 |
| 165 void ServiceClient::UnregisterHost( | 186 void ServiceClient::UnregisterHost( |
| 166 const std::string& host_id, | 187 const std::string& host_id, |
| 167 const std::string& oauth_access_token, | 188 const std::string& oauth_access_token, |
| 168 Delegate* delegate) { | 189 Delegate* delegate) { |
| 169 return core_->UnregisterHost(host_id, oauth_access_token, delegate); | 190 return core_->UnregisterHost(host_id, oauth_access_token, delegate); |
| 170 } | 191 } |
| 171 | 192 |
| 172 } // namespace gaia | 193 } // namespace gaia |
| OLD | NEW |