blob: a027a7f17a9d3d4f4726665c581482bbd30c3f24 [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"
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
18namespace gcm {
19
20namespace {
21
22const char kGCMChannelStatusRequestURL[] =
23 "https://clients4.google.com/chrome-sync/command/";
24const char kRequestContentType[] = "application/x-protobuf";
25const char kGCMChannelTag[] = "gcm_channel";
26const int kDefaultPollIntervalSeconds = 60 * 60; // 60 minutes.
27const int kMinPollIntervalSeconds = 30 * 60; // 30 minutes.
28
29} // namespace
30
31GCMChannelStatusRequest::GCMChannelStatusRequest(
32 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
33 const GCMChannelStatusRequestCallback& callback)
34 : request_context_getter_(request_context_getter),
35 callback_(callback),
36 backoff_entry_(&(GetGCMBackoffPolicy())),
37 weak_ptr_factory_(this) {
38}
39
40GCMChannelStatusRequest::~GCMChannelStatusRequest() {
41}
42
43// static
44int GCMChannelStatusRequest::default_poll_interval_seconds_for_testing() {
45 return kDefaultPollIntervalSeconds;
46}
47
48// static
49int GCMChannelStatusRequest::min_poll_interval_seconds_for_testing() {
50 return kMinPollIntervalSeconds;
51}
52
53void GCMChannelStatusRequest::Start() {
54 DCHECK(!url_fetcher_.get());
55
56 GURL request_url(kGCMChannelStatusRequestURL);
57
58 gcm_proto::ExperimentStatusRequest proto_data;
59 proto_data.add_experiment_name(kGCMChannelTag);
60 std::string upload_data;
61 DCHECK(proto_data.SerializeToString(&upload_data));
62
63 url_fetcher_.reset(
64 net::URLFetcher::Create(request_url, net::URLFetcher::POST, this));
65 url_fetcher_->SetRequestContext(request_context_getter_.get());
66 url_fetcher_->SetUploadData(kRequestContentType, upload_data);
67 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
68 net::LOAD_DO_NOT_SAVE_COOKIES);
69 url_fetcher_->Start();
70}
71
72void GCMChannelStatusRequest::OnURLFetchComplete(
73 const net::URLFetcher* source) {
74 if (ParseResponse(source))
75 return;
76
77 RetryWithBackoff(true);
78}
79
80bool GCMChannelStatusRequest::ParseResponse(const net::URLFetcher* source) {
81 if (!source->GetStatus().is_success()) {
82 LOG(ERROR) << "GCM channel request failed.";
83 return false;
84 }
85
86 if (source->GetResponseCode() != net::HTTP_OK) {
87 LOG(ERROR) << "GCM channel request failed. HTTP status: "
88 << source->GetResponseCode();
89 return false;
90 }
91
92 std::string response_string;
93 if (!source->GetResponseAsString(&response_string) ||
94 response_string.empty()) {
95 LOG(ERROR) << "GCM channel response failed to be retrieved.";
96 return false;
97 }
98
99 gcm_proto::ExperimentStatusResponse response_proto;
100 if (!response_proto.ParseFromString(response_string)) {
101 LOG(ERROR) << "GCM channel response failed to be parse as proto.";
102 return false;
103 }
104
105 bool enabled = true;
106 if (response_proto.has_gcm_channel() &&
107 response_proto.gcm_channel().has_enabled()) {
108 enabled = response_proto.gcm_channel().enabled();
109 }
110
111 int poll_interval_seconds;
112 if (response_proto.has_poll_interval_seconds())
113 poll_interval_seconds = response_proto.poll_interval_seconds();
114 else
115 poll_interval_seconds = kDefaultPollIntervalSeconds;
116 if (poll_interval_seconds < kMinPollIntervalSeconds)
117 poll_interval_seconds = kMinPollIntervalSeconds;
118
119 callback_.Run(enabled, poll_interval_seconds);
120
121 return true;
122}
123
124void GCMChannelStatusRequest::RetryWithBackoff(bool update_backoff) {
125 if (update_backoff) {
126 url_fetcher_.reset();
127 backoff_entry_.InformOfRequest(false);
128 }
129
130 if (backoff_entry_.ShouldRejectRequest()) {
131 DVLOG(1) << "Delaying GCM channel request for "
132 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
133 << " ms.";
134 base::MessageLoop::current()->PostDelayedTask(
135 FROM_HERE,
136 base::Bind(&GCMChannelStatusRequest::RetryWithBackoff,
137 weak_ptr_factory_.GetWeakPtr(),
138 false),
139 backoff_entry_.GetTimeUntilRelease());
140 return;
141 }
142
143 Start();
144}
145
146} // namespace gcm