Home | History | Annotate | Download | only in glue
      1 // Copyright (c) 2011 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_MODEL_ASSOCIATOR_H_
      6 #define CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATOR_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 #include "chrome/browser/sync/syncable/model_type.h"
     11 
     12 namespace sync_api {
     13 class BaseNode;
     14 }
     15 
     16 namespace browser_sync {
     17 
     18 // This represents the fundamental operations used for model association that
     19 // are common to all ModelAssociators and do not depend on types of the models
     20 // being associated.
     21 class AssociatorInterface {
     22  public:
     23   virtual ~AssociatorInterface() {}
     24 
     25   // Iterates through both the sync and the chrome model looking for
     26   // matched pairs of items. After successful completion, the models
     27   // should be identical and corresponding. Returns true on
     28   // success. On failure of this step, we should abort the sync
     29   // operation and report an error to the user.
     30   virtual bool AssociateModels() = 0;
     31 
     32   // Clears all the associations between the chrome and sync models.
     33   virtual bool DisassociateModels() = 0;
     34 
     35   // The has_nodes out parameter is set to true if the sync model has
     36   // nodes other than the permanent tagged nodes.  The method may
     37   // return false if an error occurred.
     38   virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes) = 0;
     39 
     40   // Calling this method while AssociateModels() is in progress will
     41   // cause the method to exit early with a "false" return value.  This
     42   // is useful for aborting model associations for shutdown.  This
     43   // method is only implemented for model associators that are invoked
     44   // off the main thread.
     45   virtual void AbortAssociation() = 0;
     46 
     47   // Returns whether the datatype is ready for encryption/decryption if the
     48   // sync service requires it.
     49   // TODO(zea): This should be implemented automatically for each datatype, see
     50   // http://crbug.com/76232.
     51   virtual bool CryptoReadyIfNecessary() = 0;
     52 };
     53 
     54 // In addition to the generic methods, association can refer to operations
     55 // that depend on the types of the actual IDs we are associating and the
     56 // underlying node type in the browser.  We collect these into a templatized
     57 // interface that encapsulates everything you need to implement to have a model
     58 // associator for a specific data type.
     59 // This template is appropriate for data types where a Node* makes sense for
     60 // referring to a particular item.  If we encounter a type that does not fit
     61 // in this world, we may want to have several PerDataType templates.
     62 template <class Node, class IDType>
     63 class PerDataTypeAssociatorInterface : public AssociatorInterface {
     64  public:
     65   virtual ~PerDataTypeAssociatorInterface() {}
     66   // Returns sync id for the given chrome model id.
     67   // Returns sync_api::kInvalidId if the sync node is not found for the given
     68   // chrome id.
     69   virtual int64 GetSyncIdFromChromeId(const IDType& id) = 0;
     70 
     71   // Returns the chrome node for the given sync id.
     72   // Returns NULL if no node is found for the given sync id.
     73   virtual const Node* GetChromeNodeFromSyncId(int64 sync_id) = 0;
     74 
     75   // Initializes the given sync node from the given chrome node id.
     76   // Returns false if no sync node was found for the given chrome node id or
     77   // if the initialization of sync node fails.
     78   virtual bool InitSyncNodeFromChromeId(const IDType& node_id,
     79                                         sync_api::BaseNode* sync_node) = 0;
     80 
     81   // Associates the given chrome node with the given sync id.
     82   virtual void Associate(const Node* node, int64 sync_id) = 0;
     83 
     84   // Remove the association that corresponds to the given sync id.
     85   virtual void Disassociate(int64 sync_id) = 0;
     86 };
     87 
     88 }  // namespace browser_sync
     89 
     90 #endif  // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATOR_H_
     91