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