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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_
11 #include "base/memory/weak_ptr.h"
12 #include "components/history/core/browser/history_types.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_ui_message_handler.h"
25 namespace user_prefs {
26 class PrefRegistrySyncable;
29 // The handler for Javascript messages related to the "most visited" view.
31 // This class manages one preference:
32 // - The URL blacklist: URLs we do not want to show in the thumbnails list. It
33 // is a dictionary for quick access (it associates a dummy boolean to the URL
35 class MostVisitedHandler : public content::WebUIMessageHandler,
36 public content::NotificationObserver {
40 ~MostVisitedHandler() override;
42 // WebUIMessageHandler override and implementation.
43 void RegisterMessages() override;
45 // Callback for the "getMostVisited" message.
46 void HandleGetMostVisited(const base::ListValue* args);
48 // Callback for the "blacklistURLFromMostVisited" message.
49 void HandleBlacklistUrl(const base::ListValue* args);
51 // Callback for the "removeURLsFromMostVisitedBlacklist" message.
52 void HandleRemoveUrlsFromBlacklist(const base::ListValue* args);
54 // Callback for the "clearMostVisitedURLsBlacklist" message.
55 void HandleClearBlacklist(const base::ListValue* args);
57 // Callback for the "mostVisitedAction" message.
58 void HandleMostVisitedAction(const base::ListValue* args);
60 // Callback for the "mostVisitedSelected" message.
61 void HandleMostVisitedSelected(const base::ListValue* args);
63 // content::NotificationObserver implementation.
64 void Observe(int type,
65 const content::NotificationSource& source,
66 const content::NotificationDetails& details) override;
68 const std::vector<GURL>& most_visited_urls() const {
69 return most_visited_urls_;
72 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
75 struct MostVisitedPage;
77 // Send a request to the HistoryService to get the most visited pages.
78 void StartQueryForMostVisited();
80 // Sets pages_value_ from a format produced by TopSites.
81 void SetPagesValueFromTopSites(const history::MostVisitedURLList& data);
83 // Callback for TopSites.
84 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& data);
86 // Puts the passed URL in the blacklist (so it does not show as a thumbnail).
87 void BlacklistUrl(const GURL& url);
89 // Returns the key used in url_blacklist_ for the passed |url|.
90 std::string GetDictionaryKeyForUrl(const std::string& url);
92 // Sends pages_value_ to the javascript side and resets page_value_.
93 void SendPagesValue();
95 content::NotificationRegistrar registrar_;
97 // The most visited URLs, in priority order.
98 // Only used for matching up clicks on the page to which most visited entry
99 // was clicked on for metrics purposes.
100 std::vector<GURL> most_visited_urls_;
102 // We pre-fetch the first set of result pages. This variable is false until
103 // we get the first getMostVisited() call.
104 bool got_first_most_visited_request_;
106 // Keep the results of the db query here.
107 scoped_ptr<base::ListValue> pages_value_;
109 // Whether the user has viewed the 'most visited' pane.
110 bool most_visited_viewed_;
112 // Whether the user has performed a "tracked" action to leave the page or not.
113 bool user_action_logged_;
115 // For callbacks which may be run after destruction.
116 base::WeakPtrFactory<MostVisitedHandler> weak_ptr_factory_;
118 DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler);
121 #endif // CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_