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 "mojo/examples/pepper_container_app/mojo_ppapi_globals.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/time/time.h"
10 #include "mojo/examples/pepper_container_app/plugin_instance.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
19 const PP_Instance kInstanceId
= 1;
23 // A non-abstract subclass of ppapi::MessageLoopShared that represents the
24 // message loop of the main thread.
25 // TODO(yzshen): Build a more general ppapi::MessageLoopShared subclass to fully
26 // support PPB_MessageLoop.
27 class MojoPpapiGlobals::MainThreadMessageLoopResource
28 : public ppapi::MessageLoopShared
{
30 explicit MainThreadMessageLoopResource(
31 base::MessageLoopProxy
* main_thread_message_loop
)
32 : MessageLoopShared(ForMainThread()),
33 main_thread_message_loop_(main_thread_message_loop
) {}
35 // ppapi::MessageLoopShared implementation.
36 virtual void PostClosure(const tracked_objects::Location
& from_here
,
37 const base::Closure
& closure
,
38 int64 delay_ms
) OVERRIDE
{
39 main_thread_message_loop_
->PostDelayedTask(
40 from_here
, closure
, base::TimeDelta::FromMilliseconds(delay_ms
));
43 virtual base::MessageLoopProxy
* GetMessageLoopProxy() OVERRIDE
{
44 return main_thread_message_loop_
.get();
47 // ppapi::thunk::PPB_MessageLoop_API implementation.
48 virtual int32_t AttachToCurrentThread() OVERRIDE
{
50 return PP_ERROR_FAILED
;
53 virtual int32_t Run() OVERRIDE
{
55 return PP_ERROR_FAILED
;
58 virtual int32_t PostWork(PP_CompletionCallback callback
,
59 int64_t delay_ms
) OVERRIDE
{
61 return PP_ERROR_FAILED
;
64 virtual int32_t PostQuit(PP_Bool should_destroy
) OVERRIDE
{
66 return PP_ERROR_FAILED
;
70 virtual ~MainThreadMessageLoopResource() {}
72 scoped_refptr
<base::MessageLoopProxy
> main_thread_message_loop_
;
73 DISALLOW_COPY_AND_ASSIGN(MainThreadMessageLoopResource
);
76 MojoPpapiGlobals::MojoPpapiGlobals(Delegate
* delegate
)
77 : delegate_(delegate
),
78 plugin_instance_(NULL
),
79 resource_tracker_(ppapi::ResourceTracker::THREAD_SAFE
) {}
81 MojoPpapiGlobals::~MojoPpapiGlobals() {}
83 PP_Instance
MojoPpapiGlobals::AddInstance(PluginInstance
* instance
) {
84 DCHECK(!plugin_instance_
);
85 plugin_instance_
= instance
;
86 resource_tracker_
.DidCreateInstance(kInstanceId
);
90 void MojoPpapiGlobals::InstanceDeleted(PP_Instance instance
) {
91 DCHECK_EQ(instance
, kInstanceId
);
92 DCHECK(plugin_instance_
);
93 resource_tracker_
.DidDeleteInstance(instance
);
94 plugin_instance_
= NULL
;
97 PluginInstance
* MojoPpapiGlobals::GetInstance(PP_Instance instance
) {
98 if (instance
== kInstanceId
)
99 return plugin_instance_
;
103 ScopedMessagePipeHandle
MojoPpapiGlobals::CreateGLES2Context() {
104 return delegate_
->CreateGLES2Context();
107 ppapi::ResourceTracker
* MojoPpapiGlobals::GetResourceTracker() {
108 return &resource_tracker_
;
111 ppapi::VarTracker
* MojoPpapiGlobals::GetVarTracker() {
116 ppapi::CallbackTracker
* MojoPpapiGlobals::GetCallbackTrackerForInstance(
117 PP_Instance instance
) {
118 if (instance
== kInstanceId
&& plugin_instance_
)
119 return plugin_instance_
->plugin_module()->callback_tracker();
123 void MojoPpapiGlobals::LogWithSource(PP_Instance instance
,
125 const std::string
& source
,
126 const std::string
& value
) {
130 void MojoPpapiGlobals::BroadcastLogWithSource(PP_Module module
,
132 const std::string
& source
,
133 const std::string
& value
) {
137 ppapi::thunk::PPB_Instance_API
* MojoPpapiGlobals::GetInstanceAPI(
138 PP_Instance instance
) {
139 if (instance
== kInstanceId
&& plugin_instance_
)
140 return plugin_instance_
;
144 ppapi::thunk::ResourceCreationAPI
* MojoPpapiGlobals::GetResourceCreationAPI(
145 PP_Instance instance
) {
146 if (instance
== kInstanceId
&& plugin_instance_
)
147 return plugin_instance_
->resource_creation();
151 PP_Module
MojoPpapiGlobals::GetModuleForInstance(PP_Instance instance
) {
156 ppapi::MessageLoopShared
* MojoPpapiGlobals::GetCurrentMessageLoop() {
157 if (base::MessageLoopProxy::current().get() == GetMainThreadMessageLoop()) {
158 if (!main_thread_message_loop_resource_
.get()) {
159 main_thread_message_loop_resource_
= new MainThreadMessageLoopResource(
160 GetMainThreadMessageLoop());
162 return main_thread_message_loop_resource_
.get();
169 base::TaskRunner
* MojoPpapiGlobals::GetFileTaskRunner() {
174 std::string
MojoPpapiGlobals::GetCmdLine() {
176 return std::string();
179 void MojoPpapiGlobals::PreCacheFontForFlash(const void* logfontw
) {
183 } // namespace examples