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