ios_blocking_page_controller_client.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ios/components/security_interstitials/ios_blocking_page_controller_client.h"
  5. #include "base/bind.h"
  6. #include "base/check_op.h"
  7. #include "base/notreached.h"
  8. #include "components/security_interstitials/core/metrics_helper.h"
  9. #import "ios/web/public/navigation/navigation_manager.h"
  10. #include "ios/web/public/navigation/reload_type.h"
  11. #include "ios/web/public/thread/web_task_traits.h"
  12. #include "ios/web/public/thread/web_thread.h"
  13. #import "ios/web/public/web_state.h"
  14. #if !defined(__has_feature) || !__has_feature(objc_arc)
  15. #error "This file requires ARC support."
  16. #endif
  17. namespace security_interstitials {
  18. IOSBlockingPageControllerClient::IOSBlockingPageControllerClient(
  19. web::WebState* web_state,
  20. std::unique_ptr<security_interstitials::MetricsHelper> metrics_helper,
  21. const std::string& app_locale)
  22. : security_interstitials::ControllerClient(std::move(metrics_helper)),
  23. web_state_(web_state),
  24. app_locale_(app_locale),
  25. weak_factory_(this) {
  26. web_state_->AddObserver(this);
  27. }
  28. IOSBlockingPageControllerClient::~IOSBlockingPageControllerClient() {
  29. if (web_state_) {
  30. web_state_->RemoveObserver(this);
  31. }
  32. }
  33. void IOSBlockingPageControllerClient::WebStateDestroyed(
  34. web::WebState* web_state) {
  35. DCHECK_EQ(web_state_, web_state);
  36. web_state_->RemoveObserver(this);
  37. web_state_ = nullptr;
  38. }
  39. bool IOSBlockingPageControllerClient::CanLaunchDateAndTimeSettings() {
  40. return false;
  41. }
  42. void IOSBlockingPageControllerClient::LaunchDateAndTimeSettings() {
  43. NOTREACHED();
  44. }
  45. void IOSBlockingPageControllerClient::GoBack() {
  46. if (CanGoBack()) {
  47. web_state_->GetNavigationManager()->GoBack();
  48. } else {
  49. // Closing the tab synchronously is problematic since web state is heavily
  50. // involved in the operation and CloseWebState interrupts it, so call
  51. // CloseWebState asynchronously.
  52. web::GetUIThreadTaskRunner({})->PostTask(
  53. FROM_HERE, base::BindOnce(&IOSBlockingPageControllerClient::Close,
  54. weak_factory_.GetWeakPtr()));
  55. }
  56. }
  57. bool IOSBlockingPageControllerClient::CanGoBack() {
  58. return web_state_->GetNavigationManager()->CanGoBack();
  59. }
  60. bool IOSBlockingPageControllerClient::CanGoBackBeforeNavigation() {
  61. NOTREACHED();
  62. return false;
  63. }
  64. void IOSBlockingPageControllerClient::GoBackAfterNavigationCommitted() {
  65. NOTREACHED();
  66. }
  67. void IOSBlockingPageControllerClient::Proceed() {
  68. NOTREACHED();
  69. }
  70. void IOSBlockingPageControllerClient::Reload() {
  71. web_state_->GetNavigationManager()->Reload(web::ReloadType::NORMAL,
  72. true /*check_for_repost*/);
  73. }
  74. void IOSBlockingPageControllerClient::OpenUrlInCurrentTab(const GURL& url) {
  75. web_state_->OpenURL(web::WebState::OpenURLParams(
  76. url, web::Referrer(), WindowOpenDisposition::CURRENT_TAB,
  77. ui::PAGE_TRANSITION_LINK, false));
  78. }
  79. void IOSBlockingPageControllerClient::OpenUrlInNewForegroundTab(
  80. const GURL& url) {
  81. web_state_->OpenURL(web::WebState::OpenURLParams(
  82. url, web::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
  83. ui::PAGE_TRANSITION_LINK, false));
  84. }
  85. void IOSBlockingPageControllerClient::OpenEnhancedProtectionSettings() {
  86. NOTREACHED() << "Enhanced protection is not supported on iOS.";
  87. }
  88. const std::string& IOSBlockingPageControllerClient::GetApplicationLocale()
  89. const {
  90. return app_locale_;
  91. }
  92. PrefService* IOSBlockingPageControllerClient::GetPrefService() {
  93. return nullptr;
  94. }
  95. const std::string
  96. IOSBlockingPageControllerClient::GetExtendedReportingPrefName() const {
  97. return std::string();
  98. }
  99. void IOSBlockingPageControllerClient::Close() {
  100. if (web_state_) {
  101. web_state_->CloseWebState();
  102. }
  103. }
  104. } // namespace security_interstitials