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/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" 24 25 namespace mojo { 26 namespace examples { 27 28 class PepperContainerApp: public ApplicationDelegate, 29 public NativeViewportClient, 30 public MojoPpapiGlobals::Delegate { 31 public: 32 explicit PepperContainerApp() 33 : ppapi_globals_(this), 34 plugin_module_(new PluginModule) {} 35 36 virtual ~PepperContainerApp() {} 37 38 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { 39 app->ConnectToService("mojo:mojo_native_viewport_service", &viewport_); 40 viewport_.set_client(this); 41 42 // TODO(jamesr): Should be mojo:mojo_gpu_service 43 app->ConnectToService("mojo:mojo_native_viewport_service", &gpu_service_); 44 45 SizePtr size(Size::New()); 46 size->width = 800; 47 size->height = 600; 48 viewport_->Create(size.Pass()); 49 viewport_->Show(); 50 } 51 52 // NativeViewportClient implementation. 53 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE { 54 native_viewport_id_ = native_viewport_id; 55 ppapi::ProxyAutoLock lock; 56 57 plugin_instance_ = plugin_module_->CreateInstance().Pass(); 58 if (!plugin_instance_->DidCreate()) 59 plugin_instance_.reset(); 60 } 61 62 virtual void OnDestroyed() OVERRIDE { 63 ppapi::ProxyAutoLock lock; 64 65 if (plugin_instance_) { 66 plugin_instance_->DidDestroy(); 67 plugin_instance_.reset(); 68 } 69 70 base::MessageLoop::current()->Quit(); 71 } 72 73 virtual void OnBoundsChanged(SizePtr bounds) OVERRIDE { 74 ppapi::ProxyAutoLock lock; 75 76 if (plugin_instance_) 77 plugin_instance_->DidChangeView(bounds.To<PP_Rect>()); 78 } 79 80 virtual void OnEvent(EventPtr event, 81 const mojo::Callback<void()>& callback) OVERRIDE { 82 if (!event->location_data.is_null()) { 83 ppapi::ProxyAutoLock lock; 84 85 // TODO(yzshen): Handle events. 86 } 87 callback.Run(); 88 } 89 90 // MojoPpapiGlobals::Delegate implementation. 91 virtual ScopedMessagePipeHandle CreateGLES2Context() OVERRIDE { 92 CommandBufferPtr command_buffer; 93 SizePtr size = Size::New(); 94 size->width = 800; 95 size->width = 600; 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(); 100 } 101 102 private: 103 MojoPpapiGlobals ppapi_globals_; 104 105 uint64_t native_viewport_id_; 106 NativeViewportPtr viewport_; 107 GpuPtr gpu_service_; 108 scoped_refptr<PluginModule> plugin_module_; 109 scoped_ptr<PluginInstance> plugin_instance_; 110 111 DISALLOW_COPY_AND_ASSIGN(PepperContainerApp); 112 }; 113 114 } // namespace examples 115 } // namespace mojo 116 117 MojoResult MojoMain(MojoHandle shell_handle) { 118 mojo::ApplicationRunnerChromium runner( 119 new mojo::examples::PepperContainerApp); 120 return runner.Run(shell_handle); 121 } 122 123