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() : false; | |
Mike Wittman
2014/06/25 02:38:50
nit: manager && manager->IsDialogActive()
Greg Billock
2014/06/25 19:06:14
Done.
| |
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 PopupManager* PopupManager::FromWebContents( | |
96 content::WebContents* web_contents) { | |
97 PopupManagerRelay* relay = static_cast<PopupManagerRelay*>( | |
98 web_contents->GetUserData(kPopupManagerUserDataKey)); | |
99 if (!relay) | |
100 return NULL; | |
101 | |
102 return relay->manager_.get(); | |
103 } | |
104 | |
105 gfx::NativeView PopupManager::GetHostView() const { | |
106 // TODO(gbillock): replace this with a PopupManagerHost or something. | |
107 return host_->GetHostView(); | |
108 } | |
109 | |
110 void PopupManager::CloseAllDialogsForTesting() { | |
111 // TODO: implement, probably in terms of something in the host_ for now, | |
Mike Wittman
2014/06/25 02:38:50
Needs resolution.
Greg Billock
2014/06/25 19:06:14
Will do. I think long term we'll absorb WCMDM, but
| |
112 // or just do something different at call sites. | |
113 | |
114 //Widget* widget = Widget::GetWidgetForNativeView(GetHostView()) | |
115 //if (!widget) | |
116 // return; | |
117 //Browser* browser = BrowserFinder::FindBrowserWithWindow( | |
118 // widget->GetNativeWindow()); | |
119 //if (!browser) | |
120 // return; | |
121 // | |
122 //for (int i = 0; i < browser->tab_strip_model()->count(); i++) | |
123 // content::WebContents* web_contents = | |
124 // browser->tab_strip_model()->GetWebContentsAt(i); | |
125 // if (!web_contents) continue; | |
126 // const WebContentsModalDialogManager* manager = | |
127 // WebContentsModalDialogManager::FromWebContents(web_contents); | |
128 // manager->CloseAllDialogs(); | |
129 //} | |
130 } | |
131 | |
132 } // namespace web_modal | |
OLD | NEW |