forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebtest.cc
211 lines (188 loc) · 7.42 KB
/
webtest.cc
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
// Copyright 2016 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 "mash/webtest/webtest.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/timer/timer.h"
#include "mash/public/interfaces/launchable.mojom.h"
#include "services/navigation/public/interfaces/view.mojom.h"
#include "services/service_manager/public/c/main.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/cpp/interface_registry.h"
#include "services/service_manager/public/cpp/service.h"
#include "services/service_manager/public/cpp/service_context.h"
#include "services/service_manager/public/cpp/service_runner.h"
#include "services/tracing/public/cpp/provider.h"
#include "ui/aura/mus/window_port_mus.h"
#include "ui/aura/window.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/paint_throbber.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/mus/aura_init.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "url/gurl.h"
namespace views {
class AuraInit;
}
namespace mash {
namespace webtest {
namespace {
// Callback from Embed().
void EmbedCallback(bool result) {}
} // namespace
class UI : public views::WidgetDelegateView,
public navigation::mojom::ViewClient {
public:
UI(Webtest* webtest,
navigation::mojom::ViewPtr view,
navigation::mojom::ViewClientRequest request)
: webtest_(webtest),
view_(std::move(view)),
view_client_binding_(this, std::move(request)) {}
~UI() override {
webtest_->RemoveWindow(GetWidget());
}
void NavigateTo(const GURL& url) {
view_->NavigateTo(url);
}
private:
// Overridden from views::WidgetDelegate:
base::string16 GetWindowTitle() const override {
// TODO(beng): use resources.
if (current_title_.empty())
return base::ASCIIToUTF16("navigation::View client");
base::string16 format = base::ASCIIToUTF16("%s - navigation::View client");
base::ReplaceFirstSubstringAfterOffset(
&format, 0, base::ASCIIToUTF16("%s"), current_title_);
return format;
}
bool CanResize() const override { return true; }
bool CanMaximize() const override { return true; }
bool CanMinimize() const override { return true; }
// Overridden from views::View:
void Layout() override {
gfx::Rect local_bounds = GetLocalBounds();
if (content_area_) {
gfx::Point offset = local_bounds.origin();
ConvertPointToWidget(this, &offset);
int width = local_bounds.width();
int height = local_bounds.height();
content_area_->SetBounds(
gfx::Rect(offset.x(), offset.y(), width, height));
}
}
void ViewHierarchyChanged(
const views::View::ViewHierarchyChangedDetails& details) override {
if (details.is_add && GetWidget() && !content_area_) {
aura::Window* window = GetWidget()->GetNativeWindow();
content_area_ = new aura::Window(nullptr);
content_area_->Init(ui::LAYER_NOT_DRAWN);
window->AddChild(content_area_);
ui::mojom::WindowTreeClientPtr client;
view_->GetWindowTreeClient(MakeRequest(&client));
const uint32_t embed_flags = 0; // Nothing special.
aura::WindowPortMus::Get(content_area_)
->Embed(std::move(client), embed_flags, base::Bind(&EmbedCallback));
}
}
// navigation::mojom::ViewClient:
void OpenURL(navigation::mojom::OpenURLParamsPtr params) override {}
void LoadingStateChanged(bool is_loading) override {}
void NavigationStateChanged(const GURL& url,
const std::string& title,
bool can_go_back,
bool can_go_forward) override {
current_title_ = base::UTF8ToUTF16(title);
GetWidget()->UpdateWindowTitle();
}
void LoadProgressChanged(double progress) override {}
void UpdateHoverURL(const GURL& url) override {}
void ViewCreated(navigation::mojom::ViewPtr view,
navigation::mojom::ViewClientRequest request,
bool is_popup,
const gfx::Rect& initial_rect,
bool user_gesture) override {
views::Widget* window = views::Widget::CreateWindowWithContextAndBounds(
new UI(webtest_, std::move(view), std::move(request)), nullptr,
initial_rect);
window->Show();
webtest_->AddWindow(window);
}
void Close() override {
GetWidget()->Close();
}
void NavigationPending(navigation::mojom::NavigationEntryPtr entry) override {
}
void NavigationCommitted(
navigation::mojom::NavigationCommittedDetailsPtr details,
int current_index) override {}
void NavigationEntryChanged(navigation::mojom::NavigationEntryPtr entry,
int entry_index) override {}
void NavigationListPruned(bool from_front, int count) override {}
Webtest* webtest_;
aura::Window* content_area_ = nullptr;
navigation::mojom::ViewPtr view_;
mojo::Binding<navigation::mojom::ViewClient> view_client_binding_;
base::string16 current_title_;
DISALLOW_COPY_AND_ASSIGN(UI);
};
Webtest::Webtest() {}
Webtest::~Webtest() {}
void Webtest::AddWindow(views::Widget* window) {
windows_.push_back(window);
}
void Webtest::RemoveWindow(views::Widget* window) {
auto it = std::find(windows_.begin(), windows_.end(), window);
DCHECK(it != windows_.end());
windows_.erase(it);
if (windows_.empty())
base::MessageLoop::current()->QuitWhenIdle();
}
void Webtest::OnStart() {
tracing_.Initialize(context()->connector(), context()->identity().name());
aura_init_ = base::MakeUnique<views::AuraInit>(
context()->connector(), context()->identity(), "views_mus_resources.pak",
std::string(), nullptr, views::AuraInit::Mode::AURA_MUS);
}
bool Webtest::OnConnect(const service_manager::ServiceInfo& remote_info,
service_manager::InterfaceRegistry* registry) {
registry->AddInterface<mojom::Launchable>(this);
return true;
}
void Webtest::Launch(uint32_t what, mojom::LaunchMode how) {
bool reuse = how == mojom::LaunchMode::REUSE ||
how == mojom::LaunchMode::DEFAULT;
if (reuse && !windows_.empty()) {
windows_.back()->Activate();
return;
}
navigation::mojom::ViewFactoryPtr view_factory;
context()->connector()->BindInterface("navigation", &view_factory);
navigation::mojom::ViewPtr view;
navigation::mojom::ViewClientPtr view_client;
navigation::mojom::ViewClientRequest view_client_request(&view_client);
view_factory->CreateView(std::move(view_client), MakeRequest(&view));
UI* ui = new UI(this, std::move(view), std::move(view_client_request));
views::Widget* window = views::Widget::CreateWindowWithContextAndBounds(
ui, nullptr, gfx::Rect(50, 10, 600, 600));
ui->NavigateTo(GURL("http://www.theverge.com/"));
window->Show();
AddWindow(window);
}
void Webtest::Create(const service_manager::Identity& remote_identity,
mojom::LaunchableRequest request) {
bindings_.AddBinding(this, std::move(request));
}
} // namespace webtest
} // namespace mash