OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ | 5 #ifndef COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ |
6 #define COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ | 6 #define COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ |
7 | 7 |
8 #include "base/scoped_observer.h" | 8 #include "base/scoped_observer.h" |
9 #include "components/history/core/browser/history_backend_observer.h" | 9 #include "components/history/core/browser/history_backend_observer.h" |
10 #include "components/history/core/browser/typed_url_sync_metadata_database.h" | 10 #include "components/history/core/browser/typed_url_sync_metadata_database.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 base::Optional<syncer::ModelError> ApplySyncChanges( | 33 base::Optional<syncer::ModelError> ApplySyncChanges( |
34 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, | 34 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
35 syncer::EntityChangeList entity_changes) override; | 35 syncer::EntityChangeList entity_changes) override; |
36 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | 36 void GetData(StorageKeyList storage_keys, DataCallback callback) override; |
37 void GetAllData(DataCallback callback) override; | 37 void GetAllData(DataCallback callback) override; |
38 std::string GetClientTag(const syncer::EntityData& entity_data) override; | 38 std::string GetClientTag(const syncer::EntityData& entity_data) override; |
39 std::string GetStorageKey(const syncer::EntityData& entity_data) override; | 39 std::string GetStorageKey(const syncer::EntityData& entity_data) override; |
40 bool SupportsGetStorageKey() const override; | 40 bool SupportsGetStorageKey() const override; |
41 | 41 |
42 // history::HistoryBackendObserver: | 42 // history::HistoryBackendObserver: |
43 void OnURLVisited(history::HistoryBackend* history_backend, | 43 void OnURLVisited(HistoryBackend* history_backend, |
44 ui::PageTransition transition, | 44 ui::PageTransition transition, |
45 const history::URLRow& row, | 45 const URLRow& row, |
46 const history::RedirectList& redirects, | 46 const RedirectList& redirects, |
47 base::Time visit_time) override; | 47 base::Time visit_time) override; |
48 void OnURLsModified(history::HistoryBackend* history_backend, | 48 void OnURLsModified(HistoryBackend* history_backend, |
49 const history::URLRows& changed_urls) override; | 49 const URLRows& changed_urls) override; |
50 void OnURLsDeleted(history::HistoryBackend* history_backend, | 50 void OnURLsDeleted(HistoryBackend* history_backend, |
51 bool all_history, | 51 bool all_history, |
52 bool expired, | 52 bool expired, |
53 const history::URLRows& deleted_rows, | 53 const URLRows& deleted_rows, |
54 const std::set<GURL>& favicon_urls) override; | 54 const std::set<GURL>& favicon_urls) override; |
55 | 55 |
56 // Must be called after creation and before any operations. | 56 // Must be called after creation and before any operations. |
57 void Init(); | 57 void Init(); |
58 | 58 |
59 // Returns the percentage of DB accesses that have resulted in an error. | 59 // Returns the percentage of DB accesses that have resulted in an error. |
60 int GetErrorPercentage() const; | 60 int GetErrorPercentage() const; |
61 | 61 |
62 // Return true if this function successfully converts the passed URL | 62 // Return true if this function successfully converts the passed URL |
63 // information to a TypedUrlSpecifics structure for writing to the sync DB. | 63 // information to a TypedUrlSpecifics structure for writing to the sync DB. |
(...skipping 26 matching lines...) Expand all Loading... |
90 // history database in |url| and |visits|, and returns a bitmask with the | 90 // history database in |url| and |visits|, and returns a bitmask with the |
91 // results of the merge: | 91 // results of the merge: |
92 // DIFF_UPDATE_NODE - changes have been made to |new_url| and |visits| which | 92 // DIFF_UPDATE_NODE - changes have been made to |new_url| and |visits| which |
93 // should be persisted to the sync node. | 93 // should be persisted to the sync node. |
94 // DIFF_LOCAL_ROW_CHANGED - The history data in |new_url| should be persisted | 94 // DIFF_LOCAL_ROW_CHANGED - The history data in |new_url| should be persisted |
95 // to the history DB. | 95 // to the history DB. |
96 // DIFF_LOCAL_VISITS_ADDED - |new_visits| contains a list of visits that | 96 // DIFF_LOCAL_VISITS_ADDED - |new_visits| contains a list of visits that |
97 // should be written to the history DB for this URL. Deletions are not | 97 // should be written to the history DB for this URL. Deletions are not |
98 // written to the DB - each client is left to age out visits on their own. | 98 // written to the DB - each client is left to age out visits on their own. |
99 static MergeResult MergeUrls(const sync_pb::TypedUrlSpecifics& typed_url, | 99 static MergeResult MergeUrls(const sync_pb::TypedUrlSpecifics& typed_url, |
100 const history::URLRow& url, | 100 const URLRow& url, |
101 history::VisitVector* visits, | 101 VisitVector* visits, |
102 history::URLRow* new_url, | 102 URLRow* new_url, |
103 std::vector<history::VisitInfo>* new_visits); | 103 std::vector<VisitInfo>* new_visits); |
| 104 |
| 105 // Diffs the set of visits between the history DB and the sync DB, using the |
| 106 // sync DB as the canonical copy. Result is the set of |new_visits| and |
| 107 // |removed_visits| that can be applied to the history DB to make it match |
| 108 // the sync DB version. |removed_visits| can be null if the caller does not |
| 109 // care about which visits to remove. |
| 110 static void DiffVisits(const VisitVector& history_visits, |
| 111 const sync_pb::TypedUrlSpecifics& sync_specifics, |
| 112 std::vector<VisitInfo>* new_visits, |
| 113 VisitVector* removed_visits); |
104 | 114 |
105 // Fills |new_url| with formatted data from |typed_url|. | 115 // Fills |new_url| with formatted data from |typed_url|. |
106 static void UpdateURLRowFromTypedUrlSpecifics( | 116 static void UpdateURLRowFromTypedUrlSpecifics( |
107 const sync_pb::TypedUrlSpecifics& typed_url, | 117 const sync_pb::TypedUrlSpecifics& typed_url, |
108 history::URLRow* new_url); | 118 URLRow* new_url); |
109 | 119 |
110 // Synchronously load sync metadata from the TypedURLSyncMetadataDatabase and | 120 // Synchronously load sync metadata from the TypedURLSyncMetadataDatabase and |
111 // pass it to the processor so that it can start tracking changes. | 121 // pass it to the processor so that it can start tracking changes. |
112 void LoadMetadata(); | 122 void LoadMetadata(); |
113 | 123 |
114 // Helper function that clears our error counters (used to reset stats after | 124 // Helper function that clears our error counters (used to reset stats after |
115 // merge so we can track merge errors separately). | 125 // merge so we can track merge errors separately). |
116 void ClearErrorStats(); | 126 void ClearErrorStats(); |
117 | 127 |
118 // Compares |server_typed_url| from the server against local history to decide | 128 // Compares |server_typed_url| from the server against local history to decide |
119 // how to merge any existing data, and updates appropriate data containers to | 129 // how to merge any existing data, and updates appropriate data containers to |
120 // write to server and backend. | 130 // write to server and backend. |
121 void UpdateUrlFromServer(const sync_pb::TypedUrlSpecifics& server_typed_url, | 131 void MergeURLWithSync(const sync_pb::TypedUrlSpecifics& server_typed_url, |
122 TypedURLMap* local_typed_urls, | 132 TypedURLMap* local_typed_urls, |
123 URLVisitVectorMap* visit_vectors, | 133 URLVisitVectorMap* visit_vectors, |
124 history::URLRows* new_synced_urls, | 134 URLRows* new_synced_urls, |
125 TypedURLVisitVector* new_synced_visits, | 135 TypedURLVisitVector* new_synced_visits, |
126 history::URLRows* updated_synced_urls); | 136 URLRows* updated_synced_urls); |
| 137 |
| 138 // Given a typed URL in the sync DB, looks for an existing entry in the |
| 139 // local history DB and generates a list of visits to add to the |
| 140 // history DB to bring it up to date (avoiding duplicates). |
| 141 // Updates the passed |visits_to_add| and |visits_to_remove| vectors with the |
| 142 // visits to add to/remove from the history DB, and adds a new entry to either |
| 143 // |updated_urls| or |new_urls| depending on whether the URL already existed |
| 144 // in the history DB. |
| 145 void UpdateFromSync(const sync_pb::TypedUrlSpecifics& typed_url, |
| 146 TypedURLVisitVector* visits_to_add, |
| 147 VisitVector* visits_to_remove, |
| 148 URLRows* updated_urls, |
| 149 URLRows* new_urls); |
| 150 |
| 151 // Utility routine that either updates an existing sync node or creates a |
| 152 // new one for the passed |typed_url| if one does not already exist. |
| 153 void UpdateSyncFromLocal(URLRow typed_url, |
| 154 syncer::MetadataChangeList* metadata_change_list); |
127 | 155 |
128 // Writes new typed url data from sync server to history backend. | 156 // Writes new typed url data from sync server to history backend. |
129 base::Optional<syncer::ModelError> WriteToHistoryBackend( | 157 base::Optional<syncer::ModelError> WriteToHistoryBackend( |
130 const history::URLRows* new_urls, | 158 const URLRows* new_urls, |
131 const history::URLRows* updated_urls, | 159 const URLRows* updated_urls, |
132 const std::vector<GURL>* deleted_urls, | 160 const std::vector<GURL>* deleted_urls, |
133 const TypedURLVisitVector* new_visits, | 161 const TypedURLVisitVector* new_visits, |
134 const history::VisitVector* deleted_visits); | 162 const VisitVector* deleted_visits); |
135 | 163 |
136 // Given a TypedUrlSpecifics object, removes all visits that are older than | 164 // Given a TypedUrlSpecifics object, removes all visits that are older than |
137 // the current expiration time. Note that this can result in having no visits | 165 // the current expiration time. Note that this can result in having no visits |
138 // at all. | 166 // at all. |
139 sync_pb::TypedUrlSpecifics FilterExpiredVisits( | 167 sync_pb::TypedUrlSpecifics FilterExpiredVisits( |
140 const sync_pb::TypedUrlSpecifics& specifics); | 168 const sync_pb::TypedUrlSpecifics& specifics); |
141 | 169 |
142 // Helper function that determines if we should ignore a URL for the purposes | 170 // Helper function that determines if we should ignore a URL for the purposes |
143 // of sync, because it contains invalid data. | 171 // of sync, because it contains invalid data. |
144 bool ShouldIgnoreUrl(const GURL& url); | 172 bool ShouldIgnoreUrl(const GURL& url); |
145 | 173 |
146 // Helper function that determines if we should ignore a URL for the purposes | 174 // Helper function that determines if we should ignore a URL for the purposes |
147 // of sync, based on the visits the URL had. | 175 // of sync, based on the visits the URL had. |
148 bool ShouldIgnoreVisits(const history::VisitVector& visits); | 176 bool ShouldIgnoreVisits(const VisitVector& visits); |
| 177 |
| 178 // Returns true if the caller should sync as a result of the passed visit |
| 179 // notification. We use this to throttle the number of sync changes we send |
| 180 // to the server so we don't hit the server for every |
| 181 // single typed URL visit. |
| 182 bool ShouldSyncVisit(int typed_count, ui::PageTransition transition); |
149 | 183 |
150 // Fetches visits from the history DB corresponding to the passed URL. This | 184 // Fetches visits from the history DB corresponding to the passed URL. This |
151 // function compensates for the fact that the history DB has rather poor data | 185 // function compensates for the fact that the history DB has rather poor data |
152 // integrity (duplicate visits, visit timestamps that don't match the | 186 // integrity (duplicate visits, visit timestamps that don't match the |
153 // last_visit timestamp, huge data sets that exhaust memory when fetched, | 187 // last_visit timestamp, huge data sets that exhaust memory when fetched, |
154 // expired visits that are not deleted by |ExpireHistoryBackend|, etc) by | 188 // expired visits that are not deleted by |ExpireHistoryBackend|, etc) by |
155 // modifying the passed |url| object and |visits| vector. The order of | 189 // modifying the passed |url| object and |visits| vector. The order of |
156 // |visits| will be from the oldest to the newest order. | 190 // |visits| will be from the oldest to the newest order. |
157 // Returns false in two cases. | 191 // Returns false in two cases. |
158 // 1. we could not fetch the visits for the passed URL, DB error. | 192 // 1. we could not fetch the visits for the passed URL, DB error. |
159 // 2. No visits for the passed url, or all the visits are expired. | 193 // 2. No visits for the passed url, or all the visits are expired. |
160 bool FixupURLAndGetVisits(URLRow* url, VisitVector* visits); | 194 bool FixupURLAndGetVisits(URLRow* url, VisitVector* visits); |
161 | 195 |
162 // Create an EntityData by URL |row| and its visits |visits|. | 196 // Create an EntityData by URL |row| and its visits |visits|. |
163 std::unique_ptr<syncer::EntityData> CreateEntityData( | 197 syncer::EntityData* CreateEntityData(const URLRow& row, |
164 const URLRow& row, | 198 const VisitVector& visits); |
165 const VisitVector& visits); | |
166 | 199 |
167 // Get all the typed urls and visits from the history db, after filtering | 200 // Get all the typed urls and visits from the history db, after filtering |
168 // them, put them into |url_to_visit| and |url_to_urlrow|. | 201 // them, put them into |url_to_visit| and |url_to_urlrow|. |
169 // Return false if cannot get urls from HistoryBackend. | 202 // Return false if cannot get urls from HistoryBackend. |
170 bool GetValidURLsAndVisits(URLVisitVectorMap* url_to_visit, | 203 bool GetValidURLsAndVisits(URLVisitVectorMap* url_to_visit, |
171 TypedURLMap* url_to_urlrow); | 204 TypedURLMap* url_to_urlrow); |
172 | 205 |
173 // Get URLID from HistoryBackend, and return URLID as storage key. | 206 // Get URLID from HistoryBackend, and return URLID as storage key. |
174 std::string GetStorageKeyInternal(const std::string& url); | 207 std::string GetStorageKeyInternal(const std::string& url); |
175 | 208 |
| 209 // Send local typed url to processor(). |
| 210 void SendTypedURLToProcessor( |
| 211 const URLRow& row, |
| 212 const VisitVector& visits, |
| 213 syncer::MetadataChangeList* metadata_change_list); |
| 214 |
176 // A non-owning pointer to the backend, which we're syncing local changes from | 215 // A non-owning pointer to the backend, which we're syncing local changes from |
177 // and sync changes to. | 216 // and sync changes to. |
178 HistoryBackend* const history_backend_; | 217 HistoryBackend* const history_backend_; |
179 | 218 |
180 // A non-owning pointer to the database, which is for storing typed urls sync | 219 // A non-owning pointer to the database, which is for storing typed urls sync |
181 // metadata and state. | 220 // metadata and state. |
182 TypedURLSyncMetadataDatabase* const sync_metadata_database_; | 221 TypedURLSyncMetadataDatabase* const sync_metadata_database_; |
183 | 222 |
184 // Statistics for the purposes of tracking the percentage of DB accesses that | 223 // Statistics for the purposes of tracking the percentage of DB accesses that |
185 // fail for each client via UMA. | 224 // fail for each client via UMA. |
186 int num_db_accesses_; | 225 int num_db_accesses_; |
187 int num_db_errors_; | 226 int num_db_errors_; |
188 | 227 |
189 // Since HistoryBackend use SequencedTaskRunner, so should use SequenceChecker | 228 // Since HistoryBackend use SequencedTaskRunner, so should use SequenceChecker |
190 // here. | 229 // here. |
191 base::SequenceChecker sequence_checker_; | 230 base::SequenceChecker sequence_checker_; |
192 | 231 |
193 // Tracks observed history backend, for receiving updates from history | 232 // Tracks observed history backend, for receiving updates from history |
194 // backend. | 233 // backend. |
195 ScopedObserver<history::HistoryBackend, history::HistoryBackendObserver> | 234 ScopedObserver<HistoryBackend, HistoryBackendObserver> |
196 history_backend_observer_; | 235 history_backend_observer_; |
197 | 236 |
198 DISALLOW_COPY_AND_ASSIGN(TypedURLSyncBridge); | 237 DISALLOW_COPY_AND_ASSIGN(TypedURLSyncBridge); |
199 }; | 238 }; |
200 | 239 |
201 } // namespace history | 240 } // namespace history |
202 | 241 |
203 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ | 242 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ |
OLD | NEW |