OLD | NEW |
| (Empty) |
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_NTP_MOST_VISITED_HANDLER_H_ | |
6 #define CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/scoped_observer.h" | |
13 #include "components/history/core/browser/history_types.h" | |
14 #include "components/history/core/browser/top_sites_observer.h" | |
15 #include "content/public/browser/web_ui_message_handler.h" | |
16 | |
17 class GURL; | |
18 | |
19 namespace base { | |
20 class ListValue; | |
21 class Value; | |
22 } | |
23 | |
24 namespace user_prefs { | |
25 class PrefRegistrySyncable; | |
26 } | |
27 | |
28 // The handler for Javascript messages related to the "most visited" view. | |
29 // | |
30 // This class manages one preference: | |
31 // - The URL blacklist: URLs we do not want to show in the thumbnails list. It | |
32 // is a dictionary for quick access (it associates a dummy boolean to the URL | |
33 // string). | |
34 class MostVisitedHandler : public content::WebUIMessageHandler, | |
35 public history::TopSitesObserver { | |
36 public: | |
37 | |
38 MostVisitedHandler(); | |
39 ~MostVisitedHandler() override; | |
40 | |
41 // WebUIMessageHandler override and implementation. | |
42 void RegisterMessages() override; | |
43 | |
44 // Callback for the "getMostVisited" message. | |
45 void HandleGetMostVisited(const base::ListValue* args); | |
46 | |
47 // Callback for the "blacklistURLFromMostVisited" message. | |
48 void HandleBlacklistUrl(const base::ListValue* args); | |
49 | |
50 // Callback for the "removeURLsFromMostVisitedBlacklist" message. | |
51 void HandleRemoveUrlsFromBlacklist(const base::ListValue* args); | |
52 | |
53 // Callback for the "clearMostVisitedURLsBlacklist" message. | |
54 void HandleClearBlacklist(const base::ListValue* args); | |
55 | |
56 // Callback for the "mostVisitedAction" message. | |
57 void HandleMostVisitedAction(const base::ListValue* args); | |
58 | |
59 // Callback for the "mostVisitedSelected" message. | |
60 void HandleMostVisitedSelected(const base::ListValue* args); | |
61 | |
62 const std::vector<GURL>& most_visited_urls() const { | |
63 return most_visited_urls_; | |
64 } | |
65 | |
66 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
67 | |
68 private: | |
69 struct MostVisitedPage; | |
70 | |
71 // Send a request to the HistoryService to get the most visited pages. | |
72 void StartQueryForMostVisited(); | |
73 | |
74 // Sets pages_value_ from a format produced by TopSites. | |
75 void SetPagesValueFromTopSites(const history::MostVisitedURLList& data); | |
76 | |
77 // Callback for TopSites. | |
78 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& data); | |
79 | |
80 // Puts the passed URL in the blacklist (so it does not show as a thumbnail). | |
81 void BlacklistUrl(const GURL& url); | |
82 | |
83 // Returns the key used in url_blacklist_ for the passed |url|. | |
84 std::string GetDictionaryKeyForUrl(const std::string& url); | |
85 | |
86 // Sends pages_value_ to the javascript side and resets page_value_. | |
87 void SendPagesValue(); | |
88 | |
89 // history::TopSitesObserver implementation. | |
90 void TopSitesLoaded(history::TopSites* top_sites) override; | |
91 void TopSitesChanged(history::TopSites* top_sites) override; | |
92 | |
93 // Scoped observer to help with TopSitesObserver registration. | |
94 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_; | |
95 | |
96 // The most visited URLs, in priority order. | |
97 // Only used for matching up clicks on the page to which most visited entry | |
98 // was clicked on for metrics purposes. | |
99 std::vector<GURL> most_visited_urls_; | |
100 | |
101 // We pre-fetch the first set of result pages. This variable is false until | |
102 // we get the first getMostVisited() call. | |
103 bool got_first_most_visited_request_; | |
104 | |
105 // Keep the results of the db query here. | |
106 scoped_ptr<base::ListValue> pages_value_; | |
107 | |
108 // Whether the user has viewed the 'most visited' pane. | |
109 bool most_visited_viewed_; | |
110 | |
111 // Whether the user has performed a "tracked" action to leave the page or not. | |
112 bool user_action_logged_; | |
113 | |
114 // For callbacks which may be run after destruction. | |
115 base::WeakPtrFactory<MostVisitedHandler> weak_ptr_factory_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler); | |
118 }; | |
119 | |
120 #endif // CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_ | |
OLD | NEW |