OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac2.h" | 5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac2.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/ui/browser_finder.h" | 8 #include "chrome/browser/ui/browser_finder.h" |
9 #include "chrome/browser/ui/browser_window.h" | 9 #include "chrome/browser/ui/browser_window.h" |
10 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_co
ntroller.h" | 10 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_co
ntroller.h" |
11 #include "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" | 11 #include "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" |
12 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 12 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/notification_types.h" | |
15 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
16 #include "content/public/browser/web_contents_view.h" | 14 #include "content/public/browser/web_contents_view.h" |
17 | 15 |
18 ConstrainedWindowMac2::ConstrainedWindowMac2( | 16 ConstrainedWindowMac2::ConstrainedWindowMac2( |
19 ConstrainedWindowMacDelegate2* delegate, | 17 ConstrainedWindowMacDelegate2* delegate, |
20 content::WebContents* web_contents, | 18 content::WebContents* web_contents, |
21 NSWindow* window) | 19 NSWindow* window) |
22 : delegate_(delegate), | 20 : delegate_(delegate), |
23 web_contents_(web_contents), | 21 web_contents_(web_contents), |
24 window_([window retain]), | 22 window_([window retain]) { |
25 pending_show_(false) { | |
26 DCHECK(web_contents); | 23 DCHECK(web_contents); |
27 DCHECK(window_.get()); | 24 DCHECK(window_.get()); |
28 ConstrainedWindowTabHelper* constrained_window_tab_helper = | 25 ConstrainedWindowTabHelper* constrained_window_tab_helper = |
29 ConstrainedWindowTabHelper::FromWebContents(web_contents); | 26 ConstrainedWindowTabHelper::FromWebContents(web_contents); |
30 constrained_window_tab_helper->AddConstrainedDialog(this); | 27 constrained_window_tab_helper->AddConstrainedDialog(this); |
31 | |
32 registrar_.Add(this, | |
33 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
34 content::Source<content::WebContents>(web_contents)); | |
35 } | 28 } |
36 | 29 |
37 ConstrainedWindowMac2::~ConstrainedWindowMac2() { | 30 ConstrainedWindowMac2::~ConstrainedWindowMac2() { |
38 } | 31 } |
39 | 32 |
40 void ConstrainedWindowMac2::ShowConstrainedWindow() { | 33 void ConstrainedWindowMac2::ShowConstrainedWindow() { |
41 NSWindow* parent_window = GetParentWindow(); | 34 NSWindow* parent_window = GetParentWindow(); |
| 35 if (!parent_window) |
| 36 return; |
| 37 |
42 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_); | 38 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_); |
43 if (!parent_window || !parent_view) { | 39 DCHECK(parent_view); |
44 pending_show_ = true; | |
45 return; | |
46 } | |
47 | 40 |
48 ConstrainedWindowSheetController* controller = | 41 ConstrainedWindowSheetController* controller = |
49 [ConstrainedWindowSheetController | 42 [ConstrainedWindowSheetController |
50 controllerForParentWindow:parent_window]; | 43 controllerForParentWindow:parent_window]; |
51 [controller showSheet:window_ forParentView:parent_view]; | 44 [controller showSheet:window_ forParentView:parent_view]; |
52 } | 45 } |
53 | 46 |
54 void ConstrainedWindowMac2::CloseConstrainedWindow() { | 47 void ConstrainedWindowMac2::CloseConstrainedWindow() { |
55 // This function may be called even if the constrained window was never shown. | |
56 // Unset |pending_show_| to prevent the window from being reshown. | |
57 pending_show_ = false; | |
58 | |
59 [[ConstrainedWindowSheetController controllerForSheet:window_] | 48 [[ConstrainedWindowSheetController controllerForSheet:window_] |
60 closeSheet:window_]; | 49 closeSheet:window_]; |
61 ConstrainedWindowTabHelper* constrained_window_tab_helper = | 50 ConstrainedWindowTabHelper* constrained_window_tab_helper = |
62 ConstrainedWindowTabHelper::FromWebContents(web_contents_); | 51 ConstrainedWindowTabHelper::FromWebContents(web_contents_); |
63 constrained_window_tab_helper->WillClose(this); | 52 constrained_window_tab_helper->WillClose(this); |
64 if (delegate_) | 53 if (delegate_) |
65 delegate_->OnConstrainedWindowClosed(this); | 54 delegate_->OnConstrainedWindowClosed(this); |
66 } | 55 } |
67 | 56 |
68 void ConstrainedWindowMac2::PulseConstrainedWindow() { | 57 void ConstrainedWindowMac2::PulseConstrainedWindow() { |
69 [[ConstrainedWindowSheetController controllerForSheet:window_] | 58 [[ConstrainedWindowSheetController controllerForSheet:window_] |
70 pulseSheet:window_]; | 59 pulseSheet:window_]; |
71 } | 60 } |
72 | 61 |
73 gfx::NativeWindow ConstrainedWindowMac2::GetNativeWindow() { | 62 gfx::NativeWindow ConstrainedWindowMac2::GetNativeWindow() { |
74 return window_; | 63 return window_; |
75 } | 64 } |
76 | 65 |
77 bool ConstrainedWindowMac2::CanShowConstrainedWindow() { | 66 bool ConstrainedWindowMac2::CanShowConstrainedWindow() { |
78 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 67 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
79 if (!browser) | 68 if (!browser) |
80 return true; | 69 return true; |
81 return !browser->window()->IsInstantTabShowing(); | 70 return !browser->window()->IsInstantTabShowing(); |
82 } | 71 } |
83 | 72 |
84 void ConstrainedWindowMac2::Observe( | |
85 int type, | |
86 const content::NotificationSource& source, | |
87 const content::NotificationDetails& details) { | |
88 if (type != content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED) { | |
89 NOTREACHED(); | |
90 return; | |
91 } | |
92 | |
93 if (pending_show_) { | |
94 pending_show_ = false; | |
95 ShowConstrainedWindow(); | |
96 } | |
97 } | |
98 | |
99 NSWindow* ConstrainedWindowMac2::GetParentWindow() const { | 73 NSWindow* ConstrainedWindowMac2::GetParentWindow() const { |
100 // Tab contents in a tabbed browser may not be inside a window. For this | 74 // Tab contents in a tabbed browser may not be inside a window. For this |
101 // reason use a browser window if possible. | 75 // reason use a browser window if possible. |
102 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 76 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
103 if (browser) | 77 if (browser) |
104 return browser->window()->GetNativeWindow(); | 78 return browser->window()->GetNativeWindow(); |
105 | 79 |
106 return web_contents_->GetView()->GetTopLevelNativeWindow(); | 80 return web_contents_->GetView()->GetTopLevelNativeWindow(); |
107 } | 81 } |
OLD | NEW |