OLD | NEW |
(Empty) | |
| 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 #include "components/web_modal/popup_manager.h" |
| 6 |
| 7 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 8 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_contents_user_data.h" |
| 11 #include "ui/gfx/geometry/size.h" |
| 12 |
| 13 using content::WebContents; |
| 14 |
| 15 namespace web_modal { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const char kPopupManagerUserDataKey[] = "PopupManager"; |
| 20 |
| 21 class PopupManagerRelay : public content::WebContentsUserData<PopupManager> { |
| 22 public: |
| 23 explicit PopupManagerRelay(base::WeakPtr<PopupManager> manager) |
| 24 : manager_(manager) {} |
| 25 |
| 26 virtual ~PopupManagerRelay() {} |
| 27 |
| 28 base::WeakPtr<PopupManager> manager_; |
| 29 }; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 PopupManager::PopupManager(WebContentsModalDialogHost* host) |
| 34 : host_(host), |
| 35 weak_factory_(this) {} |
| 36 |
| 37 PopupManager::~PopupManager() { |
| 38 } |
| 39 |
| 40 void PopupManager::ShowPopup(scoped_ptr<SinglePopupManager> manager) { |
| 41 content::WebContents* web_contents = manager->GetBoundWebContents(); |
| 42 // TODO(gbillock): get rid of this when we handle bubbles |
| 43 DCHECK(web_contents); |
| 44 |
| 45 // TODO(gbillock): remove when we port the popup management logic to this |
| 46 // class. |
| 47 NativeWebContentsModalDialog dialog = |
| 48 static_cast<NativeWebContentsModalDialog>(manager->popup()); |
| 49 |
| 50 WebContentsModalDialogManager* wm_manager = |
| 51 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 52 DCHECK(wm_manager); |
| 53 wm_manager->ShowModalDialog(dialog); |
| 54 } |
| 55 |
| 56 void PopupManager::ShowModalDialog(NativePopup popup, |
| 57 content::WebContents* web_contents) { |
| 58 // TODO make a new native popup manager and call ShowPopup. |
| 59 // For now just lay off to WCMDM. |
| 60 WebContentsModalDialogManager* manager = |
| 61 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 62 manager->ShowModalDialog(popup); |
| 63 } |
| 64 |
| 65 bool PopupManager::IsWebModalDialogActive( |
| 66 const content::WebContents* web_contents) const { |
| 67 if (web_contents == NULL) |
| 68 return false; |
| 69 |
| 70 const WebContentsModalDialogManager* manager = |
| 71 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 72 return manager && manager->IsDialogActive(); |
| 73 } |
| 74 |
| 75 void PopupManager::WasFocused(const content::WebContents* web_contents) { |
| 76 if (!IsWebModalDialogActive(web_contents)) |
| 77 return; |
| 78 |
| 79 const WebContentsModalDialogManager* manager = |
| 80 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 81 if (manager) |
| 82 manager->FocusTopmostDialog(); |
| 83 } |
| 84 |
| 85 void PopupManager::WillClose(NativePopup popup) { |
| 86 } |
| 87 |
| 88 void PopupManager::RegisterWith(content::WebContents* web_contents) { |
| 89 web_contents->SetUserData(kPopupManagerUserDataKey, |
| 90 new PopupManagerRelay(weak_factory_.GetWeakPtr())); |
| 91 // TODO(gbillock): Need to do something more extreme here to manage changing |
| 92 // popup managers with popups in-flight? |
| 93 } |
| 94 |
| 95 void PopupManager::UnregisterWith(content::WebContents* web_contents) { |
| 96 web_contents->RemoveUserData(kPopupManagerUserDataKey); |
| 97 // TODO(gbillock): Need to do something more extreme here to manage changing |
| 98 // popup managers with popups in-flight? |
| 99 } |
| 100 |
| 101 PopupManager* PopupManager::FromWebContents( |
| 102 content::WebContents* web_contents) { |
| 103 PopupManagerRelay* relay = static_cast<PopupManagerRelay*>( |
| 104 web_contents->GetUserData(kPopupManagerUserDataKey)); |
| 105 if (!relay) |
| 106 return NULL; |
| 107 |
| 108 return relay->manager_.get(); |
| 109 } |
| 110 |
| 111 gfx::NativeView PopupManager::GetHostView() const { |
| 112 // TODO(gbillock): replace this with a PopupManagerHost or something. |
| 113 DCHECK(host_); |
| 114 return host_->GetHostView(); |
| 115 } |
| 116 |
| 117 void PopupManager::CloseAllDialogsForTesting( |
| 118 content::WebContents* web_contents) { |
| 119 // TODO: re-implement, probably in terms of something in the host_, |
| 120 // or of owned WCMDMs. |
| 121 WebContentsModalDialogManager* manager = |
| 122 WebContentsModalDialogManager::FromWebContents(web_contents); |
| 123 manager->CloseAllDialogs(); |
| 124 } |
| 125 |
| 126 } // namespace web_modal |
OLD | NEW |