Chromium Code Reviews| Index: components/gcm_driver/gcm_channel_status_request.cc |
| diff --git a/components/gcm_driver/gcm_channel_status_request.cc b/components/gcm_driver/gcm_channel_status_request.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..030b556550cb25bd1efdda3c345f505fc8682c2c |
| --- /dev/null |
| +++ b/components/gcm_driver/gcm_channel_status_request.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/gcm_driver/gcm_channel_status_request.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "components/gcm_driver/gcm_backoff_policy.h" |
| +#include "components/gcm_driver/proto/gcm_channel_status.pb.h" |
| +#include "net/base/escape.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "url/gurl.h" |
| + |
| +namespace gcm { |
| + |
| +namespace { |
| + |
| +const char kGCMChannelStatusRequestURL[] = |
| + "https://clients4.google.com/chrome-sync/command/"; |
| +const char kClientParameter[] = "?client="; |
| +const char kClientIDParameter[] = "&client_id="; |
| +const char kRequestContentType[] = "application/x-protobuf"; |
| +const char kGCMChannelTag[] = "gcm_channel"; |
| +const base::TimeDelta kDefaultPollInterval = base::TimeDelta::FromMinutes(60); |
| +const base::TimeDelta kMinPollInterval = base::TimeDelta::FromMinutes(30); |
| + |
| +#if defined(GOOGLE_CHROME_BUILD) |
| +const char kClientName[] = "Google Chrome"; |
| +#else |
| +const char kClientName[] = "Chromium"; |
| +#endif // defined(GOOGLE_CHROME_BUILD) |
| + |
| +} // namespace |
| + |
| +GCMChannelStatusRequest::GCMChannelStatusRequest( |
| + const std::string& client_id, |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| + const GCMChannelStatusRequestCallback& callback) |
| + : client_id_(client_id), |
| + request_context_getter_(request_context_getter), |
| + callback_(callback), |
| + backoff_entry_(&(GetGCMBackoffPolicy())), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +GCMChannelStatusRequest::~GCMChannelStatusRequest() { |
| +} |
| + |
| +// static |
| +base::TimeDelta GCMChannelStatusRequest::default_poll_interval_for_testing() { |
| + return kDefaultPollInterval; |
| +} |
| + |
| +// static |
| +base::TimeDelta GCMChannelStatusRequest::min_poll_interval_for_testing() { |
| + return kMinPollInterval; |
| +} |
| + |
| +void GCMChannelStatusRequest::Start() { |
| + DCHECK(!url_fetcher_.get()); |
| + |
| + std::string request_url_string(kGCMChannelStatusRequestURL); |
| + request_url_string += kClientParameter; |
| + request_url_string += net::EscapeUrlEncodedData(kClientName, true); |
| + request_url_string += kClientIDParameter; |
| + request_url_string += net::EscapeUrlEncodedData(client_id_, true); |
| + GURL request_url(request_url_string); |
| + |
| + gcm_proto::ExperimentStatusRequest proto_data; |
| + proto_data.add_experiment_name(kGCMChannelTag); |
| + std::string upload_data; |
| + CHECK(proto_data.SerializeToString(&upload_data)); |
| + |
| + url_fetcher_.reset( |
| + net::URLFetcher::Create(request_url, net::URLFetcher::POST, this)); |
| + url_fetcher_->SetRequestContext(request_context_getter_); |
| + url_fetcher_->SetUploadData(kRequestContentType, upload_data); |
| + url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
|
fgorski
2014/09/03 20:42:16
Did you check with sync that we are not supposed t
jianli
2014/09/03 21:17:55
Anyone can talk with the server without authentica
|
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + url_fetcher_->Start(); |
| +} |
| + |
| +void GCMChannelStatusRequest::OnURLFetchComplete( |
| + const net::URLFetcher* source) { |
| + if (ParseResponse(source)) |
| + return; |
| + |
| + RetryWithBackoff(true); |
| +} |
| + |
| +bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) { |
| + if (!source->GetStatus().is_success()) { |
| + LOG(ERROR) << "GCM channel request failed."; |
| + return false; |
| + } |
| + |
| + if (source->GetResponseCode() != net::HTTP_OK) { |
| + LOG(ERROR) << "GCM channel request failed. HTTP status: " |
| + << source->GetResponseCode(); |
| + return false; |
| + } |
| + |
| + std::string response_string; |
| + if (!source->GetResponseAsString(&response_string) || |
| + response_string.empty()) { |
| + LOG(ERROR) << "GCM channel response failed to be retrieved."; |
| + return false; |
| + } |
| + |
| + gcm_proto::ExperimentStatusResponse response_proto; |
| + if (!response_proto.ParseFromString(response_string)) { |
| + LOG(ERROR) << "GCM channel response failed to be parse as proto."; |
| + return false; |
| + } |
| + |
| + bool enabled = true; |
| + if (response_proto.has_gcm_channel() && |
| + response_proto.gcm_channel().has_enabled()) { |
| + enabled = response_proto.gcm_channel().enabled(); |
| + } |
| + |
| + base::TimeDelta poll_interval; |
| + if (response_proto.has_poll_interval_seconds()) { |
| + poll_interval = base::TimeDelta::FromSeconds( |
| + response_proto.poll_interval_seconds()); |
| + } else { |
| + poll_interval = kDefaultPollInterval; |
| + } |
| + if (poll_interval < kMinPollInterval) |
| + poll_interval = kMinPollInterval; |
| + |
| + callback_.Run(enabled, poll_interval); |
| + |
| + return true; |
| +} |
| + |
| +void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) { |
| + if (update_backoff) { |
| + url_fetcher_.reset(); |
| + backoff_entry_.InformOfRequest(false); |
| + } |
| + |
| + if (backoff_entry_.ShouldRejectRequest()) { |
| + DVLOG(1) << "Delaying GCM channel request for " |
| + << backoff_entry_.GetTimeUntilRelease().InMilliseconds() |
| + << " ms."; |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&GCMChannelStatusRequest::RetryWithBackoff, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + false), |
| + backoff_entry_.GetTimeUntilRelease()); |
| + return; |
| + } |
| + |
| + Start(); |
| +} |
| + |
| +} // namespace gcm |