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 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_CYCLE_SYNC_CYCLE_CONTEXT_H_
6 #define COMPONENTS_SYNC_ENGINE_IMPL_CYCLE_SYNC_CYCLE_CONTEXT_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "base/observer_list.h"
15 #include "base/time/time.h"
16 #include "components/sync/engine_impl/cycle/debug_info_getter.h"
17 #include "components/sync/engine_impl/model_type_registry.h"
18 #include "components/sync/engine_impl/sync_engine_event_listener.h"
19 
20 namespace syncer {
21 
22 class ExtensionsActivity;
23 class ModelTypeRegistry;
24 class ServerConnectionManager;
25 
26 // Default number of items a client can commit in a single message.
27 static const int kDefaultMaxCommitBatchSize = 25;
28 
29 // SyncCycleContext encapsulates the contextual information and engine
30 // components specific to a SyncCycle.  Unlike the SyncCycle, the context
31 // can be reused across several sync cycles.
32 //
33 // The context does not take ownership of its pointer members.  It's up to
34 // the surrounding classes to ensure those members remain valid while the
35 // context is in use.
36 //
37 // It can only be used from the SyncerThread.
38 class SyncCycleContext {
39  public:
40   SyncCycleContext(ServerConnectionManager* connection_manager,
41                    ExtensionsActivity* extensions_activity,
42                    const std::vector<SyncEngineEventListener*>& listeners,
43                    DebugInfoGetter* debug_info_getter,
44                    ModelTypeRegistry* model_type_registry,
45                    const std::string& invalidator_client_id,
46                    const std::string& cache_guid,
47                    const std::string& birthday,
48                    const std::string& bag_of_chips,
49                    base::TimeDelta poll_interval);
50 
51   ~SyncCycleContext();
52 
connection_manager()53   ServerConnectionManager* connection_manager() { return connection_manager_; }
54 
55   ModelTypeSet GetEnabledTypes() const;
56 
extensions_activity()57   ExtensionsActivity* extensions_activity() {
58     return extensions_activity_.get();
59   }
60 
debug_info_getter()61   DebugInfoGetter* debug_info_getter() { return debug_info_getter_; }
62 
63   // Talk notification status.
set_notifications_enabled(bool enabled)64   void set_notifications_enabled(bool enabled) {
65     notifications_enabled_ = enabled;
66   }
notifications_enabled()67   bool notifications_enabled() { return notifications_enabled_; }
68 
cache_guid()69   const std::string& cache_guid() const { return cache_guid_; }
70 
71   void set_birthday(const std::string& birthday);
birthday()72   const std::string& birthday() const { return birthday_; }
73 
74   void set_bag_of_chips(const std::string& bag_of_chips);
bag_of_chips()75   const std::string& bag_of_chips() const { return bag_of_chips_; }
76 
set_account_name(const std::string & name)77   void set_account_name(const std::string& name) { account_name_ = name; }
account_name()78   const std::string& account_name() const { return account_name_; }
79 
set_max_commit_batch_size(int batch_size)80   void set_max_commit_batch_size(int batch_size) {
81     max_commit_batch_size_ = batch_size;
82   }
max_commit_batch_size()83   int32_t max_commit_batch_size() const { return max_commit_batch_size_; }
84 
listeners()85   base::ObserverList<SyncEngineEventListener>::Unchecked* listeners() {
86     return &listeners_;
87   }
88 
set_hierarchy_conflict_detected(bool value)89   void set_hierarchy_conflict_detected(bool value) {
90     client_status_.set_hierarchy_conflict_detected(value);
91   }
92 
set_is_sync_feature_enabled(bool value)93   void set_is_sync_feature_enabled(bool value) {
94     client_status_.set_is_sync_feature_enabled(value);
95   }
96 
client_status()97   const sync_pb::ClientStatus& client_status() const { return client_status_; }
98 
invalidator_client_id()99   const std::string& invalidator_client_id() const {
100     return invalidator_client_id_;
101   }
102 
set_invalidator_client_id(const std::string & id)103   void set_invalidator_client_id(const std::string& id) {
104     invalidator_client_id_ = id;
105   }
106 
model_type_registry()107   ModelTypeRegistry* model_type_registry() { return model_type_registry_; }
108 
cookie_jar_mismatch()109   bool cookie_jar_mismatch() const { return cookie_jar_mismatch_; }
110 
set_cookie_jar_mismatch(bool cookie_jar_mismatch)111   void set_cookie_jar_mismatch(bool cookie_jar_mismatch) {
112     cookie_jar_mismatch_ = cookie_jar_mismatch;
113   }
114 
cookie_jar_empty()115   bool cookie_jar_empty() const { return cookie_jar_empty_; }
116 
set_cookie_jar_empty(bool empty_jar)117   void set_cookie_jar_empty(bool empty_jar) { cookie_jar_empty_ = empty_jar; }
118 
single_client()119   bool single_client() const { return single_client_; }
set_single_client(bool single_client)120   void set_single_client(bool single_client) { single_client_ = single_client; }
121 
poll_interval()122   base::TimeDelta poll_interval() const { return poll_interval_; }
set_poll_interval(base::TimeDelta interval)123   void set_poll_interval(base::TimeDelta interval) {
124     DCHECK(!interval.is_zero());
125     poll_interval_ = interval;
126   }
127 
128  private:
129   base::ObserverList<SyncEngineEventListener>::Unchecked listeners_;
130 
131   ServerConnectionManager* const connection_manager_;
132 
133   // We use this to stuff extensions activity into CommitMessages so the server
134   // can correlate commit traffic with extension-related bookmark mutations.
135   scoped_refptr<ExtensionsActivity> extensions_activity_;
136 
137   // Kept up to date with talk events to determine whether notifications are
138   // enabled. True only if the notification channel is authorized and open.
139   bool notifications_enabled_;
140 
141   const std::string cache_guid_;
142 
143   std::string birthday_;
144 
145   std::string bag_of_chips_;
146 
147   // The name of the account being synced.
148   std::string account_name_;
149 
150   // The server limits the number of items a client can commit in one batch.
151   int max_commit_batch_size_;
152 
153   // We use this to get debug info to send to the server for debugging
154   // client behavior on server side.
155   DebugInfoGetter* const debug_info_getter_;
156 
157   ModelTypeRegistry* model_type_registry_;
158 
159   // Satus information to be sent up to the server.
160   sync_pb::ClientStatus client_status_;
161 
162   // This is a copy of the identifier the that the invalidations client used to
163   // register itself with the invalidations server during startup.  We need to
164   // provide this to the sync server when we make changes to enable it to
165   // prevent us from receiving notifications of changes we make ourselves.
166   std::string invalidator_client_id_;
167 
168   // Whether the account(s) present in the content area's cookie jar match the
169   // chrome account. If multiple accounts are present in the cookie jar, a
170   // mismatch implies all of them are different from the chrome account.
171   bool cookie_jar_mismatch_;
172 
173   // If there's a cookie jar mismatch, whether the cookie jar was empty or not.
174   bool cookie_jar_empty_;
175 
176   // If there are no other known active devices.
177   bool single_client_;
178 
179   base::TimeDelta poll_interval_;
180 
181   DISALLOW_COPY_AND_ASSIGN(SyncCycleContext);
182 };
183 
184 }  // namespace syncer
185 
186 #endif  // COMPONENTS_SYNC_ENGINE_IMPL_CYCLE_SYNC_CYCLE_CONTEXT_H_
187