Home | History | Annotate | Download | only in ntp
      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_NEW_TAB_UI_H_
      6 #define CHROME_BROWSER_UI_WEBUI_NTP_NEW_TAB_UI_H_
      7 
      8 #include <string>
      9 
     10 #include "base/gtest_prod_util.h"
     11 #include "base/prefs/pref_change_registrar.h"
     12 #include "base/time/time.h"
     13 #include "base/timer/timer.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/url_data_source.h"
     17 #include "content/public/browser/web_contents.h"
     18 #include "content/public/browser/web_ui_controller.h"
     19 
     20 class GURL;
     21 class Profile;
     22 
     23 namespace base {
     24 class DictionaryValue;
     25 }
     26 
     27 namespace user_prefs {
     28 class PrefRegistrySyncable;
     29 }
     30 
     31 // The WebUIController used for the New Tab page.
     32 class NewTabUI : public content::WebUIController,
     33                  public content::NotificationObserver {
     34  public:
     35   explicit NewTabUI(content::WebUI* web_ui);
     36   virtual ~NewTabUI();
     37 
     38   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     39 
     40   // Returns whether or not to show apps pages.
     41   static bool ShouldShowApps();
     42 
     43   // Returns whether or not "Discovery" in the NTP is Enabled.
     44   static bool IsDiscoveryInNTPEnabled();
     45 
     46   // Adds "url", "title", and "direction" keys on incoming dictionary, setting
     47   // title as the url as a fallback on empty title.
     48   static void SetUrlTitleAndDirection(base::DictionaryValue* dictionary,
     49                                       const string16& title,
     50                                       const GURL& gurl);
     51 
     52   // Returns a pointer to a NewTabUI if the WebUIController object is a new tab
     53   // page.
     54   static NewTabUI* FromWebUIController(content::WebUIController* ui);
     55 
     56   // The current preference version.
     57   static int current_pref_version() { return current_pref_version_; }
     58 
     59   // WebUIController implementation:
     60   virtual void RenderViewCreated(
     61       content::RenderViewHost* render_view_host) OVERRIDE;
     62   virtual void RenderViewReused(
     63       content::RenderViewHost* render_view_host) OVERRIDE;
     64 
     65   bool showing_sync_bubble() { return showing_sync_bubble_; }
     66   void set_showing_sync_bubble(bool showing) { showing_sync_bubble_ = showing; }
     67 
     68   class NewTabHTMLSource : public content::URLDataSource {
     69    public:
     70     explicit NewTabHTMLSource(Profile* profile);
     71     virtual ~NewTabHTMLSource();
     72 
     73     // content::URLDataSource implementation.
     74     virtual std::string GetSource() const OVERRIDE;
     75     virtual void StartDataRequest(
     76         const std::string& path,
     77         int render_process_id,
     78         int render_view_id,
     79         const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
     80     virtual std::string GetMimeType(const std::string&) const OVERRIDE;
     81     virtual bool ShouldReplaceExistingSource() const OVERRIDE;
     82     virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE;
     83 
     84     // Adds |resource| to the source. |resource_id| is resource id or 0,
     85     // which means return empty data set. |mime_type| is mime type of the
     86     // resource.
     87     void AddResource(const char* resource,
     88                      const char* mime_type,
     89                      int resource_id);
     90 
     91    private:
     92     // Pointer back to the original profile.
     93     Profile* profile_;
     94 
     95     // Maps resource files to mime types an resource ids.
     96     std::map<std::string, std::pair<std::string, int> > resource_map_;
     97 
     98     DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource);
     99   };
    100 
    101  private:
    102   FRIEND_TEST_ALL_PREFIXES(NewTabUITest, UpdateUserPrefsVersion);
    103 
    104   // content::NotificationObserver implementation.
    105   virtual void Observe(int type,
    106                        const content::NotificationSource& source,
    107                        const content::NotificationDetails& details) OVERRIDE;
    108 
    109   // If |web_contents| has an NTP URL, emits the number of NTP mouseovers
    110   // associated with |web_contents|, to be logged in UMA histogram.
    111   void EmitMouseoverCount(content::WebContents* web_contents);
    112 
    113   void OnShowBookmarkBarChanged();
    114 
    115   void StartTimingPaint(content::RenderViewHost* render_view_host);
    116   void PaintTimeout();
    117 
    118   Profile* GetProfile() const;
    119 
    120   content::NotificationRegistrar registrar_;
    121 
    122   // The time when we started benchmarking.
    123   base::TimeTicks start_;
    124   // The last time we got a paint notification.
    125   base::TimeTicks last_paint_;
    126   // Scoping so we can be sure our timeouts don't outlive us.
    127   base::OneShotTimer<NewTabUI> timer_;
    128   // The preference version. This used for migrating prefs of the NTP.
    129   static const int current_pref_version_ = 3;
    130 
    131   // If the sync promo NTP bubble is being shown.
    132   bool showing_sync_bubble_;
    133 
    134   PrefChangeRegistrar pref_change_registrar_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(NewTabUI);
    137 };
    138 
    139 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_NEW_TAB_UI_H_
    140