| 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); |
| 135 | 149 |
| 136 pref_change_registrar_.Init(GetProfile()->GetPrefs()); | 150 pref_change_registrar_.Init(GetProfile()->GetPrefs()); |
| 137 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); | 151 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
| 138 // Listen for theme installation. | 152 // Listen for theme installation. |
| 139 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 153 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 140 content::Source<ThemeService>( | 154 content::Source<ThemeService>( |
| 141 ThemeServiceFactory::GetForProfile(GetProfile()))); | 155 ThemeServiceFactory::GetForProfile(GetProfile()))); |
| 142 } | 156 } |
| 143 | 157 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 Profile* profile = GetProfile(); | 241 Profile* profile = GetProfile(); |
| 228 ThemeSource* theme = new ThemeSource(profile); | 242 ThemeSource* theme = new ThemeSource(profile); |
| 229 profile->GetChromeURLDataManager()->AddDataSource(theme); | 243 profile->GetChromeURLDataManager()->AddDataSource(theme); |
| 230 } | 244 } |
| 231 | 245 |
| 232 // static | 246 // static |
| 233 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { | 247 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { |
| 234 NewTabPageHandler::RegisterUserPrefs(prefs); | 248 NewTabPageHandler::RegisterUserPrefs(prefs); |
| 235 AppLauncherHandler::RegisterUserPrefs(prefs); | 249 AppLauncherHandler::RegisterUserPrefs(prefs); |
| 236 MostVisitedHandler::RegisterUserPrefs(prefs); | 250 MostVisitedHandler::RegisterUserPrefs(prefs); |
| 251 if (NewTabUI::IsSuggestionsPageEnabled()) |
| 252 SuggestionsHandler::RegisterUserPrefs(prefs); |
| 237 } | 253 } |
| 238 | 254 |
| 239 // static | 255 // static |
| 240 void NewTabUI::SetupFieldTrials() { | 256 void NewTabUI::SetupFieldTrials() { |
| 241 scoped_refptr<base::FieldTrial> trial( | 257 scoped_refptr<base::FieldTrial> trial( |
| 242 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled", | 258 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled", |
| 243 2012, 6, 1)); | 259 2012, 6, 1)); |
| 244 | 260 |
| 245 // Try to give the user a consistent experience, if possible. | 261 // Try to give the user a consistent experience, if possible. |
| 246 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) | 262 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 259 } | 275 } |
| 260 | 276 |
| 261 // static | 277 // static |
| 262 bool NewTabUI::ShouldShowAppInstallHint() { | 278 bool NewTabUI::ShouldShowAppInstallHint() { |
| 263 const CommandLine* cli = CommandLine::ForCurrentProcess(); | 279 const CommandLine* cli = CommandLine::ForCurrentProcess(); |
| 264 return cli->HasSwitch(switches::kNtpAppInstallHint) || | 280 return cli->HasSwitch(switches::kNtpAppInstallHint) || |
| 265 WebStoreLinkExperimentGroupIs(g_hint_group); | 281 WebStoreLinkExperimentGroupIs(g_hint_group); |
| 266 } | 282 } |
| 267 | 283 |
| 268 // static | 284 // static |
| 285 bool NewTabUI::IsSuggestionsPageEnabled() { |
| 286 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 287 switches::kEnableSuggestionsTabPage); |
| 288 } |
| 289 |
| 290 // static |
| 269 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, | 291 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, |
| 270 const string16& title, | 292 const string16& title, |
| 271 const GURL& gurl) { | 293 const GURL& gurl) { |
| 272 dictionary->SetString("url", gurl.spec()); | 294 dictionary->SetString("url", gurl.spec()); |
| 273 | 295 |
| 274 bool using_url_as_the_title = false; | 296 bool using_url_as_the_title = false; |
| 275 string16 title_to_set(title); | 297 string16 title_to_set(title); |
| 276 if (title_to_set.empty()) { | 298 if (title_to_set.empty()) { |
| 277 using_url_as_the_title = true; | 299 using_url_as_the_title = true; |
| 278 title_to_set = UTF8ToUTF16(gurl.spec()); | 300 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) | 340 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
| 319 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), | 341 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), |
| 320 profile_(profile) { | 342 profile_(profile) { |
| 321 } | 343 } |
| 322 | 344 |
| 323 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, | 345 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, |
| 324 bool is_incognito, | 346 bool is_incognito, |
| 325 int request_id) { | 347 int request_id) { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 | 349 |
| 350 std::map<std::string, std::pair<std::string, int> >::iterator it = |
| 351 resource_map_.find(path); |
| 352 if (it != resource_map_.end()) { |
| 353 scoped_refptr<RefCountedStaticMemory> resource_bytes( |
| 354 it->second.second ? |
| 355 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 356 it->second.second) : |
| 357 new RefCountedStaticMemory); |
| 358 SendResponse(request_id, resource_bytes); |
| 359 return; |
| 360 } |
| 361 |
| 328 if (!path.empty() && path[0] != '#') { | 362 if (!path.empty() && path[0] != '#') { |
| 329 // A path under new-tab was requested; it's likely a bad relative | 363 // 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. | 364 // 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"; | 365 NOTREACHED() << path << " should not have been requested on the NTP"; |
| 332 return; | 366 return; |
| 333 } | 367 } |
| 334 | 368 |
| 335 scoped_refptr<RefCountedMemory> html_bytes( | 369 scoped_refptr<RefCountedMemory> html_bytes( |
| 336 NTPResourceCacheFactory::GetForProfile(profile_)-> | 370 NTPResourceCacheFactory::GetForProfile(profile_)-> |
| 337 GetNewTabHTML(is_incognito)); | 371 GetNewTabHTML(is_incognito)); |
| 338 | 372 |
| 339 SendResponse(request_id, html_bytes); | 373 SendResponse(request_id, html_bytes); |
| 340 } | 374 } |
| 341 | 375 |
| 342 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { | 376 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) |
| 377 const { |
| 378 std::map<std::string, std::pair<std::string, int> >::const_iterator it = |
| 379 resource_map_.find(resource); |
| 380 if (it != resource_map_.end()) |
| 381 return it->second.first; |
| 343 return "text/html"; | 382 return "text/html"; |
| 344 } | 383 } |
| 345 | 384 |
| 346 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 385 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
| 347 return false; | 386 return false; |
| 348 } | 387 } |
| 388 |
| 389 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, |
| 390 const char* mime_type, |
| 391 int resource_id) { |
| 392 DCHECK(resource); |
| 393 DCHECK(mime_type); |
| 394 resource_map_[std::string(resource)] = |
| 395 std::make_pair(std::string(mime_type), resource_id); |
| 396 } |
| OLD | NEW |