model_associator.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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
12namespace sync_api {
13class BaseNode;
14}
15
16namespace 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.
21class 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
48// In addition to the generic methods, association can refer to operations
49// that depend on the types of the actual IDs we are associating and the
50// underlying node type in the browser.  We collect these into a templatized
51// interface that encapsulates everything you need to implement to have a model
52// associator for a specific data type.
53// This template is appropriate for data types where a Node* makes sense for
54// referring to a particular item.  If we encounter a type that does not fit
55// in this world, we may want to have several PerDataType templates.
56template <class Node, class IDType>
57class PerDataTypeAssociatorInterface : public AssociatorInterface {
58 public:
59  virtual ~PerDataTypeAssociatorInterface() {}
60  // Returns sync id for the given chrome model id.
61  // Returns sync_api::kInvalidId if the sync node is not found for the given
62  // chrome id.
63  virtual int64 GetSyncIdFromChromeId(const IDType& id) = 0;
64
65  // Returns the chrome node for the given sync id.
66  // Returns NULL if no node is found for the given sync id.
67  virtual const Node* GetChromeNodeFromSyncId(int64 sync_id) = 0;
68
69  // Initializes the given sync node from the given chrome node id.
70  // Returns false if no sync node was found for the given chrome node id or
71  // if the initialization of sync node fails.
72  virtual bool InitSyncNodeFromChromeId(const IDType& node_id,
73                                        sync_api::BaseNode* sync_node) = 0;
74
75  // Associates the given chrome node with the given sync id.
76  virtual void Associate(const Node* node, int64 sync_id) = 0;
77
78  // Remove the association that corresponds to the given sync id.
79  virtual void Disassociate(int64 sync_id) = 0;
80};
81
82}  // namespace browser_sync
83
84#endif  // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATOR_H_
85