- add sources.
[platform/framework/web/crosswalk.git] / src / sync / sessions / sync_session_context.h
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. 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 SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
19 #define SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
20
21 #include <map>
22 #include <string>
23 #include <vector>
24
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"
35
36 namespace syncer {
37
38 class ExtensionsActivity;
39 class ServerConnectionManager;
40
41 namespace syncable {
42 class Directory;
43 }
44
45 // Default number of items a client can commit in a single message.
46 static const int kDefaultMaxCommitBatchSize = 25;
47
48 namespace sessions {
49 class TestScopedSessionEventListener;
50
51 class SYNC_EXPORT_PRIVATE SyncSessionContext {
52  public:
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);
63
64   ~SyncSessionContext();
65
66   ServerConnectionManager* connection_manager() {
67     return connection_manager_;
68   }
69   syncable::Directory* directory() {
70     return directory_;
71   }
72
73   const ModelSafeRoutingInfo& routing_info() const {
74     return routing_info_;
75   }
76
77   void set_routing_info(const ModelSafeRoutingInfo& routing_info);
78
79   UpdateHandlerMap* update_handler_map() {
80     return &update_handler_map_;
81   }
82
83   CommitContributorMap* commit_contributor_map() {
84     return &commit_contributor_map_;
85   }
86
87   const std::vector<scoped_refptr<ModelSafeWorker> >& workers() const {
88     return workers_;
89   }
90
91   ExtensionsActivity* extensions_activity() {
92     return extensions_activity_.get();
93   }
94
95   DebugInfoGetter* debug_info_getter() {
96     return debug_info_getter_;
97   }
98
99   // Talk notification status.
100   void set_notifications_enabled(bool enabled) {
101     notifications_enabled_ = enabled;
102   }
103   bool notifications_enabled() { return notifications_enabled_; }
104
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;
109   }
110   const std::string& account_name() const { return account_name_; }
111
112   void set_max_commit_batch_size(int batch_size) {
113     max_commit_batch_size_ = batch_size;
114   }
115   int32 max_commit_batch_size() const { return max_commit_batch_size_; }
116
117   void NotifyListeners(const SyncEngineEvent& event) {
118     FOR_EACH_OBSERVER(SyncEngineEventListener, listeners_,
119                       OnSyncEngineEvent(event));
120   }
121
122   TrafficRecorder* traffic_recorder() {
123     return traffic_recorder_;
124   }
125
126   bool keystore_encryption_enabled() const {
127     return keystore_encryption_enabled_;
128   }
129
130   void set_hierarchy_conflict_detected(bool value) {
131     client_status_.set_hierarchy_conflict_detected(value);
132   }
133
134   const sync_pb::ClientStatus& client_status() const {
135     return client_status_;
136   }
137
138   const std::string& invalidator_client_id() const {
139     return invalidator_client_id_;
140   }
141
142   bool ShouldFetchUpdatesBeforeCommit() const {
143     return !(server_enabled_pre_commit_update_avoidance_ ||
144              client_enabled_pre_commit_update_avoidance_);
145   }
146
147   void set_server_enabled_pre_commit_update_avoidance(bool value) {
148     server_enabled_pre_commit_update_avoidance_ = value;
149   }
150
151  private:
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;
156
157   ObserverList<SyncEngineEventListener> listeners_;
158
159   ServerConnectionManager* const connection_manager_;
160   syncable::Directory* const directory_;
161
162   // A cached copy of SyncBackendRegistrar's routing info.
163   // Must be updated manually when SBR's state is modified.
164   ModelSafeRoutingInfo routing_info_;
165
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_;
170
171   // Deleter for the |update_handler_map_|.
172   STLValueDeleter<UpdateHandlerMap> update_handler_deleter_;
173
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_;
178
179   // Deleter for the |commit_contributor_map_|.
180   STLValueDeleter<CommitContributorMap> commit_contributor_deleter_;
181
182   // The set of ModelSafeWorkers.  Used to execute tasks of various threads.
183   std::vector<scoped_refptr<ModelSafeWorker> > workers_;
184
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_;
188
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_;
192
193   // The name of the account being synced.
194   std::string account_name_;
195
196   // The server limits the number of items a client can commit in one batch.
197   int max_commit_batch_size_;
198
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_;
202
203   TrafficRecorder* traffic_recorder_;
204
205   // Satus information to be sent up to the server.
206   sync_pb::ClientStatus client_status_;
207
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_;
212
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_;
218
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_;
223
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_;
227
228   DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
229 };
230
231 }  // namespace sessions
232 }  // namespace syncer
233
234 #endif  // SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_