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/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 // 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 class SyncStorage
: public invalidation::Storage
{
83 SyncStorage(StateWriter
* state_writer
, invalidation::Scheduler
* scheduler
);
85 virtual ~SyncStorage();
87 void SetInitialState(const std::string
& value
) {
88 cached_state_
= value
;
91 // invalidation::Storage implementation.
92 virtual void WriteKey(const std::string
& key
, const std::string
& value
,
93 invalidation::WriteKeyCallback
* done
) OVERRIDE
;
95 virtual void ReadKey(const std::string
& key
,
96 invalidation::ReadKeyCallback
* done
) OVERRIDE
;
98 virtual void DeleteKey(const std::string
& key
,
99 invalidation::DeleteKeyCallback
* done
) OVERRIDE
;
101 virtual void ReadAllKeys(
102 invalidation::ReadAllKeysCallback
* key_callback
) OVERRIDE
;
104 virtual void SetSystemResources(
105 invalidation::SystemResources
* resources
) OVERRIDE
;
108 // Runs the given storage callback with SUCCESS status and deletes it.
109 void RunAndDeleteWriteKeyCallback(
110 invalidation::WriteKeyCallback
* callback
);
112 // Runs the given callback with the given value and deletes it.
113 void RunAndDeleteReadKeyCallback(
114 invalidation::ReadKeyCallback
* callback
, const std::string
& value
);
116 StateWriter
* state_writer_
;
117 invalidation::Scheduler
* scheduler_
;
118 std::string cached_state_
;
121 class SYNC_EXPORT_PRIVATE SyncSystemResources
122 : public NON_EXPORTED_BASE(invalidation::SystemResources
) {
124 SyncSystemResources(scoped_ptr
<notifier::PushClient
> push_client
,
125 StateWriter
* state_writer
);
127 virtual ~SyncSystemResources();
129 // invalidation::SystemResources implementation.
130 virtual void Start() OVERRIDE
;
131 virtual void Stop() OVERRIDE
;
132 virtual bool IsStarted() const OVERRIDE
;
133 virtual void set_platform(const std::string
& platform
);
134 virtual std::string
platform() const OVERRIDE
;
135 virtual SyncLogger
* logger() OVERRIDE
;
136 virtual SyncStorage
* storage() OVERRIDE
;
137 virtual PushClientChannel
* network() OVERRIDE
;
138 virtual SyncInvalidationScheduler
* internal_scheduler() OVERRIDE
;
139 virtual SyncInvalidationScheduler
* listener_scheduler() OVERRIDE
;
143 std::string platform_
;
144 scoped_ptr
<SyncLogger
> logger_
;
145 scoped_ptr
<SyncInvalidationScheduler
> internal_scheduler_
;
146 scoped_ptr
<SyncInvalidationScheduler
> listener_scheduler_
;
147 scoped_ptr
<SyncStorage
> storage_
;
148 PushClientChannel push_client_channel_
;
151 } // namespace syncer
153 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_