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" |
13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
14 #include "content/public/browser/web_contents_view.h" | 16 #include "content/public/browser/web_contents_view.h" |
15 | 17 |
16 ConstrainedWindowMac2::ConstrainedWindowMac2( | 18 ConstrainedWindowMac2::ConstrainedWindowMac2( |
17 ConstrainedWindowMacDelegate2* delegate, | 19 ConstrainedWindowMacDelegate2* delegate, |
18 content::WebContents* web_contents, | 20 content::WebContents* web_contents, |
19 NSWindow* window) | 21 NSWindow* window) |
20 : delegate_(delegate), | 22 : delegate_(delegate), |
21 web_contents_(web_contents), | 23 web_contents_(web_contents), |
22 window_([window retain]) { | 24 window_([window retain]), |
| 25 pending_show_(false) { |
23 DCHECK(web_contents); | 26 DCHECK(web_contents); |
24 DCHECK(window_.get()); | 27 DCHECK(window_.get()); |
25 ConstrainedWindowTabHelper* constrained_window_tab_helper = | 28 ConstrainedWindowTabHelper* constrained_window_tab_helper = |
26 ConstrainedWindowTabHelper::FromWebContents(web_contents); | 29 ConstrainedWindowTabHelper::FromWebContents(web_contents); |
27 constrained_window_tab_helper->AddConstrainedDialog(this); | 30 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)); |
28 } | 35 } |
29 | 36 |
30 ConstrainedWindowMac2::~ConstrainedWindowMac2() { | 37 ConstrainedWindowMac2::~ConstrainedWindowMac2() { |
31 } | 38 } |
32 | 39 |
33 void ConstrainedWindowMac2::ShowConstrainedWindow() { | 40 void ConstrainedWindowMac2::ShowConstrainedWindow() { |
34 NSWindow* parent_window = GetParentWindow(); | 41 NSWindow* parent_window = GetParentWindow(); |
35 if (!parent_window) | 42 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_); |
| 43 if (!parent_window || !parent_view) { |
| 44 pending_show_ = true; |
36 return; | 45 return; |
37 | 46 } |
38 NSView* parent_view = GetSheetParentViewForWebContents(web_contents_); | |
39 DCHECK(parent_view); | |
40 | 47 |
41 ConstrainedWindowSheetController* controller = | 48 ConstrainedWindowSheetController* controller = |
42 [ConstrainedWindowSheetController | 49 [ConstrainedWindowSheetController |
43 controllerForParentWindow:parent_window]; | 50 controllerForParentWindow:parent_window]; |
44 [controller showSheet:window_ forParentView:parent_view]; | 51 [controller showSheet:window_ forParentView:parent_view]; |
45 } | 52 } |
46 | 53 |
47 void ConstrainedWindowMac2::CloseConstrainedWindow() { | 54 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 |
48 [[ConstrainedWindowSheetController controllerForSheet:window_] | 59 [[ConstrainedWindowSheetController controllerForSheet:window_] |
49 closeSheet:window_]; | 60 closeSheet:window_]; |
50 ConstrainedWindowTabHelper* constrained_window_tab_helper = | 61 ConstrainedWindowTabHelper* constrained_window_tab_helper = |
51 ConstrainedWindowTabHelper::FromWebContents(web_contents_); | 62 ConstrainedWindowTabHelper::FromWebContents(web_contents_); |
52 constrained_window_tab_helper->WillClose(this); | 63 constrained_window_tab_helper->WillClose(this); |
53 if (delegate_) | 64 if (delegate_) |
54 delegate_->OnConstrainedWindowClosed(this); | 65 delegate_->OnConstrainedWindowClosed(this); |
55 } | 66 } |
56 | 67 |
57 void ConstrainedWindowMac2::PulseConstrainedWindow() { | 68 void ConstrainedWindowMac2::PulseConstrainedWindow() { |
58 [[ConstrainedWindowSheetController controllerForSheet:window_] | 69 [[ConstrainedWindowSheetController controllerForSheet:window_] |
59 pulseSheet:window_]; | 70 pulseSheet:window_]; |
60 } | 71 } |
61 | 72 |
62 gfx::NativeWindow ConstrainedWindowMac2::GetNativeWindow() { | 73 gfx::NativeWindow ConstrainedWindowMac2::GetNativeWindow() { |
63 return window_; | 74 return window_; |
64 } | 75 } |
65 | 76 |
66 bool ConstrainedWindowMac2::CanShowConstrainedWindow() { | 77 bool ConstrainedWindowMac2::CanShowConstrainedWindow() { |
67 Browser* browser = browser::FindBrowserWithWebContents(web_contents_); | 78 Browser* browser = browser::FindBrowserWithWebContents(web_contents_); |
68 if (!browser) | 79 if (!browser) |
69 return true; | 80 return true; |
70 return !browser->window()->IsInstantTabShowing(); | 81 return !browser->window()->IsInstantTabShowing(); |
71 } | 82 } |
72 | 83 |
| 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 |
73 NSWindow* ConstrainedWindowMac2::GetParentWindow() const { | 99 NSWindow* ConstrainedWindowMac2::GetParentWindow() const { |
74 // Tab contents in a tabbed browser may not be inside a window. For this | 100 // Tab contents in a tabbed browser may not be inside a window. For this |
75 // reason use a browser window if possible. | 101 // reason use a browser window if possible. |
76 Browser* browser = browser::FindBrowserWithWebContents(web_contents_); | 102 Browser* browser = browser::FindBrowserWithWebContents(web_contents_); |
77 if (browser) | 103 if (browser) |
78 return browser->window()->GetNativeWindow(); | 104 return browser->window()->GetNativeWindow(); |
79 | 105 |
80 return web_contents_->GetView()->GetTopLevelNativeWindow(); | 106 return web_contents_->GetView()->GetTopLevelNativeWindow(); |
81 } | 107 } |
OLD | NEW |