• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/stl_util.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "sync/base/sync_export.h"
12 #include "sync/engine/non_blocking_sync_common.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/protocol/sync.pb.h"
15 
16 namespace syncer {
17 
18 class SyncCoreProxy;
19 class ModelThreadSyncEntity;
20 class NonBlockingTypeProcessorCoreInterface;
21 
22 // A sync component embedded on the synced type's thread that helps to handle
23 // communication between sync and model type threads.
24 class SYNC_EXPORT_PRIVATE NonBlockingTypeProcessor : base::NonThreadSafe {
25  public:
26   NonBlockingTypeProcessor(ModelType type);
27   virtual ~NonBlockingTypeProcessor();
28 
29   // Returns true if this object believes that sync is preferred for this type.
30   //
31   // By "preferred", we mean that a policy decision has been made that this
32   // type should be synced.  Most of the time this is controlled by a user
33   // clicking a checkbox on the settings page.
34   //
35   // The canonical preferred state is based on SyncPrefs on the UI thread.  At
36   // best, this value is stale and may lag behind the one set on the UI thread.
37   // Before this type has registered with the UI thread, it's mostly just an
38   // informed guess.
39   bool IsPreferred() const;
40 
41   // Returns true if the handshake with sync thread is complete.
42   bool IsConnected() const;
43 
44   // Returns the model type handled by this processor.
45   ModelType GetModelType() const;
46 
47   // Starts the handshake with the sync thread.
48   void Enable(scoped_ptr<SyncCoreProxy> core_proxy);
49 
50   // Severs all ties to the sync thread and may delete local sync state.
51   // Another call to Enable() can be used to re-establish this connection.
52   void Disable();
53 
54   // Severs all ties to the sync thread.
55   // Another call to Enable() can be used to re-establish this connection.
56   void Disconnect();
57 
58   // Callback used to process the handshake response.
59   void OnConnect(
60       scoped_ptr<NonBlockingTypeProcessorCoreInterface> core_interface);
61 
62   // Requests that an item be stored in sync.
63   void Put(const std::string& client_tag,
64            const sync_pb::EntitySpecifics& specifics);
65 
66   // Deletes an item from sync.
67   void Delete(const std::string& client_tag);
68 
69   // Informs this object that some of its commit requests have been
70   // successfully serviced.
71   void OnCommitCompletion(const DataTypeState& type_state,
72                           const CommitResponseDataList& response_list);
73 
74   // Informs this object that there are some incoming updates is should
75   // handle.
76   void OnUpdateReceived(const DataTypeState& type_state,
77                         const UpdateResponseDataList& response_list);
78 
79   // Returns the long-lived WeakPtr that is intended to be registered with the
80   // ProfileSyncService.
81   base::WeakPtr<NonBlockingTypeProcessor> AsWeakPtrForUI();
82 
83  private:
84   typedef std::map<std::string, ModelThreadSyncEntity*> EntityMap;
85 
86   // Sends all commit requests that are due to be sent to the sync thread.
87   void FlushPendingCommitRequests();
88 
89   ModelType type_;
90   DataTypeState data_type_state_;
91 
92   // Whether or not sync is preferred for this type.  This is a cached copy of
93   // the canonical copy information on the UI thread.
94   bool is_preferred_;
95 
96   // Whether or not this object has completed its initial handshake with the
97   // SyncCoreProxy.
98   bool is_connected_;
99 
100   // Our link to data type management on the sync thread.
101   // Used for enabling and disabling sync for this type.
102   //
103   // Beware of NULL pointers: This object is uninitialized when we are not
104   // connected to sync.
105   scoped_ptr<SyncCoreProxy> sync_core_proxy_;
106 
107   // Reference to the NonBlockingTypeProcessorCore.
108   //
109   // The interface hides the posting of tasks across threads as well as the
110   // NonBlockingTypeProcessorCore's implementation.  Both of these features are
111   // useful in tests.
112   scoped_ptr<NonBlockingTypeProcessorCoreInterface> core_interface_;
113 
114   // The set of sync entities known to this object.
115   EntityMap entities_;
116   STLValueDeleter<EntityMap> entities_deleter_;
117 
118   // We use two different WeakPtrFactories because we want the pointers they
119   // issue to have different lifetimes.  When asked to disconnect from the sync
120   // thread, we want to make sure that no tasks generated as part of the
121   // now-obsolete connection to affect us.  But we also want the WeakPtr we
122   // sent to the UI thread to remain valid.
123   base::WeakPtrFactory<NonBlockingTypeProcessor> weak_ptr_factory_for_ui_;
124   base::WeakPtrFactory<NonBlockingTypeProcessor> weak_ptr_factory_for_sync_;
125 };
126 
127 }  // namespace syncer
128 
129 #endif  // SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_H_
130