| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/constrained_html_ui_delegate_impl.h" | 5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_nsobject.h" | 9 #include "base/memory/scoped_nsobject.h" |
| 10 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" | 10 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 12 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 12 #include "chrome/browser/ui/webui/web_dialog_ui.h" |
| 13 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" | 13 #include "chrome/browser/ui/webui/web_dialog_web_contents_delegate.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
| 16 | 16 |
| 17 using content::WebContents; | 17 using content::WebContents; |
| 18 | 18 |
| 19 class ConstrainedHtmlDelegateMac : | 19 class ConstrainedWebDialogDelegateMac : |
| 20 public ConstrainedWindowMacDelegateCustomSheet, | 20 public ConstrainedWindowMacDelegateCustomSheet, |
| 21 public ConstrainedHtmlUIDelegate { | 21 public ConstrainedWebDialogDelegate { |
| 22 | 22 |
| 23 public: | 23 public: |
| 24 ConstrainedHtmlDelegateMac(Profile* profile, | 24 ConstrainedWebDialogDelegateMac( |
| 25 HtmlDialogUIDelegate* delegate, | 25 Profile* profile, |
| 26 HtmlDialogTabContentsDelegate* tab_delegate); | 26 WebDialogDelegate* delegate, |
| 27 virtual ~ConstrainedHtmlDelegateMac() {} | 27 WebDialogWebContentsDelegate* tab_delegate); |
| 28 virtual ~ConstrainedWebDialogDelegateMac() {} |
| 28 | 29 |
| 29 void set_window(ConstrainedWindow* window) { | 30 void set_window(ConstrainedWindow* window) { |
| 30 return impl_->set_window(window); | 31 return impl_->set_window(window); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // ConstrainedHtmlUIDelegate interface | 34 // ConstrainedWebDialogDelegate interface |
| 34 virtual const HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() const OVERRIDE { | 35 virtual const WebDialogDelegate* |
| 35 return impl_->GetHtmlDialogUIDelegate(); | 36 GetWebDialogDelegate() const OVERRIDE { |
| 37 return impl_->GetWebDialogDelegate(); |
| 36 } | 38 } |
| 37 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE { | 39 virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE { |
| 38 return impl_->GetHtmlDialogUIDelegate(); | 40 return impl_->GetWebDialogDelegate(); |
| 39 } | 41 } |
| 40 virtual void OnDialogCloseFromWebUI() OVERRIDE { | 42 virtual void OnDialogCloseFromWebUI() OVERRIDE { |
| 41 return impl_->OnDialogCloseFromWebUI(); | 43 return impl_->OnDialogCloseFromWebUI(); |
| 42 } | 44 } |
| 43 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE { | 45 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE { |
| 44 return impl_->ReleaseTabContentsOnDialogClose(); | 46 return impl_->ReleaseTabContentsOnDialogClose(); |
| 45 } | 47 } |
| 46 virtual ConstrainedWindow* window() OVERRIDE { | 48 virtual ConstrainedWindow* window() OVERRIDE { |
| 47 return impl_->window(); | 49 return impl_->window(); |
| 48 } | 50 } |
| 49 virtual TabContentsWrapper* tab() OVERRIDE { | 51 virtual TabContentsWrapper* tab() OVERRIDE { |
| 50 return impl_->tab(); | 52 return impl_->tab(); |
| 51 } | 53 } |
| 52 | 54 |
| 53 // ConstrainedWindowMacDelegateCustomSheet interface | 55 // ConstrainedWindowMacDelegateCustomSheet interface |
| 54 virtual void DeleteDelegate() OVERRIDE { | 56 virtual void DeleteDelegate() OVERRIDE { |
| 55 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to | 57 // From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to |
| 56 // your delegate in this method." | 58 // your delegate in this method." |
| 57 if (is_sheet_open()) | 59 if (is_sheet_open()) |
| 58 [NSApp endSheet:sheet()]; | 60 [NSApp endSheet:sheet()]; |
| 59 if (!impl_->closed_via_webui()) | 61 if (!impl_->closed_via_webui()) |
| 60 GetHtmlDialogUIDelegate()->OnDialogClosed(""); | 62 GetWebDialogDelegate()->OnDialogClosed(""); |
| 61 delete this; | 63 delete this; |
| 62 } | 64 } |
| 63 | 65 |
| 64 private: | 66 private: |
| 65 scoped_ptr<ConstrainedHtmlUIDelegateImpl> impl_; | 67 scoped_ptr<ConstrainedWebDialogDelegateBase> impl_; |
| 66 | 68 |
| 67 DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac); | 69 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac); |
| 68 }; | 70 }; |
| 69 | 71 |
| 70 // The delegate used to forward events from the sheet to the constrained | 72 // The delegate used to forward events from the sheet to the constrained |
| 71 // window delegate. This bridge needs to be passed into the customsheet | 73 // window delegate. This bridge needs to be passed into the customsheet |
| 72 // to allow the HtmlDialog to know when the sheet closes. | 74 // to allow the WebDialog to know when the sheet closes. |
| 73 @interface ConstrainedHtmlDialogSheetCocoa : NSObject { | 75 @interface ConstrainedWebDialogSheetCocoa : NSObject { |
| 74 ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak | 76 ConstrainedWebDialogDelegateMac* constrainedWebDelegate_; // weak |
| 75 } | 77 } |
| 76 - (id)initWithConstrainedHtmlDelegateMac: | 78 - (id)initWithConstrainedWebDialogDelegateMac: |
| 77 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac; | 79 (ConstrainedWebDialogDelegateMac*)ConstrainedWebDialogDelegateMac; |
| 78 - (void)sheetDidEnd:(NSWindow*)sheet | 80 - (void)sheetDidEnd:(NSWindow*)sheet |
| 79 returnCode:(int)returnCode | 81 returnCode:(int)returnCode |
| 80 contextInfo:(void*)contextInfo; | 82 contextInfo:(void*)contextInfo; |
| 81 @end | 83 @end |
| 82 | 84 |
| 83 ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac( | 85 ConstrainedWebDialogDelegateMac::ConstrainedWebDialogDelegateMac( |
| 84 Profile* profile, | 86 Profile* profile, |
| 85 HtmlDialogUIDelegate* delegate, | 87 WebDialogDelegate* delegate, |
| 86 HtmlDialogTabContentsDelegate* tab_delegate) | 88 WebDialogWebContentsDelegate* tab_delegate) |
| 87 : impl_(new ConstrainedHtmlUIDelegateImpl(profile, delegate, tab_delegate)) { | 89 : impl_(new ConstrainedWebDialogDelegateBase(profile, |
| 90 delegate, |
| 91 tab_delegate)) { |
| 88 // Create NSWindow to hold web_contents in the constrained sheet: | 92 // Create NSWindow to hold web_contents in the constrained sheet: |
| 89 gfx::Size size; | 93 gfx::Size size; |
| 90 delegate->GetDialogSize(&size); | 94 delegate->GetDialogSize(&size); |
| 91 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); | 95 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); |
| 92 | 96 |
| 93 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when | 97 // |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when |
| 94 // the sheet is initialized. | 98 // the sheet is initialized. |
| 95 scoped_nsobject<NSWindow> window; | 99 scoped_nsobject<NSWindow> window; |
| 96 window.reset( | 100 window.reset( |
| 97 [[NSWindow alloc] initWithContentRect:frame | 101 [[NSWindow alloc] initWithContentRect:frame |
| 98 styleMask:NSTitledWindowMask | 102 styleMask:NSTitledWindowMask |
| 99 backing:NSBackingStoreBuffered | 103 backing:NSBackingStoreBuffered |
| 100 defer:YES]); | 104 defer:YES]); |
| 101 | 105 |
| 102 [window.get() setContentView:tab()->web_contents()->GetNativeView()]; | 106 [window.get() setContentView:tab()->web_contents()->GetNativeView()]; |
| 103 | 107 |
| 104 // Set the custom sheet to point to the new window. | 108 // Set the custom sheet to point to the new window. |
| 105 ConstrainedWindowMacDelegateCustomSheet::init( | 109 ConstrainedWindowMacDelegateCustomSheet::init( |
| 106 window.get(), | 110 window.get(), |
| 107 [[[ConstrainedHtmlDialogSheetCocoa alloc] | 111 [[[ConstrainedWebDialogSheetCocoa alloc] |
| 108 initWithConstrainedHtmlDelegateMac:this] autorelease], | 112 initWithConstrainedWebDialogDelegateMac:this] autorelease], |
| 109 @selector(sheetDidEnd:returnCode:contextInfo:)); | 113 @selector(sheetDidEnd:returnCode:contextInfo:)); |
| 110 } | 114 } |
| 111 | 115 |
| 112 // static | 116 // static |
| 113 ConstrainedHtmlUIDelegate* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | 117 ConstrainedWebDialogDelegate* |
| 114 Profile* profile, | 118 ConstrainedWebDialogUI::CreateConstrainedWebDialog( |
| 115 HtmlDialogUIDelegate* delegate, | 119 Profile* profile, |
| 116 HtmlDialogTabContentsDelegate* tab_delegate, | 120 WebDialogDelegate* delegate, |
| 117 TabContentsWrapper* wrapper) { | 121 WebDialogWebContentsDelegate* tab_delegate, |
| 118 // Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs. | 122 TabContentsWrapper* wrapper) { |
| 119 ConstrainedHtmlDelegateMac* constrained_delegate = | 123 // Deleted when ConstrainedWebDialogDelegateMac::DeleteDelegate() runs. |
| 120 new ConstrainedHtmlDelegateMac(profile, delegate, tab_delegate); | 124 ConstrainedWebDialogDelegateMac* constrained_delegate = |
| 121 // Deleted when ConstrainedHtmlDelegateMac::OnDialogCloseFromWebUI() runs. | 125 new ConstrainedWebDialogDelegateMac(profile, delegate, tab_delegate); |
| 126 // Deleted when ConstrainedWebDialogDelegateMac::OnDialogCloseFromWebUI() |
| 127 // runs. |
| 122 ConstrainedWindow* constrained_window = | 128 ConstrainedWindow* constrained_window = |
| 123 new ConstrainedWindowMac(wrapper, constrained_delegate); | 129 new ConstrainedWindowMac(wrapper, constrained_delegate); |
| 124 constrained_delegate->set_window(constrained_window); | 130 constrained_delegate->set_window(constrained_window); |
| 125 return constrained_delegate; | 131 return constrained_delegate; |
| 126 } | 132 } |
| 127 | 133 |
| 128 @implementation ConstrainedHtmlDialogSheetCocoa | 134 @implementation ConstrainedWebDialogSheetCocoa |
| 129 | 135 |
| 130 - (id)initWithConstrainedHtmlDelegateMac: | 136 - (id)initWithConstrainedWebDialogDelegateMac: |
| 131 (ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac { | 137 (ConstrainedWebDialogDelegateMac*)ConstrainedWebDialogDelegateMac { |
| 132 if ((self = [super init])) | 138 if ((self = [super init])) |
| 133 constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac; | 139 constrainedWebDelegate_ = ConstrainedWebDialogDelegateMac; |
| 134 return self; | 140 return self; |
| 135 } | 141 } |
| 136 | 142 |
| 137 - (void)sheetDidEnd:(NSWindow*)sheet | 143 - (void)sheetDidEnd:(NSWindow*)sheet |
| 138 returnCode:(int)returnCode | 144 returnCode:(int)returnCode |
| 139 contextInfo:(void *)contextInfo { | 145 contextInfo:(void *)contextInfo { |
| 140 [sheet orderOut:self]; | 146 [sheet orderOut:self]; |
| 141 } | 147 } |
| 142 | 148 |
| 143 @end | 149 @end |
| OLD | NEW |