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.
5 // Simple system resources class that uses the current message loop
6 // for scheduling. Assumes the current message loop is already
9 #ifndef COMPONENTS_INVALIDATION_SYNC_SYSTEM_RESOURCES_H_
10 #define COMPONENTS_INVALIDATION_SYNC_SYSTEM_RESOURCES_H_
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/state_writer.h"
24 #include "google/cacheinvalidation/include/system-resources.h"
25 #include "jingle/notifier/base/notifier_options.h"
26 #include "sync/internal_api/public/base/invalidator_state.h"
30 class GCMNetworkChannelDelegate
;
32 class SyncLogger
: public invalidation::Logger
{
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
{
48 SyncInvalidationScheduler();
50 virtual ~SyncInvalidationScheduler();
52 // Start and stop the scheduler.
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
;
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_
;
78 base::WeakPtrFactory
<SyncInvalidationScheduler
> weak_factory_
;
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 INVALIDATION_EXPORT_PRIVATE SyncNetworkChannel
88 : public NON_EXPORTED_BASE(invalidation::NetworkChannel
) {
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 // 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
;
114 // Subclass should implement UpdateCredentials to pass new token to channel
116 virtual void UpdateCredentials(const std::string
& email
,
117 const std::string
& token
) = 0;
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;
124 // Subclass should implement RequestDetailedStatus to provide debugging
126 virtual void RequestDetailedStatus(
127 base::Callback
<void(const base::DictionaryValue
&)> callback
) = 0;
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
);
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
);
142 // Get the count of how many valid received messages were received.
143 int GetReceivedMessagesCount() const;
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
);
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
);
157 // Subclass should call DeliverIncomingMessage for message to reach
158 // invalidations library.
159 bool DeliverIncomingMessage(const std::string
& message
);
162 typedef std::vector
<invalidation::NetworkStatusCallback
*>
163 NetworkStatusReceiverList
;
165 // Callbacks into invalidation library
166 scoped_ptr
<invalidation::MessageCallback
> incoming_receiver_
;
167 NetworkStatusReceiverList network_status_receivers_
;
169 // Last network status for new network status receivers.
170 bool last_network_status_
;
172 int received_messages_count_
;
174 ObserverList
<Observer
> observers_
;
177 class SyncStorage
: public invalidation::Storage
{
179 SyncStorage(StateWriter
* state_writer
, invalidation::Scheduler
* scheduler
);
181 virtual ~SyncStorage();
183 void SetInitialState(const std::string
& value
) {
184 cached_state_
= value
;
187 // invalidation::Storage implementation.
188 virtual void WriteKey(const std::string
& key
, const std::string
& value
,
189 invalidation::WriteKeyCallback
* done
) OVERRIDE
;
191 virtual void ReadKey(const std::string
& key
,
192 invalidation::ReadKeyCallback
* done
) OVERRIDE
;
194 virtual void DeleteKey(const std::string
& key
,
195 invalidation::DeleteKeyCallback
* done
) OVERRIDE
;
197 virtual void ReadAllKeys(
198 invalidation::ReadAllKeysCallback
* key_callback
) OVERRIDE
;
200 virtual void SetSystemResources(
201 invalidation::SystemResources
* resources
) OVERRIDE
;
204 // Runs the given storage callback with SUCCESS status and deletes it.
205 void RunAndDeleteWriteKeyCallback(
206 invalidation::WriteKeyCallback
* callback
);
208 // Runs the given callback with the given value and deletes it.
209 void RunAndDeleteReadKeyCallback(
210 invalidation::ReadKeyCallback
* callback
, const std::string
& value
);
212 StateWriter
* state_writer_
;
213 invalidation::Scheduler
* scheduler_
;
214 std::string cached_state_
;
217 class INVALIDATION_EXPORT_PRIVATE SyncSystemResources
218 : public NON_EXPORTED_BASE(invalidation::SystemResources
) {
220 SyncSystemResources(SyncNetworkChannel
* sync_network_channel
,
221 StateWriter
* state_writer
);
223 virtual ~SyncSystemResources();
225 // invalidation::SystemResources implementation.
226 virtual void Start() OVERRIDE
;
227 virtual void Stop() OVERRIDE
;
228 virtual bool IsStarted() const OVERRIDE
;
229 virtual void set_platform(const std::string
& platform
);
230 virtual std::string
platform() const OVERRIDE
;
231 virtual SyncLogger
* logger() OVERRIDE
;
232 virtual SyncStorage
* storage() OVERRIDE
;
233 virtual SyncNetworkChannel
* network() OVERRIDE
;
234 virtual SyncInvalidationScheduler
* internal_scheduler() OVERRIDE
;
235 virtual SyncInvalidationScheduler
* listener_scheduler() OVERRIDE
;
239 std::string platform_
;
240 scoped_ptr
<SyncLogger
> logger_
;
241 scoped_ptr
<SyncInvalidationScheduler
> internal_scheduler_
;
242 scoped_ptr
<SyncInvalidationScheduler
> listener_scheduler_
;
243 scoped_ptr
<SyncStorage
> storage_
;
244 // sync_network_channel_ is owned by SyncInvalidationListener.
245 SyncNetworkChannel
* sync_network_channel_
;
248 } // namespace syncer
250 #endif // COMPONENTS_INVALIDATION_SYNC_SYSTEM_RESOURCES_H_