web_contents_modal_dialog_manager.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
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/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"
17
18namespace web_modal {
19
20class WebContentsModalDialogManagerDelegate;
21
22// Per-WebContents class to manage WebContents-modal dialogs.
23class WebContentsModalDialogManager
24    : public NativeWebContentsModalDialogManagerDelegate,
25      public content::WebContentsObserver,
26      public content::WebContentsUserData<WebContentsModalDialogManager>,
27      public content::NotificationObserver {
28 public:
29  virtual ~WebContentsModalDialogManager();
30
31  WebContentsModalDialogManagerDelegate* delegate() const { return delegate_; }
32  void set_delegate(WebContentsModalDialogManagerDelegate* d) { delegate_ = d; }
33
34  static NativeWebContentsModalDialogManager* CreateNativeManager(
35      NativeWebContentsModalDialogManagerDelegate* native_delegate);
36
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);
40
41  // Returns true if a dialog is currently being shown.
42  bool IsShowingDialog() const;
43
44  // Focus the topmost modal dialog.  IsShowingDialog() must be true when
45  // calling this function.
46  void FocusTopmostDialog();
47
48  // Overriden from NativeWebContentsModalDialogManagerDelegate:
49  virtual content::WebContents* GetWebContents() const OVERRIDE;
50  // Called when a WebContentsModalDialogs we own is about to be closed.
51  virtual void WillClose(NativeWebContentsModalDialog dialog) OVERRIDE;
52
53  // content::NotificationObserver overrides
54  virtual void Observe(int type,
55                       const content::NotificationSource& source,
56                       const content::NotificationDetails& details) OVERRIDE;
57
58  // For testing.
59  class TestApi {
60   public:
61    explicit TestApi(WebContentsModalDialogManager* manager)
62        : manager_(manager) {}
63
64    void CloseAllDialogs() { manager_->CloseAllDialogs(); }
65    void ResetNativeManager(NativeWebContentsModalDialogManager* delegate) {
66      manager_->native_manager_.reset(delegate);
67    }
68
69   private:
70    WebContentsModalDialogManager* manager_;
71
72    DISALLOW_COPY_AND_ASSIGN(TestApi);
73  };
74
75 private:
76  explicit WebContentsModalDialogManager(content::WebContents* web_contents);
77  friend class content::WebContentsUserData<WebContentsModalDialogManager>;
78
79  typedef std::deque<NativeWebContentsModalDialog> WebContentsModalDialogList;
80
81  // Blocks/unblocks interaction with renderer process.
82  void BlockWebContentsInteraction(bool blocked);
83
84  bool IsWebContentsVisible() const;
85
86  // Closes all WebContentsModalDialogs.
87  void CloseAllDialogs();
88
89  // Overridden from content::WebContentsObserver:
90  virtual void DidNavigateMainFrame(
91      const content::LoadCommittedDetails& details,
92      const content::FrameNavigateParams& params) OVERRIDE;
93  virtual void DidGetIgnoredUIEvent() OVERRIDE;
94  virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE;
95
96  // Delegate for notifying our owner about stuff. Not owned by us.
97  WebContentsModalDialogManagerDelegate* delegate_;
98
99  // Delegate for native UI-specific functions on the dialog.
100  scoped_ptr<NativeWebContentsModalDialogManager> native_manager_;
101
102  // All active dialogs.
103  WebContentsModalDialogList child_dialogs_;
104
105  // True while closing the dialogs on WebContents close.
106  bool closing_all_dialogs_;
107
108  // A scoped container for notification registries.
109  content::NotificationRegistrar registrar_;
110
111  DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager);
112};
113
114}  // namespace web_modal
115
116#endif  // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
117