Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: chrome/browser/ui/cocoa/constrained_html_delegate_mac.mm

Issue 8220026: Add new methods to create constrained windows for print preview and release the internal TabConte... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 }
30
31 TabContentsWrapper* tab() {
32 return tab_.get();
33 }
27 34
28 // ConstrainedWindowMacDelegateCustomSheet ----------------------------------- 35 // ConstrainedWindowMacDelegateCustomSheet -----------------------------------
29 virtual void DeleteDelegate() OVERRIDE { 36 virtual void DeleteDelegate() OVERRIDE {
30 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to 37 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to
31 // your delegate in this method." 38 // your delegate in this method."
32 if (is_sheet_open()) 39 if (is_sheet_open())
33 [NSApp endSheet:sheet()]; 40 [NSApp endSheet:sheet()];
34 if (!closed_via_webui_) 41 if (!closed_via_webui_)
35 html_delegate_->OnDialogClosed(""); 42 html_delegate_->OnDialogClosed("");
36 delete this; 43 delete this;
37 } 44 }
38 45
39 // ConstrainedHtmlDelegate --------------------------------------------------- 46 // ConstrainedHtmlDelegate ---------------------------------------------------
40 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE; 47 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE;
41 virtual void OnDialogCloseFromWebUI() OVERRIDE; 48 virtual void OnDialogCloseFromWebUI() OVERRIDE;
49 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE;
42 50
43 // HtmlDialogTabContentsDelegate --------------------------------------------- 51 // HtmlDialogTabContentsDelegate ---------------------------------------------
44 virtual void HandleKeyboardEvent( 52 virtual void HandleKeyboardEvent(
45 const NativeWebKeyboardEvent& event) OVERRIDE {} 53 const NativeWebKeyboardEvent& event) OVERRIDE {}
46 54
47 void set_window(ConstrainedWindow* window) { 55 void set_window(ConstrainedWindow* window) {
48 constrained_window_ = window; 56 constrained_window_ = window;
49 } 57 }
50 58
51 private: 59 private:
52 TabContents tab_contents_; // Holds the HTML to be displayed in the sheet. 60 // Holds the HTML to be displayed in the sheet.
61 scoped_ptr<TabContentsWrapper> tab_;
53 HtmlDialogUIDelegate* html_delegate_; // weak. 62 HtmlDialogUIDelegate* html_delegate_; // weak.
54 63
55 // The constrained window that owns |this|. Saved here because it needs to be 64 // The constrained window that owns |this|. Saved here because it needs to be
56 // closed in response to the WebUI OnDialogClose callback. 65 // closed in response to the WebUI OnDialogClose callback.
57 ConstrainedWindow* constrained_window_; 66 ConstrainedWindow* constrained_window_;
58 67
59 // Was the dialog closed from WebUI (in which case |html_delegate_|'s 68 // Was the dialog closed from WebUI (in which case |html_delegate_|'s
60 // OnDialogClosed() method has already been called)? 69 // OnDialogClosed() method has already been called)?
61 bool closed_via_webui_; 70 bool closed_via_webui_;
62 71
72 // If true, release |tab_| on close instead of destroying it.
73 bool release_tab_on_close_;
74
63 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac); 75 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac);
64 }; 76 };
65 77
66 // The delegate used to forward events from the sheet to the constrained 78 // The delegate used to forward events from the sheet to the constrained
67 // window delegate. This bridge needs to be passed into the customsheet 79 // window delegate. This bridge needs to be passed into the customsheet
68 // to allow the HtmlDialog to know when the sheet closes. 80 // to allow the HtmlDialog to know when the sheet closes.
69 @interface ConstrainedHtmlDialogSheetCocoa : NSObject { 81 @interface ConstrainedHtmlDialogSheetCocoa : NSObject {
70 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak 82 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak
71 } 83 }
72 - (id)initWithConstrainedHtmlDelegateMac: 84 - (id)initWithConstrainedHtmlDelegateMac:
73 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac; 85 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac;
74 - (void)sheetDidEnd:(NSWindow*)sheet 86 - (void)sheetDidEnd:(NSWindow*)sheet
75 returnCode:(int)returnCode 87 returnCode:(int)returnCode
76 contextInfo:(void*)contextInfo; 88 contextInfo:(void*)contextInfo;
77 @end 89 @end
78 90
79 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac( 91 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac(
80 Profile* profile, 92 Profile* profile,
81 HtmlDialogUIDelegate* delegate) 93 HtmlDialogUIDelegate* delegate)
82 : HtmlDialogTabContentsDelegate(profile), 94 : HtmlDialogTabContentsDelegate(profile),
83 tab_contents_(profile, NULL, MSG_ROUTING_NONE, NULL, NULL),
84 html_delegate_(delegate), 95 html_delegate_(delegate),
85 constrained_window_(NULL), 96 constrained_window_(NULL),
86 closed_via_webui_(false) { 97 closed_via_webui_(false),
87 tab_contents_.set_delegate(this); 98 release_tab_on_close_(false) {
99 TabContents* tab_contents =
100 new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL);
101 tab_.reset(new TabContentsWrapper(tab_contents));
102 tab_contents->set_delegate(this);
88 103
89 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI 104 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI
90 // can get a reference to |this|. 105 // can get a reference to |this|.
91 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty( 106 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty(
92 tab_contents_.property_bag(), this); 107 tab_contents->property_bag(), this);
93 108
94 tab_contents_.controller().LoadURL(delegate->GetDialogContentURL(), 109 tab_contents->controller().LoadURL(delegate->GetDialogContentURL(),
95 GURL(), PageTransition::START_PAGE, 110 GURL(), PageTransition::START_PAGE,
96 std::string()); 111 std::string());
97 112
98 // Create NSWindow to hold tab_contents in the constrained sheet: 113 // Create NSWindow to hold tab_contents in the constrained sheet:
99 gfx::Size size; 114 gfx::Size size;
100 delegate->GetDialogSize(&size); 115 delegate->GetDialogSize(&size);
101 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); 116 NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
102 117
103 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when 118 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when
104 // the sheet is initialized. 119 // the sheet is initialized.
105 scoped_nsobject<NSWindow> window; 120 scoped_nsobject<NSWindow> window;
106 window.reset( 121 window.reset(
107 [[NSWindow alloc] initWithContentRect:frame 122 [[NSWindow alloc] initWithContentRect:frame
108 styleMask:NSTitledWindowMask 123 styleMask:NSTitledWindowMask
109 backing:NSBackingStoreBuffered 124 backing:NSBackingStoreBuffered
110 defer:YES]); 125 defer:YES]);
111 126
112 [window.get() setContentView:tab_contents_.GetNativeView()]; 127 [window.get() setContentView:tab_contents->GetNativeView()];
113 128
114 // Set the custom sheet to point to the new window. 129 // Set the custom sheet to point to the new window.
115 ConstrainedWindowMacDelegateCustomSheet::init( 130 ConstrainedWindowMacDelegateCustomSheet::init(
116 window.get(), 131 window.get(),
117 [[[ConstrainedHtmlDialogSheetCocoa alloc] 132 [[[ConstrainedHtmlDialogSheetCocoa alloc]
118 initWithConstrainedHtmlDelegateMac:this] autorelease], 133 initWithConstrainedHtmlDelegateMac:this] autorelease],
119 @selector(sheetDidEnd:returnCode:contextInfo:)); 134 @selector(sheetDidEnd:returnCode:contextInfo:));
120 } 135 }
121 136
122 HtmlDialogUIDelegate* ConstrainedHtmlDelegateMac::GetHtmlDialogUIDelegate() { 137 HtmlDialogUIDelegate* ConstrainedHtmlDelegateMac::GetHtmlDialogUIDelegate() {
123 return html_delegate_; 138 return html_delegate_;
124 } 139 }
125 140
126 void ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() { 141 void ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() {
127 closed_via_webui_ = true; 142 closed_via_webui_ = true;
128 DCHECK(constrained_window_); 143 DCHECK(constrained_window_);
129 if (constrained_window_) 144 if (constrained_window_)
130 constrained_window_->CloseConstrainedWindow(); 145 constrained_window_->CloseConstrainedWindow();
131 } 146 }
132 147
148 void ConstrainedHtmlDelegateMac::ReleaseTabContentsOnDialogClose() {
149 release_tab_on_close_ = true;
150 }
151
133 // static 152 // static
134 ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( 153 ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog(
135 Profile* profile, 154 Profile* profile,
136 HtmlDialogUIDelegate* delegate, 155 HtmlDialogUIDelegate* delegate,
137 TabContentsWrapper* wrapper) { 156 TabContentsWrapper* wrapper) {
138 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs. 157 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs.
139 ConstrainedHtmlDelegateMac* constrained_delegate = 158 ConstrainedHtmlDelegateMac* constrained_delegate =
140 new ConstrainedHtmlDelegateMac(profile, delegate); 159 new ConstrainedHtmlDelegateMac(profile, delegate);
141 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs. 160 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs.
142 ConstrainedWindow* constrained_window = 161 ConstrainedWindow* constrained_window =
143 new ConstrainedWindowMac(wrapper, constrained_delegate); 162 new ConstrainedWindowMac(wrapper, constrained_delegate);
144 constrained_delegate->set_window(constrained_window); 163 constrained_delegate->set_window(constrained_window);
145 return constrained_window; 164 return constrained_window;
146 } 165 }
147 166
167 // static
168 TabContentsWrapper* ConstrainedHtmlUI::CreateConstrainedPrintPreviewHtmlUI(
169 Profile* profile,
170 HtmlDialogUIDelegate* delegate,
171 TabContentsWrapper* wrapper) {
172 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs.
173 ConstrainedHtmlDelegateMac* constrained_delegate =
174 new ConstrainedHtmlDelegateMac(profile, delegate);
175 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs.
176 ConstrainedWindow* constrained_window =
177 new ConstrainedWindowMac(wrapper, constrained_delegate);
178 constrained_delegate->set_window(constrained_window);
179 return constrained_delegate->tab();
180 }
181
148 @implementation ConstrainedHtmlDialogSheetCocoa 182 @implementation ConstrainedHtmlDialogSheetCocoa
149 183
150 - (id)initWithConstrainedHtmlDelegateMac: 184 - (id)initWithConstrainedHtmlDelegateMac:
151 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac { 185 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac {
152 if ((self = [super init])) 186 if ((self = [super init]))
153 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac; 187 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac;
154 return self; 188 return self;
155 } 189 }
156 190
157 - (void)sheetDidEnd:(NSWindow*)sheet 191 - (void)sheetDidEnd:(NSWindow*)sheet
158 returnCode:(int)returnCode 192 returnCode:(int)returnCode
159 contextInfo:(void *)contextInfo { 193 contextInfo:(void *)contextInfo {
160 [sheet orderOut:self]; 194 [sheet orderOut:self];
161 } 195 }
162 196
163 @end 197 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698