typed_url_model_associator.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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_TYPED_URL_MODEL_ASSOCIATOR_H_ 6#define CHROME_BROWSER_SYNC_GLUE_TYPED_URL_MODEL_ASSOCIATOR_H_ 7#pragma once 8 9#include <map> 10#include <string> 11#include <vector> 12 13#include "base/basictypes.h" 14#include "base/string16.h" 15#include "base/task.h" 16#include "chrome/browser/chrome_thread.h" 17#include "chrome/browser/history/history_types.h" 18#include "chrome/browser/sync/glue/model_associator.h" 19#include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" 20 21class GURL; 22class MessageLoop; 23class ProfileSyncService; 24 25namespace history { 26class HistoryBackend; 27class URLRow; 28}; 29 30namespace sync_api { 31class WriteNode; 32class WriteTransaction; 33}; 34 35namespace browser_sync { 36 37class TypedUrlChangeProcessor; 38class UnrecoverableErrorHandler; 39 40extern const char kTypedUrlTag[]; 41 42// Contains all model association related logic: 43// * Algorithm to associate typed_url model and sync model. 44// * Persisting model associations and loading them back. 45// We do not check if we have local data before this run; we always 46// merge and sync. 47class TypedUrlModelAssociator 48 : public PerDataTypeAssociatorInterface<std::string, std::string> { 49 public: 50 typedef std::vector<std::pair<GURL, string16> > TypedUrlTitleVector; 51 typedef std::vector<history::URLRow> TypedUrlVector; 52 typedef std::vector<std::pair<history::URLID, history::URLRow> > 53 TypedUrlUpdateVector; 54 typedef std::vector<std::pair<GURL, std::vector<base::Time> > > 55 TypedUrlVisitVector; 56 57 static syncable::ModelType model_type() { return syncable::TYPED_URLS; } 58 TypedUrlModelAssociator(ProfileSyncService* sync_service, 59 history::HistoryBackend* history_backend); 60 virtual ~TypedUrlModelAssociator() { } 61 62 // PerDataTypeAssociatorInterface implementation. 63 // 64 // Iterates through the sync model looking for matched pairs of items. 65 virtual bool AssociateModels(); 66 67 // Delete all typed url nodes. 68 bool DeleteAllNodes(sync_api::WriteTransaction* trans); 69 70 // Clears all associations. 71 virtual bool DisassociateModels(); 72 73 // The has_nodes out param is true if the sync model has nodes other 74 // than the permanent tagged nodes. 75 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); 76 77 virtual void AbortAssociation() { 78 // TODO(zork): Implement this. 79 } 80 81 // Not implemented. 82 virtual const std::string* GetChromeNodeFromSyncId(int64 sync_id) { 83 return NULL; 84 } 85 86 // Not implemented. 87 virtual bool InitSyncNodeFromChromeId(std::string node_id, 88 sync_api::BaseNode* sync_node) { 89 return false; 90 } 91 92 // Returns the sync id for the given typed_url name, or sync_api::kInvalidId 93 // if the typed_url name is not associated to any sync id. 94 virtual int64 GetSyncIdFromChromeId(std::string node_id); 95 96 // Associates the given typed_url name with the given sync id. 97 virtual void Associate(const std::string* node, int64 sync_id); 98 99 // Remove the association that corresponds to the given sync id. 100 virtual void Disassociate(int64 sync_id); 101 102 // Returns whether a node with the given permanent tag was found and update 103 // |sync_id| with that node's id. 104 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); 105 106 bool WriteToHistoryBackend(const TypedUrlTitleVector* titles, 107 const TypedUrlVector* new_urls, 108 const TypedUrlUpdateVector* updated_urls, 109 const TypedUrlVisitVector* new_visits, 110 const history::VisitVector* deleted_visits); 111 112 enum { 113 DIFF_NONE = 0x0000, 114 DIFF_NODE_CHANGED = 0x0001, 115 DIFF_TITLE_CHANGED = 0x0002, 116 DIFF_ROW_CHANGED = 0x0004, 117 DIFF_VISITS_ADDED = 0x0008, 118 }; 119 120 static int MergeUrls(const sync_pb::TypedUrlSpecifics& typed_url, 121 const history::URLRow& url, 122 history::VisitVector* visits, 123 history::URLRow* new_url, 124 std::vector<base::Time>* new_visits); 125 static void WriteToSyncNode(const history::URLRow& url, 126 const history::VisitVector& visits, 127 sync_api::WriteNode* node); 128 129 static void DiffVisits(const history::VisitVector& old_visits, 130 const sync_pb::TypedUrlSpecifics& new_url, 131 std::vector<base::Time>* new_visits, 132 history::VisitVector* removed_visits); 133 134 protected: 135 // Returns sync service instance. 136 ProfileSyncService* sync_service() { return sync_service_; } 137 138 private: 139 typedef std::map<std::string, int64> TypedUrlToSyncIdMap; 140 typedef std::map<int64, std::string> SyncIdToTypedUrlMap; 141 142 ProfileSyncService* sync_service_; 143 history::HistoryBackend* history_backend_; 144 int64 typed_url_node_id_; 145 146 MessageLoop* expected_loop_; 147 148 TypedUrlToSyncIdMap id_map_; 149 SyncIdToTypedUrlMap id_map_inverse_; 150 151 DISALLOW_COPY_AND_ASSIGN(TypedUrlModelAssociator); 152}; 153 154} // namespace browser_sync 155 156#endif // CHROME_BROWSER_SYNC_GLUE_TYPED_URL_MODEL_ASSOCIATOR_H_ 157