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