OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 37 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
39 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" | 39 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" |
40 #include "chrome/browser/ui/webui/theme_source.h" | 40 #include "chrome/browser/ui/webui/theme_source.h" |
41 #include "chrome/common/chrome_notification_types.h" | 41 #include "chrome/common/chrome_notification_types.h" |
42 #include "chrome/common/chrome_switches.h" | 42 #include "chrome/common/chrome_switches.h" |
43 #include "chrome/common/extensions/extension.h" | 43 #include "chrome/common/extensions/extension.h" |
44 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
45 #include "chrome/common/url_constants.h" | 45 #include "chrome/common/url_constants.h" |
46 #include "content/browser/renderer_host/render_view_host.h" | 46 #include "content/browser/renderer_host/render_view_host.h" |
| 47 #include "content/browser/webui/web_ui.h" |
47 #include "content/public/browser/browser_thread.h" | 48 #include "content/public/browser/browser_thread.h" |
48 #include "content/public/browser/notification_service.h" | 49 #include "content/public/browser/notification_service.h" |
49 #include "content/public/browser/user_metrics.h" | 50 #include "content/public/browser/user_metrics.h" |
50 #include "content/public/browser/web_contents.h" | 51 #include "content/public/browser/web_contents.h" |
51 #include "grit/generated_resources.h" | 52 #include "grit/generated_resources.h" |
52 #include "grit/theme_resources.h" | 53 #include "grit/theme_resources.h" |
53 #include "ui/base/l10n/l10n_util.h" | 54 #include "ui/base/l10n/l10n_util.h" |
54 | 55 |
55 using content::BrowserThread; | 56 using content::BrowserThread; |
56 using content::UserMetricsAction; | 57 using content::UserMetricsAction; |
57 using content::WebContents; | 58 using content::WebContents; |
| 59 using content::WebUIController; |
58 | 60 |
59 namespace { | 61 namespace { |
60 | 62 |
61 // The amount of time there must be no painting for us to consider painting | 63 // The amount of time there must be no painting for us to consider painting |
62 // finished. Observed times are in the ~1200ms range on Windows. | 64 // finished. Observed times are in the ~1200ms range on Windows. |
63 const int kTimeoutMs = 2000; | 65 const int kTimeoutMs = 2000; |
64 | 66 |
65 // Strings sent to the page via jstemplates used to set the direction of the | 67 // Strings sent to the page via jstemplates used to set the direction of the |
66 // HTML document based on locale. | 68 // HTML document based on locale. |
67 const char kRTLHtmlTextDirection[] = "rtl"; | 69 const char kRTLHtmlTextDirection[] = "rtl"; |
68 const char kLTRHtmlTextDirection[] = "ltr"; | 70 const char kLTRHtmlTextDirection[] = "ltr"; |
69 | 71 |
70 static base::LazyInstance<std::set<const WebUI*> > g_live_new_tabs; | 72 static base::LazyInstance<std::set<const WebUIController*> > g_live_new_tabs; |
71 | 73 |
72 } // namespace | 74 } // namespace |
73 | 75 |
74 /////////////////////////////////////////////////////////////////////////////// | 76 /////////////////////////////////////////////////////////////////////////////// |
75 // NewTabUI | 77 // NewTabUI |
76 | 78 |
77 NewTabUI::NewTabUI(WebContents* contents) | 79 NewTabUI::NewTabUI(WebUI* web_ui) |
78 : WebUI(contents, this) { | 80 : WebUIController(web_ui) { |
79 g_live_new_tabs.Pointer()->insert(this); | 81 g_live_new_tabs.Pointer()->insert(this); |
80 // Override some options on the Web UI. | 82 // Override some options on the Web UI. |
81 hide_favicon_ = true; | 83 web_ui->HideFavicon(); |
82 | 84 |
83 focus_location_bar_by_default_ = true; | 85 web_ui->FocusLocationBarByDefault(); |
84 should_hide_url_ = true; | 86 web_ui->HideURL(); |
85 overridden_title_ = l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE); | 87 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
86 | 88 |
87 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 89 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
88 // highly. Note this means we're including clicks on not only most visited | 90 // highly. Note this means we're including clicks on not only most visited |
89 // thumbnails, but also clicks on recently bookmarked. | 91 // thumbnails, but also clicks on recently bookmarked. |
90 link_transition_type_ = content::PAGE_TRANSITION_AUTO_BOOKMARK; | 92 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); |
91 | 93 |
92 if (!GetProfile()->IsOffTheRecord()) { | 94 if (!GetProfile()->IsOffTheRecord()) { |
93 AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 95 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
94 AddMessageHandler(new MostVisitedHandler()); | 96 web_ui->AddMessageHandler(new MostVisitedHandler()); |
95 AddMessageHandler(new RecentlyClosedTabsHandler()); | 97 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); |
96 AddMessageHandler(new MetricsHandler()); | 98 web_ui->AddMessageHandler(new MetricsHandler()); |
97 if (GetProfile()->IsSyncAccessible()) | 99 if (GetProfile()->IsSyncAccessible()) |
98 AddMessageHandler(new NewTabPageSyncHandler()); | 100 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); |
99 ExtensionService* service = GetProfile()->GetExtensionService(); | 101 ExtensionService* service = GetProfile()->GetExtensionService(); |
100 // We might not have an ExtensionService (on ChromeOS when not logged in | 102 // We might not have an ExtensionService (on ChromeOS when not logged in |
101 // for example). | 103 // for example). |
102 if (service) | 104 if (service) |
103 AddMessageHandler(new AppLauncherHandler(service)); | 105 web_ui->AddMessageHandler(new AppLauncherHandler(service)); |
104 | 106 |
105 AddMessageHandler(new NewTabPageHandler()); | 107 web_ui->AddMessageHandler(new NewTabPageHandler()); |
106 AddMessageHandler(new FaviconWebUIHandler()); | 108 web_ui->AddMessageHandler(new FaviconWebUIHandler()); |
107 } | 109 } |
108 | 110 |
109 if (NTPLoginHandler::ShouldShow(GetProfile())) | 111 if (NTPLoginHandler::ShouldShow(GetProfile())) |
110 AddMessageHandler(new NTPLoginHandler()); | 112 web_ui->AddMessageHandler(new NTPLoginHandler()); |
111 | 113 |
112 // Initializing the CSS and HTML can require some CPU, so do it after | 114 // Initializing the CSS and HTML can require some CPU, so do it after |
113 // we've hooked up the most visited handler. This allows the DB query | 115 // we've hooked up the most visited handler. This allows the DB query |
114 // for the new tab thumbs to happen earlier. | 116 // for the new tab thumbs to happen earlier. |
115 InitializeCSSCaches(); | 117 InitializeCSSCaches(); |
116 NewTabHTMLSource* html_source = | 118 NewTabHTMLSource* html_source = |
117 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); | 119 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); |
118 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 120 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); |
119 profile->GetChromeURLDataManager()->AddDataSource(html_source); | |
120 | 121 |
121 // Listen for theme installation. | 122 // Listen for theme installation. |
122 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 123 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
123 content::Source<ThemeService>( | 124 content::Source<ThemeService>( |
124 ThemeServiceFactory::GetForProfile(GetProfile()))); | 125 ThemeServiceFactory::GetForProfile(GetProfile()))); |
125 } | 126 } |
126 | 127 |
127 NewTabUI::~NewTabUI() { | 128 NewTabUI::~NewTabUI() { |
128 g_live_new_tabs.Pointer()->erase(this); | 129 g_live_new_tabs.Pointer()->erase(this); |
129 } | 130 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 const content::NotificationSource& source, | 183 const content::NotificationSource& source, |
183 const content::NotificationDetails& details) { | 184 const content::NotificationDetails& details) { |
184 switch (type) { | 185 switch (type) { |
185 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { | 186 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { |
186 InitializeCSSCaches(); | 187 InitializeCSSCaches(); |
187 ListValue args; | 188 ListValue args; |
188 args.Append(Value::CreateStringValue( | 189 args.Append(Value::CreateStringValue( |
189 ThemeServiceFactory::GetForProfile(GetProfile())->HasCustomImage( | 190 ThemeServiceFactory::GetForProfile(GetProfile())->HasCustomImage( |
190 IDR_THEME_NTP_ATTRIBUTION) ? | 191 IDR_THEME_NTP_ATTRIBUTION) ? |
191 "true" : "false")); | 192 "true" : "false")); |
192 CallJavascriptFunction("themeChanged", args); | 193 web_ui()->CallJavascriptFunction("themeChanged", args); |
193 break; | 194 break; |
194 } | 195 } |
195 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT: { | 196 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_PAINT: { |
196 last_paint_ = base::TimeTicks::Now(); | 197 last_paint_ = base::TimeTicks::Now(); |
197 break; | 198 break; |
198 } | 199 } |
199 default: | 200 default: |
200 CHECK(false) << "Unexpected notification: " << type; | 201 CHECK(false) << "Unexpected notification: " << type; |
201 } | 202 } |
202 } | 203 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 base::i18n::StringContainsStrongRTLChars(title)) { | 245 base::i18n::StringContainsStrongRTLChars(title)) { |
245 direction = kRTLHtmlTextDirection; | 246 direction = kRTLHtmlTextDirection; |
246 } else { | 247 } else { |
247 direction = kLTRHtmlTextDirection; | 248 direction = kLTRHtmlTextDirection; |
248 } | 249 } |
249 dictionary->SetString("title", title_to_set); | 250 dictionary->SetString("title", title_to_set); |
250 dictionary->SetString("direction", direction); | 251 dictionary->SetString("direction", direction); |
251 } | 252 } |
252 | 253 |
253 // static | 254 // static |
254 NewTabUI* NewTabUI::FromWebUI(WebUI* ui) { | 255 NewTabUI* NewTabUI::FromWebUIController(content::WebUIController* ui) { |
255 if (!g_live_new_tabs.Pointer()->count(ui)) | 256 if (!g_live_new_tabs.Pointer()->count(ui)) |
256 return NULL; | 257 return NULL; |
257 return static_cast<NewTabUI*>(ui); | 258 return static_cast<NewTabUI*>(ui); |
258 } | 259 } |
259 | 260 |
260 Profile* NewTabUI::GetProfile() const { | 261 Profile* NewTabUI::GetProfile() const { |
261 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 262 return Profile::FromBrowserContext( |
| 263 web_ui()->web_contents()->GetBrowserContext()); |
262 } | 264 } |
263 | 265 |
264 /////////////////////////////////////////////////////////////////////////////// | 266 /////////////////////////////////////////////////////////////////////////////// |
265 // NewTabHTMLSource | 267 // NewTabHTMLSource |
266 | 268 |
267 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) | 269 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
268 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), | 270 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), |
269 profile_(profile) { | 271 profile_(profile) { |
270 } | 272 } |
271 | 273 |
(...skipping 16 matching lines...) Expand all Loading... |
288 SendResponse(request_id, html_bytes); | 290 SendResponse(request_id, html_bytes); |
289 } | 291 } |
290 | 292 |
291 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { | 293 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { |
292 return "text/html"; | 294 return "text/html"; |
293 } | 295 } |
294 | 296 |
295 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 297 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
296 return false; | 298 return false; |
297 } | 299 } |
OLD | NEW |