OLD | NEW |
| (Empty) |
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. | |
8 | |
9 #ifndef SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_ | |
10 #define SYNC_NOTIFIER_SYNC_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/message_loop.h" | |
20 #include "base/threading/non_thread_safe.h" | |
21 #include "base/values.h" | |
22 #include "google/cacheinvalidation/include/system-resources.h" | |
23 #include "jingle/notifier/base/notifier_options.h" | |
24 #include "sync/base/sync_export.h" | |
25 #include "sync/notifier/invalidator_state.h" | |
26 #include "sync/notifier/state_writer.h" | |
27 | |
28 namespace syncer { | |
29 | |
30 class GCMNetworkChannelDelegate; | |
31 | |
32 class SyncLogger : public invalidation::Logger { | |
33 public: | |
34 SyncLogger(); | |
35 | |
36 virtual ~SyncLogger(); | |
37 | |
38 // invalidation::Logger implementation. | |
39 virtual void Log(LogLevel level, const char* file, int line, | |
40 const char* format, ...) OVERRIDE; | |
41 | |
42 virtual void SetSystemResources( | |
43 invalidation::SystemResources* resources) OVERRIDE; | |
44 }; | |
45 | |
46 class SyncInvalidationScheduler : public invalidation::Scheduler { | |
47 public: | |
48 SyncInvalidationScheduler(); | |
49 | |
50 virtual ~SyncInvalidationScheduler(); | |
51 | |
52 // Start and stop the scheduler. | |
53 void Start(); | |
54 void Stop(); | |
55 | |
56 // invalidation::Scheduler implementation. | |
57 virtual void Schedule(invalidation::TimeDelta delay, | |
58 invalidation::Closure* task) OVERRIDE; | |
59 | |
60 virtual bool IsRunningOnThread() const OVERRIDE; | |
61 | |
62 virtual invalidation::Time GetCurrentTime() const OVERRIDE; | |
63 | |
64 virtual void SetSystemResources( | |
65 invalidation::SystemResources* resources) OVERRIDE; | |
66 | |
67 private: | |
68 // Runs the task, deletes it, and removes it from |posted_tasks_|. | |
69 void RunPostedTask(invalidation::Closure* task); | |
70 | |
71 // Holds all posted tasks that have not yet been run. | |
72 std::set<invalidation::Closure*> posted_tasks_; | |
73 | |
74 const base::MessageLoop* created_on_loop_; | |
75 bool is_started_; | |
76 bool is_stopped_; | |
77 | |
78 base::WeakPtrFactory<SyncInvalidationScheduler> weak_factory_; | |
79 }; | |
80 | |
81 // SyncNetworkChannel implements common tasks needed to interact with | |
82 // invalidation library: | |
83 // - registering message and network status callbacks | |
84 // - notifying observers about network channel state change | |
85 // Implementation of particular network protocol should implement | |
86 // SendMessage and call NotifyStateChange and DeliverIncomingMessage. | |
87 class SYNC_EXPORT_PRIVATE SyncNetworkChannel | |
88 : public NON_EXPORTED_BASE(invalidation::NetworkChannel) { | |
89 public: | |
90 class Observer { | |
91 public: | |
92 // Called when network channel state changes. Possible states are: | |
93 // - INVALIDATIONS_ENABLED : connection is established and working | |
94 // - TRANSIENT_INVALIDATION_ERROR : no network, connection lost, etc. | |
95 // - INVALIDATION_CREDENTIALS_REJECTED : Issues with auth token | |
96 virtual void OnNetworkChannelStateChanged( | |
97 InvalidatorState invalidator_state) = 0; | |
98 }; | |
99 | |
100 SyncNetworkChannel(); | |
101 | |
102 virtual ~SyncNetworkChannel(); | |
103 | |
104 // invalidation::NetworkChannel implementation. | |
105 // SyncNetworkChannel doesn't implement SendMessage. It is responsibility of | |
106 // subclass to implement it. | |
107 virtual void SetMessageReceiver( | |
108 invalidation::MessageCallback* incoming_receiver) OVERRIDE; | |
109 virtual void AddNetworkStatusReceiver( | |
110 invalidation::NetworkStatusCallback* network_status_receiver) OVERRIDE; | |
111 virtual void SetSystemResources( | |
112 invalidation::SystemResources* resources) OVERRIDE; | |
113 | |
114 // Subclass should implement UpdateCredentials to pass new token to channel | |
115 // library. | |
116 virtual void UpdateCredentials(const std::string& email, | |
117 const std::string& token) = 0; | |
118 | |
119 // Return value from GetInvalidationClientType will be passed to | |
120 // invalidation::CreateInvalidationClient. Subclass should return one of the | |
121 // values from ipc::invalidation::ClientType enum from types.proto. | |
122 virtual int GetInvalidationClientType() = 0; | |
123 | |
124 // Subclass should implement RequestDetailedStatus to provide debugging | |
125 // information. | |
126 virtual void RequestDetailedStatus( | |
127 base::Callback<void(const base::DictionaryValue&)> callback) = 0; | |
128 | |
129 // Classes interested in network channel state changes should implement | |
130 // SyncNetworkChannel::Observer and register here. | |
131 void AddObserver(Observer* observer); | |
132 void RemoveObserver(Observer* observer); | |
133 | |
134 // Helper functions that know how to construct network channels from channel | |
135 // specific parameters. | |
136 static scoped_ptr<SyncNetworkChannel> CreatePushClientChannel( | |
137 const notifier::NotifierOptions& notifier_options); | |
138 static scoped_ptr<SyncNetworkChannel> CreateGCMNetworkChannel( | |
139 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
140 scoped_ptr<GCMNetworkChannelDelegate> delegate); | |
141 | |
142 // Get the count of how many valid received messages were received. | |
143 int GetReceivedMessagesCount() const; | |
144 | |
145 protected: | |
146 // Subclass should notify about connection state through NotifyStateChange. | |
147 void NotifyStateChange(InvalidatorState invalidator_state); | |
148 // Subclass should call DeliverIncomingMessage for message to reach | |
149 // invalidations library. | |
150 bool DeliverIncomingMessage(const std::string& message); | |
151 | |
152 private: | |
153 typedef std::vector<invalidation::NetworkStatusCallback*> | |
154 NetworkStatusReceiverList; | |
155 | |
156 // Callbacks into invalidation library | |
157 scoped_ptr<invalidation::MessageCallback> incoming_receiver_; | |
158 NetworkStatusReceiverList network_status_receivers_; | |
159 | |
160 // Last channel state for new network status receivers. | |
161 InvalidatorState invalidator_state_; | |
162 | |
163 int received_messages_count_; | |
164 | |
165 ObserverList<Observer> observers_; | |
166 }; | |
167 | |
168 class SyncStorage : public invalidation::Storage { | |
169 public: | |
170 SyncStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler); | |
171 | |
172 virtual ~SyncStorage(); | |
173 | |
174 void SetInitialState(const std::string& value) { | |
175 cached_state_ = value; | |
176 } | |
177 | |
178 // invalidation::Storage implementation. | |
179 virtual void WriteKey(const std::string& key, const std::string& value, | |
180 invalidation::WriteKeyCallback* done) OVERRIDE; | |
181 | |
182 virtual void ReadKey(const std::string& key, | |
183 invalidation::ReadKeyCallback* done) OVERRIDE; | |
184 | |
185 virtual void DeleteKey(const std::string& key, | |
186 invalidation::DeleteKeyCallback* done) OVERRIDE; | |
187 | |
188 virtual void ReadAllKeys( | |
189 invalidation::ReadAllKeysCallback* key_callback) OVERRIDE; | |
190 | |
191 virtual void SetSystemResources( | |
192 invalidation::SystemResources* resources) OVERRIDE; | |
193 | |
194 private: | |
195 // Runs the given storage callback with SUCCESS status and deletes it. | |
196 void RunAndDeleteWriteKeyCallback( | |
197 invalidation::WriteKeyCallback* callback); | |
198 | |
199 // Runs the given callback with the given value and deletes it. | |
200 void RunAndDeleteReadKeyCallback( | |
201 invalidation::ReadKeyCallback* callback, const std::string& value); | |
202 | |
203 StateWriter* state_writer_; | |
204 invalidation::Scheduler* scheduler_; | |
205 std::string cached_state_; | |
206 }; | |
207 | |
208 class SYNC_EXPORT_PRIVATE SyncSystemResources | |
209 : public NON_EXPORTED_BASE(invalidation::SystemResources) { | |
210 public: | |
211 SyncSystemResources(SyncNetworkChannel* sync_network_channel, | |
212 StateWriter* state_writer); | |
213 | |
214 virtual ~SyncSystemResources(); | |
215 | |
216 // invalidation::SystemResources implementation. | |
217 virtual void Start() OVERRIDE; | |
218 virtual void Stop() OVERRIDE; | |
219 virtual bool IsStarted() const OVERRIDE; | |
220 virtual void set_platform(const std::string& platform); | |
221 virtual std::string platform() const OVERRIDE; | |
222 virtual SyncLogger* logger() OVERRIDE; | |
223 virtual SyncStorage* storage() OVERRIDE; | |
224 virtual SyncNetworkChannel* network() OVERRIDE; | |
225 virtual SyncInvalidationScheduler* internal_scheduler() OVERRIDE; | |
226 virtual SyncInvalidationScheduler* listener_scheduler() OVERRIDE; | |
227 | |
228 private: | |
229 bool is_started_; | |
230 std::string platform_; | |
231 scoped_ptr<SyncLogger> logger_; | |
232 scoped_ptr<SyncInvalidationScheduler> internal_scheduler_; | |
233 scoped_ptr<SyncInvalidationScheduler> listener_scheduler_; | |
234 scoped_ptr<SyncStorage> storage_; | |
235 // sync_network_channel_ is owned by SyncInvalidationListener. | |
236 SyncNetworkChannel* sync_network_channel_; | |
237 }; | |
238 | |
239 } // namespace syncer | |
240 | |
241 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_ | |
OLD | NEW |