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/webui/constrained_web_dialog_delegate_base.h" | 5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" | 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" |
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | 11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" |
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialo
g_sheet.h" | 12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialo
g_sheet.h" |
13 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" | |
14 #include "content/public/browser/render_view_host.h" | |
15 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
16 #include "content/public/browser/web_contents_observer.h" | |
17 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
18 #include "ui/web_dialogs/web_dialog_delegate.h" | 15 #include "ui/web_dialogs/web_dialog_delegate.h" |
19 #include "ui/web_dialogs/web_dialog_ui.h" | 16 #include "ui/web_dialogs/web_dialog_ui.h" |
20 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | 17 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" |
21 | 18 |
22 using content::WebContents; | 19 using content::WebContents; |
23 using ui::WebDialogDelegate; | 20 using ui::WebDialogDelegate; |
24 using ui::WebDialogWebContentsDelegate; | 21 using ui::WebDialogWebContentsDelegate; |
25 | 22 |
26 namespace { | 23 namespace { |
27 | 24 |
28 class ConstrainedWebDialogDelegateMac; | |
29 | |
30 // This class is to trigger a resize to the dialog window when | |
31 // ResizeDueToAutoResize() is invoked. | |
32 class WebDialogWebContentsDelegateMac | |
33 : public ui::WebDialogWebContentsDelegate { | |
34 public: | |
35 WebDialogWebContentsDelegateMac(content::BrowserContext* browser_context, | |
36 content::WebContentsObserver* observer, | |
37 ConstrainedWebDialogDelegateBase* delegate) | |
38 : ui::WebDialogWebContentsDelegate(browser_context, | |
39 new ChromeWebContentsHandler()), | |
40 observer_(observer), | |
41 delegate_(delegate) { | |
42 } | |
43 ~WebDialogWebContentsDelegateMac() override {} | |
44 | |
45 void ResizeDueToAutoResize(content::WebContents* source, | |
46 const gfx::Size& preferred_size) override { | |
47 if (!observer_->web_contents()) | |
48 return; | |
49 delegate_->ResizeToGivenSize(preferred_size); | |
50 } | |
51 | |
52 private: | |
53 // These members must outlive the instance. | |
54 content::WebContentsObserver* const observer_; | |
55 ConstrainedWebDialogDelegateBase* delegate_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateMac); | |
58 }; | |
59 | |
60 class ConstrainedWebDialogDelegateMac | 25 class ConstrainedWebDialogDelegateMac |
61 : public ConstrainedWebDialogDelegateBase { | 26 : public ConstrainedWebDialogDelegateBase { |
62 public: | 27 public: |
63 ConstrainedWebDialogDelegateMac( | 28 ConstrainedWebDialogDelegateMac( |
64 content::BrowserContext* browser_context, | 29 content::BrowserContext* browser_context, |
65 WebDialogDelegate* delegate, | 30 WebDialogDelegate* delegate) |
66 content::WebContentsObserver* observer) | 31 : ConstrainedWebDialogDelegateBase(browser_context, delegate, NULL) {} |
67 : ConstrainedWebDialogDelegateBase(browser_context, delegate, | |
68 new WebDialogWebContentsDelegateMac(browser_context, observer, | |
69 this)) {} | |
70 | 32 |
71 // WebDialogWebContentsDelegate interface. | 33 // WebDialogWebContentsDelegate interface. |
72 void CloseContents(WebContents* source) override { | 34 void CloseContents(WebContents* source) override { |
73 window_->CloseWebContentsModalDialog(); | 35 window_->CloseWebContentsModalDialog(); |
74 } | 36 } |
75 | 37 |
76 // ConstrainedWebDialogDelegateBase: | |
77 void ResizeToGivenSize(const gfx::Size size) override { | |
78 NSSize updated_preferred_size = NSMakeSize(size.width(), | |
79 size.height()); | |
80 [window_->sheet() resizeWithNewSize:updated_preferred_size]; | |
81 } | |
82 | |
83 void set_window(ConstrainedWindowMac* window) { window_ = window; } | 38 void set_window(ConstrainedWindowMac* window) { window_ = window; } |
84 ConstrainedWindowMac* window() const { return window_; } | 39 ConstrainedWindowMac* window() const { return window_; } |
85 | 40 |
86 private: | 41 private: |
87 // Weak, owned by ConstrainedWebDialogDelegateViewMac. | 42 // Weak, owned by ConstrainedWebDialogDelegateViewMac. |
88 ConstrainedWindowMac* window_; | 43 ConstrainedWindowMac* window_; |
89 | 44 |
90 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac); | 45 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac); |
91 }; | 46 }; |
92 | 47 |
93 } // namespace | 48 } // namespace |
94 | 49 |
95 class ConstrainedWebDialogDelegateViewMac : | 50 class ConstrainedWebDialogDelegateViewMac : |
96 public ConstrainedWindowMacDelegate, | 51 public ConstrainedWindowMacDelegate, |
97 public ConstrainedWebDialogDelegate, | 52 public ConstrainedWebDialogDelegate { |
98 public content::WebContentsObserver { | |
99 | 53 |
100 public: | 54 public: |
101 ConstrainedWebDialogDelegateViewMac( | 55 ConstrainedWebDialogDelegateViewMac( |
102 content::BrowserContext* browser_context, | 56 content::BrowserContext* browser_context, |
103 WebDialogDelegate* delegate, | 57 WebDialogDelegate* delegate, |
104 content::WebContents* web_contents, | 58 content::WebContents* web_contents); |
105 const gfx::Size& min_size, | |
106 const gfx::Size& max_size); | |
107 ~ConstrainedWebDialogDelegateViewMac() override {} | 59 ~ConstrainedWebDialogDelegateViewMac() override {} |
108 | 60 |
109 // ConstrainedWebDialogDelegate interface | 61 // ConstrainedWebDialogDelegate interface |
110 const WebDialogDelegate* GetWebDialogDelegate() const override { | 62 const WebDialogDelegate* GetWebDialogDelegate() const override { |
111 return impl_->GetWebDialogDelegate(); | 63 return impl_->GetWebDialogDelegate(); |
112 } | 64 } |
113 WebDialogDelegate* GetWebDialogDelegate() override { | 65 WebDialogDelegate* GetWebDialogDelegate() override { |
114 return impl_->GetWebDialogDelegate(); | 66 return impl_->GetWebDialogDelegate(); |
115 } | 67 } |
116 void OnDialogCloseFromWebUI() override { | 68 void OnDialogCloseFromWebUI() override { |
117 return impl_->OnDialogCloseFromWebUI(); | 69 return impl_->OnDialogCloseFromWebUI(); |
118 } | 70 } |
119 void ReleaseWebContentsOnDialogClose() override { | 71 void ReleaseWebContentsOnDialogClose() override { |
120 return impl_->ReleaseWebContentsOnDialogClose(); | 72 return impl_->ReleaseWebContentsOnDialogClose(); |
121 } | 73 } |
122 gfx::NativeWindow GetNativeDialog() override { return window_; } | 74 gfx::NativeWindow GetNativeDialog() override { return window_; } |
123 WebContents* GetWebContents() override { return impl_->GetWebContents(); } | 75 WebContents* GetWebContents() override { return impl_->GetWebContents(); } |
124 gfx::Size GetMinimumSize() const override { | 76 gfx::Size GetMinimumSize() const override { |
125 return min_size_; | 77 NOTIMPLEMENTED(); |
| 78 return gfx::Size(); |
126 } | 79 } |
127 gfx::Size GetMaximumSize() const override { | 80 gfx::Size GetMaximumSize() const override { |
128 return max_size_; | 81 NOTIMPLEMENTED(); |
| 82 return gfx::Size(); |
129 } | 83 } |
130 gfx::Size GetPreferredSize() const override { | 84 gfx::Size GetPreferredSize() const override { |
131 gfx::Size size; | 85 NOTIMPLEMENTED(); |
132 if (!impl_->closed_via_webui()) { | 86 return gfx::Size(); |
133 NSRect frame = [window_ frame]; | |
134 size = gfx::Size(frame.size.width, frame.size.height); | |
135 } | |
136 return size; | |
137 } | |
138 | |
139 // content::WebContentsObserver: | |
140 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | |
141 if (IsDialogAutoResizable()) | |
142 EnableAutoResize(); | |
143 } | |
144 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
145 content::RenderViewHost* new_host) override { | |
146 if (IsDialogAutoResizable()) | |
147 EnableAutoResize(); | |
148 } | |
149 void DocumentOnLoadCompletedInMainFrame() override { | |
150 if (!IsDialogAutoResizable()) | |
151 return; | |
152 | |
153 EnableAutoResize(); | |
154 if (GetWebContents()) | |
155 constrained_window_->ShowWebContentsModalDialog(); | |
156 } | 87 } |
157 | 88 |
158 // ConstrainedWindowMacDelegate interface | 89 // ConstrainedWindowMacDelegate interface |
159 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { | 90 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { |
160 if (!impl_->closed_via_webui()) | 91 if (!impl_->closed_via_webui()) |
161 GetWebDialogDelegate()->OnDialogClosed(""); | 92 GetWebDialogDelegate()->OnDialogClosed(""); |
162 delete this; | 93 delete this; |
163 } | 94 } |
164 | 95 |
165 private: | 96 private: |
166 void EnableAutoResize() { | |
167 if (!GetWebContents()) | |
168 return; | |
169 | |
170 content::RenderViewHost* render_view_host = | |
171 GetWebContents()->GetRenderViewHost(); | |
172 render_view_host->EnableAutoResize(min_size_, max_size_); | |
173 } | |
174 | |
175 // Whether or not the dialog is autoresizable is determined based on whether | |
176 // |max_size_| was specified. | |
177 bool IsDialogAutoResizable() { | |
178 return !max_size_.IsEmpty(); | |
179 } | |
180 | |
181 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_; | 97 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_; |
182 scoped_ptr<ConstrainedWindowMac> constrained_window_; | 98 scoped_ptr<ConstrainedWindowMac> constrained_window_; |
183 base::scoped_nsobject<NSWindow> window_; | 99 base::scoped_nsobject<NSWindow> window_; |
184 | 100 |
185 // Minimum and maximum sizes to determine dialog bounds for auto-resizing. | |
186 const gfx::Size min_size_; | |
187 const gfx::Size max_size_; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac); | 101 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac); |
190 }; | 102 }; |
191 | 103 |
192 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac( | 104 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac( |
193 content::BrowserContext* browser_context, | 105 content::BrowserContext* browser_context, |
194 WebDialogDelegate* delegate, | 106 WebDialogDelegate* delegate, |
195 content::WebContents* web_contents, | 107 content::WebContents* web_contents) |
196 const gfx::Size& min_size, | 108 : impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate)) { |
197 const gfx::Size& max_size) | |
198 : content::WebContentsObserver(web_contents), | |
199 impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate, | |
200 this)), | |
201 min_size_(min_size), | |
202 max_size_(max_size) { | |
203 if (IsDialogAutoResizable()) | |
204 Observe(GetWebContents()); | |
205 | |
206 // Create a window to hold web_contents in the constrained sheet: | 109 // Create a window to hold web_contents in the constrained sheet: |
207 gfx::Size size; | 110 gfx::Size size; |
208 delegate->GetDialogSize(&size); | 111 delegate->GetDialogSize(&size); |
209 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); | 112 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); |
210 | 113 |
211 window_.reset([[ConstrainedWindowCustomWindow alloc] | 114 window_.reset( |
212 initWithContentRect:frame]); | 115 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]); |
213 [GetWebContents()->GetNativeView() setFrame:frame]; | 116 [GetWebContents()->GetNativeView() setFrame:frame]; |
214 [GetWebContents()->GetNativeView() setAutoresizingMask: | 117 [GetWebContents()->GetNativeView() setAutoresizingMask: |
215 NSViewWidthSizable|NSViewHeightSizable]; | 118 NSViewWidthSizable|NSViewHeightSizable]; |
216 [[window_ contentView] addSubview:GetWebContents()->GetNativeView()]; | 119 [[window_ contentView] addSubview:GetWebContents()->GetNativeView()]; |
217 | 120 |
218 base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet( | 121 base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet( |
219 [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_ | 122 [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_ |
220 webDialogDelegate:delegate]); | 123 webDialogDelegate:delegate]); |
221 | 124 constrained_window_.reset(new ConstrainedWindowMac( |
222 if (IsDialogAutoResizable()) { | 125 this, web_contents, sheet)); |
223 constrained_window_.reset(CreateWebModalDialogMac( | |
224 this, web_contents, sheet)); | |
225 } else { | |
226 constrained_window_.reset(CreateAndShowWebModalDialogMac( | |
227 this, web_contents, sheet)); | |
228 } | |
229 | 126 |
230 impl_->set_window(constrained_window_.get()); | 127 impl_->set_window(constrained_window_.get()); |
231 } | 128 } |
232 | 129 |
233 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( | 130 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
234 content::BrowserContext* browser_context, | 131 content::BrowserContext* browser_context, |
235 WebDialogDelegate* delegate, | 132 WebDialogDelegate* delegate, |
236 content::WebContents* web_contents) { | 133 content::WebContents* web_contents) { |
237 // Deleted when the dialog closes. | 134 // Deleted when the dialog closes. |
238 ConstrainedWebDialogDelegateViewMac* constrained_delegate = | 135 ConstrainedWebDialogDelegateViewMac* constrained_delegate = |
239 new ConstrainedWebDialogDelegateViewMac( | 136 new ConstrainedWebDialogDelegateViewMac( |
240 browser_context, delegate, web_contents, | 137 browser_context, delegate, web_contents); |
241 gfx::Size(), gfx::Size()); | |
242 return constrained_delegate; | 138 return constrained_delegate; |
243 } | 139 } |
244 | |
245 ConstrainedWebDialogDelegate* ShowConstrainedWebDialogWithAutoResize( | |
246 content::BrowserContext* browser_context, | |
247 WebDialogDelegate* delegate, | |
248 content::WebContents* web_contents, | |
249 const gfx::Size& min_size, | |
250 const gfx::Size& max_size) { | |
251 DCHECK(!min_size.IsEmpty()); | |
252 DCHECK(!max_size.IsEmpty()); | |
253 // Deleted when the dialog closes. | |
254 ConstrainedWebDialogDelegateViewMac* constrained_delegate = | |
255 new ConstrainedWebDialogDelegateViewMac( | |
256 browser_context, delegate, web_contents, | |
257 min_size, max_size); | |
258 return constrained_delegate; | |
259 } | |
OLD | NEW |