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 #include "chrome/browser/ui/cocoa/web_contents_modal_dialog_host_cocoa.h"
7 #include "chrome/browser/ui/browser_finder.h"
8 #include "chrome/browser/ui/tab_dialogs.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/size.h"
14 WebContentsModalDialogHostCocoa::WebContentsModalDialogHostCocoa(
15 ConstrainedWindowSheetController* sheet_controller)
16 : sheet_controller_(sheet_controller) {
19 WebContentsModalDialogHostCocoa::~WebContentsModalDialogHostCocoa() {
20 // Toolkit-Views calls OnHostDestroying on observers here, but the Cocoa host
21 // doesn't need to be observed.
24 gfx::NativeView WebContentsModalDialogHostCocoa::GetHostView() const {
25 // To avoid the constrained window controller having to know about the browser
26 // view layout, use the active tab in the parent window.
27 NSWindow* parent_window = [sheet_controller_ parentWindow];
28 Browser* browser = chrome::FindBrowserWithWindow(parent_window);
29 // This could be null for packaged app windows, but this dialog host is
30 // currently only used for browsers.
32 content::WebContents* web_contents =
33 browser->tab_strip_model()->GetActiveWebContents();
35 TabDialogs* tab_dialogs = TabDialogs::FromWebContents(web_contents);
38 // Note this returns the WebContents' superview, so it doesn't really matter
39 // which WebContents inside the browser we actually chose above.
40 return tab_dialogs->GetDialogParentView();
43 gfx::Point WebContentsModalDialogHostCocoa::GetDialogPosition(
44 const gfx::Size& size) {
45 // Dialogs are always re-positioned by the constrained window sheet controller
46 // so nothing interesting to return yet.
50 void WebContentsModalDialogHostCocoa::AddObserver(
51 web_modal::ModalDialogHostObserver* observer) {
54 void WebContentsModalDialogHostCocoa::RemoveObserver(
55 web_modal::ModalDialogHostObserver* observer) {
59 gfx::Size WebContentsModalDialogHostCocoa::GetMaximumDialogSize() {
60 // The dialog should try to fit within the overlay for the web contents.
61 // Note that, for things like print preview, this is just a suggested maximum.
63 [sheet_controller_ overlayWindowSizeForParentView:GetHostView()]);