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

Side by Side Diff: chrome/browser/cocoa/html_dialog_window_controller.mm

Issue 402065: Made HtmlDialogWindowController on OS X use its own browser. (Closed)
Patch Set: synced to head Created 11 years, 1 month 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/html_dialog_window_controller.h" 5 #import "chrome/browser/cocoa/html_dialog_window_controller.h"
6 6
7 #include "base/gfx/size.h" 7 #include "base/gfx/size.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/scoped_nsobject.h" 9 #include "base/scoped_nsobject.h"
10 #include "base/sys_string_conversions.h" 10 #include "base/sys_string_conversions.h"
11 #include "chrome/browser/browser.h" 11 #include "chrome/browser/browser.h"
12 #import "chrome/browser/cocoa/browser_command_executor.h" 12 #import "chrome/browser/cocoa/browser_command_executor.h"
13 #import "chrome/browser/cocoa/chrome_event_processing_window.h" 13 #import "chrome/browser/cocoa/chrome_event_processing_window.h"
14 #include "chrome/browser/dom_ui/html_dialog_ui.h" 14 #include "chrome/browser/dom_ui/html_dialog_ui.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
17 17
18 // ChromeEventProcessingWindow expects its controller to implement the
19 // BrowserCommandExecutor protocol.
20 @interface HtmlDialogWindowController (InternalAPI) <BrowserCommandExecutor>
21
22 // BrowserCommandExecutor methods.
23 - (void)executeCommand:(int)command;
24
25 @end
26
18 HtmlDialogWindowDelegateBridge::HtmlDialogWindowDelegateBridge( 27 HtmlDialogWindowDelegateBridge::HtmlDialogWindowDelegateBridge(
19 HtmlDialogUIDelegate* delegate, NSWindowController* controller, 28 NSWindowController* controller, HtmlDialogUIDelegate* delegate,
20 NSWindow* window, Browser* browser) 29 Browser* browser)
21 : delegate_(delegate), controller_(controller), window_(window), 30 : controller_(controller), delegate_(delegate), browser_(browser) {
22 browser_(browser) { 31 DCHECK(controller_);
23 DCHECK(delegate_); 32 DCHECK(delegate_);
24 DCHECK(controller_);
25 DCHECK(window_);
26 DCHECK(browser_); 33 DCHECK(browser_);
27 } 34 }
28 35
29 HtmlDialogWindowDelegateBridge::~HtmlDialogWindowDelegateBridge() {} 36 HtmlDialogWindowDelegateBridge::~HtmlDialogWindowDelegateBridge() {}
30 37
31 void HtmlDialogWindowDelegateBridge::WindowControllerClosed() { 38 void HtmlDialogWindowDelegateBridge::WindowControllerClosed() {
32 DelegateOnDialogClosed(""); 39 DelegateOnDialogClosed("");
40 controller_ = nil;
41 browser_ = NULL;
33 } 42 }
34 43
35 bool HtmlDialogWindowDelegateBridge::DelegateOnDialogClosed( 44 bool HtmlDialogWindowDelegateBridge::DelegateOnDialogClosed(
36 const std::string& json_retval) { 45 const std::string& json_retval) {
37 if (delegate_) { 46 if (delegate_) {
38 HtmlDialogUIDelegate* real_delegate = delegate_; 47 HtmlDialogUIDelegate* real_delegate = delegate_;
39 delegate_ = NULL; 48 delegate_ = NULL;
40 real_delegate->OnDialogClosed(json_retval); 49 real_delegate->OnDialogClosed(json_retval);
41 return true; 50 return true;
42 } 51 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 std::string HtmlDialogWindowDelegateBridge::GetDialogArgs() const { 96 std::string HtmlDialogWindowDelegateBridge::GetDialogArgs() const {
88 return delegate_ ? delegate_->GetDialogArgs() : ""; 97 return delegate_ ? delegate_->GetDialogArgs() : "";
89 } 98 }
90 99
91 void HtmlDialogWindowDelegateBridge::OnDialogClosed( 100 void HtmlDialogWindowDelegateBridge::OnDialogClosed(
92 const std::string& json_retval) { 101 const std::string& json_retval) {
93 // [controller_ close] should be called at most once, too. 102 // [controller_ close] should be called at most once, too.
94 if (DelegateOnDialogClosed(json_retval)) { 103 if (DelegateOnDialogClosed(json_retval)) {
95 [controller_ close]; 104 [controller_ close];
96 } 105 }
106 controller_ = nil;
107 browser_ = NULL;
97 } 108 }
98 109
99 // TabContentsDelegate definitions. Most of this logic is copied from 110 // TabContentsDelegate definitions. Most of this logic is copied from
100 // chrome/browser/views/html_dialog_view.cc . All functions with empty 111 // chrome/browser/views/html_dialog_view.cc . All functions with empty
101 // bodies are notifications we don't care about. 112 // bodies are notifications we don't care about.
102 113
103 void HtmlDialogWindowDelegateBridge::OpenURLFromTab( 114 void HtmlDialogWindowDelegateBridge::OpenURLFromTab(
104 TabContents* source, const GURL& url, const GURL& referrer, 115 TabContents* source, const GURL& url, const GURL& referrer,
105 WindowOpenDisposition disposition, PageTransition::Type transition) { 116 WindowOpenDisposition disposition, PageTransition::Type transition) {
106 // Force all links to open in a new window. 117 if (browser_) {
107 static_cast<TabContentsDelegate*>(browser_)-> 118 // Force all links to open in a new window.
108 OpenURLFromTab(source, url, referrer, NEW_WINDOW, transition); 119 static_cast<TabContentsDelegate*>(browser_)->
120 OpenURLFromTab(source, url, referrer, NEW_WINDOW, transition);
121 }
109 } 122 }
110 123
111 void HtmlDialogWindowDelegateBridge::NavigationStateChanged( 124 void HtmlDialogWindowDelegateBridge::NavigationStateChanged(
112 const TabContents* source, unsigned changed_flags) { 125 const TabContents* source, unsigned changed_flags) {
113 } 126 }
114 127
115 void HtmlDialogWindowDelegateBridge::AddNewContents( 128 void HtmlDialogWindowDelegateBridge::AddNewContents(
116 TabContents* source, TabContents* new_contents, 129 TabContents* source, TabContents* new_contents,
117 WindowOpenDisposition disposition, const gfx::Rect& initial_pos, 130 WindowOpenDisposition disposition, const gfx::Rect& initial_pos,
118 bool user_gesture) { 131 bool user_gesture) {
119 // Force this to open in a new window, too. 132 if (browser_) {
120 static_cast<TabContentsDelegate*>(browser_)-> 133 // Force this to open in a new window, too.
121 AddNewContents(source, new_contents, NEW_WINDOW, 134 static_cast<TabContentsDelegate*>(browser_)->
122 initial_pos, user_gesture); 135 AddNewContents(source, new_contents, NEW_WINDOW,
136 initial_pos, user_gesture);
137 }
123 } 138 }
124 139
125 void HtmlDialogWindowDelegateBridge::ActivateContents(TabContents* contents) {} 140 void HtmlDialogWindowDelegateBridge::ActivateContents(TabContents* contents) {}
126 141
127 void HtmlDialogWindowDelegateBridge::LoadingStateChanged(TabContents* source) {} 142 void HtmlDialogWindowDelegateBridge::LoadingStateChanged(TabContents* source) {}
128 143
129 void HtmlDialogWindowDelegateBridge::CloseContents(TabContents* source) {} 144 void HtmlDialogWindowDelegateBridge::CloseContents(TabContents* source) {}
130 145
131 void HtmlDialogWindowDelegateBridge::MoveContents(TabContents* source, 146 void HtmlDialogWindowDelegateBridge::MoveContents(TabContents* source,
132 const gfx::Rect& pos) { 147 const gfx::Rect& pos) {
(...skipping 13 matching lines...) Expand all
146 161
147 void HtmlDialogWindowDelegateBridge::URLStarredChanged( 162 void HtmlDialogWindowDelegateBridge::URLStarredChanged(
148 TabContents* source, bool starred) { 163 TabContents* source, bool starred) {
149 // We don't have a visible star to click in the window. 164 // We don't have a visible star to click in the window.
150 NOTREACHED(); 165 NOTREACHED();
151 } 166 }
152 167
153 void HtmlDialogWindowDelegateBridge::UpdateTargetURL( 168 void HtmlDialogWindowDelegateBridge::UpdateTargetURL(
154 TabContents* source, const GURL& url) {} 169 TabContents* source, const GURL& url) {}
155 170
156 // ChromeEventProcessingWindow expect its controller to implement this
157 // protocol.
158
159 @interface HtmlDialogWindowController (InternalAPI) <BrowserCommandExecutor>
160
161 - (void)executeCommand:(int)command;
162
163 @end
164
165 @implementation HtmlDialogWindowController (InternalAPI) 171 @implementation HtmlDialogWindowController (InternalAPI)
166 172
167 - (void)executeCommand:(int)command { 173 // This gets called whenever a chrome-specific keyboard shortcut is performed
168 if (browser_->command_updater()->IsCommandEnabled(command)) { 174 // in the HTML dialog window. We simply swallow all those events.
169 browser_->ExecuteCommand(command); 175 - (void)executeCommand:(int)command {}
170 }
171 }
172 176
173 @end 177 @end
174 178
175 @implementation HtmlDialogWindowController 179 @implementation HtmlDialogWindowController
176 180
177 + (void)showHtmlDialog:(HtmlDialogUIDelegate*)delegate 181 + (void)showHtmlDialog:(HtmlDialogUIDelegate*)delegate
178 parentWindow:(gfx::NativeWindow)parent_window 182 profile:(Profile*)profile
179 browser:(Browser*)browser { 183 parentWindow:(gfx::NativeWindow)parentWindow {
180 HtmlDialogWindowController* html_dialog_window_controller = 184 HtmlDialogWindowController* htmlDialogWindowController =
181 [[HtmlDialogWindowController alloc] initWithDelegate:delegate 185 [[HtmlDialogWindowController alloc] initWithDelegate:delegate
182 parentWindow:parent_window 186 profile:profile
183 browser:browser]; 187 parentWindow:parentWindow];
184 [html_dialog_window_controller loadDialogContents]; 188 [htmlDialogWindowController loadDialogContents];
185 [html_dialog_window_controller showWindow:nil]; 189 [htmlDialogWindowController showWindow:nil];
186 } 190 }
187 191
188 - (id)initWithDelegate:(HtmlDialogUIDelegate*)delegate 192 - (id)initWithDelegate:(HtmlDialogUIDelegate*)delegate
189 parentWindow:(gfx::NativeWindow)parent_window 193 profile:(Profile*)profile
190 browser:(Browser*)browser { 194 parentWindow:(gfx::NativeWindow)parentWindow {
191 DCHECK(delegate); 195 DCHECK(delegate);
192 DCHECK(parent_window); 196 DCHECK(profile);
193 DCHECK(browser); 197 DCHECK(parentWindow);
194 198
195 // Put the dialog box in the center of the window. 199 // Put the dialog box in the center of the window.
196 // 200 //
197 // TODO(akalin): Surely there must be a cleaner way to do this. 201 // TODO(akalin): Surely there must be a cleaner way to do this.
198 // 202 //
199 // TODO(akalin): Perhaps use [window center] instead, which centers 203 // TODO(akalin): Perhaps use [window center] instead, which centers
200 // the dialog to the screen, although it doesn't match the Windows 204 // the dialog to the screen, although it doesn't match the Windows
201 // behavior. 205 // behavior.
202 NSRect parent_window_frame = [parent_window frame]; 206 NSRect parentWindowFrame = [parentWindow frame];
203 NSPoint parent_window_origin = parent_window_frame.origin; 207 NSPoint parentWindowOrigin = parentWindowFrame.origin;
204 NSSize parent_window_size = parent_window_frame.size; 208 NSSize parentWindowSize = parentWindowFrame.size;
205 gfx::Size dialog_size; 209 gfx::Size dialogSize;
206 delegate->GetDialogSize(&dialog_size); 210 delegate->GetDialogSize(&dialogSize);
207 NSRect dialog_rect = 211 NSRect dialogRect =
208 NSMakeRect(parent_window_origin.x + 212 NSMakeRect(parentWindowOrigin.x +
209 (parent_window_size.width - dialog_size.width()) / 2, 213 (parentWindowSize.width - dialogSize.width()) / 2,
210 parent_window_origin.y + 214 parentWindowOrigin.y +
211 (parent_window_size.height - dialog_size.height()) / 2, 215 (parentWindowSize.height - dialogSize.height()) / 2,
212 dialog_size.width(), 216 dialogSize.width(),
213 dialog_size.height()); 217 dialogSize.height());
214 // TODO(akalin): Make the window resizable (but with the minimum size being 218 // TODO(akalin): Make the window resizable (but with the minimum size being
215 // dialog_size and always on top (but not modal) to match the Windows 219 // dialog_size and always on top (but not modal) to match the Windows
216 // behavior. 220 // behavior.
217 NSUInteger style = NSTitledWindowMask | NSClosableWindowMask; 221 NSUInteger style = NSTitledWindowMask | NSClosableWindowMask;
218 scoped_nsobject<ChromeEventProcessingWindow> window( 222 scoped_nsobject<ChromeEventProcessingWindow> window(
219 [[ChromeEventProcessingWindow alloc] 223 [[ChromeEventProcessingWindow alloc]
220 initWithContentRect:dialog_rect 224 initWithContentRect:dialogRect
221 styleMask:style 225 styleMask:style
222 backing:NSBackingStoreBuffered 226 backing:NSBackingStoreBuffered
223 defer:YES]); 227 defer:YES]);
224 if (!window.get()) { 228 if (!window.get()) {
225 return nil; 229 return nil;
226 } 230 }
227 self = [super initWithWindow:window]; 231 self = [super initWithWindow:window];
228 if (!self) { 232 if (!self) {
229 return nil; 233 return nil;
230 } 234 }
231 [window setWindowController:self]; 235 [window setWindowController:self];
232 [window setDelegate:self]; 236 [window setDelegate:self];
233 [window setTitle:base::SysWideToNSString(delegate->GetDialogTitle())]; 237 [window setTitle:base::SysWideToNSString(delegate->GetDialogTitle())];
234 browser_ = browser; 238 browser_.reset(new Browser(Browser::TYPE_NORMAL, profile));
235 delegate_.reset( 239 delegate_.reset(
236 new HtmlDialogWindowDelegateBridge(delegate, self, window, browser)); 240 new HtmlDialogWindowDelegateBridge(self, delegate, browser_.get()));
237 return self; 241 return self;
238 } 242 }
239 243
240 - (void)loadDialogContents { 244 - (void)loadDialogContents {
241 // TODO(akalin): Figure out if this can be an incognito profile.
242 Profile* profile = browser_->profile(); 245 Profile* profile = browser_->profile();
243 tab_contents_.reset(new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL)); 246 tabContents_.reset(new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL));
244 [[self window] setContentView:tab_contents_->GetNativeView()]; 247 [[self window] setContentView:tabContents_->GetNativeView()];
245 tab_contents_->set_delegate(delegate_.get()); 248 tabContents_->set_delegate(delegate_.get());
246 249
247 // This must be done before loading the page; see the comments in 250 // This must be done before loading the page; see the comments in
248 // HtmlDialogUI. 251 // HtmlDialogUI.
249 HtmlDialogUI::GetPropertyAccessor().SetProperty(tab_contents_->property_bag(), 252 HtmlDialogUI::GetPropertyAccessor().SetProperty(tabContents_->property_bag(),
250 delegate_.get()); 253 delegate_.get());
251 254
252 tab_contents_->controller().LoadURL(delegate_->GetDialogContentURL(), 255 tabContents_->controller().LoadURL(delegate_->GetDialogContentURL(),
253 GURL(), PageTransition::START_PAGE); 256 GURL(), PageTransition::START_PAGE);
254 257
255 // TODO(akalin): add accelerator for ESC to close the dialog box. 258 // TODO(akalin): add accelerator for ESC to close the dialog box.
256 // 259 //
257 // TODO(akalin): Figure out why implementing (void)cancel:(id)sender 260 // TODO(akalin): Figure out why implementing (void)cancel:(id)sender
258 // to do the above doesn't work. 261 // to do the above doesn't work.
259 } 262 }
260 263
261 - (void)windowWillClose:(NSNotification*)notification { 264 - (void)windowWillClose:(NSNotification*)notification {
262 delegate_->WindowControllerClosed(); 265 delegate_->WindowControllerClosed();
263 [self autorelease]; 266 [self autorelease];
264 } 267 }
265 268
266 @end 269 @end
267 270
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/html_dialog_window_controller.h ('k') | chrome/browser/cocoa/html_dialog_window_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698