• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // SyncSessionContext encapsulates the contextual information and engine
6 // components specific to a SyncSession.  Unlike the SyncSession, the context
7 // can be reused across several sync cycles.
8 //
9 // The context does not take ownership of its pointer members.  It's up to
10 // the surrounding classes to ensure those members remain valid while the
11 // context is in use.
12 //
13 // It can only be used from the SyncerThread.
14 
15 #ifndef SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
16 #define SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "base/stl_util.h"
23 #include "sync/base/sync_export.h"
24 #include "sync/engine/sync_directory_commit_contributor.h"
25 #include "sync/engine/sync_directory_update_handler.h"
26 #include "sync/engine/sync_engine_event.h"
27 #include "sync/engine/syncer_types.h"
28 #include "sync/engine/traffic_recorder.h"
29 #include "sync/internal_api/public/engine/model_safe_worker.h"
30 #include "sync/protocol/sync.pb.h"
31 #include "sync/sessions/debug_info_getter.h"
32 
33 namespace syncer {
34 
35 class ExtensionsActivity;
36 class ServerConnectionManager;
37 
38 namespace syncable {
39 class Directory;
40 }
41 
42 // Default number of items a client can commit in a single message.
43 static const int kDefaultMaxCommitBatchSize = 25;
44 
45 namespace sessions {
46 class TestScopedSessionEventListener;
47 
48 class SYNC_EXPORT_PRIVATE SyncSessionContext {
49  public:
50   SyncSessionContext(ServerConnectionManager* connection_manager,
51                      syncable::Directory* directory,
52                      const std::vector<ModelSafeWorker*>& workers,
53                      ExtensionsActivity* extensions_activity,
54                      const std::vector<SyncEngineEventListener*>& listeners,
55                      DebugInfoGetter* debug_info_getter,
56                      TrafficRecorder* traffic_recorder,
57                      bool keystore_encryption_enabled,
58                      bool client_enabled_pre_commit_update_avoidance,
59                      const std::string& invalidator_client_id);
60 
61   ~SyncSessionContext();
62 
connection_manager()63   ServerConnectionManager* connection_manager() {
64     return connection_manager_;
65   }
directory()66   syncable::Directory* directory() {
67     return directory_;
68   }
69 
enabled_types()70   ModelTypeSet enabled_types() const {
71     return enabled_types_;
72   }
73 
74   void set_routing_info(const ModelSafeRoutingInfo& routing_info);
75 
update_handler_map()76   UpdateHandlerMap* update_handler_map() {
77     return &update_handler_map_;
78   }
79 
commit_contributor_map()80   CommitContributorMap* commit_contributor_map() {
81     return &commit_contributor_map_;
82   }
83 
extensions_activity()84   ExtensionsActivity* extensions_activity() {
85     return extensions_activity_.get();
86   }
87 
debug_info_getter()88   DebugInfoGetter* debug_info_getter() {
89     return debug_info_getter_;
90   }
91 
92   // Talk notification status.
set_notifications_enabled(bool enabled)93   void set_notifications_enabled(bool enabled) {
94     notifications_enabled_ = enabled;
95   }
notifications_enabled()96   bool notifications_enabled() { return notifications_enabled_; }
97 
98   // Account name, set once a directory has been opened.
set_account_name(const std::string & name)99   void set_account_name(const std::string& name) {
100     DCHECK(account_name_.empty());
101     account_name_ = name;
102   }
account_name()103   const std::string& account_name() const { return account_name_; }
104 
set_max_commit_batch_size(int batch_size)105   void set_max_commit_batch_size(int batch_size) {
106     max_commit_batch_size_ = batch_size;
107   }
max_commit_batch_size()108   int32 max_commit_batch_size() const { return max_commit_batch_size_; }
109 
NotifyListeners(const SyncEngineEvent & event)110   void NotifyListeners(const SyncEngineEvent& event) {
111     FOR_EACH_OBSERVER(SyncEngineEventListener, listeners_,
112                       OnSyncEngineEvent(event));
113   }
114 
traffic_recorder()115   TrafficRecorder* traffic_recorder() {
116     return traffic_recorder_;
117   }
118 
keystore_encryption_enabled()119   bool keystore_encryption_enabled() const {
120     return keystore_encryption_enabled_;
121   }
122 
set_hierarchy_conflict_detected(bool value)123   void set_hierarchy_conflict_detected(bool value) {
124     client_status_.set_hierarchy_conflict_detected(value);
125   }
126 
client_status()127   const sync_pb::ClientStatus& client_status() const {
128     return client_status_;
129   }
130 
invalidator_client_id()131   const std::string& invalidator_client_id() const {
132     return invalidator_client_id_;
133   }
134 
ShouldFetchUpdatesBeforeCommit()135   bool ShouldFetchUpdatesBeforeCommit() const {
136     return !(server_enabled_pre_commit_update_avoidance_ ||
137              client_enabled_pre_commit_update_avoidance_);
138   }
139 
set_server_enabled_pre_commit_update_avoidance(bool value)140   void set_server_enabled_pre_commit_update_avoidance(bool value) {
141     server_enabled_pre_commit_update_avoidance_ = value;
142   }
143 
144  private:
145   // Rather than force clients to set and null-out various context members, we
146   // extend our encapsulation boundary to scoped helpers that take care of this
147   // once they are allocated. See definitions of these below.
148   friend class TestScopedSessionEventListener;
149 
150   ObserverList<SyncEngineEventListener> listeners_;
151 
152   ServerConnectionManager* const connection_manager_;
153   syncable::Directory* const directory_;
154 
155   // The set of enabled types.  Derrived from the routing info set with
156   // set_routing_info().
157   ModelTypeSet enabled_types_;
158 
159   // A map of 'update handlers', one for each enabled type.
160   // This must be kept in sync with the routing info.  Our temporary solution to
161   // that problem is to initialize this map in set_routing_info().
162   UpdateHandlerMap update_handler_map_;
163 
164   // Deleter for the |update_handler_map_|.
165   STLValueDeleter<UpdateHandlerMap> update_handler_deleter_;
166 
167   // A map of 'commit contributors', one for each enabled type.
168   // This must be kept in sync with the routing info.  Our temporary solution to
169   // that problem is to initialize this map in set_routing_info().
170   CommitContributorMap commit_contributor_map_;
171 
172   // Deleter for the |commit_contributor_map_|.
173   STLValueDeleter<CommitContributorMap> commit_contributor_deleter_;
174 
175   // The set of ModelSafeWorkers.  Used to execute tasks of various threads.
176   std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_;
177 
178   // We use this to stuff extensions activity into CommitMessages so the server
179   // can correlate commit traffic with extension-related bookmark mutations.
180   scoped_refptr<ExtensionsActivity> extensions_activity_;
181 
182   // Kept up to date with talk events to determine whether notifications are
183   // enabled. True only if the notification channel is authorized and open.
184   bool notifications_enabled_;
185 
186   // The name of the account being synced.
187   std::string account_name_;
188 
189   // The server limits the number of items a client can commit in one batch.
190   int max_commit_batch_size_;
191 
192   // We use this to get debug info to send to the server for debugging
193   // client behavior on server side.
194   DebugInfoGetter* const debug_info_getter_;
195 
196   TrafficRecorder* traffic_recorder_;
197 
198   // Satus information to be sent up to the server.
199   sync_pb::ClientStatus client_status_;
200 
201   // Temporary variable while keystore encryption is behind a flag. True if
202   // we should attempt performing keystore encryption related work, false if
203   // the experiment is not enabled.
204   bool keystore_encryption_enabled_;
205 
206   // This is a copy of the identifier the that the invalidations client used to
207   // register itself with the invalidations server during startup.  We need to
208   // provide this to the sync server when we make changes to enable it to
209   // prevent us from receiving notifications of changes we make ourselves.
210   const std::string invalidator_client_id_;
211 
212   // Flag to enable or disable the no pre-commit GetUpdates experiment.  When
213   // this flag is set to false, the syncer has the option of not performing at
214   // GetUpdates request when there is nothing to fetch.
215   bool server_enabled_pre_commit_update_avoidance_;
216 
217   // If true, indicates that we've been passed a command-line flag to force
218   // enable the pre-commit update avoidance experiment described above.
219   const bool client_enabled_pre_commit_update_avoidance_;
220 
221   DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
222 };
223 
224 }  // namespace sessions
225 }  // namespace syncer
226 
227 #endif  // SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
228