constrained_window_mac.mm revision 010d83a9304c5a91596085d917d248abff47903a
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.
4
5#include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
6
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
17using web_modal::WebContentsModalDialogManager;
18using web_modal::NativeWebContentsModalDialog;
19
20ConstrainedWindowMac::ConstrainedWindowMac(
21    ConstrainedWindowMacDelegate* delegate,
22    content::WebContents* web_contents,
23    id<ConstrainedWindowSheet> sheet)
24    : delegate_(delegate),
25      web_contents_(web_contents),
26      sheet_([sheet retain]),
27      shown_(false) {
28  DCHECK(web_contents);
29  DCHECK(sheet_.get());
30  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
31      WebContentsModalDialogManager::FromWebContents(web_contents);
32  web_contents_modal_dialog_manager->ShowModalDialog(this);
33}
34
35ConstrainedWindowMac::~ConstrainedWindowMac() {
36  CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
37}
38
39void ConstrainedWindowMac::ShowWebContentsModalDialog() {
40  if (shown_)
41    return;
42
43  NSWindow* parent_window = GetParentWindow();
44  NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
45  if (!parent_window || !parent_view)
46    return;
47
48  shown_ = true;
49  ConstrainedWindowSheetController* controller =
50      [ConstrainedWindowSheetController
51          controllerForParentWindow:parent_window];
52  [controller showSheet:sheet_ forParentView:parent_view];
53}
54
55void ConstrainedWindowMac::CloseWebContentsModalDialog() {
56  [[ConstrainedWindowSheetController controllerForSheet:sheet_]
57      closeSheet:sheet_];
58  // TODO(gbillock): get this object in config, not from a global.
59  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
60      WebContentsModalDialogManager::FromWebContents(web_contents_);
61
62  // Will result in the delegate being deleted.
63  if (delegate_)
64    delegate_->OnConstrainedWindowClosed(this);
65
66  // Will cause this object to be deleted.
67  web_contents_modal_dialog_manager->WillClose(this);
68}
69
70void ConstrainedWindowMac::FocusWebContentsModalDialog() {
71}
72
73void ConstrainedWindowMac::PulseWebContentsModalDialog() {
74  [[ConstrainedWindowSheetController controllerForSheet:sheet_]
75      pulseSheet:sheet_];
76}
77
78NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
79  // TODO(wittman): Ultimately this should be changed to the
80  // ConstrainedWindowSheet pointer, in conjunction with the corresponding
81  // changes to NativeWebContentsModalDialogManagerCocoa.
82  return this;
83}
84
85NSWindow* ConstrainedWindowMac::GetParentWindow() const {
86  // Tab contents in a tabbed browser may not be inside a window. For this
87  // reason use a browser window if possible.
88  Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
89  if (browser)
90    return browser->window()->GetNativeWindow();
91
92  return web_contents_->GetTopLevelNativeWindow();
93}
94