Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2011 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_MOST_VISITED_HANDLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_MOST_VISITED_HANDLER_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "chrome/browser/history/history_types.h"
     13 #include "content/browser/cancelable_request.h"
     14 #include "content/browser/webui/web_ui.h"
     15 #include "content/common/notification_observer.h"
     16 #include "content/common/notification_registrar.h"
     17 
     18 class GURL;
     19 class ListValue;
     20 class PageUsageData;
     21 class PrefService;
     22 class Value;
     23 
     24 // The handler for Javascript messages related to the "most visited" view.
     25 //
     26 // This class manages two preferences:
     27 // - The URL blacklist: URLs we do not want to show in the thumbnails list.  It
     28 //   is a dictionary for quick access (it associates a dummy boolean to the URL
     29 //   string).
     30 // - Pinned URLs: This is a dictionary for the pinned URLs for the the most
     31 //   visited part of the new tab page. The key of the dictionary is a hash of
     32 //   the URL and the value is a dictionary with title, url and index.  This is
     33 //   owned by the PrefService.
     34 class MostVisitedHandler : public WebUIMessageHandler,
     35                            public NotificationObserver {
     36  public:
     37 
     38   MostVisitedHandler();
     39   virtual ~MostVisitedHandler();
     40 
     41   // WebUIMessageHandler override and implementation.
     42   virtual WebUIMessageHandler* Attach(WebUI* web_ui);
     43   virtual void RegisterMessages();
     44 
     45   // Callback for the "getMostVisited" message.
     46   void HandleGetMostVisited(const ListValue* args);
     47 
     48   // Callback for the "blacklistURLFromMostVisited" message.
     49   void HandleBlacklistURL(const ListValue* args);
     50 
     51   // Callback for the "removeURLsFromMostVisitedBlacklist" message.
     52   void HandleRemoveURLsFromBlacklist(const ListValue* args);
     53 
     54   // Callback for the "clearMostVisitedURLsBlacklist" message.
     55   void HandleClearBlacklist(const ListValue* args);
     56 
     57   // Callback for the "addPinnedURL" message.
     58   void HandleAddPinnedURL(const ListValue* args);
     59 
     60   // Callback for the "removePinnedURL" message.
     61   void HandleRemovePinnedURL(const ListValue* args);
     62 
     63   // NotificationObserver implementation.
     64   virtual void Observe(NotificationType type,
     65                        const NotificationSource& source,
     66                        const NotificationDetails& details);
     67 
     68   const std::vector<GURL>& most_visited_urls() const {
     69     return most_visited_urls_;
     70   }
     71 
     72   static void RegisterUserPrefs(PrefService* prefs);
     73 
     74   // Returns a vector containing the urls for the prepopulated pages.
     75   // Used only in testing.
     76   static std::vector<GURL> GetPrePopulatedUrls();
     77 
     78  private:
     79   struct MostVisitedPage;
     80 
     81   // Send a request to the HistoryService to get the most visited pages.
     82   void StartQueryForMostVisited();
     83 
     84   // Sets pages_value_ from a format produced by TopSites.
     85   void SetPagesValueFromTopSites(const history::MostVisitedURLList& data);
     86 
     87   // Callback for TopSites.
     88   void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data);
     89 
     90   // Puts the passed URL in the blacklist (so it does not show as a thumbnail).
     91   void BlacklistURL(const GURL& url);
     92 
     93   // Returns the key used in url_blacklist_ and pinned_urls_ for the passed
     94   // |url|.
     95   std::string GetDictionaryKeyForURL(const std::string& url);
     96 
     97   // Gets the page data for a pinned URL at a given index. This returns
     98   // true if found.
     99   bool GetPinnedURLAtIndex(int index, MostVisitedPage* page);
    100 
    101   void AddPinnedURL(const MostVisitedPage& page, int index);
    102   void RemovePinnedURL(const GURL& url);
    103 
    104   // Sends pages_value_ to the javascript side to and resets page_value_.
    105   void SendPagesValue();
    106 
    107   // Returns true if we should treat this as the first run of the new tab page.
    108   bool IsFirstRun();
    109 
    110   static const std::vector<MostVisitedPage>& GetPrePopulatedPages();
    111 
    112   NotificationRegistrar registrar_;
    113 
    114   // Our consumer for the history service.
    115   CancelableRequestConsumerTSimple<PageUsageData*> cancelable_consumer_;
    116   CancelableRequestConsumer topsites_consumer_;
    117 
    118   // The most visited URLs, in priority order.
    119   // Only used for matching up clicks on the page to which most visited entry
    120   // was clicked on for metrics purposes.
    121   std::vector<GURL> most_visited_urls_;
    122 
    123   // We pre-fetch the first set of result pages.  This variable is false until
    124   // we get the first getMostVisited() call.
    125   bool got_first_most_visited_request_;
    126 
    127   // Keep the results of the db query here.
    128   scoped_ptr<ListValue> pages_value_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(MostVisitedHandler);
    131 };
    132 
    133 #endif  // CHROME_BROWSER_UI_WEBUI_MOST_VISITED_HANDLER_H_
    134