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.
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/examples/launcher/launcher.mojom.h"
12 #include "mojo/examples/view_manager/view_manager.mojom.h"
13 #include "mojo/public/bindings/allocation_scope.h"
14 #include "mojo/public/bindings/remote_ptr.h"
15 #include "mojo/public/shell/application.h"
16 #include "mojo/public/shell/shell.mojom.h"
17 #include "mojo/public/system/core.h"
18 #include "mojo/public/system/macros.h"
19 #include "mojo/services/native_viewport/geometry_conversions.h"
20 #include "mojo/services/native_viewport/native_viewport.mojom.h"
21 #include "ui/events/event_constants.h"
22 #include "ui/events/keycodes/keyboard_codes.h"
23 #include "ui/gfx/rect.h"
27 #define CDECL __cdecl)
29 #define VIEW_MANAGER_EXPORT __declspec(dllexport)
32 #define VIEW_MANAGER_EXPORT __attribute__((visibility("default")))
38 class ViewImpl
: public View
{
40 explicit ViewImpl(ScopedViewClientHandle handle
)
42 client_(handle
.Pass(), this) {}
43 virtual ~ViewImpl() {}
46 // Overridden from View:
47 virtual void SetId(int view_id
) OVERRIDE
{
50 virtual void GetId(const mojo::Callback
<void(int32_t)>& callback
) OVERRIDE
{
55 RemotePtr
<ViewClient
> client_
;
57 DISALLOW_COPY_AND_ASSIGN(ViewImpl
);
60 class ViewManagerImpl
: public Service
<ViewManager
, ViewManagerImpl
>,
61 public NativeViewportClient
,
62 public LauncherClient
{
64 explicit ViewManagerImpl() {
69 // Overridden from ViewManager:
70 virtual void CreateView(const Callback
<void(ScopedViewHandle
)>& callback
)
72 InterfacePipe
<View
> pipe
;
73 views_
.push_back(new ViewImpl(pipe
.handle_to_peer
.Pass()));
74 callback
.Run(pipe
.handle_to_self
.Pass());
77 // Overridden from NativeViewportClient:
78 virtual void OnCreated() OVERRIDE
{
80 virtual void OnDestroyed() OVERRIDE
{
81 base::MessageLoop::current()->Quit();
83 virtual void OnBoundsChanged(const Rect
& bounds
) OVERRIDE
{
86 virtual void OnEvent(const Event
& event
) OVERRIDE
{
87 if (!event
.location().is_null())
88 native_viewport_
->AckEvent(event
);
89 if (event
.action() == ui::ET_KEY_RELEASED
) {
90 if (event
.key_data().key_code() == ui::VKEY_L
&&
91 (event
.flags() & ui::EF_CONTROL_DOWN
)) {
98 // Overridden from LauncherClient:
99 virtual void OnURLEntered(const mojo::String
& url
) OVERRIDE
{
100 std::string url_spec
= url
.To
<std::string
>();
101 printf("Received URL from launcher app: %s\n", url_spec
.c_str());
105 void InitNativeViewport() {
106 InterfacePipe
<NativeViewport
, AnyInterface
> pipe
;
108 AllocationScope scope
;
109 shell()->Connect("mojo:mojo_native_viewport_service",
110 pipe
.handle_to_peer
.Pass());
112 native_viewport_
.reset(pipe
.handle_to_self
.Pass(), this);
113 native_viewport_
->Create(gfx::Rect(50, 50, 800, 600));
114 native_viewport_
->Show();
117 void InitLauncher() {
118 if (!launcher_
.is_null())
121 InterfacePipe
<Launcher
, AnyInterface
> pipe
;
123 AllocationScope scope
;
124 shell()->Connect("mojo:mojo_launcher", pipe
.handle_to_peer
.Pass());
126 launcher_
.reset(pipe
.handle_to_self
.Pass(), this);
129 void DidCreateContext() {
132 ScopedVector
<ViewImpl
> views_
;
133 RemotePtr
<NativeViewport
> native_viewport_
;
134 RemotePtr
<Launcher
> launcher_
;
136 DISALLOW_COPY_AND_ASSIGN(ViewManagerImpl
);
139 } // namespace examples
142 extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL
MojoMain(
143 MojoHandle shell_handle
) {
144 base::MessageLoop loop
;
145 mojo::Application
app(shell_handle
);
146 app
.AddServiceFactory(
147 new mojo::ServiceFactory
<mojo::examples::ViewManagerImpl
>);
150 return MOJO_RESULT_OK
;