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.
5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/scoped_nsobject.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialog_sheet.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ui/gfx/geometry/size.h"
15 #include "ui/web_dialogs/web_dialog_delegate.h"
16 #include "ui/web_dialogs/web_dialog_ui.h"
17 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
19 using content::WebContents;
20 using ui::WebDialogDelegate;
21 using ui::WebDialogWebContentsDelegate;
25 class ConstrainedWebDialogDelegateMac
26 : public ConstrainedWebDialogDelegateBase {
28 ConstrainedWebDialogDelegateMac(
29 content::BrowserContext* browser_context,
30 WebDialogDelegate* delegate)
31 : ConstrainedWebDialogDelegateBase(browser_context, delegate, NULL) {}
33 // WebDialogWebContentsDelegate interface.
34 void CloseContents(WebContents* source) override {
35 window_->CloseWebContentsModalDialog();
38 void set_window(ConstrainedWindowMac* window) { window_ = window; }
39 ConstrainedWindowMac* window() const { return window_; }
42 // Weak, owned by ConstrainedWebDialogDelegateViewMac.
43 ConstrainedWindowMac* window_;
45 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac);
50 class ConstrainedWebDialogDelegateViewMac :
51 public ConstrainedWindowMacDelegate,
52 public ConstrainedWebDialogDelegate {
55 ConstrainedWebDialogDelegateViewMac(
56 content::BrowserContext* browser_context,
57 WebDialogDelegate* delegate,
58 content::WebContents* web_contents);
59 ~ConstrainedWebDialogDelegateViewMac() override {}
61 // ConstrainedWebDialogDelegate interface
62 const WebDialogDelegate* GetWebDialogDelegate() const override {
63 return impl_->GetWebDialogDelegate();
65 WebDialogDelegate* GetWebDialogDelegate() override {
66 return impl_->GetWebDialogDelegate();
68 void OnDialogCloseFromWebUI() override {
69 return impl_->OnDialogCloseFromWebUI();
71 void ReleaseWebContentsOnDialogClose() override {
72 return impl_->ReleaseWebContentsOnDialogClose();
74 gfx::NativeWindow GetNativeDialog() override { return window_; }
75 WebContents* GetWebContents() override { return impl_->GetWebContents(); }
76 gfx::Size GetMinimumSize() const override {
80 gfx::Size GetMaximumSize() const override {
84 gfx::Size GetPreferredSize() const override {
89 // ConstrainedWindowMacDelegate interface
90 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
91 if (!impl_->closed_via_webui())
92 GetWebDialogDelegate()->OnDialogClosed("");
97 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_;
98 scoped_ptr<ConstrainedWindowMac> constrained_window_;
99 base::scoped_nsobject<NSWindow> window_;
101 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac);
104 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac(
105 content::BrowserContext* browser_context,
106 WebDialogDelegate* delegate,
107 content::WebContents* web_contents)
108 : impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate)) {
109 // Create a window to hold web_contents in the constrained sheet:
111 delegate->GetDialogSize(&size);
112 NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
115 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]);
116 [GetWebContents()->GetNativeView() setFrame:frame];
117 [GetWebContents()->GetNativeView() setAutoresizingMask:
118 NSViewWidthSizable|NSViewHeightSizable];
119 [[window_ contentView] addSubview:GetWebContents()->GetNativeView()];
121 base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet(
122 [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_
123 webDialogDelegate:delegate]);
124 constrained_window_.reset(new ConstrainedWindowMac(
125 this, web_contents, sheet));
127 impl_->set_window(constrained_window_.get());
130 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog(
131 content::BrowserContext* browser_context,
132 WebDialogDelegate* delegate,
133 content::WebContents* web_contents) {
134 // Deleted when the dialog closes.
135 ConstrainedWebDialogDelegateViewMac* constrained_delegate =
136 new ConstrainedWebDialogDelegateViewMac(
137 browser_context, delegate, web_contents);
138 return constrained_delegate;