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/macros.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "build/build_config.h"
10 #include "mojo/application/application_runner_chromium.h"
11 #include "mojo/examples/pepper_container_app/mojo_ppapi_globals.h"
12 #include "mojo/examples/pepper_container_app/plugin_instance.h"
13 #include "mojo/examples/pepper_container_app/plugin_module.h"
14 #include "mojo/examples/pepper_container_app/type_converters.h"
15 #include "mojo/public/c/system/main.h"
16 #include "mojo/public/cpp/application/application_delegate.h"
17 #include "mojo/public/cpp/application/application_impl.h"
18 #include "mojo/public/cpp/system/core.h"
19 #include "mojo/services/public/interfaces/geometry/geometry.mojom.h"
20 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h"
21 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
22 #include "ppapi/c/pp_rect.h"
23 #include "ppapi/shared_impl/proxy_lock.h"
28 class PepperContainerApp
: public ApplicationDelegate
,
29 public NativeViewportClient
,
30 public MojoPpapiGlobals::Delegate
{
32 explicit PepperContainerApp()
33 : ppapi_globals_(this),
34 plugin_module_(new PluginModule
) {}
36 virtual ~PepperContainerApp() {}
38 virtual void Initialize(ApplicationImpl
* app
) MOJO_OVERRIDE
{
39 app
->ConnectToService("mojo:mojo_native_viewport_service", &viewport_
);
40 viewport_
.set_client(this);
42 // TODO(jamesr): Should be mojo:mojo_gpu_service
43 app
->ConnectToService("mojo:mojo_native_viewport_service", &gpu_service_
);
45 SizePtr
size(Size::New());
48 viewport_
->Create(size
.Pass());
52 // NativeViewportClient implementation.
53 virtual void OnCreated(uint64_t native_viewport_id
) OVERRIDE
{
54 native_viewport_id_
= native_viewport_id
;
55 ppapi::ProxyAutoLock lock
;
57 plugin_instance_
= plugin_module_
->CreateInstance().Pass();
58 if (!plugin_instance_
->DidCreate())
59 plugin_instance_
.reset();
62 virtual void OnDestroyed() OVERRIDE
{
63 ppapi::ProxyAutoLock lock
;
65 if (plugin_instance_
) {
66 plugin_instance_
->DidDestroy();
67 plugin_instance_
.reset();
70 base::MessageLoop::current()->Quit();
73 virtual void OnBoundsChanged(SizePtr bounds
) OVERRIDE
{
74 ppapi::ProxyAutoLock lock
;
77 plugin_instance_
->DidChangeView(bounds
.To
<PP_Rect
>());
80 virtual void OnEvent(EventPtr event
,
81 const mojo::Callback
<void()>& callback
) OVERRIDE
{
82 if (!event
->location_data
.is_null()) {
83 ppapi::ProxyAutoLock lock
;
85 // TODO(yzshen): Handle events.
90 // MojoPpapiGlobals::Delegate implementation.
91 virtual ScopedMessagePipeHandle
CreateGLES2Context() OVERRIDE
{
92 CommandBufferPtr command_buffer
;
93 SizePtr size
= Size::New();
96 // TODO(jamesr): Output a surface to the native viewport instead.
97 gpu_service_
->CreateOnscreenGLES2Context(
98 native_viewport_id_
, size
.Pass(), Get(&command_buffer
));
99 return command_buffer
.PassMessagePipe();
103 MojoPpapiGlobals ppapi_globals_
;
105 uint64_t native_viewport_id_
;
106 NativeViewportPtr viewport_
;
108 scoped_refptr
<PluginModule
> plugin_module_
;
109 scoped_ptr
<PluginInstance
> plugin_instance_
;
111 DISALLOW_COPY_AND_ASSIGN(PepperContainerApp
);
114 } // namespace examples
117 MojoResult
MojoMain(MojoHandle shell_handle
) {
118 mojo::ApplicationRunnerChromium
runner(
119 new mojo::examples::PepperContainerApp
);
120 return runner
.Run(shell_handle
);