Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Unified Diff: google_apis/gcm/engine/registration_request.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new files Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: google_apis/gcm/engine/registration_request.cc
diff --git a/google_apis/gcm/engine/registration_request.cc b/google_apis/gcm/engine/registration_request.cc
index 2e52fba53cfbb786c5a044fda29a906fb39ca8db..a90e0fcaf5faa7ef8b730db403ef09a797ea5045 100644
--- a/google_apis/gcm/engine/registration_request.cc
+++ b/google_apis/gcm/engine/registration_request.cc
@@ -27,10 +27,18 @@ const char kRegistrationRequestContentType[] =
"application/x-www-form-urlencoded";
// Request constants.
+// Common keys.
const char kAppIdKey[] = "app";
const char kDeviceIdKey[] = "device";
const char kLoginHeader[] = "AidLogin";
const char kSenderKey[] = "sender";
+// Keys specific to InstanceID's GetToken request.
+const char kGMSVersionKey[] = "gmsv";
+const char kInstanceIDKey[] = "appid";
+const char kScopeKey[] = "scope";
+
+// Prefix that needs to be added for each option key.
+const char kOptionKeyPrefix[] = "X-";
// Request validation constants.
const size_t kMaxSenders = 100;
@@ -84,29 +92,85 @@ void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) {
} // namespace
-RegistrationRequest::RequestInfo::RequestInfo(
- uint64 android_id,
- uint64 security_token,
- const std::string& app_id,
- const std::vector<std::string>& sender_ids)
- : android_id(android_id),
- security_token(security_token),
- app_id(app_id),
- sender_ids(sender_ids) {
-}
+RegistrationRequest::RequestInfo::RequestInfo() {}
RegistrationRequest::RequestInfo::~RequestInfo() {}
+void RegistrationRequest::RequestInfo::BuildRequestHeaders(
+ std::string* extra_headers) {
+ net::HttpRequestHeaders headers;
+ headers.SetHeader(
+ net::HttpRequestHeaders::kAuthorization,
+ std::string(kLoginHeader) + " " + base::Uint64ToString(android_id) + ":" +
+ base::Uint64ToString(security_token));
+ *extra_headers = headers.ToString();
+}
+
+void RegistrationRequest::RequestInfo::BuildRequestBody(std::string* body){
+ BuildFormEncoding(kAppIdKey, app_id, body);
+ BuildFormEncoding(kDeviceIdKey, base::Uint64ToString(android_id), body);
+}
+
+RegistrationRequest::GCMRequestInfo::GCMRequestInfo() {}
+
+RegistrationRequest::GCMRequestInfo::~GCMRequestInfo() {}
+
+void RegistrationRequest::GCMRequestInfo::BuildRequestBody(std::string* body){
+ DCHECK(0 < sender_ids.size() && sender_ids.size() <= kMaxSenders);
+
+ RequestInfo::BuildRequestBody(body);
+
+ std::string senders;
+ for (auto iter = sender_ids.begin(); iter != sender_ids.end(); ++iter) {
+ if (!senders.empty())
+ senders.append(",");
+ senders.append(*iter);
+ }
+ BuildFormEncoding(kSenderKey, senders, body);
+ UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount", sender_ids.size());
+}
+
+std::string RegistrationRequest::GCMRequestInfo::GetSenders() const {
+ std::string senders;
+ for (auto iter = sender_ids.begin(); iter != sender_ids.end(); ++iter) {
+ if (!senders.empty())
+ senders.append(",");
+ senders.append(*iter);
+ }
+ return senders;
+}
+
+RegistrationRequest::InstanceIDRequestInfo::InstanceIDRequestInfo() {}
+
+RegistrationRequest::InstanceIDRequestInfo::~InstanceIDRequestInfo() {}
+
+void RegistrationRequest::InstanceIDRequestInfo::BuildRequestBody(
+ std::string* body){
+ RequestInfo::BuildRequestBody(body);
+
+ BuildFormEncoding(kGMSVersionKey, chrome_version, body);
+ BuildFormEncoding(kInstanceIDKey, instance_id, body);
+ BuildFormEncoding(kSenderKey, authorized_entity, body);
+ BuildFormEncoding(kScopeKey, scope, body);
+ for (auto iter = options.begin(); iter != options.end(); ++iter)
+ BuildFormEncoding(kOptionKeyPrefix + iter->first, iter->second, body);
+}
+
+std::string RegistrationRequest::InstanceIDRequestInfo::GetSenders()
+ const {
+ return authorized_entity;
+}
+
RegistrationRequest::RegistrationRequest(
const GURL& registration_url,
- const RequestInfo& request_info,
+ scoped_ptr<RequestInfo> request_info,
const net::BackoffEntry::Policy& backoff_policy,
const RegistrationCallback& callback,
int max_retry_count,
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
GCMStatsRecorder* recorder)
: callback_(callback),
- request_info_(request_info),
+ request_info_(request_info.Pass()),
registration_url_(registration_url),
backoff_entry_(&backoff_policy),
request_context_getter_(request_context_getter),
@@ -120,47 +184,28 @@ RegistrationRequest::~RegistrationRequest() {}
void RegistrationRequest::Start() {
DCHECK(!callback_.is_null());
- DCHECK(request_info_.android_id != 0UL);
- DCHECK(request_info_.security_token != 0UL);
- DCHECK(0 < request_info_.sender_ids.size() &&
- request_info_.sender_ids.size() <= kMaxSenders);
-
+ DCHECK(request_info_->android_id != 0UL);
+ DCHECK(request_info_->security_token != 0UL);
DCHECK(!url_fetcher_.get());
+
url_fetcher_ =
net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this);
url_fetcher_->SetRequestContext(request_context_getter_.get());
url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
- std::string android_id = base::Uint64ToString(request_info_.android_id);
- std::string auth_header =
- std::string(net::HttpRequestHeaders::kAuthorization) + ": " +
- kLoginHeader + " " + android_id + ":" +
- base::Uint64ToString(request_info_.security_token);
- url_fetcher_->SetExtraRequestHeaders(auth_header);
+ std::string extra_headers;
+ request_info_->BuildRequestHeaders(&extra_headers);
+ url_fetcher_->SetExtraRequestHeaders(extra_headers);
std::string body;
- BuildFormEncoding(kAppIdKey, request_info_.app_id, &body);
- BuildFormEncoding(kDeviceIdKey, android_id, &body);
-
- std::string senders;
- for (std::vector<std::string>::const_iterator iter =
- request_info_.sender_ids.begin();
- iter != request_info_.sender_ids.end();
- ++iter) {
- DCHECK(!iter->empty());
- if (!senders.empty())
- senders.append(",");
- senders.append(*iter);
- }
- BuildFormEncoding(kSenderKey, senders, &body);
- UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount",
- request_info_.sender_ids.size());
+ request_info_->BuildRequestBody(&body);
- DVLOG(1) << "Performing registration for: " << request_info_.app_id;
+ DVLOG(1) << "Performing registration for: " << request_info_->app_id;
DVLOG(1) << "Registration request: " << body;
url_fetcher_->SetUploadData(kRegistrationRequestContentType, body);
- recorder_->RecordRegistrationSent(request_info_.app_id, senders);
+ recorder_->RecordRegistrationSent(request_info_->app_id,
+ request_info_->GetSenders());
request_start_time_ = base::TimeTicks::Now();
url_fetcher_->Start();
}
@@ -175,7 +220,7 @@ void RegistrationRequest::RetryWithBackoff(bool update_backoff) {
if (backoff_entry_.ShouldRejectRequest()) {
DVLOG(1) << "Delaying GCM registration of app: "
- << request_info_.app_id << ", for "
+ << request_info_->app_id << ", for "
<< backoff_entry_.GetTimeUntilRelease().InMilliseconds()
<< " milliseconds.";
base::MessageLoop::current()->PostDelayedTask(
@@ -236,15 +281,15 @@ void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) {
Status status = ParseResponse(source, &token);
RecordRegistrationStatusToUMA(status);
recorder_->RecordRegistrationResponse(
- request_info_.app_id,
- request_info_.sender_ids,
+ request_info_->app_id,
+ request_info_->GetSenders(),
status);
if (ShouldRetryWithStatus(status)) {
if (retries_left_ > 0) {
recorder_->RecordRegistrationRetryRequested(
- request_info_.app_id,
- request_info_.sender_ids,
+ request_info_->app_id,
+ request_info_->GetSenders(),
retries_left_);
RetryWithBackoff(true);
return;
@@ -252,8 +297,8 @@ void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) {
status = REACHED_MAX_RETRIES;
recorder_->RecordRegistrationResponse(
- request_info_.app_id,
- request_info_.sender_ids,
+ request_info_->app_id,
+ request_info_->GetSenders(),
status);
RecordRegistrationStatusToUMA(status);
}

Powered by Google App Engine
This is Rietveld 408576698