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
|
// Copyright 2014 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 "athena/content/web_activity.h"
#include "athena/activity/public/activity_factory.h"
#include "athena/activity/public/activity_manager.h"
#include "athena/input/public/accelerator_manager.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "ui/aura/window.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/widget/widget.h"
namespace athena {
namespace {
class WebActivityController : public AcceleratorHandler {
public:
enum Command {
CMD_BACK,
CMD_FORWARD,
CMD_RELOAD,
CMD_RELOAD_IGNORE_CACHE,
CMD_CLOSE,
};
explicit WebActivityController(views::WebView* web_view)
: web_view_(web_view), reserved_accelerator_enabled_(true) {}
virtual ~WebActivityController() {}
// Installs accelerators for web activity.
void InstallAccelerators() {
accelerator_manager_ = AcceleratorManager::CreateForFocusManager(
web_view_->GetFocusManager()).Pass();
const AcceleratorData accelerator_data[] = {
{TRIGGER_ON_PRESS, ui::VKEY_R, ui::EF_CONTROL_DOWN, CMD_RELOAD,
AF_NONE},
{TRIGGER_ON_PRESS, ui::VKEY_BROWSER_REFRESH, ui::EF_NONE, CMD_RELOAD,
AF_NONE},
{TRIGGER_ON_PRESS, ui::VKEY_BROWSER_REFRESH, ui::EF_CONTROL_DOWN,
CMD_RELOAD_IGNORE_CACHE, AF_NONE},
{TRIGGER_ON_PRESS, ui::VKEY_BROWSER_FORWARD, ui::EF_NONE, CMD_FORWARD,
AF_NONE},
{TRIGGER_ON_PRESS, ui::VKEY_BROWSER_BACK, ui::EF_NONE, CMD_BACK,
AF_NONE},
{TRIGGER_ON_PRESS, ui::VKEY_W, ui::EF_CONTROL_DOWN, CMD_CLOSE, AF_NONE},
};
accelerator_manager_->RegisterAccelerators(
accelerator_data, arraysize(accelerator_data), this);
}
// Methods that are called before and after key events are consumed by the web
// contents.
// See the documentation in WebContentsDelegate: for more details.
bool PreHandleKeyboardEvent(content::WebContents* source,
const content::NativeWebKeyboardEvent& event,
bool* is_keyboard_shortcut) {
ui::Accelerator accelerator(
static_cast<ui::KeyboardCode>(event.windowsKeyCode),
content::GetModifiersFromNativeWebKeyboardEvent(event));
if (event.type == blink::WebInputEvent::KeyUp)
accelerator.set_type(ui::ET_KEY_RELEASED);
if (reserved_accelerator_enabled_ &&
accelerator_manager_->IsRegistered(accelerator, AF_RESERVED)) {
return web_view_->GetFocusManager()->ProcessAccelerator(accelerator);
}
*is_keyboard_shortcut =
accelerator_manager_->IsRegistered(accelerator, AF_NONE);
return false;
}
void HandleKeyboardEvent(content::WebContents* source,
const content::NativeWebKeyboardEvent& event) {
unhandled_keyboard_event_handler_.HandleKeyboardEvent(
event, web_view_->GetFocusManager());
}
private:
// AcceleratorHandler:
virtual bool IsCommandEnabled(int command_id) const OVERRIDE {
switch (command_id) {
case CMD_RELOAD:
case CMD_RELOAD_IGNORE_CACHE:
return true;
case CMD_BACK:
return web_view_->GetWebContents()->GetController().CanGoBack();
case CMD_FORWARD:
return web_view_->GetWebContents()->GetController().CanGoForward();
case CMD_CLOSE:
// TODO(oshima): check onbeforeunload handler.
return true;
}
return false;
}
virtual bool OnAcceleratorFired(int command_id,
const ui::Accelerator& accelerator) OVERRIDE {
switch (command_id) {
case CMD_RELOAD:
web_view_->GetWebContents()->GetController().Reload(false);
return true;
case CMD_RELOAD_IGNORE_CACHE:
web_view_->GetWebContents()->GetController().ReloadIgnoringCache(false);
return true;
case CMD_BACK:
web_view_->GetWebContents()->GetController().GoBack();
return true;
case CMD_FORWARD:
web_view_->GetWebContents()->GetController().GoForward();
return true;
case CMD_CLOSE:
web_view_->GetWidget()->Close();
return true;
}
return false;
}
views::WebView* web_view_;
bool reserved_accelerator_enabled_;
scoped_ptr<AcceleratorManager> accelerator_manager_;
views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_;
DISALLOW_COPY_AND_ASSIGN(WebActivityController);
};
const SkColor kDefaultTitleColor = SkColorSetRGB(0xf2, 0xf2, 0xf2);
const SkColor kDefaultUnavailableColor = SkColorSetRGB(0xbb, 0x77, 0x77);
} // namespace
// A web view for athena's web activity. Note that AthenaWebView will create its
// own content so that it can eject and reload it.
class AthenaWebView : public views::WebView {
public:
AthenaWebView(content::BrowserContext* context)
: views::WebView(context), controller_(new WebActivityController(this)),
fullscreen_(false) {
SetEmbedFullscreenWidgetMode(true);
// TODO(skuhne): Add content observer to detect renderer crash and set
// content status to unloaded if that happens.
}
AthenaWebView(content::WebContents* web_contents)
: views::WebView(web_contents->GetBrowserContext()),
controller_(new WebActivityController(this)) {
scoped_ptr<content::WebContents> old_contents(
SwapWebContents(scoped_ptr<content::WebContents>(web_contents)));
}
virtual ~AthenaWebView() {}
void InstallAccelerators() { controller_->InstallAccelerators(); }
void EvictContent() {
scoped_ptr<content::WebContents> old_contents(SwapWebContents(
scoped_ptr<content::WebContents>(content::WebContents::Create(
content::WebContents::CreateParams(browser_context())))));
evicted_web_contents_.reset(
content::WebContents::Create(content::WebContents::CreateParams(
old_contents->GetBrowserContext())));
evicted_web_contents_->GetController().CopyStateFrom(
old_contents->GetController());
// As soon as the new contents becomes visible, it should reload.
// TODO(skuhne): This breaks script connections with other activities.
// Even though this is the same technique as used by the TabStripModel,
// we might want to address this cleaner since we are more likely to
// run into this state. by unloading.
}
void ReloadContent() {
CHECK(evicted_web_contents_.get());
scoped_ptr<content::WebContents> replaced_contents(SwapWebContents(
evicted_web_contents_.Pass()));
}
// Check if the content got evicted.
const bool IsContentEvicted() { return !!evicted_web_contents_.get(); }
// content::WebContentsDelegate:
virtual content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) OVERRIDE {
switch(params.disposition) {
case CURRENT_TAB: {
DCHECK(source == web_contents());
content::NavigationController::LoadURLParams load_url_params(
params.url);
load_url_params.referrer = params.referrer;
load_url_params.frame_tree_node_id = params.frame_tree_node_id;
load_url_params.transition_type = params.transition;
load_url_params.extra_headers = params.extra_headers;
load_url_params.should_replace_current_entry =
params.should_replace_current_entry;
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
load_url_params.transferred_global_request_id =
params.transferred_global_request_id;
web_contents()->GetController().LoadURLWithParams(load_url_params);
return web_contents();
}
case NEW_FOREGROUND_TAB:
case NEW_BACKGROUND_TAB:
case NEW_POPUP:
case NEW_WINDOW: {
ActivityManager::Get()->AddActivity(
ActivityFactory::Get()->CreateWebActivity(browser_context(),
params.url));
break;
}
default:
break;
}
// NULL is returned if the URL wasn't opened immediately.
return NULL;
}
virtual void AddNewContents(content::WebContents* source,
content::WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture,
bool* was_blocked) OVERRIDE {
ActivityManager::Get()->AddActivity(
new WebActivity(new AthenaWebView(new_contents)));
}
virtual bool PreHandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event,
bool* is_keyboard_shortcut) OVERRIDE {
return controller_->PreHandleKeyboardEvent(
source, event, is_keyboard_shortcut);
}
virtual void HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) OVERRIDE {
controller_->HandleKeyboardEvent(source, event);
}
virtual void ToggleFullscreenModeForTab(content::WebContents* web_contents,
bool enter_fullscreen) OVERRIDE {
fullscreen_ = enter_fullscreen;
GetWidget()->SetFullscreen(fullscreen_);
}
virtual bool IsFullscreenForTabOrPending(
const content::WebContents* web_contents) const OVERRIDE {
return fullscreen_;
}
private:
scoped_ptr<WebActivityController> controller_;
// If the activity got evicted, this is the web content which holds the known
// state of the content before eviction.
scoped_ptr<content::WebContents> evicted_web_contents_;
// TODO(oshima): Find out if we should support window fullscreen.
// It may still useful when a user is in split mode.
bool fullscreen_;
DISALLOW_COPY_AND_ASSIGN(AthenaWebView);
};
WebActivity::WebActivity(content::BrowserContext* browser_context,
const GURL& url)
: browser_context_(browser_context),
url_(url),
web_view_(NULL),
title_color_(kDefaultTitleColor),
current_state_(ACTIVITY_UNLOADED) {
}
WebActivity::WebActivity(AthenaWebView* web_view)
: browser_context_(web_view->browser_context()),
url_(web_view->GetWebContents()->GetURL()),
web_view_(web_view),
current_state_(ACTIVITY_UNLOADED) {
// Transition to state ACTIVITY_INVISIBLE to perform the same setup steps
// as on new activities (namely adding a WebContentsObserver).
SetCurrentState(ACTIVITY_INVISIBLE);
}
WebActivity::~WebActivity() {
// It is not required to change the activity state to UNLOADED - unless we
// would add state observers.
}
ActivityViewModel* WebActivity::GetActivityViewModel() {
return this;
}
void WebActivity::SetCurrentState(Activity::ActivityState state) {
switch (state) {
case ACTIVITY_VISIBLE:
// Fall through (for the moment).
case ACTIVITY_INVISIBLE:
// By clearing the overview mode image we allow the content to be shown.
overview_mode_image_ = gfx::ImageSkia();
if (web_view_->IsContentEvicted()) {
DCHECK_EQ(ACTIVITY_UNLOADED, current_state_);
web_view_->ReloadContent();
}
Observe(web_view_->GetWebContents());
break;
case ACTIVITY_BACKGROUND_LOW_PRIORITY:
DCHECK(ACTIVITY_VISIBLE == current_state_ ||
ACTIVITY_INVISIBLE == current_state_);
// TODO(skuhne): Do this.
break;
case ACTIVITY_PERSISTENT:
DCHECK_EQ(ACTIVITY_BACKGROUND_LOW_PRIORITY, current_state_);
// TODO(skuhne): Do this. As soon as the new resource management is
// agreed upon - or remove otherwise.
break;
case ACTIVITY_UNLOADED:
DCHECK_NE(ACTIVITY_UNLOADED, current_state_);
Observe(NULL);
web_view_->EvictContent();
break;
}
// Remember the last requested state.
current_state_ = state;
}
Activity::ActivityState WebActivity::GetCurrentState() {
if (!web_view_ || web_view_->IsContentEvicted()) {
DCHECK_EQ(ACTIVITY_UNLOADED, current_state_);
return ACTIVITY_UNLOADED;
}
// TODO(skuhne): This should be controlled by an observer and should not
// reside here.
if (IsVisible() && current_state_ != ACTIVITY_VISIBLE)
SetCurrentState(ACTIVITY_VISIBLE);
// Note: If the activity is not visible it does not necessarily mean that it
// does not have GPU compositor resources (yet).
return current_state_;
}
bool WebActivity::IsVisible() {
return web_view_ &&
web_view_->IsDrawn() &&
current_state_ != ACTIVITY_UNLOADED &&
GetWindow() &&
GetWindow()->IsVisible();
}
Activity::ActivityMediaState WebActivity::GetMediaState() {
// TODO(skuhne): The function GetTabMediaStateForContents(WebContents),
// and the AudioStreamMonitor needs to be moved from Chrome into contents to
// make it more modular and so that we can use it from here.
return Activity::ACTIVITY_MEDIA_STATE_NONE;
}
aura::Window* WebActivity::GetWindow() {
return !web_view_ ? NULL : web_view_->GetWidget()->GetNativeWindow();
}
void WebActivity::Init() {
DCHECK(web_view_);
web_view_->InstallAccelerators();
}
SkColor WebActivity::GetRepresentativeColor() const {
// TODO(sad): Compute the color from the favicon.
return web_view_ ? title_color_ : kDefaultUnavailableColor;
}
base::string16 WebActivity::GetTitle() const {
return web_view_ ? base::UTF8ToUTF16(
web_view_->GetWebContents()->GetVisibleURL().host())
: base::string16();
}
bool WebActivity::UsesFrame() const {
return true;
}
views::View* WebActivity::GetContentsView() {
if (!web_view_) {
web_view_ = new AthenaWebView(browser_context_);
web_view_->LoadInitialURL(url_);
SetCurrentState(ACTIVITY_INVISIBLE);
// Reset the overview mode image.
overview_mode_image_ = gfx::ImageSkia();
}
return web_view_;
}
void WebActivity::CreateOverviewModeImage() {
// TODO(skuhne): Create an overview.
}
gfx::ImageSkia WebActivity::GetOverviewModeImage() {
return overview_mode_image_;
}
void WebActivity::TitleWasSet(content::NavigationEntry* entry,
bool explicit_set) {
ActivityManager::Get()->UpdateActivity(this);
}
void WebActivity::DidUpdateFaviconURL(
const std::vector<content::FaviconURL>& candidates) {
ActivityManager::Get()->UpdateActivity(this);
}
void WebActivity::DidChangeThemeColor(SkColor theme_color) {
title_color_ = theme_color;
}
} // namespace athena
|