Reland "Close web contents modal dialogs on content load start"
[chromium-blink-merge.git] / components / web_modal / web_contents_modal_dialog_manager.h
blob00671b2db1a1956f10a686423a6626be45acf643
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_
8 #include <deque>
10 #include "base/memory/scoped_ptr.h"
11 #include "components/web_modal/native_web_contents_modal_dialog_manager.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "content/public/browser/web_contents_user_data.h"
16 #include "ui/gfx/native_widget_types.h"
18 namespace web_modal {
20 class WebContentsModalDialogManagerDelegate;
22 // Per-WebContents class to manage WebContents-modal dialogs.
23 class WebContentsModalDialogManager
24 : public NativeWebContentsModalDialogManagerDelegate,
25 public content::WebContentsObserver,
26 public content::WebContentsUserData<WebContentsModalDialogManager>,
27 public content::NotificationObserver {
28 public:
29 virtual ~WebContentsModalDialogManager();
31 WebContentsModalDialogManagerDelegate* delegate() const { return delegate_; }
32 void set_delegate(WebContentsModalDialogManagerDelegate* d) { delegate_ = d; }
34 static NativeWebContentsModalDialogManager* CreateNativeManager(
35 NativeWebContentsModalDialogManagerDelegate* native_delegate);
37 // Shows the dialog as a web contents modal dialog. The dialog will notify via
38 // WillClose() when it is being destroyed.
39 void ShowDialog(NativeWebContentsModalDialog dialog);
41 // Returns true if a dialog is currently being shown.
42 bool IsShowingDialog() const;
44 // Focus the topmost modal dialog. IsShowingDialog() must be true when
45 // calling this function.
46 void FocusTopmostDialog();
48 // Set to true to prevent closing the window when a page load starts on the
49 // WebContents.
50 void SetPreventCloseOnLoadStart(NativeWebContentsModalDialog dialog,
51 bool prevent);
53 // Overriden from NativeWebContentsModalDialogManagerDelegate:
54 virtual content::WebContents* GetWebContents() const OVERRIDE;
55 // Called when a WebContentsModalDialogs we own is about to be closed.
56 virtual void WillClose(NativeWebContentsModalDialog dialog) OVERRIDE;
58 // content::NotificationObserver overrides
59 virtual void Observe(int type,
60 const content::NotificationSource& source,
61 const content::NotificationDetails& details) OVERRIDE;
63 // For testing.
64 class TestApi {
65 public:
66 explicit TestApi(WebContentsModalDialogManager* manager)
67 : manager_(manager) {}
69 void CloseAllDialogs() { manager_->CloseAllDialogs(); }
70 void ResetNativeManager(NativeWebContentsModalDialogManager* delegate) {
71 manager_->native_manager_.reset(delegate);
74 private:
75 WebContentsModalDialogManager* manager_;
77 DISALLOW_COPY_AND_ASSIGN(TestApi);
80 private:
81 explicit WebContentsModalDialogManager(content::WebContents* web_contents);
82 friend class content::WebContentsUserData<WebContentsModalDialogManager>;
84 struct DialogState {
85 explicit DialogState(NativeWebContentsModalDialog dialog);
87 NativeWebContentsModalDialog dialog;
88 bool prevent_close_on_load_start;
91 typedef std::deque<DialogState> WebContentsModalDialogList;
93 // Utility function to get the dialog state for a dialog.
94 WebContentsModalDialogList::iterator FindDialogState(
95 NativeWebContentsModalDialog dialog);
97 // Blocks/unblocks interaction with renderer process.
98 void BlockWebContentsInteraction(bool blocked);
100 bool IsWebContentsVisible() const;
102 // Closes all WebContentsModalDialogs.
103 void CloseAllDialogs();
105 // Overridden from content::WebContentsObserver:
106 virtual void DidGetIgnoredUIEvent() OVERRIDE;
107 virtual void WebContentsDestroyed(content::WebContents* tab) 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 // A scoped container for notification registries.
122 content::NotificationRegistrar registrar_;
124 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager);
127 } // namespace web_modal
129 #endif // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_