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/guest_view/web_view/web_view_guest.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
13 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_contents.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),
27 sheet_([sheet retain]),
30 extensions::WebViewGuest* web_view_guest =
31 extensions::WebViewGuest::FromWebContents(web_contents);
32 // For embedded WebContents, use the embedder's WebContents for constrained
34 web_contents_ = web_view_guest && web_view_guest->embedder_web_contents() ?
35 web_view_guest->embedder_web_contents() : web_contents;
37 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
38 WebContentsModalDialogManager::FromWebContents(web_contents_);
39 web_contents_modal_dialog_manager->ShowModalDialog(this);
42 ConstrainedWindowMac::~ConstrainedWindowMac() {
43 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
46 void ConstrainedWindowMac::ShowWebContentsModalDialog() {
50 NSWindow* parent_window = GetParentWindow();
51 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
52 if (!parent_window || !parent_view)
56 ConstrainedWindowSheetController* controller =
57 [ConstrainedWindowSheetController
58 controllerForParentWindow:parent_window];
59 [controller showSheet:sheet_ forParentView:parent_view];
62 void ConstrainedWindowMac::CloseWebContentsModalDialog() {
63 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
65 // TODO(gbillock): get this object in config, not from a global.
66 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
67 WebContentsModalDialogManager::FromWebContents(web_contents_);
69 // Will result in the delegate being deleted.
71 delegate_->OnConstrainedWindowClosed(this);
73 // Will cause this object to be deleted.
74 web_contents_modal_dialog_manager->WillClose(this);
77 void ConstrainedWindowMac::FocusWebContentsModalDialog() {
80 void ConstrainedWindowMac::PulseWebContentsModalDialog() {
81 [[ConstrainedWindowSheetController controllerForSheet:sheet_]
85 NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
86 // TODO(wittman): Ultimately this should be changed to the
87 // ConstrainedWindowSheet pointer, in conjunction with the corresponding
88 // changes to NativeWebContentsModalDialogManagerCocoa.
92 NSWindow* ConstrainedWindowMac::GetParentWindow() const {
93 // Tab contents in a tabbed browser may not be inside a window. For this
94 // reason use a browser window if possible.
95 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
97 return browser->window()->GetNativeWindow();
99 return web_contents_->GetTopLevelNativeWindow();