Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / gcm_driver / gcm_channel_status_syncer.h
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_SYNCER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_
7
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"
14
15 class PrefService;
16 class PrefRegistrySimple;
17
18 namespace net {
19 class URLRequestContextGetter;
20 }
21
22 namespace user_prefs {
23 class PrefRegistrySyncable;
24 }
25
26 namespace gcm {
27
28 class GCMChannelStatusRequest;
29 class GCMDriver;
30
31 // Syncing with the server for GCM channel status that controls if GCM
32 // functionality should be enabled or disabled.
33 class GCMChannelStatusSyncer {
34  public:
35   static void RegisterPrefs(PrefRegistrySimple* registry);
36   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
37
38   GCMChannelStatusSyncer(
39       GCMDriver* driver,
40       PrefService* prefs,
41       const scoped_refptr<net::URLRequestContextGetter>& request_context);
42   ~GCMChannelStatusSyncer();
43
44   void EnsureStarted();
45   void Stop();
46
47   bool gcm_enabled() const { return gcm_enabled_; }
48
49   // For testing purpose.
50   void set_delay_removed_for_testing(bool delay_removed) {
51     delay_removed_for_testing_ = delay_removed;
52   }
53   base::TimeDelta current_request_delay_interval() const {
54     return current_request_delay_interval_;
55   }
56   static int first_time_delay_seconds();
57
58  private:
59   // Called when a request is completed.
60   void OnRequestCompleted(bool enabled, int poll_interval_seconds);
61
62   // Schedules next request to start after appropriate delay.
63   void ScheduleRequest();
64
65   // Creates and starts a request immediately.
66   void StartRequest();
67
68   // Computes and returns a delay with the fuzzing variation added if needed,
69   // after which the request could start.
70   base::TimeDelta GetRequestDelayInterval() const;
71
72   // GCMDriver owns GCMChannelStatusSyncer instance.
73   GCMDriver* driver_;
74   PrefService* prefs_;
75
76   scoped_refptr<net::URLRequestContextGetter> request_context_;
77   scoped_ptr<GCMChannelStatusRequest> request_;
78
79   bool gcm_enabled_;
80   int poll_interval_seconds_;
81   base::Time last_check_time_;
82
83   // The flag that indicates if the delay, including fuzzing variation and poll
84   // interval, is removed for testing purpose.
85   bool delay_removed_for_testing_;
86
87   // Tracked for testing purpose.
88   base::TimeDelta current_request_delay_interval_;
89
90   // Used to pass a weak pointer to a task.
91   base::WeakPtrFactory<GCMChannelStatusSyncer> weak_ptr_factory_;
92
93   DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncer);
94 };
95
96 }  // namespace gcm
97
98 #endif  // COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_