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
|
// 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 "mojo/services/kiosk_wm/kiosk_wm.h"
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "components/window_manager/basic_focus_rules.h"
#include "mojo/services/kiosk_wm/merged_service_provider.h"
namespace kiosk_wm {
KioskWM::KioskWM()
: window_manager_app_(new window_manager::WindowManagerApp(this, this)),
root_(nullptr),
content_(nullptr),
navigator_host_(this),
weak_factory_(this) {
exposed_services_impl_.AddService(this);
}
KioskWM::~KioskWM() {
}
base::WeakPtr<KioskWM> KioskWM::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void KioskWM::Initialize(mojo::ApplicationImpl* app) {
window_manager_app_->Initialize(app);
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
base::CommandLine::StringVector args = command_line->GetArgs();
if (args.empty()) {
default_url_ = "http://www.google.com/";
} else {
#if defined(OS_WIN)
default_url_ = base::WideToUTF8(args[0]);
#else
default_url_ = args[0];
#endif
}
}
bool KioskWM::ConfigureIncomingConnection(
mojo::ApplicationConnection* connection) {
window_manager_app_->ConfigureIncomingConnection(connection);
return true;
}
bool KioskWM::ConfigureOutgoingConnection(
mojo::ApplicationConnection* connection) {
window_manager_app_->ConfigureOutgoingConnection(connection);
return true;
}
void KioskWM::OnEmbed(
mojo::View* root,
mojo::InterfaceRequest<mojo::ServiceProvider> services,
mojo::ServiceProviderPtr exposed_services) {
// KioskWM does not support being embedded more than once.
CHECK(!root_);
root_ = root;
root_->AddObserver(this);
#if defined(OS_ANDROID)
// Resize to match the Nexus 5 aspect ratio:
window_manager_app_->SetViewportSize(gfx::Size(320, 640));
#else
window_manager_app_->SetViewportSize(gfx::Size(1280, 800));
#endif
content_ = root->view_manager()->CreateView();
content_->SetBounds(root_->bounds());
root_->AddChild(content_);
content_->SetVisible(true);
window_manager_app_->InitFocus(
make_scoped_ptr(new window_manager::BasicFocusRules(root_)));
window_manager_app_->accelerator_manager()->Register(
ui::Accelerator(ui::VKEY_BROWSER_BACK, 0),
ui::AcceleratorManager::kNormalPriority, this);
// Now that we're ready, either load a pending url or the default url.
if (!pending_url_.empty())
Embed(pending_url_, services.Pass(), exposed_services.Pass());
else if (!default_url_.empty())
Embed(default_url_, services.Pass(), exposed_services.Pass());
}
void KioskWM::Embed(const mojo::String& url,
mojo::InterfaceRequest<mojo::ServiceProvider> services,
mojo::ServiceProviderPtr exposed_services) {
// We can get Embed calls before we've actually been
// embedded into the root view and content_ is created.
// Just save the last url, we'll embed it when we're ready.
if (!content_) {
pending_url_ = url;
return;
}
merged_service_provider_.reset(
new MergedServiceProvider(exposed_services.Pass(), this));
content_->Embed(url, services.Pass(),
merged_service_provider_->GetServiceProviderPtr().Pass());
navigator_host_.RecordNavigation(url);
}
void KioskWM::Create(mojo::ApplicationConnection* connection,
mojo::InterfaceRequest<mojo::NavigatorHost> request) {
navigator_host_.Bind(request.Pass());
}
void KioskWM::OnViewManagerDisconnected(
mojo::ViewManager* view_manager) {
root_ = nullptr;
}
void KioskWM::OnViewDestroyed(mojo::View* view) {
view->RemoveObserver(this);
}
void KioskWM::OnViewBoundsChanged(mojo::View* view,
const mojo::Rect& old_bounds,
const mojo::Rect& new_bounds) {
content_->SetBounds(new_bounds);
}
// Convenience method:
void KioskWM::ReplaceContentWithURL(const mojo::String& url) {
Embed(url, nullptr, nullptr);
}
bool KioskWM::AcceleratorPressed(const ui::Accelerator& accelerator) {
if (accelerator.key_code() != ui::VKEY_BROWSER_BACK)
return false;
navigator_host_.RequestNavigateHistory(-1);
return true;
}
bool KioskWM::CanHandleAccelerators() const {
return true;
}
} // namespace kiosk_wm
|