OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
8 | |
9 #ifndef SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_ | |
10 #define SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_ | |
11 | |
12 #include <set> | |
13 #include <string> | |
14 #include <vector> | |
15 | |
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.h" | |
20 #include "base/threading/non_thread_safe.h" | |
21 #include "google/cacheinvalidation/include/system-resources.h" | |
22 #include "sync/notifier/push_client_channel.h" | |
23 #include "sync/notifier/state_writer.h" | |
24 | |
25 namespace notifier { | |
26 class PushClient; | |
27 } // namespace notifier | |
28 | |
29 namespace syncer { | |
30 | |
31 class ChromeLogger : public invalidation::Logger { | |
32 public: | |
33 ChromeLogger(); | |
34 | |
35 virtual ~ChromeLogger(); | |
36 | |
37 // invalidation::Logger implementation. | |
38 virtual void Log(LogLevel level, const char* file, int line, | |
39 const char* format, ...) OVERRIDE; | |
40 | |
41 virtual void SetSystemResources( | |
42 invalidation::SystemResources* resources) OVERRIDE; | |
43 }; | |
44 | |
45 class ChromeScheduler : public invalidation::Scheduler { | |
46 public: | |
47 ChromeScheduler(); | |
48 | |
49 virtual ~ChromeScheduler(); | |
50 | |
51 // Start and stop the scheduler. | |
52 void Start(); | |
53 void Stop(); | |
54 | |
55 // invalidation::Scheduler implementation. | |
56 virtual void Schedule(invalidation::TimeDelta delay, | |
57 invalidation::Closure* task) OVERRIDE; | |
58 | |
59 virtual bool IsRunningOnThread() const OVERRIDE; | |
60 | |
61 virtual invalidation::Time GetCurrentTime() const OVERRIDE; | |
62 | |
63 virtual void SetSystemResources( | |
64 invalidation::SystemResources* resources) OVERRIDE; | |
65 | |
66 private: | |
67 base::WeakPtrFactory<ChromeScheduler> weak_factory_; | |
68 // Holds all posted tasks that have not yet been run. | |
69 std::set<invalidation::Closure*> posted_tasks_; | |
70 | |
71 const MessageLoop* created_on_loop_; | |
72 bool is_started_; | |
73 bool is_stopped_; | |
74 | |
75 // Runs the task, deletes it, and removes it from |posted_tasks_|. | |
76 void RunPostedTask(invalidation::Closure* task); | |
77 }; | |
78 | |
79 class ChromeStorage : public invalidation::Storage { | |
80 public: | |
81 ChromeStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler); | |
82 | |
83 virtual ~ChromeStorage(); | |
84 | |
85 void SetInitialState(const std::string& value) { | |
86 cached_state_ = value; | |
87 } | |
88 | |
89 // invalidation::Storage implementation. | |
90 virtual void WriteKey(const std::string& key, const std::string& value, | |
91 invalidation::WriteKeyCallback* done) OVERRIDE; | |
92 | |
93 virtual void ReadKey(const std::string& key, | |
94 invalidation::ReadKeyCallback* done) OVERRIDE; | |
95 | |
96 virtual void DeleteKey(const std::string& key, | |
97 invalidation::DeleteKeyCallback* done) OVERRIDE; | |
98 | |
99 virtual void ReadAllKeys( | |
100 invalidation::ReadAllKeysCallback* key_callback) OVERRIDE; | |
101 | |
102 virtual void SetSystemResources( | |
103 invalidation::SystemResources* resources) OVERRIDE; | |
104 | |
105 private: | |
106 // Runs the given storage callback with SUCCESS status and deletes it. | |
107 void RunAndDeleteWriteKeyCallback( | |
108 invalidation::WriteKeyCallback* callback); | |
109 | |
110 // Runs the given callback with the given value and deletes it. | |
111 void RunAndDeleteReadKeyCallback( | |
112 invalidation::ReadKeyCallback* callback, const std::string& value); | |
113 | |
114 StateWriter* state_writer_; | |
115 invalidation::Scheduler* scheduler_; | |
116 std::string cached_state_; | |
117 }; | |
118 | |
119 class ChromeSystemResources : public invalidation::SystemResources { | |
120 public: | |
121 ChromeSystemResources(scoped_ptr<notifier::PushClient> push_client, | |
122 StateWriter* state_writer); | |
123 | |
124 virtual ~ChromeSystemResources(); | |
125 | |
126 // invalidation::SystemResources implementation. | |
127 virtual void Start() OVERRIDE; | |
128 virtual void Stop() OVERRIDE; | |
129 virtual bool IsStarted() const OVERRIDE; | |
130 virtual void set_platform(const std::string& platform); | |
131 virtual std::string platform() const OVERRIDE; | |
132 virtual ChromeLogger* logger() OVERRIDE; | |
133 virtual ChromeStorage* storage() OVERRIDE; | |
134 virtual PushClientChannel* network() OVERRIDE; | |
135 virtual ChromeScheduler* internal_scheduler() OVERRIDE; | |
136 virtual ChromeScheduler* listener_scheduler() OVERRIDE; | |
137 | |
138 private: | |
139 bool is_started_; | |
140 std::string platform_; | |
141 scoped_ptr<ChromeLogger> logger_; | |
142 scoped_ptr<ChromeScheduler> internal_scheduler_; | |
143 scoped_ptr<ChromeScheduler> listener_scheduler_; | |
144 scoped_ptr<ChromeStorage> storage_; | |
145 PushClientChannel push_client_channel_; | |
146 }; | |
147 | |
148 } // namespace syncer | |
149 | |
150 #endif // SYNC_NOTIFIER_CHROME_SYSTEM_RESOURCES_H_ | |
OLD | NEW |