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