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 #import "chrome/browser/ui/cocoa/html_dialog_window_controller.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_nsobject.h" | |
9 #include "base/property_bag.h" | |
10 #include "base/sys_string_conversions.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #import "chrome/browser/ui/browser_dialogs.h" | |
14 #import "chrome/browser/ui/cocoa/browser_command_executor.h" | |
15 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | |
16 #include "chrome/browser/ui/dialog_style.h" | |
17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
18 #include "chrome/browser/ui/webui/html_dialog_controller.h" | |
19 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" | |
20 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
21 #include "content/public/browser/native_web_keyboard_event.h" | |
22 #include "content/public/browser/web_contents.h" | |
23 #include "content/public/browser/web_ui_message_handler.h" | |
24 #include "ui/base/keycodes/keyboard_codes.h" | |
25 #include "ui/gfx/size.h" | |
26 | |
27 using content::WebContents; | |
28 using content::WebUIMessageHandler; | |
29 | |
30 // Thin bridge that routes notifications to | |
31 // HtmlDialogWindowController's member variables. | |
32 class HtmlDialogWindowDelegateBridge : public HtmlDialogUIDelegate, | |
33 public HtmlDialogTabContentsDelegate { | |
34 public: | |
35 // All parameters must be non-NULL/non-nil. | |
36 HtmlDialogWindowDelegateBridge(HtmlDialogWindowController* controller, | |
37 Profile* profile, | |
38 Browser* browser, | |
39 HtmlDialogUIDelegate* delegate); | |
40 | |
41 virtual ~HtmlDialogWindowDelegateBridge(); | |
42 | |
43 // Called when the window is directly closed, e.g. from the close | |
44 // button or from an accelerator. | |
45 void WindowControllerClosed(); | |
46 | |
47 // HtmlDialogUIDelegate declarations. | |
48 virtual ui::ModalType GetDialogModalType() const OVERRIDE; | |
49 virtual string16 GetDialogTitle() const OVERRIDE; | |
50 virtual GURL GetDialogContentURL() const OVERRIDE; | |
51 virtual void GetWebUIMessageHandlers( | |
52 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; | |
53 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; | |
54 virtual void GetMinimumDialogSize(gfx::Size* size) const OVERRIDE; | |
55 virtual std::string GetDialogArgs() const OVERRIDE; | |
56 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; | |
57 virtual void OnCloseContents(WebContents* source, | |
58 bool* out_close_dialog) OVERRIDE; | |
59 virtual bool ShouldShowDialogTitle() const OVERRIDE { return true; } | |
60 | |
61 // HtmlDialogTabContentsDelegate declarations. | |
62 virtual void MoveContents(WebContents* source, const gfx::Rect& pos); | |
63 virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event); | |
64 virtual void CloseContents(WebContents* source) OVERRIDE; | |
65 virtual content::WebContents* OpenURLFromTab( | |
66 content::WebContents* source, | |
67 const content::OpenURLParams& params) OVERRIDE; | |
68 virtual void AddNewContents(content::WebContents* source, | |
69 content::WebContents* new_contents, | |
70 WindowOpenDisposition disposition, | |
71 const gfx::Rect& initial_pos, | |
72 bool user_gesture) OVERRIDE; | |
73 virtual void LoadingStateChanged(content::WebContents* source) OVERRIDE; | |
74 | |
75 private: | |
76 HtmlDialogWindowController* controller_; // weak | |
77 HtmlDialogUIDelegate* delegate_; // weak, owned by controller_ | |
78 HtmlDialogController* dialog_controller_; | |
79 | |
80 // Calls delegate_'s OnDialogClosed() exactly once, nulling it out | |
81 // afterwards so that no other HtmlDialogUIDelegate calls are sent | |
82 // to it. Returns whether or not the OnDialogClosed() was actually | |
83 // called on the delegate. | |
84 bool DelegateOnDialogClosed(const std::string& json_retval); | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(HtmlDialogWindowDelegateBridge); | |
87 }; | |
88 | |
89 // ChromeEventProcessingWindow expects its controller to implement the | |
90 // BrowserCommandExecutor protocol. | |
91 @interface HtmlDialogWindowController (InternalAPI) <BrowserCommandExecutor> | |
92 | |
93 // BrowserCommandExecutor methods. | |
94 - (void)executeCommand:(int)command; | |
95 | |
96 @end | |
97 | |
98 namespace browser { | |
99 | |
100 gfx::NativeWindow ShowHtmlDialog(gfx::NativeWindow parent, | |
101 Profile* profile, | |
102 Browser* browser, | |
103 HtmlDialogUIDelegate* delegate, | |
104 DialogStyle style) { | |
105 return [HtmlDialogWindowController showHtmlDialog:delegate | |
106 profile:profile | |
107 browser:browser]; | |
108 } | |
109 | |
110 void CloseHtmlDialog(gfx::NativeWindow window) { | |
111 [window performClose:nil]; | |
112 } | |
113 | |
114 } // namespace html_dialog_window_controller | |
115 | |
116 HtmlDialogWindowDelegateBridge::HtmlDialogWindowDelegateBridge( | |
117 HtmlDialogWindowController* controller, | |
118 Profile* profile, | |
119 Browser* browser, | |
120 HtmlDialogUIDelegate* delegate) | |
121 : HtmlDialogTabContentsDelegate(profile), | |
122 controller_(controller), | |
123 delegate_(delegate), | |
124 dialog_controller_(new HtmlDialogController(this, profile, browser)) { | |
125 DCHECK(controller_); | |
126 DCHECK(delegate_); | |
127 } | |
128 | |
129 HtmlDialogWindowDelegateBridge::~HtmlDialogWindowDelegateBridge() {} | |
130 | |
131 void HtmlDialogWindowDelegateBridge::WindowControllerClosed() { | |
132 Detach(); | |
133 delete dialog_controller_; | |
134 controller_ = nil; | |
135 DelegateOnDialogClosed(""); | |
136 } | |
137 | |
138 bool HtmlDialogWindowDelegateBridge::DelegateOnDialogClosed( | |
139 const std::string& json_retval) { | |
140 if (delegate_) { | |
141 HtmlDialogUIDelegate* real_delegate = delegate_; | |
142 delegate_ = NULL; | |
143 real_delegate->OnDialogClosed(json_retval); | |
144 return true; | |
145 } | |
146 return false; | |
147 } | |
148 | |
149 // HtmlDialogUIDelegate definitions. | |
150 | |
151 // All of these functions check for NULL first since delegate_ is set | |
152 // to NULL when the window is closed. | |
153 | |
154 ui::ModalType HtmlDialogWindowDelegateBridge::GetDialogModalType() const { | |
155 // TODO(akalin): Support modal dialog boxes. | |
156 if (delegate_ && delegate_->GetDialogModalType() != ui::MODAL_TYPE_NONE) { | |
157 LOG(WARNING) << "Modal HTML dialogs are not supported yet"; | |
158 } | |
159 return ui::MODAL_TYPE_NONE; | |
160 } | |
161 | |
162 string16 HtmlDialogWindowDelegateBridge::GetDialogTitle() const { | |
163 return delegate_ ? delegate_->GetDialogTitle() : string16(); | |
164 } | |
165 | |
166 GURL HtmlDialogWindowDelegateBridge::GetDialogContentURL() const { | |
167 return delegate_ ? delegate_->GetDialogContentURL() : GURL(); | |
168 } | |
169 | |
170 void HtmlDialogWindowDelegateBridge::GetWebUIMessageHandlers( | |
171 std::vector<WebUIMessageHandler*>* handlers) const { | |
172 if (delegate_) { | |
173 delegate_->GetWebUIMessageHandlers(handlers); | |
174 } else { | |
175 // TODO(akalin): Add this clause in the windows version. Also | |
176 // make sure that everything expects handlers to be non-NULL and | |
177 // document it. | |
178 handlers->clear(); | |
179 } | |
180 } | |
181 | |
182 void HtmlDialogWindowDelegateBridge::GetDialogSize(gfx::Size* size) const { | |
183 if (delegate_) | |
184 delegate_->GetDialogSize(size); | |
185 else | |
186 *size = gfx::Size(); | |
187 } | |
188 | |
189 void HtmlDialogWindowDelegateBridge::GetMinimumDialogSize( | |
190 gfx::Size* size) const { | |
191 if (delegate_) | |
192 delegate_->GetMinimumDialogSize(size); | |
193 else | |
194 *size = gfx::Size(); | |
195 } | |
196 | |
197 std::string HtmlDialogWindowDelegateBridge::GetDialogArgs() const { | |
198 return delegate_ ? delegate_->GetDialogArgs() : ""; | |
199 } | |
200 | |
201 void HtmlDialogWindowDelegateBridge::OnDialogClosed( | |
202 const std::string& json_retval) { | |
203 Detach(); | |
204 // [controller_ close] should be called at most once, too. | |
205 if (DelegateOnDialogClosed(json_retval)) { | |
206 [controller_ close]; | |
207 } | |
208 controller_ = nil; | |
209 } | |
210 | |
211 void HtmlDialogWindowDelegateBridge::OnCloseContents(WebContents* source, | |
212 bool* out_close_dialog) { | |
213 if (out_close_dialog) | |
214 *out_close_dialog = true; | |
215 } | |
216 | |
217 void HtmlDialogWindowDelegateBridge::CloseContents(WebContents* source) { | |
218 bool close_dialog = false; | |
219 OnCloseContents(source, &close_dialog); | |
220 if (close_dialog) | |
221 OnDialogClosed(std::string()); | |
222 } | |
223 | |
224 content::WebContents* HtmlDialogWindowDelegateBridge::OpenURLFromTab( | |
225 content::WebContents* source, | |
226 const content::OpenURLParams& params) { | |
227 content::WebContents* new_contents = NULL; | |
228 if (delegate_ && | |
229 delegate_->HandleOpenURLFromTab(source, params, &new_contents)) { | |
230 return new_contents; | |
231 } | |
232 return HtmlDialogTabContentsDelegate::OpenURLFromTab(source, params); | |
233 } | |
234 | |
235 void HtmlDialogWindowDelegateBridge::AddNewContents( | |
236 content::WebContents* source, | |
237 content::WebContents* new_contents, | |
238 WindowOpenDisposition disposition, | |
239 const gfx::Rect& initial_pos, | |
240 bool user_gesture) { | |
241 if (delegate_ && delegate_->HandleAddNewContents( | |
242 source, new_contents, disposition, initial_pos, user_gesture)) { | |
243 return; | |
244 } | |
245 HtmlDialogTabContentsDelegate::AddNewContents( | |
246 source, new_contents, disposition, initial_pos, user_gesture); | |
247 } | |
248 | |
249 void HtmlDialogWindowDelegateBridge::LoadingStateChanged( | |
250 content::WebContents* source) { | |
251 if (delegate_) | |
252 delegate_->OnLoadingStateChanged(source); | |
253 } | |
254 | |
255 void HtmlDialogWindowDelegateBridge::MoveContents(WebContents* source, | |
256 const gfx::Rect& pos) { | |
257 // TODO(akalin): Actually set the window bounds. | |
258 } | |
259 | |
260 // A simplified version of BrowserWindowCocoa::HandleKeyboardEvent(). | |
261 // We don't handle global keyboard shortcuts here, but that's fine since | |
262 // they're all browser-specific. (This may change in the future.) | |
263 void HtmlDialogWindowDelegateBridge::HandleKeyboardEvent( | |
264 const NativeWebKeyboardEvent& event) { | |
265 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) | |
266 return; | |
267 | |
268 // Close ourselves if the user hits Esc or Command-. . The normal | |
269 // way to do this is to implement (void)cancel:(int)sender, but | |
270 // since we handle keyboard events ourselves we can't do that. | |
271 // | |
272 // According to experiments, hitting Esc works regardless of the | |
273 // presence of other modifiers (as long as it's not an app-level | |
274 // shortcut, e.g. Commmand-Esc for Front Row) but no other modifiers | |
275 // can be present for Command-. to work. | |
276 // | |
277 // TODO(thakis): It would be nice to get cancel: to work somehow. | |
278 // Bug: http://code.google.com/p/chromium/issues/detail?id=32828 . | |
279 if (event.type == NativeWebKeyboardEvent::RawKeyDown && | |
280 ((event.windowsKeyCode == ui::VKEY_ESCAPE) || | |
281 (event.windowsKeyCode == ui::VKEY_OEM_PERIOD && | |
282 event.modifiers == NativeWebKeyboardEvent::MetaKey))) { | |
283 [controller_ close]; | |
284 return; | |
285 } | |
286 | |
287 ChromeEventProcessingWindow* event_window = | |
288 static_cast<ChromeEventProcessingWindow*>([controller_ window]); | |
289 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); | |
290 [event_window redispatchKeyEvent:event.os_event]; | |
291 } | |
292 | |
293 @implementation HtmlDialogWindowController (InternalAPI) | |
294 | |
295 // This gets called whenever a chrome-specific keyboard shortcut is performed | |
296 // in the HTML dialog window. We simply swallow all those events. | |
297 - (void)executeCommand:(int)command {} | |
298 | |
299 @end | |
300 | |
301 @implementation HtmlDialogWindowController | |
302 | |
303 // NOTE(akalin): We'll probably have to add the parentWindow parameter back | |
304 // in once we implement modal dialogs. | |
305 | |
306 + (NSWindow*)showHtmlDialog:(HtmlDialogUIDelegate*)delegate | |
307 profile:(Profile*)profile | |
308 browser:(Browser*)browser { | |
309 HtmlDialogWindowController* htmlDialogWindowController = | |
310 [[HtmlDialogWindowController alloc] initWithDelegate:delegate | |
311 profile:profile | |
312 browser:browser]; | |
313 [htmlDialogWindowController loadDialogContents]; | |
314 [htmlDialogWindowController showWindow:nil]; | |
315 return [htmlDialogWindowController window]; | |
316 } | |
317 | |
318 - (id)initWithDelegate:(HtmlDialogUIDelegate*)delegate | |
319 profile:(Profile*)profile | |
320 browser:(Browser*)browser { | |
321 DCHECK(delegate); | |
322 DCHECK(profile); | |
323 | |
324 gfx::Size dialogSize; | |
325 delegate->GetDialogSize(&dialogSize); | |
326 NSRect dialogRect = NSMakeRect(0, 0, dialogSize.width(), dialogSize.height()); | |
327 NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | | |
328 NSResizableWindowMask; | |
329 scoped_nsobject<ChromeEventProcessingWindow> window( | |
330 [[ChromeEventProcessingWindow alloc] | |
331 initWithContentRect:dialogRect | |
332 styleMask:style | |
333 backing:NSBackingStoreBuffered | |
334 defer:YES]); | |
335 if (!window.get()) { | |
336 return nil; | |
337 } | |
338 self = [super initWithWindow:window]; | |
339 if (!self) { | |
340 return nil; | |
341 } | |
342 [window setWindowController:self]; | |
343 [window setDelegate:self]; | |
344 [window setTitle:base::SysUTF16ToNSString(delegate->GetDialogTitle())]; | |
345 [window setMinSize:dialogRect.size]; | |
346 [window center]; | |
347 delegate_.reset( | |
348 new HtmlDialogWindowDelegateBridge(self, profile, browser, delegate)); | |
349 return self; | |
350 } | |
351 | |
352 - (void)loadDialogContents { | |
353 contentsWrapper_.reset(new TabContentsWrapper(WebContents::Create( | |
354 delegate_->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL))); | |
355 [[self window] | |
356 setContentView:contentsWrapper_->web_contents()->GetNativeView()]; | |
357 contentsWrapper_->web_contents()->SetDelegate(delegate_.get()); | |
358 | |
359 // This must be done before loading the page; see the comments in | |
360 // HtmlDialogUI. | |
361 HtmlDialogUI::GetPropertyAccessor().SetProperty( | |
362 contentsWrapper_->web_contents()->GetPropertyBag(), delegate_.get()); | |
363 | |
364 contentsWrapper_->web_contents()->GetController().LoadURL( | |
365 delegate_->GetDialogContentURL(), | |
366 content::Referrer(), | |
367 content::PAGE_TRANSITION_START_PAGE, | |
368 std::string()); | |
369 | |
370 // TODO(akalin): add accelerator for ESC to close the dialog box. | |
371 // | |
372 // TODO(akalin): Figure out why implementing (void)cancel:(id)sender | |
373 // to do the above doesn't work. | |
374 } | |
375 | |
376 - (void)windowWillClose:(NSNotification*)notification { | |
377 delegate_->WindowControllerClosed(); | |
378 [self autorelease]; | |
379 } | |
380 | |
381 @end | |
OLD | NEW |