| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/i18n/rtl.h" | 14 #include "base/i18n/rtl.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/browser/defaults.h" | 21 #include "chrome/browser/defaults.h" |
| 22 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/sessions/session_types.h" | 24 #include "chrome/browser/sessions/session_types.h" |
| 25 #include "chrome/browser/sync/profile_sync_service.h" | 25 #include "chrome/browser/sync/profile_sync_service.h" |
| 26 #include "chrome/browser/themes/theme_service.h" | 26 #include "chrome/browser/themes/theme_service.h" |
| 27 #include "chrome/browser/themes/theme_service_factory.h" | 27 #include "chrome/browser/themes/theme_service_factory.h" |
| 28 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
| 29 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 30 #include "chrome/browser/ui/webui/metrics_handler.h" | 29 #include "chrome/browser/ui/webui/metrics_handler.h" |
| 31 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" | 30 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" |
| 32 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" | 31 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
| 33 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" | 32 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" |
| 34 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 33 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
| 35 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 34 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 36 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" | 35 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" |
| 37 #include "chrome/browser/ui/webui/theme_source.h" | 36 #include "chrome/browser/ui/webui/theme_source.h" |
| 38 #include "chrome/common/chrome_notification_types.h" | 37 #include "chrome/common/chrome_notification_types.h" |
| 39 #include "chrome/common/chrome_switches.h" | 38 #include "chrome/common/chrome_switches.h" |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 // NewTabHTMLSource | 361 // NewTabHTMLSource |
| 363 | 362 |
| 364 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) | 363 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) |
| 365 : profile_(profile) { | 364 : profile_(profile) { |
| 366 } | 365 } |
| 367 | 366 |
| 368 std::string NewTabUI::NewTabHTMLSource::GetSource() { | 367 std::string NewTabUI::NewTabHTMLSource::GetSource() { |
| 369 return chrome::kChromeUINewTabHost; | 368 return chrome::kChromeUINewTabHost; |
| 370 } | 369 } |
| 371 | 370 |
| 372 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, | 371 void NewTabUI::NewTabHTMLSource::StartDataRequest( |
| 373 bool is_incognito, | 372 const std::string& path, |
| 374 int request_id) { | 373 bool is_incognito, |
| 374 const content::URLDataSource::GotDataCallback& callback) { |
| 375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 376 | 376 |
| 377 std::map<std::string, std::pair<std::string, int> >::iterator it = | 377 std::map<std::string, std::pair<std::string, int> >::iterator it = |
| 378 resource_map_.find(path); | 378 resource_map_.find(path); |
| 379 if (it != resource_map_.end()) { | 379 if (it != resource_map_.end()) { |
| 380 scoped_refptr<base::RefCountedStaticMemory> resource_bytes( | 380 scoped_refptr<base::RefCountedStaticMemory> resource_bytes( |
| 381 it->second.second ? | 381 it->second.second ? |
| 382 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 382 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 383 it->second.second) : | 383 it->second.second) : |
| 384 new base::RefCountedStaticMemory); | 384 new base::RefCountedStaticMemory); |
| 385 url_data_source()->SendResponse(request_id, resource_bytes); | 385 callback.Run(resource_bytes); |
| 386 return; | 386 return; |
| 387 } | 387 } |
| 388 | 388 |
| 389 if (!path.empty() && path[0] != '#') { | 389 if (!path.empty() && path[0] != '#') { |
| 390 // A path under new-tab was requested; it's likely a bad relative | 390 // A path under new-tab was requested; it's likely a bad relative |
| 391 // URL from the new tab page, but in any case it's an error. | 391 // URL from the new tab page, but in any case it's an error. |
| 392 | 392 |
| 393 // TODO(dtrainor): Can remove this #if check once we update the | 393 // TODO(dtrainor): Can remove this #if check once we update the |
| 394 // accessibility script to no longer try to access urls like | 394 // accessibility script to no longer try to access urls like |
| 395 // '?2314124523523'. | 395 // '?2314124523523'. |
| 396 // See http://crbug.com/150252. | 396 // See http://crbug.com/150252. |
| 397 #if !defined(OS_ANDROID) | 397 #if !defined(OS_ANDROID) |
| 398 NOTREACHED() << path << " should not have been requested on the NTP"; | 398 NOTREACHED() << path << " should not have been requested on the NTP"; |
| 399 #endif | 399 #endif |
| 400 return; | 400 return; |
| 401 } | 401 } |
| 402 | 402 |
| 403 scoped_refptr<base::RefCountedMemory> html_bytes( | 403 scoped_refptr<base::RefCountedMemory> html_bytes( |
| 404 NTPResourceCacheFactory::GetForProfile(profile_)-> | 404 NTPResourceCacheFactory::GetForProfile(profile_)-> |
| 405 GetNewTabHTML(is_incognito)); | 405 GetNewTabHTML(is_incognito)); |
| 406 | 406 |
| 407 url_data_source()->SendResponse(request_id, html_bytes); | 407 callback.Run(html_bytes); |
| 408 } | 408 } |
| 409 | 409 |
| 410 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) | 410 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& resource) |
| 411 const { | 411 const { |
| 412 std::map<std::string, std::pair<std::string, int> >::const_iterator it = | 412 std::map<std::string, std::pair<std::string, int> >::const_iterator it = |
| 413 resource_map_.find(resource); | 413 resource_map_.find(resource); |
| 414 if (it != resource_map_.end()) | 414 if (it != resource_map_.end()) |
| 415 return it->second.first; | 415 return it->second.first; |
| 416 return "text/html"; | 416 return "text/html"; |
| 417 } | 417 } |
| 418 | 418 |
| 419 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { | 419 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { |
| 420 return false; | 420 return false; |
| 421 } | 421 } |
| 422 | 422 |
| 423 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, | 423 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource, |
| 424 const char* mime_type, | 424 const char* mime_type, |
| 425 int resource_id) { | 425 int resource_id) { |
| 426 DCHECK(resource); | 426 DCHECK(resource); |
| 427 DCHECK(mime_type); | 427 DCHECK(mime_type); |
| 428 resource_map_[std::string(resource)] = | 428 resource_map_[std::string(resource)] = |
| 429 std::make_pair(std::string(mime_type), resource_id); | 429 std::make_pair(std::string(mime_type), resource_id); |
| 430 } | 430 } |
| 431 | 431 |
| 432 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} | 432 NewTabUI::NewTabHTMLSource::~NewTabHTMLSource() {} |
| OLD | NEW |