Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / sample_app / sample_app.cc
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.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "mojo/examples/sample_app/gles2_client_impl.h"
9 #include "mojo/public/cpp/application/application_connection.h"
10 #include "mojo/public/cpp/application/application_delegate.h"
11 #include "mojo/public/cpp/application/application_impl.h"
12 #include "mojo/public/cpp/system/core.h"
13 #include "mojo/public/cpp/system/macros.h"
14 #include "mojo/public/cpp/utility/run_loop.h"
15 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
16
17 namespace examples {
18
19 class SampleApp : public mojo::ApplicationDelegate,
20                   public mojo::NativeViewportClient {
21  public:
22   SampleApp() {}
23
24   virtual ~SampleApp() {
25     // TODO(darin): Fix shutdown so we don't need to leak this.
26     MOJO_ALLOW_UNUSED GLES2ClientImpl* leaked = gles2_client_.release();
27   }
28
29   virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE {
30     app->ConnectToService("mojo:mojo_native_viewport_service", &viewport_);
31     viewport_.set_client(this);
32
33     mojo::RectPtr rect(mojo::Rect::New());
34     rect->x = 10;
35     rect->y = 10;
36     rect->width = 800;
37     rect->height = 600;
38     viewport_->Create(rect.Pass());
39     viewport_->Show();
40
41     mojo::CommandBufferPtr command_buffer;
42     viewport_->CreateGLES2Context(Get(&command_buffer));
43     gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass()));
44   }
45
46   virtual void OnCreated() MOJO_OVERRIDE {
47   }
48
49   virtual void OnDestroyed(
50       const mojo::Callback<void()>& callback) MOJO_OVERRIDE {
51     mojo::RunLoop::current()->Quit();
52     callback.Run();
53   }
54
55   virtual void OnBoundsChanged(mojo::RectPtr bounds) MOJO_OVERRIDE {
56     assert(bounds);
57     mojo::SizePtr size(mojo::Size::New());
58     size->width = bounds->width;
59     size->height = bounds->height;
60     gles2_client_->SetSize(*size);
61   }
62
63   virtual void OnEvent(mojo::EventPtr event,
64                        const mojo::Callback<void()>& callback) MOJO_OVERRIDE {
65     assert(event);
66     if (event->location)
67       gles2_client_->HandleInputEvent(*event);
68     callback.Run();
69   }
70
71  private:
72   scoped_ptr<GLES2ClientImpl> gles2_client_;
73   mojo::NativeViewportPtr viewport_;
74
75   DISALLOW_COPY_AND_ASSIGN(SampleApp);
76 };
77
78 }  // namespace examples
79
80 namespace mojo {
81
82 // static
83 ApplicationDelegate* ApplicationDelegate::Create() {
84   return new examples::SampleApp();
85 }
86
87 }  // namespace mojo