| 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 "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 5 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/webui/metrics_handler.h" | 17 #include "chrome/browser/ui/webui/metrics_handler.h" |
| 18 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | |
| 19 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | |
| 20 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" | 18 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" |
| 21 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | 19 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
| 22 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" | 20 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" |
| 23 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" | |
| 24 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" | |
| 25 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" | |
| 26 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 21 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
| 27 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 22 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 28 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" | 23 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" |
| 29 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" | 24 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" |
| 30 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" | |
| 31 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 32 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 33 #include "components/user_prefs/pref_registry_syncable.h" | 27 #include "components/user_prefs/pref_registry_syncable.h" |
| 34 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 36 #include "content/public/browser/render_process_host.h" | 30 #include "content/public/browser/render_process_host.h" |
| 37 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
| 38 #include "content/public/browser/url_data_source.h" | 32 #include "content/public/browser/url_data_source.h" |
| 39 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 40 #include "content/public/browser/web_ui.h" | 34 #include "content/public/browser/web_ui.h" |
| 41 #include "grit/browser_resources.h" | 35 #include "grit/browser_resources.h" |
| 42 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
| 43 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 44 | 38 |
| 39 #if !defined(OS_ANDROID) |
| 40 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
| 41 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 42 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" |
| 43 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" |
| 44 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" |
| 45 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" |
| 46 #else |
| 47 #include "chrome/browser/ui/webui/ntp/android/bookmarks_handler.h" |
| 48 #include "chrome/browser/ui/webui/ntp/android/context_menu_handler.h" |
| 49 #include "chrome/browser/ui/webui/ntp/android/navigation_handler.h" |
| 50 #include "chrome/browser/ui/webui/ntp/android/new_tab_page_ready_handler.h" |
| 51 #include "chrome/browser/ui/webui/ntp/android/promo_handler.h" |
| 52 #endif |
| 53 |
| 45 #if defined(ENABLE_THEMES) | 54 #if defined(ENABLE_THEMES) |
| 46 #include "chrome/browser/ui/webui/theme_handler.h" | 55 #include "chrome/browser/ui/webui/theme_handler.h" |
| 47 #endif | 56 #endif |
| 48 | 57 |
| 49 #if defined(USE_ASH) | 58 #if defined(USE_ASH) |
| 50 #include "chrome/browser/ui/host_desktop.h" | 59 #include "chrome/browser/ui/host_desktop.h" |
| 51 #endif | 60 #endif |
| 52 | 61 |
| 53 using content::BrowserThread; | 62 using content::BrowserThread; |
| 54 using content::RenderViewHost; | 63 using content::RenderViewHost; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 98 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
| 90 // highly. Note this means we're including clicks on not only most visited | 99 // highly. Note this means we're including clicks on not only most visited |
| 91 // thumbnails, but also clicks on recently bookmarked. | 100 // thumbnails, but also clicks on recently bookmarked. |
| 92 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); | 101 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 93 | 102 |
| 94 if (!GetProfile()->IsOffTheRecord()) { | 103 if (!GetProfile()->IsOffTheRecord()) { |
| 95 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 104 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
| 96 web_ui->AddMessageHandler(new MetricsHandler()); | 105 web_ui->AddMessageHandler(new MetricsHandler()); |
| 97 web_ui->AddMessageHandler(new MostVisitedHandler()); | 106 web_ui->AddMessageHandler(new MostVisitedHandler()); |
| 98 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); | 107 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); |
| 108 #if !defined(OS_ANDROID) |
| 99 web_ui->AddMessageHandler(new FaviconWebUIHandler()); | 109 web_ui->AddMessageHandler(new FaviconWebUIHandler()); |
| 100 web_ui->AddMessageHandler(new NewTabPageHandler()); | 110 web_ui->AddMessageHandler(new NewTabPageHandler()); |
| 101 web_ui->AddMessageHandler(new CoreAppLauncherHandler()); | 111 web_ui->AddMessageHandler(new CoreAppLauncherHandler()); |
| 102 if (NewTabUI::IsDiscoveryInNTPEnabled()) | 112 if (NewTabUI::IsDiscoveryInNTPEnabled()) |
| 103 web_ui->AddMessageHandler(new SuggestionsHandler()); | 113 web_ui->AddMessageHandler(new SuggestionsHandler()); |
| 114 // Android doesn't have a sync promo/username on NTP. |
| 104 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); | 115 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); |
| 105 | 116 |
| 106 ExtensionService* service = GetProfile()->GetExtensionService(); | 117 if (MightShowApps()) { |
| 107 // We might not have an ExtensionService (on ChromeOS when not logged in | 118 ExtensionService* service = GetProfile()->GetExtensionService(); |
| 108 // for example). | 119 // We might not have an ExtensionService (on ChromeOS when not logged in |
| 109 if (service) | 120 // for example). |
| 110 web_ui->AddMessageHandler(new AppLauncherHandler(service)); | 121 if (service) |
| 122 web_ui->AddMessageHandler(new AppLauncherHandler(service)); |
| 123 } |
| 124 #endif |
| 111 } | 125 } |
| 112 | 126 |
| 127 #if defined(OS_ANDROID) |
| 128 // These handlers are specific to the Android NTP page. |
| 129 web_ui->AddMessageHandler(new BookmarksHandler()); |
| 130 web_ui->AddMessageHandler(new ContextMenuHandler()); |
| 131 web_ui->AddMessageHandler(new FaviconWebUIHandler()); |
| 132 web_ui->AddMessageHandler(new NavigationHandler()); |
| 133 web_ui->AddMessageHandler(new NewTabPageReadyHandler()); |
| 134 if (!GetProfile()->IsOffTheRecord()) |
| 135 web_ui->AddMessageHandler(new PromoHandler()); |
| 136 #else |
| 137 // Android uses native UI for sync setup. |
| 113 if (NTPLoginHandler::ShouldShow(GetProfile())) | 138 if (NTPLoginHandler::ShouldShow(GetProfile())) |
| 114 web_ui->AddMessageHandler(new NTPLoginHandler()); | 139 web_ui->AddMessageHandler(new NTPLoginHandler()); |
| 140 #endif |
| 115 | 141 |
| 116 #if defined(ENABLE_THEMES) | 142 #if defined(ENABLE_THEMES) |
| 117 // The theme handler can require some CPU, so do it after hooking up the most | 143 // The theme handler can require some CPU, so do it after hooking up the most |
| 118 // visited handler. This allows the DB query for the new tab thumbs to happen | 144 // visited handler. This allows the DB query for the new tab thumbs to happen |
| 119 // earlier. | 145 // earlier. |
| 120 web_ui->AddMessageHandler(new ThemeHandler()); | 146 web_ui->AddMessageHandler(new ThemeHandler()); |
| 121 #endif | 147 #endif |
| 122 | 148 |
| 123 scoped_ptr<NewTabHTMLSource> html_source(new NewTabHTMLSource( | 149 scoped_ptr<NewTabHTMLSource> html_source(new NewTabHTMLSource( |
| 124 GetProfile()->GetOriginalProfile())); | 150 GetProfile()->GetOriginalProfile())); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 void NewTabUI::OnShowBookmarkBarChanged() { | 240 void NewTabUI::OnShowBookmarkBarChanged() { |
| 215 base::StringValue attached( | 241 base::StringValue attached( |
| 216 GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) ? | 242 GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) ? |
| 217 "true" : "false"); | 243 "true" : "false"); |
| 218 web_ui()->CallJavascriptFunction("ntp.setBookmarkBarAttached", attached); | 244 web_ui()->CallJavascriptFunction("ntp.setBookmarkBarAttached", attached); |
| 219 } | 245 } |
| 220 | 246 |
| 221 // static | 247 // static |
| 222 void NewTabUI::RegisterProfilePrefs( | 248 void NewTabUI::RegisterProfilePrefs( |
| 223 user_prefs::PrefRegistrySyncable* registry) { | 249 user_prefs::PrefRegistrySyncable* registry) { |
| 250 #if !defined(OS_ANDROID) |
| 224 CoreAppLauncherHandler::RegisterProfilePrefs(registry); | 251 CoreAppLauncherHandler::RegisterProfilePrefs(registry); |
| 225 NewTabPageHandler::RegisterProfilePrefs(registry); | 252 NewTabPageHandler::RegisterProfilePrefs(registry); |
| 226 if (NewTabUI::IsDiscoveryInNTPEnabled()) | 253 if (NewTabUI::IsDiscoveryInNTPEnabled()) |
| 227 SuggestionsHandler::RegisterProfilePrefs(registry); | 254 SuggestionsHandler::RegisterProfilePrefs(registry); |
| 255 #endif |
| 228 MostVisitedHandler::RegisterProfilePrefs(registry); | 256 MostVisitedHandler::RegisterProfilePrefs(registry); |
| 229 browser_sync::ForeignSessionHandler::RegisterProfilePrefs(registry); | 257 browser_sync::ForeignSessionHandler::RegisterProfilePrefs(registry); |
| 230 } | 258 } |
| 231 | 259 |
| 232 // static | 260 // static |
| 261 bool NewTabUI::MightShowApps() { |
| 262 // Android does not have apps. |
| 263 #if defined(OS_ANDROID) |
| 264 return false; |
| 265 #else |
| 266 return true; |
| 267 #endif |
| 268 } |
| 269 |
| 270 // static |
| 233 bool NewTabUI::ShouldShowApps() { | 271 bool NewTabUI::ShouldShowApps() { |
| 234 // Ash shows apps in app list thus should not show apps page in NTP4. | 272 // Ash shows apps in app list thus should not show apps page in NTP4. |
| 235 #if defined(USE_ASH) | 273 // Android does not have apps. |
| 274 #if defined(OS_ANDROID) |
| 275 return false; |
| 276 #elif defined(USE_ASH) |
| 236 return chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH; | 277 return chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH; |
| 237 #else | 278 #else |
| 238 return true; | 279 return true; |
| 239 #endif | 280 #endif |
| 240 } | 281 } |
| 241 | 282 |
| 242 // static | 283 // static |
| 243 bool NewTabUI::IsDiscoveryInNTPEnabled() { | 284 bool NewTabUI::IsDiscoveryInNTPEnabled() { |
| 244 // TODO(beaudoin): The flag was removed during a clean-up pass. We leave that | 285 // TODO(beaudoin): The flag was removed during a clean-up pass. We leave that |
| 245 // here to easily enable it back when we will explore this option again. | 286 // here to easily enable it back when we will explore this option again. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 365 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 325 it->second.second) : | 366 it->second.second) : |
| 326 new base::RefCountedStaticMemory); | 367 new base::RefCountedStaticMemory); |
| 327 callback.Run(resource_bytes.get()); | 368 callback.Run(resource_bytes.get()); |
| 328 return; | 369 return; |
| 329 } | 370 } |
| 330 | 371 |
| 331 if (!path.empty() && path[0] != '#') { | 372 if (!path.empty() && path[0] != '#') { |
| 332 // A path under new-tab was requested; it's likely a bad relative | 373 // A path under new-tab was requested; it's likely a bad relative |
| 333 // URL from the new tab page, but in any case it's an error. | 374 // URL from the new tab page, but in any case it's an error. |
| 375 |
| 376 // TODO(dtrainor): Can remove this #if check once we update the |
| 377 // accessibility script to no longer try to access urls like |
| 378 // '?2314124523523'. |
| 379 // See http://crbug.com/150252. |
| 380 #if !defined(OS_ANDROID) |
| 334 NOTREACHED() << path << " should not have been requested on the NTP"; | 381 NOTREACHED() << path << " should not have been requested on the NTP"; |
| 382 #endif |
| 335 callback.Run(NULL); | 383 callback.Run(NULL); |
| 336 return; | 384 return; |
| 337 } | 385 } |
| 338 | 386 |
| 339 content::RenderProcessHost* render_host = | 387 content::RenderProcessHost* render_host = |
| 340 content::RenderProcessHost::FromID(render_process_id); | 388 content::RenderProcessHost::FromID(render_process_id); |
| 341 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( | 389 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( |
| 342 profile_, render_host); | 390 profile_, render_host); |
| 343 scoped_refptr<base::RefCountedMemory> html_bytes( | 391 scoped_refptr<base::RefCountedMemory> html_bytes( |
| 344 NTPResourceCacheFactory::GetForProfile(profile_)-> | 392 NTPResourceCacheFactory::GetForProfile(profile_)-> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 367 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, | 415 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, |
| 368 const char* mime_type, | 416 const char* mime_type, |
| 369 int resource_id) { | 417 int resource_id) { |
| 370 DCHECK(resource); | 418 DCHECK(resource); |
| 371 DCHECK(mime_type); | 419 DCHECK(mime_type); |
| 372 resource_map_[std::string(resource)] = | 420 resource_map_[std::string(resource)] = |
| 373 std::make_pair(std::string(mime_type), resource_id); | 421 std::make_pair(std::string(mime_type), resource_id); |
| 374 } | 422 } |
| 375 | 423 |
| 376 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} | 424 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} |
| OLD | NEW |