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