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. 8 9 #ifndef SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_ 10 #define SYNC_NOTIFIER_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 "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" 25 26 namespace notifier { 27 class PushClient; 28 } // namespace notifier 29 30 namespace syncer { 31 32 class SyncLogger : public invalidation::Logger { 33 public: 34 SyncLogger(); 35 36 virtual ~SyncLogger(); 37 38 // invalidation::Logger implementation. 39 virtual void Log(LogLevel level, const char* file, int line, 40 const char* format, ...) OVERRIDE; 41 42 virtual void SetSystemResources( 43 invalidation::SystemResources* resources) OVERRIDE; 44 }; 45 46 class SyncInvalidationScheduler : public invalidation::Scheduler { 47 public: 48 SyncInvalidationScheduler(); 49 50 virtual ~SyncInvalidationScheduler(); 51 52 // Start and stop the scheduler. 53 void Start(); 54 void Stop(); 55 56 // invalidation::Scheduler implementation. 57 virtual void Schedule(invalidation::TimeDelta delay, 58 invalidation::Closure* task) OVERRIDE; 59 60 virtual bool IsRunningOnThread() const OVERRIDE; 61 62 virtual invalidation::Time GetCurrentTime() const OVERRIDE; 63 64 virtual void SetSystemResources( 65 invalidation::SystemResources* resources) OVERRIDE; 66 67 private: 68 base::WeakPtrFactory<SyncInvalidationScheduler> weak_factory_; 69 // Holds all posted tasks that have not yet been run. 70 std::set<invalidation::Closure*> posted_tasks_; 71 72 const base::MessageLoop* created_on_loop_; 73 bool is_started_; 74 bool is_stopped_; 75 76 // Runs the task, deletes it, and removes it from |posted_tasks_|. 77 void RunPostedTask(invalidation::Closure* task); 78 }; 79 80 class SyncStorage : public invalidation::Storage { 81 public: 82 SyncStorage(StateWriter* state_writer, invalidation::Scheduler* scheduler); 83 84 virtual ~SyncStorage(); 85 86 void SetInitialState(const std::string& value) { 87 cached_state_ = value; 88 } 89 90 // invalidation::Storage implementation. 91 virtual void WriteKey(const std::string& key, const std::string& value, 92 invalidation::WriteKeyCallback* done) OVERRIDE; 93 94 virtual void ReadKey(const std::string& key, 95 invalidation::ReadKeyCallback* done) OVERRIDE; 96 97 virtual void DeleteKey(const std::string& key, 98 invalidation::DeleteKeyCallback* done) OVERRIDE; 99 100 virtual void ReadAllKeys( 101 invalidation::ReadAllKeysCallback* key_callback) OVERRIDE; 102 103 virtual void SetSystemResources( 104 invalidation::SystemResources* resources) OVERRIDE; 105 106 private: 107 // Runs the given storage callback with SUCCESS status and deletes it. 108 void RunAndDeleteWriteKeyCallback( 109 invalidation::WriteKeyCallback* callback); 110 111 // Runs the given callback with the given value and deletes it. 112 void RunAndDeleteReadKeyCallback( 113 invalidation::ReadKeyCallback* callback, const std::string& value); 114 115 StateWriter* state_writer_; 116 invalidation::Scheduler* scheduler_; 117 std::string cached_state_; 118 }; 119 120 class SYNC_EXPORT_PRIVATE SyncSystemResources 121 : public NON_EXPORTED_BASE(invalidation::SystemResources) { 122 public: 123 SyncSystemResources(scoped_ptr<notifier::PushClient> push_client, 124 StateWriter* state_writer); 125 126 virtual ~SyncSystemResources(); 127 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; 139 140 private: 141 bool is_started_; 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_; 148 }; 149 150 } // namespace syncer 151 152 #endif // SYNC_NOTIFIER_SYNC_SYSTEM_RESOURCES_H_ 153