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