Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / web_modal / popup_manager.cc
blobabc9edef28c0fe54feff34e2be1e1efc98a89d95
1 // Copyright 2014 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.
5 #include "components/web_modal/popup_manager.h"
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"
13 using content::WebContents;
15 namespace web_modal {
17 namespace {
19 const char kPopupManagerUserDataKey[] = "PopupManager";
21 // This class provides a hook to get a PopupManager from a WebContents.
22 // The PopupManager is browser-scoped, but will use a FromWebContents API
23 // to attach to each WebContents in that browser.
24 class PopupManagerRelay : public content::WebContentsUserData<PopupManager> {
25 public:
26 explicit PopupManagerRelay(base::WeakPtr<PopupManager> manager)
27 : manager_(manager) {}
29 ~PopupManagerRelay() override {}
31 base::WeakPtr<PopupManager> manager_;
34 } // namespace
36 PopupManager::PopupManager(WebContentsModalDialogHost* host)
37 : host_(host),
38 weak_factory_(this) {}
40 PopupManager::~PopupManager() {
43 void PopupManager::ShowPopup(scoped_ptr<SinglePopupManager> manager) {
44 content::WebContents* web_contents = manager->GetBoundWebContents();
45 // TODO(gbillock): get rid of this when we handle bubbles
46 DCHECK(web_contents);
48 WebContentsModalDialogManager* wm_manager =
49 WebContentsModalDialogManager::FromWebContents(web_contents);
50 DCHECK(wm_manager);
51 wm_manager->ShowModalDialog(manager->popup());
54 void PopupManager::ShowModalDialog(gfx::NativeWindow popup,
55 content::WebContents* web_contents) {
56 // TODO make a new native popup manager and call ShowPopup.
57 // For now just lay off to WCMDM.
58 WebContentsModalDialogManager* manager =
59 WebContentsModalDialogManager::FromWebContents(web_contents);
60 manager->ShowModalDialog(popup);
63 bool PopupManager::IsWebModalDialogActive(
64 const content::WebContents* web_contents) const {
65 if (web_contents == NULL)
66 return false;
68 const WebContentsModalDialogManager* manager =
69 WebContentsModalDialogManager::FromWebContents(web_contents);
70 return manager && manager->IsDialogActive();
73 void PopupManager::WasFocused(const content::WebContents* web_contents) {
74 if (!IsWebModalDialogActive(web_contents))
75 return;
77 const WebContentsModalDialogManager* manager =
78 WebContentsModalDialogManager::FromWebContents(web_contents);
79 if (manager)
80 manager->FocusTopmostDialog();
83 void PopupManager::WillClose(gfx::NativeWindow popup) {
86 void PopupManager::RegisterWith(content::WebContents* web_contents) {
87 web_contents->SetUserData(kPopupManagerUserDataKey,
88 new PopupManagerRelay(weak_factory_.GetWeakPtr()));
89 // TODO(gbillock): Need to do something more extreme here to manage changing
90 // popup managers with popups in-flight?
93 void PopupManager::UnregisterWith(content::WebContents* web_contents) {
94 web_contents->RemoveUserData(kPopupManagerUserDataKey);
95 // TODO(gbillock): Need to do something more extreme here to manage changing
96 // popup managers with popups in-flight?
99 PopupManager* PopupManager::FromWebContents(
100 content::WebContents* web_contents) {
101 PopupManagerRelay* relay = static_cast<PopupManagerRelay*>(
102 web_contents->GetUserData(kPopupManagerUserDataKey));
103 if (!relay)
104 return NULL;
106 return relay->manager_.get();
109 gfx::NativeView PopupManager::GetHostView() const {
110 // TODO(gbillock): replace this with a PopupManagerHost or something.
111 DCHECK(host_);
112 return host_->GetHostView();
115 void PopupManager::CloseAllDialogsForTesting(
116 content::WebContents* web_contents) {
117 // TODO: re-implement, probably in terms of something in the host_,
118 // or of owned WCMDMs.
119 WebContentsModalDialogManager* manager =
120 WebContentsModalDialogManager::FromWebContents(web_contents);
121 manager->CloseAllDialogs();
124 } // namespace web_modal