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/weak_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/timer/timer.h" |
| 14 #include "components/mus/public/cpp/window.h" |
| 15 #include "components/mus/public/cpp/window_tree_connection.h" |
| 16 #include "mash/public/interfaces/launchable.mojom.h" |
| 17 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 18 #include "mojo/public/c/system/main.h" |
| 19 #include "services/navigation/public/interfaces/view.mojom.h" |
| 20 #include "services/shell/public/cpp/application_runner.h" |
| 21 #include "services/shell/public/cpp/connector.h" |
| 22 #include "services/shell/public/cpp/shell_client.h" |
| 23 #include "services/tracing/public/cpp/tracing_impl.h" |
| 24 #include "ui/aura/mus/mus_util.h" |
| 25 #include "ui/gfx/canvas.h" |
| 26 #include "ui/gfx/paint_throbber.h" |
| 27 #include "ui/native_theme/native_theme.h" |
| 28 #include "ui/views/background.h" |
| 29 #include "ui/views/controls/button/label_button.h" |
| 30 #include "ui/views/controls/textfield/textfield.h" |
| 31 #include "ui/views/controls/textfield/textfield_controller.h" |
| 32 #include "ui/views/mus/aura_init.h" |
| 33 #include "ui/views/mus/window_manager_connection.h" |
| 34 #include "ui/views/widget/widget_delegate.h" |
| 35 #include "url/gurl.h" |
| 36 |
| 37 namespace views { |
| 38 class AuraInit; |
| 39 } |
| 40 |
| 41 namespace mash { |
| 42 namespace webtest { |
| 43 |
| 44 class UI : public views::WidgetDelegateView, |
| 45 public navigation::mojom::ViewClient { |
| 46 public: |
| 47 UI(Webtest* webtest, |
| 48 bool is_popup, |
| 49 navigation::mojom::ViewPtr view, |
| 50 navigation::mojom::ViewClientRequest request) |
| 51 : webtest_(webtest), |
| 52 is_popup_(is_popup), |
| 53 view_(std::move(view)), |
| 54 view_client_binding_(this, std::move(request)) {} |
| 55 ~UI() override { |
| 56 webtest_->RemoveWindow(GetWidget()); |
| 57 } |
| 58 |
| 59 void NavigateTo(const GURL& url) { |
| 60 view_->NavigateTo(url); |
| 61 } |
| 62 |
| 63 private: |
| 64 // Overridden from views::WidgetDelegate: |
| 65 views::View* GetContentsView() override { return this; } |
| 66 base::string16 GetWindowTitle() const override { |
| 67 // TODO(beng): use resources. |
| 68 if (current_title_.empty()) |
| 69 return base::ASCIIToUTF16("navigation::View client"); |
| 70 base::string16 format = base::ASCIIToUTF16("%s - navigation::View client"); |
| 71 base::ReplaceFirstSubstringAfterOffset( |
| 72 &format, 0, base::ASCIIToUTF16("%s"), current_title_); |
| 73 return format; |
| 74 } |
| 75 bool CanResize() const override { return true; } |
| 76 bool CanMaximize() const override { return true; } |
| 77 bool CanMinimize() const override { return true; } |
| 78 |
| 79 // Overridden from views::View: |
| 80 void Layout() override { |
| 81 gfx::Rect local_bounds = GetLocalBounds(); |
| 82 if (content_area_) { |
| 83 gfx::Point offset = local_bounds.origin(); |
| 84 ConvertPointToWidget(this, &offset); |
| 85 int width = local_bounds.width(); |
| 86 int height = local_bounds.height(); |
| 87 content_area_->SetBounds( |
| 88 gfx::Rect(offset.x(), offset.y(), width, height)); |
| 89 } |
| 90 } |
| 91 void ViewHierarchyChanged( |
| 92 const views::View::ViewHierarchyChangedDetails& details) override { |
| 93 if (details.is_add && GetWidget() && !content_area_) { |
| 94 mus::Window* window = aura::GetMusWindow(GetWidget()->GetNativeWindow()); |
| 95 content_area_ = window->connection()->NewWindow(nullptr); |
| 96 window->AddChild(content_area_); |
| 97 |
| 98 mus::mojom::WindowTreeClientPtr client; |
| 99 view_->GetWindowTreeClient(GetProxy(&client)); |
| 100 content_area_->Embed(std::move(client)); |
| 101 } |
| 102 } |
| 103 |
| 104 // navigation::mojom::ViewClient: |
| 105 void LoadingStateChanged(bool is_loading) override {} |
| 106 void NavigationStateChanged(const GURL& url, |
| 107 const mojo::String& title, |
| 108 bool can_go_back, |
| 109 bool can_go_forward) override { |
| 110 current_title_ = base::UTF8ToUTF16(title.get()); |
| 111 GetWidget()->UpdateWindowTitle(); |
| 112 } |
| 113 void LoadProgressChanged(double progress) override {} |
| 114 void ViewCreated(navigation::mojom::ViewPtr view, |
| 115 navigation::mojom::ViewClientRequest request, |
| 116 bool is_popup, |
| 117 mojo::RectPtr initial_rect, |
| 118 bool user_gesture) override { |
| 119 views::Widget* window = views::Widget::CreateWindowWithContextAndBounds( |
| 120 new UI(webtest_, is_popup, std::move(view), std::move(request)), |
| 121 nullptr, initial_rect.To<gfx::Rect>()); |
| 122 window->Show(); |
| 123 webtest_->AddWindow(window); |
| 124 } |
| 125 void Close() override { |
| 126 GetWidget()->Close(); |
| 127 } |
| 128 |
| 129 Webtest* webtest_; |
| 130 bool is_popup_; |
| 131 mus::Window* content_area_ = nullptr; |
| 132 navigation::mojom::ViewPtr view_; |
| 133 mojo::Binding<navigation::mojom::ViewClient> view_client_binding_; |
| 134 base::string16 current_title_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(UI); |
| 137 }; |
| 138 |
| 139 Webtest::Webtest() {} |
| 140 Webtest::~Webtest() {} |
| 141 |
| 142 void Webtest::AddWindow(views::Widget* window) { |
| 143 windows_.push_back(window); |
| 144 } |
| 145 |
| 146 void Webtest::RemoveWindow(views::Widget* window) { |
| 147 auto it = std::find(windows_.begin(), windows_.end(), window); |
| 148 DCHECK(it != windows_.end()); |
| 149 windows_.erase(it); |
| 150 if (windows_.empty()) |
| 151 base::MessageLoop::current()->QuitWhenIdle(); |
| 152 } |
| 153 |
| 154 void Webtest::Initialize(shell::Connector* connector, |
| 155 const shell::Identity& identity, |
| 156 uint32_t id) { |
| 157 connector_ = connector; |
| 158 tracing_.Initialize(connector, identity.name()); |
| 159 |
| 160 aura_init_.reset(new views::AuraInit(connector, "views_mus_resources.pak")); |
| 161 views::WindowManagerConnection::Create(connector, identity); |
| 162 } |
| 163 |
| 164 bool Webtest::AcceptConnection(shell::Connection* connection) { |
| 165 connection->AddInterface<mojom::Launchable>(this); |
| 166 return true; |
| 167 } |
| 168 |
| 169 void Webtest::Launch(uint32_t what, mojom::LaunchMode how) { |
| 170 bool reuse = how == mojom::LaunchMode::REUSE || |
| 171 how == mojom::LaunchMode::DEFAULT; |
| 172 if (reuse && !windows_.empty()) { |
| 173 windows_.back()->Activate(); |
| 174 return; |
| 175 } |
| 176 |
| 177 navigation::mojom::ViewFactoryPtr view_factory; |
| 178 connector_->ConnectToInterface("exe:navigation", &view_factory); |
| 179 navigation::mojom::ViewPtr view; |
| 180 navigation::mojom::ViewClientPtr view_client; |
| 181 navigation::mojom::ViewClientRequest view_client_request = |
| 182 GetProxy(&view_client); |
| 183 view_factory->CreateView(std::move(view_client), GetProxy(&view)); |
| 184 UI* ui = new UI(this, false, std::move(view), std::move(view_client_request)); |
| 185 views::Widget* window = views::Widget::CreateWindowWithContextAndBounds( |
| 186 ui, nullptr, gfx::Rect(50, 10, 600, 600)); |
| 187 ui->NavigateTo(GURL("http://www.theverge.com/")); |
| 188 window->Show(); |
| 189 AddWindow(window); |
| 190 } |
| 191 |
| 192 void Webtest::Create(shell::Connection* connection, |
| 193 mojom::LaunchableRequest request) { |
| 194 bindings_.AddBinding(this, std::move(request)); |
| 195 } |
| 196 |
| 197 } // namespace webtest |
| 198 } // namespace mash |
OLD | NEW |