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 // 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
16 // It can only be used from the SyncerThread.
18 #ifndef SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
19 #define SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
25 #include "base/stl_util.h"
26 #include "sync/base/sync_export.h"
27 #include "sync/engine/sync_directory_commit_contributor.h"
28 #include "sync/engine/sync_directory_update_handler.h"
29 #include "sync/engine/sync_engine_event.h"
30 #include "sync/engine/syncer_types.h"
31 #include "sync/engine/traffic_recorder.h"
32 #include "sync/internal_api/public/engine/model_safe_worker.h"
33 #include "sync/protocol/sync.pb.h"
34 #include "sync/sessions/debug_info_getter.h"
38 class ExtensionsActivity;
39 class ServerConnectionManager;
45 // Default number of items a client can commit in a single message.
46 static const int kDefaultMaxCommitBatchSize = 25;
49 class TestScopedSessionEventListener;
51 class SYNC_EXPORT_PRIVATE SyncSessionContext {
53 SyncSessionContext(ServerConnectionManager* connection_manager,
54 syncable::Directory* directory,
55 const std::vector<ModelSafeWorker*>& workers,
56 ExtensionsActivity* extensions_activity,
57 const std::vector<SyncEngineEventListener*>& listeners,
58 DebugInfoGetter* debug_info_getter,
59 TrafficRecorder* traffic_recorder,
60 bool keystore_encryption_enabled,
61 bool client_enabled_pre_commit_update_avoidance,
62 const std::string& invalidator_client_id);
64 ~SyncSessionContext();
66 ServerConnectionManager* connection_manager() {
67 return connection_manager_;
69 syncable::Directory* directory() {
73 const ModelSafeRoutingInfo& routing_info() const {
77 void set_routing_info(const ModelSafeRoutingInfo& routing_info);
79 UpdateHandlerMap* update_handler_map() {
80 return &update_handler_map_;
83 CommitContributorMap* commit_contributor_map() {
84 return &commit_contributor_map_;
87 const std::vector<scoped_refptr<ModelSafeWorker> >& workers() const {
91 ExtensionsActivity* extensions_activity() {
92 return extensions_activity_.get();
95 DebugInfoGetter* debug_info_getter() {
96 return debug_info_getter_;
99 // Talk notification status.
100 void set_notifications_enabled(bool enabled) {
101 notifications_enabled_ = enabled;
103 bool notifications_enabled() { return notifications_enabled_; }
105 // Account name, set once a directory has been opened.
106 void set_account_name(const std::string& name) {
107 DCHECK(account_name_.empty());
108 account_name_ = name;
110 const std::string& account_name() const { return account_name_; }
112 void set_max_commit_batch_size(int batch_size) {
113 max_commit_batch_size_ = batch_size;
115 int32 max_commit_batch_size() const { return max_commit_batch_size_; }
117 void NotifyListeners(const SyncEngineEvent& event) {
118 FOR_EACH_OBSERVER(SyncEngineEventListener, listeners_,
119 OnSyncEngineEvent(event));
122 TrafficRecorder* traffic_recorder() {
123 return traffic_recorder_;
126 bool keystore_encryption_enabled() const {
127 return keystore_encryption_enabled_;
130 void set_hierarchy_conflict_detected(bool value) {
131 client_status_.set_hierarchy_conflict_detected(value);
134 const sync_pb::ClientStatus& client_status() const {
135 return client_status_;
138 const std::string& invalidator_client_id() const {
139 return invalidator_client_id_;
142 bool ShouldFetchUpdatesBeforeCommit() const {
143 return !(server_enabled_pre_commit_update_avoidance_ ||
144 client_enabled_pre_commit_update_avoidance_);
147 void set_server_enabled_pre_commit_update_avoidance(bool value) {
148 server_enabled_pre_commit_update_avoidance_ = value;
152 // Rather than force clients to set and null-out various context members, we
153 // extend our encapsulation boundary to scoped helpers that take care of this
154 // once they are allocated. See definitions of these below.
155 friend class TestScopedSessionEventListener;
157 ObserverList<SyncEngineEventListener> listeners_;
159 ServerConnectionManager* const connection_manager_;
160 syncable::Directory* const directory_;
162 // A cached copy of SyncBackendRegistrar's routing info.
163 // Must be updated manually when SBR's state is modified.
164 ModelSafeRoutingInfo routing_info_;
166 // A map of 'update handlers', one for each enabled type.
167 // This must be kept in sync with the routing info. Our temporary solution to
168 // that problem is to initialize this map in set_routing_info().
169 UpdateHandlerMap update_handler_map_;
171 // Deleter for the |update_handler_map_|.
172 STLValueDeleter<UpdateHandlerMap> update_handler_deleter_;
174 // A map of 'commit contributors', one for each enabled type.
175 // This must be kept in sync with the routing info. Our temporary solution to
176 // that problem is to initialize this map in set_routing_info().
177 CommitContributorMap commit_contributor_map_;
179 // Deleter for the |commit_contributor_map_|.
180 STLValueDeleter<CommitContributorMap> commit_contributor_deleter_;
182 // The set of ModelSafeWorkers. Used to execute tasks of various threads.
183 std::vector<scoped_refptr<ModelSafeWorker> > workers_;
185 // We use this to stuff extensions activity into CommitMessages so the server
186 // can correlate commit traffic with extension-related bookmark mutations.
187 scoped_refptr<ExtensionsActivity> extensions_activity_;
189 // Kept up to date with talk events to determine whether notifications are
190 // enabled. True only if the notification channel is authorized and open.
191 bool notifications_enabled_;
193 // The name of the account being synced.
194 std::string account_name_;
196 // The server limits the number of items a client can commit in one batch.
197 int max_commit_batch_size_;
199 // We use this to get debug info to send to the server for debugging
200 // client behavior on server side.
201 DebugInfoGetter* const debug_info_getter_;
203 TrafficRecorder* traffic_recorder_;
205 // Satus information to be sent up to the server.
206 sync_pb::ClientStatus client_status_;
208 // Temporary variable while keystore encryption is behind a flag. True if
209 // we should attempt performing keystore encryption related work, false if
210 // the experiment is not enabled.
211 bool keystore_encryption_enabled_;
213 // This is a copy of the identifier the that the invalidations client used to
214 // register itself with the invalidations server during startup. We need to
215 // provide this to the sync server when we make changes to enable it to
216 // prevent us from receiving notifications of changes we make ourselves.
217 const std::string invalidator_client_id_;
219 // Flag to enable or disable the no pre-commit GetUpdates experiment. When
220 // this flag is set to false, the syncer has the option of not performing at
221 // GetUpdates request when there is nothing to fetch.
222 bool server_enabled_pre_commit_update_avoidance_;
224 // If true, indicates that we've been passed a command-line flag to force
225 // enable the pre-commit update avoidance experiment described above.
226 const bool client_enabled_pre_commit_update_avoidance_;
228 DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
231 } // namespace sessions
232 } // namespace syncer
234 #endif // SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_