Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 10 #include "components/gcm_driver/proto/gcm_channel_status.pb.h" | |
| 11 #include "net/base/escape.h" | |
| 12 #include "net/base/load_flags.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 #include "net/url_request/url_request_status.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace gcm { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kGCMChannelStatusRequestURL[] = | |
| 23 "https://clients4.google.com/chrome-sync/command/"; | |
| 24 const char kClientParameter[] = "?client="; | |
| 25 const char kClientIDParameter[] = "&client_id="; | |
| 26 const char kRequestContentType[] = "application/x-protobuf"; | |
| 27 const char kGCMChannelTag[] = "gcm_channel"; | |
| 28 const base::TimeDelta kDefaultPollInterval = base::TimeDelta::FromMinutes(60); | |
|
Nicolas Zea
2014/09/04 23:57:50
const values should be POD, not classes like TimeD
Nicolas Zea
2014/09/04 23:58:12
Rather, global values should be POD
jianli
2014/09/05 18:41:55
Done.
jianli
2014/09/05 18:41:55
Done.
| |
| 29 const base::TimeDelta kMinPollInterval = base::TimeDelta::FromMinutes(30); | |
| 30 | |
| 31 #if defined(GOOGLE_CHROME_BUILD) | |
| 32 const char kClientName[] = "Google Chrome"; | |
|
Nicolas Zea
2014/09/04 23:57:50
Why do we differentiate?
jianli
2014/09/05 18:41:55
This is to differentiate between official build an
Nicolas Zea
2014/09/05 18:53:03
Why do we care in this case?
jianli
2014/09/05 21:26:17
Removed. We don't need this any more.
| |
| 33 #else | |
| 34 const char kClientName[] = "Chromium"; | |
| 35 #endif // defined(GOOGLE_CHROME_BUILD) | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 GCMChannelStatusRequest::GCMChannelStatusRequest( | |
| 40 const std::string& client_id, | |
| 41 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
| 42 const GCMChannelStatusRequestCallback& callback) | |
| 43 : client_id_(client_id), | |
| 44 request_context_getter_(request_context_getter), | |
| 45 callback_(callback), | |
| 46 backoff_entry_(&(GetGCMBackoffPolicy())), | |
| 47 weak_ptr_factory_(this) { | |
| 48 } | |
| 49 | |
| 50 GCMChannelStatusRequest::~GCMChannelStatusRequest() { | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 base::TimeDelta GCMChannelStatusRequest::default_poll_interval_for_testing() { | |
| 55 return kDefaultPollInterval; | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 base::TimeDelta GCMChannelStatusRequest::min_poll_interval_for_testing() { | |
| 60 return kMinPollInterval; | |
| 61 } | |
| 62 | |
| 63 void GCMChannelStatusRequest::Start() { | |
| 64 DCHECK(!url_fetcher_.get()); | |
| 65 | |
| 66 std::string request_url_string(kGCMChannelStatusRequestURL); | |
| 67 request_url_string += kClientParameter; | |
| 68 request_url_string += net::EscapeUrlEncodedData(kClientName, true); | |
| 69 request_url_string += kClientIDParameter; | |
| 70 request_url_string += net::EscapeUrlEncodedData(client_id_, true); | |
| 71 GURL request_url(request_url_string); | |
| 72 | |
| 73 gcm_proto::ExperimentStatusRequest proto_data; | |
| 74 proto_data.add_experiment_name(kGCMChannelTag); | |
| 75 std::string upload_data; | |
| 76 CHECK(proto_data.SerializeToString(&upload_data)); | |
|
Nicolas Zea
2014/09/04 23:57:50
Why is this a CHECK?
jianli
2014/09/05 18:41:54
Changed to DCHECK.
| |
| 77 | |
| 78 url_fetcher_.reset( | |
| 79 net::URLFetcher::Create(request_url, net::URLFetcher::POST, this)); | |
| 80 url_fetcher_->SetRequestContext(request_context_getter_); | |
| 81 url_fetcher_->SetUploadData(kRequestContentType, upload_data); | |
| 82 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 83 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 84 url_fetcher_->Start(); | |
| 85 } | |
| 86 | |
| 87 void GCMChannelStatusRequest::OnURLFetchComplete( | |
| 88 const net::URLFetcher* source) { | |
| 89 if (ParseResponse(source)) | |
| 90 return; | |
| 91 | |
| 92 RetryWithBackoff(true); | |
| 93 } | |
| 94 | |
| 95 bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) { | |
| 96 if (!source->GetStatus().is_success()) { | |
| 97 LOG(ERROR) << "GCM channel request failed."; | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 if (source->GetResponseCode() != net::HTTP_OK) { | |
| 102 LOG(ERROR) << "GCM channel request failed. HTTP status: " | |
| 103 << source->GetResponseCode(); | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 std::string response_string; | |
| 108 if (!source->GetResponseAsString(&response_string) || | |
| 109 response_string.empty()) { | |
| 110 LOG(ERROR) << "GCM channel response failed to be retrieved."; | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 gcm_proto::ExperimentStatusResponse response_proto; | |
| 115 if (!response_proto.ParseFromString(response_string)) { | |
| 116 LOG(ERROR) << "GCM channel response failed to be parse as proto."; | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 bool enabled = true; | |
| 121 if (response_proto.has_gcm_channel() && | |
| 122 response_proto.gcm_channel().has_enabled()) { | |
| 123 enabled = response_proto.gcm_channel().enabled(); | |
| 124 } | |
| 125 | |
| 126 base::TimeDelta poll_interval; | |
| 127 if (response_proto.has_poll_interval_seconds()) { | |
| 128 poll_interval = base::TimeDelta::FromSeconds( | |
| 129 response_proto.poll_interval_seconds()); | |
| 130 } else { | |
| 131 poll_interval = kDefaultPollInterval; | |
| 132 } | |
| 133 if (poll_interval < kMinPollInterval) | |
| 134 poll_interval = kMinPollInterval; | |
| 135 | |
| 136 callback_.Run(enabled, poll_interval); | |
| 137 | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) { | |
| 142 if (update_backoff) { | |
| 143 url_fetcher_.reset(); | |
| 144 backoff_entry_.InformOfRequest(false); | |
| 145 } | |
| 146 | |
| 147 if (backoff_entry_.ShouldRejectRequest()) { | |
| 148 DVLOG(1) << "Delaying GCM channel request for " | |
| 149 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() | |
| 150 << " ms."; | |
| 151 base::MessageLoop::current()->PostDelayedTask( | |
| 152 FROM_HERE, | |
| 153 base::Bind(&GCMChannelStatusRequest::RetryWithBackoff, | |
| 154 weak_ptr_factory_.GetWeakPtr(), | |
| 155 false), | |
| 156 backoff_entry_.GetTimeUntilRelease()); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 Start(); | |
| 161 } | |
| 162 | |
| 163 } // namespace gcm | |
| OLD | NEW |