Get foreground tab on Android
[chromium-blink-merge.git] / sync / notifier / sync_system_resources.h
blobb14d352bd1f1c817ae41b6f9b8219342f6fc53f6
1 // Copyright 2012 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 // Simple system resources class that uses the current message loop
6 // for scheduling. Assumes the current message loop is already
7 // running.
9 #ifndef SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_
10 #define SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_
12 #include <set>
13 #include <string>
14 #include <vector>
16 #include "base/compiler_specific.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/threading/non_thread_safe.h"
21 #include "google/cacheinvalidation/include/system-resources.h"
22 #include "sync/base/sync_export.h"
23 #include "sync/notifier/push_client_channel.h"
24 #include "sync/notifier/state_writer.h"
26 namespace notifier {
27 class PushClient;
28 } // namespace notifier
30 namespace syncer {
32 class SyncLogger : public invalidation::Logger {
33 public:
34 SyncLogger();
36 virtual ~SyncLogger();
38 // invalidation::Logger implementation.
39 virtual void Log(LogLevel level, const char* file, int line,
40 const char* format, ...) OVERRIDE;
42 virtual void SetSystemResources(
43 invalidation::SystemResources* resources) OVERRIDE;
46 class SyncInvalidationScheduler : public invalidation::Scheduler {
47 public:
48 SyncInvalidationScheduler();
50 virtual ~SyncInvalidationScheduler();
52 // Start and stop the scheduler.
53 void Start();
54 void Stop();
56 // invalidation::Scheduler implementation.
57 virtual void Schedule(invalidation::TimeDelta delay,
58 invalidation::Closure* task) OVERRIDE;
60 virtual bool IsRunningOnThread() const OVERRIDE;
62 virtual invalidation::Time GetCurrentTime() const OVERRIDE;
64 virtual void SetSystemResources(
65 invalidation::SystemResources* resources) OVERRIDE;
67 private:
68 // Runs the task, deletes it, and removes it from |posted_tasks_|.
69 void RunPostedTask(invalidation::Closure* task);
71 // Holds all posted tasks that have not yet been run.
72 std::set<invalidation::Closure*> posted_tasks_;
74 const base::MessageLoop* created_on_loop_;
75 bool is_started_;
76 bool is_stopped_;
78 base::WeakPtrFactory<SyncInvalidationScheduler> weak_factory_;
81 class SyncStorage : public invalidation::Storage {
82 public:
83 SyncStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler);
85 virtual ~SyncStorage();
87 void SetInitialState(const std::string& value) {
88 cached_state_ = value;
91 // invalidation::Storage implementation.
92 virtual void WriteKey(const std::string& key, const std::string& value,
93 invalidation::WriteKeyCallback* done) OVERRIDE;
95 virtual void ReadKey(const std::string& key,
96 invalidation::ReadKeyCallback* done) OVERRIDE;
98 virtual void DeleteKey(const std::string& key,
99 invalidation::DeleteKeyCallback* done) OVERRIDE;
101 virtual void ReadAllKeys(
102 invalidation::ReadAllKeysCallback* key_callback) OVERRIDE;
104 virtual void SetSystemResources(
105 invalidation::SystemResources* resources) OVERRIDE;
107 private:
108 // Runs the given storage callback with SUCCESS status and deletes it.
109 void RunAndDeleteWriteKeyCallback(
110 invalidation::WriteKeyCallback* callback);
112 // Runs the given callback with the given value and deletes it.
113 void RunAndDeleteReadKeyCallback(
114 invalidation::ReadKeyCallback* callback, const std::string& value);
116 StateWriter* state_writer_;
117 invalidation::Scheduler* scheduler_;
118 std::string cached_state_;
121 class SYNC_EXPORT_PRIVATE SyncSystemResources
122 : public NON_EXPORTED_BASE(invalidation::SystemResources) {
123 public:
124 SyncSystemResources(scoped_ptr<notifier::PushClient> push_client,
125 StateWriter* state_writer);
127 virtual ~SyncSystemResources();
129 // invalidation::SystemResources implementation.
130 virtual void Start() OVERRIDE;
131 virtual void Stop() OVERRIDE;
132 virtual bool IsStarted() const OVERRIDE;
133 virtual void set_platform(const std::string& platform);
134 virtual std::string platform() const OVERRIDE;
135 virtual SyncLogger* logger() OVERRIDE;
136 virtual SyncStorage* storage() OVERRIDE;
137 virtual PushClientChannel* network() OVERRIDE;
138 virtual SyncInvalidationScheduler* internal_scheduler() OVERRIDE;
139 virtual SyncInvalidationScheduler* listener_scheduler() OVERRIDE;
141 private:
142 bool is_started_;
143 std::string platform_;
144 scoped_ptr<SyncLogger> logger_;
145 scoped_ptr<SyncInvalidationScheduler> internal_scheduler_;
146 scoped_ptr<SyncInvalidationScheduler> listener_scheduler_;
147 scoped_ptr<SyncStorage> storage_;
148 PushClientChannel push_client_channel_;
151 } // namespace syncer
153 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_