| 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 29 matching lines...) Expand all Loading... |
| 92 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 94 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
| 93 | 95 |
| 94 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 96 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
| 95 // highly. Note this means we're including clicks on not only most visited | 97 // highly. Note this means we're including clicks on not only most visited |
| 96 // thumbnails, but also clicks on recently bookmarked. | 98 // thumbnails, but also clicks on recently bookmarked. |
| 97 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); | 99 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 98 | 100 |
| 99 if (!GetProfile()->IsOffTheRecord()) { | 101 if (!GetProfile()->IsOffTheRecord()) { |
| 100 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 102 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
| 101 web_ui->AddMessageHandler(new MostVisitedHandler()); | 103 web_ui->AddMessageHandler(new MostVisitedHandler()); |
| 104 if (NewTabUI::IsSuggestionsPageEnabled()) |
| 105 web_ui->AddMessageHandler(new SuggestionsHandler()); |
| 102 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); | 106 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); |
| 103 web_ui->AddMessageHandler(new MetricsHandler()); | 107 web_ui->AddMessageHandler(new MetricsHandler()); |
| 104 if (GetProfile()->IsSyncAccessible()) | 108 if (GetProfile()->IsSyncAccessible()) |
| 105 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); | 109 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); |
| 106 ExtensionService* service = GetProfile()->GetExtensionService(); | 110 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 107 // We might not have an ExtensionService (on ChromeOS when not logged in | 111 // We might not have an ExtensionService (on ChromeOS when not logged in |
| 108 // for example). | 112 // for example). |
| 109 if (service) | 113 if (service) |
| 110 web_ui->AddMessageHandler(new AppLauncherHandler(service)); | 114 web_ui->AddMessageHandler(new AppLauncherHandler(service)); |
| 111 | 115 |
| 112 web_ui->AddMessageHandler(new NewTabPageHandler()); | 116 web_ui->AddMessageHandler(new NewTabPageHandler()); |
| 113 web_ui->AddMessageHandler(new FaviconWebUIHandler()); | 117 web_ui->AddMessageHandler(new FaviconWebUIHandler()); |
| 114 } | 118 } |
| 115 | 119 |
| 116 if (NTPLoginHandler::ShouldShow(GetProfile())) | 120 if (NTPLoginHandler::ShouldShow(GetProfile())) |
| 117 web_ui->AddMessageHandler(new NTPLoginHandler()); | 121 web_ui->AddMessageHandler(new NTPLoginHandler()); |
| 118 | 122 |
| 119 // Initializing the CSS and HTML can require some CPU, so do it after | 123 // Initializing the CSS and HTML can require some CPU, so do it after |
| 120 // we've hooked up the most visited handler. This allows the DB query | 124 // we've hooked up the most visited handler. This allows the DB query |
| 121 // for the new tab thumbs to happen earlier. | 125 // for the new tab thumbs to happen earlier. |
| 122 InitializeCSSCaches(); | 126 InitializeCSSCaches(); |
| 123 NewTabHTMLSource* html_source = | 127 NewTabHTMLSource* html_source = |
| 124 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); | 128 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); |
| 125 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); | 129 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 130 // These two resources should be loaded only if suggestions NTP is enabled. |
| 131 html_source->AddResource("suggestions_page.css", "text/css", |
| 132 NewTabUI::IsSuggestionsPageEnabled() ? IDR_SUGGESTIONS_PAGE_CSS : 0); |
| 133 if (NewTabUI::IsSuggestionsPageEnabled()) { |
| 134 html_source->AddResource("suggestions_page.js", "text/javascript", |
| 135 IDR_SUGGESTIONS_PAGE_JS); |
| 136 } |
| 126 | 137 |
| 127 // Listen for theme installation. | 138 // Listen for theme installation. |
| 128 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 139 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 129 content::Source<ThemeService>( | 140 content::Source<ThemeService>( |
| 130 ThemeServiceFactory::GetForProfile(GetProfile()))); | 141 ThemeServiceFactory::GetForProfile(GetProfile()))); |
| 131 } | 142 } |
| 132 | 143 |
| 133 NewTabUI::~NewTabUI() { | 144 NewTabUI::~NewTabUI() { |
| 134 g_live_new_tabs.Pointer()->erase(this); | 145 g_live_new_tabs.Pointer()->erase(this); |
| 135 } | 146 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 Profile* profile = GetProfile(); | 222 Profile* profile = GetProfile(); |
| 212 ThemeSource* theme = new ThemeSource(profile); | 223 ThemeSource* theme = new ThemeSource(profile); |
| 213 profile->GetChromeURLDataManager()->AddDataSource(theme); | 224 profile->GetChromeURLDataManager()->AddDataSource(theme); |
| 214 } | 225 } |
| 215 | 226 |
| 216 // static | 227 // static |
| 217 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { | 228 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { |
| 218 NewTabPageHandler::RegisterUserPrefs(prefs); | 229 NewTabPageHandler::RegisterUserPrefs(prefs); |
| 219 AppLauncherHandler::RegisterUserPrefs(prefs); | 230 AppLauncherHandler::RegisterUserPrefs(prefs); |
| 220 MostVisitedHandler::RegisterUserPrefs(prefs); | 231 MostVisitedHandler::RegisterUserPrefs(prefs); |
| 232 if (NewTabUI::IsSuggestionsPageEnabled()) |
| 233 SuggestionsHandler::RegisterUserPrefs(prefs); |
| 221 } | 234 } |
| 222 | 235 |
| 223 // static | 236 // static |
| 224 void NewTabUI::SetupFieldTrials() { | 237 void NewTabUI::SetupFieldTrials() { |
| 225 scoped_refptr<base::FieldTrial> trial( | 238 scoped_refptr<base::FieldTrial> trial( |
| 226 new base::FieldTrial("WebStoreLinkExperiment", 1000, "Disabled", | 239 new base::FieldTrial("WebStoreLinkExperiment", 1000, "Disabled", |
| 227 2012, 6, 1)); | 240 2012, 6, 1)); |
| 228 | 241 |
| 229 // Try to give the user a consistent experience, if possible. | 242 // Try to give the user a consistent experience, if possible. |
| 230 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) | 243 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 241 return true; | 254 return true; |
| 242 | 255 |
| 243 if (!base::FieldTrialList::TrialExists(kWebStoreLinkExperiment)) | 256 if (!base::FieldTrialList::TrialExists(kWebStoreLinkExperiment)) |
| 244 return false; | 257 return false; |
| 245 | 258 |
| 246 return base::FieldTrialList::FindValue(kWebStoreLinkExperiment) != | 259 return base::FieldTrialList::FindValue(kWebStoreLinkExperiment) != |
| 247 base::FieldTrial::kDefaultGroupNumber; | 260 base::FieldTrial::kDefaultGroupNumber; |
| 248 } | 261 } |
| 249 | 262 |
| 250 // static | 263 // static |
| 264 bool NewTabUI::IsSuggestionsPageEnabled() { |
| 265 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 266 switches::kEnableSuggestionsTabPage); |
| 267 } |
| 268 |
| 269 // static |
| 251 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, | 270 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, |
| 252 const string16& title, | 271 const string16& title, |
| 253 const GURL& gurl) { | 272 const GURL& gurl) { |
| 254 dictionary->SetString("url", gurl.spec()); | 273 dictionary->SetString("url", gurl.spec()); |
| 255 | 274 |
| 256 bool using_url_as_the_title = false; | 275 bool using_url_as_the_title = false; |
| 257 string16 title_to_set(title); | 276 string16 title_to_set(title); |
| 258 if (title_to_set.empty()) { | 277 if (title_to_set.empty()) { |
| 259 using_url_as_the_title = true; | 278 using_url_as_the_title = true; |
| 260 title_to_set = UTF8ToUTF16(gurl.spec()); | 279 title_to_set = UTF8ToUTF16(gurl.spec()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) | 319 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
| 301 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), | 320 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), |
| 302 profile_(profile) { | 321 profile_(profile) { |
| 303 } | 322 } |
| 304 | 323 |
| 305 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, | 324 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, |
| 306 bool is_incognito, | 325 bool is_incognito, |
| 307 int request_id) { | 326 int request_id) { |
| 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 309 | 328 |
| 329 std::map<std::string, std::pair<std::string, int> >::iterator it = |
| 330 resource_map_.find(path); |
| 331 if (it != resource_map_.end()) { |
| 332 scoped_refptr<RefCountedStaticMemory> resource_bytes( |
| 333 it->second.second ? |
| 334 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 335 it->second.second) : |
| 336 new RefCountedStaticMemory); |
| 337 SendResponse(request_id, resource_bytes); |
| 338 return; |
| 339 } |
| 340 |
| 310 if (!path.empty() && path[0] != '#') { | 341 if (!path.empty() && path[0] != '#') { |
| 311 // A path under new-tab was requested; it's likely a bad relative | 342 // A path under new-tab was requested; it's likely a bad relative |
| 312 // URL from the new tab page, but in any case it's an error. | 343 // URL from the new tab page, but in any case it's an error. |
| 313 NOTREACHED() << path << " should not have been requested on the NTP"; | 344 NOTREACHED() << path << " should not have been requested on the NTP"; |
| 314 return; | 345 return; |
| 315 } | 346 } |
| 316 | 347 |
| 317 scoped_refptr<RefCountedMemory> html_bytes( | 348 scoped_refptr<RefCountedMemory> html_bytes( |
| 318 NTPResourceCacheFactory::GetForProfile(profile_)-> | 349 NTPResourceCacheFactory::GetForProfile(profile_)-> |
| 319 GetNewTabHTML(is_incognito)); | 350 GetNewTabHTML(is_incognito)); |
| 320 | 351 |
| 321 SendResponse(request_id, html_bytes); | 352 SendResponse(request_id, html_bytes); |
| 322 } | 353 } |
| 323 | 354 |
| 324 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { | 355 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& s) |
| 356 const { |
| 357 std::map<std::string, std::pair<std::string, int> >::const_iterator it = |
| 358 resource_map_.find(s); |
| 359 if (it != resource_map_.end()) |
| 360 return it->second.first; |
| 325 return "text/html"; | 361 return "text/html"; |
| 326 } | 362 } |
| 327 | 363 |
| 328 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 364 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
| 329 return false; | 365 return false; |
| 330 } | 366 } |
| 367 |
| 368 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, |
| 369 const char* mime_type, |
| 370 int resource_id) { |
| 371 DCHECK(resource); |
| 372 DCHECK(mime_type); |
| 373 resource_map_[std::string(resource)] = |
| 374 std::make_pair(std::string(mime_type), resource_id); |
| 375 } |
| OLD | NEW |