OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_html_ui.h" | 5 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/memory/scoped_nsobject.h" | 9 #include "base/memory/scoped_nsobject.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" | 11 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
13 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 13 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
14 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" | 14 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" |
15 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
16 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
17 | 17 |
18 class ConstrainedHtmlDelegateMac : | 18 class ConstrainedHtmlDelegateMac : |
19 public ConstrainedWindowMacDelegateCustomSheet, | 19 public ConstrainedWindowMacDelegateCustomSheet, |
20 public HtmlDialogTabContentsDelegate, | 20 public HtmlDialogTabContentsDelegate, |
21 public ConstrainedHtmlUIDelegate { | 21 public ConstrainedHtmlUIDelegate { |
22 | 22 |
23 public: | 23 public: |
24 ConstrainedHtmlDelegateMac(Profile* profile, | 24 ConstrainedHtmlDelegateMac(Profile* profile, |
25 HtmlDialogUIDelegate* delegate); | 25 HtmlDialogUIDelegate* delegate); |
26 ~ConstrainedHtmlDelegateMac() {} | 26 ~ConstrainedHtmlDelegateMac() { |
| 27 if (release_tab_on_close_) |
| 28 ignore_result(tab_.release()); |
| 29 } |
27 | 30 |
28 // ConstrainedWindowMacDelegateCustomSheet ----------------------------------- | 31 // ConstrainedWindowMacDelegateCustomSheet ----------------------------------- |
29 virtual void DeleteDelegate() OVERRIDE { | 32 virtual void DeleteDelegate() OVERRIDE { |
30 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to | 33 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to |
31 // your delegate in this method." | 34 // your delegate in this method." |
32 if (is_sheet_open()) | 35 if (is_sheet_open()) |
33 [NSApp endSheet:sheet()]; | 36 [NSApp endSheet:sheet()]; |
34 if (!closed_via_webui_) | 37 if (!closed_via_webui_) |
35 html_delegate_->OnDialogClosed(""); | 38 html_delegate_->OnDialogClosed(""); |
36 delete this; | 39 delete this; |
37 } | 40 } |
38 | 41 |
39 // ConstrainedHtmlDelegate --------------------------------------------------- | 42 // ConstrainedHtmlDelegate --------------------------------------------------- |
40 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE; | 43 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE; |
41 virtual void OnDialogCloseFromWebUI() OVERRIDE; | 44 virtual void OnDialogCloseFromWebUI() OVERRIDE; |
| 45 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE; |
| 46 virtual ConstrainedWindow* window() OVERRIDE { return constrained_window_; } |
| 47 virtual TabContentsWrapper* tab() OVERRIDE { return tab_.get(); } |
| 48 |
42 | 49 |
43 // HtmlDialogTabContentsDelegate --------------------------------------------- | 50 // HtmlDialogTabContentsDelegate --------------------------------------------- |
44 virtual void HandleKeyboardEvent( | 51 virtual void HandleKeyboardEvent( |
45 const NativeWebKeyboardEvent& event) OVERRIDE {} | 52 const NativeWebKeyboardEvent& event) OVERRIDE {} |
46 | 53 |
47 void set_window(ConstrainedWindow* window) { | 54 void set_window(ConstrainedWindow* window) { |
48 constrained_window_ = window; | 55 constrained_window_ = window; |
49 } | 56 } |
50 | 57 |
51 private: | 58 private: |
52 TabContents tab_contents_; // Holds the HTML to be displayed in the sheet. | 59 // Holds the HTML to be displayed in the sheet. |
| 60 scoped_ptr<TabContentsWrapper> tab_; |
53 HtmlDialogUIDelegate* html_delegate_; // weak. | 61 HtmlDialogUIDelegate* html_delegate_; // weak. |
54 | 62 |
55 // The constrained window that owns |this|. Saved here because it needs to be | 63 // The constrained window that owns |this|. Saved here because it needs to be |
56 // closed in response to the WebUI OnDialogClose callback. | 64 // closed in response to the WebUI OnDialogClose callback. |
57 ConstrainedWindow* constrained_window_; | 65 ConstrainedWindow* constrained_window_; |
58 | 66 |
59 // Was the dialog closed from WebUI (in which case |html_delegate_|'s | 67 // Was the dialog closed from WebUI (in which case |html_delegate_|'s |
60 // OnDialogClosed() method has already been called)? | 68 // OnDialogClosed() method has already been called)? |
61 bool closed_via_webui_; | 69 bool closed_via_webui_; |
62 | 70 |
| 71 // If true, release |tab_| on close instead of destroying it. |
| 72 bool release_tab_on_close_; |
| 73 |
63 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac); | 74 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac); |
64 }; | 75 }; |
65 | 76 |
66 // The delegate used to forward events from the sheet to the constrained | 77 // The delegate used to forward events from the sheet to the constrained |
67 // window delegate. This bridge needs to be passed into the customsheet | 78 // window delegate. This bridge needs to be passed into the customsheet |
68 // to allow the HtmlDialog to know when the sheet closes. | 79 // to allow the HtmlDialog to know when the sheet closes. |
69 @interface ConstrainedHtmlDialogSheetCocoa : NSObject { | 80 @interface ConstrainedHtmlDialogSheetCocoa : NSObject { |
70 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak | 81 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak |
71 } | 82 } |
72 - (id)initWithConstrainedHtmlDelegateMac: | 83 - (id)initWithConstrainedHtmlDelegateMac: |
73 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac; | 84 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac; |
74 - (void)sheetDidEnd:(NSWindow*)sheet | 85 - (void)sheetDidEnd:(NSWindow*)sheet |
75 returnCode:(int)returnCode | 86 returnCode:(int)returnCode |
76 contextInfo:(void*)contextInfo; | 87 contextInfo:(void*)contextInfo; |
77 @end | 88 @end |
78 | 89 |
79 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac( | 90 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac( |
80 Profile* profile, | 91 Profile* profile, |
81 HtmlDialogUIDelegate* delegate) | 92 HtmlDialogUIDelegate* delegate) |
82 : HtmlDialogTabContentsDelegate(profile), | 93 : HtmlDialogTabContentsDelegate(profile), |
83 tab_contents_(profile, NULL, MSG_ROUTING_NONE, NULL, NULL), | |
84 html_delegate_(delegate), | 94 html_delegate_(delegate), |
85 constrained_window_(NULL), | 95 constrained_window_(NULL), |
86 closed_via_webui_(false) { | 96 closed_via_webui_(false), |
87 tab_contents_.set_delegate(this); | 97 release_tab_on_close_(false) { |
| 98 TabContents* tab_contents = |
| 99 new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL); |
| 100 tab_.reset(new TabContentsWrapper(tab_contents)); |
| 101 tab_contents->set_delegate(this); |
88 | 102 |
89 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI | 103 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI |
90 // can get a reference to |this|. | 104 // can get a reference to |this|. |
91 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty( | 105 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty( |
92 tab_contents_.property_bag(), this); | 106 tab_contents->property_bag(), this); |
93 | 107 |
94 tab_contents_.controller().LoadURL(delegate->GetDialogContentURL(), | 108 tab_contents->controller().LoadURL(delegate->GetDialogContentURL(), |
95 GURL(), | 109 GURL(), |
96 content::PAGE_TRANSITION_START_PAGE, | 110 content::PAGE_TRANSITION_START_PAGE, |
97 std::string()); | 111 std::string()); |
98 | 112 |
99 // Create NSWindow to hold tab_contents in the constrained sheet: | 113 // Create NSWindow to hold tab_contents in the constrained sheet: |
100 gfx::Size size; | 114 gfx::Size size; |
101 delegate->GetDialogSize(&size); | 115 delegate->GetDialogSize(&size); |
102 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); | 116 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); |
103 | 117 |
104 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when | 118 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when |
105 // the sheet is initialized. | 119 // the sheet is initialized. |
106 scoped_nsobject<NSWindow> window; | 120 scoped_nsobject<NSWindow> window; |
107 window.reset( | 121 window.reset( |
108 [[NSWindow alloc] initWithContentRect:frame | 122 [[NSWindow alloc] initWithContentRect:frame |
109 styleMask:NSTitledWindowMask | 123 styleMask:NSTitledWindowMask |
110 backing:NSBackingStoreBuffered | 124 backing:NSBackingStoreBuffered |
111 defer:YES]); | 125 defer:YES]); |
112 | 126 |
113 [window.get() setContentView:tab_contents_.GetNativeView()]; | 127 [window.get() setContentView:tab_contents->GetNativeView()]; |
114 | 128 |
115 // Set the custom sheet to point to the new window. | 129 // Set the custom sheet to point to the new window. |
116 ConstrainedWindowMacDelegateCustomSheet::init( | 130 ConstrainedWindowMacDelegateCustomSheet::init( |
117 window.get(), | 131 window.get(), |
118 [[[ConstrainedHtmlDialogSheetCocoa alloc] | 132 [[[ConstrainedHtmlDialogSheetCocoa alloc] |
119 initWithConstrainedHtmlDelegateMac:this] autorelease], | 133 initWithConstrainedHtmlDelegateMac:this] autorelease], |
120 @selector(sheetDidEnd:returnCode:contextInfo:)); | 134 @selector(sheetDidEnd:returnCode:contextInfo:)); |
121 } | 135 } |
122 | 136 |
123 HtmlDialogUIDelegate* ConstrainedHtmlDelegateMac::GetHtmlDialogUIDelegate() { | 137 HtmlDialogUIDelegate* ConstrainedHtmlDelegateMac::GetHtmlDialogUIDelegate() { |
124 return html_delegate_; | 138 return html_delegate_; |
125 } | 139 } |
126 | 140 |
127 void ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() { | 141 void ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() { |
128 closed_via_webui_ = true; | 142 closed_via_webui_ = true; |
129 DCHECK(constrained_window_); | 143 DCHECK(constrained_window_); |
130 if (constrained_window_) | 144 if (constrained_window_) |
131 constrained_window_->CloseConstrainedWindow(); | 145 constrained_window_->CloseConstrainedWindow(); |
132 } | 146 } |
133 | 147 |
| 148 void ConstrainedHtmlDelegateMac::ReleaseTabContentsOnDialogClose() { |
| 149 release_tab_on_close_ = true; |
| 150 } |
| 151 |
134 // static | 152 // static |
135 ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | 153 ConstrainedHtmlUIDelegate* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( |
136 Profile* profile, | 154 Profile* profile, |
137 HtmlDialogUIDelegate* delegate, | 155 HtmlDialogUIDelegate* delegate, |
138 TabContentsWrapper* wrapper) { | 156 TabContentsWrapper* wrapper) { |
139 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs. | 157 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs. |
140 ConstrainedHtmlDelegateMac* constrained_delegate = | 158 ConstrainedHtmlDelegateMac* constrained_delegate = |
141 new ConstrainedHtmlDelegateMac(profile, delegate); | 159 new ConstrainedHtmlDelegateMac(profile, delegate); |
142 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs. | 160 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs. |
143 ConstrainedWindow* constrained_window = | 161 ConstrainedWindow* constrained_window = |
144 new ConstrainedWindowMac(wrapper, constrained_delegate); | 162 new ConstrainedWindowMac(wrapper, constrained_delegate); |
145 constrained_delegate->set_window(constrained_window); | 163 constrained_delegate->set_window(constrained_window); |
146 return constrained_window; | 164 return constrained_delegate; |
147 } | 165 } |
148 | 166 |
149 @implementation ConstrainedHtmlDialogSheetCocoa | 167 @implementation ConstrainedHtmlDialogSheetCocoa |
150 | 168 |
151 - (id)initWithConstrainedHtmlDelegateMac: | 169 - (id)initWithConstrainedHtmlDelegateMac: |
152 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac { | 170 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac { |
153 if ((self = [super init])) | 171 if ((self = [super init])) |
154 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac; | 172 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac; |
155 return self; | 173 return self; |
156 } | 174 } |
157 | 175 |
158 - (void)sheetDidEnd:(NSWindow*)sheet | 176 - (void)sheetDidEnd:(NSWindow*)sheet |
159 returnCode:(int)returnCode | 177 returnCode:(int)returnCode |
160 contextInfo:(void *)contextInfo { | 178 contextInfo:(void *)contextInfo { |
161 [sheet orderOut:self]; | 179 [sheet orderOut:self]; |
162 } | 180 } |
163 | 181 |
164 @end | 182 @end |
OLD | NEW |