Make |context| passed to queryRequiredElement optional
[chromium-blink-merge.git] / components / gcm_driver / gcm_channel_status_syncer.h
blobe9fd7e3f517192d1128ac48101e0fc5c77b09006
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.
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_
8 #include "base/compiler_specific.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
15 class PrefService;
16 class PrefRegistrySimple;
18 namespace net {
19 class URLRequestContextGetter;
22 namespace user_prefs {
23 class PrefRegistrySyncable;
26 namespace gcm {
28 class GCMChannelStatusRequest;
29 class GCMDriver;
31 namespace prefs {
32 // The GCM channel's enabled state.
33 extern const char kGCMChannelStatus[];
34 } // namepsace prefs
36 // Syncing with the server for GCM channel status that controls if GCM
37 // functionality should be enabled or disabled.
38 class GCMChannelStatusSyncer {
39 public:
40 static void RegisterPrefs(PrefRegistrySimple* registry);
41 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
43 GCMChannelStatusSyncer(
44 GCMDriver* driver,
45 PrefService* prefs,
46 const std::string& channel_status_request_url,
47 const std::string& user_agent,
48 const scoped_refptr<net::URLRequestContextGetter>& request_context);
49 ~GCMChannelStatusSyncer();
51 void EnsureStarted();
52 void Stop();
54 bool gcm_enabled() const { return gcm_enabled_; }
56 // For testing purpose.
57 void set_delay_removed_for_testing(bool delay_removed) {
58 delay_removed_for_testing_ = delay_removed;
60 base::TimeDelta current_request_delay_interval() const {
61 return current_request_delay_interval_;
63 static int first_time_delay_seconds();
65 private:
66 // Called when a request is completed.
67 void OnRequestCompleted(bool update_received,
68 bool enabled,
69 int poll_interval_seconds);
71 // Schedules next request to start after appropriate delay.
72 void ScheduleRequest();
74 // Creates and starts a request immediately.
75 void StartRequest();
77 // Computes and returns a delay with the fuzzing variation added if needed,
78 // after which the request could start.
79 base::TimeDelta GetRequestDelayInterval() const;
81 // GCMDriver owns GCMChannelStatusSyncer instance.
82 GCMDriver* driver_;
83 PrefService* prefs_;
84 const std::string channel_status_request_url_;
85 const std::string user_agent_;
87 scoped_refptr<net::URLRequestContextGetter> request_context_;
88 scoped_ptr<GCMChannelStatusRequest> request_;
90 bool started_;
91 bool gcm_enabled_;
92 int poll_interval_seconds_;
93 base::Time last_check_time_;
95 // If non-zero, |poll_interval_seconds_| is overriden by the command line
96 // options for testing purpose. Each time when the custom poll interval is
97 // used, this count is subtracted by one. When it reaches zero, the default
98 // poll interval will be used instead.
99 int custom_poll_interval_use_count_;
101 // The flag that indicates if the delay, including fuzzing variation and poll
102 // interval, is removed for testing purpose.
103 bool delay_removed_for_testing_;
105 // Tracked for testing purpose.
106 base::TimeDelta current_request_delay_interval_;
108 // Used to pass a weak pointer to a task.
109 base::WeakPtrFactory<GCMChannelStatusSyncer> weak_ptr_factory_;
111 DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncer);
114 } // namespace gcm
116 #endif // COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_