popup_navigation_delegate_impl.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2020 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 "weblayer/browser/popup_navigation_delegate_impl.h"
  5. #include "base/callback_helpers.h"
  6. #include "build/build_config.h"
  7. #include "components/infobars/content/content_infobar_manager.h"
  8. #include "content/public/browser/web_contents.h"
  9. #include "weblayer/browser/host_content_settings_map_factory.h"
  10. #if BUILDFLAG(IS_ANDROID)
  11. #include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
  12. #endif
  13. namespace weblayer {
  14. PopupNavigationDelegateImpl::PopupNavigationDelegateImpl(
  15. const content::OpenURLParams& params,
  16. content::WebContents* source_contents,
  17. content::RenderFrameHost* opener)
  18. : params_(params),
  19. source_contents_(source_contents),
  20. opener_(opener),
  21. original_user_gesture_(params_.user_gesture) {}
  22. content::RenderFrameHost* PopupNavigationDelegateImpl::GetOpener() {
  23. return opener_;
  24. }
  25. bool PopupNavigationDelegateImpl::GetOriginalUserGesture() {
  26. return original_user_gesture_;
  27. }
  28. const GURL& PopupNavigationDelegateImpl::GetURL() {
  29. return params_.url;
  30. }
  31. blocked_content::PopupNavigationDelegate::NavigateResult
  32. PopupNavigationDelegateImpl::NavigateWithGesture(
  33. const blink::mojom::WindowFeatures& window_features,
  34. absl::optional<WindowOpenDisposition> updated_disposition) {
  35. // It's safe to mutate |params_| here because NavigateWithGesture() will only
  36. // be called once, and the user gesture value has already been saved in
  37. // |original_user_gesture_|.
  38. params_.user_gesture = true;
  39. if (updated_disposition)
  40. params_.disposition = updated_disposition.value();
  41. content::WebContents* new_contents = source_contents_->OpenURL(params_);
  42. return NavigateResult{
  43. new_contents,
  44. params_.disposition,
  45. };
  46. }
  47. void PopupNavigationDelegateImpl::OnPopupBlocked(
  48. content::WebContents* web_contents,
  49. int total_popups_blocked_on_page) {
  50. #if BUILDFLAG(IS_ANDROID)
  51. blocked_content::PopupBlockedInfoBarDelegate::Create(
  52. infobars::ContentInfoBarManager::FromWebContents(web_contents),
  53. total_popups_blocked_on_page,
  54. HostContentSettingsMapFactory::GetForBrowserContext(
  55. web_contents->GetBrowserContext()),
  56. base::NullCallback());
  57. #endif
  58. }
  59. } // namespace weblayer