Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / web_dialog_window_controller.mm
blob87f40074d3795e6a190a57d177eb5fee8596d381
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.
5 #import "chrome/browser/ui/cocoa/web_dialog_window_controller.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/browser_dialogs.h"
11 #import "chrome/browser/ui/cocoa/browser_command_executor.h"
12 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
13 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
14 #include "content/public/browser/native_web_keyboard_event.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_ui_message_handler.h"
17 #include "ui/events/keycodes/keyboard_codes.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "ui/web_dialogs/web_dialog_delegate.h"
20 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
22 using content::NativeWebKeyboardEvent;
23 using content::WebContents;
24 using content::WebUIMessageHandler;
25 using ui::WebDialogDelegate;
26 using ui::WebDialogUI;
27 using ui::WebDialogWebContentsDelegate;
29 // Thin bridge that routes notifications to
30 // WebDialogWindowController's member variables.
31 class WebDialogWindowDelegateBridge
32     : public WebDialogDelegate,
33       public WebDialogWebContentsDelegate {
34 public:
35   // All parameters must be non-NULL/non-nil.
36   WebDialogWindowDelegateBridge(WebDialogWindowController* controller,
37                                 content::BrowserContext* context,
38                                 WebDialogDelegate* delegate);
40   ~WebDialogWindowDelegateBridge() override;
42   // Called when the window is directly closed, e.g. from the close
43   // button or from an accelerator.
44   void WindowControllerClosed();
46   // WebDialogDelegate declarations.
47   ui::ModalType GetDialogModalType() const override;
48   base::string16 GetDialogTitle() const override;
49   GURL GetDialogContentURL() const override;
50   void GetWebUIMessageHandlers(
51       std::vector<WebUIMessageHandler*>* handlers) const override;
52   void GetDialogSize(gfx::Size* size) const override;
53   void GetMinimumDialogSize(gfx::Size* size) const override;
54   std::string GetDialogArgs() const override;
55   void OnDialogClosed(const std::string& json_retval) override;
56   void OnCloseContents(WebContents* source, bool* out_close_dialog) override;
57   bool ShouldShowDialogTitle() const override { return true; }
59   // WebDialogWebContentsDelegate declarations.
60   void MoveContents(WebContents* source, const gfx::Rect& pos) override;
61   void HandleKeyboardEvent(content::WebContents* source,
62                            const NativeWebKeyboardEvent& event) override;
63   void CloseContents(WebContents* source) override;
64   content::WebContents* OpenURLFromTab(
65       content::WebContents* source,
66       const content::OpenURLParams& params) override;
67   void AddNewContents(content::WebContents* source,
68                       content::WebContents* new_contents,
69                       WindowOpenDisposition disposition,
70                       const gfx::Rect& initial_rect,
71                       bool user_gesture,
72                       bool* was_blocked) override;
73   void LoadingStateChanged(content::WebContents* source,
74                            bool to_different_document) override;
76 private:
77   WebDialogWindowController* controller_;  // weak
78   WebDialogDelegate* delegate_;  // weak, owned by controller_
80   // Calls delegate_'s OnDialogClosed() exactly once, nulling it out afterwards
81   // so that no other WebDialogDelegate calls are sent to it. Returns whether or
82   // not the OnDialogClosed() was actually called on the delegate.
83   bool DelegateOnDialogClosed(const std::string& json_retval);
85   DISALLOW_COPY_AND_ASSIGN(WebDialogWindowDelegateBridge);
88 // ChromeEventProcessingWindow expects its controller to implement the
89 // BrowserCommandExecutor protocol.
90 @interface WebDialogWindowController (InternalAPI) <BrowserCommandExecutor>
92 // BrowserCommandExecutor methods.
93 - (void)executeCommand:(int)command;
95 @end
97 namespace chrome {
99 gfx::NativeWindow ShowWebDialog(gfx::NativeView parent,
100                                 content::BrowserContext* context,
101                                 WebDialogDelegate* delegate) {
102   return [WebDialogWindowController showWebDialog:delegate
103                                           context:context];
106 }  // namespace chrome
108 WebDialogWindowDelegateBridge::WebDialogWindowDelegateBridge(
109     WebDialogWindowController* controller,
110     content::BrowserContext* context,
111     WebDialogDelegate* delegate)
112     : WebDialogWebContentsDelegate(context, new ChromeWebContentsHandler),
113       controller_(controller),
114       delegate_(delegate) {
115   DCHECK(controller_);
116   DCHECK(delegate_);
119 WebDialogWindowDelegateBridge::~WebDialogWindowDelegateBridge() {}
121 void WebDialogWindowDelegateBridge::WindowControllerClosed() {
122   Detach();
123   controller_ = nil;
124   DelegateOnDialogClosed("");
127 bool WebDialogWindowDelegateBridge::DelegateOnDialogClosed(
128     const std::string& json_retval) {
129   if (delegate_) {
130     WebDialogDelegate* real_delegate = delegate_;
131     delegate_ = NULL;
132     real_delegate->OnDialogClosed(json_retval);
133     return true;
134   }
135   return false;
138 // WebDialogDelegate definitions.
140 // All of these functions check for NULL first since delegate_ is set
141 // to NULL when the window is closed.
143 ui::ModalType WebDialogWindowDelegateBridge::GetDialogModalType() const {
144   // TODO(akalin): Support modal dialog boxes.
145   if (delegate_ && delegate_->GetDialogModalType() != ui::MODAL_TYPE_NONE) {
146     LOG(WARNING) << "Modal Web dialogs are not supported yet";
147   }
148   return ui::MODAL_TYPE_NONE;
151 base::string16 WebDialogWindowDelegateBridge::GetDialogTitle() const {
152   return delegate_ ? delegate_->GetDialogTitle() : base::string16();
155 GURL WebDialogWindowDelegateBridge::GetDialogContentURL() const {
156   return delegate_ ? delegate_->GetDialogContentURL() : GURL();
159 void WebDialogWindowDelegateBridge::GetWebUIMessageHandlers(
160     std::vector<WebUIMessageHandler*>* handlers) const {
161   if (delegate_) {
162     delegate_->GetWebUIMessageHandlers(handlers);
163   } else {
164     // TODO(akalin): Add this clause in the windows version.  Also
165     // make sure that everything expects handlers to be non-NULL and
166     // document it.
167     handlers->clear();
168   }
171 void WebDialogWindowDelegateBridge::GetDialogSize(gfx::Size* size) const {
172   if (delegate_)
173     delegate_->GetDialogSize(size);
174   else
175     *size = gfx::Size();
178 void WebDialogWindowDelegateBridge::GetMinimumDialogSize(
179     gfx::Size* size) const {
180   if (delegate_)
181     delegate_->GetMinimumDialogSize(size);
182   else
183     *size = gfx::Size();
186 std::string WebDialogWindowDelegateBridge::GetDialogArgs() const {
187   return delegate_ ? delegate_->GetDialogArgs() : "";
190 void WebDialogWindowDelegateBridge::OnDialogClosed(
191     const std::string& json_retval) {
192   Detach();
193   // [controller_ close] should be called at most once, too.
194   if (DelegateOnDialogClosed(json_retval)) {
195     [controller_ close];
196   }
197   controller_ = nil;
200 void WebDialogWindowDelegateBridge::OnCloseContents(WebContents* source,
201                                                     bool* out_close_dialog) {
202   *out_close_dialog = true;
205 void WebDialogWindowDelegateBridge::CloseContents(WebContents* source) {
206   bool close_dialog = false;
207   OnCloseContents(source, &close_dialog);
208   if (close_dialog)
209     OnDialogClosed(std::string());
212 content::WebContents* WebDialogWindowDelegateBridge::OpenURLFromTab(
213     content::WebContents* source,
214     const content::OpenURLParams& params) {
215   content::WebContents* new_contents = NULL;
216   if (delegate_ &&
217       delegate_->HandleOpenURLFromTab(source, params, &new_contents)) {
218     return new_contents;
219   }
220   return WebDialogWebContentsDelegate::OpenURLFromTab(source, params);
223 void WebDialogWindowDelegateBridge::AddNewContents(
224     content::WebContents* source,
225     content::WebContents* new_contents,
226     WindowOpenDisposition disposition,
227     const gfx::Rect& initial_rect,
228     bool user_gesture,
229     bool* was_blocked) {
230   if (delegate_ && delegate_->HandleAddNewContents(
231           source, new_contents, disposition, initial_rect, user_gesture)) {
232     return;
233   }
234   WebDialogWebContentsDelegate::AddNewContents(
235       source, new_contents, disposition, initial_rect, user_gesture,
236       was_blocked);
239 void WebDialogWindowDelegateBridge::LoadingStateChanged(
240     content::WebContents* source, bool to_different_document) {
241   if (delegate_)
242     delegate_->OnLoadingStateChanged(source);
245 void WebDialogWindowDelegateBridge::MoveContents(WebContents* source,
246                                                  const gfx::Rect& pos) {
247   // TODO(akalin): Actually set the window bounds.
250 // A simplified version of BrowserWindowCocoa::HandleKeyboardEvent().
251 // We don't handle global keyboard shortcuts here, but that's fine since
252 // they're all browser-specific. (This may change in the future.)
253 void WebDialogWindowDelegateBridge::HandleKeyboardEvent(
254     content::WebContents* source,
255     const NativeWebKeyboardEvent& event) {
256   if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char)
257     return;
259   // Close ourselves if the user hits Esc or Command-. .  The normal
260   // way to do this is to implement (void)cancel:(int)sender, but
261   // since we handle keyboard events ourselves we can't do that.
262   //
263   // According to experiments, hitting Esc works regardless of the
264   // presence of other modifiers (as long as it's not an app-level
265   // shortcut, e.g. Commmand-Esc for Front Row) but no other modifiers
266   // can be present for Command-. to work.
267   //
268   // TODO(thakis): It would be nice to get cancel: to work somehow.
269   // Bug: http://code.google.com/p/chromium/issues/detail?id=32828 .
270   if (event.type == NativeWebKeyboardEvent::RawKeyDown &&
271       ((event.windowsKeyCode == ui::VKEY_ESCAPE) ||
272        (event.windowsKeyCode == ui::VKEY_OEM_PERIOD &&
273         event.modifiers == NativeWebKeyboardEvent::MetaKey))) {
274     [controller_ close];
275     return;
276   }
278   ChromeEventProcessingWindow* event_window =
279       static_cast<ChromeEventProcessingWindow*>([controller_ window]);
280   DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]);
281   [event_window redispatchKeyEvent:event.os_event];
284 @implementation WebDialogWindowController (InternalAPI)
286 // This gets called whenever a chrome-specific keyboard shortcut is performed
287 // in the Web dialog window.  We simply swallow all those events.
288 - (void)executeCommand:(int)command {}
290 @end
292 @implementation WebDialogWindowController
294 // NOTE(akalin): We'll probably have to add the parentWindow parameter back
295 // in once we implement modal dialogs.
297 + (NSWindow*)showWebDialog:(WebDialogDelegate*)delegate
298                    context:(content::BrowserContext*)context {
299   WebDialogWindowController* webDialogWindowController =
300     [[WebDialogWindowController alloc] initWithDelegate:delegate
301                                                 context:context];
302   [webDialogWindowController loadDialogContents];
303   [webDialogWindowController showWindow:nil];
304   return [webDialogWindowController window];
307 - (id)initWithDelegate:(WebDialogDelegate*)delegate
308                context:(content::BrowserContext*)context {
309   DCHECK(delegate);
310   DCHECK(context);
312   gfx::Size dialogSize;
313   delegate->GetDialogSize(&dialogSize);
314   NSRect dialogRect = NSMakeRect(0, 0, dialogSize.width(), dialogSize.height());
315   NSUInteger style = NSTitledWindowMask | NSClosableWindowMask |
316       NSResizableWindowMask;
317   base::scoped_nsobject<ChromeEventProcessingWindow> window(
318       [[ChromeEventProcessingWindow alloc]
319           initWithContentRect:dialogRect
320                     styleMask:style
321                       backing:NSBackingStoreBuffered
322                         defer:YES]);
323   if (!window.get()) {
324     return nil;
325   }
326   self = [super initWithWindow:window];
327   if (!self) {
328     return nil;
329   }
330   [window setWindowController:self];
331   [window setDelegate:self];
332   [window setTitle:base::SysUTF16ToNSString(delegate->GetDialogTitle())];
333   [window setMinSize:dialogRect.size];
334   [window center];
335   delegate_.reset(
336       new WebDialogWindowDelegateBridge(self, context, delegate));
337   return self;
340 - (void)loadDialogContents {
341   webContents_.reset(WebContents::Create(
342       WebContents::CreateParams(delegate_->browser_context())));
343   [[self window] setContentView:webContents_->GetNativeView()];
344   webContents_->SetDelegate(delegate_.get());
346   // This must be done before loading the page; see the comments in
347   // WebDialogUI.
348   WebDialogUI::SetDelegate(webContents_.get(), delegate_.get());
350   webContents_->GetController().LoadURL(
351       delegate_->GetDialogContentURL(),
352       content::Referrer(),
353       ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
354       std::string());
356   // TODO(akalin): add accelerator for ESC to close the dialog box.
357   //
358   // TODO(akalin): Figure out why implementing (void)cancel:(id)sender
359   // to do the above doesn't work.
362 - (void)windowWillClose:(NSNotification*)notification {
363   delegate_->WindowControllerClosed();
364   [self autorelease];
367 @end