Home | History | Annotate | Download | only in sessions
      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 "sync/base/sync_export.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 
     63   ServerConnectionManager* connection_manager() {
     64     return connection_manager_;
     65   }
     66   syncable::Directory* directory() {
     67     return directory_;
     68   }
     69 
     70   const ModelSafeRoutingInfo& routing_info() const {
     71     return routing_info_;
     72   }
     73 
     74   void set_routing_info(const ModelSafeRoutingInfo& routing_info) {
     75     routing_info_ = routing_info;
     76   }
     77 
     78   const std::vector<scoped_refptr<ModelSafeWorker> >& workers() const {
     79     return workers_;
     80   }
     81 
     82   ExtensionsActivity* extensions_activity() {
     83     return extensions_activity_.get();
     84   }
     85 
     86   DebugInfoGetter* debug_info_getter() {
     87     return debug_info_getter_;
     88   }
     89 
     90   // Talk notification status.
     91   void set_notifications_enabled(bool enabled) {
     92     notifications_enabled_ = enabled;
     93   }
     94   bool notifications_enabled() { return notifications_enabled_; }
     95 
     96   // Account name, set once a directory has been opened.
     97   void set_account_name(const std::string& name) {
     98     DCHECK(account_name_.empty());
     99     account_name_ = name;
    100   }
    101   const std::string& account_name() const { return account_name_; }
    102 
    103   void set_max_commit_batch_size(int batch_size) {
    104     max_commit_batch_size_ = batch_size;
    105   }
    106   int32 max_commit_batch_size() const { return max_commit_batch_size_; }
    107 
    108   void NotifyListeners(const SyncEngineEvent& event) {
    109     FOR_EACH_OBSERVER(SyncEngineEventListener, listeners_,
    110                       OnSyncEngineEvent(event));
    111   }
    112 
    113   TrafficRecorder* traffic_recorder() {
    114     return traffic_recorder_;
    115   }
    116 
    117   bool keystore_encryption_enabled() const {
    118     return keystore_encryption_enabled_;
    119   }
    120 
    121   void set_hierarchy_conflict_detected(bool value) {
    122     client_status_.set_hierarchy_conflict_detected(value);
    123   }
    124 
    125   const sync_pb::ClientStatus& client_status() const {
    126     return client_status_;
    127   }
    128 
    129   const std::string& invalidator_client_id() const {
    130     return invalidator_client_id_;
    131   }
    132 
    133   bool ShouldFetchUpdatesBeforeCommit() const {
    134     return !(server_enabled_pre_commit_update_avoidance_ ||
    135              client_enabled_pre_commit_update_avoidance_);
    136   }
    137 
    138   void set_server_enabled_pre_commit_update_avoidance(bool value) {
    139     server_enabled_pre_commit_update_avoidance_ = value;
    140   }
    141 
    142  private:
    143   // Rather than force clients to set and null-out various context members, we
    144   // extend our encapsulation boundary to scoped helpers that take care of this
    145   // once they are allocated. See definitions of these below.
    146   friend class TestScopedSessionEventListener;
    147 
    148   ObserverList<SyncEngineEventListener> listeners_;
    149 
    150   ServerConnectionManager* const connection_manager_;
    151   syncable::Directory* const directory_;
    152 
    153   // A cached copy of SyncBackendRegistrar's routing info.
    154   // Must be updated manually when SBR's state is modified.
    155   ModelSafeRoutingInfo routing_info_;
    156 
    157   // The set of ModelSafeWorkers.  Used to execute tasks of various threads.
    158   std::vector<scoped_refptr<ModelSafeWorker> > workers_;
    159 
    160   // We use this to stuff extensions activity into CommitMessages so the server
    161   // can correlate commit traffic with extension-related bookmark mutations.
    162   scoped_refptr<ExtensionsActivity> extensions_activity_;
    163 
    164   // Kept up to date with talk events to determine whether notifications are
    165   // enabled. True only if the notification channel is authorized and open.
    166   bool notifications_enabled_;
    167 
    168   // The name of the account being synced.
    169   std::string account_name_;
    170 
    171   // The server limits the number of items a client can commit in one batch.
    172   int max_commit_batch_size_;
    173 
    174   // We use this to get debug info to send to the server for debugging
    175   // client behavior on server side.
    176   DebugInfoGetter* const debug_info_getter_;
    177 
    178   TrafficRecorder* traffic_recorder_;
    179 
    180   // Satus information to be sent up to the server.
    181   sync_pb::ClientStatus client_status_;
    182 
    183   // Temporary variable while keystore encryption is behind a flag. True if
    184   // we should attempt performing keystore encryption related work, false if
    185   // the experiment is not enabled.
    186   bool keystore_encryption_enabled_;
    187 
    188   // This is a copy of the identifier the that the invalidations client used to
    189   // register itself with the invalidations server during startup.  We need to
    190   // provide this to the sync server when we make changes to enable it to
    191   // prevent us from receiving notifications of changes we make ourselves.
    192   const std::string invalidator_client_id_;
    193 
    194   // Flag to enable or disable the no pre-commit GetUpdates experiment.  When
    195   // this flag is set to false, the syncer has the option of not performing at
    196   // GetUpdates request when there is nothing to fetch.
    197   bool server_enabled_pre_commit_update_avoidance_;
    198 
    199   // If true, indicates that we've been passed a command-line flag to force
    200   // enable the pre-commit update avoidance experiment described above.
    201   const bool client_enabled_pre_commit_update_avoidance_;
    202 
    203   DISALLOW_COPY_AND_ASSIGN(SyncSessionContext);
    204 };
    205 
    206 }  // namespace sessions
    207 }  // namespace syncer
    208 
    209 #endif  // SYNC_SESSIONS_SYNC_SESSION_CONTEXT_H_
    210