OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mash/webtest/webtest.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/timer/timer.h" | |
15 #include "mash/public/interfaces/launchable.mojom.h" | |
16 #include "services/navigation/public/interfaces/view.mojom.h" | |
17 #include "services/service_manager/public/c/main.h" | |
18 #include "services/service_manager/public/cpp/connector.h" | |
19 #include "services/service_manager/public/cpp/service.h" | |
20 #include "services/service_manager/public/cpp/service_context.h" | |
21 #include "services/service_manager/public/cpp/service_runner.h" | |
22 #include "ui/aura/mus/window_port_mus.h" | |
23 #include "ui/aura/window.h" | |
24 #include "ui/gfx/canvas.h" | |
25 #include "ui/gfx/paint_throbber.h" | |
26 #include "ui/native_theme/native_theme.h" | |
27 #include "ui/views/background.h" | |
28 #include "ui/views/controls/button/label_button.h" | |
29 #include "ui/views/controls/textfield/textfield.h" | |
30 #include "ui/views/controls/textfield/textfield_controller.h" | |
31 #include "ui/views/mus/aura_init.h" | |
32 #include "ui/views/widget/widget.h" | |
33 #include "ui/views/widget/widget_delegate.h" | |
34 #include "url/gurl.h" | |
35 | |
36 namespace views { | |
37 class AuraInit; | |
38 } | |
39 | |
40 namespace mash { | |
41 namespace webtest { | |
42 namespace { | |
43 | |
44 // Callback from Embed(). | |
45 void EmbedCallback(bool result) {} | |
46 | |
47 } // namespace | |
48 | |
49 class UI : public views::WidgetDelegateView, | |
50 public navigation::mojom::ViewClient { | |
51 public: | |
52 UI(Webtest* webtest, | |
53 navigation::mojom::ViewPtr view, | |
54 navigation::mojom::ViewClientRequest request) | |
55 : webtest_(webtest), | |
56 view_(std::move(view)), | |
57 view_client_binding_(this, std::move(request)) {} | |
58 ~UI() override { | |
59 webtest_->RemoveWindow(GetWidget()); | |
60 } | |
61 | |
62 void NavigateTo(const GURL& url) { | |
63 view_->NavigateTo(url); | |
64 } | |
65 | |
66 private: | |
67 // Overridden from views::WidgetDelegate: | |
68 base::string16 GetWindowTitle() const override { | |
69 // TODO(beng): use resources. | |
70 if (current_title_.empty()) | |
71 return base::ASCIIToUTF16("navigation::View client"); | |
72 base::string16 format = base::ASCIIToUTF16("%s - navigation::View client"); | |
73 base::ReplaceFirstSubstringAfterOffset( | |
74 &format, 0, base::ASCIIToUTF16("%s"), current_title_); | |
75 return format; | |
76 } | |
77 bool CanResize() const override { return true; } | |
78 bool CanMaximize() const override { return true; } | |
79 bool CanMinimize() const override { return true; } | |
80 | |
81 // Overridden from views::View: | |
82 void Layout() override { | |
83 gfx::Rect local_bounds = GetLocalBounds(); | |
84 if (content_area_) { | |
85 gfx::Point offset = local_bounds.origin(); | |
86 ConvertPointToWidget(this, &offset); | |
87 int width = local_bounds.width(); | |
88 int height = local_bounds.height(); | |
89 content_area_->SetBounds( | |
90 gfx::Rect(offset.x(), offset.y(), width, height)); | |
91 } | |
92 } | |
93 void ViewHierarchyChanged( | |
94 const views::View::ViewHierarchyChangedDetails& details) override { | |
95 if (details.is_add && GetWidget() && !content_area_) { | |
96 aura::Window* window = GetWidget()->GetNativeWindow(); | |
97 content_area_ = new aura::Window(nullptr); | |
98 content_area_->Init(ui::LAYER_NOT_DRAWN); | |
99 window->AddChild(content_area_); | |
100 | |
101 ui::mojom::WindowTreeClientPtr client; | |
102 view_->GetWindowTreeClient(MakeRequest(&client)); | |
103 const uint32_t embed_flags = 0; // Nothing special. | |
104 aura::WindowPortMus::Get(content_area_) | |
105 ->Embed(std::move(client), embed_flags, base::Bind(&EmbedCallback)); | |
106 } | |
107 } | |
108 | |
109 // navigation::mojom::ViewClient: | |
110 void OpenURL(navigation::mojom::OpenURLParamsPtr params) override {} | |
111 void LoadingStateChanged(bool is_loading) override {} | |
112 void NavigationStateChanged(const GURL& url, | |
113 const std::string& title, | |
114 bool can_go_back, | |
115 bool can_go_forward) override { | |
116 current_title_ = base::UTF8ToUTF16(title); | |
117 GetWidget()->UpdateWindowTitle(); | |
118 } | |
119 void LoadProgressChanged(double progress) override {} | |
120 void UpdateHoverURL(const GURL& url) override {} | |
121 void ViewCreated(navigation::mojom::ViewPtr view, | |
122 navigation::mojom::ViewClientRequest request, | |
123 bool is_popup, | |
124 const gfx::Rect& initial_rect, | |
125 bool user_gesture) override { | |
126 views::Widget* window = views::Widget::CreateWindowWithContextAndBounds( | |
127 new UI(webtest_, std::move(view), std::move(request)), nullptr, | |
128 initial_rect); | |
129 window->Show(); | |
130 webtest_->AddWindow(window); | |
131 } | |
132 void Close() override { | |
133 GetWidget()->Close(); | |
134 } | |
135 void NavigationPending(navigation::mojom::NavigationEntryPtr entry) override { | |
136 } | |
137 void NavigationCommitted( | |
138 navigation::mojom::NavigationCommittedDetailsPtr details, | |
139 int current_index) override {} | |
140 void NavigationEntryChanged(navigation::mojom::NavigationEntryPtr entry, | |
141 int entry_index) override {} | |
142 void NavigationListPruned(bool from_front, int count) override {} | |
143 | |
144 Webtest* webtest_; | |
145 aura::Window* content_area_ = nullptr; | |
146 navigation::mojom::ViewPtr view_; | |
147 mojo::Binding<navigation::mojom::ViewClient> view_client_binding_; | |
148 base::string16 current_title_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(UI); | |
151 }; | |
152 | |
153 Webtest::Webtest() { | |
154 registry_.AddInterface<mojom::Launchable>( | |
155 base::Bind(&Webtest::Create, base::Unretained(this))); | |
156 } | |
157 Webtest::~Webtest() {} | |
158 | |
159 void Webtest::AddWindow(views::Widget* window) { | |
160 windows_.push_back(window); | |
161 } | |
162 | |
163 void Webtest::RemoveWindow(views::Widget* window) { | |
164 auto it = std::find(windows_.begin(), windows_.end(), window); | |
165 DCHECK(it != windows_.end()); | |
166 windows_.erase(it); | |
167 if (windows_.empty()) | |
168 base::MessageLoop::current()->QuitWhenIdle(); | |
169 } | |
170 | |
171 void Webtest::OnStart() { | |
172 aura_init_ = base::MakeUnique<views::AuraInit>( | |
173 context()->connector(), context()->identity(), "views_mus_resources.pak", | |
174 std::string(), nullptr, views::AuraInit::Mode::AURA_MUS); | |
175 } | |
176 | |
177 void Webtest::OnBindInterface( | |
178 const service_manager::BindSourceInfo& source_info, | |
179 const std::string& interface_name, | |
180 mojo::ScopedMessagePipeHandle interface_pipe) { | |
181 registry_.BindInterface(source_info, interface_name, | |
182 std::move(interface_pipe)); | |
183 } | |
184 | |
185 void Webtest::Launch(uint32_t what, mojom::LaunchMode how) { | |
186 bool reuse = how == mojom::LaunchMode::REUSE || | |
187 how == mojom::LaunchMode::DEFAULT; | |
188 if (reuse && !windows_.empty()) { | |
189 windows_.back()->Activate(); | |
190 return; | |
191 } | |
192 | |
193 navigation::mojom::ViewFactoryPtr view_factory; | |
194 context()->connector()->BindInterface("navigation", &view_factory); | |
195 navigation::mojom::ViewPtr view; | |
196 navigation::mojom::ViewClientPtr view_client; | |
197 auto view_client_request = mojo::MakeRequest(&view_client); | |
198 view_factory->CreateView(std::move(view_client), MakeRequest(&view)); | |
199 UI* ui = new UI(this, std::move(view), std::move(view_client_request)); | |
200 views::Widget* window = views::Widget::CreateWindowWithContextAndBounds( | |
201 ui, nullptr, gfx::Rect(50, 10, 600, 600)); | |
202 ui->NavigateTo(GURL("http://www.theverge.com/")); | |
203 window->Show(); | |
204 AddWindow(window); | |
205 } | |
206 | |
207 void Webtest::Create(const service_manager::BindSourceInfo& source_info, | |
208 mojom::LaunchableRequest request) { | |
209 bindings_.AddBinding(this, std::move(request)); | |
210 } | |
211 | |
212 } // namespace webtest | |
213 } // namespace mash | |
OLD | NEW |