1 // Copyright (c) 2012 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/constrained_window/constrained_window_mac.h"
7 #include "base/logging.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.h"
18 using web_modal::WebContentsModalDialogManager;
19 using web_modal::NativeWebContentsModalDialog;
21 ConstrainedWindowMac::ConstrainedWindowMac(
22 ConstrainedWindowMacDelegate* delegate,
23 content::WebContents* web_contents,
24 id<ConstrainedWindowSheet> sheet)
25 : delegate_(delegate),
26 web_contents_(web_contents),
27 sheet_([sheet retain]),
31 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
32 WebContentsModalDialogManager::FromWebContents(web_contents);
33 web_contents_modal_dialog_manager->ShowDialog(this);
36 ConstrainedWindowMac::~ConstrainedWindowMac() {
37 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
40 void ConstrainedWindowMac::ShowWebContentsModalDialog() {
44 NSWindow* parent_window = GetParentWindow();
45 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
46 if (!parent_window || !parent_view)
50 ConstrainedWindowSheetController* controller =
51 [ConstrainedWindowSheetController
52 controllerForParentWindow:parent_window];
53 [controller showSheet:sheet_ forParentView:parent_view];
56 void ConstrainedWindowMac::CloseWebContentsModalDialog() {
57 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
59 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
60 WebContentsModalDialogManager::FromWebContents(web_contents_);
61 web_contents_modal_dialog_manager->WillClose(this);
63 delegate_->OnConstrainedWindowClosed(this);
66 void ConstrainedWindowMac::FocusWebContentsModalDialog() {
69 void ConstrainedWindowMac::PulseWebContentsModalDialog() {
70 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
74 NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
75 // TODO(wittman): Ultimately this should be changed to the
76 // ConstrainedWindowSheet pointer, in conjunction with the corresponding
77 // changes to NativeWebContentsModalDialogManagerCocoa.
81 NSWindow* ConstrainedWindowMac::GetParentWindow() const {
82 // Tab contents in a tabbed browser may not be inside a window. For this
83 // reason use a browser window if possible.
84 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
86 return browser->window()->GetNativeWindow();
88 return web_contents_->GetView()->GetTopLevelNativeWindow();