jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "components/gcm_driver/gcm_channel_status_request.h" |
| 6 | |
| 7 | #include "base/bind.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 8 | #include "base/location.h" |
| 9 | #include "base/single_thread_task_runner.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 10 | #include "base/threading/thread_task_runner_handle.h" |
rajendrant | bfee22e | 2017-01-20 20:34:17 | [diff] [blame] | 11 | #include "components/data_use_measurement/core/data_use_user_data.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 12 | #include "components/gcm_driver/gcm_backoff_policy.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 13 | #include "components/sync/protocol/experiment_status.pb.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 14 | #include "net/base/escape.h" |
| 15 | #include "net/base/load_flags.h" |
| 16 | #include "net/http/http_status_code.h" |
rhalavati | 85dc6754 | 2017-02-22 13:00:13 | [diff] [blame^] | 17 | #include "net/traffic_annotation/network_traffic_annotation.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 18 | #include "net/url_request/url_fetcher.h" |
| 19 | #include "net/url_request/url_request_status.h" |
| 20 | #include "url/gurl.h" |
| 21 | |
| 22 | namespace gcm { |
| 23 | |
| 24 | namespace { |
| 25 | |
jianli | c6338d7 | 2014-09-16 02:25:11 | [diff] [blame] | 26 | const char kRequestContentType[] = "application/octet-stream"; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 27 | const char kGCMChannelTag[] = "gcm_channel"; |
| 28 | const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes. |
| 29 | const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes. |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | GCMChannelStatusRequest::GCMChannelStatusRequest( |
| 34 | const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 35 | const std::string& channel_status_request_url, |
| 36 | const std::string& user_agent, |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 37 | const GCMChannelStatusRequestCallback& callback) |
| 38 | : request_context_getter_(request_context_getter), |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 39 | channel_status_request_url_(channel_status_request_url), |
| 40 | user_agent_(user_agent), |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 41 | callback_(callback), |
| 42 | backoff_entry_(&(GetGCMBackoffPolicy())), |
| 43 | weak_ptr_factory_(this) { |
| 44 | } |
| 45 | |
| 46 | GCMChannelStatusRequest::~GCMChannelStatusRequest() { |
| 47 | } |
| 48 | |
| 49 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 50 | int GCMChannelStatusRequest::default_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 51 | return kDefaultPollIntervalSeconds; |
| 52 | } |
| 53 | |
| 54 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 55 | int GCMChannelStatusRequest::min_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 56 | return kMinPollIntervalSeconds; |
| 57 | } |
| 58 | |
| 59 | void GCMChannelStatusRequest::Start() { |
| 60 | DCHECK(!url_fetcher_.get()); |
| 61 | |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 62 | GURL request_url(channel_status_request_url_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 63 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 64 | sync_pb::ExperimentStatusRequest proto_data; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 65 | proto_data.add_experiment_name(kGCMChannelTag); |
| 66 | std::string upload_data; |
jianli | 115ee92 | 2014-10-17 21:03:57 | [diff] [blame] | 67 | if (!proto_data.SerializeToString(&upload_data)) { |
| 68 | NOTREACHED(); |
| 69 | } |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 70 | |
rhalavati | 85dc6754 | 2017-02-22 13:00:13 | [diff] [blame^] | 71 | 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); |
rajendrant | bfee22e | 2017-01-20 20:34:17 | [diff] [blame] | 107 | data_use_measurement::DataUseUserData::AttachToFetcher( |
| 108 | url_fetcher_.get(), data_use_measurement::DataUseUserData::GCM_DRIVER); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 109 | url_fetcher_->SetRequestContext(request_context_getter_.get()); |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 110 | url_fetcher_->AddExtraRequestHeader("User-Agent: " + user_agent_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 111 | 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 | |
| 117 | void GCMChannelStatusRequest::OnURLFetchComplete( |
| 118 | const net::URLFetcher* source) { |
| 119 | if (ParseResponse(source)) |
| 120 | return; |
| 121 | |
| 122 | RetryWithBackoff(true); |
| 123 | } |
| 124 | |
| 125 | bool 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 Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 138 | if (!source->GetResponseAsString(&response_string)) { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 139 | LOG(ERROR) << "GCM channel response failed to be retrieved."; |
| 140 | return false; |
| 141 | } |
| 142 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 143 | // Empty response means to keep the existing values. |
| 144 | if (response_string.empty()) { |
| 145 | callback_.Run(false, false, 0); |
| 146 | return true; |
| 147 | } |
| 148 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 149 | sync_pb::ExperimentStatusResponse response_proto; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 150 | if (!response_proto.ParseFromString(response_string)) { |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 151 | LOG(ERROR) << "GCM channel response failed to be parsed as proto."; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| 155 | bool enabled = true; |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 156 | 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(); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 160 | } |
| 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 Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 170 | callback_.Run(true, enabled, poll_interval_seconds); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | void 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."; |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 185 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 186 | FROM_HERE, base::Bind(&GCMChannelStatusRequest::RetryWithBackoff, |
| 187 | weak_ptr_factory_.GetWeakPtr(), false), |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 188 | backoff_entry_.GetTimeUntilRelease()); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | Start(); |
| 193 | } |
| 194 | |
| 195 | } // namespace gcm |