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