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/macros.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/time/time.h"
11 #include "mojo/examples/pepper_container_app/plugin_instance.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
20 const PP_Instance kInstanceId
= 1;
24 // A non-abstract subclass of ppapi::MessageLoopShared that represents the
25 // message loop of the main thread.
26 // TODO(yzshen): Build a more general ppapi::MessageLoopShared subclass to fully
27 // support PPB_MessageLoop.
28 class MojoPpapiGlobals::MainThreadMessageLoopResource
29 : public ppapi::MessageLoopShared
{
31 explicit MainThreadMessageLoopResource(
32 base::MessageLoopProxy
* main_thread_message_loop
)
33 : MessageLoopShared(ForMainThread()),
34 main_thread_message_loop_(main_thread_message_loop
) {}
36 // ppapi::MessageLoopShared implementation.
37 virtual void PostClosure(const tracked_objects::Location
& from_here
,
38 const base::Closure
& closure
,
39 int64 delay_ms
) override
{
40 main_thread_message_loop_
->PostDelayedTask(
41 from_here
, closure
, base::TimeDelta::FromMilliseconds(delay_ms
));
44 virtual base::MessageLoopProxy
* GetMessageLoopProxy() override
{
45 return main_thread_message_loop_
.get();
48 virtual bool CurrentlyHandlingBlockingMessage() override
{
52 // ppapi::thunk::PPB_MessageLoop_API implementation.
53 virtual int32_t AttachToCurrentThread() override
{
55 return PP_ERROR_FAILED
;
58 virtual int32_t Run() override
{
60 return PP_ERROR_FAILED
;
63 virtual int32_t PostWork(PP_CompletionCallback callback
,
64 int64_t delay_ms
) override
{
66 return PP_ERROR_FAILED
;
69 virtual int32_t PostQuit(PP_Bool should_destroy
) override
{
71 return PP_ERROR_FAILED
;
75 virtual ~MainThreadMessageLoopResource() {}
77 scoped_refptr
<base::MessageLoopProxy
> main_thread_message_loop_
;
78 DISALLOW_COPY_AND_ASSIGN(MainThreadMessageLoopResource
);
81 MojoPpapiGlobals::MojoPpapiGlobals(Delegate
* delegate
)
82 : delegate_(delegate
),
83 plugin_instance_(NULL
),
84 resource_tracker_(ppapi::ResourceTracker::THREAD_SAFE
) {}
86 MojoPpapiGlobals::~MojoPpapiGlobals() {}
88 PP_Instance
MojoPpapiGlobals::AddInstance(PluginInstance
* instance
) {
89 DCHECK(!plugin_instance_
);
90 plugin_instance_
= instance
;
91 resource_tracker_
.DidCreateInstance(kInstanceId
);
95 void MojoPpapiGlobals::InstanceDeleted(PP_Instance instance
) {
96 DCHECK_EQ(instance
, kInstanceId
);
97 DCHECK(plugin_instance_
);
98 resource_tracker_
.DidDeleteInstance(instance
);
99 plugin_instance_
= NULL
;
102 PluginInstance
* MojoPpapiGlobals::GetInstance(PP_Instance instance
) {
103 if (instance
== kInstanceId
)
104 return plugin_instance_
;
108 ScopedMessagePipeHandle
MojoPpapiGlobals::CreateGLES2Context() {
109 return delegate_
->CreateGLES2Context();
112 ppapi::ResourceTracker
* MojoPpapiGlobals::GetResourceTracker() {
113 return &resource_tracker_
;
116 ppapi::VarTracker
* MojoPpapiGlobals::GetVarTracker() {
121 ppapi::CallbackTracker
* MojoPpapiGlobals::GetCallbackTrackerForInstance(
122 PP_Instance instance
) {
123 if (instance
== kInstanceId
&& plugin_instance_
)
124 return plugin_instance_
->plugin_module()->callback_tracker();
128 void MojoPpapiGlobals::LogWithSource(PP_Instance instance
,
130 const std::string
& source
,
131 const std::string
& value
) {
135 void MojoPpapiGlobals::BroadcastLogWithSource(PP_Module module
,
137 const std::string
& source
,
138 const std::string
& value
) {
142 ppapi::thunk::PPB_Instance_API
* MojoPpapiGlobals::GetInstanceAPI(
143 PP_Instance instance
) {
144 if (instance
== kInstanceId
&& plugin_instance_
)
145 return plugin_instance_
;
149 ppapi::thunk::ResourceCreationAPI
* MojoPpapiGlobals::GetResourceCreationAPI(
150 PP_Instance instance
) {
151 if (instance
== kInstanceId
&& plugin_instance_
)
152 return plugin_instance_
->resource_creation();
156 PP_Module
MojoPpapiGlobals::GetModuleForInstance(PP_Instance instance
) {
161 ppapi::MessageLoopShared
* MojoPpapiGlobals::GetCurrentMessageLoop() {
162 if (base::MessageLoopProxy::current().get() == GetMainThreadMessageLoop()) {
163 if (!main_thread_message_loop_resource_
.get()) {
164 main_thread_message_loop_resource_
= new MainThreadMessageLoopResource(
165 GetMainThreadMessageLoop());
167 return main_thread_message_loop_resource_
.get();
174 base::TaskRunner
* MojoPpapiGlobals::GetFileTaskRunner() {
179 std::string
MojoPpapiGlobals::GetCmdLine() {
181 return std::string();
184 void MojoPpapiGlobals::PreCacheFontForFlash(const void* logfontw
) {
188 } // namespace examples