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 | #ifndef COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_ |
| 7 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 10 | #include "base/callback.h" |
| 11 | #include "base/compiler_specific.h" |
jianli | 115ee92 | 2014-10-17 21:03:57 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/ref_counted.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "net/base/backoff_entry.h" |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 17 | |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 18 | namespace network { |
| 19 | class SharedURLLoaderFactory; |
| 20 | class SimpleURLLoader; |
| 21 | } // namespace network |
| 22 | |
| 23 | namespace sync_pb { |
| 24 | class ExperimentStatusResponse; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | namespace gcm { |
| 28 | |
| 29 | // Defines the request to talk with the server to determine if the GCM support |
| 30 | // should be enabled. |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 31 | class GCMChannelStatusRequest { |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 32 | public: |
| 33 | // Callback completing the channel status request. |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 34 | // |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)> |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 42 | GCMChannelStatusRequestCallback; |
| 43 | |
| 44 | GCMChannelStatusRequest( |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 45 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 46 | const std::string& channel_status_request_url, |
| 47 | const std::string& user_agent, |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 48 | const GCMChannelStatusRequestCallback& callback); |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 49 | ~GCMChannelStatusRequest(); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 50 | |
| 51 | void Start(); |
| 52 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 53 | static int default_poll_interval_seconds(); |
| 54 | static int min_poll_interval_seconds(); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 55 | |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 56 | // Public so tests can use it. |
| 57 | void ParseResponseProto(sync_pb::ExperimentStatusResponse response_proto); |
| 58 | |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 59 | private: |
jianli | 115ee92 | 2014-10-17 21:03:57 | [diff] [blame] | 60 | FRIEND_TEST_ALL_PREFIXES(GCMChannelStatusRequestTest, RequestData); |
| 61 | |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 62 | void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body); |
| 63 | bool ParseResponse(std::unique_ptr<std::string> response_body); |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 64 | void RetryWithBackoff(bool update_backoff); |
| 65 | |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 66 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 67 | const std::string channel_status_request_url_; |
| 68 | const std::string user_agent_; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 69 | GCMChannelStatusRequestCallback callback_; |
Mark Pilgrim | 7634f5b5 | 2018-06-27 19:53:27 | [diff] [blame] | 70 | std::unique_ptr<network::SimpleURLLoader> simple_url_loader_; |
jianli | 1235e51c | 2014-09-08 18:56:41 | [diff] [blame] | 71 | 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_ |