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_SUGGESTIONS_PAGE_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_PAGE_HANDLER_H_ 7 8 #include <string> 9 10 #include "chrome/browser/common/cancelable_request.h" 11 #include "chrome/browser/history/history_types.h" 12 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.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 17 class GURL; 18 class PageUsageData; 19 20 namespace base { 21 class ListValue; 22 class Value; 23 } 24 25 namespace user_prefs { 26 class PrefRegistrySyncable; 27 } 28 29 // The handler for Javascript messages related to the "suggestions" 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). 35 class SuggestionsHandler : public content::WebUIMessageHandler, 36 public content::NotificationObserver, 37 public SuggestionsCombiner::Delegate { 38 public: 39 SuggestionsHandler(); 40 virtual ~SuggestionsHandler(); 41 42 // WebUIMessageHandler override and implementation. 43 virtual void RegisterMessages() OVERRIDE; 44 45 // Callback for the "getSuggestions" message. 46 void HandleGetSuggestions(const base::ListValue* args); 47 48 // Callback for the "blacklistURLFromSuggestions" message. 49 void HandleBlacklistURL(const base::ListValue* args); 50 51 // Callback for the "removeURLsFromSuggestionsBlacklist" message. 52 void HandleRemoveURLsFromBlacklist(const base::ListValue* args); 53 54 // Callback for the "clearSuggestionsURLsBlacklist" message. 55 void HandleClearBlacklist(const base::ListValue* args); 56 57 // Callback for the "suggestedSitesAction" message. 58 void HandleSuggestedSitesAction(const base::ListValue* args); 59 60 // Callback for the "suggestedSitesSelected" message. 61 void HandleSuggestedSitesSelected(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 // SuggestionsCombiner::Delegate implementation. 69 virtual void OnSuggestionsReady() OVERRIDE; 70 71 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 72 73 private: 74 // Puts the passed URL in the blacklist (so it does not show as a thumbnail). 75 void BlacklistURL(const GURL& url); 76 77 // Returns the key used in url_blacklist_ for the passed |url|. 78 std::string GetDictionaryKeyForURL(const std::string& url); 79 80 // Sends pages_value_ to the javascript side to and resets page_value_. 81 void SendPagesValue(); 82 83 content::NotificationRegistrar registrar_; 84 85 // We pre-fetch the first set of result pages. This variable is false until 86 // we get the first getSuggestions() call. 87 bool got_first_suggestions_request_; 88 89 // Used to combine suggestions from various sources. 90 scoped_ptr<SuggestionsCombiner> suggestions_combiner_; 91 92 // Whether the user has viewed the 'suggested' pane. 93 bool suggestions_viewed_; 94 95 // Whether the user has performed a "tracked" action to leave the page or not. 96 bool user_action_logged_; 97 98 DISALLOW_COPY_AND_ASSIGN(SuggestionsHandler); 99 }; 100 101 #endif // CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_PAGE_HANDLER_H_ 102