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/web_contents_observer.h" 13 #include "content/public/browser/web_contents_user_data.h" 14 #include "ui/gfx/native_widget_types.h" 15 16 namespace web_modal { 17 18 class WebContentsModalDialogManagerDelegate; 19 20 // Per-WebContents class to manage WebContents-modal dialogs. 21 class WebContentsModalDialogManager 22 : public NativeWebContentsModalDialogManagerDelegate, 23 public content::WebContentsObserver, 24 public content::WebContentsUserData<WebContentsModalDialogManager> { 25 public: 26 virtual ~WebContentsModalDialogManager(); 27 delegate()28 WebContentsModalDialogManagerDelegate* delegate() const { return delegate_; } 29 void SetDelegate(WebContentsModalDialogManagerDelegate* d); 30 31 static NativeWebContentsModalDialogManager* CreateNativeManager( 32 NativeWebContentsModalDialogManagerDelegate* 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 ShowDialog(NativeWebContentsModalDialog dialog); 37 38 // Returns true if any dialogs are active and not closed. 39 bool IsDialogActive() const; 40 41 // Focus the topmost modal dialog. IsDialogActive() must be true when calling 42 // this function. 43 void FocusTopmostDialog(); 44 45 // Set to true to close the window when a page load starts on the WebContents. 46 void SetCloseOnInterstitialPage(NativeWebContentsModalDialog dialog, 47 bool close); 48 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; 53 54 // For testing. 55 class TestApi { 56 public: TestApi(WebContentsModalDialogManager * manager)57 explicit TestApi(WebContentsModalDialogManager* manager) 58 : manager_(manager) {} 59 CloseAllDialogs()60 void CloseAllDialogs() { manager_->CloseAllDialogs(); } DidAttachInterstitialPage()61 void DidAttachInterstitialPage() { manager_->DidAttachInterstitialPage(); } ResetNativeManager(NativeWebContentsModalDialogManager * delegate)62 void ResetNativeManager(NativeWebContentsModalDialogManager* delegate) { 63 manager_->native_manager_.reset(delegate); 64 } WebContentsWasShown()65 void WebContentsWasShown() { manager_->WasShown(); } WebContentsWasHidden()66 void WebContentsWasHidden() { manager_->WasHidden(); } 67 68 private: 69 WebContentsModalDialogManager* manager_; 70 71 DISALLOW_COPY_AND_ASSIGN(TestApi); 72 }; 73 74 private: 75 explicit WebContentsModalDialogManager(content::WebContents* web_contents); 76 friend class content::WebContentsUserData<WebContentsModalDialogManager>; 77 78 struct DialogState { 79 explicit DialogState(NativeWebContentsModalDialog dialog); 80 81 NativeWebContentsModalDialog dialog; 82 bool close_on_interstitial_webui; 83 }; 84 85 typedef std::deque<DialogState> WebContentsModalDialogList; 86 87 // Utility function to get the dialog state for a dialog. 88 WebContentsModalDialogList::iterator FindDialogState( 89 NativeWebContentsModalDialog dialog); 90 91 // Blocks/unblocks interaction with renderer process. 92 void BlockWebContentsInteraction(bool blocked); 93 94 bool IsWebContentsVisible() const; 95 96 // Closes all WebContentsModalDialogs. 97 void CloseAllDialogs(); 98 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; 108 109 // Delegate for notifying our owner about stuff. Not owned by us. 110 WebContentsModalDialogManagerDelegate* delegate_; 111 112 // Delegate for native UI-specific functions on the dialog. 113 scoped_ptr<NativeWebContentsModalDialogManager> native_manager_; 114 115 // All active dialogs. 116 WebContentsModalDialogList child_dialogs_; 117 118 // True while closing the dialogs on WebContents close. 119 bool closing_all_dialogs_; 120 121 DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogManager); 122 }; 123 124 } // namespace web_modal 125 126 #endif // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_ 127