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