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
3 // found in the LICENSE file.
5 #include "components/gcm_driver/gcm_channel_status_request.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/gcm_driver/gcm_backoff_policy.h"
10 #include "components/gcm_driver/proto/gcm_channel_status.pb.h"
11 #include "net/base/escape.h"
12 #include "net/base/load_flags.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_request_status.h"
22 const char kGCMChannelStatusRequestURL[] =
23 "https://clients4.google.com/chrome-sync/command/";
24 const char kRequestContentType[] = "application/octet-stream";
25 const char kGCMChannelTag[] = "gcm_channel";
26 const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes.
27 const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes.
31 GCMChannelStatusRequest::GCMChannelStatusRequest(
32 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
33 const GCMChannelStatusRequestCallback& callback)
34 : request_context_getter_(request_context_getter),
36 backoff_entry_(&(GetGCMBackoffPolicy())),
37 weak_ptr_factory_(this) {
40 GCMChannelStatusRequest::~GCMChannelStatusRequest() {
44 int GCMChannelStatusRequest::default_poll_interval_seconds() {
45 return kDefaultPollIntervalSeconds;
49 int GCMChannelStatusRequest::min_poll_interval_seconds() {
50 return kMinPollIntervalSeconds;
53 void GCMChannelStatusRequest::Start() {
54 DCHECK(!url_fetcher_.get());
56 GURL request_url(kGCMChannelStatusRequestURL);
58 gcm_proto::ExperimentStatusRequest proto_data;
59 proto_data.add_experiment_name(kGCMChannelTag);
60 std::string upload_data;
61 DCHECK(proto_data.SerializeToString(&upload_data));
64 net::URLFetcher::Create(request_url, net::URLFetcher::POST, this));
65 url_fetcher_->SetRequestContext(request_context_getter_.get());
66 url_fetcher_->SetUploadData(kRequestContentType, upload_data);
67 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
68 net::LOAD_DO_NOT_SAVE_COOKIES);
69 url_fetcher_->Start();
72 void GCMChannelStatusRequest::OnURLFetchComplete(
73 const net::URLFetcher* source) {
74 if (ParseResponse(source))
77 RetryWithBackoff(true);
80 bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) {
81 if (!source->GetStatus().is_success()) {
82 LOG(ERROR) << "GCM channel request failed.";
86 if (source->GetResponseCode() != net::HTTP_OK) {
87 LOG(ERROR) << "GCM channel request failed. HTTP status: "
88 << source->GetResponseCode();
92 std::string response_string;
93 if (!source->GetResponseAsString(&response_string) ||
94 response_string.empty()) {
95 LOG(ERROR) << "GCM channel response failed to be retrieved.";
99 gcm_proto::ExperimentStatusResponse response_proto;
100 if (!response_proto.ParseFromString(response_string)) {
101 LOG(ERROR) << "GCM channel response failed to be parse as proto.";
106 if (response_proto.has_gcm_channel() &&
107 response_proto.gcm_channel().has_enabled()) {
108 enabled = response_proto.gcm_channel().enabled();
111 int poll_interval_seconds;
112 if (response_proto.has_poll_interval_seconds())
113 poll_interval_seconds = response_proto.poll_interval_seconds();
115 poll_interval_seconds = kDefaultPollIntervalSeconds;
116 if (poll_interval_seconds < kMinPollIntervalSeconds)
117 poll_interval_seconds = kMinPollIntervalSeconds;
119 callback_.Run(enabled, poll_interval_seconds);
124 void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) {
125 if (update_backoff) {
126 url_fetcher_.reset();
127 backoff_entry_.InformOfRequest(false);
130 if (backoff_entry_.ShouldRejectRequest()) {
131 DVLOG(1) << "Delaying GCM channel request for "
132 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
134 base::MessageLoop::current()->PostDelayedTask(
136 base::Bind(&GCMChannelStatusRequest::RetryWithBackoff,
137 weak_ptr_factory_.GetWeakPtr(),
139 backoff_entry_.GetTimeUntilRelease());