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

Side by Side Diff: chrome/browser/ui/webui/browsing_history_handler.h

Issue 2450453002: Refactor BrowsingHistoryHandler, create BrowsingHistoryService (Closed)
Patch Set: Add back #include<utility> 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_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"
17 #include "base/scoped_observer.h"
18 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
19 #include "base/task/cancelable_task_tracker.h"
20 #include "base/timer/timer.h"
21 #include "base/values.h" 17 #include "base/values.h"
22 #include "components/history/core/browser/history_service_observer.h" 18 #include "chrome/browser/ui/history_ui_service.h"
23 #include "components/history/core/browser/url_row.h"
24 #include "components/history/core/browser/web_history_service.h"
25 #include "components/history/core/browser/web_history_service_observer.h"
26 #include "components/sync/driver/sync_service_observer.h"
27 #include "content/public/browser/web_ui_message_handler.h" 19 #include "content/public/browser/web_ui_message_handler.h"
28 #include "url/gurl.h"
29 20
30 class SupervisedUserService; 21 class SupervisedUserService;
31 22
32 namespace bookmarks { 23 namespace bookmarks {
33 class BookmarkModel; 24 class BookmarkModel;
34 } // namespace bookmarks 25 } // namespace bookmarks
35 26
36 namespace browser_sync {
37 class ProfileSyncService;
38 } // namespace browser_sync
39
40 namespace history { 27 namespace history {
41 class HistoryService;
42 class QueryResults;
43 struct QueryOptions; 28 struct QueryOptions;
44 } // namespace history 29 } // namespace history
45 30
46 namespace syncer {
47 class SyncServiceObserver;
48 } // namespace syncer
49
50 // The handler for Javascript messages related to the "history" view. 31 // The handler for Javascript messages related to the "history" view.
51 class BrowsingHistoryHandler : public content::WebUIMessageHandler, 32 class BrowsingHistoryHandler :
52 public history::HistoryServiceObserver, 33 public content::WebUIMessageHandler,
53 public history::WebHistoryServiceObserver, 34 public HistoryUiService::HistoryUiServiceHandler {
54 public syncer::SyncServiceObserver {
55 public: 35 public:
56 // 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,
58 // since only the most recent visit on a particular day is shown.
59 struct HistoryEntry {
60 // Values indicating whether an entry represents only local visits, only
61 // remote visits, or a mixture of both.
62 enum EntryType {
63 EMPTY_ENTRY = 0,
64 LOCAL_ENTRY,
65 REMOTE_ENTRY,
66 COMBINED_ENTRY
67 };
68
69 HistoryEntry(EntryType type, const GURL& url, const base::string16& title,
70 base::Time time, const std::string& client_id,
71 bool is_search_result, const base::string16& snippet,
72 bool blocked_visit);
73 HistoryEntry();
74 HistoryEntry(const HistoryEntry& other);
75 virtual ~HistoryEntry();
76
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.
89 static bool SortByTimeDescending(
90 const HistoryEntry& entry1, const HistoryEntry& entry2);
91
92 // The type of visits this entry represents: local, remote, or both.
93 EntryType entry_type;
94
95 GURL url;
96 base::string16 title; // Title of the entry. May be empty.
97
98 // The time of the entry. Usually this will be the time of the most recent
99 // visit to |url| on a particular day as defined in the local timezone.
100 base::Time time;
101
102 // The sync ID of the client on which the most recent visit occurred.
103 std::string client_id;
104
105 // Timestamps of all local or remote visits the same URL on the same day.
106 std::set<int64_t> all_timestamps;
107
108 // If true, this entry is a search result.
109 bool is_search_result;
110
111 // The entry's search snippet, if this entry is a search result.
112 base::string16 snippet;
113
114 // Whether this entry was blocked when it was attempted.
115 bool blocked_visit;
116 };
117
118 BrowsingHistoryHandler(); 36 BrowsingHistoryHandler();
119 ~BrowsingHistoryHandler() override; 37 ~BrowsingHistoryHandler() override;
120 38
121 // WebUIMessageHandler implementation. 39 // WebUIMessageHandler implementation.
122 void RegisterMessages() override; 40 void RegisterMessages() override;
123 41
124 // SyncServiceObserver implementation.
125 void OnStateChanged() override;
126
127 // Handler for the "queryHistory" message. 42 // Handler for the "queryHistory" message.
128 void HandleQueryHistory(const base::ListValue* args); 43 void HandleQueryHistory(const base::ListValue* args);
129 44
130 // Handler for the "removeVisits" message. 45 // Handler for the "removeVisits" message.
131 void HandleRemoveVisits(const base::ListValue* args); 46 void HandleRemoveVisits(const base::ListValue* args);
132 47
133 // Handler for "clearBrowsingData" message. 48 // Handler for "clearBrowsingData" message.
134 void HandleClearBrowsingData(const base::ListValue* args); 49 void HandleClearBrowsingData(const base::ListValue* args);
135 50
136 // Handler for "removeBookmark" message. 51 // Handler for "removeBookmark" message.
137 void HandleRemoveBookmark(const base::ListValue* args); 52 void HandleRemoveBookmark(const base::ListValue* args);
138 53
139 // Merges duplicate entries from the query results, only retaining the most 54 // HistoryUiServiceHandler implementation.
140 // recent visit to a URL on a particular day. That visit contains the 55 void OnQueryComplete(
141 // timestamps of the other visits. 56 std::vector<HistoryUiService::HistoryEntry>* results,
142 static void MergeDuplicateResults( 57 HistoryUiService::QueryResultsInfo* query_results_info) override;
143 std::vector<BrowsingHistoryHandler::HistoryEntry>* results); 58 void OnRemoveVisitsComplete() override;
144 59 void OnRemoveVisitsFailed() override;
145 protected: 60 void HistoryDeleted() override;
146 // Callback from the history system when a history query has completed. 61 void HasOtherFormsOfBrowsingHistory(
147 // Exposed for testing. 62 bool has_other_forms, bool has_synced_results) override;
148 void QueryComplete(const base::string16& search_text,
149 const history::QueryOptions& options,
150 history::QueryResults* results);
151 63
152 private: 64 private:
153 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest, 65 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest,
154 ObservingWebHistoryDeletions); 66 ObservingWebHistoryDeletions);
67 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest,
68 MdTruncatesTitles);
155 69
156 // The range for which to return results: 70 // The range for which to return results:
157 // - ALLTIME: allows access to all the results in a paginated way. 71 // - ALLTIME: allows access to all the results in a paginated way.
158 // - WEEK: the last 7 days. 72 // - WEEK: the last 7 days.
159 // - MONTH: the last calendar month. 73 // - MONTH: the last calendar month.
160 enum Range { 74 enum Range {
161 ALL_TIME = 0, 75 ALL_TIME = 0,
162 WEEK = 1, 76 WEEK = 1,
163 MONTH = 2 77 MONTH = 2
164 }; 78 };
165 79
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
171 // server, and sends the combined results to the front end.
172 void ReturnResultsToFrontEnd();
173
174 // Callback from |web_history_timer_| when a response from web history has
175 // not been received in time.
176 void WebHistoryTimeout();
177
178 // Callback from the WebHistoryService when a query has completed.
179 void WebHistoryQueryComplete(const base::string16& search_text,
180 const history::QueryOptions& options,
181 base::TimeTicks start_time,
182 history::WebHistoryService::Request* request,
183 const base::DictionaryValue* results_value);
184
185 // Callback telling us whether other forms of browsing history were found
186 // on the history server.
187 void OtherFormsOfBrowsingHistoryQueryComplete(
188 bool found_other_forms_of_browsing_history);
189
190 // Callback from the history system when visits were deleted.
191 void RemoveComplete();
192
193 // Callback from history server when visits were deleted.
194 void RemoveWebHistoryComplete(bool success);
195
196 bool ExtractIntegerValueAtIndex( 80 bool ExtractIntegerValueAtIndex(
197 const base::ListValue* value, int index, int* out_int); 81 const base::ListValue* value, int index, int* out_int);
198 82
199 // Sets the query options for a week-wide query, |offset| weeks ago. 83 // Sets the query options for a week-wide query, |offset| weeks ago.
200 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options); 84 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options);
201 85
202 // Sets the query options for a monthly query, |offset| months ago. 86 // Sets the query options for a monthly query, |offset| months ago.
203 void SetQueryTimeInMonths(int offset, history::QueryOptions* options); 87 void SetQueryTimeInMonths(int offset, history::QueryOptions* options);
204 88
205 // kAcceptLanguages pref value. 89 std::unique_ptr<HistoryUiService> history_ui_service_;
206 std::string GetAcceptLanguages() const;
207
208 // history::HistoryServiceObserver:
209 void OnURLsDeleted(history::HistoryService* history_service,
210 bool all_history,
211 bool expired,
212 const history::URLRows& deleted_rows,
213 const std::set<GURL>& favicon_urls) override;
214
215 // history::WebHistoryServiceObserver:
216 void OnWebHistoryDeleted() override;
217
218 // Tracker for search requests to the history service.
219 base::CancelableTaskTracker query_task_tracker_;
220
221 // The currently-executing request for synced history results.
222 // Deleting the request will cancel it.
223 std::unique_ptr<history::WebHistoryService::Request> web_history_request_;
224
225 // True if there is a pending delete requests to the history service.
226 bool has_pending_delete_request_;
227
228 // Tracker for delete requests to the history service.
229 base::CancelableTaskTracker delete_task_tracker_;
230
231 // The list of URLs that are in the process of being deleted.
232 std::set<GURL> urls_to_be_deleted_;
233
234 // The info value that is returned to the front end with the query results.
235 base::DictionaryValue results_info_value_;
236
237 // The list of query results received from the history service.
238 std::vector<HistoryEntry> query_results_;
239
240 // The list of query results received from the history server.
241 std::vector<HistoryEntry> web_history_query_results_;
242
243 // Timer used to implement a timeout on a Web History response.
244 base::OneShotTimer web_history_timer_;
245
246 // HistoryService (local history) observer.
247 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
248 history_service_observer_;
249
250 // WebHistoryService (synced history) observer.
251 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver>
252 web_history_service_observer_;
253
254 // ProfileSyncService observer listens to late initialization of history sync.
255 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver>
256 sync_service_observer_;
257
258 // Whether the last call to Web History returned synced results.
259 bool has_synced_results_;
260
261 // Whether there are other forms of browsing history on the history server.
262 bool has_other_forms_of_browsing_history_;
263
264 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_;
265 90
266 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); 91 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler);
267 }; 92 };
268 93
269 #endif // CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ 94 #endif // CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698