9ec38645ca3c27ae7ba9a9f3d76e1526fb47ad6f
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / constrained_web_dialog_delegate_mac.mm
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 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/mac/scoped_nsobject.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ui/gfx/size.h"
15 #include "ui/web_dialogs/web_dialog_delegate.h"
16 #include "ui/web_dialogs/web_dialog_ui.h"
17 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
18
19 using content::WebContents;
20 using ui::WebDialogDelegate;
21 using ui::WebDialogWebContentsDelegate;
22 using web_modal::NativeWebContentsModalDialog;
23
24 namespace {
25
26 class ConstrainedWebDialogDelegateMac
27     : public ConstrainedWebDialogDelegateBase {
28  public:
29   ConstrainedWebDialogDelegateMac(
30       content::BrowserContext* browser_context,
31       WebDialogDelegate* delegate,
32       WebDialogWebContentsDelegate* tab_delegate)
33       : ConstrainedWebDialogDelegateBase(
34             browser_context, delegate, tab_delegate) {}
35
36   // WebDialogWebContentsDelegate interface.
37   virtual void CloseContents(WebContents* source) OVERRIDE {
38     window_->CloseWebContentsModalDialog();
39   }
40
41   void set_window(ConstrainedWindowMac* window) { window_ = window; }
42   ConstrainedWindowMac* window() const { return window_; }
43
44  private:
45   // Weak, owned by ConstrainedWebDialogDelegateViewMac.
46   ConstrainedWindowMac* window_;
47
48   DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac);
49 };
50
51 }  // namespace
52
53 class ConstrainedWebDialogDelegateViewMac :
54     public ConstrainedWindowMacDelegate,
55     public ConstrainedWebDialogDelegate {
56
57  public:
58   ConstrainedWebDialogDelegateViewMac(
59       content::BrowserContext* browser_context,
60       WebDialogDelegate* delegate,
61       WebDialogWebContentsDelegate* tab_delegate,
62       content::WebContents* web_contents);
63   virtual ~ConstrainedWebDialogDelegateViewMac() {}
64
65   // ConstrainedWebDialogDelegate interface
66   virtual const WebDialogDelegate*
67       GetWebDialogDelegate() const OVERRIDE {
68     return impl_->GetWebDialogDelegate();
69   }
70   virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE {
71     return impl_->GetWebDialogDelegate();
72   }
73   virtual void OnDialogCloseFromWebUI() OVERRIDE {
74     return impl_->OnDialogCloseFromWebUI();
75   }
76   virtual void ReleaseWebContentsOnDialogClose() OVERRIDE {
77     return impl_->ReleaseWebContentsOnDialogClose();
78   }
79   virtual NativeWebContentsModalDialog GetNativeDialog() OVERRIDE {
80     return constrained_window_->GetNativeDialog();
81   }
82   virtual WebContents* GetWebContents() OVERRIDE {
83     return impl_->GetWebContents();
84   }
85
86   // ConstrainedWindowMacDelegate interface
87   virtual void OnConstrainedWindowClosed(
88       ConstrainedWindowMac* window) OVERRIDE {
89     if (!impl_->closed_via_webui())
90       GetWebDialogDelegate()->OnDialogClosed("");
91     delete this;
92   }
93
94  private:
95   scoped_ptr<ConstrainedWebDialogDelegateMac> impl_;
96   scoped_ptr<ConstrainedWindowMac> constrained_window_;
97   base::scoped_nsobject<NSWindow> window_;
98
99   DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac);
100 };
101
102 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac(
103     content::BrowserContext* browser_context,
104     WebDialogDelegate* delegate,
105     WebDialogWebContentsDelegate* tab_delegate,
106     content::WebContents* web_contents)
107     : impl_(new ConstrainedWebDialogDelegateMac(browser_context,
108                                                 delegate,
109                                                 tab_delegate)) {
110   // Create a window to hold web_contents in the constrained sheet:
111   gfx::Size size;
112   delegate->GetDialogSize(&size);
113   NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
114
115   window_.reset(
116       [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]);
117   [GetWebContents()->GetNativeView() setFrame:frame];
118   [[window_ contentView] addSubview:GetWebContents()->GetNativeView()];
119
120   base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
121       [[CustomConstrainedWindowSheet alloc] initWithCustomWindow:window_]);
122   constrained_window_.reset(new ConstrainedWindowMac(
123       this, web_contents, sheet));
124
125   impl_->set_window(constrained_window_.get());
126 }
127
128 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog(
129         content::BrowserContext* browser_context,
130         WebDialogDelegate* delegate,
131         WebDialogWebContentsDelegate* tab_delegate,
132         content::WebContents* web_contents) {
133   // Deleted when the dialog closes.
134   ConstrainedWebDialogDelegateViewMac* constrained_delegate =
135       new ConstrainedWebDialogDelegateViewMac(
136           browser_context, delegate, tab_delegate, web_contents);
137   return constrained_delegate;
138 }