OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/constrained_html_ui_delegate_impl.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/memory/scoped_nsobject.h" | |
10 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" | |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
12 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
13 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "ui/gfx/size.h" | |
16 | |
17 using content::WebContents; | |
18 | |
19 class ConstrainedHtmlDelegateMac : | |
20 public ConstrainedWindowMacDelegateCustomSheet, | |
21 public ConstrainedHtmlUIDelegate { | |
22 | |
23 public: | |
24 ConstrainedHtmlDelegateMac(Profile* profile, | |
25 HtmlDialogUIDelegate* delegate, | |
26 HtmlDialogTabContentsDelegate* tab_delegate); | |
27 virtual ~ConstrainedHtmlDelegateMac() {} | |
28 | |
29 void set_window(ConstrainedWindow* window) { | |
30 return impl_->set_window(window); | |
31 } | |
32 | |
33 // ConstrainedHtmlUIDelegate interface | |
34 virtual const HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() const OVERRIDE { | |
35 return impl_->GetHtmlDialogUIDelegate(); | |
36 } | |
37 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE { | |
38 return impl_->GetHtmlDialogUIDelegate(); | |
39 } | |
40 virtual void OnDialogCloseFromWebUI() OVERRIDE { | |
41 return impl_->OnDialogCloseFromWebUI(); | |
42 } | |
43 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE { | |
44 return impl_->ReleaseTabContentsOnDialogClose(); | |
45 } | |
46 virtual ConstrainedWindow* window() OVERRIDE { | |
47 return impl_->window(); | |
48 } | |
49 virtual TabContentsWrapper* tab() OVERRIDE { | |
50 return impl_->tab(); | |
51 } | |
52 | |
53 // ConstrainedWindowMacDelegateCustomSheet interface | |
54 virtual void DeleteDelegate() OVERRIDE { | |
55 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to | |
56 // your delegate in this method." | |
57 if (is_sheet_open()) | |
58 [NSApp endSheet:sheet()]; | |
59 if (!impl_->closed_via_webui()) | |
60 GetHtmlDialogUIDelegate()->OnDialogClosed(""); | |
61 delete this; | |
62 } | |
63 | |
64 private: | |
65 scoped_ptr<ConstrainedHtmlUIDelegateImpl> impl_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac); | |
68 }; | |
69 | |
70 // The delegate used to forward events from the sheet to the constrained | |
71 // window delegate. This bridge needs to be passed into the customsheet | |
72 // to allow the HtmlDialog to know when the sheet closes. | |
73 @interface ConstrainedHtmlDialogSheetCocoa : NSObject { | |
74 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak | |
75 } | |
76 - (id)initWithConstrainedHtmlDelegateMac: | |
77 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac; | |
78 - (void)sheetDidEnd:(NSWindow*)sheet | |
79 returnCode:(int)returnCode | |
80 contextInfo:(void*)contextInfo; | |
81 @end | |
82 | |
83 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac( | |
84 Profile* profile, | |
85 HtmlDialogUIDelegate* delegate, | |
86 HtmlDialogTabContentsDelegate* tab_delegate) | |
87 : impl_(new ConstrainedHtmlUIDelegateImpl(profile, delegate, tab_delegate)) { | |
88 // Create NSWindow to hold web_contents in the constrained sheet: | |
89 gfx::Size size; | |
90 delegate->GetDialogSize(&size); | |
91 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); | |
92 | |
93 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when | |
94 // the sheet is initialized. | |
95 scoped_nsobject<NSWindow> window; | |
96 window.reset( | |
97 [[NSWindow alloc] initWithContentRect:frame | |
98 styleMask:NSTitledWindowMask | |
99 backing:NSBackingStoreBuffered | |
100 defer:YES]); | |
101 | |
102 [window.get() setContentView:tab()->web_contents()->GetNativeView()]; | |
103 | |
104 // Set the custom sheet to point to the new window. | |
105 ConstrainedWindowMacDelegateCustomSheet::init( | |
106 window.get(), | |
107 [[[ConstrainedHtmlDialogSheetCocoa alloc] | |
108 initWithConstrainedHtmlDelegateMac:this] autorelease], | |
109 @selector(sheetDidEnd:returnCode:contextInfo:)); | |
110 } | |
111 | |
112 // static | |
113 ConstrainedHtmlUIDelegate* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | |
114 Profile* profile, | |
115 HtmlDialogUIDelegate* delegate, | |
116 HtmlDialogTabContentsDelegate* tab_delegate, | |
117 TabContentsWrapper* wrapper) { | |
118 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs. | |
119 ConstrainedHtmlDelegateMac* constrained_delegate = | |
120 new ConstrainedHtmlDelegateMac(profile, delegate, tab_delegate); | |
121 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs. | |
122 ConstrainedWindow* constrained_window = | |
123 new ConstrainedWindowMac(wrapper, constrained_delegate); | |
124 constrained_delegate->set_window(constrained_window); | |
125 return constrained_delegate; | |
126 } | |
127 | |
128 @implementation ConstrainedHtmlDialogSheetCocoa | |
129 | |
130 - (id)initWithConstrainedHtmlDelegateMac: | |
131 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac { | |
132 if ((self = [super init])) | |
133 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac; | |
134 return self; | |
135 } | |
136 | |
137 - (void)sheetDidEnd:(NSWindow*)sheet | |
138 returnCode:(int)returnCode | |
139 contextInfo:(void *)contextInfo { | |
140 [sheet orderOut:self]; | |
141 } | |
142 | |
143 @end | |
OLD | NEW |