Home | History | Annotate | Download | only in engine
      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_MODEL_TYPE_SYNC_PROXY_IMPL_H_
      6 #define SYNC_ENGINE_MODEL_TYPE_SYNC_PROXY_IMPL_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/internal_api/public/base/model_type.h"
     13 #include "sync/internal_api/public/non_blocking_sync_common.h"
     14 #include "sync/protocol/sync.pb.h"
     15 
     16 namespace syncer {
     17 
     18 class SyncContextProxy;
     19 class ModelTypeEntity;
     20 class ModelTypeSyncWorker;
     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 ModelTypeSyncProxyImpl : base::NonThreadSafe {
     25  public:
     26   ModelTypeSyncProxyImpl(ModelType type);
     27   virtual ~ModelTypeSyncProxyImpl();
     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 type sync proxy.
     45   ModelType GetModelType() const;
     46 
     47   // Starts the handshake with the sync thread.
     48   void Enable(scoped_ptr<SyncContextProxy> context_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(scoped_ptr<ModelTypeSyncWorker> worker);
     60 
     61   // Requests that an item be stored in sync.
     62   void Put(const std::string& client_tag,
     63            const sync_pb::EntitySpecifics& specifics);
     64 
     65   // Deletes an item from sync.
     66   void Delete(const std::string& client_tag);
     67 
     68   // Informs this object that some of its commit requests have been
     69   // successfully serviced.
     70   void OnCommitCompleted(const DataTypeState& type_state,
     71                          const CommitResponseDataList& response_list);
     72 
     73   // Informs this object that there are some incoming updates is should
     74   // handle.
     75   void OnUpdateReceived(const DataTypeState& type_state,
     76                         const UpdateResponseDataList& response_list,
     77                         const UpdateResponseDataList& pending_updates);
     78 
     79   // Returns the list of pending updates.
     80   //
     81   // This is used as a helper function, but it's public mainly for testing.
     82   // The current test harness setup doesn't allow us to test the data that the
     83   // proxy sends to the worker during initialization, so we use this to inspect
     84   // its state instead.
     85   UpdateResponseDataList GetPendingUpdates();
     86 
     87   // Returns the long-lived WeakPtr that is intended to be registered with the
     88   // ProfileSyncService.
     89   base::WeakPtr<ModelTypeSyncProxyImpl> AsWeakPtrForUI();
     90 
     91  private:
     92   typedef std::map<std::string, ModelTypeEntity*> EntityMap;
     93   typedef std::map<std::string, UpdateResponseData*> UpdateMap;
     94 
     95   // Sends all commit requests that are due to be sent to the sync thread.
     96   void FlushPendingCommitRequests();
     97 
     98   // Clears any state related to outstanding communications with the
     99   // ModelTypeSyncWorker.  Used when we want to disconnect from
    100   // the current worker.
    101   void ClearTransientSyncState();
    102 
    103   // Clears any state related to our communications with the current sync
    104   // account.  Useful when a user signs out of the current account.
    105   void ClearSyncState();
    106 
    107   ModelType type_;
    108   DataTypeState data_type_state_;
    109 
    110   // Whether or not sync is preferred for this type.  This is a cached copy of
    111   // the canonical copy information on the UI thread.
    112   bool is_preferred_;
    113 
    114   // Whether or not this object has completed its initial handshake with the
    115   // SyncContextProxy.
    116   bool is_connected_;
    117 
    118   // Our link to data type management on the sync thread.
    119   // Used for enabling and disabling sync for this type.
    120   //
    121   // Beware of NULL pointers: This object is uninitialized when we are not
    122   // connected to sync.
    123   scoped_ptr<SyncContextProxy> sync_context_proxy_;
    124 
    125   // Reference to the ModelTypeSyncWorker.
    126   //
    127   // The interface hides the posting of tasks across threads as well as the
    128   // ModelTypeSyncWorker's implementation.  Both of these features are
    129   // useful in tests.
    130   scoped_ptr<ModelTypeSyncWorker> worker_;
    131 
    132   // The set of sync entities known to this object.
    133   EntityMap entities_;
    134   STLValueDeleter<EntityMap> entities_deleter_;
    135 
    136   // A set of updates that can not be applied at this time.  These are never
    137   // used by the model.  They are kept here only so we can save and restore
    138   // them across restarts, and keep them in sync with our progress markers.
    139   UpdateMap pending_updates_map_;
    140   STLValueDeleter<UpdateMap> pending_updates_map_deleter_;
    141 
    142   // We use two different WeakPtrFactories because we want the pointers they
    143   // issue to have different lifetimes.  When asked to disconnect from the sync
    144   // thread, we want to make sure that no tasks generated as part of the
    145   // now-obsolete connection to affect us.  But we also want the WeakPtr we
    146   // sent to the UI thread to remain valid.
    147   base::WeakPtrFactory<ModelTypeSyncProxyImpl> weak_ptr_factory_for_ui_;
    148   base::WeakPtrFactory<ModelTypeSyncProxyImpl> weak_ptr_factory_for_sync_;
    149 };
    150 
    151 }  // namespace syncer
    152 
    153 #endif  // SYNC_ENGINE_MODEL_TYPE_SYNC_PROXY_IMPL_H_
    154