OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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/examples/pepper_container_app/mojo_ppapi_globals.h" |
| 11 #include "mojo/examples/pepper_container_app/plugin_instance.h" |
| 12 #include "mojo/examples/pepper_container_app/plugin_module.h" |
| 13 #include "mojo/examples/pepper_container_app/type_converters.h" |
| 14 #include "mojo/public/bindings/allocation_scope.h" |
| 15 #include "mojo/public/bindings/remote_ptr.h" |
| 16 #include "mojo/public/environment/environment.h" |
| 17 #include "mojo/public/gles2/gles2_cpp.h" |
| 18 #include "mojo/public/shell/application.h" |
| 19 #include "mojo/public/shell/shell.mojom.h" |
| 20 #include "mojo/public/system/core.h" |
| 21 #include "mojo/services/native_viewport/native_viewport.mojom.h" |
| 22 #include "ppapi/c/pp_rect.h" |
| 23 #include "ppapi/shared_impl/proxy_lock.h" |
| 24 |
| 25 #if defined(OS_WIN) |
| 26 #if !defined(CDECL) |
| 27 #define CDECL __cdecl |
| 28 #endif |
| 29 #define PEPPER_CONTAINER_APP_EXPORT __declspec(dllexport) |
| 30 #else |
| 31 #define CDECL |
| 32 #define PEPPER_CONTAINER_APP_EXPORT __attribute__((visibility("default"))) |
| 33 #endif |
| 34 |
| 35 namespace mojo { |
| 36 namespace examples { |
| 37 |
| 38 class PepperContainerApp: public Application, |
| 39 public NativeViewportClient, |
| 40 public MojoPpapiGlobals::Delegate { |
| 41 public: |
| 42 explicit PepperContainerApp(MojoHandle shell_handle) |
| 43 : Application(shell_handle), |
| 44 ppapi_globals_(this), |
| 45 plugin_module_(new PluginModule) { |
| 46 InterfacePipe<NativeViewport, AnyInterface> viewport_pipe; |
| 47 mojo::AllocationScope scope; |
| 48 shell()->Connect("mojo:mojo_native_viewport_service", |
| 49 viewport_pipe.handle_to_peer.Pass()); |
| 50 viewport_.reset(viewport_pipe.handle_to_self.Pass(), this); |
| 51 Rect::Builder rect; |
| 52 Point::Builder point; |
| 53 point.set_x(10); |
| 54 point.set_y(10); |
| 55 rect.set_position(point.Finish()); |
| 56 Size::Builder size; |
| 57 size.set_width(800); |
| 58 size.set_height(600); |
| 59 rect.set_size(size.Finish()); |
| 60 viewport_->Create(rect.Finish()); |
| 61 viewport_->Show(); |
| 62 } |
| 63 |
| 64 virtual ~PepperContainerApp() {} |
| 65 |
| 66 // NativeViewportClient implementation. |
| 67 virtual void OnCreated() OVERRIDE { |
| 68 ppapi::ProxyAutoLock lock; |
| 69 |
| 70 plugin_instance_ = plugin_module_->CreateInstance().Pass(); |
| 71 if (!plugin_instance_->DidCreate()) |
| 72 plugin_instance_.reset(); |
| 73 } |
| 74 |
| 75 virtual void OnDestroyed() OVERRIDE { |
| 76 ppapi::ProxyAutoLock lock; |
| 77 |
| 78 if (plugin_instance_) { |
| 79 plugin_instance_->DidDestroy(); |
| 80 plugin_instance_.reset(); |
| 81 } |
| 82 |
| 83 base::MessageLoop::current()->Quit(); |
| 84 } |
| 85 |
| 86 virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE { |
| 87 ppapi::ProxyAutoLock lock; |
| 88 |
| 89 if (plugin_instance_) |
| 90 plugin_instance_->DidChangeView(bounds); |
| 91 } |
| 92 |
| 93 virtual void OnEvent(const Event& event) OVERRIDE { |
| 94 if (event.location().is_null()) |
| 95 return; |
| 96 |
| 97 { |
| 98 ppapi::ProxyAutoLock lock; |
| 99 |
| 100 // TODO(yzshen): Handle events. |
| 101 } |
| 102 viewport_->AckEvent(event); |
| 103 } |
| 104 |
| 105 // MojoPpapiGlobals::Delegate implementation. |
| 106 virtual ScopedMessagePipeHandle CreateGLES2Context() OVERRIDE { |
| 107 MessagePipe gles2_pipe; |
| 108 viewport_->CreateGLES2Context(gles2_pipe.handle1.Pass()); |
| 109 return gles2_pipe.handle0.Pass(); |
| 110 } |
| 111 |
| 112 private: |
| 113 MojoPpapiGlobals ppapi_globals_; |
| 114 |
| 115 RemotePtr<NativeViewport> viewport_; |
| 116 scoped_refptr<PluginModule> plugin_module_; |
| 117 scoped_ptr<PluginInstance> plugin_instance_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(PepperContainerApp); |
| 120 }; |
| 121 |
| 122 } // namespace examples |
| 123 } // namespace mojo |
| 124 |
| 125 extern "C" PEPPER_CONTAINER_APP_EXPORT MojoResult CDECL MojoMain( |
| 126 MojoHandle shell_handle) { |
| 127 mojo::Environment env; |
| 128 mojo::GLES2Initializer gles2; |
| 129 base::MessageLoop run_loop; |
| 130 mojo::examples::PepperContainerApp app(shell_handle); |
| 131 |
| 132 run_loop.Run(); |
| 133 return MOJO_RESULT_OK; |
| 134 } |
OLD | NEW |