1 // Copyright 2014 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 COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_ 6 #define COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_ 7 8 #include "components/sync_driver/data_type_error_handler.h" 9 #include "sync/internal_api/public/base_transaction.h" 10 #include "sync/internal_api/public/change_record.h" 11 #include "sync/internal_api/public/user_share.h" 12 13 namespace syncer { 14 class UnrecoverableErrorHandler; 15 } // namespace syncer 16 17 namespace sync_driver { 18 19 class ModelAssociator; 20 21 // An interface used to apply changes from the sync model to the browser's 22 // native model. This does not currently distinguish between model data types. 23 class ChangeProcessor { 24 public: 25 explicit ChangeProcessor(DataTypeErrorHandler* error_handler); 26 virtual ~ChangeProcessor(); 27 28 // Call when the processor should accept changes from either provided model 29 // and apply them to the other. Both the native model and sync_api are 30 // expected to be initialized and loaded. You must have set a valid 31 // ModelAssociator and UnrecoverableErrorHandler before using this method, and 32 // the two models should be associated w.r.t the ModelAssociator provided. 33 void Start(syncer::UserShare* share_handle); 34 35 // Changes have been applied to the backend model and are ready to be 36 // applied to the frontend model. 37 virtual void ApplyChangesFromSyncModel( 38 const syncer::BaseTransaction* trans, 39 int64 model_version, 40 const syncer::ImmutableChangeRecordList& changes) = 0; 41 42 // The changes found in ApplyChangesFromSyncModel may be too slow to be 43 // performed while holding a [Read/Write]Transaction lock or may interact 44 // with another thread, which might itself be waiting on the transaction lock, 45 // putting us at risk of deadlock. 46 // This function is called once the transactional lock is released and it is 47 // safe to perform inter-thread or slow I/O operations. Note that not all 48 // datatypes need this, so we provide an empty default version. 49 virtual void CommitChangesFromSyncModel(); 50 51 // This ensures that startobserving gets called after stopobserving even 52 // if there is an early return in the function. 53 template <class T> 54 class ScopedStopObserving { 55 public: ScopedStopObserving(T * processor)56 explicit ScopedStopObserving(T* processor) 57 : processor_(processor) { 58 processor_->StopObserving(); 59 } ~ScopedStopObserving()60 ~ScopedStopObserving() { 61 processor_->StartObserving(); 62 } 63 64 private: ScopedStopObserving()65 ScopedStopObserving() {} 66 T* processor_; 67 }; 68 69 protected: 70 // These methods are invoked by Start() and Stop() to do 71 // implementation-specific work. 72 virtual void StartImpl() = 0; 73 74 DataTypeErrorHandler* error_handler() const; 75 virtual syncer::UserShare* share_handle() const; 76 77 private: 78 DataTypeErrorHandler* error_handler_; // Guaranteed to outlive us. 79 80 // The sync model we are processing changes from. 81 syncer::UserShare* share_handle_; 82 83 DISALLOW_COPY_AND_ASSIGN(ChangeProcessor); 84 }; 85 86 } // namespace sync_driver 87 88 #endif // COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_ 89