| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 // A class representing an attempt to synchronize the local syncable data |    5 // A class representing an attempt to synchronize the local syncable data | 
|    6 // store with a sync server. A SyncSession instance is passed as a stateful |    6 // store with a sync server. A SyncSession instance is passed as a stateful | 
|    7 // bundle to and from various SyncerCommands with the goal of converging the |    7 // bundle to and from various SyncerCommands with the goal of converging the | 
|    8 // client view of data with that of the server. The commands twiddle with |    8 // client view of data with that of the server. The commands twiddle with | 
|    9 // session status in response to events and hiccups along the way, set and |    9 // session status in response to events and hiccups along the way, set and | 
|   10 // query session progress with regards to conflict resolution and applying |   10 // query session progress with regards to conflict resolution and applying | 
|   11 // server updates, and access the SyncSessionContext for the current session |   11 // server updates, and access the SyncSessionContext for the current session | 
|   12 // via SyncSession instances. |   12 // via SyncSession instances. | 
|   13  |   13  | 
|   14 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_ |   14 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_ | 
|   15 #define SYNC_SESSIONS_SYNC_SESSION_H_ |   15 #define SYNC_SESSIONS_SYNC_SESSION_H_ | 
|   16  |   16  | 
|   17 #include <map> |   17 #include <map> | 
|   18 #include <set> |   18 #include <set> | 
|   19 #include <string> |   19 #include <string> | 
|   20 #include <utility> |   20 #include <utility> | 
|   21 #include <vector> |   21 #include <vector> | 
|   22  |   22  | 
|   23 #include "base/basictypes.h" |   23 #include "base/basictypes.h" | 
|   24 #include "base/memory/scoped_ptr.h" |   24 #include "base/memory/scoped_ptr.h" | 
|   25 #include "base/time.h" |   25 #include "base/time.h" | 
|   26 #include "sync/internal_api/public/base/model_type.h" |   26 #include "sync/internal_api/public/base/model_type.h" | 
|   27 #include "sync/internal_api/public/engine/model_safe_worker.h" |   27 #include "sync/internal_api/public/engine/model_safe_worker.h" | 
|   28 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |   28 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | 
|   29 #include "sync/sessions/ordered_commit_set.h" |   29 #include "sync/sessions/ordered_commit_set.h" | 
|   30 #include "sync/sessions/session_state.h" |  | 
|   31 #include "sync/sessions/status_controller.h" |   30 #include "sync/sessions/status_controller.h" | 
|   32 #include "sync/sessions/sync_session_context.h" |   31 #include "sync/sessions/sync_session_context.h" | 
|   33 #include "sync/util/extensions_activity_monitor.h" |   32 #include "sync/util/extensions_activity_monitor.h" | 
|   34  |   33  | 
|   35 namespace syncer { |   34 namespace syncer { | 
|   36 class ModelSafeWorker; |   35 class ModelSafeWorker; | 
|   37  |   36  | 
|   38 namespace syncable { |   37 namespace syncable { | 
|   39 class WriteTransaction; |   38 class WriteTransaction; | 
|   40 } |   39 } | 
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  101               const ModelSafeRoutingInfo& routing_info, |  100               const ModelSafeRoutingInfo& routing_info, | 
|  102               const std::vector<ModelSafeWorker*>& workers); |  101               const std::vector<ModelSafeWorker*>& workers); | 
|  103   ~SyncSession(); |  102   ~SyncSession(); | 
|  104  |  103  | 
|  105   // Builds a thread-safe and read-only copy of the current session state. |  104   // Builds a thread-safe and read-only copy of the current session state. | 
|  106   SyncSessionSnapshot TakeSnapshot() const; |  105   SyncSessionSnapshot TakeSnapshot() const; | 
|  107  |  106  | 
|  108   // Builds and sends a snapshot to the session context's listeners. |  107   // Builds and sends a snapshot to the session context's listeners. | 
|  109   void SendEventNotification(SyncEngineEvent::EventCause cause); |  108   void SendEventNotification(SyncEngineEvent::EventCause cause); | 
|  110  |  109  | 
|  111   // Returns true if this session contains data that should go through the sync |  | 
|  112   // engine again. |  | 
|  113   bool HasMoreToSync() const; |  | 
|  114  |  | 
|  115   // Returns true if we completely ran the session without errors. |  110   // Returns true if we completely ran the session without errors. | 
|  116   // |  111   // | 
|  117   // There are many errors that could prevent a sync cycle from succeeding. |  112   // There are many errors that could prevent a sync cycle from succeeding. | 
|  118   // These include invalid local state, inability to contact the server, |  113   // These include invalid local state, inability to contact the server, | 
|  119   // inability to authenticate with the server, and server errors.  What they |  114   // inability to authenticate with the server, and server errors.  What they | 
|  120   // have in common is that the we either need to take some action and then |  115   // have in common is that the we either need to take some action and then | 
|  121   // retry the sync cycle or, in the case of transient errors, retry after some |  116   // retry the sync cycle or, in the case of transient errors, retry after some | 
|  122   // backoff timer has expired.  Most importantly, the SyncScheduler should not |  117   // backoff timer has expired.  Most importantly, the SyncScheduler should not | 
|  123   // assume that the original action that triggered the sync cycle (ie. a nudge |  118   // assume that the original action that triggered the sync cycle (ie. a nudge | 
|  124   // or a notification) has been properly serviced. |  119   // or a notification) has been properly serviced. | 
|  125   // |  120   // | 
|  126   // This function also returns false if SyncShare has not been called on this |  121   // This function also returns false if SyncShare has not been called on this | 
|  127   // session yet, or if ResetTransientState() has been called on this session |  122   // session yet. | 
|  128   // since the last call to SyncShare. |  | 
|  129   bool Succeeded() const; |  123   bool Succeeded() const; | 
|  130  |  124  | 
|  131   // Returns true if we reached the server successfully and the server did not |  125   // Returns true if we reached the server successfully and the server did not | 
|  132   // return any error codes. Returns false if no connection was attempted. |  126   // return any error codes. Returns false if no connection was attempted. | 
|  133   bool SuccessfullyReachedServer() const; |  127   bool SuccessfullyReachedServer() const; | 
|  134  |  128  | 
|  135   // Collects all state pertaining to how and why |s| originated and unions it |  129   // Collects all state pertaining to how and why |s| originated and unions it | 
|  136   // with corresponding state in |this|, leaving |s| unchanged.  Allows |this| |  130   // with corresponding state in |this|, leaving |s| unchanged.  Allows |this| | 
|  137   // to take on the responsibilities |s| had (e.g. certain data types) in the |  131   // to take on the responsibilities |s| had (e.g. certain data types) in the | 
|  138   // next SyncShare operation using |this|, rather than needed two separate |  132   // next SyncShare operation using |this|, rather than needed two separate | 
|  139   // sessions. |  133   // sessions. | 
|  140   void Coalesce(const SyncSession& session); |  134   void Coalesce(const SyncSession& session); | 
|  141  |  135  | 
|  142   // Compares the routing_info_, workers and payload map with the passed in |  136   // Compares the routing_info_, workers and payload map with the passed in | 
|  143   // session. Purges types from the above 3 which are not in session. Useful |  137   // session. Purges types from the above 3 which are not in session. Useful | 
|  144   // to update the sync session when the user has disabled some types from |  138   // to update the sync session when the user has disabled some types from | 
|  145   // syncing. |  139   // syncing. | 
|  146   void RebaseRoutingInfoWithLatest(const SyncSession& session); |  140   void RebaseRoutingInfoWithLatest(const SyncSession& session); | 
|  147  |  141  | 
|  148   // Should be called any time |this| is being re-used in a new call to |  | 
|  149   // SyncShare (e.g., HasMoreToSync returned true). |  | 
|  150   void PrepareForAnotherSyncCycle(); |  | 
|  151  |  | 
|  152   // TODO(akalin): Split this into context() and mutable_context(). |  142   // TODO(akalin): Split this into context() and mutable_context(). | 
|  153   SyncSessionContext* context() const { return context_; } |  143   SyncSessionContext* context() const { return context_; } | 
|  154   Delegate* delegate() const { return delegate_; } |  144   Delegate* delegate() const { return delegate_; } | 
|  155   syncable::WriteTransaction* write_transaction() { return write_transaction_; } |  145   syncable::WriteTransaction* write_transaction() { return write_transaction_; } | 
|  156   const StatusController& status_controller() const { |  146   const StatusController& status_controller() const { | 
|  157     return *status_controller_.get(); |  147     return *status_controller_.get(); | 
|  158   } |  148   } | 
|  159   StatusController* mutable_status_controller() { |  149   StatusController* mutable_status_controller() { | 
|  160     return status_controller_.get(); |  150     return status_controller_.get(); | 
|  161   } |  151   } | 
|  162  |  152  | 
|  163   const ExtensionsActivityMonitor::Records& extensions_activity() const { |  153   const ExtensionsActivityMonitor::Records& extensions_activity() const { | 
|  164     return extensions_activity_; |  154     return extensions_activity_; | 
|  165   } |  155   } | 
|  166   ExtensionsActivityMonitor::Records* mutable_extensions_activity() { |  156   ExtensionsActivityMonitor::Records* mutable_extensions_activity() { | 
|  167     return &extensions_activity_; |  157     return &extensions_activity_; | 
|  168   } |  158   } | 
|  169  |  159  | 
|  170   const std::vector<ModelSafeWorker*>& workers() const { return workers_; } |  160   const std::vector<ModelSafeWorker*>& workers() const { return workers_; } | 
|  171   const ModelSafeRoutingInfo& routing_info() const { return routing_info_; } |  161   const ModelSafeRoutingInfo& routing_info() const { return routing_info_; } | 
|  172   const SyncSourceInfo& source() const { return source_; } |  162   const SyncSourceInfo& source() const { return source_; } | 
|  173  |  163  | 
|  174   // Returns the set of groups which have enabled types. |  164   // Returns the set of groups which have enabled types. | 
|  175   const std::set<ModelSafeGroup>& GetEnabledGroups() const; |  165   const std::set<ModelSafeGroup>& GetEnabledGroups() const; | 
|  176  |  166  | 
|  177   // Returns the set of enabled groups that have conflicts. |  | 
|  178   std::set<ModelSafeGroup> GetEnabledGroupsWithConflicts() const; |  | 
|  179  |  | 
|  180   // Returns the set of enabled groups that have verified updates. |  | 
|  181   std::set<ModelSafeGroup> GetEnabledGroupsWithVerifiedUpdates() const; |  | 
|  182  |  | 
|  183   // Mark the session has having finished all the sync steps it needed. |  167   // Mark the session has having finished all the sync steps it needed. | 
|  184   void SetFinished(); |  168   void SetFinished(); | 
|  185  |  169  | 
|  186  private: |  170  private: | 
|  187   // Extend the encapsulation boundary to utilities for internal member |  171   // Extend the encapsulation boundary to utilities for internal member | 
|  188   // assignments. This way, the scope of these actions is explicit, they can't |  172   // assignments. This way, the scope of these actions is explicit, they can't | 
|  189   // be overridden, and assigning is always accompanied by unassigning. |  173   // be overridden, and assigning is always accompanied by unassigning. | 
|  190   friend class ScopedSetSessionWriteTransaction; |  174   friend class ScopedSetSessionWriteTransaction; | 
|  191  |  175  | 
|  192   // The context for this session, guaranteed to outlive |this|. |  176   // The context for this session, guaranteed to outlive |this|. | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
|  213  |  197  | 
|  214   // The routing info for the duration of this session, dictating which |  198   // The routing info for the duration of this session, dictating which | 
|  215   // datatypes should be synced and which workers should be used when working |  199   // datatypes should be synced and which workers should be used when working | 
|  216   // on those datatypes. |  200   // on those datatypes. | 
|  217   ModelSafeRoutingInfo routing_info_; |  201   ModelSafeRoutingInfo routing_info_; | 
|  218  |  202  | 
|  219   // The set of groups with enabled types.  Computed from |  203   // The set of groups with enabled types.  Computed from | 
|  220   // |routing_info_|. |  204   // |routing_info_|. | 
|  221   std::set<ModelSafeGroup> enabled_groups_; |  205   std::set<ModelSafeGroup> enabled_groups_; | 
|  222  |  206  | 
|  223   // Whether this session has reached its last step or not. Gets reset on each |  207   // Whether this session has reached its last step or not. | 
|  224   // new cycle (via PrepareForAnotherSyncCycle). |  | 
|  225   bool finished_; |  208   bool finished_; | 
|  226  |  209  | 
|  227   DISALLOW_COPY_AND_ASSIGN(SyncSession); |  210   DISALLOW_COPY_AND_ASSIGN(SyncSession); | 
|  228 }; |  211 }; | 
|  229  |  212  | 
|  230 // Installs a WriteTransaction to a given session and later clears it when the |  213 // Installs a WriteTransaction to a given session and later clears it when the | 
|  231 // utility falls out of scope. Transactions are not nestable, so it is an error |  214 // utility falls out of scope. Transactions are not nestable, so it is an error | 
|  232 // to try and use one of these if the session already has a transaction. |  215 // to try and use one of these if the session already has a transaction. | 
|  233 class ScopedSetSessionWriteTransaction { |  216 class ScopedSetSessionWriteTransaction { | 
|  234  public: |  217  public: | 
|  235   ScopedSetSessionWriteTransaction(SyncSession* session, |  218   ScopedSetSessionWriteTransaction(SyncSession* session, | 
|  236                                    syncable::WriteTransaction* trans) |  219                                    syncable::WriteTransaction* trans) | 
|  237       : session_(session) { |  220       : session_(session) { | 
|  238     DCHECK(!session_->write_transaction_); |  221     DCHECK(!session_->write_transaction_); | 
|  239     session_->write_transaction_ = trans; |  222     session_->write_transaction_ = trans; | 
|  240   } |  223   } | 
|  241   ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; } |  224   ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; } | 
|  242  |  225  | 
|  243  private: |  226  private: | 
|  244   SyncSession* session_; |  227   SyncSession* session_; | 
|  245   DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction); |  228   DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction); | 
|  246 }; |  229 }; | 
|  247  |  230  | 
|  248 }  // namespace sessions |  231 }  // namespace sessions | 
|  249 }  // namespace syncer |  232 }  // namespace syncer | 
|  250  |  233  | 
|  251 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_ |  234 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_ | 
| OLD | NEW |