Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_
      6 #define CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_
      7 
      8 #include "base/strings/string16.h"
      9 #include "base/timer/timer.h"
     10 #include "base/values.h"
     11 #include "chrome/browser/common/cancelable_request.h"
     12 #include "chrome/browser/history/history_service.h"
     13 #include "chrome/browser/history/web_history_service.h"
     14 #include "chrome/common/cancelable_task_tracker.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/web_ui_controller.h"
     17 #include "content/public/browser/web_ui_message_handler.h"
     18 
     19 class BookmarkModel;
     20 class ManagedUserService;
     21 class ProfileSyncService;
     22 
     23 // The handler for Javascript messages related to the "history" view.
     24 class BrowsingHistoryHandler : public content::WebUIMessageHandler,
     25                                public content::NotificationObserver {
     26  public:
     27   // Represents a history entry to be shown to the user, representing either
     28   // a local or remote visit. A single entry can represent multiple visits,
     29   // since only the most recent visit on a particular day is shown.
     30   struct HistoryEntry {
     31     // Values indicating whether an entry represents only local visits, only
     32     // remote visits, or a mixture of both.
     33     enum EntryType {
     34       EMPTY_ENTRY = 0,
     35       LOCAL_ENTRY,
     36       REMOTE_ENTRY,
     37       COMBINED_ENTRY
     38     };
     39 
     40     HistoryEntry(EntryType type, const GURL& url, const string16& title,
     41                  base::Time time, const std::string& client_id,
     42                  bool is_search_result, const string16& snippet,
     43                  bool blocked_visit);
     44     HistoryEntry();
     45     virtual ~HistoryEntry();
     46 
     47     // Formats this entry's URL and title and adds them to |result|.
     48     void SetUrlAndTitle(DictionaryValue* result) const;
     49 
     50     // Converts the entry to a DictionaryValue to be owned by the caller.
     51     scoped_ptr<DictionaryValue> ToValue(
     52         BookmarkModel* bookmark_model,
     53         ManagedUserService* managed_user_service,
     54         const ProfileSyncService* sync_service) const;
     55 
     56     // Comparison function for sorting HistoryEntries from newest to oldest.
     57     static bool SortByTimeDescending(
     58         const HistoryEntry& entry1, const HistoryEntry& entry2);
     59 
     60     // The type of visits this entry represents: local, remote, or both.
     61     EntryType entry_type;
     62 
     63     GURL url;
     64     string16 title;  // Title of the entry. May be empty.
     65 
     66     // The time of the entry. Usually this will be the time of the most recent
     67     // visit to |url| on a particular day as defined in the local timezone.
     68     base::Time time;
     69 
     70     // The sync ID of the client on which the most recent visit occurred.
     71     std::string client_id;
     72 
     73     // Timestamps of all local or remote visits the same URL on the same day.
     74     std::set<int64> all_timestamps;
     75 
     76     // If true, this entry is a search result.
     77     bool is_search_result;
     78 
     79     // The entry's search snippet, if this entry is a search result.
     80     string16 snippet;
     81 
     82     // Whether this entry was blocked when it was attempted.
     83     bool blocked_visit;
     84   };
     85 
     86   BrowsingHistoryHandler();
     87   virtual ~BrowsingHistoryHandler();
     88 
     89   // WebUIMessageHandler implementation.
     90   virtual void RegisterMessages() OVERRIDE;
     91 
     92   // Handler for the "queryHistory" message.
     93   void HandleQueryHistory(const base::ListValue* args);
     94 
     95   // Handler for the "removeVisits" message.
     96   void HandleRemoveVisits(const base::ListValue* args);
     97 
     98   // Handler for "clearBrowsingData" message.
     99   void HandleClearBrowsingData(const base::ListValue* args);
    100 
    101   // Handler for "removeBookmark" message.
    102   void HandleRemoveBookmark(const base::ListValue* args);
    103 
    104   // content::NotificationObserver implementation.
    105   virtual void Observe(int type,
    106                        const content::NotificationSource& source,
    107                        const content::NotificationDetails& details) OVERRIDE;
    108 
    109   // Merges duplicate entries from the query results, only retaining the most
    110   // recent visit to a URL on a particular day. That visit contains the
    111   // timestamps of the other visits.
    112   static void MergeDuplicateResults(
    113       std::vector<BrowsingHistoryHandler::HistoryEntry>* results);
    114 
    115  private:
    116   // The range for which to return results:
    117   // - ALLTIME: allows access to all the results in a paginated way.
    118   // - WEEK: the last 7 days.
    119   // - MONTH: the last calendar month.
    120   enum Range {
    121     ALL_TIME = 0,
    122     WEEK = 1,
    123     MONTH = 2
    124   };
    125 
    126   // Core implementation of history querying.
    127   void QueryHistory(string16 search_text, const history::QueryOptions& options);
    128 
    129   // Combines the query results from the local history database and the history
    130   // server, and sends the combined results to the front end.
    131   void ReturnResultsToFrontEnd();
    132 
    133   // Callback from |web_history_timer_| when a response from web history has
    134   // not been received in time.
    135   void WebHistoryTimeout();
    136 
    137   // Callback from the history system when a history query has completed.
    138   void QueryComplete(const string16& search_text,
    139                      const history::QueryOptions& options,
    140                      HistoryService::Handle request_handle,
    141                      history::QueryResults* results);
    142 
    143   // Callback from the WebHistoryService when a query has completed.
    144   void WebHistoryQueryComplete(const string16& search_text,
    145                                const history::QueryOptions& options,
    146                                base::TimeTicks start_time,
    147                                history::WebHistoryService::Request* request,
    148                                const base::DictionaryValue* results_value);
    149 
    150   // Callback from the history system when visits were deleted.
    151   void RemoveComplete();
    152 
    153   // Callback from history server when visits were deleted.
    154   void RemoveWebHistoryComplete(history::WebHistoryService::Request* request,
    155                                 bool success);
    156 
    157   bool ExtractIntegerValueAtIndex(
    158       const base::ListValue* value, int index, int* out_int);
    159 
    160   // Sets the query options for a week-wide query, |offset| weeks ago.
    161   void SetQueryTimeInWeeks(int offset, history::QueryOptions* options);
    162 
    163   // Sets the query options for a monthly query, |offset| months ago.
    164   void SetQueryTimeInMonths(int offset, history::QueryOptions* options);
    165 
    166   content::NotificationRegistrar registrar_;
    167 
    168   // Consumer for search requests to the history service.
    169   CancelableRequestConsumerT<int, 0> history_request_consumer_;
    170 
    171   // The currently-executing request for synced history results.
    172   // Deleting the request will cancel it.
    173   scoped_ptr<history::WebHistoryService::Request> web_history_request_;
    174 
    175   // The currently-executing delete request for synced history.
    176   // Deleting the request will cancel it.
    177   scoped_ptr<history::WebHistoryService::Request> web_history_delete_request_;
    178 
    179   // Tracker for delete requests to the history service.
    180   CancelableTaskTracker delete_task_tracker_;
    181 
    182   // The list of URLs that are in the process of being deleted.
    183   std::set<GURL> urls_to_be_deleted_;
    184 
    185   // The info value that is returned to the front end with the query results.
    186   base::DictionaryValue results_info_value_;
    187 
    188   // The list of query results received from the history service.
    189   std::vector<HistoryEntry> query_results_;
    190 
    191   // The list of query results received from the history server.
    192   std::vector<HistoryEntry> web_history_query_results_;
    193 
    194   // Timer used to implement a timeout on a Web History response.
    195   base::OneShotTimer<BrowsingHistoryHandler> web_history_timer_;
    196 
    197   DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler);
    198 };
    199 
    200 class HistoryUI : public content::WebUIController {
    201  public:
    202   explicit HistoryUI(content::WebUI* web_ui);
    203 
    204   // Return the URL for a given search term.
    205   static const GURL GetHistoryURLWithSearchText(const string16& text);
    206 
    207   static base::RefCountedMemory* GetFaviconResourceBytes(
    208       ui::ScaleFactor scale_factor);
    209 
    210  private:
    211   DISALLOW_COPY_AND_ASSIGN(HistoryUI);
    212 };
    213 
    214 #endif  // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_
    215