web_contents_modal_dialog_manager.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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.
4
5#ifndef COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
6#define COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
7
8#include <deque>
9
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"
15
16namespace web_modal {
17
18class WebContentsModalDialogManagerDelegate;
19
20// Per-WebContents class to manage WebContents-modal dialogs.
21class WebContentsModalDialogManager
22    : public SingleWebContentsDialogManagerDelegate,
23      public content::WebContentsObserver,
24      public content::WebContentsUserData<WebContentsModalDialogManager> {
25 public:
26  virtual ~WebContentsModalDialogManager();
27
28  WebContentsModalDialogManagerDelegate* delegate() const { return delegate_; }
29  void SetDelegate(WebContentsModalDialogManagerDelegate* d);
30
31  static SingleWebContentsDialogManager* CreateNativeWebModalManager(
32      SingleWebContentsDialogManagerDelegate* native_delegate);
33
34  // Shows the dialog as a web contents modal dialog. The dialog will notify via
35  // WillClose() when it is being destroyed.
36  void ShowModalDialog(NativeWebContentsModalDialog dialog);
37
38  // Allow clients to supply their own native dialog manager. Suitable for
39  // bubble clients.
40  void ShowDialogWithManager(
41      NativeWebContentsModalDialog dialog,
42      scoped_ptr<SingleWebContentsDialogManager> manager);
43
44  // Returns true if any dialogs are active and not closed.
45  bool IsDialogActive() const;
46
47  // Focus the topmost modal dialog.  IsDialogActive() must be true when calling
48  // this function.
49  void FocusTopmostDialog();
50
51  // Set to true to close the window when a page load starts on the WebContents.
52  void SetCloseOnInterstitialPage(NativeWebContentsModalDialog dialog,
53                                  bool close);
54
55  // Overriden from SingleWebContentsDialogManagerDelegate:
56  virtual content::WebContents* GetWebContents() const OVERRIDE;
57  // Called when a WebContentsModalDialogs we own is about to be closed.
58  virtual void WillClose(NativeWebContentsModalDialog dialog) OVERRIDE;
59
60  // For testing.
61  class TestApi {
62   public:
63    explicit TestApi(WebContentsModalDialogManager* manager)
64        : manager_(manager) {}
65
66    void CloseAllDialogs() { manager_->CloseAllDialogs(); }
67    void DidAttachInterstitialPage() { manager_->DidAttachInterstitialPage(); }
68    void WebContentsWasShown() { manager_->WasShown(); }
69    void WebContentsWasHidden() { manager_->WasHidden(); }
70
71   private:
72    WebContentsModalDialogManager* manager_;
73
74    DISALLOW_COPY_AND_ASSIGN(TestApi);
75  };
76
77 private:
78  explicit WebContentsModalDialogManager(content::WebContents* web_contents);
79  friend class content::WebContentsUserData<WebContentsModalDialogManager>;
80
81  struct DialogState {
82    DialogState(NativeWebContentsModalDialog dialog,
83                scoped_ptr<SingleWebContentsDialogManager> manager);
84    ~DialogState();
85
86    NativeWebContentsModalDialog dialog;
87    scoped_ptr<SingleWebContentsDialogManager> manager;
88    bool close_on_interstitial_webui;
89  };
90
91  typedef std::deque<DialogState*> WebContentsModalDialogList;
92
93  // Utility function to get the dialog state for a dialog.
94  WebContentsModalDialogList::iterator FindDialogState(
95      NativeWebContentsModalDialog dialog);
96
97  // Blocks/unblocks interaction with renderer process.
98  void BlockWebContentsInteraction(bool blocked);
99
100  bool IsWebContentsVisible() const;
101
102  // Closes all WebContentsModalDialogs.
103  void CloseAllDialogs();
104
105  // Overridden from content::WebContentsObserver:
106  virtual void DidNavigateMainFrame(
107      const content::LoadCommittedDetails& details,
108      const content::FrameNavigateParams& params) OVERRIDE;
109  virtual void DidGetIgnoredUIEvent() OVERRIDE;
110  virtual void WasShown() OVERRIDE;
111  virtual void WasHidden() OVERRIDE;
112  virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE;
113  virtual void DidAttachInterstitialPage() OVERRIDE;
114
115  // Delegate for notifying our owner about stuff. Not owned by us.
116  WebContentsModalDialogManagerDelegate* delegate_;
117
118  // All active dialogs.
119  WebContentsModalDialogList child_dialogs_;
120
121  // True while closing the dialogs on WebContents close.
122  bool closing_all_dialogs_;
123
124  DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager);
125};
126
127}  // namespace web_modal
128
129#endif  // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
130