Enable lock even when no password hash is present.
[chromium-blink-merge.git] / mojo / examples / aura_demo / aura_demo.cc
blobe49d1f5025a8f56ce366aa7be4492f1cf1b77961
1 // Copyright 2013 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 <stdio.h>
6 #include <string>
8 #include "base/bind.h"
9 #include "mojo/aura/context_factory_mojo.h"
10 #include "mojo/aura/screen_mojo.h"
11 #include "mojo/aura/window_tree_host_mojo.h"
12 #include "mojo/aura/window_tree_host_mojo_delegate.h"
13 #include "mojo/public/c/system/main.h"
14 #include "mojo/public/cpp/application/application_connection.h"
15 #include "mojo/public/cpp/application/application_delegate.h"
16 #include "mojo/public/cpp/application/application_impl.h"
17 #include "mojo/public/cpp/application/application_runner_chromium.h"
18 #include "mojo/public/cpp/system/core.h"
19 #include "mojo/services/public/cpp/view_manager/view.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager.h"
21 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
22 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
23 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
24 #include "ui/aura/client/default_capture_client.h"
25 #include "ui/aura/client/window_tree_client.h"
26 #include "ui/aura/env.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_delegate.h"
29 #include "ui/base/hit_test.h"
30 #include "ui/gfx/canvas.h"
31 #include "ui/gfx/codec/png_codec.h"
33 namespace mojo {
34 namespace examples {
36 // Trivial WindowDelegate implementation that draws a colored background.
37 class DemoWindowDelegate : public aura::WindowDelegate {
38 public:
39 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
41 // Overridden from WindowDelegate:
42 virtual gfx::Size GetMinimumSize() const OVERRIDE {
43 return gfx::Size();
46 virtual gfx::Size GetMaximumSize() const OVERRIDE {
47 return gfx::Size();
50 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
51 const gfx::Rect& new_bounds) OVERRIDE {}
52 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
53 return gfx::kNullCursor;
55 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
56 return HTCAPTION;
58 virtual bool ShouldDescendIntoChildForEventHandling(
59 aura::Window* child,
60 const gfx::Point& location) OVERRIDE {
61 return true;
63 virtual bool CanFocus() OVERRIDE { return true; }
64 virtual void OnCaptureLost() OVERRIDE {}
65 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
66 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
68 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
69 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
70 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
71 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
72 virtual bool HasHitTestMask() const OVERRIDE { return false; }
73 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
75 private:
76 const SkColor color_;
78 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
81 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
82 public:
83 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
84 aura::client::SetWindowTreeClient(window_, this);
87 virtual ~DemoWindowTreeClient() {
88 aura::client::SetWindowTreeClient(window_, NULL);
91 // Overridden from aura::client::WindowTreeClient:
92 virtual aura::Window* GetDefaultParent(aura::Window* context,
93 aura::Window* window,
94 const gfx::Rect& bounds) OVERRIDE {
95 if (!capture_client_) {
96 capture_client_.reset(
97 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
99 return window_;
102 private:
103 aura::Window* window_;
104 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
106 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
109 class AuraDemo : public ApplicationDelegate,
110 public WindowTreeHostMojoDelegate,
111 public ViewManagerDelegate {
112 public:
113 AuraDemo() : window1_(NULL), window2_(NULL), window21_(NULL) {}
114 virtual ~AuraDemo() {}
116 private:
117 // Overridden from ViewManagerDelegate:
118 virtual void OnEmbed(ViewManager* view_manager,
119 View* root,
120 ServiceProviderImpl* exported_services,
121 scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
122 // TODO(beng): this function could be called multiple times!
123 root_ = root;
125 window_tree_host_.reset(new WindowTreeHostMojo(root, this));
126 window_tree_host_->InitHost();
128 window_tree_client_.reset(
129 new DemoWindowTreeClient(window_tree_host_->window()));
131 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
132 window1_ = new aura::Window(delegate1_.get());
133 window1_->Init(aura::WINDOW_LAYER_TEXTURED);
134 window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
135 window1_->Show();
136 window_tree_host_->window()->AddChild(window1_);
138 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
139 window2_ = new aura::Window(delegate2_.get());
140 window2_->Init(aura::WINDOW_LAYER_TEXTURED);
141 window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
142 window2_->Show();
143 window_tree_host_->window()->AddChild(window2_);
145 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
146 window21_ = new aura::Window(delegate21_.get());
147 window21_->Init(aura::WINDOW_LAYER_TEXTURED);
148 window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
149 window21_->Show();
150 window2_->AddChild(window21_);
152 window_tree_host_->Show();
154 virtual void OnViewManagerDisconnected(
155 ViewManager* view_manager) OVERRIDE {
156 base::MessageLoop::current()->Quit();
159 // WindowTreeHostMojoDelegate:
160 virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE {
161 root_->SetContents(bitmap);
164 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
165 view_manager_client_factory_.reset(
166 new ViewManagerClientFactory(app->shell(), this));
167 aura::Env::CreateInstance(true);
168 context_factory_.reset(new ContextFactoryMojo);
169 aura::Env::GetInstance()->set_context_factory(context_factory_.get());
170 screen_.reset(ScreenMojo::Create());
171 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
174 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
175 MOJO_OVERRIDE {
176 connection->AddService(view_manager_client_factory_.get());
177 return true;
180 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
182 scoped_ptr<ui::ContextFactory> context_factory_;
184 scoped_ptr<ScreenMojo> screen_;
186 scoped_ptr<DemoWindowDelegate> delegate1_;
187 scoped_ptr<DemoWindowDelegate> delegate2_;
188 scoped_ptr<DemoWindowDelegate> delegate21_;
190 aura::Window* window1_;
191 aura::Window* window2_;
192 aura::Window* window21_;
194 View* root_;
196 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
198 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
200 DISALLOW_COPY_AND_ASSIGN(AuraDemo);
203 } // namespace examples
204 } // namespace mojo
206 MojoResult MojoMain(MojoHandle shell_handle) {
207 mojo::ApplicationRunnerChromium runner(new mojo::examples::AuraDemo);
208 return runner.Run(shell_handle);