constrained_window_mac.mm revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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#include "content/public/browser/web_contents_view.h"
17
18using web_modal::WebContentsModalDialogManager;
19using web_modal::NativeWebContentsModalDialog;
20
21ConstrainedWindowMac::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]),
28      shown_(false) {
29  DCHECK(web_contents);
30  DCHECK(sheet_.get());
31  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
32      WebContentsModalDialogManager::FromWebContents(web_contents);
33  web_contents_modal_dialog_manager->ShowDialog(this);
34}
35
36ConstrainedWindowMac::~ConstrainedWindowMac() {
37  CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
38}
39
40void ConstrainedWindowMac::ShowWebContentsModalDialog() {
41  if (shown_)
42    return;
43
44  NSWindow* parent_window = GetParentWindow();
45  NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
46  if (!parent_window || !parent_view)
47    return;
48
49  shown_ = true;
50  ConstrainedWindowSheetController* controller =
51      [ConstrainedWindowSheetController
52          controllerForParentWindow:parent_window];
53  [controller showSheet:sheet_ forParentView:parent_view];
54}
55
56void ConstrainedWindowMac::CloseWebContentsModalDialog() {
57  [[ConstrainedWindowSheetController controllerForSheet:sheet_]
58      closeSheet:sheet_];
59  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
60      WebContentsModalDialogManager::FromWebContents(web_contents_);
61  web_contents_modal_dialog_manager->WillClose(this);
62  if (delegate_)
63    delegate_->OnConstrainedWindowClosed(this);
64}
65
66void ConstrainedWindowMac::FocusWebContentsModalDialog() {
67}
68
69void ConstrainedWindowMac::PulseWebContentsModalDialog() {
70  [[ConstrainedWindowSheetController controllerForSheet:sheet_]
71      pulseSheet:sheet_];
72}
73
74NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
75  // TODO(wittman): Ultimately this should be changed to the
76  // ConstrainedWindowSheet pointer, in conjunction with the corresponding
77  // changes to NativeWebContentsModalDialogManagerCocoa.
78  return this;
79}
80
81NSWindow* 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_);
85  if (browser)
86    return browser->window()->GetNativeWindow();
87
88  return web_contents_->GetView()->GetTopLevelNativeWindow();
89}
90