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 18 class GURL; 19 class PageUsageData; 20 21 namespace base { 22 class ListValue; 23 class Value; 24 } 25 26 namespace user_prefs { 27 class 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). 36 class 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 RegisterProfilePrefs(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 // Removes recommended URLs if a matching URL is already open in the Browser, 94 // if the Most Visited Tile Placement experiment is enabled, and the client is 95 // in the experiment group. 96 void MaybeRemovePageValues(); 97 98 // Sends pages_value_ to the javascript side and resets page_value_. 99 void SendPagesValue(); 100 101 content::NotificationRegistrar registrar_; 102 103 // For callbacks may be run after destruction. 104 base::WeakPtrFactory<MostVisitedHandler> weak_ptr_factory_; 105 106 // The most visited URLs, in priority order. 107 // Only used for matching up clicks on the page to which most visited entry 108 // was clicked on for metrics purposes. 109 std::vector<GURL> most_visited_urls_; 110 111 // We pre-fetch the first set of result pages. This variable is false until 112 // we get the first getMostVisited() call. 113 bool got_first_most_visited_request_; 114 115 // Keep the results of the db query here. 116 scoped_ptr<base::ListValue> pages_value_; 117 118 // Whether the user has viewed the 'most visited' pane. 119 bool most_visited_viewed_; 120 121 // Whether the user has performed a "tracked" action to leave the page or not. 122 bool user_action_logged_; 123 124 DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler); 125 }; 126 127 #endif // CHROME_BROWSER_UI_WEBUI_NTP_MOST_VISITED_HANDLER_H_ 128