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