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, |
| 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 { |
|
Theresa
2016/10/24 15:40:29
This new struct is used to pass information about
| |
| 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 { | |
|
Theresa
2016/10/24 15:40:29
Places where BrowsingHistoryHandler called JS meth
| |
| 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 // The range for which to return results: | |
| 154 // - ALLTIME: allows access to all the results in a paginated way. | |
| 155 // - WEEK: the last 7 days. | |
| 156 // - MONTH: the last calendar month. | |
| 157 enum Range { | |
| 158 ALL_TIME = 0, | |
| 159 WEEK = 1, | |
| 160 MONTH = 2 | |
| 161 }; | |
| 162 | |
| 163 // Core implementation of history querying. | |
| 164 void QueryHistory(const base::string16& search_text, | |
| 165 const history::QueryOptions& options); | |
| 166 | |
| 167 // Combines the query results from the local history database and the history | 175 // Combines the query results from the local history database and the history |
| 168 // server, and sends the combined results to the front end. | 176 // server, and sends the combined results to the HistoryUiServiceHandler. |
| 169 void ReturnResultsToFrontEnd(); | 177 void ReturnResultsToHandler(); |
| 170 | 178 |
| 171 // Callback from |web_history_timer_| when a response from web history has | 179 // Callback from |web_history_timer_| when a response from web history has |
| 172 // not been received in time. | 180 // not been received in time. |
| 173 void WebHistoryTimeout(); | 181 void WebHistoryTimeout(); |
| 174 | 182 |
| 175 // Callback from the WebHistoryService when a query has completed. | 183 // Callback from the WebHistoryService when a query has completed. |
| 176 void WebHistoryQueryComplete(const base::string16& search_text, | 184 void WebHistoryQueryComplete(const base::string16& search_text, |
| 177 const history::QueryOptions& options, | 185 const history::QueryOptions& options, |
| 178 base::TimeTicks start_time, | 186 base::TimeTicks start_time, |
| 179 history::WebHistoryService::Request* request, | 187 history::WebHistoryService::Request* request, |
| 180 const base::DictionaryValue* results_value); | 188 const base::DictionaryValue* results_value); |
| 181 | 189 |
| 182 // Callback telling us whether other forms of browsing history were found | 190 // Callback telling us whether other forms of browsing history were found |
| 183 // on the history server. | 191 // on the history server. |
| 184 void OtherFormsOfBrowsingHistoryQueryComplete( | 192 void OtherFormsOfBrowsingHistoryQueryComplete( |
| 185 bool found_other_forms_of_browsing_history); | 193 bool found_other_forms_of_browsing_history); |
| 186 | 194 |
| 187 // Callback from the history system when visits were deleted. | 195 // Callback from the history system when visits were deleted. |
| 188 void RemoveComplete(); | 196 void RemoveComplete(); |
| 189 | 197 |
| 190 // Callback from history server when visits were deleted. | 198 // Callback from history server when visits were deleted. |
| 191 void RemoveWebHistoryComplete(bool success); | 199 void RemoveWebHistoryComplete(bool success); |
| 192 | 200 |
| 193 bool ExtractIntegerValueAtIndex( | |
| 194 const base::ListValue* value, int index, int* out_int); | |
| 195 | |
| 196 // Sets the query options for a week-wide query, |offset| weeks ago. | |
| 197 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options); | |
| 198 | |
| 199 // Sets the query options for a monthly query, |offset| months ago. | |
| 200 void SetQueryTimeInMonths(int offset, history::QueryOptions* options); | |
| 201 | |
| 202 // kAcceptLanguages pref value. | |
| 203 std::string GetAcceptLanguages() const; | |
| 204 | |
| 205 // history::HistoryServiceObserver: | 201 // history::HistoryServiceObserver: |
| 206 void OnURLsDeleted(history::HistoryService* history_service, | 202 void OnURLsDeleted(history::HistoryService* history_service, |
| 207 bool all_history, | 203 bool all_history, |
| 208 bool expired, | 204 bool expired, |
| 209 const history::URLRows& deleted_rows, | 205 const history::URLRows& deleted_rows, |
| 210 const std::set<GURL>& favicon_urls) override; | 206 const std::set<GURL>& favicon_urls) override; |
| 211 | 207 |
| 212 // history::WebHistoryServiceObserver: | 208 // history::WebHistoryServiceObserver: |
| 213 void OnWebHistoryDeleted() override; | 209 void OnWebHistoryDeleted() override; |
| 214 | 210 |
| 215 // Tracker for search requests to the history service. | 211 // Tracker for search requests to the history service. |
| 216 base::CancelableTaskTracker query_task_tracker_; | 212 base::CancelableTaskTracker query_task_tracker_; |
| 217 | 213 |
| 218 // The currently-executing request for synced history results. | 214 // The currently-executing request for synced history results. |
| 219 // Deleting the request will cancel it. | 215 // Deleting the request will cancel it. |
| 220 std::unique_ptr<history::WebHistoryService::Request> web_history_request_; | 216 std::unique_ptr<history::WebHistoryService::Request> web_history_request_; |
| 221 | 217 |
| 222 // True if there is a pending delete requests to the history service. | 218 // True if there is a pending delete requests to the history service. |
| 223 bool has_pending_delete_request_; | 219 bool has_pending_delete_request_; |
| 224 | 220 |
| 225 // Tracker for delete requests to the history service. | 221 // Tracker for delete requests to the history service. |
| 226 base::CancelableTaskTracker delete_task_tracker_; | 222 base::CancelableTaskTracker delete_task_tracker_; |
| 227 | 223 |
| 228 // The list of URLs that are in the process of being deleted. | 224 // The list of URLs that are in the process of being deleted. |
| 229 std::set<GURL> urls_to_be_deleted_; | 225 std::set<GURL> urls_to_be_deleted_; |
| 230 | 226 |
| 231 // The info value that is returned to the front end with the query results. | 227 // The info value that is returned to the handler with the query results. |
| 232 base::DictionaryValue results_info_value_; | 228 HistoryUiService::QueryResultsInfo query_results_info_; |
| 233 | 229 |
| 234 // The list of query results received from the history service. | 230 // The list of query results received from the history service. |
| 235 std::vector<HistoryEntry> query_results_; | 231 std::vector<HistoryEntry> query_results_; |
| 236 | 232 |
| 237 // The list of query results received from the history server. | 233 // The list of query results received from the history server. |
| 238 std::vector<HistoryEntry> web_history_query_results_; | 234 std::vector<HistoryEntry> web_history_query_results_; |
| 239 | 235 |
| 240 // Timer used to implement a timeout on a Web History response. | 236 // Timer used to implement a timeout on a Web History response. |
| 241 base::OneShotTimer web_history_timer_; | 237 base::OneShotTimer web_history_timer_; |
| 242 | 238 |
| 243 // HistoryService (local history) observer. | 239 // HistoryService (local history) observer. |
| 244 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 240 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 245 history_service_observer_; | 241 history_service_observer_; |
| 246 | 242 |
| 247 // WebHistoryService (synced history) observer. | 243 // WebHistoryService (synced history) observer. |
| 248 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver> | 244 ScopedObserver<history::WebHistoryService, history::WebHistoryServiceObserver> |
| 249 web_history_service_observer_; | 245 web_history_service_observer_; |
| 250 | 246 |
| 251 // ProfileSyncService observer listens to late initialization of history sync. | 247 // ProfileSyncService observer listens to late initialization of history sync. |
| 252 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver> | 248 ScopedObserver<browser_sync::ProfileSyncService, syncer::SyncServiceObserver> |
| 253 sync_service_observer_; | 249 sync_service_observer_; |
| 254 | 250 |
| 255 // Whether the last call to Web History returned synced results. | 251 // Whether the last call to Web History returned synced results. |
| 256 bool has_synced_results_; | 252 bool has_synced_results_; |
| 257 | 253 |
| 258 // Whether there are other forms of browsing history on the history server. | 254 // Whether there are other forms of browsing history on the history server. |
| 259 bool has_other_forms_of_browsing_history_; | 255 bool has_other_forms_of_browsing_history_; |
| 260 | 256 |
| 261 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_; | 257 Profile* profile_; |
| 262 | 258 |
| 263 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); | 259 HistoryUiServiceHandler* handler_; |
| 260 | |
| 261 base::WeakPtrFactory<HistoryUiService> weak_factory_; | |
| 262 | |
| 263 DISALLOW_COPY_AND_ASSIGN(HistoryUiService); | |
| 264 }; | 264 }; |
| 265 | 265 |
| 266 #endif // CHROME_BROWSER_UI_WEBUI_BROWSING_HISTORY_HANDLER_H_ | 266 #endif // CHROME_BROWSER_UI_HISTORY_UI_SERVICE_H_ |
| OLD | NEW |