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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
// 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/views/ash/panel_view_aura.h"
#include "ash/wm/panel_frame_view.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/extension_tabs_module_constants.h"
#include "chrome/browser/extensions/extension_window_controller.h"
#include "chrome/browser/extensions/extension_window_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/view_type_utils.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_messages.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "googleurl/src/gurl.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_macros.h"
#include "ui/aura/window.h"
#include "ui/views/widget/widget.h"
namespace {
const int kMinWidth = 100;
const int kMinHeight = 100;
const int kDefaultWidth = 200;
const int kDefaultHeight = 300;
}
namespace keys = extension_tabs_module_constants;
namespace internal {
////////////////////////////////////////////////////////////////////////////////
// PanelHost
class PanelHost : public content::WebContentsDelegate,
public content::WebContentsObserver,
public ExtensionFunctionDispatcher::Delegate {
public:
PanelHost(PanelViewAura* panel_view, Profile* profile);
virtual ~PanelHost();
void Init(const GURL& url);
content::WebContents* web_contents() const { return web_contents_.get(); }
Profile* profile() const { return profile_; }
// ExtensionFunctionDispatcher::Delegate overrides.
virtual ExtensionWindowController* GetExtensionWindowController()
const OVERRIDE;
virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
// content::WebContentsDelegate implementation:
virtual void CloseContents(content::WebContents* source) OVERRIDE;
virtual void HandleMouseDown() OVERRIDE;
virtual void UpdatePreferredSize(content::WebContents* source,
const gfx::Size& pref_size) OVERRIDE;
virtual void AddNewContents(content::WebContents* source,
content::WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture) OVERRIDE;
// content::WebContentsObserver implementation:
virtual void RenderViewCreated(
content::RenderViewHost* render_view_host) OVERRIDE;
virtual void RenderViewReady() OVERRIDE;
virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
protected:
// Message handlers
void OnRequest(const ExtensionHostMsg_Request_Params& params);
private:
PanelViewAura* panel_view_;
Profile* profile_;
ExtensionFunctionDispatcher extension_function_dispatcher_;
scoped_ptr<content::WebContents> web_contents_;
// Site instance to be used for opening new links.
scoped_refptr<content::SiteInstance> site_instance_;
};
PanelHost::PanelHost(PanelViewAura* panel_view, Profile* profile)
: panel_view_(panel_view),
profile_(profile),
ALLOW_THIS_IN_INITIALIZER_LIST(
extension_function_dispatcher_(profile, this)) {
}
PanelHost::~PanelHost() {
}
void PanelHost::Init(const GURL& url) {
site_instance_ = content::SiteInstance::CreateForURL(profile_, url);
web_contents_.reset(content::WebContents::Create(
profile_, site_instance_.get(), MSG_ROUTING_NONE, NULL, NULL));
chrome::SetViewType(web_contents_.get(), chrome::VIEW_TYPE_PANEL);
web_contents_->SetDelegate(this);
Observe(web_contents_.get());
web_contents_->GetController().LoadURL(
url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
}
ExtensionWindowController* PanelHost::GetExtensionWindowController() const {
return panel_view_->extension_window_controller();
}
content::WebContents* PanelHost::GetAssociatedWebContents() const {
return web_contents_.get();
}
void PanelHost::CloseContents(content::WebContents* source) {
panel_view_->CloseView();
}
void PanelHost::HandleMouseDown() {
}
void PanelHost::UpdatePreferredSize(content::WebContents* source,
const gfx::Size& pref_size) {
panel_view_->SetContentPreferredSize(pref_size);
}
// This handles launching a new page from within the panel.
// TODO(stevenjb): Determine whether or not this is the desired/expected
// behavior for panels.
void PanelHost::AddNewContents(content::WebContents* source,
content::WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture) {
Browser* browser = browser::FindLastActiveWithProfile(
Profile::FromBrowserContext(new_contents->GetBrowserContext()));
if (!browser)
return;
browser->AddWebContents(new_contents, disposition, initial_pos, user_gesture);
}
void PanelHost::RenderViewCreated(content::RenderViewHost* render_view_host) {
}
void PanelHost::RenderViewReady() {
}
void PanelHost::RenderViewGone(base::TerminationStatus status) {
CloseContents(web_contents_.get());
}
bool PanelHost::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PanelHost, message)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PanelHost::OnRequest(const ExtensionHostMsg_Request_Params& params) {
extension_function_dispatcher_.Dispatch(params,
web_contents_->GetRenderViewHost());
}
////////////////////////////////////////////////////////////////////////////////
// PanelExtensionWindowController
class PanelExtensionWindowController : public ExtensionWindowController {
public:
PanelExtensionWindowController(PanelViewAura* panel_view,
PanelHost* panel_host);
// Overriden from ExtensionWindowController:
virtual int GetWindowId() const OVERRIDE;
virtual std::string GetWindowTypeText() const OVERRIDE;
virtual base::DictionaryValue* CreateWindowValue() const OVERRIDE;
virtual base::DictionaryValue* CreateWindowValueWithTabs() const OVERRIDE;
virtual bool CanClose(Reason* reason) const OVERRIDE;
virtual void SetFullscreenMode(bool is_fullscreen,
const GURL& extension_url) const OVERRIDE;
virtual bool IsVisibleToExtension(
const extensions::Extension* extension) const OVERRIDE;
private:
PanelViewAura* panel_view_;
PanelHost* panel_host_;
DISALLOW_COPY_AND_ASSIGN(PanelExtensionWindowController);
};
PanelExtensionWindowController::PanelExtensionWindowController(
PanelViewAura* panel_view,
PanelHost* panel_host)
: ExtensionWindowController(panel_view, panel_host->profile()),
panel_view_(panel_view),
panel_host_(panel_host) {
}
int PanelExtensionWindowController::GetWindowId() const {
return static_cast<int>(panel_view_->session_id().id());
}
std::string PanelExtensionWindowController::GetWindowTypeText() const {
return keys::kWindowTypeValuePanel;
}
base::DictionaryValue*
PanelExtensionWindowController::CreateWindowValue() const {
DictionaryValue* result = ExtensionWindowController::CreateWindowValue();
return result;
}
base::DictionaryValue*
PanelExtensionWindowController::CreateWindowValueWithTabs() const {
DictionaryValue* result = CreateWindowValue();
// TODO(stevenjb): Implement tab interface for Aura panels.
// Currently there is no mechanism to get a tab id without an associated
// TabContentsWrapper. We will need to either add a TabContentsWrapper for
// panels, or add another mechanism for tracking tabs. crbug.com/115532.
return result;
}
bool PanelExtensionWindowController::CanClose(Reason* reason) const {
return true;
}
void PanelExtensionWindowController::SetFullscreenMode(
bool is_fullscreen, const GURL& extension_url) const {
}
bool PanelExtensionWindowController::IsVisibleToExtension(
const extensions::Extension* extension) const {
ExtensionProcessManager* process_manager =
ExtensionSystem::Get(panel_host_->profile())->process_manager();
const extensions::Extension* panel_extension = process_manager->
GetExtensionForRenderViewHost(
panel_view_->WebContents()->GetRenderViewHost());
// Non-extension panels are visible to all extensions.
if (!panel_extension)
return !extension->is_platform_app();
// Platform apps can only see their own panels.
if (extension->is_platform_app())
return extension == panel_extension;
// Other extensions can see non-platform app panels.
return !panel_extension->is_platform_app();
}
} // namespace internal
////////////////////////////////////////////////////////////////////////////////
// PanelViewAura
PanelViewAura::PanelViewAura(const std::string& title)
: title_(title),
preferred_size_(kMinWidth, kMinHeight),
widget_(NULL) {
}
PanelViewAura::~PanelViewAura() {
}
views::Widget* PanelViewAura::Init(Profile* profile,
const GURL& url,
const gfx::Rect& bounds) {
widget_ = new views::Widget;
views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
params.delegate = this;
params.bounds = bounds;
if (params.bounds.width() == 0)
params.bounds.set_width(kDefaultWidth);
else if (params.bounds.width() < kMinWidth)
params.bounds.set_width(kMinWidth);
if (params.bounds.height() == 0)
params.bounds.set_height(kDefaultHeight);
else if (params.bounds.height() < kMinHeight)
params.bounds.set_height(kMinHeight);
widget_->Init(params);
widget_->GetNativeView()->SetName(title_);
host_.reset(new internal::PanelHost(this, profile));
host_->Init(url);
Attach(host_->web_contents()->GetNativeView());
// Add the browser to the list of windows available to the extension API.
extension_window_controller_.reset(
new internal::PanelExtensionWindowController(this, host_.get()));
// Show the window, but don't activate it by default.
widget_->ShowInactive();
return widget_;
}
content::WebContents* PanelViewAura::WebContents() {
return host_->web_contents();
}
void PanelViewAura::CloseView() {
widget_->CloseNow();
}
void PanelViewAura::SetContentPreferredSize(const gfx::Size& size) {
if (size.width() > kMinWidth)
preferred_size_.set_width(size.width());
if (size.height() > kMinHeight)
preferred_size_.set_height(size.height());
}
// views::View implementation:
gfx::Size PanelViewAura::GetPreferredSize() {
return preferred_size_;
}
// views::WidgetDelegate implementation:
bool PanelViewAura::CanResize() const {
// TODO(stevenjb): Can/should panels be able to prevent resizing?
return true;
}
string16 PanelViewAura::GetWindowTitle() const {
return UTF8ToUTF16(title_);
}
views::View* PanelViewAura::GetContentsView() {
return this;
}
views::View* PanelViewAura::GetInitiallyFocusedView() {
return this;
}
bool PanelViewAura::ShouldShowWindowTitle() const {
return true;
}
views::Widget* PanelViewAura::GetWidget() {
return View::GetWidget();
}
const views::Widget* PanelViewAura::GetWidget() const {
return View::GetWidget();
}
views::NonClientFrameView* PanelViewAura::CreateNonClientFrameView(
views::Widget* widget) {
return new ash::PanelFrameView(widget);
}
// BaseWindow implementation:
bool PanelViewAura::IsActive() const {
return GetWidget()->IsActive();
}
bool PanelViewAura::IsMaximized() const {
return GetWidget()->IsMaximized();
}
bool PanelViewAura::IsMinimized() const {
return GetWidget()->IsMinimized();
}
bool PanelViewAura::IsFullscreen() const {
return GetWidget()->IsFullscreen();
}
gfx::Rect PanelViewAura::GetRestoredBounds() const {
return GetWidget()->GetRestoredBounds();
}
gfx::Rect PanelViewAura::GetBounds() const {
return GetWidget()->GetWindowScreenBounds();
}
void PanelViewAura::Show() {
GetWidget()->Show();
}
void PanelViewAura::ShowInactive() {
GetWidget()->ShowInactive();
}
void PanelViewAura::Close() {
GetWidget()->Close();
}
void PanelViewAura::Activate() {
GetWidget()->Activate();
}
void PanelViewAura::Deactivate() {
GetWidget()->Deactivate();
}
void PanelViewAura::Maximize() {
// Maximize is not implemented for panels.
}
void PanelViewAura::Minimize() {
// TODO(stevenjb): Implement this properly.
GetWidget()->Minimize();
NOTIMPLEMENTED();
}
void PanelViewAura::Restore() {
// TODO(stevenjb): Implement this properly.
GetWidget()->Restore();
NOTIMPLEMENTED();
}
void PanelViewAura::SetBounds(const gfx::Rect& bounds) {
GetWidget()->SetBounds(bounds);
}
void PanelViewAura::SetDraggableRegion(SkRegion* region) {
NOTIMPLEMENTED();
}
void PanelViewAura::FlashFrame(bool flash) {
// TODO(stevenjb): Implement
NOTIMPLEMENTED();
}
bool PanelViewAura::IsAlwaysOnTop() const {
return true;
}
|