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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// Copyright (c) 2012 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/cocoa/web_intent_picker_cocoa.h"
#import <Cocoa/Cocoa.h>
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/constrained_window_mac.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h"
#include "chrome/browser/ui/intents/web_intent_inline_disposition_delegate.h"
#include "chrome/browser/ui/intents/web_intent_picker.h"
#include "chrome/browser/ui/intents/web_intent_picker_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "content/public/browser/web_contents.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/gfx/image/image.h"
using content::WebContents;
namespace {
// Since any delegates for constrained windows are tasked with deleting
// themselves, and the WebIntentPicker needs to live longer than the
// constrained window, we need this forwarding class.
class ConstrainedPickerSheetDelegate :
public ConstrainedWindowMacDelegateCustomSheet {
public:
ConstrainedPickerSheetDelegate(
WebIntentPickerCocoa* picker,
WebIntentPickerSheetController* sheet_controller);
virtual ~ConstrainedPickerSheetDelegate() {}
// ConstrainedWindowMacDelegateCustomSheet interface
virtual void DeleteDelegate() OVERRIDE;
private:
WebIntentPickerCocoa* picker_; // Weak reference to picker
DISALLOW_COPY_AND_ASSIGN(ConstrainedPickerSheetDelegate);
};
ConstrainedPickerSheetDelegate::ConstrainedPickerSheetDelegate(
WebIntentPickerCocoa* picker,
WebIntentPickerSheetController* sheet_controller)
: picker_(picker) {
init([sheet_controller window], sheet_controller,
@selector(sheetDidEnd:returnCode:contextInfo:));
}
void ConstrainedPickerSheetDelegate::DeleteDelegate() {
if (is_sheet_open())
[NSApp endSheet:sheet()];
delete this;
}
} // namespace
// static
WebIntentPicker* WebIntentPicker::Create(TabContentsWrapper* wrapper,
WebIntentPickerDelegate* delegate,
WebIntentPickerModel* model) {
return new WebIntentPickerCocoa(wrapper, delegate, model);
}
WebIntentPickerCocoa::WebIntentPickerCocoa()
: delegate_(NULL),
model_(NULL),
wrapper_(NULL),
sheet_controller_(nil),
service_invoked(false) {
}
WebIntentPickerCocoa::WebIntentPickerCocoa(TabContentsWrapper* wrapper,
WebIntentPickerDelegate* delegate,
WebIntentPickerModel* model)
: delegate_(delegate),
model_(model),
wrapper_(wrapper),
sheet_controller_(nil),
service_invoked(false) {
model_->set_observer(this);
DCHECK(delegate);
DCHECK(wrapper);
sheet_controller_ = [
[WebIntentPickerSheetController alloc] initWithPicker:this];
// Deleted when ConstrainedPickerSheetDelegate::DeleteDelegate() runs.
ConstrainedPickerSheetDelegate* constrained_delegate =
new ConstrainedPickerSheetDelegate(this, sheet_controller_);
window_ = new ConstrainedWindowMac(wrapper, constrained_delegate);
}
WebIntentPickerCocoa::~WebIntentPickerCocoa() {
if (model_ != NULL)
model_->set_observer(NULL);
}
void WebIntentPickerCocoa::OnSheetDidEnd(NSWindow* sheet) {
[sheet orderOut:sheet_controller_];
if (window_)
window_->CloseConstrainedWindow();
delegate_->OnClosing();
}
void WebIntentPickerCocoa::Close() {
DCHECK(sheet_controller_);
[sheet_controller_ closeSheet];
if (inline_disposition_tab_contents_.get())
inline_disposition_tab_contents_->web_contents()->OnCloseStarted();
}
void WebIntentPickerCocoa::SetActionString(const string16& action_string) {
[sheet_controller_ setActionString:base::SysUTF16ToNSString(action_string)];
}
void WebIntentPickerCocoa::PerformLayout() {
DCHECK(sheet_controller_);
// If the window is animating closed when this is called, the
// animation could be holding the last reference to |controller_|
// (and thus |this|). Pin it until the task is completed.
scoped_nsobject<WebIntentPickerSheetController>
keep_alive([sheet_controller_ retain]);
[sheet_controller_ performLayoutWithModel:model_];
}
void WebIntentPickerCocoa::OnModelChanged(WebIntentPickerModel* model) {
PerformLayout();
}
void WebIntentPickerCocoa::OnFaviconChanged(WebIntentPickerModel* model,
size_t index) {
// We don't handle individual icon changes - just redo the whole model.
PerformLayout();
}
void WebIntentPickerCocoa::OnExtensionIconChanged(
WebIntentPickerModel* model,
const string16& extension_id) {
// We don't handle individual icon changes - just redo the whole model.
PerformLayout();
}
void WebIntentPickerCocoa::OnInlineDisposition(WebIntentPickerModel* model,
const GURL& url) {
content::WebContents* web_contents = content::WebContents::Create(
wrapper_->profile(),
tab_util::GetSiteInstanceForNewTab(wrapper_->profile(), url),
MSG_ROUTING_NONE, NULL, NULL);
inline_disposition_tab_contents_.reset(new TabContentsWrapper(web_contents));
inline_disposition_delegate_.reset(
new WebIntentInlineDispositionDelegate(this));
web_contents->SetDelegate(inline_disposition_delegate_.get());
// Must call this immediately after WebContents creation to avoid race
// with load.
delegate_->OnInlineDispositionWebContentsCreated(web_contents);
inline_disposition_tab_contents_->web_contents()->GetController().LoadURL(
url,
content::Referrer(),
content::PAGE_TRANSITION_START_PAGE,
std::string());
[sheet_controller_ setInlineDispositionTabContents:
inline_disposition_tab_contents_.get()];
PerformLayout();
}
void WebIntentPickerCocoa::OnCancelled() {
DCHECK(delegate_);
if (!service_invoked)
delegate_->OnCancelled();
delegate_->OnClosing();
MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
void WebIntentPickerCocoa::OnServiceChosen(size_t index) {
DCHECK(delegate_);
const WebIntentPickerModel::InstalledService& installed_service =
model_->GetInstalledServiceAt(index);
service_invoked = true;
delegate_->OnServiceChosen(installed_service.url,
installed_service.disposition);
}
void WebIntentPickerCocoa::OnExtensionInstallRequested(
const std::string& extension_id) {
delegate_->OnExtensionInstallRequested(extension_id);
}
void WebIntentPickerCocoa::OnExtensionInstallSuccess(const std::string& id) {
DCHECK(sheet_controller_);
[sheet_controller_ stopThrobber];
}
void WebIntentPickerCocoa::OnExtensionInstallFailure(const std::string& id) {
// TODO(groby): What to do on failure? (See also binji for views/gtk)
DCHECK(sheet_controller_);
[sheet_controller_ stopThrobber];
}
void WebIntentPickerCocoa::OnExtensionLinkClicked(const std::string& id) {
DCHECK(delegate_);
delegate_->OnExtensionLinkClicked(id);
}
void WebIntentPickerCocoa::OnSuggestionsLinkClicked() {
DCHECK(delegate_);
delegate_->OnSuggestionsLinkClicked();
}
|