1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/constrained_html_ui.h"
#import <Cocoa/Cocoa.h>
#include "base/memory/scoped_nsobject.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cocoa/constrained_window_mac.h"
#include "chrome/browser/ui/webui/html_dialog_ui.h"
#include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "ui/gfx/size.h"
class ConstrainedHtmlDelegateMac :
public ConstrainedWindowMacDelegateCustomSheet,
public HtmlDialogTabContentsDelegate,
public ConstrainedHtmlUIDelegate {
public:
ConstrainedHtmlDelegateMac(Profile* profile,
HtmlDialogUIDelegate* delegate);
~ConstrainedHtmlDelegateMac() {}
// ConstrainedWindowMacDelegateCustomSheet -----------------------------------
virtual void DeleteDelegate() {
// From ConstrainedWindowMacDelegate: "you MUST close the sheet belonging to
// your delegate in this method."
if (is_sheet_open())
[NSApp endSheet:sheet()];
html_delegate_->OnDialogClosed("");
delete this;
}
// ConstrainedHtmlDelegate ---------------------------------------------------
virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate();
virtual void OnDialogClose();
// HtmlDialogTabContentsDelegate ---------------------------------------------
void MoveContents(TabContents* source, const gfx::Rect& pos) {}
void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
void set_window(ConstrainedWindow* window) {
constrained_window_ = window;
}
private:
TabContents tab_contents_; // Holds the HTML to be displayed in the sheet.
HtmlDialogUIDelegate* html_delegate_; // weak.
// The constrained window that owns |this|. Saved here because it needs to be
// closed in response to the WebUI OnDialogClose callback.
ConstrainedWindow* constrained_window_;
DISALLOW_COPY_AND_ASSIGN(ConstrainedHtmlDelegateMac);
};
// The delegate used to forward events from the sheet to the constrained
// window delegate. This bridge needs to be passed into the customsheet
// to allow the HtmlDialog to know when the sheet closes.
@interface ConstrainedHtmlDialogSheetCocoa : NSObject {
ConstrainedHtmlDelegateMac* constrainedHtmlDelegate_; // weak
}
- (id)initWithConstrainedHtmlDelegateMac:
(ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac;
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo;
@end
ConstrainedHtmlDelegateMac::ConstrainedHtmlDelegateMac(
Profile* profile,
HtmlDialogUIDelegate* delegate)
: HtmlDialogTabContentsDelegate(profile),
tab_contents_(profile, NULL, MSG_ROUTING_NONE, NULL, NULL),
html_delegate_(delegate),
constrained_window_(NULL) {
tab_contents_.set_delegate(this);
// Set |this| as a property on the tab contents so that the ConstrainedHtmlUI
// can get a reference to |this|.
ConstrainedHtmlUI::GetPropertyAccessor().SetProperty(
tab_contents_.property_bag(), this);
tab_contents_.controller().LoadURL(delegate->GetDialogContentURL(),
GURL(), PageTransition::START_PAGE);
// Create NSWindow to hold tab_contents in the constrained sheet:
gfx::Size size;
delegate->GetDialogSize(&size);
NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
// |window| is retained by the ConstrainedWindowMacDelegateCustomSheet when
// the sheet is initialized.
scoped_nsobject<NSWindow> window;
window.reset(
[[NSWindow alloc] initWithContentRect:frame
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:YES]);
[window.get() setContentView:tab_contents_.GetNativeView()];
// Set the custom sheet to point to the new window.
ConstrainedWindowMacDelegateCustomSheet::init(
window.get(),
[[[ConstrainedHtmlDialogSheetCocoa alloc]
initWithConstrainedHtmlDelegateMac:this] autorelease],
@selector(sheetDidEnd:returnCode:contextInfo:));
}
HtmlDialogUIDelegate* ConstrainedHtmlDelegateMac::GetHtmlDialogUIDelegate() {
return html_delegate_;
}
void ConstrainedHtmlDelegateMac::OnDialogClose() {
DCHECK(constrained_window_);
if (constrained_window_)
constrained_window_->CloseConstrainedWindow();
}
// static
ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog(
Profile* profile,
HtmlDialogUIDelegate* delegate,
TabContents* overshadowed) {
// Deleted when ConstrainedHtmlDelegateMac::DeleteDelegate() runs.
ConstrainedHtmlDelegateMac* constrained_delegate =
new ConstrainedHtmlDelegateMac(profile, delegate);
// Deleted when ConstrainedHtmlDelegateMac::OnDialogClose() runs.
ConstrainedWindow* constrained_window =
overshadowed->CreateConstrainedDialog(constrained_delegate);
constrained_delegate->set_window(constrained_window);
return constrained_window;
}
@implementation ConstrainedHtmlDialogSheetCocoa
- (id)initWithConstrainedHtmlDelegateMac:
(ConstrainedHtmlDelegateMac*)ConstrainedHtmlDelegateMac {
if ((self = [super init]))
constrainedHtmlDelegate_ = ConstrainedHtmlDelegateMac;
return self;
}
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void *)contextInfo {
[sheet orderOut:self];
}
@end
|