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_sheet.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_view.h"
15 #include "ui/gfx/size.h"
16 #include "ui/web_dialogs/web_dialog_delegate.h"
17 #include "ui/web_dialogs/web_dialog_ui.h"
18 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
20 using content::WebContents;
21 using ui::WebDialogDelegate;
22 using ui::WebDialogWebContentsDelegate;
23 using web_modal::NativeWebContentsModalDialog;
27 class ConstrainedWebDialogDelegateMac
28 : public ConstrainedWebDialogDelegateBase {
30 ConstrainedWebDialogDelegateMac(
31 content::BrowserContext* browser_context,
32 WebDialogDelegate* delegate,
33 WebDialogWebContentsDelegate* tab_delegate)
34 : ConstrainedWebDialogDelegateBase(
35 browser_context, delegate, tab_delegate) {}
37 // WebDialogWebContentsDelegate interface.
38 virtual void CloseContents(WebContents* source) OVERRIDE {
39 window_->CloseWebContentsModalDialog();
42 void set_window(ConstrainedWindowMac* window) { window_ = window; }
43 ConstrainedWindowMac* window() const { return window_; }
46 // Weak, owned by ConstrainedWebDialogDelegateViewMac.
47 ConstrainedWindowMac* window_;
49 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac);
54 class ConstrainedWebDialogDelegateViewMac :
55 public ConstrainedWindowMacDelegate,
56 public ConstrainedWebDialogDelegate {
59 ConstrainedWebDialogDelegateViewMac(
60 content::BrowserContext* browser_context,
61 WebDialogDelegate* delegate,
62 WebDialogWebContentsDelegate* tab_delegate,
63 content::WebContents* web_contents);
64 virtual ~ConstrainedWebDialogDelegateViewMac() {}
66 // ConstrainedWebDialogDelegate interface
67 virtual const WebDialogDelegate*
68 GetWebDialogDelegate() const OVERRIDE {
69 return impl_->GetWebDialogDelegate();
71 virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE {
72 return impl_->GetWebDialogDelegate();
74 virtual void OnDialogCloseFromWebUI() OVERRIDE {
75 return impl_->OnDialogCloseFromWebUI();
77 virtual void ReleaseWebContentsOnDialogClose() OVERRIDE {
78 return impl_->ReleaseWebContentsOnDialogClose();
80 virtual NativeWebContentsModalDialog GetNativeDialog() OVERRIDE {
81 return constrained_window_->GetNativeDialog();
83 virtual WebContents* GetWebContents() OVERRIDE {
84 return impl_->GetWebContents();
87 // ConstrainedWindowMacDelegate interface
88 virtual void OnConstrainedWindowClosed(
89 ConstrainedWindowMac* window) OVERRIDE {
90 if (!impl_->closed_via_webui())
91 GetWebDialogDelegate()->OnDialogClosed("");
96 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_;
97 scoped_ptr<ConstrainedWindowMac> constrained_window_;
98 base::scoped_nsobject<NSWindow> window_;
100 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac);
103 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac(
104 content::BrowserContext* browser_context,
105 WebDialogDelegate* delegate,
106 WebDialogWebContentsDelegate* tab_delegate,
107 content::WebContents* web_contents)
108 : impl_(new ConstrainedWebDialogDelegateMac(browser_context,
111 // Create a window to hold web_contents in the constrained sheet:
113 delegate->GetDialogSize(&size);
114 NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
117 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]);
118 [GetWebContents()->GetView()->GetNativeView() setFrame:frame];
119 [[window_ contentView]
120 addSubview:GetWebContents()->GetView()->GetNativeView()];
122 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
123 [[CustomConstrainedWindowSheet alloc] initWithCustomWindow:window_]);
124 constrained_window_.reset(new ConstrainedWindowMac(
125 this, web_contents, sheet));
127 impl_->set_window(constrained_window_.get());
130 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog(
131 content::BrowserContext* browser_context,
132 WebDialogDelegate* delegate,
133 WebDialogWebContentsDelegate* tab_delegate,
134 content::WebContents* web_contents) {
135 // Deleted when the dialog closes.
136 ConstrainedWebDialogDelegateViewMac* constrained_delegate =
137 new ConstrainedWebDialogDelegateViewMac(
138 browser_context, delegate, tab_delegate, web_contents);
139 return constrained_delegate;