cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / mandoline / ui / omnibox / omnibox_application.cc
blob8e336910aa06d8e82d0529f8b4db7088113ad113
1 // Copyright 2015 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.
5 #include "mandoline/ui/omnibox/omnibox_application.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/url_formatter/url_fixer.h"
10 #include "components/view_manager/public/cpp/view_tree_connection.h"
11 #include "components/view_manager/public/cpp/view_tree_delegate.h"
12 #include "mandoline/ui/aura/aura_init.h"
13 #include "mandoline/ui/aura/native_widget_view_manager.h"
14 #include "mandoline/ui/desktop_ui/public/interfaces/view_embedder.mojom.h"
15 #include "mojo/application/public/cpp/application_impl.h"
16 #include "mojo/common/common_type_converters.h"
17 #include "mojo/converters/geometry/geometry_type_converters.h"
18 #include "ui/views/background.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/controls/textfield/textfield_controller.h"
21 #include "ui/views/layout/layout_manager.h"
22 #include "ui/views/widget/widget_delegate.h"
24 namespace mandoline {
26 ////////////////////////////////////////////////////////////////////////////////
27 // OmniboxImpl
29 class OmniboxImpl : public mojo::ViewTreeDelegate,
30 public views::LayoutManager,
31 public views::TextfieldController,
32 public Omnibox {
33 public:
34 OmniboxImpl(mojo::ApplicationImpl* app,
35 mojo::ApplicationConnection* connection,
36 mojo::InterfaceRequest<Omnibox> request);
37 ~OmniboxImpl() override;
39 private:
40 // Overridden from mojo::ViewTreeDelegate:
41 void OnEmbed(mojo::View* root) override;
42 void OnConnectionLost(mojo::ViewTreeConnection* connection) override;
44 // Overridden from views::LayoutManager:
45 gfx::Size GetPreferredSize(const views::View* view) const override;
46 void Layout(views::View* host) override;
48 // Overridden from views::TextfieldController:
49 bool HandleKeyEvent(views::Textfield* sender,
50 const ui::KeyEvent& key_event) override;
52 // Overridden from Omnibox:
53 void GetViewTreeClient(
54 mojo::InterfaceRequest<mojo::ViewTreeClient> request) override;
55 void ShowForURL(const mojo::String& url) override;
57 void HideWindow();
58 void ShowWindow();
60 scoped_ptr<AuraInit> aura_init_;
61 mojo::ApplicationImpl* app_;
62 mojo::View* root_;
63 mojo::String url_;
64 views::Textfield* edit_;
65 mojo::Binding<Omnibox> binding_;
66 ViewEmbedderPtr view_embedder_;
68 DISALLOW_COPY_AND_ASSIGN(OmniboxImpl);
71 ////////////////////////////////////////////////////////////////////////////////
72 // OmniboxApplication, public:
74 OmniboxApplication::OmniboxApplication() : app_(nullptr) {}
75 OmniboxApplication::~OmniboxApplication() {}
77 ////////////////////////////////////////////////////////////////////////////////
78 // OmniboxApplication, mojo::ApplicationDelegate implementation:
80 void OmniboxApplication::Initialize(mojo::ApplicationImpl* app) {
81 app_ = app;
84 bool OmniboxApplication::ConfigureIncomingConnection(
85 mojo::ApplicationConnection* connection) {
86 connection->AddService<Omnibox>(this);
87 return true;
90 ////////////////////////////////////////////////////////////////////////////////
91 // OmniboxApplication, mojo::InterfaceFactory<Omnibox> implementation:
93 void OmniboxApplication::Create(mojo::ApplicationConnection* connection,
94 mojo::InterfaceRequest<Omnibox> request) {
95 new OmniboxImpl(app_, connection, request.Pass());
98 ////////////////////////////////////////////////////////////////////////////////
99 // OmniboxImpl, public:
101 OmniboxImpl::OmniboxImpl(mojo::ApplicationImpl* app,
102 mojo::ApplicationConnection* connection,
103 mojo::InterfaceRequest<Omnibox> request)
104 : app_(app),
105 root_(nullptr),
106 edit_(nullptr),
107 binding_(this, request.Pass()) {
108 connection->ConnectToService(&view_embedder_);
110 OmniboxImpl::~OmniboxImpl() {}
112 ////////////////////////////////////////////////////////////////////////////////
113 // OmniboxImpl, mojo::ViewTreeDelegate implementation:
115 void OmniboxImpl::OnEmbed(mojo::View* root) {
116 root_ = root;
118 if (!aura_init_.get()) {
119 aura_init_.reset(new AuraInit(root, app_->shell()));
120 edit_ = new views::Textfield;
121 edit_->set_controller(this);
122 edit_->SetTextInputType(ui::TEXT_INPUT_TYPE_URL);
125 const int kOpacity = 0xC0;
126 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
127 widget_delegate->GetContentsView()->set_background(
128 views::Background::CreateSolidBackground(
129 SkColorSetA(0xDDDDDD, kOpacity)));
130 widget_delegate->GetContentsView()->AddChildView(edit_);
131 widget_delegate->GetContentsView()->SetLayoutManager(this);
133 // TODO(beng): we may be leaking these on subsequent calls to OnEmbed()...
134 // probably should only allow once instance per view.
135 views::Widget* widget = new views::Widget;
136 views::Widget::InitParams params(
137 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
138 params.native_widget =
139 new NativeWidgetViewManager(widget, app_->shell(), root);
140 params.delegate = widget_delegate;
141 params.bounds = root->bounds().To<gfx::Rect>();
142 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
143 widget->Init(params);
144 widget->Show();
145 widget->GetCompositor()->SetBackgroundColor(
146 SkColorSetA(SK_ColorBLACK, kOpacity));
148 ShowWindow();
151 void OmniboxImpl::OnConnectionLost(mojo::ViewTreeConnection* connection) {
152 root_ = nullptr;
155 ////////////////////////////////////////////////////////////////////////////////
156 // OmniboxImpl, views::LayoutManager implementation:
158 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const {
159 return gfx::Size();
162 void OmniboxImpl::Layout(views::View* host) {
163 gfx::Rect edit_bounds = host->bounds();
164 edit_bounds.Inset(10, 10, 10, host->bounds().height() - 40);
165 edit_->SetBoundsRect(edit_bounds);
167 // TODO(beng): layout dropdown...
170 ////////////////////////////////////////////////////////////////////////////////
171 // OmniboxImpl, views::TextfieldController implementation:
173 bool OmniboxImpl::HandleKeyEvent(views::Textfield* sender,
174 const ui::KeyEvent& key_event) {
175 if (key_event.key_code() == ui::VKEY_RETURN) {
176 // TODO(beng): call back to browser.
177 mojo::URLRequestPtr request(mojo::URLRequest::New());
178 GURL url = url_formatter::FixupURL(base::UTF16ToUTF8(sender->text()),
179 std::string());
180 request->url = url.spec();
181 view_embedder_->Embed(request.Pass());
182 HideWindow();
183 return true;
185 return false;
188 ////////////////////////////////////////////////////////////////////////////////
189 // OmniboxImpl, Omnibox implementation:
191 void OmniboxImpl::GetViewTreeClient(
192 mojo::InterfaceRequest<mojo::ViewTreeClient> request) {
193 mojo::ViewTreeConnection::Create(this, request.Pass());
196 void OmniboxImpl::ShowForURL(const mojo::String& url) {
197 url_ = url;
198 if (root_) {
199 ShowWindow();
200 } else {
201 mojo::URLRequestPtr request(mojo::URLRequest::New());
202 request->url = mojo::String::From("mojo:omnibox");
203 view_embedder_->Embed(request.Pass());
207 ////////////////////////////////////////////////////////////////////////////////
208 // OmniboxImpl, private:
210 void OmniboxImpl::ShowWindow() {
211 DCHECK(root_);
212 root_->SetVisible(true);
213 root_->SetFocus();
214 root_->MoveToFront();
215 edit_->SetText(url_.To<base::string16>());
216 edit_->SelectAll(false);
217 edit_->RequestFocus();
220 void OmniboxImpl::HideWindow() {
221 DCHECK(root_);
222 root_->SetVisible(false);
225 } // namespace mandoline