rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / sync / notifier / sync_system_resources.h
blob14711dd9b238bd56dc7350d2bb2a9b45c532cc4b
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 "jingle/notifier/base/notifier_options.h"
23 #include "sync/base/sync_export.h"
24 #include "sync/notifier/invalidator_state.h"
25 #include "sync/notifier/state_writer.h"
27 namespace syncer {
29 class GCMNetworkChannelDelegate;
31 class SyncLogger : public invalidation::Logger {
32 public:
33 SyncLogger();
35 virtual ~SyncLogger();
37 // invalidation::Logger implementation.
38 virtual void Log(LogLevel level, const char* file, int line,
39 const char* format, ...) OVERRIDE;
41 virtual void SetSystemResources(
42 invalidation::SystemResources* resources) OVERRIDE;
45 class SyncInvalidationScheduler : public invalidation::Scheduler {
46 public:
47 SyncInvalidationScheduler();
49 virtual ~SyncInvalidationScheduler();
51 // Start and stop the scheduler.
52 void Start();
53 void Stop();
55 // invalidation::Scheduler implementation.
56 virtual void Schedule(invalidation::TimeDelta delay,
57 invalidation::Closure* task) OVERRIDE;
59 virtual bool IsRunningOnThread() const OVERRIDE;
61 virtual invalidation::Time GetCurrentTime() const OVERRIDE;
63 virtual void SetSystemResources(
64 invalidation::SystemResources* resources) OVERRIDE;
66 private:
67 // Runs the task, deletes it, and removes it from |posted_tasks_|.
68 void RunPostedTask(invalidation::Closure* task);
70 // Holds all posted tasks that have not yet been run.
71 std::set<invalidation::Closure*> posted_tasks_;
73 const base::MessageLoop* created_on_loop_;
74 bool is_started_;
75 bool is_stopped_;
77 base::WeakPtrFactory<SyncInvalidationScheduler> weak_factory_;
80 // SyncNetworkChannel implements common tasks needed to interact with
81 // invalidation library:
82 // - registering message and network status callbacks
83 // - Encoding/Decoding message to ClientGatewayMessage
84 // - notifying observers about network channel state change
85 // Implementation of particular network protocol should implement
86 // SendEncodedMessage 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;
100 SyncNetworkChannel();
102 virtual ~SyncNetworkChannel();
104 // invalidation::NetworkChannel implementation.
105 virtual void SendMessage(const std::string& outgoing_message) OVERRIDE;
106 virtual void SetMessageReceiver(
107 invalidation::MessageCallback* incoming_receiver) OVERRIDE;
108 virtual void AddNetworkStatusReceiver(
109 invalidation::NetworkStatusCallback* network_status_receiver) OVERRIDE;
110 virtual void SetSystemResources(
111 invalidation::SystemResources* resources) OVERRIDE;
113 // Subclass should implement SendEncodedMessage to send encoded message to
114 // Tango over network.
115 virtual void SendEncodedMessage(const std::string& encoded_message) = 0;
116 virtual void UpdateCredentials(const std::string& email,
117 const std::string& token) = 0;
119 // Classes interested in network channel state changes should implement
120 // SyncNetworkChannel::Observer and register here.
121 void AddObserver(Observer* observer);
122 void RemoveObserver(Observer* observer);
124 // Helper functions that know how to construct network channels from channel
125 // specific parameters.
126 static scoped_ptr<SyncNetworkChannel> CreatePushClientChannel(
127 const notifier::NotifierOptions& notifier_options);
128 static scoped_ptr<SyncNetworkChannel> CreateGCMNetworkChannel(
129 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
130 scoped_ptr<GCMNetworkChannelDelegate> delegate);
132 const std::string& GetServiceContextForTest() const;
134 int64 GetSchedulingHashForTest() const;
136 static std::string EncodeMessageForTest(
137 const std::string& message,
138 const std::string& service_context,
139 int64 scheduling_hash);
141 static bool DecodeMessageForTest(
142 const std::string& notification,
143 std::string* message,
144 std::string* service_context,
145 int64* scheduling_hash);
147 protected:
148 // Subclass should notify about connection state through NotifyStateChange.
149 void NotifyStateChange(InvalidatorState invalidator_state);
150 // Subclass should call DeliverIncomingMessage for message to reach
151 // invalidations library.
152 void DeliverIncomingMessage(const std::string& message);
154 private:
155 typedef std::vector<invalidation::NetworkStatusCallback*>
156 NetworkStatusReceiverList;
158 static void EncodeMessage(
159 std::string* encoded_message,
160 const std::string& message,
161 const std::string& service_context,
162 int64 scheduling_hash);
164 static bool DecodeMessage(
165 const std::string& data,
166 std::string* message,
167 std::string* service_context,
168 int64* scheduling_hash);
170 // Callbacks into invalidation library
171 scoped_ptr<invalidation::MessageCallback> incoming_receiver_;
172 NetworkStatusReceiverList network_status_receivers_;
174 // Last channel state for new network status receivers.
175 InvalidatorState invalidator_state_;
177 ObserverList<Observer> observers_;
179 std::string service_context_;
180 int64 scheduling_hash_;
183 class SyncStorage : public invalidation::Storage {
184 public:
185 SyncStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler);
187 virtual ~SyncStorage();
189 void SetInitialState(const std::string& value) {
190 cached_state_ = value;
193 // invalidation::Storage implementation.
194 virtual void WriteKey(const std::string& key, const std::string& value,
195 invalidation::WriteKeyCallback* done) OVERRIDE;
197 virtual void ReadKey(const std::string& key,
198 invalidation::ReadKeyCallback* done) OVERRIDE;
200 virtual void DeleteKey(const std::string& key,
201 invalidation::DeleteKeyCallback* done) OVERRIDE;
203 virtual void ReadAllKeys(
204 invalidation::ReadAllKeysCallback* key_callback) OVERRIDE;
206 virtual void SetSystemResources(
207 invalidation::SystemResources* resources) OVERRIDE;
209 private:
210 // Runs the given storage callback with SUCCESS status and deletes it.
211 void RunAndDeleteWriteKeyCallback(
212 invalidation::WriteKeyCallback* callback);
214 // Runs the given callback with the given value and deletes it.
215 void RunAndDeleteReadKeyCallback(
216 invalidation::ReadKeyCallback* callback, const std::string& value);
218 StateWriter* state_writer_;
219 invalidation::Scheduler* scheduler_;
220 std::string cached_state_;
223 class SYNC_EXPORT_PRIVATE SyncSystemResources
224 : public NON_EXPORTED_BASE(invalidation::SystemResources) {
225 public:
226 SyncSystemResources(SyncNetworkChannel* sync_network_channel,
227 StateWriter* state_writer);
229 virtual ~SyncSystemResources();
231 // invalidation::SystemResources implementation.
232 virtual void Start() OVERRIDE;
233 virtual void Stop() OVERRIDE;
234 virtual bool IsStarted() const OVERRIDE;
235 virtual void set_platform(const std::string& platform);
236 virtual std::string platform() const OVERRIDE;
237 virtual SyncLogger* logger() OVERRIDE;
238 virtual SyncStorage* storage() OVERRIDE;
239 virtual SyncNetworkChannel* network() OVERRIDE;
240 virtual SyncInvalidationScheduler* internal_scheduler() OVERRIDE;
241 virtual SyncInvalidationScheduler* listener_scheduler() OVERRIDE;
243 private:
244 bool is_started_;
245 std::string platform_;
246 scoped_ptr<SyncLogger> logger_;
247 scoped_ptr<SyncInvalidationScheduler> internal_scheduler_;
248 scoped_ptr<SyncInvalidationScheduler> listener_scheduler_;
249 scoped_ptr<SyncStorage> storage_;
250 // sync_network_channel_ is owned by SyncInvalidationListener.
251 SyncNetworkChannel* sync_network_channel_;
254 } // namespace syncer
256 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_