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