| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 // SyncSessionContext encapsulates the contextual information and engine |
| 6 // components specific to a SyncSession. A context is accessible via |
| 7 // a SyncSession so that session SyncerCommands and parts of the engine have |
| 8 // a convenient way to access other parts. In this way it can be thought of as |
| 9 // the surrounding environment for the SyncSession. The components of this |
| 10 // environment are either valid or not valid for the entire context lifetime, |
| 11 // or they are valid for explicitly scoped periods of time by using Scoped |
| 12 // installation utilities found below. This means that the context assumes no |
| 13 // ownership whatsoever of any object that was not created by the context |
| 14 // itself. |
| 15 // |
| 16 // It can only be used from the SyncerThread. |
| 17 |
| 18 #ifndef CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_ |
| 19 #define CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_ |
| 20 |
| 21 #include <string> |
| 22 #include "chrome/browser/chrome_thread.h" |
| 23 #include "chrome/browser/sync/engine/model_safe_worker.h" |
| 24 #include "chrome/browser/sync/engine/syncer_types.h" |
| 25 #include "chrome/browser/sync/util/extensions_activity_monitor.h" |
| 26 |
| 27 namespace syncable { |
| 28 class DirectoryManager; |
| 29 } |
| 30 |
| 31 namespace browser_sync { |
| 32 |
| 33 class ConflictResolver; |
| 34 class ServerConnectionManager; |
| 35 |
| 36 namespace sessions { |
| 37 class ScopedSessionContextConflictResolver; |
| 38 class ScopedSessionContextSyncerEventChannel; |
| 39 |
| 40 class SyncSessionContext { |
| 41 public: |
| 42 SyncSessionContext(ServerConnectionManager* connection_manager, |
| 43 syncable::DirectoryManager* directory_manager, |
| 44 ModelSafeWorker* model_safe_worker) |
| 45 : resolver_(NULL), |
| 46 syncer_event_channel_(NULL), |
| 47 connection_manager_(connection_manager), |
| 48 directory_manager_(directory_manager), |
| 49 model_safe_worker_(model_safe_worker), |
| 50 extensions_activity_monitor_(new ExtensionsActivityMonitor()), |
| 51 notifications_enabled_(false) { |
| 52 } |
| 53 |
| 54 ~SyncSessionContext() { |
| 55 // In unittests, there may be no UI thread, so the above will fail. |
| 56 if (!ChromeThread::DeleteSoon(ChromeThread::UI, FROM_HERE, |
| 57 extensions_activity_monitor_)) { |
| 58 delete extensions_activity_monitor_; |
| 59 } |
| 60 } |
| 61 |
| 62 ConflictResolver* resolver() { return resolver_; } |
| 63 ServerConnectionManager* connection_manager() { |
| 64 return connection_manager_; |
| 65 } |
| 66 syncable::DirectoryManager* directory_manager() { |
| 67 return directory_manager_; |
| 68 } |
| 69 SyncerEventChannel* syncer_event_channel() { |
| 70 return syncer_event_channel_; |
| 71 } |
| 72 ModelSafeWorker* model_safe_worker() { |
| 73 return model_safe_worker_.get(); |
| 74 } |
| 75 ExtensionsActivityMonitor* extensions_monitor() { |
| 76 return extensions_activity_monitor_; |
| 77 } |
| 78 |
| 79 // Talk notification status. |
| 80 void set_notifications_enabled(bool enabled) { |
| 81 notifications_enabled_ = enabled; |
| 82 } |
| 83 bool notifications_enabled() { return notifications_enabled_; } |
| 84 |
| 85 // Account name, set once a directory has been opened. |
| 86 void set_account_name(const std::string name) { |
| 87 DCHECK(account_name_.empty()); |
| 88 account_name_ = name; |
| 89 } |
| 90 const std::string& account_name() { return account_name_; } |
| 91 |
| 92 private: |
| 93 // Rather than force clients to set and null-out various context members, we |
| 94 // extend our encapsulation boundary to scoped helpers that take care of this |
| 95 // once they are allocated. See definitions of these below. |
| 96 friend class ScopedSessionContextConflictResolver; |
| 97 friend class ScopedSessionContextSyncerEventChannel; |
| 98 |
| 99 // These are installed by Syncer objects when needed and may be NULL. |
| 100 ConflictResolver* resolver_; |
| 101 SyncerEventChannel* syncer_event_channel_; |
| 102 |
| 103 ServerConnectionManager* const connection_manager_; |
| 104 syncable::DirectoryManager* const directory_manager_; |
| 105 |
| 106 // A worker capable of processing work closures on a thread that is |
| 107 // guaranteed to be safe for model modifications. |
| 108 scoped_ptr<ModelSafeWorker> model_safe_worker_; |
| 109 |
| 110 // We use this to stuff extensions activity into CommitMessages so the server |
| 111 // can correlate commit traffic with extension-related bookmark mutations. |
| 112 ExtensionsActivityMonitor* extensions_activity_monitor_; |
| 113 |
| 114 // Kept up to date with talk events to determine whether notifications are |
| 115 // enabled. True only if the notification channel is authorized and open. |
| 116 bool notifications_enabled_; |
| 117 |
| 118 // The name of the account being synced. |
| 119 std::string account_name_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(SyncSessionContext); |
| 122 }; |
| 123 |
| 124 // Installs a ConflictResolver to a given session context for the lifetime of |
| 125 // the ScopedSessionContextConflictResolver. There should never be more than |
| 126 // one ConflictResolver in the system, so it is an error to use this if the |
| 127 // context already has a resolver. |
| 128 class ScopedSessionContextConflictResolver { |
| 129 public: |
| 130 // Note: |context| and |resolver| should outlive |this|. |
| 131 ScopedSessionContextConflictResolver(SyncSessionContext* context, |
| 132 ConflictResolver* resolver) |
| 133 : context_(context), resolver_(resolver) { |
| 134 DCHECK(NULL == context->resolver_); |
| 135 context->resolver_ = resolver; |
| 136 } |
| 137 ~ScopedSessionContextConflictResolver() { |
| 138 context_->resolver_ = NULL; |
| 139 } |
| 140 private: |
| 141 SyncSessionContext* context_; |
| 142 ConflictResolver* resolver_; |
| 143 DISALLOW_COPY_AND_ASSIGN(ScopedSessionContextConflictResolver); |
| 144 }; |
| 145 |
| 146 // Installs a SyncerEventChannel to a given session context for the lifetime of |
| 147 // the ScopedSessionContextSyncerEventChannel. There should never be more than |
| 148 // one SyncerEventChannel in the context, so it is an error to use this if the |
| 149 // context already has a channel. |
| 150 class ScopedSessionContextSyncerEventChannel { |
| 151 public: |
| 152 ScopedSessionContextSyncerEventChannel(SyncSessionContext* context, |
| 153 SyncerEventChannel* channel) |
| 154 : context_(context), channel_(channel) { |
| 155 DCHECK(NULL == context->syncer_event_channel_); |
| 156 context->syncer_event_channel_ = channel_; |
| 157 } |
| 158 ~ScopedSessionContextSyncerEventChannel() { |
| 159 context_->syncer_event_channel_ = NULL; |
| 160 } |
| 161 private: |
| 162 SyncSessionContext* context_; |
| 163 SyncerEventChannel* channel_; |
| 164 DISALLOW_COPY_AND_ASSIGN(ScopedSessionContextSyncerEventChannel); |
| 165 }; |
| 166 |
| 167 } // namespace sessions |
| 168 } // namespace browser_sync |
| 169 |
| 170 #endif // CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_ |
| OLD | NEW |