blob: e59e14d2df6f432ed9fffbf6f575fcca17c36e51 [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#ifndef COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_
6#define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_
7
dchenga77e28eb2016-04-21 21:34:378#include <memory>
9
jianli1235e51c2014-09-08 18:56:4110#include "base/callback.h"
11#include "base/compiler_specific.h"
jianli115ee922014-10-17 21:03:5712#include "base/gtest_prod_util.h"
jianli1235e51c2014-09-08 18:56:4113#include "base/macros.h"
14#include "base/memory/ref_counted.h"
jianli1235e51c2014-09-08 18:56:4115#include "base/memory/weak_ptr.h"
16#include "net/base/backoff_entry.h"
jianli1235e51c2014-09-08 18:56:4117
Mark Pilgrim7634f5b52018-06-27 19:53:2718namespace network {
19class SharedURLLoaderFactory;
20class SimpleURLLoader;
21} // namespace network
22
23namespace sync_pb {
24class ExperimentStatusResponse;
jianli1235e51c2014-09-08 18:56:4125}
26
27namespace gcm {
28
29// Defines the request to talk with the server to determine if the GCM support
30// should be enabled.
Mark Pilgrim7634f5b52018-06-27 19:53:2731class GCMChannelStatusRequest {
jianli1235e51c2014-09-08 18:56:4132 public:
33 // Callback completing the channel status request.
Jian Li74eadb02014-10-16 05:53:5634 // |update_received|: use the existing values if it is false which means no
35 // update is received.
36 // |enabled|: indicates if GCM is enabled (allowed to run) or not.
37 // |poll_interval_seconds|: the interval in seconds to start next poll
38 // request.
39 typedef base::Callback<void(bool update_received,
40 bool enabled,
41 int poll_interval_seconds)>
jianli1235e51c2014-09-08 18:56:4142 GCMChannelStatusRequestCallback;
43
44 GCMChannelStatusRequest(
Mark Pilgrim7634f5b52018-06-27 19:53:2745 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
fgorski3f6b84cd2014-10-10 21:04:1846 const std::string& channel_status_request_url,
47 const std::string& user_agent,
jianli1235e51c2014-09-08 18:56:4148 const GCMChannelStatusRequestCallback& callback);
Mark Pilgrim7634f5b52018-06-27 19:53:2749 ~GCMChannelStatusRequest();
jianli1235e51c2014-09-08 18:56:4150
51 void Start();
52
jianli2dc910b02014-09-19 02:42:4653 static int default_poll_interval_seconds();
54 static int min_poll_interval_seconds();
jianli1235e51c2014-09-08 18:56:4155
Mark Pilgrim7634f5b52018-06-27 19:53:2756 // Public so tests can use it.
57 void ParseResponseProto(sync_pb::ExperimentStatusResponse response_proto);
58
jianli1235e51c2014-09-08 18:56:4159 private:
jianli115ee922014-10-17 21:03:5760 FRIEND_TEST_ALL_PREFIXES(GCMChannelStatusRequestTest, RequestData);
61
Mark Pilgrim7634f5b52018-06-27 19:53:2762 void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
63 bool ParseResponse(std::unique_ptr<std::string> response_body);
jianli1235e51c2014-09-08 18:56:4164 void RetryWithBackoff(bool update_backoff);
65
Mark Pilgrim7634f5b52018-06-27 19:53:2766 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
fgorski3f6b84cd2014-10-10 21:04:1867 const std::string channel_status_request_url_;
68 const std::string user_agent_;
jianli1235e51c2014-09-08 18:56:4169 GCMChannelStatusRequestCallback callback_;
Mark Pilgrim7634f5b52018-06-27 19:53:2770 std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
jianli1235e51c2014-09-08 18:56:4171 net::BackoffEntry backoff_entry_;
72 base::WeakPtrFactory<GCMChannelStatusRequest> weak_ptr_factory_;
73
74 DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusRequest);
75};
76
77} // namespace gcm
78
79#endif // COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_