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.
5 #ifndef COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_
6 #define COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_
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"
14 class UnrecoverableErrorHandler
;
17 namespace sync_driver
{
19 class ModelAssociator
;
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
{
25 explicit ChangeProcessor(DataTypeErrorHandler
* error_handler
);
26 virtual ~ChangeProcessor();
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
);
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
,
40 const syncer::ImmutableChangeRecordList
& changes
) = 0;
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();
51 // This ensures that startobserving gets called after stopobserving even
52 // if there is an early return in the function.
54 class ScopedStopObserving
{
56 explicit ScopedStopObserving(T
* processor
)
57 : processor_(processor
) {
58 processor_
->StopObserving();
60 ~ScopedStopObserving() {
61 processor_
->StartObserving();
65 ScopedStopObserving() {}
70 // These methods are invoked by Start() and Stop() to do
71 // implementation-specific work.
72 virtual void StartImpl() = 0;
74 DataTypeErrorHandler
* error_handler() const;
75 virtual syncer::UserShare
* share_handle() const;
78 DataTypeErrorHandler
* error_handler_
; // Guaranteed to outlive us.
80 // The sync model we are processing changes from.
81 syncer::UserShare
* share_handle_
;
83 DISALLOW_COPY_AND_ASSIGN(ChangeProcessor
);
86 } // namespace sync_driver
88 #endif // COMPONENTS_SYNC_DRIVER_CHANGE_PROCESSOR_H_