blob: d609f5506bd3690bfe1603f158537413b607faf7 [file] [log] [blame]
jianli1235e51c2014-09-08 18:56:411// 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.
4
5#include "components/gcm_driver/gcm_channel_status_request.h"
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9#include "components/gcm_driver/gcm_backoff_policy.h"
jianli1235e51c2014-09-08 18:56:4110#include "net/base/escape.h"
11#include "net/base/load_flags.h"
12#include "net/http/http_status_code.h"
13#include "net/url_request/url_fetcher.h"
14#include "net/url_request/url_request_status.h"
fgorski3689f8d2014-10-09 04:39:3115#include "sync/protocol/experiment_status.pb.h"
jianli1235e51c2014-09-08 18:56:4116#include "url/gurl.h"
17
18namespace gcm {
19
20namespace {
21
jianlic6338d72014-09-16 02:25:1122const char kRequestContentType[] = "application/octet-stream";
jianli1235e51c2014-09-08 18:56:4123const char kGCMChannelTag[] = "gcm_channel";
24const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes.
25const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes.
26
27} // namespace
28
29GCMChannelStatusRequest::GCMChannelStatusRequest(
30 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
fgorski3f6b84cd2014-10-10 21:04:1831 const std::string& channel_status_request_url,
32 const std::string& user_agent,
jianli1235e51c2014-09-08 18:56:4133 const GCMChannelStatusRequestCallback& callback)
34 : request_context_getter_(request_context_getter),
fgorski3f6b84cd2014-10-10 21:04:1835 channel_status_request_url_(channel_status_request_url),
36 user_agent_(user_agent),
jianli1235e51c2014-09-08 18:56:4137 callback_(callback),
38 backoff_entry_(&(GetGCMBackoffPolicy())),
39 weak_ptr_factory_(this) {
40}
41
42GCMChannelStatusRequest::~GCMChannelStatusRequest() {
43}
44
45// static
jianli2dc910b02014-09-19 02:42:4646int GCMChannelStatusRequest::default_poll_interval_seconds() {
jianli1235e51c2014-09-08 18:56:4147 return kDefaultPollIntervalSeconds;
48}
49
50// static
jianli2dc910b02014-09-19 02:42:4651int GCMChannelStatusRequest::min_poll_interval_seconds() {
jianli1235e51c2014-09-08 18:56:4152 return kMinPollIntervalSeconds;
53}
54
55void GCMChannelStatusRequest::Start() {
56 DCHECK(!url_fetcher_.get());
57
fgorski3f6b84cd2014-10-10 21:04:1858 GURL request_url(channel_status_request_url_);
jianli1235e51c2014-09-08 18:56:4159
fgorski3689f8d2014-10-09 04:39:3160 sync_pb::ExperimentStatusRequest proto_data;
jianli1235e51c2014-09-08 18:56:4161 proto_data.add_experiment_name(kGCMChannelTag);
62 std::string upload_data;
jianli115ee922014-10-17 21:03:5763 if (!proto_data.SerializeToString(&upload_data)) {
64 NOTREACHED();
65 }
jianli1235e51c2014-09-08 18:56:4166
67 url_fetcher_.reset(
68 net::URLFetcher::Create(request_url, net::URLFetcher::POST, this));
69 url_fetcher_->SetRequestContext(request_context_getter_.get());
fgorski3f6b84cd2014-10-10 21:04:1870 url_fetcher_->AddExtraRequestHeader("User-Agent: " + user_agent_);
jianli1235e51c2014-09-08 18:56:4171 url_fetcher_->SetUploadData(kRequestContentType, upload_data);
72 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
73 net::LOAD_DO_NOT_SAVE_COOKIES);
74 url_fetcher_->Start();
75}
76
77void GCMChannelStatusRequest::OnURLFetchComplete(
78 const net::URLFetcher* source) {
79 if (ParseResponse(source))
80 return;
81
82 RetryWithBackoff(true);
83}
84
85bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) {
86 if (!source->GetStatus().is_success()) {
87 LOG(ERROR) << "GCM channel request failed.";
88 return false;
89 }
90
91 if (source->GetResponseCode() != net::HTTP_OK) {
92 LOG(ERROR) << "GCM channel request failed. HTTP status: "
93 << source->GetResponseCode();
94 return false;
95 }
96
97 std::string response_string;
Jian Li74eadb02014-10-16 05:53:5698 if (!source->GetResponseAsString(&response_string)) {
jianli1235e51c2014-09-08 18:56:4199 LOG(ERROR) << "GCM channel response failed to be retrieved.";
100 return false;
101 }
102
Jian Li74eadb02014-10-16 05:53:56103 // Empty response means to keep the existing values.
104 if (response_string.empty()) {
105 callback_.Run(false, false, 0);
106 return true;
107 }
108
fgorski3689f8d2014-10-09 04:39:31109 sync_pb::ExperimentStatusResponse response_proto;
jianli1235e51c2014-09-08 18:56:41110 if (!response_proto.ParseFromString(response_string)) {
Jian Li74eadb02014-10-16 05:53:56111 LOG(ERROR) << "GCM channel response failed to be parsed as proto.";
jianli1235e51c2014-09-08 18:56:41112 return false;
113 }
114
115 bool enabled = true;
fgorski3689f8d2014-10-09 04:39:31116 if (response_proto.experiment_size() == 1 &&
117 response_proto.experiment(0).has_gcm_channel() &&
118 response_proto.experiment(0).gcm_channel().has_enabled()) {
119 enabled = response_proto.experiment(0).gcm_channel().enabled();
jianli1235e51c2014-09-08 18:56:41120 }
121
122 int poll_interval_seconds;
123 if (response_proto.has_poll_interval_seconds())
124 poll_interval_seconds = response_proto.poll_interval_seconds();
125 else
126 poll_interval_seconds = kDefaultPollIntervalSeconds;
127 if (poll_interval_seconds < kMinPollIntervalSeconds)
128 poll_interval_seconds = kMinPollIntervalSeconds;
129
Jian Li74eadb02014-10-16 05:53:56130 callback_.Run(true, enabled, poll_interval_seconds);
jianli1235e51c2014-09-08 18:56:41131
132 return true;
133}
134
135void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) {
136 if (update_backoff) {
137 url_fetcher_.reset();
138 backoff_entry_.InformOfRequest(false);
139 }
140
141 if (backoff_entry_.ShouldRejectRequest()) {
142 DVLOG(1) << "Delaying GCM channel request for "
143 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
144 << " ms.";
145 base::MessageLoop::current()->PostDelayedTask(
146 FROM_HERE,
147 base::Bind(&GCMChannelStatusRequest::RetryWithBackoff,
148 weak_ptr_factory_.GetWeakPtr(),
149 false),
150 backoff_entry_.GetTimeUntilRelease());
151 return;
152 }
153
154 Start();
155}
156
157} // namespace gcm