Performance histograms for extension content verification
[chromium-blink-merge.git] / mojo / examples / browser / browser.cc
blobfddf0aa45b2de4eeb1f6e4c575c861ababefda62
1 // Copyright 2014 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 "base/basictypes.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/public/cpp/application/application.h"
9 #include "mojo/services/public/cpp/view_manager/node.h"
10 #include "mojo/services/public/cpp/view_manager/view.h"
11 #include "mojo/services/public/cpp/view_manager/view_manager.h"
12 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
13 #include "mojo/services/public/cpp/view_manager/view_observer.h"
14 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
15 #include "mojo/views/native_widget_view_manager.h"
16 #include "mojo/views/views_init.h"
17 #include "ui/events/event.h"
18 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/controls/textfield/textfield_controller.h"
20 #include "ui/views/layout/layout_manager.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/widget/widget_delegate.h"
23 #include "url/gurl.h"
25 namespace mojo {
26 namespace examples {
28 class BrowserLayoutManager : public views::LayoutManager {
29 public:
30 BrowserLayoutManager() {}
31 virtual ~BrowserLayoutManager() {}
33 private:
34 // Overridden from views::LayoutManager:
35 virtual void Layout(views::View* host) OVERRIDE {
36 // Browser view has one child, a text input field.
37 DCHECK_EQ(1, host->child_count());
38 views::View* text_field = host->child_at(0);
39 gfx::Size ps = text_field->GetPreferredSize();
40 text_field->SetBoundsRect(gfx::Rect(host->width(), ps.height()));
42 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE {
43 return gfx::Size();
46 DISALLOW_COPY_AND_ASSIGN(BrowserLayoutManager);
49 // This is the basics of creating a views widget with a textfield.
50 // TODO: cleanup!
51 class Browser : public Application,
52 public view_manager::ViewManagerDelegate,
53 public views::TextfieldController {
54 public:
55 Browser() : view_manager_(NULL) {}
57 virtual ~Browser() {
60 private:
61 // Overridden from Application:
62 virtual void Initialize() MOJO_OVERRIDE {
63 views_init_.reset(new ViewsInit);
64 view_manager::ViewManager::Create(this, this);
65 ConnectTo("mojo:mojo_window_manager", &navigator_host_);
68 void CreateWidget(view_manager::Node* node) {
69 views::Textfield* textfield = new views::Textfield;
70 textfield->set_controller(this);
72 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
73 widget_delegate->GetContentsView()->AddChildView(textfield);
74 widget_delegate->GetContentsView()->SetLayoutManager(
75 new BrowserLayoutManager);
77 views::Widget* widget = new views::Widget;
78 views::Widget::InitParams params(
79 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
80 params.native_widget = new NativeWidgetViewManager(widget, node);
81 params.delegate = widget_delegate;
82 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height());
83 widget->Init(params);
84 widget->Show();
85 textfield->RequestFocus();
88 // view_manager::ViewManagerDelegate:
89 virtual void OnRootAdded(view_manager::ViewManager* view_manager,
90 view_manager::Node* root) OVERRIDE {
91 // TODO: deal with OnRootAdded() being invoked multiple times.
92 view_manager_ = view_manager;
93 root->SetActiveView(view_manager::View::Create(view_manager));
94 root->SetFocus();
95 CreateWidget(root);
98 // views::TextfieldController:
99 virtual bool HandleKeyEvent(views::Textfield* sender,
100 const ui::KeyEvent& key_event) OVERRIDE {
101 if (key_event.key_code() == ui::VKEY_RETURN) {
102 GURL url(sender->text());
103 printf("User entered this URL: %s\n", url.spec().c_str());
104 navigation::NavigationDetailsPtr nav_details(
105 navigation::NavigationDetails::New());
106 nav_details->url = url.spec();
107 navigator_host_->RequestNavigate(view_manager_->GetRoots().front()->id(),
108 navigation::NEW_NODE,
109 nav_details.Pass());
111 return false;
114 scoped_ptr<ViewsInit> views_init_;
116 view_manager::ViewManager* view_manager_;
117 navigation::NavigatorHostPtr navigator_host_;
119 DISALLOW_COPY_AND_ASSIGN(Browser);
122 } // namespace examples
124 // static
125 Application* Application::Create() {
126 return new examples::Browser;
129 } // namespace mojo