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