1 // Copyright (c) 2006-2009 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 CHROME_BROWSER_SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_ 6 #define CHROME_BROWSER_SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_ 7 #pragma once 8 9 #include "chrome/browser/sync/engine/syncer_command.h" 10 11 namespace browser_sync { 12 namespace sessions { 13 class SyncSession; 14 } 15 16 // An abstract SyncerCommand which dispatches its Execute step to the 17 // model-safe worker thread. Classes derived from ModelChangingSyncerCommand 18 // instead of SyncerCommand must implement ModelChangingExecuteImpl instead of 19 // ExecuteImpl, but otherwise, the contract is the same. 20 // 21 // A command should derive from ModelChangingSyncerCommand instead of 22 // SyncerCommand whenever the operation might change any client-visible 23 // fields on any syncable::Entry. If the operation involves creating a 24 // WriteTransaction, this is a sign that ModelChangingSyncerCommand is likely 25 // necessary. 26 class ModelChangingSyncerCommand : public SyncerCommand { 27 public: 28 ModelChangingSyncerCommand() : work_session_(NULL) { } 29 virtual ~ModelChangingSyncerCommand() { } 30 31 // SyncerCommand implementation. Sets work_session to session. 32 virtual void ExecuteImpl(sessions::SyncSession* session); 33 34 // wrapper so implementations don't worry about storing work_session 35 void StartChangingModel() { 36 ModelChangingExecuteImpl(work_session_); 37 } 38 39 // Sometimes, a command has work to do that needs to touch global state 40 // belonging to multiple ModelSafeGroups, but in a way that is known to be 41 // safe. This will be called once, prior to ModelChangingExecuteImpl, 42 // *without* a ModelSafeGroup restriction in place on the SyncSession. 43 // Returns true on success, false on failure. 44 // TODO(tim): Remove this (bug 36594). 45 virtual bool ModelNeutralExecuteImpl(sessions::SyncSession* session); 46 47 // Abstract method to be implemented by subclasses to handle logic that 48 // operates on the model. This is invoked with a SyncSession ModelSafeGroup 49 // restriction in place so that bits of state belonging to data types 50 // running on an unsafe thread are siloed away. 51 virtual void ModelChangingExecuteImpl(sessions::SyncSession* session) = 0; 52 53 private: 54 // ExecuteImpl is expected to be run by SyncerCommand to set work_session. 55 // StartChangingModel is called to start this command running. 56 // Implementations will implement ModelChangingExecuteImpl and not 57 // worry about storing the session or setting it. They are given work_session. 58 sessions::SyncSession* work_session_; 59 60 DISALLOW_COPY_AND_ASSIGN(ModelChangingSyncerCommand); 61 }; 62 63 } // namespace browser_sync 64 65 #endif // CHROME_BROWSER_SYNC_ENGINE_MODEL_CHANGING_SYNCER_COMMAND_H_ 66