OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 20 matching lines...) Expand all Loading... |
31 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 31 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
32 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" | 32 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" |
33 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | 33 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
34 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" | 34 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" |
35 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" | 35 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" |
36 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" | 36 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" |
37 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" | 37 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.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/ntp_resource_cache.h" | 39 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
40 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" | 40 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" |
| 41 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" |
41 #include "chrome/browser/ui/webui/theme_source.h" | 42 #include "chrome/browser/ui/webui/theme_source.h" |
42 #include "chrome/common/chrome_notification_types.h" | 43 #include "chrome/common/chrome_notification_types.h" |
43 #include "chrome/common/chrome_switches.h" | 44 #include "chrome/common/chrome_switches.h" |
44 #include "chrome/common/extensions/extension.h" | 45 #include "chrome/common/extensions/extension.h" |
45 #include "chrome/common/pref_names.h" | 46 #include "chrome/common/pref_names.h" |
46 #include "chrome/common/url_constants.h" | 47 #include "chrome/common/url_constants.h" |
47 #include "content/browser/renderer_host/render_view_host.h" | 48 #include "content/browser/renderer_host/render_view_host.h" |
48 #include "content/public/browser/browser_thread.h" | 49 #include "content/public/browser/browser_thread.h" |
49 #include "content/public/browser/notification_service.h" | 50 #include "content/public/browser/notification_service.h" |
50 #include "content/public/browser/user_metrics.h" | 51 #include "content/public/browser/user_metrics.h" |
51 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
52 #include "content/public/browser/web_ui.h" | 53 #include "content/public/browser/web_ui.h" |
| 54 #include "grit/browser_resources.h" |
53 #include "grit/generated_resources.h" | 55 #include "grit/generated_resources.h" |
54 #include "grit/theme_resources.h" | 56 #include "grit/theme_resources.h" |
55 #include "ui/base/l10n/l10n_util.h" | 57 #include "ui/base/l10n/l10n_util.h" |
56 | 58 |
57 using content::BrowserThread; | 59 using content::BrowserThread; |
58 using content::UserMetricsAction; | 60 using content::UserMetricsAction; |
59 using content::WebContents; | 61 using content::WebContents; |
60 using content::WebUIController; | 62 using content::WebUIController; |
61 | 63 |
62 namespace { | 64 namespace { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 103 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
102 | 104 |
103 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 105 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
104 // highly. Note this means we're including clicks on not only most visited | 106 // highly. Note this means we're including clicks on not only most visited |
105 // thumbnails, but also clicks on recently bookmarked. | 107 // thumbnails, but also clicks on recently bookmarked. |
106 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); | 108 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); |
107 | 109 |
108 if (!GetProfile()->IsOffTheRecord()) { | 110 if (!GetProfile()->IsOffTheRecord()) { |
109 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 111 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
110 web_ui->AddMessageHandler(new MostVisitedHandler()); | 112 web_ui->AddMessageHandler(new MostVisitedHandler()); |
| 113 if (NewTabUI::IsSuggestionsPageEnabled()) |
| 114 web_ui->AddMessageHandler(new SuggestionsHandler()); |
111 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); | 115 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); |
112 web_ui->AddMessageHandler(new MetricsHandler()); | 116 web_ui->AddMessageHandler(new MetricsHandler()); |
113 if (GetProfile()->IsSyncAccessible()) | 117 if (GetProfile()->IsSyncAccessible()) |
114 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); | 118 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); |
115 ExtensionService* service = GetProfile()->GetExtensionService(); | 119 ExtensionService* service = GetProfile()->GetExtensionService(); |
116 // We might not have an ExtensionService (on ChromeOS when not logged in | 120 // We might not have an ExtensionService (on ChromeOS when not logged in |
117 // for example). | 121 // for example). |
118 if (service) | 122 if (service) |
119 web_ui->AddMessageHandler(new AppLauncherHandler(service)); | 123 web_ui->AddMessageHandler(new AppLauncherHandler(service)); |
120 | 124 |
121 web_ui->AddMessageHandler(new NewTabPageHandler()); | 125 web_ui->AddMessageHandler(new NewTabPageHandler()); |
122 web_ui->AddMessageHandler(new FaviconWebUIHandler()); | 126 web_ui->AddMessageHandler(new FaviconWebUIHandler()); |
123 } | 127 } |
124 | 128 |
125 if (NTPLoginHandler::ShouldShow(GetProfile())) | 129 if (NTPLoginHandler::ShouldShow(GetProfile())) |
126 web_ui->AddMessageHandler(new NTPLoginHandler()); | 130 web_ui->AddMessageHandler(new NTPLoginHandler()); |
127 | 131 |
128 // Initializing the CSS and HTML can require some CPU, so do it after | 132 // Initializing the CSS and HTML can require some CPU, so do it after |
129 // we've hooked up the most visited handler. This allows the DB query | 133 // we've hooked up the most visited handler. This allows the DB query |
130 // for the new tab thumbs to happen earlier. | 134 // for the new tab thumbs to happen earlier. |
131 InitializeCSSCaches(); | 135 InitializeCSSCaches(); |
132 NewTabHTMLSource* html_source = | 136 NewTabHTMLSource* html_source = |
133 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); | 137 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); |
| 138 // These two resources should be loaded only if suggestions NTP is enabled. |
| 139 html_source->AddResource("suggestions_page.css", "text/css", |
| 140 NewTabUI::IsSuggestionsPageEnabled() ? IDR_SUGGESTIONS_PAGE_CSS : 0); |
| 141 if (NewTabUI::IsSuggestionsPageEnabled()) { |
| 142 html_source->AddResource("suggestions_page.js", "application/javascript", |
| 143 IDR_SUGGESTIONS_PAGE_JS); |
| 144 } |
| 145 // ChromeURLDataManager assumes the ownership of the html_source and in some |
| 146 // tests immediately deletes it, so html_source should not be accessed after |
| 147 // this call. |
134 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); | 148 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 149 // These two resources should be loaded only if suggestions NTP is enabled. |
| 150 html_source->AddResource("suggestions_page.css", "text/css", |
| 151 NewTabUI::IsSuggestionsPageEnabled() ? IDR_SUGGESTIONS_PAGE_CSS : 0); |
| 152 if (NewTabUI::IsSuggestionsPageEnabled()) { |
| 153 html_source->AddResource("suggestions_page.js", "application/javascript", |
| 154 IDR_SUGGESTIONS_PAGE_JS); |
| 155 } |
135 | 156 |
136 pref_change_registrar_.Init(GetProfile()->GetPrefs()); | 157 pref_change_registrar_.Init(GetProfile()->GetPrefs()); |
137 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); | 158 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
138 // Listen for theme installation. | 159 // Listen for theme installation. |
139 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 160 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
140 content::Source<ThemeService>( | 161 content::Source<ThemeService>( |
141 ThemeServiceFactory::GetForProfile(GetProfile()))); | 162 ThemeServiceFactory::GetForProfile(GetProfile()))); |
142 } | 163 } |
143 | 164 |
144 NewTabUI::~NewTabUI() { | 165 NewTabUI::~NewTabUI() { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 Profile* profile = GetProfile(); | 248 Profile* profile = GetProfile(); |
228 ThemeSource* theme = new ThemeSource(profile); | 249 ThemeSource* theme = new ThemeSource(profile); |
229 profile->GetChromeURLDataManager()->AddDataSource(theme); | 250 profile->GetChromeURLDataManager()->AddDataSource(theme); |
230 } | 251 } |
231 | 252 |
232 // static | 253 // static |
233 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { | 254 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { |
234 NewTabPageHandler::RegisterUserPrefs(prefs); | 255 NewTabPageHandler::RegisterUserPrefs(prefs); |
235 AppLauncherHandler::RegisterUserPrefs(prefs); | 256 AppLauncherHandler::RegisterUserPrefs(prefs); |
236 MostVisitedHandler::RegisterUserPrefs(prefs); | 257 MostVisitedHandler::RegisterUserPrefs(prefs); |
| 258 if (NewTabUI::IsSuggestionsPageEnabled()) |
| 259 SuggestionsHandler::RegisterUserPrefs(prefs); |
237 } | 260 } |
238 | 261 |
239 // static | 262 // static |
240 void NewTabUI::SetupFieldTrials() { | 263 void NewTabUI::SetupFieldTrials() { |
241 scoped_refptr<base::FieldTrial> trial( | 264 scoped_refptr<base::FieldTrial> trial( |
242 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled", | 265 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled", |
243 2012, 6, 1)); | 266 2012, 6, 1)); |
244 | 267 |
245 // Try to give the user a consistent experience, if possible. | 268 // Try to give the user a consistent experience, if possible. |
246 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) | 269 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) |
(...skipping 12 matching lines...) Expand all Loading... |
259 } | 282 } |
260 | 283 |
261 // static | 284 // static |
262 bool NewTabUI::ShouldShowAppInstallHint() { | 285 bool NewTabUI::ShouldShowAppInstallHint() { |
263 const CommandLine* cli = CommandLine::ForCurrentProcess(); | 286 const CommandLine* cli = CommandLine::ForCurrentProcess(); |
264 return cli->HasSwitch(switches::kNtpAppInstallHint) || | 287 return cli->HasSwitch(switches::kNtpAppInstallHint) || |
265 WebStoreLinkExperimentGroupIs(g_hint_group); | 288 WebStoreLinkExperimentGroupIs(g_hint_group); |
266 } | 289 } |
267 | 290 |
268 // static | 291 // static |
| 292 bool NewTabUI::IsSuggestionsPageEnabled() { |
| 293 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 294 switches::kEnableSuggestionsTabPage); |
| 295 } |
| 296 |
| 297 // static |
269 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, | 298 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, |
270 const string16& title, | 299 const string16& title, |
271 const GURL& gurl) { | 300 const GURL& gurl) { |
272 dictionary->SetString("url", gurl.spec()); | 301 dictionary->SetString("url", gurl.spec()); |
273 | 302 |
274 bool using_url_as_the_title = false; | 303 bool using_url_as_the_title = false; |
275 string16 title_to_set(title); | 304 string16 title_to_set(title); |
276 if (title_to_set.empty()) { | 305 if (title_to_set.empty()) { |
277 using_url_as_the_title = true; | 306 using_url_as_the_title = true; |
278 title_to_set = UTF8ToUTF16(gurl.spec()); | 307 title_to_set = UTF8ToUTF16(gurl.spec()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) | 347 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
319 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), | 348 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), |
320 profile_(profile) { | 349 profile_(profile) { |
321 } | 350 } |
322 | 351 |
323 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, | 352 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, |
324 bool is_incognito, | 353 bool is_incognito, |
325 int request_id) { | 354 int request_id) { |
326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
327 | 356 |
| 357 std::map<std::string, std::pair<std::string, int> >::iterator it = |
| 358 resource_map_.find(path); |
| 359 if (it != resource_map_.end()) { |
| 360 scoped_refptr<RefCountedStaticMemory> resource_bytes( |
| 361 it->second.second ? |
| 362 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 363 it->second.second) : |
| 364 new RefCountedStaticMemory); |
| 365 SendResponse(request_id, resource_bytes); |
| 366 return; |
| 367 } |
| 368 |
328 if (!path.empty() && path[0] != '#') { | 369 if (!path.empty() && path[0] != '#') { |
329 // A path under new-tab was requested; it's likely a bad relative | 370 // A path under new-tab was requested; it's likely a bad relative |
330 // URL from the new tab page, but in any case it's an error. | 371 // URL from the new tab page, but in any case it's an error. |
331 NOTREACHED() << path << " should not have been requested on the NTP"; | 372 NOTREACHED() << path << " should not have been requested on the NTP"; |
332 return; | 373 return; |
333 } | 374 } |
334 | 375 |
335 scoped_refptr<RefCountedMemory> html_bytes( | 376 scoped_refptr<RefCountedMemory> html_bytes( |
336 NTPResourceCacheFactory::GetForProfile(profile_)-> | 377 NTPResourceCacheFactory::GetForProfile(profile_)-> |
337 GetNewTabHTML(is_incognito)); | 378 GetNewTabHTML(is_incognito)); |
338 | 379 |
339 SendResponse(request_id, html_bytes); | 380 SendResponse(request_id, html_bytes); |
340 } | 381 } |
341 | 382 |
342 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { | 383 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) |
| 384 const { |
| 385 std::map<std::string, std::pair<std::string, int> >::const_iterator it = |
| 386 resource_map_.find(resource); |
| 387 if (it != resource_map_.end()) |
| 388 return it->second.first; |
343 return "text/html"; | 389 return "text/html"; |
344 } | 390 } |
345 | 391 |
346 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 392 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
347 return false; | 393 return false; |
348 } | 394 } |
| 395 |
| 396 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, |
| 397 const char* mime_type, |
| 398 int resource_id) { |
| 399 DCHECK(resource); |
| 400 DCHECK(mime_type); |
| 401 resource_map_[std::string(resource)] = |
| 402 std::make_pair(std::string(mime_type), resource_id); |
| 403 } |
OLD | NEW |