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