1 // Copyright 2014 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.
5 #include "components/web_modal/popup_manager.h"
7 #include "components/web_modal/web_contents_modal_dialog_host.h"
8 #include "components/web_modal/web_contents_modal_dialog_manager.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/browser/web_contents_user_data.h"
11 #include "ui/gfx/geometry/size.h"
13 using content::WebContents
;
19 const char kPopupManagerUserDataKey
[] = "PopupManager";
21 // This class provides a hook to get a PopupManager from a WebContents.
22 // The PopupManager is browser-scoped, but will use a FromWebContents API
23 // to attach to each WebContents in that browser.
24 class PopupManagerRelay
: public content::WebContentsUserData
<PopupManager
> {
26 explicit PopupManagerRelay(base::WeakPtr
<PopupManager
> manager
)
27 : manager_(manager
) {}
29 ~PopupManagerRelay() override
{}
31 base::WeakPtr
<PopupManager
> manager_
;
36 PopupManager::PopupManager(WebContentsModalDialogHost
* host
)
38 weak_factory_(this) {}
40 PopupManager::~PopupManager() {
43 void PopupManager::ShowModalDialog(gfx::NativeWindow popup
,
44 content::WebContents
* web_contents
) {
45 // TODO make a new native popup manager and call ShowPopup.
46 // For now just lay off to WCMDM.
47 WebContentsModalDialogManager
* manager
=
48 WebContentsModalDialogManager::FromWebContents(web_contents
);
49 manager
->ShowModalDialog(popup
);
52 bool PopupManager::IsWebModalDialogActive(
53 const content::WebContents
* web_contents
) const {
54 if (web_contents
== NULL
)
57 const WebContentsModalDialogManager
* manager
=
58 WebContentsModalDialogManager::FromWebContents(web_contents
);
59 return manager
&& manager
->IsDialogActive();
62 void PopupManager::WasFocused(const content::WebContents
* web_contents
) {
63 if (!IsWebModalDialogActive(web_contents
))
66 const WebContentsModalDialogManager
* manager
=
67 WebContentsModalDialogManager::FromWebContents(web_contents
);
69 manager
->FocusTopmostDialog();
72 void PopupManager::WillClose(gfx::NativeWindow popup
) {
75 void PopupManager::RegisterWith(content::WebContents
* web_contents
) {
76 web_contents
->SetUserData(kPopupManagerUserDataKey
,
77 new PopupManagerRelay(weak_factory_
.GetWeakPtr()));
78 // TODO(gbillock): Need to do something more extreme here to manage changing
79 // popup managers with popups in-flight?
82 void PopupManager::UnregisterWith(content::WebContents
* web_contents
) {
83 web_contents
->RemoveUserData(kPopupManagerUserDataKey
);
84 // TODO(gbillock): Need to do something more extreme here to manage changing
85 // popup managers with popups in-flight?
88 PopupManager
* PopupManager::FromWebContents(
89 content::WebContents
* web_contents
) {
90 PopupManagerRelay
* relay
= static_cast<PopupManagerRelay
*>(
91 web_contents
->GetUserData(kPopupManagerUserDataKey
));
95 return relay
->manager_
.get();
98 gfx::NativeView
PopupManager::GetHostView() const {
99 // TODO(gbillock): replace this with a PopupManagerHost or something.
101 return host_
->GetHostView();
104 void PopupManager::CloseAllDialogsForTesting(
105 content::WebContents
* web_contents
) {
106 // TODO: re-implement, probably in terms of something in the host_,
107 // or of owned WCMDMs.
108 WebContentsModalDialogManager
* manager
=
109 WebContentsModalDialogManager::FromWebContents(web_contents
);
110 manager
->CloseAllDialogs();
113 } // namespace web_modal