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.
5 // Simple system resources class that uses the current message loop
6 // for scheduling. Assumes the current message loop is already
9 #ifndef SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_
10 #define SYNC_NOTIFIER_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.h"
20 #include "base/threading/non_thread_safe.h"
21 #include "google/cacheinvalidation/include/system-resources.h"
22 #include "sync/base/sync_export.h"
23 #include "sync/notifier/push_client_channel.h"
24 #include "sync/notifier/state_writer.h"
28 } // namespace notifier
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 base::WeakPtrFactory
<SyncInvalidationScheduler
> weak_factory_
;
69 // Holds all posted tasks that have not yet been run.
70 std::set
<invalidation::Closure
*> posted_tasks_
;
72 const MessageLoop
* created_on_loop_
;
76 // Runs the task, deletes it, and removes it from |posted_tasks_|.
77 void RunPostedTask(invalidation::Closure
* task
);
80 class SyncStorage
: public invalidation::Storage
{
82 SyncStorage(StateWriter
* state_writer
, invalidation::Scheduler
* scheduler
);
84 virtual ~SyncStorage();
86 void SetInitialState(const std::string
& value
) {
87 cached_state_
= value
;
90 // invalidation::Storage implementation.
91 virtual void WriteKey(const std::string
& key
, const std::string
& value
,
92 invalidation::WriteKeyCallback
* done
) OVERRIDE
;
94 virtual void ReadKey(const std::string
& key
,
95 invalidation::ReadKeyCallback
* done
) OVERRIDE
;
97 virtual void DeleteKey(const std::string
& key
,
98 invalidation::DeleteKeyCallback
* done
) OVERRIDE
;
100 virtual void ReadAllKeys(
101 invalidation::ReadAllKeysCallback
* key_callback
) OVERRIDE
;
103 virtual void SetSystemResources(
104 invalidation::SystemResources
* resources
) OVERRIDE
;
107 // Runs the given storage callback with SUCCESS status and deletes it.
108 void RunAndDeleteWriteKeyCallback(
109 invalidation::WriteKeyCallback
* callback
);
111 // Runs the given callback with the given value and deletes it.
112 void RunAndDeleteReadKeyCallback(
113 invalidation::ReadKeyCallback
* callback
, const std::string
& value
);
115 StateWriter
* state_writer_
;
116 invalidation::Scheduler
* scheduler_
;
117 std::string cached_state_
;
120 class SYNC_EXPORT_PRIVATE SyncSystemResources
121 : public NON_EXPORTED_BASE(invalidation::SystemResources
) {
123 SyncSystemResources(scoped_ptr
<notifier::PushClient
> push_client
,
124 StateWriter
* state_writer
);
126 virtual ~SyncSystemResources();
128 // invalidation::SystemResources implementation.
129 virtual void Start() OVERRIDE
;
130 virtual void Stop() OVERRIDE
;
131 virtual bool IsStarted() const OVERRIDE
;
132 virtual void set_platform(const std::string
& platform
);
133 virtual std::string
platform() const OVERRIDE
;
134 virtual SyncLogger
* logger() OVERRIDE
;
135 virtual SyncStorage
* storage() OVERRIDE
;
136 virtual PushClientChannel
* network() OVERRIDE
;
137 virtual SyncInvalidationScheduler
* internal_scheduler() OVERRIDE
;
138 virtual SyncInvalidationScheduler
* listener_scheduler() OVERRIDE
;
142 std::string platform_
;
143 scoped_ptr
<SyncLogger
> logger_
;
144 scoped_ptr
<SyncInvalidationScheduler
> internal_scheduler_
;
145 scoped_ptr
<SyncInvalidationScheduler
> listener_scheduler_
;
146 scoped_ptr
<SyncStorage
> storage_
;
147 PushClientChannel push_client_channel_
;
150 } // namespace syncer
152 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_