| 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/history_ui.h" | 5 #include "chrome/browser/ui/webui/history_ui.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
| 13 #include "base/i18n/time_formatting.h" | 13 #include "base/i18n/time_formatting.h" |
| 14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 24 #include "chrome/browser/chrome_notification_types.h" | 24 #include "chrome/browser/chrome_notification_types.h" |
| 25 #include "chrome/browser/history/history_notifications.h" | 25 #include "chrome/browser/history/history_notifications.h" |
| 26 #include "chrome/browser/history/history_service.h" |
| 26 #include "chrome/browser/history/history_service_factory.h" | 27 #include "chrome/browser/history/history_service_factory.h" |
| 27 #include "chrome/browser/history/web_history_service.h" | 28 #include "chrome/browser/history/web_history_service.h" |
| 28 #include "chrome/browser/history/web_history_service_factory.h" | 29 #include "chrome/browser/history/web_history_service_factory.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/sync/profile_sync_service.h" | 31 #include "chrome/browser/sync/profile_sync_service.h" |
| 31 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 32 #include "chrome/browser/ui/browser_finder.h" | 33 #include "chrome/browser/ui/browser_finder.h" |
| 33 #include "chrome/browser/ui/chrome_pages.h" | 34 #include "chrome/browser/ui/chrome_pages.h" |
| 34 #include "chrome/browser/ui/webui/favicon_source.h" | 35 #include "chrome/browser/ui/webui/favicon_source.h" |
| 35 #include "chrome/browser/ui/webui/metrics_handler.h" | 36 #include "chrome/browser/ui/webui/metrics_handler.h" |
| 36 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
| 37 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
| 38 #include "chrome/common/url_constants.h" | 39 #include "chrome/common/url_constants.h" |
| 39 #include "chrome/grit/generated_resources.h" | 40 #include "chrome/grit/generated_resources.h" |
| 40 #include "components/bookmarks/browser/bookmark_model.h" | 41 #include "components/bookmarks/browser/bookmark_model.h" |
| 41 #include "components/bookmarks/browser/bookmark_utils.h" | 42 #include "components/bookmarks/browser/bookmark_utils.h" |
| 42 #include "components/history/core/browser/history_types.h" | 43 #include "components/history/core/browser/history_types.h" |
| 43 #include "components/search/search.h" | 44 #include "components/search/search.h" |
| 44 #include "components/sync_driver/device_info.h" | 45 #include "components/sync_driver/device_info.h" |
| 45 #include "content/public/browser/notification_details.h" | |
| 46 #include "content/public/browser/notification_source.h" | |
| 47 #include "content/public/browser/url_data_source.h" | 46 #include "content/public/browser/url_data_source.h" |
| 48 #include "content/public/browser/web_ui.h" | 47 #include "content/public/browser/web_ui.h" |
| 49 #include "content/public/browser/web_ui_data_source.h" | 48 #include "content/public/browser/web_ui_data_source.h" |
| 50 #include "grit/browser_resources.h" | 49 #include "grit/browser_resources.h" |
| 51 #include "grit/theme_resources.h" | 50 #include "grit/theme_resources.h" |
| 52 #include "net/base/escape.h" | 51 #include "net/base/escape.h" |
| 53 #include "net/base/net_util.h" | 52 #include "net/base/net_util.h" |
| 54 #include "sync/protocol/history_delete_directive_specifics.pb.h" | 53 #include "sync/protocol/history_delete_directive_specifics.pb.h" |
| 55 #include "ui/base/l10n/l10n_util.h" | 54 #include "ui/base/l10n/l10n_util.h" |
| 56 #include "ui/base/l10n/time_format.h" | 55 #include "ui/base/l10n/time_format.h" |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 } | 397 } |
| 399 | 398 |
| 400 bool BrowsingHistoryHandler::HistoryEntry::SortByTimeDescending( | 399 bool BrowsingHistoryHandler::HistoryEntry::SortByTimeDescending( |
| 401 const BrowsingHistoryHandler::HistoryEntry& entry1, | 400 const BrowsingHistoryHandler::HistoryEntry& entry1, |
| 402 const BrowsingHistoryHandler::HistoryEntry& entry2) { | 401 const BrowsingHistoryHandler::HistoryEntry& entry2) { |
| 403 return entry1.time > entry2.time; | 402 return entry1.time > entry2.time; |
| 404 } | 403 } |
| 405 | 404 |
| 406 BrowsingHistoryHandler::BrowsingHistoryHandler() | 405 BrowsingHistoryHandler::BrowsingHistoryHandler() |
| 407 : has_pending_delete_request_(false), | 406 : has_pending_delete_request_(false), |
| 407 history_service_observer_(this), |
| 408 weak_factory_(this) { | 408 weak_factory_(this) { |
| 409 } | 409 } |
| 410 | 410 |
| 411 BrowsingHistoryHandler::~BrowsingHistoryHandler() { | 411 BrowsingHistoryHandler::~BrowsingHistoryHandler() { |
| 412 query_task_tracker_.TryCancelAll(); | 412 query_task_tracker_.TryCancelAll(); |
| 413 web_history_request_.reset(); | 413 web_history_request_.reset(); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void BrowsingHistoryHandler::RegisterMessages() { | 416 void BrowsingHistoryHandler::RegisterMessages() { |
| 417 // Create our favicon data source. | 417 // Create our favicon data source. |
| 418 Profile* profile = Profile::FromWebUI(web_ui()); | 418 Profile* profile = Profile::FromWebUI(web_ui()); |
| 419 content::URLDataSource::Add( | 419 content::URLDataSource::Add( |
| 420 profile, new FaviconSource(profile, FaviconSource::ANY)); | 420 profile, new FaviconSource(profile, FaviconSource::ANY)); |
| 421 | 421 |
| 422 // Get notifications when history is cleared. | 422 // Get notifications when history is cleared. |
| 423 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 423 HistoryService* hs = |
| 424 content::Source<Profile>(profile->GetOriginalProfile())); | 424 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
| 425 if (hs) |
| 426 history_service_observer_.Add(hs); |
| 425 | 427 |
| 426 web_ui()->RegisterMessageCallback("queryHistory", | 428 web_ui()->RegisterMessageCallback("queryHistory", |
| 427 base::Bind(&BrowsingHistoryHandler::HandleQueryHistory, | 429 base::Bind(&BrowsingHistoryHandler::HandleQueryHistory, |
| 428 base::Unretained(this))); | 430 base::Unretained(this))); |
| 429 web_ui()->RegisterMessageCallback("removeVisits", | 431 web_ui()->RegisterMessageCallback("removeVisits", |
| 430 base::Bind(&BrowsingHistoryHandler::HandleRemoveVisits, | 432 base::Bind(&BrowsingHistoryHandler::HandleRemoveVisits, |
| 431 base::Unretained(this))); | 433 base::Unretained(this))); |
| 432 web_ui()->RegisterMessageCallback("clearBrowsingData", | 434 web_ui()->RegisterMessageCallback("clearBrowsingData", |
| 433 base::Bind(&BrowsingHistoryHandler::HandleClearBrowsingData, | 435 base::Bind(&BrowsingHistoryHandler::HandleClearBrowsingData, |
| 434 base::Unretained(this))); | 436 base::Unretained(this))); |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 967 options->begin_time = base::Time::FromLocalExploded(exploded); | 969 options->begin_time = base::Time::FromLocalExploded(exploded); |
| 968 } | 970 } |
| 969 } | 971 } |
| 970 | 972 |
| 971 // Helper function for Observe that determines if there are any differences | 973 // Helper function for Observe that determines if there are any differences |
| 972 // between the URLs noticed for deletion and the ones we are expecting. | 974 // between the URLs noticed for deletion and the ones we are expecting. |
| 973 static bool DeletionsDiffer(const history::URLRows& deleted_rows, | 975 static bool DeletionsDiffer(const history::URLRows& deleted_rows, |
| 974 const std::set<GURL>& urls_to_be_deleted) { | 976 const std::set<GURL>& urls_to_be_deleted) { |
| 975 if (deleted_rows.size() != urls_to_be_deleted.size()) | 977 if (deleted_rows.size() != urls_to_be_deleted.size()) |
| 976 return true; | 978 return true; |
| 977 for (history::URLRows::const_iterator i = deleted_rows.begin(); | 979 for (const auto& i : deleted_rows) { |
| 978 i != deleted_rows.end(); ++i) { | 980 if (urls_to_be_deleted.find(i.url()) == urls_to_be_deleted.end()) |
| 979 if (urls_to_be_deleted.find(i->url()) == urls_to_be_deleted.end()) | |
| 980 return true; | 981 return true; |
| 981 } | 982 } |
| 982 return false; | 983 return false; |
| 983 } | 984 } |
| 984 | 985 |
| 985 void BrowsingHistoryHandler::Observe( | |
| 986 int type, | |
| 987 const content::NotificationSource& source, | |
| 988 const content::NotificationDetails& details) { | |
| 989 if (type != chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | |
| 990 NOTREACHED(); | |
| 991 return; | |
| 992 } | |
| 993 history::URLsDeletedDetails* deletedDetails = | |
| 994 content::Details<history::URLsDeletedDetails>(details).ptr(); | |
| 995 if (deletedDetails->all_history || | |
| 996 DeletionsDiffer(deletedDetails->rows, urls_to_be_deleted_)) | |
| 997 web_ui()->CallJavascriptFunction("historyDeleted"); | |
| 998 } | |
| 999 | |
| 1000 std::string BrowsingHistoryHandler::GetAcceptLanguages() const { | 986 std::string BrowsingHistoryHandler::GetAcceptLanguages() const { |
| 1001 Profile* profile = Profile::FromWebUI(web_ui()); | 987 Profile* profile = Profile::FromWebUI(web_ui()); |
| 1002 return profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 988 return profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 1003 } | 989 } |
| 1004 | 990 |
| 991 void BrowsingHistoryHandler::OnURLsDeleted(HistoryService* history_service, |
| 992 bool all_history, |
| 993 bool expired, |
| 994 const history::URLRows& deleted_rows, |
| 995 const std::set<GURL>& favicon_urls) { |
| 996 if (all_history || DeletionsDiffer(deleted_rows, urls_to_be_deleted_)) |
| 997 web_ui()->CallJavascriptFunction("historyDeleted"); |
| 998 } |
| 999 |
| 1005 //////////////////////////////////////////////////////////////////////////////// | 1000 //////////////////////////////////////////////////////////////////////////////// |
| 1006 // | 1001 // |
| 1007 // HistoryUI | 1002 // HistoryUI |
| 1008 // | 1003 // |
| 1009 //////////////////////////////////////////////////////////////////////////////// | 1004 //////////////////////////////////////////////////////////////////////////////// |
| 1010 | 1005 |
| 1011 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 1006 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 1012 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); | 1007 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); |
| 1013 web_ui->AddMessageHandler(new MetricsHandler()); | 1008 web_ui->AddMessageHandler(new MetricsHandler()); |
| 1014 | 1009 |
| 1015 // On mobile we deal with foreign sessions differently. | 1010 // On mobile we deal with foreign sessions differently. |
| 1016 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 1011 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1017 if (chrome::IsInstantExtendedAPIEnabled()) { | 1012 if (chrome::IsInstantExtendedAPIEnabled()) { |
| 1018 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 1013 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
| 1019 web_ui->AddMessageHandler(new NTPLoginHandler()); | 1014 web_ui->AddMessageHandler(new NTPLoginHandler()); |
| 1020 } | 1015 } |
| 1021 #endif | 1016 #endif |
| 1022 | 1017 |
| 1023 // Set up the chrome://history-frame/ source. | 1018 // Set up the chrome://history-frame/ source. |
| 1024 Profile* profile = Profile::FromWebUI(web_ui); | 1019 Profile* profile = Profile::FromWebUI(web_ui); |
| 1025 content::WebUIDataSource::Add(profile, CreateHistoryUIHTMLSource(profile)); | 1020 content::WebUIDataSource::Add(profile, CreateHistoryUIHTMLSource(profile)); |
| 1026 } | 1021 } |
| 1027 | 1022 |
| 1028 // static | 1023 // static |
| 1029 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes( | 1024 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes( |
| 1030 ui::ScaleFactor scale_factor) { | 1025 ui::ScaleFactor scale_factor) { |
| 1031 return ResourceBundle::GetSharedInstance(). | 1026 return ResourceBundle::GetSharedInstance(). |
| 1032 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor); | 1027 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor); |
| 1033 } | 1028 } |
| OLD | NEW |