OLD | NEW |
| (Empty) |
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 // 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 COMPONENTS_INVALIDATION_SYNC_SYSTEM_RESOURCES_H_ | |
10 #define COMPONENTS_INVALIDATION_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 "components/invalidation/invalidation_export.h" | |
23 #include "components/invalidation/invalidator_state.h" | |
24 #include "components/invalidation/state_writer.h" | |
25 #include "google/cacheinvalidation/include/system-resources.h" | |
26 #include "jingle/notifier/base/notifier_options.h" | |
27 | |
28 namespace syncer { | |
29 | |
30 class GCMNetworkChannelDelegate; | |
31 | |
32 class SyncLogger : public invalidation::Logger { | |
33 public: | |
34 SyncLogger(); | |
35 | |
36 ~SyncLogger() override; | |
37 | |
38 // invalidation::Logger implementation. | |
39 void Log(LogLevel level, | |
40 const char* file, | |
41 int line, | |
42 const char* format, | |
43 ...) override; | |
44 | |
45 void SetSystemResources(invalidation::SystemResources* resources) override; | |
46 }; | |
47 | |
48 class SyncInvalidationScheduler : public invalidation::Scheduler { | |
49 public: | |
50 SyncInvalidationScheduler(); | |
51 | |
52 ~SyncInvalidationScheduler() override; | |
53 | |
54 // Start and stop the scheduler. | |
55 void Start(); | |
56 void Stop(); | |
57 | |
58 // invalidation::Scheduler implementation. | |
59 void Schedule(invalidation::TimeDelta delay, | |
60 invalidation::Closure* task) override; | |
61 | |
62 bool IsRunningOnThread() const override; | |
63 | |
64 invalidation::Time GetCurrentTime() const override; | |
65 | |
66 void SetSystemResources(invalidation::SystemResources* resources) override; | |
67 | |
68 private: | |
69 // Runs the task, deletes it, and removes it from |posted_tasks_|. | |
70 void RunPostedTask(invalidation::Closure* task); | |
71 | |
72 // Holds all posted tasks that have not yet been run. | |
73 std::set<invalidation::Closure*> posted_tasks_; | |
74 | |
75 const base::MessageLoop* created_on_loop_; | |
76 bool is_started_; | |
77 bool is_stopped_; | |
78 | |
79 base::WeakPtrFactory<SyncInvalidationScheduler> weak_factory_; | |
80 }; | |
81 | |
82 // SyncNetworkChannel implements common tasks needed to interact with | |
83 // invalidation library: | |
84 // - registering message and network status callbacks | |
85 // - notifying observers about network channel state change | |
86 // Implementation of particular network protocol should implement | |
87 // SendMessage and call NotifyStateChange and DeliverIncomingMessage. | |
88 class INVALIDATION_EXPORT_PRIVATE SyncNetworkChannel | |
89 : public NON_EXPORTED_BASE(invalidation::NetworkChannel) { | |
90 public: | |
91 class Observer { | |
92 public: | |
93 // Called when network channel state changes. Possible states are: | |
94 // - INVALIDATIONS_ENABLED : connection is established and working | |
95 // - TRANSIENT_INVALIDATION_ERROR : no network, connection lost, etc. | |
96 // - INVALIDATION_CREDENTIALS_REJECTED : Issues with auth token | |
97 virtual void OnNetworkChannelStateChanged( | |
98 InvalidatorState invalidator_state) = 0; | |
99 }; | |
100 | |
101 SyncNetworkChannel(); | |
102 | |
103 ~SyncNetworkChannel() override; | |
104 | |
105 // invalidation::NetworkChannel implementation. | |
106 // SyncNetworkChannel doesn't implement SendMessage. It is responsibility of | |
107 // subclass to implement it. | |
108 void SetMessageReceiver( | |
109 invalidation::MessageCallback* incoming_receiver) override; | |
110 void AddNetworkStatusReceiver( | |
111 invalidation::NetworkStatusCallback* network_status_receiver) override; | |
112 void SetSystemResources(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 call NotifyNetworkStatusChange to notify about network | |
147 // changes. This triggers cacheinvalidation to try resending failed message | |
148 // ahead of schedule when client comes online or IP address changes. | |
149 void NotifyNetworkStatusChange(bool online); | |
150 | |
151 // Subclass should notify about connection state through | |
152 // NotifyChannelStateChange. If communication doesn't work and it is possible | |
153 // that invalidations from server will not reach this client then channel | |
154 // should call this function with TRANSIENT_INVALIDATION_ERROR. | |
155 void NotifyChannelStateChange(InvalidatorState invalidator_state); | |
156 | |
157 // Subclass should call DeliverIncomingMessage for message to reach | |
158 // invalidations library. | |
159 bool DeliverIncomingMessage(const std::string& message); | |
160 | |
161 private: | |
162 typedef std::vector<invalidation::NetworkStatusCallback*> | |
163 NetworkStatusReceiverList; | |
164 | |
165 // Callbacks into invalidation library | |
166 scoped_ptr<invalidation::MessageCallback> incoming_receiver_; | |
167 NetworkStatusReceiverList network_status_receivers_; | |
168 | |
169 // Last network status for new network status receivers. | |
170 bool last_network_status_; | |
171 | |
172 int received_messages_count_; | |
173 | |
174 base::ObserverList<Observer> observers_; | |
175 }; | |
176 | |
177 class SyncStorage : public invalidation::Storage { | |
178 public: | |
179 SyncStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler); | |
180 | |
181 ~SyncStorage() override; | |
182 | |
183 void SetInitialState(const std::string& value) { | |
184 cached_state_ = value; | |
185 } | |
186 | |
187 // invalidation::Storage implementation. | |
188 void WriteKey(const std::string& key, | |
189 const std::string& value, | |
190 invalidation::WriteKeyCallback* done) override; | |
191 | |
192 void ReadKey(const std::string& key, | |
193 invalidation::ReadKeyCallback* done) override; | |
194 | |
195 void DeleteKey(const std::string& key, | |
196 invalidation::DeleteKeyCallback* done) override; | |
197 | |
198 void ReadAllKeys(invalidation::ReadAllKeysCallback* key_callback) override; | |
199 | |
200 void SetSystemResources(invalidation::SystemResources* resources) override; | |
201 | |
202 private: | |
203 // Runs the given storage callback with SUCCESS status and deletes it. | |
204 void RunAndDeleteWriteKeyCallback( | |
205 invalidation::WriteKeyCallback* callback); | |
206 | |
207 // Runs the given callback with the given value and deletes it. | |
208 void RunAndDeleteReadKeyCallback( | |
209 invalidation::ReadKeyCallback* callback, const std::string& value); | |
210 | |
211 StateWriter* state_writer_; | |
212 invalidation::Scheduler* scheduler_; | |
213 std::string cached_state_; | |
214 }; | |
215 | |
216 class INVALIDATION_EXPORT_PRIVATE SyncSystemResources | |
217 : public NON_EXPORTED_BASE(invalidation::SystemResources) { | |
218 public: | |
219 SyncSystemResources(SyncNetworkChannel* sync_network_channel, | |
220 StateWriter* state_writer); | |
221 | |
222 ~SyncSystemResources() override; | |
223 | |
224 // invalidation::SystemResources implementation. | |
225 void Start() override; | |
226 void Stop() override; | |
227 bool IsStarted() const override; | |
228 virtual void set_platform(const std::string& platform); | |
229 std::string platform() const override; | |
230 SyncLogger* logger() override; | |
231 SyncStorage* storage() override; | |
232 SyncNetworkChannel* network() override; | |
233 SyncInvalidationScheduler* internal_scheduler() override; | |
234 SyncInvalidationScheduler* listener_scheduler() override; | |
235 | |
236 private: | |
237 bool is_started_; | |
238 std::string platform_; | |
239 scoped_ptr<SyncLogger> logger_; | |
240 scoped_ptr<SyncInvalidationScheduler> internal_scheduler_; | |
241 scoped_ptr<SyncInvalidationScheduler> listener_scheduler_; | |
242 scoped_ptr<SyncStorage> storage_; | |
243 // sync_network_channel_ is owned by SyncInvalidationListener. | |
244 SyncNetworkChannel* sync_network_channel_; | |
245 }; | |
246 | |
247 } // namespace syncer | |
248 | |
249 #endif // COMPONENTS_INVALIDATION_SYNC_SYSTEM_RESOURCES_H_ | |
OLD | NEW |