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

Side by Side Diff: chrome/browser/history/browsing_history_service.h

Issue 2450453002: Refactor BrowsingHistoryHandler, create BrowsingHistoryService (Closed)
Patch Set: Rename HistoryUiService to BrowsingHistoryService Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/history/browsing_history_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ 5 #ifndef CHROME_BROWSER_HISTORY_BROWSING_HISTORY_SERVICE_H_
6 #define CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ 6 #define CHROME_BROWSER_HISTORY_BROWSING_HISTORY_SERVICE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/scoped_observer.h" 17 #include "base/scoped_observer.h"
18 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
19 #include "base/task/cancelable_task_tracker.h" 19 #include "base/task/cancelable_task_tracker.h"
20 #include "base/timer/timer.h" 20 #include "base/timer/timer.h"
21 #include "base/values.h" 21 #include "base/values.h"
22 #include "components/history/core/browser/history_service_observer.h" 22 #include "components/history/core/browser/history_service_observer.h"
23 #include "components/history/core/browser/url_row.h" 23 #include "components/history/core/browser/url_row.h"
24 #include "components/history/core/browser/web_history_service.h" 24 #include "components/history/core/browser/web_history_service.h"
25 #include "components/history/core/browser/web_history_service_observer.h" 25 #include "components/history/core/browser/web_history_service_observer.h"
26 #include "components/sync/driver/sync_service_observer.h" 26 #include "components/sync/driver/sync_service_observer.h"
27 #include "content/public/browser/web_ui_message_handler.h"
28 #include "url/gurl.h" 27 #include "url/gurl.h"
29 28
30 class SupervisedUserService;
31
32 namespace bookmarks {
33 class BookmarkModel;
34 } // namespace bookmarks
35
36 namespace browser_sync { 29 namespace browser_sync {
37 class ProfileSyncService; 30 class ProfileSyncService;
38 } // namespace browser_sync 31 } // namespace browser_sync
39 32
40 namespace history { 33 namespace history {
41 class HistoryService; 34 class HistoryService;
42 class QueryResults; 35 class QueryResults;
43 struct QueryOptions; 36 struct QueryOptions;
44 } // namespace history 37 } // namespace history
45 38
46 namespace syncer { 39 namespace syncer {
47 class SyncServiceObserver; 40 class SyncServiceObserver;
48 } // namespace syncer 41 } // namespace syncer
49 42
50 // The handler for Javascript messages related to the "history" view. 43 class Profile;
51 class BrowsingHistoryHandler : public content::WebUIMessageHandler, 44
52 public history::HistoryServiceObserver, 45 // Interacts with HistoryService, WebHistoryService, and SyncService to query
46 // history and provide results to the associated BrowsingHistoryServiceHandler.
47 class BrowsingHistoryService : public history::HistoryServiceObserver,
53 public history::WebHistoryServiceObserver, 48 public history::WebHistoryServiceObserver,
54 public syncer::SyncServiceObserver { 49 public syncer::SyncServiceObserver {
55 public: 50 public:
56 // Represents a history entry to be shown to the user, representing either 51 // Represents a history entry to be shown to the user, representing either
57 // a local or remote visit. A single entry can represent multiple visits, 52 // a local or remote visit. A single entry can represent multiple visits,
58 // since only the most recent visit on a particular day is shown. 53 // since only the most recent visit on a particular day is shown.
59 struct HistoryEntry { 54 struct HistoryEntry {
60 // Values indicating whether an entry represents only local visits, only 55 // Values indicating whether an entry represents only local visits, only
61 // remote visits, or a mixture of both. 56 // remote visits, or a mixture of both.
62 enum EntryType { 57 enum EntryType {
63 EMPTY_ENTRY = 0, 58 EMPTY_ENTRY = 0,
64 LOCAL_ENTRY, 59 LOCAL_ENTRY,
65 REMOTE_ENTRY, 60 REMOTE_ENTRY,
66 COMBINED_ENTRY 61 COMBINED_ENTRY
67 }; 62 };
68 63
69 HistoryEntry(EntryType type, const GURL& url, const base::string16& title, 64 HistoryEntry(EntryType type, const GURL& url, const base::string16& title,
70 base::Time time, const std::string& client_id, 65 base::Time time, const std::string& client_id,
71 bool is_search_result, const base::string16& snippet, 66 bool is_search_result, const base::string16& snippet,
72 bool blocked_visit); 67 bool blocked_visit);
73 HistoryEntry(); 68 HistoryEntry();
74 HistoryEntry(const HistoryEntry& other); 69 HistoryEntry(const HistoryEntry& other);
75 virtual ~HistoryEntry(); 70 virtual ~HistoryEntry();
76 71
77 // Formats this entry's URL and title and adds them to |result|.
78 void SetUrlAndTitle(base::DictionaryValue* result,
79 bool limit_title_length) const;
80
81 // Converts the entry to a DictionaryValue to be owned by the caller.
82 std::unique_ptr<base::DictionaryValue> ToValue(
83 bookmarks::BookmarkModel* bookmark_model,
84 SupervisedUserService* supervised_user_service,
85 const browser_sync::ProfileSyncService* sync_service,
86 bool limit_title_length) const;
87
88 // Comparison function for sorting HistoryEntries from newest to oldest. 72 // Comparison function for sorting HistoryEntries from newest to oldest.
89 static bool SortByTimeDescending( 73 static bool SortByTimeDescending(
90 const HistoryEntry& entry1, const HistoryEntry& entry2); 74 const HistoryEntry& entry1, const HistoryEntry& entry2);
91 75
92 // The type of visits this entry represents: local, remote, or both. 76 // The type of visits this entry represents: local, remote, or both.
93 EntryType entry_type; 77 EntryType entry_type;
94 78
95 GURL url; 79 GURL url;
96 base::string16 title; // Title of the entry. May be empty. 80 base::string16 title; // Title of the entry. May be empty.
97 81
(...skipping 10 matching lines...) Expand all
108 // If true, this entry is a search result. 92 // If true, this entry is a search result.
109 bool is_search_result; 93 bool is_search_result;
110 94
111 // The entry's search snippet, if this entry is a search result. 95 // The entry's search snippet, if this entry is a search result.
112 base::string16 snippet; 96 base::string16 snippet;
113 97
114 // Whether this entry was blocked when it was attempted. 98 // Whether this entry was blocked when it was attempted.
115 bool blocked_visit; 99 bool blocked_visit;
116 }; 100 };
117 101
118 BrowsingHistoryHandler(); 102 // Contains information about a completed history query.
119 ~BrowsingHistoryHandler() override; 103 struct QueryResultsInfo {
104 QueryResultsInfo();
105 ~QueryResultsInfo();
120 106
121 // WebUIMessageHandler implementation. 107 // The query search text.
122 void RegisterMessages() override; 108 base::string16 search_text;
109
110 // Whether the query reached the beginning of the database.
111 bool reached_beginning;
112
113 // Whether the last call to Web History returned synced results.
114 bool has_synced_results;
115
116 // The localized query start time.
117 base::Time start_time;
118
119 // The localized query end time.
120 base::Time end_time;
121 };
122
123 // Interface for handling calls from the BrowsingHistoryService.
124 class BrowsingHistoryServiceHandler {
sky 2016/11/07 23:49:19 Move this into it's own header (as style guide sug
Theresa 2016/11/08 00:59:48 Done.
125 public:
126 // Callback for QueryHistory().
127 virtual void OnQueryComplete(
128 std::vector<BrowsingHistoryService::HistoryEntry>* results,
129 BrowsingHistoryService::QueryResultsInfo* query_results_info) = 0;
130
131 // Callback for RemoveVisits().
132 virtual void OnRemoveVisitsComplete() = 0;
133
134 // Callback for RemoveVisits() that fails.
135 virtual void OnRemoveVisitsFailed() = 0;
136
137 // Called when HistoryService or WebHistoryService deletes one or more
138 // items.
139 virtual void HistoryDeleted() = 0;
140
141 // Whether other forms of browsing history were found on the history
142 // service.
143 virtual void HasOtherFormsOfBrowsingHistory(
144 bool has_other_forms, bool has_synced_results) = 0;
145 };
sky 2016/11/07 23:49:18 make constructor protected.
Theresa 2016/11/08 00:59:48 Done.
146
147 BrowsingHistoryService(
148 Profile* profile,
149 BrowsingHistoryService::BrowsingHistoryServiceHandler* handler);
150 ~BrowsingHistoryService() override;
151
152 // Core implementation of history querying.
153 void QueryHistory(const base::string16& search_text,
154 const history::QueryOptions& options);
155
156 // Removes |items| from history.
157 void RemoveVisits(
158 std::vector<std::unique_ptr<BrowsingHistoryService::HistoryEntry>>*
159 items);
123 160
124 // SyncServiceObserver implementation. 161 // SyncServiceObserver implementation.
125 void OnStateChanged() override; 162 void OnStateChanged() override;
126 163
127 // Handler for the "queryHistory" message.
128 void HandleQueryHistory(const base::ListValue* args);
129
130 // Handler for the "removeVisits" message.
131 void HandleRemoveVisits(const base::ListValue* args);
132
133 // Handler for "clearBrowsingData" message.
134 void HandleClearBrowsingData(const base::ListValue* args);
135
136 // Handler for "removeBookmark" message.
137 void HandleRemoveBookmark(const base::ListValue* args);
138
139 // Merges duplicate entries from the query results, only retaining the most 164 // Merges duplicate entries from the query results, only retaining the most
140 // recent visit to a URL on a particular day. That visit contains the 165 // recent visit to a URL on a particular day. That visit contains the
141 // timestamps of the other visits. 166 // timestamps of the other visits.
142 static void MergeDuplicateResults( 167 static void MergeDuplicateResults(
143 std::vector<BrowsingHistoryHandler::HistoryEntry>* results); 168 std::vector<BrowsingHistoryService::HistoryEntry>* results);
144 169
145 protected:
146 // Callback from the history system when a history query has completed. 170 // Callback from the history system when a history query has completed.
147 // Exposed for testing. 171 // Exposed for testing.
148 void QueryComplete(const base::string16& search_text, 172 void QueryComplete(const base::string16& search_text,
149 const history::QueryOptions& options, 173 const history::QueryOptions& options,
150 history::QueryResults* results); 174 history::QueryResults* results);
151 175
152 private: 176 private:
153 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest, 177 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest,
154 ObservingWebHistoryDeletions); 178 ObservingWebHistoryDeletions);
155 179
156 // The range for which to return results:
157 // - ALLTIME: allows access to all the results in a paginated way.
158 // - WEEK: the last 7 days.
159 // - MONTH: the last calendar month.
160 enum Range {
161 ALL_TIME = 0,
162 WEEK = 1,
163 MONTH = 2
164 };
165
166 // Core implementation of history querying.
167 void QueryHistory(const base::string16& search_text,
168 const history::QueryOptions& options);
169
170 // Combines the query results from the local history database and the history 180 // Combines the query results from the local history database and the history
171 // server, and sends the combined results to the front end. 181 // server, and sends the combined results to the
172 void ReturnResultsToFrontEnd(); 182 // BrowsingHistoryServiceHandler.
183 void ReturnResultsToHandler();
173 184
174 // Callback from |web_history_timer_| when a response from web history has 185 // Callback from |web_history_timer_| when a response from web history has
175 // not been received in time. 186 // not been received in time.
176 void WebHistoryTimeout(); 187 void WebHistoryTimeout();
177 188
178 // Callback from the WebHistoryService when a query has completed. 189 // Callback from the WebHistoryService when a query has completed.
179 void WebHistoryQueryComplete(const base::string16& search_text, 190 void WebHistoryQueryComplete(const base::string16& search_text,
180 const history::QueryOptions& options, 191 const history::QueryOptions& options,
181 base::TimeTicks start_time, 192 base::TimeTicks start_time,
182 history::WebHistoryService::Request* request, 193 history::WebHistoryService::Request* request,
183 const base::DictionaryValue* results_value); 194 const base::DictionaryValue* results_value);
184 195
185 // Callback telling us whether other forms of browsing history were found 196 // Callback telling us whether other forms of browsing history were found
186 // on the history server. 197 // on the history server.
187 void OtherFormsOfBrowsingHistoryQueryComplete( 198 void OtherFormsOfBrowsingHistoryQueryComplete(
188 bool found_other_forms_of_browsing_history); 199 bool found_other_forms_of_browsing_history);
189 200
190 // Callback from the history system when visits were deleted. 201 // Callback from the history system when visits were deleted.
191 void RemoveComplete(); 202 void RemoveComplete();
192 203
193 // Callback from history server when visits were deleted. 204 // Callback from history server when visits were deleted.
194 void RemoveWebHistoryComplete(bool success); 205 void RemoveWebHistoryComplete(bool success);
195 206
196 bool ExtractIntegerValueAtIndex(
197 const base::ListValue* value, int index, int* out_int);
198
199 // Sets the query options for a week-wide query, |offset| weeks ago.
200 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options);
201
202 // Sets the query options for a monthly query, |offset| months ago.
203 void SetQueryTimeInMonths(int offset, history::QueryOptions* options);
204
205 // kAcceptLanguages pref value.
206 std::string GetAcceptLanguages() const;
207
208 // history::HistoryServiceObserver: 207 // history::HistoryServiceObserver:
209 void OnURLsDeleted(history::HistoryService* history_service, 208 void OnURLsDeleted(history::HistoryService* history_service,
210 bool all_history, 209 bool all_history,
211 bool expired, 210 bool expired,
212 const history::URLRows& deleted_rows, 211 const history::URLRows& deleted_rows,
213 const std::set<GURL>& favicon_urls) override; 212 const std::set<GURL>& favicon_urls) override;
214 213
215 // history::WebHistoryServiceObserver: 214 // history::WebHistoryServiceObserver:
216 void OnWebHistoryDeleted() override; 215 void OnWebHistoryDeleted() override;
217 216
218 // Tracker for search requests to the history service. 217 // Tracker for search requests to the history service.
219 base::CancelableTaskTracker query_task_tracker_; 218 base::CancelableTaskTracker query_task_tracker_;
220 219
221 // The currently-executing request for synced history results. 220 // The currently-executing request for synced history results.
222 // Deleting the request will cancel it. 221 // Deleting the request will cancel it.
223 std::unique_ptr<history::WebHistoryService::Request> web_history_request_; 222 std::unique_ptr<history::WebHistoryService::Request> web_history_request_;
224 223
225 // True if there is a pending delete requests to the history service. 224 // True if there is a pending delete requests to the history service.
226 bool has_pending_delete_request_; 225 bool has_pending_delete_request_;
227 226
228 // Tracker for delete requests to the history service. 227 // Tracker for delete requests to the history service.
229 base::CancelableTaskTracker delete_task_tracker_; 228 base::CancelableTaskTracker delete_task_tracker_;
230 229
231 // The list of URLs that are in the process of being deleted. 230 // The list of URLs that are in the process of being deleted.
232 std::set<GURL> urls_to_be_deleted_; 231 std::set<GURL> urls_to_be_deleted_;
233 232
234 // The info value that is returned to the front end with the query results. 233 // The info value that is returned to the handler with the query results.
235 base::DictionaryValue results_info_value_; 234 BrowsingHistoryService::QueryResultsInfo query_results_info_;
236 235
237 // The list of query results received from the history service. 236 // The list of query results received from the history service.
238 std::vector<HistoryEntry> query_results_; 237 std::vector<HistoryEntry> query_results_;
239 238
240 // The list of query results received from the history server. 239 // The list of query results received from the history server.
241 std::vector<HistoryEntry> web_history_query_results_; 240 std::vector<HistoryEntry> web_history_query_results_;
242 241
243 // Timer used to implement a timeout on a Web History response. 242 // Timer used to implement a timeout on a Web History response.
244 base::OneShotTimer web_history_timer_; 243 base::OneShotTimer web_history_timer_;
245 244
246 // HistoryService (local history) observer. 245 // HistoryService (local history) observer.
247 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 246 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
248 history_service_observer_; 247 history_service_observer_;
249 248
250 // WebHistoryService (synced history) observer. 249 // WebHistoryService (synced history) observer.
251 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver> 250 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver>
252 web_history_service_observer_; 251 web_history_service_observer_;
253 252
254 // ProfileSyncService observer listens to late initialization of history sync. 253 // ProfileSyncService observer listens to late initialization of history sync.
255 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver> 254 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver>
256 sync_service_observer_; 255 sync_service_observer_;
257 256
258 // Whether the last call to Web History returned synced results. 257 // Whether the last call to Web History returned synced results.
259 bool has_synced_results_; 258 bool has_synced_results_;
260 259
261 // Whether there are other forms of browsing history on the history server. 260 // Whether there are other forms of browsing history on the history server.
262 bool has_other_forms_of_browsing_history_; 261 bool has_other_forms_of_browsing_history_;
263 262
264 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_; 263 Profile* profile_;
265 264
266 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); 265 BrowsingHistoryServiceHandler* handler_;
266
267 base::WeakPtrFactory<BrowsingHistoryService> weak_factory_;
268
269 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryService);
267 }; 270 };
268 271
269 #endif // CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ 272 #endif // CHROME_BROWSER_HISTORY_BROWSING_HISTORY_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/history/browsing_history_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698