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_contents_observer.h" 19 #include "content/public/browser/web_ui_controller.h" 20 21 class GURL; 22 class Profile; 23 24 namespace base { 25 class DictionaryValue; 26 } 27 28 namespace user_prefs { 29 class PrefRegistrySyncable; 30 } 31 32 // The WebUIController used for the New Tab page. 33 class NewTabUI : public content::WebUIController, 34 public content::WebContentsObserver, 35 public content::NotificationObserver { 36 public: 37 explicit NewTabUI(content::WebUI* web_ui); 38 virtual ~NewTabUI(); 39 40 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 41 42 // Returns whether or not this browser process might ever need to show apps 43 // on the NTP. With Win8 running in immersive mode, for example, apps are 44 // displayed on a ChromeOS-style apps bar and not on the NTP. In desktop, 45 // however, apps are displayed on the NTP. Since they both share the same 46 // browser process instance, a different decision is necessary for whether 47 // or not to register app message handlers versus whether to show it on NTP. 48 static bool MightShowApps(); 49 50 // Returns whether or not to show apps pages. 51 static bool ShouldShowApps(); 52 53 // Returns whether or not "Discovery" in the NTP is Enabled. 54 static bool IsDiscoveryInNTPEnabled(); 55 56 // Adds "url", "title", and "direction" keys on incoming dictionary, setting 57 // title as the url as a fallback on empty title. 58 static void SetUrlTitleAndDirection(base::DictionaryValue* dictionary, 59 const base::string16& title, 60 const GURL& gurl); 61 62 // Adds "full_name" and "full_name_direction" keys on incoming dictionary. 63 static void SetFullNameAndDirection(const base::string16& full_name, 64 base::DictionaryValue* dictionary); 65 66 // Returns a pointer to a NewTabUI if the WebUIController object is a new tab 67 // page. 68 static NewTabUI* FromWebUIController(content::WebUIController* ui); 69 70 // The current preference version. 71 static int current_pref_version() { return current_pref_version_; } 72 73 // WebUIController implementation: 74 virtual void RenderViewCreated( 75 content::RenderViewHost* render_view_host) OVERRIDE; 76 virtual void RenderViewReused( 77 content::RenderViewHost* render_view_host) OVERRIDE; 78 79 // WebContentsObserver implementation: 80 virtual void WasHidden() OVERRIDE; 81 82 bool showing_sync_bubble() { return showing_sync_bubble_; } 83 void set_showing_sync_bubble(bool showing) { showing_sync_bubble_ = showing; } 84 85 class NewTabHTMLSource : public content::URLDataSource { 86 public: 87 explicit NewTabHTMLSource(Profile* profile); 88 virtual ~NewTabHTMLSource(); 89 90 // content::URLDataSource implementation. 91 virtual std::string GetSource() const OVERRIDE; 92 virtual void StartDataRequest( 93 const std::string& path, 94 int render_process_id, 95 int render_view_id, 96 const content::URLDataSource::GotDataCallback& callback) OVERRIDE; 97 virtual std::string GetMimeType(const std::string&) const OVERRIDE; 98 virtual bool ShouldReplaceExistingSource() const OVERRIDE; 99 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE; 100 101 // Adds |resource| to the source. |resource_id| is resource id or 0, 102 // which means return empty data set. |mime_type| is mime type of the 103 // resource. 104 void AddResource(const char* resource, 105 const char* mime_type, 106 int resource_id); 107 108 private: 109 // Pointer back to the original profile. 110 Profile* profile_; 111 112 // Maps resource files to mime types an resource ids. 113 std::map<std::string, std::pair<std::string, int> > resource_map_; 114 115 DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource); 116 }; 117 118 private: 119 FRIEND_TEST_ALL_PREFIXES(NewTabUITest, UpdateUserPrefsVersion); 120 121 // content::NotificationObserver implementation. 122 virtual void Observe(int type, 123 const content::NotificationSource& source, 124 const content::NotificationDetails& details) OVERRIDE; 125 126 // If |web_contents| has an NTP URL, emits a number of NTP statistics (like 127 // mouseovers counts) associated with |web_contents|, to be logged in UMA 128 // histograms. 129 void EmitNtpStatistics(); 130 131 void OnShowBookmarkBarChanged(); 132 133 void StartTimingPaint(content::RenderViewHost* render_view_host); 134 void PaintTimeout(); 135 136 Profile* GetProfile() const; 137 138 content::NotificationRegistrar registrar_; 139 140 // The time when we started benchmarking. 141 base::TimeTicks start_; 142 // The last time we got a paint notification. 143 base::TimeTicks last_paint_; 144 // Scoping so we can be sure our timeouts don't outlive us. 145 base::OneShotTimer<NewTabUI> timer_; 146 // The preference version. This used for migrating prefs of the NTP. 147 static const int current_pref_version_ = 3; 148 149 // If the sync promo NTP bubble is being shown. 150 bool showing_sync_bubble_; 151 152 PrefChangeRegistrar pref_change_registrar_; 153 154 DISALLOW_COPY_AND_ASSIGN(NewTabUI); 155 }; 156 157 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NEW_TAB_UI_H_ 158