Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / public / non_blocking_type_processor.h
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 SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_H_
6 #define SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_H_
7
8 #include "base/memory/weak_ptr.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "sync/base/sync_export.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/protocol/sync.pb.h"
14
15 namespace syncer {
16
17 class SyncCoreProxy;
18 class NonBlockingTypeProcessorCore;
19
20 // A sync component embedded on the synced type's thread that helps to handle
21 // communication between sync and model type threads.
22 class SYNC_EXPORT_PRIVATE NonBlockingTypeProcessor : base::NonThreadSafe {
23  public:
24   NonBlockingTypeProcessor(ModelType type);
25   virtual ~NonBlockingTypeProcessor();
26
27   // Returns true if this object believes that sync is preferred for this type.
28   //
29   // By "preferred", we mean that a policy decision has been made that this
30   // type should be synced.  Most of the time this is controlled by a user
31   // clicking a checkbox on the settings page.
32   //
33   // The canonical preferred state is based on SyncPrefs on the UI thread.  At
34   // best, this value is stale and may lag behind the one set on the UI thread.
35   // Before this type has registered with the UI thread, it's mostly just an
36   // informed guess.
37   bool IsPreferred() const;
38
39   // Returns true if the handshake with sync thread is complete.
40   bool IsConnected() const;
41
42   // Returns the model type handled by this processor.
43   ModelType GetModelType() const;
44
45   // Starts the handshake with the sync thread.
46   void Enable(SyncCoreProxy* core_proxy);
47
48   // Severs all ties to the sync thread and may delete local sync state.
49   // Another call to Enable() can be used to re-establish this connection.
50   void Disable();
51
52   // Severs all ties to the sync thread.
53   // Another call to Enable() can be used to re-establish this connection.
54   void Disconnect();
55
56   // Callback used to process the handshake response.
57   void OnConnect(base::WeakPtr<NonBlockingTypeProcessorCore> core,
58                  scoped_refptr<base::SequencedTaskRunner> sync_thread);
59
60   base::WeakPtr<NonBlockingTypeProcessor> AsWeakPtr();
61
62  private:
63   ModelType type_;
64   sync_pb::DataTypeProgressMarker progress_marker_;
65
66   // Whether or not sync is preferred for this type.  This is a cached copy of
67   // the canonical copy information on the UI thread.
68   bool is_preferred_;
69
70   // Whether or not this object has completed its initial handshake with the
71   // SyncCoreProxy.
72   bool is_connected_;
73
74   base::WeakPtr<NonBlockingTypeProcessorCore> core_;
75   scoped_refptr<base::SequencedTaskRunner> sync_thread_;
76
77   base::WeakPtrFactory<NonBlockingTypeProcessor> weak_ptr_factory_;
78 };
79
80 }  // namespace syncer
81
82 #endif  // SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_H_