Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_UI_HISTORY_UI_SERVICE_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_HISTORY_UI_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 |
| 53 public history::WebHistoryServiceObserver, | 46 // history and provide results to the associated HistoryUiServiceHandler. |
| 54 public syncer::SyncServiceObserver { | 47 class HistoryUiService : public history::HistoryServiceObserver, |
|
sky
2016/11/07 20:42:52
Is there a reason not to keep this code in the web
Theresa
2016/11/07 21:02:15
I'm working on changing Android to have its own hi
| |
| 48 public history::WebHistoryServiceObserver, | |
| 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 Loading... | |
| 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 HistoryUiService. | |
| 124 class HistoryUiServiceHandler { | |
| 125 public: | |
| 126 // Callback for QueryHistory(). | |
| 127 virtual void OnQueryComplete( | |
| 128 std::vector<HistoryUiService::HistoryEntry>* results, | |
| 129 HistoryUiService::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 }; | |
| 146 | |
| 147 HistoryUiService(Profile* profile, | |
| 148 HistoryUiService::HistoryUiServiceHandler* handler); | |
| 149 ~HistoryUiService() override; | |
| 150 | |
| 151 // Core implementation of history querying. | |
| 152 void QueryHistory(const base::string16& search_text, | |
| 153 const history::QueryOptions& options); | |
| 154 | |
| 155 // Removes |items| from history. | |
| 156 void RemoveVisits( | |
| 157 std::vector<std::unique_ptr<HistoryUiService::HistoryEntry>>* items); | |
| 123 | 158 |
| 124 // SyncServiceObserver implementation. | 159 // SyncServiceObserver implementation. |
| 125 void OnStateChanged() override; | 160 void OnStateChanged() override; |
| 126 | 161 |
| 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 | 162 // 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 | 163 // recent visit to a URL on a particular day. That visit contains the |
| 141 // timestamps of the other visits. | 164 // timestamps of the other visits. |
| 142 static void MergeDuplicateResults( | 165 static void MergeDuplicateResults( |
| 143 std::vector<BrowsingHistoryHandler::HistoryEntry>* results); | 166 std::vector<HistoryUiService::HistoryEntry>* results); |
| 144 | 167 |
| 145 protected: | |
| 146 // Callback from the history system when a history query has completed. | 168 // Callback from the history system when a history query has completed. |
| 147 // Exposed for testing. | 169 // Exposed for testing. |
| 148 void QueryComplete(const base::string16& search_text, | 170 void QueryComplete(const base::string16& search_text, |
| 149 const history::QueryOptions& options, | 171 const history::QueryOptions& options, |
| 150 history::QueryResults* results); | 172 history::QueryResults* results); |
| 151 | 173 |
| 152 private: | 174 private: |
| 153 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest, | 175 FRIEND_TEST_ALL_PREFIXES(BrowsingHistoryHandlerTest, |
| 154 ObservingWebHistoryDeletions); | 176 ObservingWebHistoryDeletions); |
| 155 | 177 |
| 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 | 178 // Combines the query results from the local history database and the history |
| 171 // server, and sends the combined results to the front end. | 179 // server, and sends the combined results to the HistoryUiServiceHandler. |
| 172 void ReturnResultsToFrontEnd(); | 180 void ReturnResultsToHandler(); |
| 173 | 181 |
| 174 // Callback from |web_history_timer_| when a response from web history has | 182 // Callback from |web_history_timer_| when a response from web history has |
| 175 // not been received in time. | 183 // not been received in time. |
| 176 void WebHistoryTimeout(); | 184 void WebHistoryTimeout(); |
| 177 | 185 |
| 178 // Callback from the WebHistoryService when a query has completed. | 186 // Callback from the WebHistoryService when a query has completed. |
| 179 void WebHistoryQueryComplete(const base::string16& search_text, | 187 void WebHistoryQueryComplete(const base::string16& search_text, |
| 180 const history::QueryOptions& options, | 188 const history::QueryOptions& options, |
| 181 base::TimeTicks start_time, | 189 base::TimeTicks start_time, |
| 182 history::WebHistoryService::Request* request, | 190 history::WebHistoryService::Request* request, |
| 183 const base::DictionaryValue* results_value); | 191 const base::DictionaryValue* results_value); |
| 184 | 192 |
| 185 // Callback telling us whether other forms of browsing history were found | 193 // Callback telling us whether other forms of browsing history were found |
| 186 // on the history server. | 194 // on the history server. |
| 187 void OtherFormsOfBrowsingHistoryQueryComplete( | 195 void OtherFormsOfBrowsingHistoryQueryComplete( |
| 188 bool found_other_forms_of_browsing_history); | 196 bool found_other_forms_of_browsing_history); |
| 189 | 197 |
| 190 // Callback from the history system when visits were deleted. | 198 // Callback from the history system when visits were deleted. |
| 191 void RemoveComplete(); | 199 void RemoveComplete(); |
| 192 | 200 |
| 193 // Callback from history server when visits were deleted. | 201 // Callback from history server when visits were deleted. |
| 194 void RemoveWebHistoryComplete(bool success); | 202 void RemoveWebHistoryComplete(bool success); |
| 195 | 203 |
| 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: | 204 // history::HistoryServiceObserver: |
| 209 void OnURLsDeleted(history::HistoryService* history_service, | 205 void OnURLsDeleted(history::HistoryService* history_service, |
| 210 bool all_history, | 206 bool all_history, |
| 211 bool expired, | 207 bool expired, |
| 212 const history::URLRows& deleted_rows, | 208 const history::URLRows& deleted_rows, |
| 213 const std::set<GURL>& favicon_urls) override; | 209 const std::set<GURL>& favicon_urls) override; |
| 214 | 210 |
| 215 // history::WebHistoryServiceObserver: | 211 // history::WebHistoryServiceObserver: |
| 216 void OnWebHistoryDeleted() override; | 212 void OnWebHistoryDeleted() override; |
| 217 | 213 |
| 218 // Tracker for search requests to the history service. | 214 // Tracker for search requests to the history service. |
| 219 base::CancelableTaskTracker query_task_tracker_; | 215 base::CancelableTaskTracker query_task_tracker_; |
| 220 | 216 |
| 221 // The currently-executing request for synced history results. | 217 // The currently-executing request for synced history results. |
| 222 // Deleting the request will cancel it. | 218 // Deleting the request will cancel it. |
| 223 std::unique_ptr<history::WebHistoryService::Request> web_history_request_; | 219 std::unique_ptr<history::WebHistoryService::Request> web_history_request_; |
| 224 | 220 |
| 225 // True if there is a pending delete requests to the history service. | 221 // True if there is a pending delete requests to the history service. |
| 226 bool has_pending_delete_request_; | 222 bool has_pending_delete_request_; |
| 227 | 223 |
| 228 // Tracker for delete requests to the history service. | 224 // Tracker for delete requests to the history service. |
| 229 base::CancelableTaskTracker delete_task_tracker_; | 225 base::CancelableTaskTracker delete_task_tracker_; |
| 230 | 226 |
| 231 // The list of URLs that are in the process of being deleted. | 227 // The list of URLs that are in the process of being deleted. |
| 232 std::set<GURL> urls_to_be_deleted_; | 228 std::set<GURL> urls_to_be_deleted_; |
| 233 | 229 |
| 234 // The info value that is returned to the front end with the query results. | 230 // The info value that is returned to the handler with the query results. |
| 235 base::DictionaryValue results_info_value_; | 231 HistoryUiService::QueryResultsInfo query_results_info_; |
| 236 | 232 |
| 237 // The list of query results received from the history service. | 233 // The list of query results received from the history service. |
| 238 std::vector<HistoryEntry> query_results_; | 234 std::vector<HistoryEntry> query_results_; |
| 239 | 235 |
| 240 // The list of query results received from the history server. | 236 // The list of query results received from the history server. |
| 241 std::vector<HistoryEntry> web_history_query_results_; | 237 std::vector<HistoryEntry> web_history_query_results_; |
| 242 | 238 |
| 243 // Timer used to implement a timeout on a Web History response. | 239 // Timer used to implement a timeout on a Web History response. |
| 244 base::OneShotTimer web_history_timer_; | 240 base::OneShotTimer web_history_timer_; |
| 245 | 241 |
| 246 // HistoryService (local history) observer. | 242 // HistoryService (local history) observer. |
| 247 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 243 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 248 history_service_observer_; | 244 history_service_observer_; |
| 249 | 245 |
| 250 // WebHistoryService (synced history) observer. | 246 // WebHistoryService (synced history) observer. |
| 251 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver> | 247 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver> |
| 252 web_history_service_observer_; | 248 web_history_service_observer_; |
| 253 | 249 |
| 254 // ProfileSyncService observer listens to late initialization of history sync. | 250 // ProfileSyncService observer listens to late initialization of history sync. |
| 255 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver> | 251 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver> |
| 256 sync_service_observer_; | 252 sync_service_observer_; |
| 257 | 253 |
| 258 // Whether the last call to Web History returned synced results. | 254 // Whether the last call to Web History returned synced results. |
| 259 bool has_synced_results_; | 255 bool has_synced_results_; |
| 260 | 256 |
| 261 // Whether there are other forms of browsing history on the history server. | 257 // Whether there are other forms of browsing history on the history server. |
| 262 bool has_other_forms_of_browsing_history_; | 258 bool has_other_forms_of_browsing_history_; |
| 263 | 259 |
| 264 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_; | 260 Profile* profile_; |
| 265 | 261 |
| 266 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); | 262 HistoryUiServiceHandler* handler_; |
| 263 | |
| 264 base::WeakPtrFactory<HistoryUiService> weak_factory_; | |
| 265 | |
| 266 DISALLOW_COPY_AND_ASSIGN(HistoryUiService); | |
| 267 }; | 267 }; |
| 268 | 268 |
| 269 #endif // CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ | 269 #endif // CHROME_BROWSER_UI_HISTORY_UI_SERVICE_H_ |
| OLD | NEW |