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