Home | History | Annotate | Download | only in engine
      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 SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_
      6 #define SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "sync/base/sync_export.h"
     10 #include "sync/engine/syncer_command.h"
     11 #include "sync/internal_api/public/engine/model_safe_worker.h"
     12 
     13 namespace syncer {
     14 namespace sessions {
     15 class SyncSession;
     16 }
     17 
     18 // An abstract SyncerCommand which dispatches its Execute step to the
     19 // model-safe worker thread.  Classes derived from ModelChangingSyncerCommand
     20 // instead of SyncerCommand must implement ModelChangingExecuteImpl instead of
     21 // ExecuteImpl, but otherwise, the contract is the same.
     22 //
     23 // A command should derive from ModelChangingSyncerCommand instead of
     24 // SyncerCommand whenever the operation might change any client-visible
     25 // fields on any syncable::Entry.  If the operation involves creating a
     26 // WriteTransaction, this is a sign that ModelChangingSyncerCommand is likely
     27 // necessary.
     28 class SYNC_EXPORT_PRIVATE ModelChangingSyncerCommand : public SyncerCommand {
     29  public:
     30   ModelChangingSyncerCommand() : work_session_(NULL) { }
     31   virtual ~ModelChangingSyncerCommand() { }
     32 
     33   // SyncerCommand implementation. Sets work_session to session.
     34   virtual SyncerError ExecuteImpl(sessions::SyncSession* session) OVERRIDE;
     35 
     36   // Wrapper so implementations don't worry about storing work_session.
     37   SyncerError StartChangingModel() {
     38     return ModelChangingExecuteImpl(work_session_);
     39   }
     40 
     41   std::set<ModelSafeGroup> GetGroupsToChangeForTest(
     42       const sessions::SyncSession& session) const {
     43     return GetGroupsToChange(session);
     44   }
     45 
     46  protected:
     47   // This should return the set of groups in |session| that need to be
     48   // changed.  The returned set should be a subset of
     49   // session.GetEnabledGroups().  Subclasses can guarantee this either
     50   // by calling one of the session.GetEnabledGroups*() functions and
     51   // filtering that, or using GetGroupForModelType() (which handles
     52   // top-level/unspecified nodes) to project from model types to
     53   // groups.
     54   virtual std::set<ModelSafeGroup> GetGroupsToChange(
     55       const sessions::SyncSession& session) const = 0;
     56 
     57   // Abstract method to be implemented by subclasses to handle logic that
     58   // operates on the model.  This is invoked with a SyncSession ModelSafeGroup
     59   // restriction in place so that bits of state belonging to data types
     60   // running on an unsafe thread are siloed away.
     61   virtual SyncerError ModelChangingExecuteImpl(
     62       sessions::SyncSession* session) = 0;
     63 
     64  private:
     65   // ExecuteImpl is expected to be run by SyncerCommand to set work_session.
     66   // StartChangingModel is called to start this command running.
     67   // Implementations will implement ModelChangingExecuteImpl and not
     68   // worry about storing the session or setting it. They are given work_session.
     69   sessions::SyncSession* work_session_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(ModelChangingSyncerCommand);
     72 };
     73 
     74 }  // namespace syncer
     75 
     76 #endif  // SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_
     77