Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: components/history/core/browser/typed_url_sync_bridge.h

Issue 2961723003: [USS] Implement ApplySyncChanges and OnURLVisited/Modified/Deleted. (Closed)
Patch Set: handle nullptr case in bridge Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 std::unique_ptr<syncer::EntityData> CreateEntityData(
164 const URLRow& row, 198 const URLRow& row,
165 const VisitVector& visits); 199 const VisitVector& visits);
166 200
167 // Get all the typed urls and visits from the history db, after filtering 201 // 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|. 202 // them, put them into |url_to_visit| and |url_to_urlrow|.
169 // Return false if cannot get urls from HistoryBackend. 203 // Return false if cannot get urls from HistoryBackend.
170 bool GetValidURLsAndVisits(URLVisitVectorMap* url_to_visit, 204 bool GetValidURLsAndVisits(URLVisitVectorMap* url_to_visit,
171 TypedURLMap* url_to_urlrow); 205 TypedURLMap* url_to_urlrow);
172 206
173 // Get URLID from HistoryBackend, and return URLID as storage key. 207 // Get URLID from HistoryBackend, and return URLID as storage key.
174 std::string GetStorageKeyInternal(const std::string& url); 208 std::string GetStorageKeyInternal(const std::string& url);
175 209
210 // Send local typed url to processor().
211 void SendTypedURLToProcessor(
212 const URLRow& row,
213 const VisitVector& visits,
214 syncer::MetadataChangeList* metadata_change_list);
215
176 // A non-owning pointer to the backend, which we're syncing local changes from 216 // A non-owning pointer to the backend, which we're syncing local changes from
177 // and sync changes to. 217 // and sync changes to.
178 HistoryBackend* const history_backend_; 218 HistoryBackend* const history_backend_;
179 219
180 // A non-owning pointer to the database, which is for storing typed urls sync 220 // A non-owning pointer to the database, which is for storing typed urls sync
181 // metadata and state. 221 // metadata and state.
182 TypedURLSyncMetadataDatabase* const sync_metadata_database_; 222 TypedURLSyncMetadataDatabase* const sync_metadata_database_;
183 223
184 // Statistics for the purposes of tracking the percentage of DB accesses that 224 // Statistics for the purposes of tracking the percentage of DB accesses that
185 // fail for each client via UMA. 225 // fail for each client via UMA.
186 int num_db_accesses_; 226 int num_db_accesses_;
187 int num_db_errors_; 227 int num_db_errors_;
188 228
189 // Since HistoryBackend use SequencedTaskRunner, so should use SequenceChecker 229 // Since HistoryBackend use SequencedTaskRunner, so should use SequenceChecker
190 // here. 230 // here.
191 base::SequenceChecker sequence_checker_; 231 base::SequenceChecker sequence_checker_;
192 232
193 // Tracks observed history backend, for receiving updates from history 233 // Tracks observed history backend, for receiving updates from history
194 // backend. 234 // backend.
195 ScopedObserver<history::HistoryBackend, history::HistoryBackendObserver> 235 ScopedObserver<HistoryBackend, HistoryBackendObserver>
196 history_backend_observer_; 236 history_backend_observer_;
197 237
198 DISALLOW_COPY_AND_ASSIGN(TypedURLSyncBridge); 238 DISALLOW_COPY_AND_ASSIGN(TypedURLSyncBridge);
199 }; 239 };
200 240
201 } // namespace history 241 } // namespace history
202 242
203 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_ 243 #endif // COMPONENTS_HISTORY_CORE_BROWSER_TYPED_URL_SYNC_BRIDGE_H_
OLDNEW
« no previous file with comments | « components/history/core/browser/history_backend.cc ('k') | components/history/core/browser/typed_url_sync_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698