c7661ce036c7874d3351187cc3b26151cfb1c0a3
[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.  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 <string>
19
20 #include "sync/base/sync_export.h"
21 #include "sync/engine/sync_engine_event_listener.h"
22 #include "sync/engine/traffic_recorder.h"
23 #include "sync/sessions/debug_info_getter.h"
24 #include "sync/sessions/model_type_registry.h"
25
26 namespace syncer {
27
28 class ExtensionsActivity;
29 class ModelTypeRegistry;
30 class ServerConnectionManager;
31
32 namespace syncable {
33 class Directory;
34 }
35
36 // Default number of items a client can commit in a single message.
37 static const int kDefaultMaxCommitBatchSize = 25;
38
39 namespace sessions {
40 class TestScopedSessionEventListener;
41
42 class SYNC_EXPORT_PRIVATE SyncSessionContext {
43  public:
44   SyncSessionContext(
45       ServerConnectionManager* connection_manager,
46       syncable::Directory* directory,
47       ExtensionsActivity* extensions_activity,
48       const std::vector<SyncEngineEventListener*>& listeners,
49       DebugInfoGetter* debug_info_getter,
50       TrafficRecorder* traffic_recorder,
51       ModelTypeRegistry* model_type_registry,
52       bool keystore_encryption_enabled,
53       bool client_enabled_pre_commit_update_avoidance,
54       const std::string& invalidator_client_id);
55
56   ~SyncSessionContext();
57
58   ServerConnectionManager* connection_manager() {
59     return connection_manager_;
60   }
61   syncable::Directory* directory() {
62     return directory_;
63   }
64
65   ModelTypeSet GetEnabledTypes() const;
66
67   void SetRoutingInfo(const ModelSafeRoutingInfo& routing_info);
68
69   ExtensionsActivity* extensions_activity() {
70     return extensions_activity_.get();
71   }
72
73   DebugInfoGetter* debug_info_getter() {
74     return debug_info_getter_;
75   }
76
77   // Talk notification status.
78   void set_notifications_enabled(bool enabled) {
79     notifications_enabled_ = enabled;
80   }
81   bool notifications_enabled() { return notifications_enabled_; }
82
83   // Account name, set once a directory has been opened.
84   void set_account_name(const std::string& name) {
85     DCHECK(account_name_.empty());
86     account_name_ = name;
87   }
88   const std::string& account_name() const { return account_name_; }
89
90   void set_max_commit_batch_size(int batch_size) {
91     max_commit_batch_size_ = batch_size;
92   }
93   int32 max_commit_batch_size() const { return max_commit_batch_size_; }
94
95   ObserverList<SyncEngineEventListener>* listeners() {
96     return &listeners_;
97   }
98
99   TrafficRecorder* traffic_recorder() {
100     return traffic_recorder_;
101   }
102
103   bool keystore_encryption_enabled() const {
104     return keystore_encryption_enabled_;
105   }
106
107   void set_hierarchy_conflict_detected(bool value) {
108     client_status_.set_hierarchy_conflict_detected(value);
109   }
110
111   const sync_pb::ClientStatus& client_status() const {
112     return client_status_;
113   }
114
115   const std::string& invalidator_client_id() const {
116     return invalidator_client_id_;
117   }
118
119   bool ShouldFetchUpdatesBeforeCommit() const {
120     return !(server_enabled_pre_commit_update_avoidance_ ||
121              client_enabled_pre_commit_update_avoidance_);
122   }
123
124   void set_server_enabled_pre_commit_update_avoidance(bool value) {
125     server_enabled_pre_commit_update_avoidance_ = value;
126   }
127
128   ModelTypeRegistry* model_type_registry() {
129     return model_type_registry_;
130   }
131
132  private:
133   // Rather than force clients to set and null-out various context members, we
134   // extend our encapsulation boundary to scoped helpers that take care of this
135   // once they are allocated. See definitions of these below.
136   friend class TestScopedSessionEventListener;
137
138   ObserverList<SyncEngineEventListener> listeners_;
139
140   ServerConnectionManager* const connection_manager_;
141   syncable::Directory* const directory_;
142
143   // We use this to stuff extensions activity into CommitMessages so the server
144   // can correlate commit traffic with extension-related bookmark mutations.
145   scoped_refptr<ExtensionsActivity> extensions_activity_;
146
147   // Kept up to date with talk events to determine whether notifications are
148   // enabled. True only if the notification channel is authorized and open.
149   bool notifications_enabled_;
150
151   // The name of the account being synced.
152   std::string account_name_;
153
154   // The server limits the number of items a client can commit in one batch.
155   int max_commit_batch_size_;
156
157   // We use this to get debug info to send to the server for debugging
158   // client behavior on server side.
159   DebugInfoGetter* const debug_info_getter_;
160
161   TrafficRecorder* traffic_recorder_;
162
163   ModelTypeRegistry* model_type_registry_;
164
165   // Satus information to be sent up to the server.
166   sync_pb::ClientStatus client_status_;
167
168   // Temporary variable while keystore encryption is behind a flag. True if
169   // we should attempt performing keystore encryption related work, false if
170   // the experiment is not enabled.
171   bool keystore_encryption_enabled_;
172
173   // This is a copy of the identifier the that the invalidations client used to
174   // register itself with the invalidations server during startup.  We need to
175   // provide this to the sync server when we make changes to enable it to
176   // prevent us from receiving notifications of changes we make ourselves.
177   const std::string invalidator_client_id_;
178
179   // Flag to enable or disable the no pre-commit GetUpdates experiment.  When
180   // this flag is set to false, the syncer has the option of not performing at
181   // GetUpdates request when there is nothing to fetch.
182   bool server_enabled_pre_commit_update_avoidance_;
183
184   // If true, indicates that we've been passed a command-line flag to force
185   // enable the pre-commit update avoidance experiment described above.
186   const bool client_enabled_pre_commit_update_avoidance_;
187
188   DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
189 };
190
191 }  // namespace sessions
192 }  // namespace syncer
193
194 #endif  // SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_