blob: b91b4454299a27c7911458a6a2f1b620b2fb9986 [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"
skyostilb0daa012015-06-02 19:03:488#include "base/location.h"
9#include "base/single_thread_task_runner.h"
gab7966d312016-05-11 20:35:0110#include "base/threading/thread_task_runner_handle.h"
rajendrantbfee22e2017-01-20 20:34:1711#include "components/data_use_measurement/core/data_use_user_data.h"
jianli1235e51c2014-09-08 18:56:4112#include "components/gcm_driver/gcm_backoff_policy.h"
Max Boguefef332d2016-07-28 22:09:0913#include "components/sync/protocol/experiment_status.pb.h"
jianli1235e51c2014-09-08 18:56:4114#include "net/base/escape.h"
15#include "net/base/load_flags.h"
16#include "net/http/http_status_code.h"
rhalavati85dc67542017-02-22 13:00:1317#include "net/traffic_annotation/network_traffic_annotation.h"
jianli1235e51c2014-09-08 18:56:4118#include "net/url_request/url_fetcher.h"
19#include "net/url_request/url_request_status.h"
20#include "url/gurl.h"
21
22namespace gcm {
23
24namespace {
25
jianlic6338d72014-09-16 02:25:1126const char kRequestContentType[] = "application/octet-stream";
jianli1235e51c2014-09-08 18:56:4127const char kGCMChannelTag[] = "gcm_channel";
28const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes.
29const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes.
30
31} // namespace
32
33GCMChannelStatusRequest::GCMChannelStatusRequest(
34 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
fgorski3f6b84cd2014-10-10 21:04:1835 const std::string& channel_status_request_url,
36 const std::string& user_agent,
jianli1235e51c2014-09-08 18:56:4137 const GCMChannelStatusRequestCallback& callback)
38 : request_context_getter_(request_context_getter),
fgorski3f6b84cd2014-10-10 21:04:1839 channel_status_request_url_(channel_status_request_url),
40 user_agent_(user_agent),
jianli1235e51c2014-09-08 18:56:4141 callback_(callback),
42 backoff_entry_(&(GetGCMBackoffPolicy())),
43 weak_ptr_factory_(this) {
44}
45
46GCMChannelStatusRequest::~GCMChannelStatusRequest() {
47}
48
49// static
jianli2dc910b02014-09-19 02:42:4650int GCMChannelStatusRequest::default_poll_interval_seconds() {
jianli1235e51c2014-09-08 18:56:4151 return kDefaultPollIntervalSeconds;
52}
53
54// static
jianli2dc910b02014-09-19 02:42:4655int GCMChannelStatusRequest::min_poll_interval_seconds() {
jianli1235e51c2014-09-08 18:56:4156 return kMinPollIntervalSeconds;
57}
58
59void GCMChannelStatusRequest::Start() {
60 DCHECK(!url_fetcher_.get());
61
fgorski3f6b84cd2014-10-10 21:04:1862 GURL request_url(channel_status_request_url_);
jianli1235e51c2014-09-08 18:56:4163
fgorski3689f8d2014-10-09 04:39:3164 sync_pb::ExperimentStatusRequest proto_data;
jianli1235e51c2014-09-08 18:56:4165 proto_data.add_experiment_name(kGCMChannelTag);
66 std::string upload_data;
jianli115ee922014-10-17 21:03:5767 if (!proto_data.SerializeToString(&upload_data)) {
68 NOTREACHED();
69 }
jianli1235e51c2014-09-08 18:56:4170
rhalavati85dc67542017-02-22 13:00:1371 net::NetworkTrafficAnnotationTag traffic_annotation =
72 net::DefineNetworkTrafficAnnotation("gcm_channel_status_request", R"(
73 semantics {
74 sender: "GCM Driver"
75 description:
76 "Google Chrome interacts with Google Cloud Messaging to receive "
77 "push messages for various browser features, as well as on behalf "
78 "of websites and extensions. The channel status request "
79 "periodically confirms with Google servers whether the feature "
80 "should be enabled."
81 trigger:
82 "Periodically when Chrome has established an active Google Cloud "
83 "Messaging subscription. The first request will be issued a minute "
84 "after the first subscription activates. Subsequent requests will "
85 "be issued each hour with a jitter of 15 minutes. Google can "
86 "adjust this interval when it deems necessary."
87 data:
88 "A user agent string containing the Chrome version, channel and "
89 "platform will be sent to the server. No user identifier is sent "
90 "along with the request."
91 destination: GOOGLE_OWNED_SERVICE
92 }
93 policy {
94 cookies_allowed: false
95 setting:
96 "Support for interacting with Google Cloud Messaging is enabled by "
97 "default, and there is no configuration option to completely "
98 "disable it. Websites wishing to receive push messages must "
99 "acquire express permission from the user for the 'Notification' "
100 "permission."
101 policy_exception_justification:
102 "Not implemented, considered not useful."
103 })");
104
105 url_fetcher_ = net::URLFetcher::Create(request_url, net::URLFetcher::POST,
106 this, traffic_annotation);
rajendrantbfee22e2017-01-20 20:34:17107 data_use_measurement::DataUseUserData::AttachToFetcher(
108 url_fetcher_.get(), data_use_measurement::DataUseUserData::GCM_DRIVER);
jianli1235e51c2014-09-08 18:56:41109 url_fetcher_->SetRequestContext(request_context_getter_.get());
fgorski3f6b84cd2014-10-10 21:04:18110 url_fetcher_->AddExtraRequestHeader("User-Agent: " + user_agent_);
jianli1235e51c2014-09-08 18:56:41111 url_fetcher_->SetUploadData(kRequestContentType, upload_data);
112 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
113 net::LOAD_DO_NOT_SAVE_COOKIES);
114 url_fetcher_->Start();
115}
116
117void GCMChannelStatusRequest::OnURLFetchComplete(
118 const net::URLFetcher* source) {
119 if (ParseResponse(source))
120 return;
121
122 RetryWithBackoff(true);
123}
124
125bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) {
126 if (!source->GetStatus().is_success()) {
127 LOG(ERROR) << "GCM channel request failed.";
128 return false;
129 }
130
131 if (source->GetResponseCode() != net::HTTP_OK) {
132 LOG(ERROR) << "GCM channel request failed. HTTP status: "
133 << source->GetResponseCode();
134 return false;
135 }
136
137 std::string response_string;
Jian Li74eadb02014-10-16 05:53:56138 if (!source->GetResponseAsString(&response_string)) {
jianli1235e51c2014-09-08 18:56:41139 LOG(ERROR) << "GCM channel response failed to be retrieved.";
140 return false;
141 }
142
Jian Li74eadb02014-10-16 05:53:56143 // Empty response means to keep the existing values.
144 if (response_string.empty()) {
145 callback_.Run(false, false, 0);
146 return true;
147 }
148
fgorski3689f8d2014-10-09 04:39:31149 sync_pb::ExperimentStatusResponse response_proto;
jianli1235e51c2014-09-08 18:56:41150 if (!response_proto.ParseFromString(response_string)) {
Jian Li74eadb02014-10-16 05:53:56151 LOG(ERROR) << "GCM channel response failed to be parsed as proto.";
jianli1235e51c2014-09-08 18:56:41152 return false;
153 }
154
155 bool enabled = true;
fgorski3689f8d2014-10-09 04:39:31156 if (response_proto.experiment_size() == 1 &&
157 response_proto.experiment(0).has_gcm_channel() &&
158 response_proto.experiment(0).gcm_channel().has_enabled()) {
159 enabled = response_proto.experiment(0).gcm_channel().enabled();
jianli1235e51c2014-09-08 18:56:41160 }
161
162 int poll_interval_seconds;
163 if (response_proto.has_poll_interval_seconds())
164 poll_interval_seconds = response_proto.poll_interval_seconds();
165 else
166 poll_interval_seconds = kDefaultPollIntervalSeconds;
167 if (poll_interval_seconds < kMinPollIntervalSeconds)
168 poll_interval_seconds = kMinPollIntervalSeconds;
169
Jian Li74eadb02014-10-16 05:53:56170 callback_.Run(true, enabled, poll_interval_seconds);
jianli1235e51c2014-09-08 18:56:41171
172 return true;
173}
174
175void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) {
176 if (update_backoff) {
177 url_fetcher_.reset();
178 backoff_entry_.InformOfRequest(false);
179 }
180
181 if (backoff_entry_.ShouldRejectRequest()) {
182 DVLOG(1) << "Delaying GCM channel request for "
183 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
184 << " ms.";
skyostilb0daa012015-06-02 19:03:48185 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
186 FROM_HERE, base::Bind(&GCMChannelStatusRequest::RetryWithBackoff,
187 weak_ptr_factory_.GetWeakPtr(), false),
jianli1235e51c2014-09-08 18:56:41188 backoff_entry_.GetTimeUntilRelease());
189 return;
190 }
191
192 Start();
193}
194
195} // namespace gcm