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

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: Sync 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..e6ddcf7903ae876d3d9b335f5c0ad992d1b72164 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,91 @@ 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){
+ DCHECK(android_id_ != 0UL && security_token_ != 0UL);
+
+ 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){
+ DCHECK(!instance_id_.empty() && !authorized_entity_.empty() &&
+ !scope_.empty());
+
+ 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 +190,26 @@ 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(!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 +224,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 +285,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 +301,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