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" |
| 8 | #include "base/message_loop/message_loop.h" |
| 9 | #include "components/gcm_driver/gcm_backoff_policy.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 10 | #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" |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 15 | #include "sync/protocol/experiment_status.pb.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 16 | #include "url/gurl.h" |
| 17 | |
| 18 | namespace gcm { |
| 19 | |
| 20 | namespace { |
| 21 | |
jianli | c6338d7 | 2014-09-16 02:25:11 | [diff] [blame] | 22 | const char kRequestContentType[] = "application/octet-stream"; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 23 | const char kGCMChannelTag[] = "gcm_channel"; |
| 24 | const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes. |
| 25 | const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes. |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | GCMChannelStatusRequest::GCMChannelStatusRequest( |
| 30 | const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 31 | const std::string& channel_status_request_url, |
| 32 | const std::string& user_agent, |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 33 | const GCMChannelStatusRequestCallback& callback) |
| 34 | : request_context_getter_(request_context_getter), |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 35 | channel_status_request_url_(channel_status_request_url), |
| 36 | user_agent_(user_agent), |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 37 | callback_(callback), |
| 38 | backoff_entry_(&(GetGCMBackoffPolicy())), |
| 39 | weak_ptr_factory_(this) { |
| 40 | } |
| 41 | |
| 42 | GCMChannelStatusRequest::~GCMChannelStatusRequest() { |
| 43 | } |
| 44 | |
| 45 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 46 | int GCMChannelStatusRequest::default_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 47 | return kDefaultPollIntervalSeconds; |
| 48 | } |
| 49 | |
| 50 | // static |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 51 | int GCMChannelStatusRequest::min_poll_interval_seconds() { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 52 | return kMinPollIntervalSeconds; |
| 53 | } |
| 54 | |
| 55 | void GCMChannelStatusRequest::Start() { |
| 56 | DCHECK(!url_fetcher_.get()); |
| 57 | |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 58 | GURL request_url(channel_status_request_url_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 59 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 60 | sync_pb::ExperimentStatusRequest proto_data; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 61 | proto_data.add_experiment_name(kGCMChannelTag); |
| 62 | std::string upload_data; |
jianli | 115ee92 | 2014-10-17 21:03:57 | [diff] [blame^] | 63 | if (!proto_data.SerializeToString(&upload_data)) { |
| 64 | NOTREACHED(); |
| 65 | } |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 66 | |
| 67 | url_fetcher_.reset( |
| 68 | net::URLFetcher::Create(request_url, net::URLFetcher::POST, this)); |
| 69 | url_fetcher_->SetRequestContext(request_context_getter_.get()); |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 70 | url_fetcher_->AddExtraRequestHeader("User-Agent: " + user_agent_); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 71 | 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 | |
| 77 | void GCMChannelStatusRequest::OnURLFetchComplete( |
| 78 | const net::URLFetcher* source) { |
| 79 | if (ParseResponse(source)) |
| 80 | return; |
| 81 | |
| 82 | RetryWithBackoff(true); |
| 83 | } |
| 84 | |
| 85 | bool 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 Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 98 | if (!source->GetResponseAsString(&response_string)) { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 99 | LOG(ERROR) << "GCM channel response failed to be retrieved."; |
| 100 | return false; |
| 101 | } |
| 102 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 103 | // Empty response means to keep the existing values. |
| 104 | if (response_string.empty()) { |
| 105 | callback_.Run(false, false, 0); |
| 106 | return true; |
| 107 | } |
| 108 | |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 109 | sync_pb::ExperimentStatusResponse response_proto; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 110 | if (!response_proto.ParseFromString(response_string)) { |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 111 | LOG(ERROR) << "GCM channel response failed to be parsed as proto."; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool enabled = true; |
fgorski | 3689f8d | 2014-10-09 04:39:31 | [diff] [blame] | 116 | 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(); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 120 | } |
| 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 Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 130 | callback_.Run(true, enabled, poll_interval_seconds); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | void 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 |