Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / constrained_window / constrained_window_mac.mm
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/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"
17
18 using web_modal::WebContentsModalDialogManager;
19 using web_modal::NativeWebContentsModalDialog;
20
21 ConstrainedWindowMac::ConstrainedWindowMac(
22     ConstrainedWindowMacDelegate* delegate,
23     content::WebContents* web_contents,
24     id<ConstrainedWindowSheet> sheet)
25     : delegate_(delegate),
26       web_contents_(NULL),
27       sheet_([sheet retain]),
28       shown_(false) {
29   DCHECK(web_contents);
30   extensions::WebViewGuest* web_view_guest =
31     extensions::WebViewGuest::FromWebContents(web_contents);
32   // For embedded WebContents, use the embedder's WebContents for constrained
33   // window.
34   web_contents_ = web_view_guest && web_view_guest->embedder_web_contents() ?
35                       web_view_guest->embedder_web_contents() : web_contents;
36   DCHECK(sheet_.get());
37   WebContentsModalDialogManager* web_contents_modal_dialog_manager =
38       WebContentsModalDialogManager::FromWebContents(web_contents_);
39   web_contents_modal_dialog_manager->ShowModalDialog(this);
40 }
41
42 ConstrainedWindowMac::~ConstrainedWindowMac() {
43   CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44 }
45
46 void ConstrainedWindowMac::ShowWebContentsModalDialog() {
47   if (shown_)
48     return;
49
50   NSWindow* parent_window = GetParentWindow();
51   NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
52   if (!parent_window || !parent_view)
53     return;
54
55   shown_ = true;
56   ConstrainedWindowSheetController* controller =
57       [ConstrainedWindowSheetController
58           controllerForParentWindow:parent_window];
59   [controller showSheet:sheet_ forParentView:parent_view];
60 }
61
62 void ConstrainedWindowMac::CloseWebContentsModalDialog() {
63   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
64       closeSheet: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_);
68
69   // Will result in the delegate being deleted.
70   if (delegate_)
71     delegate_->OnConstrainedWindowClosed(this);
72
73   // Will cause this object to be deleted.
74   web_contents_modal_dialog_manager->WillClose(this);
75 }
76
77 void ConstrainedWindowMac::FocusWebContentsModalDialog() {
78 }
79
80 void ConstrainedWindowMac::PulseWebContentsModalDialog() {
81   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
82       pulseSheet:sheet_];
83 }
84
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.
89   return this;
90 }
91
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_);
96   if (browser)
97     return browser->window()->GetNativeWindow();
98
99   return web_contents_->GetTopLevelNativeWindow();
100 }