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 "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"
16
17class GURL;
18class PageUsageData;
19
20namespace base {
21class ListValue;
22class Value;
23}
24
25namespace user_prefs {
26class PrefRegistrySyncable;
27}
28
29// The handler for Javascript messages related to the "most visited" view.
30//
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
34//   string).
35class MostVisitedHandler : public content::WebUIMessageHandler,
36                           public content::NotificationObserver {
37 public:
38
39  MostVisitedHandler();
40  virtual ~MostVisitedHandler();
41
42  // WebUIMessageHandler override and implementation.
43  virtual void RegisterMessages() OVERRIDE;
44
45  // Callback for the "getMostVisited" message.
46  void HandleGetMostVisited(const base::ListValue* args);
47
48  // Callback for the "blacklistURLFromMostVisited" message.
49  void HandleBlacklistUrl(const base::ListValue* args);
50
51  // Callback for the "removeURLsFromMostVisitedBlacklist" message.
52  void HandleRemoveUrlsFromBlacklist(const base::ListValue* args);
53
54  // Callback for the "clearMostVisitedURLsBlacklist" message.
55  void HandleClearBlacklist(const base::ListValue* args);
56
57  // Callback for the "mostVisitedAction" message.
58  void HandleMostVisitedAction(const base::ListValue* args);
59
60  // Callback for the "mostVisitedSelected" message.
61  void HandleMostVisitedSelected(const base::ListValue* args);
62
63  // content::NotificationObserver implementation.
64  virtual void Observe(int type,
65                       const content::NotificationSource& source,
66                       const content::NotificationDetails& details) OVERRIDE;
67
68  const std::vector<GURL>& most_visited_urls() const {
69    return most_visited_urls_;
70  }
71
72  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
73
74 private:
75  struct MostVisitedPage;
76
77  // Send a request to the HistoryService to get the most visited pages.
78  void StartQueryForMostVisited();
79
80  // Sets pages_value_ from a format produced by TopSites.
81  void SetPagesValueFromTopSites(const history::MostVisitedURLList& data);
82
83  // Callback for TopSites.
84  void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& data);
85
86  // Puts the passed URL in the blacklist (so it does not show as a thumbnail).
87  void BlacklistUrl(const GURL& url);
88
89  // Returns the key used in url_blacklist_ for the passed |url|.
90  std::string GetDictionaryKeyForUrl(const std::string& url);
91
92  // Sends pages_value_ to the javascript side and resets page_value_.
93  void SendPagesValue();
94
95  content::NotificationRegistrar registrar_;
96
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_;
101
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_;
105
106  // Keep the results of the db query here.
107  scoped_ptr<base::ListValue> pages_value_;
108
109  // Whether the user has viewed the 'most visited' pane.
110  bool most_visited_viewed_;
111
112  // Whether the user has performed a "tracked" action to leave the page or not.
113  bool user_action_logged_;
114
115  // For callbacks which may be run after destruction.
116  base::WeakPtrFactory<MostVisitedHandler> weak_ptr_factory_;
117
118  DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler);
119};
120
121#endif  // CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_
122