Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/base64.h" | |
| 6 #include "google/cacheinvalidation/android_channel.pb.h" | |
| 7 #include "google/cacheinvalidation/channel_common.pb.h" | |
| 5 #include "google_apis/gaia/google_service_auth_error.h" | 8 #include "google_apis/gaia/google_service_auth_error.h" |
| 6 #include "net/http/http_status_code.h" | 9 #include "net/http/http_status_code.h" |
| 7 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
| 8 #include "net/url_request/url_request_status.h" | 11 #include "net/url_request/url_request_status.h" |
| 9 #include "sync/notifier/gcm_network_channel.h" | 12 #include "sync/notifier/gcm_network_channel.h" |
| 10 #include "sync/notifier/gcm_network_channel_delegate.h" | 13 #include "sync/notifier/gcm_network_channel_delegate.h" |
| 11 | 14 |
| 12 namespace syncer { | 15 namespace syncer { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 19 const char kCacheInvalidationEndpointUrl[] = | |
| 20 "https://clients4.google.com/invalidation/android/request/"; | |
| 21 const char kCacheInvalidationPackageName[] = "com.google.chrome.invalidations"; | |
| 22 | |
| 16 // Register backoff policy. | 23 // Register backoff policy. |
| 17 const net::BackoffEntry::Policy kRegisterBackoffPolicy = { | 24 const net::BackoffEntry::Policy kRegisterBackoffPolicy = { |
| 18 // Number of initial errors (in sequence) to ignore before applying | 25 // Number of initial errors (in sequence) to ignore before applying |
| 19 // exponential back-off rules. | 26 // exponential back-off rules. |
| 20 0, | 27 0, |
| 21 | 28 |
| 22 // Initial delay for exponential back-off in ms. | 29 // Initial delay for exponential back-off in ms. |
| 23 2000, // 2 seconds. | 30 2000, // 2 seconds. |
| 24 | 31 |
| 25 // Factor by which the waiting time will be multiplied. | 32 // Factor by which the waiting time will be multiplied. |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 // token service. Just drop this request, cacheinvalidations will retry | 142 // token service. Just drop this request, cacheinvalidations will retry |
| 136 // sending message and at that time we'll retry requesting access token. | 143 // sending message and at that time we'll retry requesting access token. |
| 137 DVLOG(1) << "RequestAccessToken failed: " << error.ToString(); | 144 DVLOG(1) << "RequestAccessToken failed: " << error.ToString(); |
| 138 return; | 145 return; |
| 139 } | 146 } |
| 140 DCHECK(!token.empty()); | 147 DCHECK(!token.empty()); |
| 141 // Save access token in case POST fails and we need to invalidate it. | 148 // Save access token in case POST fails and we need to invalidate it. |
| 142 access_token_ = token; | 149 access_token_ = token; |
| 143 | 150 |
| 144 DVLOG(2) << "Got access token, sending message"; | 151 DVLOG(2) << "Got access token, sending message"; |
| 145 | 152 fetcher_.reset(net::URLFetcher::Create( |
| 146 fetcher_.reset(net::URLFetcher::Create(BuildUrl(), net::URLFetcher::POST, | 153 BuildUrl(registration_id_), net::URLFetcher::POST, this)); |
| 147 this)); | |
| 148 fetcher_->SetRequestContext(request_context_getter_); | 154 fetcher_->SetRequestContext(request_context_getter_); |
| 149 const std::string auth_header("Authorization: Bearer " + access_token_); | 155 const std::string auth_header("Authorization: Bearer " + access_token_); |
| 150 fetcher_->AddExtraRequestHeader(auth_header); | 156 fetcher_->AddExtraRequestHeader(auth_header); |
| 151 fetcher_->SetUploadData("application/x-protobuffer", encoded_message_); | 157 fetcher_->SetUploadData("application/x-protobuffer", encoded_message_); |
| 152 fetcher_->Start(); | 158 fetcher_->Start(); |
| 153 // Clear message to prevent accidentally resending it in the future. | 159 // Clear message to prevent accidentally resending it in the future. |
| 154 encoded_message_.clear(); | 160 encoded_message_.clear(); |
| 155 } | 161 } |
| 156 | 162 |
| 157 void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) { | 163 void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) { |
| 158 DCHECK(CalledOnValidThread()); | 164 DCHECK(CalledOnValidThread()); |
| 159 DCHECK_EQ(fetcher_, source); | 165 DCHECK_EQ(fetcher_, source); |
| 160 // Free fetcher at the end of function. | 166 // Free fetcher at the end of function. |
| 161 scoped_ptr<net::URLFetcher> fetcher = fetcher_.Pass(); | 167 scoped_ptr<net::URLFetcher> fetcher = fetcher_.Pass(); |
| 162 | 168 |
| 163 net::URLRequestStatus status = fetcher->GetStatus(); | 169 net::URLRequestStatus status = fetcher->GetStatus(); |
| 164 if (!status.is_success()) { | 170 if (!status.is_success()) { |
| 165 DVLOG(1) << "URLFetcher failure"; | 171 DVLOG(1) << "URLFetcher failure"; |
| 166 return; | 172 return; |
| 167 } | 173 } |
| 168 | 174 |
| 169 if (fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) { | 175 if (fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) { |
| 170 DVLOG(1) << "URLFetcher failure: HTTP_UNAUTHORIZED"; | 176 DVLOG(1) << "URLFetcher failure: HTTP_UNAUTHORIZED"; |
| 171 delegate_->InvalidateToken(access_token_); | 177 delegate_->InvalidateToken(access_token_); |
| 172 return; | 178 return; |
| 173 } | 179 } |
| 174 DVLOG(2) << "URLFetcher success"; | 180 DVLOG(2) << "URLFetcher success"; |
| 175 } | 181 } |
| 176 | 182 |
| 177 GURL GCMNetworkChannel::BuildUrl() { | 183 GURL GCMNetworkChannel::BuildUrl(const std::string& registration_id) { |
| 178 DCHECK(!registration_id_.empty()); | 184 DCHECK(!registration_id.empty()); |
| 179 // Prepare NetworkEndpointId using registration_id | 185 std::string buffer; |
| 180 // Serialize NetworkEndpointId into byte array and base64 encode. | 186 |
| 181 // Format url using encoded NetworkEndpointId. | 187 ipc::invalidation::EndpointId endpoint_id; |
| 182 // TODO(pavely): implement all of the above. | 188 endpoint_id.set_c2dm_registration_id(registration_id); |
| 183 return GURL("http://invalid.url.com"); | 189 endpoint_id.set_client_key(std::string()); |
| 190 endpoint_id.set_package_name(kCacheInvalidationPackageName); | |
| 191 endpoint_id.mutable_channel_version()->set_major_version( | |
| 192 ipc::invalidation::INITIAL); | |
| 193 endpoint_id.SerializeToString(&buffer); | |
| 194 | |
| 195 ipc::invalidation::NetworkEndpointId network_endpoint_id; | |
| 196 network_endpoint_id.set_network_address( | |
| 197 ipc::invalidation::NetworkEndpointId_NetworkAddress_ANDROID); | |
| 198 network_endpoint_id.set_client_address(buffer); | |
|
rlarocque
2014/02/26 22:55:46
Couldn't you get the same serialized result by mak
pavely
2014/02/26 23:11:13
If network_address is not NetworkAddress::ANDROID
| |
| 199 network_endpoint_id.SerializeToString(&buffer); | |
|
rlarocque
2014/02/26 22:55:46
I'd prefer to see a different buffer for this seri
pavely
2014/02/26 23:11:13
Done.
| |
| 200 | |
| 201 std::string base64URLPiece; | |
| 202 base::Base64Encode(buffer, &base64URLPiece); | |
| 203 | |
| 204 std::string url(kCacheInvalidationEndpointUrl); | |
| 205 url += base64URLPiece; | |
| 206 return GURL(url); | |
| 184 } | 207 } |
| 185 | 208 |
| 186 } // namespace syncer | 209 } // namespace syncer |
| OLD | NEW |