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() {
35 NSView* parent_view = host_->GetHostView();
36 // Note that simply [parent_view window] for an inactive tab is nil. However,
37 // the following should always be non-nil for all WebContents containers.
38 NSWindow* parent_window =
39 delegate_->GetWebContents()->GetTopLevelNativeWindow();
42 [[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
43 showSheet:sheet_ forParentView:parent_view];
46 void SingleWebContentsDialogManagerCocoa::Hide() {
49 void SingleWebContentsDialogManagerCocoa::Close() {
50 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
53 client_->set_manager(nullptr);
54 client_->OnDialogClosing(); // |client_| might delete itself here.
57 delegate_->WillClose(dialog());
60 void SingleWebContentsDialogManagerCocoa::Focus() {
63 void SingleWebContentsDialogManagerCocoa::Pulse() {
64 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
68 void SingleWebContentsDialogManagerCocoa::HostChanged(
69 web_modal::WebContentsModalDialogHost* new_host) {
70 // No need to observe the host. For Cocoa, the constrained window controller
71 // will reposition the dialog when necessary. The host can also never change.
72 // Tabs showing a dialog can not be dragged off a Cocoa browser window.
73 // However, closing a tab with a dialog open will set the host back to null.
74 DCHECK_NE(!!host_, !!new_host);
78 gfx::NativeWindow SingleWebContentsDialogManagerCocoa::dialog() {
79 return [sheet_ sheetWindow];