summaryrefslogtreecommitdiffstats
path: root/components/gcm_driver/gcm_channel_status_syncer.h
diff options
context:
space:
mode:
authorjianli <jianli@chromium.org>2014-09-18 12:33:59 -0700
committerCommit bot <commit-bot@chromium.org>2014-09-18 19:34:17 +0000
commit3c23f4a188e171998f3042ad62f4aa5717e66d63 (patch)
tree804ae61bd1098e97dcdd55f5c2ae5a8712e8c547 /components/gcm_driver/gcm_channel_status_syncer.h
parent9065985da2ce2a4f73150ac5eabab29c5d67505d (diff)
downloadchromium_src-3c23f4a188e171998f3042ad62f4aa5717e66d63.zip
chromium_src-3c23f4a188e171998f3042ad62f4aa5717e66d63.tar.gz
chromium_src-3c23f4a188e171998f3042ad62f4aa5717e66d63.tar.bz2
Add GCMChannelStatusSyncer to schedule requests and enable/disable GCM
BUG=384041 TEST=new tests added Review URL: https://codereview.chromium.org/561943002 Cr-Commit-Position: refs/heads/master@{#295524}
Diffstat (limited to 'components/gcm_driver/gcm_channel_status_syncer.h')
-rw-r--r--components/gcm_driver/gcm_channel_status_syncer.h98
1 files changed, 98 insertions, 0 deletions
diff --git a/components/gcm_driver/gcm_channel_status_syncer.h b/components/gcm_driver/gcm_channel_status_syncer.h
new file mode 100644
index 0000000..83cfbda3
--- /dev/null
+++ b/components/gcm_driver/gcm_channel_status_syncer.h
@@ -0,0 +1,98 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_
+#define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_
+
+#include "base/compiler_specific.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/time/time.h"
+
+class PrefService;
+class PrefRegistrySimple;
+
+namespace net {
+class URLRequestContextGetter;
+}
+
+namespace user_prefs {
+class PrefRegistrySyncable;
+}
+
+namespace gcm {
+
+class GCMChannelStatusRequest;
+class GCMDriver;
+
+// Syncing with the server for GCM channel status that controls if GCM
+// functionality should be enabled or disabled.
+class GCMChannelStatusSyncer {
+ public:
+ static void RegisterPrefs(PrefRegistrySimple* registry);
+ static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
+
+ GCMChannelStatusSyncer(
+ GCMDriver* driver,
+ PrefService* prefs,
+ const scoped_refptr<net::URLRequestContextGetter>& request_context);
+ ~GCMChannelStatusSyncer();
+
+ void EnsureStarted();
+ void Stop();
+
+ bool gcm_enabled() const { return gcm_enabled_; }
+
+ // For testing purpose.
+ void set_delay_removed_for_testing(bool delay_removed) {
+ delay_removed_for_testing_ = delay_removed;
+ }
+ base::TimeDelta current_request_delay_interval() const {
+ return current_request_delay_interval_;
+ }
+ static int first_time_delay_seconds();
+
+ private:
+ // Called when a request is completed.
+ void OnRequestCompleted(bool enabled, int poll_interval_seconds);
+
+ // Schedules next request to start after appropriate delay.
+ void ScheduleRequest();
+
+ // Creates and starts a request immediately.
+ void StartRequest();
+
+ // Computes and returns a delay with the fuzzing variation added if needed,
+ // after which the request could start.
+ base::TimeDelta GetRequestDelayInterval() const;
+
+ // GCMDriver owns GCMChannelStatusSyncer instance.
+ GCMDriver* driver_;
+ PrefService* prefs_;
+
+ scoped_refptr<net::URLRequestContextGetter> request_context_;
+ scoped_ptr<GCMChannelStatusRequest> request_;
+
+ bool gcm_enabled_;
+ int poll_interval_seconds_;
+ base::Time last_check_time_;
+
+ // The flag that indicates if the delay, including fuzzing variation and poll
+ // interval, is removed for testing purpose.
+ bool delay_removed_for_testing_;
+
+ // Tracked for testing purpose.
+ base::TimeDelta current_request_delay_interval_;
+
+ // Used to pass a weak pointer to a task.
+ base::WeakPtrFactory<GCMChannelStatusSyncer> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusSyncer);
+};
+
+} // namespace gcm
+
+#endif // COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_SYNCER_H_