Give Cocoa browser windows a WebContentsModalDialogHost
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / single_web_contents_dialog_manager_cocoa.mm
blob518b2288864256e3c2b22b0ed89ae924fa34cd4e
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)
18     : client_(client),
19       sheet_([sheet retain]),
20       delegate_(delegate),
21       host_(nullptr),
22       shown_(false) {
23   if (client)
24     client->set_manager(this);
27 SingleWebContentsDialogManagerCocoa::~SingleWebContentsDialogManagerCocoa() {
30 void SingleWebContentsDialogManagerCocoa::Show() {
31   if (shown_)
32     return;
34   DCHECK(host_);
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();
41   shown_ = true;
42   [[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
43       showSheet:sheet_ forParentView:parent_view];
46 void SingleWebContentsDialogManagerCocoa::Hide() {
49 void SingleWebContentsDialogManagerCocoa::Close() {
50   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
51       closeSheet:sheet_];
52   if (client_) {
53     client_->set_manager(nullptr);
54     client_->OnDialogClosing();  // |client_| might delete itself here.
55     client_ = nullptr;
56   }
57   delegate_->WillClose(dialog());
60 void SingleWebContentsDialogManagerCocoa::Focus() {
63 void SingleWebContentsDialogManagerCocoa::Pulse() {
64   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
65       pulseSheet: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);
75   host_ = new_host;
78 gfx::NativeWindow SingleWebContentsDialogManagerCocoa::dialog() {
79   return [sheet_ sheetWindow];