1 // Copyright 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/single_web_contents_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 SingleWebContentsDialogManagerDelegate,
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 SingleWebContentsDialogManager* CreateNativeWebModalManager(
32 NativeWebContentsModalDialog dialog,
33 SingleWebContentsDialogManagerDelegate* native_delegate);
35 // Shows the dialog as a web contents modal dialog. The dialog will notify via
36 // WillClose() when it is being destroyed.
37 void ShowModalDialog(NativeWebContentsModalDialog dialog);
39 // Allow clients to supply their own native dialog manager. Suitable for
41 void ShowDialogWithManager(
42 NativeWebContentsModalDialog dialog,
43 scoped_ptr<SingleWebContentsDialogManager> manager);
45 // Returns true if any dialogs are active and not closed.
46 bool IsDialogActive() const;
48 // Focus the topmost modal dialog. IsDialogActive() must be true when calling
50 void FocusTopmostDialog();
52 // Set to true to close the window when a page load starts on the WebContents.
53 void SetCloseOnInterstitialPage(NativeWebContentsModalDialog dialog,
56 // Overriden from SingleWebContentsDialogManagerDelegate:
57 virtual content::WebContents* GetWebContents() const OVERRIDE;
58 // Called when a WebContentsModalDialogs we own is about to be closed.
59 virtual void WillClose(NativeWebContentsModalDialog dialog) OVERRIDE;
64 explicit TestApi(WebContentsModalDialogManager* manager)
65 : manager_(manager) {}
67 void CloseAllDialogs() { manager_->CloseAllDialogs(); }
68 void DidAttachInterstitialPage() { manager_->DidAttachInterstitialPage(); }
69 void WebContentsWasShown() { manager_->WasShown(); }
70 void WebContentsWasHidden() { manager_->WasHidden(); }
73 WebContentsModalDialogManager* manager_;
75 DISALLOW_COPY_AND_ASSIGN(TestApi);
79 explicit WebContentsModalDialogManager(content::WebContents* web_contents);
80 friend class content::WebContentsUserData<WebContentsModalDialogManager>;
83 DialogState(NativeWebContentsModalDialog dialog,
84 scoped_ptr<SingleWebContentsDialogManager> manager);
87 NativeWebContentsModalDialog dialog;
88 scoped_ptr<SingleWebContentsDialogManager> manager;
89 bool close_on_interstitial_webui;
92 typedef std::deque<DialogState*> WebContentsModalDialogList;
94 // Utility function to get the dialog state for a dialog.
95 WebContentsModalDialogList::iterator FindDialogState(
96 NativeWebContentsModalDialog dialog);
98 // Blocks/unblocks interaction with renderer process.
99 void BlockWebContentsInteraction(bool blocked);
101 bool IsWebContentsVisible() const;
103 // Closes all WebContentsModalDialogs.
104 void CloseAllDialogs();
106 // Overridden from content::WebContentsObserver:
107 virtual void DidNavigateMainFrame(
108 const content::LoadCommittedDetails& details,
109 const content::FrameNavigateParams& params) OVERRIDE;
110 virtual void DidGetIgnoredUIEvent() OVERRIDE;
111 virtual void WasShown() OVERRIDE;
112 virtual void WasHidden() OVERRIDE;
113 virtual void WebContentsDestroyed() OVERRIDE;
114 virtual void DidAttachInterstitialPage() OVERRIDE;
116 // Delegate for notifying our owner about stuff. Not owned by us.
117 WebContentsModalDialogManagerDelegate* delegate_;
119 // All active dialogs.
120 WebContentsModalDialogList child_dialogs_;
122 // True while closing the dialogs on WebContents close.
123 bool closing_all_dialogs_;
125 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager);
128 } // namespace web_modal
130 #endif // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_