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" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 11 | #include "components/gcm_driver/gcm_backoff_policy.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 12 | #include "net/base/escape.h" |
| 13 | #include "net/base/load_flags.h" |
| 14 | #include "net/http/http_status_code.h" |
| 15 | #include "net/url_request/url_fetcher.h" |
| 16 | #include "net/url_request/url_request_status.h" |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 17 | #include "sync/protocol/experiment_status.pb.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 18 | #include "url/gurl.h" |
| 19 | |
| 20 | namespace gcm { |
| 21 | |
| 22 | namespace { |
| 23 | |
jianli | c6338d7 | 2014-09-16 02:25:11 | [diff] [blame] | 24 | const char kRequestContentType[] = "application/octet-stream"; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 25 | const char kGCMChannelTag[] = "gcm_channel"; |
| 26 | const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes. |
| 27 | const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes. |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | GCMChannelStatusRequest::GCMChannelStatusRequest( |
| 32 | const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 33 | const std::string& channel_status_request_url, |
| 34 | const std::string& user_agent, |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 35 | const GCMChannelStatusRequestCallback& callback) |
| 36 | : request_context_getter_(request_context_getter), |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 37 | channel_status_request_url_(channel_status_request_url), |
| 38 | user_agent_(user_agent), |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 39 | callback_(callback), |
| 40 | backoff_entry_(&(GetGCMBackoffPolicy())), |
| 41 | weak_ptr_factory_(this) { |
| 42 | } |
| 43 | |
| 44 | GCMChannelStatusRequest::~GCMChannelStatusRequest() { |
| 45 | } |
| 46 | |
| 47 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 48 | int GCMChannelStatusRequest::default_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 49 | return kDefaultPollIntervalSeconds; |
| 50 | } |
| 51 | |
| 52 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 53 | int GCMChannelStatusRequest::min_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 54 | return kMinPollIntervalSeconds; |
| 55 | } |
| 56 | |
| 57 | void GCMChannelStatusRequest::Start() { |
| 58 | DCHECK(!url_fetcher_.get()); |
| 59 | |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 60 | GURL request_url(channel_status_request_url_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 61 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 62 | sync_pb::ExperimentStatusRequest proto_data; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 63 | proto_data.add_experiment_name(kGCMChannelTag); |
| 64 | std::string upload_data; |
jianli | 115ee92 | 2014-10-17 21:03:57 | [diff] [blame] | 65 | if (!proto_data.SerializeToString(&upload_data)) { |
| 66 | NOTREACHED(); |
| 67 | } |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 68 | |
dtapuska | dafcf89 | 2015-05-01 13:58:25 | [diff] [blame] | 69 | url_fetcher_ = |
| 70 | net::URLFetcher::Create(request_url, net::URLFetcher::POST, this); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 71 | url_fetcher_->SetRequestContext(request_context_getter_.get()); |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 72 | url_fetcher_->AddExtraRequestHeader("User-Agent: " + user_agent_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 73 | url_fetcher_->SetUploadData(kRequestContentType, upload_data); |
| 74 | url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 75 | net::LOAD_DO_NOT_SAVE_COOKIES); |
| 76 | url_fetcher_->Start(); |
| 77 | } |
| 78 | |
| 79 | void GCMChannelStatusRequest::OnURLFetchComplete( |
| 80 | const net::URLFetcher* source) { |
| 81 | if (ParseResponse(source)) |
| 82 | return; |
| 83 | |
| 84 | RetryWithBackoff(true); |
| 85 | } |
| 86 | |
| 87 | bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) { |
| 88 | if (!source->GetStatus().is_success()) { |
| 89 | LOG(ERROR) << "GCM channel request failed."; |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (source->GetResponseCode() != net::HTTP_OK) { |
| 94 | LOG(ERROR) << "GCM channel request failed. HTTP status: " |
| 95 | << source->GetResponseCode(); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | std::string response_string; |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 100 | if (!source->GetResponseAsString(&response_string)) { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 101 | LOG(ERROR) << "GCM channel response failed to be retrieved."; |
| 102 | return false; |
| 103 | } |
| 104 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 105 | // Empty response means to keep the existing values. |
| 106 | if (response_string.empty()) { |
| 107 | callback_.Run(false, false, 0); |
| 108 | return true; |
| 109 | } |
| 110 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 111 | sync_pb::ExperimentStatusResponse response_proto; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 112 | if (!response_proto.ParseFromString(response_string)) { |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 113 | LOG(ERROR) << "GCM channel response failed to be parsed as proto."; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool enabled = true; |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 118 | if (response_proto.experiment_size() == 1 && |
| 119 | response_proto.experiment(0).has_gcm_channel() && |
| 120 | response_proto.experiment(0).gcm_channel().has_enabled()) { |
| 121 | enabled = response_proto.experiment(0).gcm_channel().enabled(); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int poll_interval_seconds; |
| 125 | if (response_proto.has_poll_interval_seconds()) |
| 126 | poll_interval_seconds = response_proto.poll_interval_seconds(); |
| 127 | else |
| 128 | poll_interval_seconds = kDefaultPollIntervalSeconds; |
| 129 | if (poll_interval_seconds < kMinPollIntervalSeconds) |
| 130 | poll_interval_seconds = kMinPollIntervalSeconds; |
| 131 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 132 | callback_.Run(true, enabled, poll_interval_seconds); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) { |
| 138 | if (update_backoff) { |
| 139 | url_fetcher_.reset(); |
| 140 | backoff_entry_.InformOfRequest(false); |
| 141 | } |
| 142 | |
| 143 | if (backoff_entry_.ShouldRejectRequest()) { |
| 144 | DVLOG(1) << "Delaying GCM channel request for " |
| 145 | << backoff_entry_.GetTimeUntilRelease().InMilliseconds() |
| 146 | << " ms."; |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 147 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 148 | FROM_HERE, base::Bind(&GCMChannelStatusRequest::RetryWithBackoff, |
| 149 | weak_ptr_factory_.GetWeakPtr(), false), |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 150 | backoff_entry_.GetTimeUntilRelease()); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | Start(); |
| 155 | } |
| 156 | |
| 157 | } // namespace gcm |