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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
6 #define COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "components/web_modal/native_web_contents_modal_dialog_manager.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h"
14 #include "ui/gfx/native_widget_types.h"
18 class WebContentsModalDialogManagerDelegate
;
20 // Per-WebContents class to manage WebContents-modal dialogs.
21 class WebContentsModalDialogManager
22 : public NativeWebContentsModalDialogManagerDelegate
,
23 public content::WebContentsObserver
,
24 public content::WebContentsUserData
<WebContentsModalDialogManager
> {
26 virtual ~WebContentsModalDialogManager();
28 WebContentsModalDialogManagerDelegate
* delegate() const { return delegate_
; }
29 void SetDelegate(WebContentsModalDialogManagerDelegate
* d
);
31 static NativeWebContentsModalDialogManager
* CreateNativeManager(
32 NativeWebContentsModalDialogManagerDelegate
* native_delegate
);
34 // Shows the dialog as a web contents modal dialog. The dialog will notify via
35 // WillClose() when it is being destroyed.
36 void ShowDialog(NativeWebContentsModalDialog dialog
);
38 // Returns true if any dialogs are active and not closed.
39 bool IsDialogActive() const;
41 // Focus the topmost modal dialog. IsDialogActive() must be true when calling
43 void FocusTopmostDialog();
45 // Set to true to close the window when a page load starts on the WebContents.
46 void SetCloseOnInterstitialPage(NativeWebContentsModalDialog dialog
,
49 // Overriden from NativeWebContentsModalDialogManagerDelegate:
50 virtual content::WebContents
* GetWebContents() const OVERRIDE
;
51 // Called when a WebContentsModalDialogs we own is about to be closed.
52 virtual void WillClose(NativeWebContentsModalDialog dialog
) OVERRIDE
;
57 explicit TestApi(WebContentsModalDialogManager
* manager
)
58 : manager_(manager
) {}
60 void CloseAllDialogs() { manager_
->CloseAllDialogs(); }
61 void DidAttachInterstitialPage() { manager_
->DidAttachInterstitialPage(); }
62 void ResetNativeManager(NativeWebContentsModalDialogManager
* delegate
) {
63 manager_
->native_manager_
.reset(delegate
);
65 void WebContentsWasShown() { manager_
->WasShown(); }
66 void WebContentsWasHidden() { manager_
->WasHidden(); }
69 WebContentsModalDialogManager
* manager_
;
71 DISALLOW_COPY_AND_ASSIGN(TestApi
);
75 explicit WebContentsModalDialogManager(content::WebContents
* web_contents
);
76 friend class content::WebContentsUserData
<WebContentsModalDialogManager
>;
79 explicit DialogState(NativeWebContentsModalDialog dialog
);
81 NativeWebContentsModalDialog dialog
;
82 bool close_on_interstitial_webui
;
85 typedef std::deque
<DialogState
> WebContentsModalDialogList
;
87 // Utility function to get the dialog state for a dialog.
88 WebContentsModalDialogList::iterator
FindDialogState(
89 NativeWebContentsModalDialog dialog
);
91 // Blocks/unblocks interaction with renderer process.
92 void BlockWebContentsInteraction(bool blocked
);
94 bool IsWebContentsVisible() const;
96 // Closes all WebContentsModalDialogs.
97 void CloseAllDialogs();
99 // Overridden from content::WebContentsObserver:
100 virtual void DidNavigateMainFrame(
101 const content::LoadCommittedDetails
& details
,
102 const content::FrameNavigateParams
& params
) OVERRIDE
;
103 virtual void DidGetIgnoredUIEvent() OVERRIDE
;
104 virtual void WasShown() OVERRIDE
;
105 virtual void WasHidden() OVERRIDE
;
106 virtual void WebContentsDestroyed(content::WebContents
* tab
) OVERRIDE
;
107 virtual void DidAttachInterstitialPage() OVERRIDE
;
109 // Delegate for notifying our owner about stuff. Not owned by us.
110 WebContentsModalDialogManagerDelegate
* delegate_
;
112 // Delegate for native UI-specific functions on the dialog.
113 scoped_ptr
<NativeWebContentsModalDialogManager
> native_manager_
;
115 // All active dialogs.
116 WebContentsModalDialogList child_dialogs_
;
118 // True while closing the dialogs on WebContents close.
119 bool closing_all_dialogs_
;
121 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager
);
124 } // namespace web_modal
126 #endif // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_