1 // Copyright 2015 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 #import "chrome/browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h"
7 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
9 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
10 #include "components/web_modal/web_contents_modal_dialog_host.h"
11 #include "components/web_modal/web_contents_modal_dialog_manager.h"
12 #include "content/public/browser/web_contents.h"
14 SingleWebContentsDialogManagerCocoa::SingleWebContentsDialogManagerCocoa(
15 ConstrainedWindowMac* client,
16 id<ConstrainedWindowSheet> sheet,
17 web_modal::SingleWebContentsDialogManagerDelegate* delegate)
19 sheet_([sheet retain]),
24 client->set_manager(this);
27 SingleWebContentsDialogManagerCocoa::~SingleWebContentsDialogManagerCocoa() {
30 void SingleWebContentsDialogManagerCocoa::Show() {
34 // If a dialog is initially shown on a hidden/background WebContents, the
35 // |delegate_| will defer the Show() until the WebContents is shown. If the
36 // defer happens during tab closure or tab dragging, a suspected data race or
37 // ObserverList ordering may result in |host_| being null here. If the tab is
38 // closing anyway, it doesn't matter. For tab dragging, avoid a crash, but the
39 // user may have to switch tabs again to see the dialog. See
40 // http://crbug.com/514826 for details.
44 NSView* parent_view = host_->GetHostView();
45 // Note that simply [parent_view window] for an inactive tab is nil. However,
46 // the following should always be non-nil for all WebContents containers.
47 NSWindow* parent_window =
48 delegate_->GetWebContents()->GetTopLevelNativeWindow();
51 [[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
52 showSheet:sheet_ forParentView:parent_view];
55 void SingleWebContentsDialogManagerCocoa::Hide() {
58 void SingleWebContentsDialogManagerCocoa::Close() {
59 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
61 client_->set_manager(nullptr);
62 client_->OnDialogClosing(); // |client_| might delete itself here.
63 delegate_->WillClose(dialog()); // Deletes |this|.
66 void SingleWebContentsDialogManagerCocoa::Focus() {
69 void SingleWebContentsDialogManagerCocoa::Pulse() {
70 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
74 void SingleWebContentsDialogManagerCocoa::HostChanged(
75 web_modal::WebContentsModalDialogHost* new_host) {
76 // No need to observe the host. For Cocoa, the constrained window controller
77 // will reposition the dialog when necessary. The host can also never change.
78 // Tabs showing a dialog can not be dragged off a Cocoa browser window.
79 // However, closing a tab with a dialog open will set the host back to null.
80 DCHECK_NE(!!host_, !!new_host);
84 gfx::NativeWindow SingleWebContentsDialogManagerCocoa::dialog() {
85 return [sheet_ sheetWindow];