1 // Copyright (c) 2012 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 "ppapi/proxy/ppb_core_proxy.h"
7 #include <stdlib.h> // For malloc
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/time.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/pp_resource.h"
15 #include "ppapi/c/ppb_core.h"
16 #include "ppapi/proxy/plugin_dispatcher.h"
17 #include "ppapi/proxy/plugin_resource_tracker.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
20 #include "ppapi/shared_impl/proxy_lock.h"
21 #include "ppapi/shared_impl/time_conversion.h"
28 void AddRefResource(PP_Resource resource
) {
29 ppapi::ProxyAutoLock lock
;
30 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(resource
);
33 void ReleaseResource(PP_Resource resource
) {
34 ppapi::ProxyAutoLock lock
;
35 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(resource
);
39 return TimeToPPTime(base::Time::Now());
42 double GetTimeTicks() {
43 return TimeTicksToPPTimeTicks(base::TimeTicks::Now());
46 void CallbackWrapper(PP_CompletionCallback callback
, int32_t result
) {
47 TRACE_EVENT2("ppapi proxy", "CallOnMainThread callback",
48 "Func", reinterpret_cast<void*>(callback
.func
),
49 "UserData", callback
.user_data
);
50 CallWhileUnlocked(PP_RunCompletionCallback
, &callback
, result
);
53 void CallOnMainThread(int delay_in_ms
,
54 PP_CompletionCallback callback
,
56 DCHECK(callback
.func
);
59 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
61 RunWhileLocked(base::Bind(&CallbackWrapper
, callback
, result
)),
62 base::TimeDelta::FromMilliseconds(delay_in_ms
));
65 PP_Bool
IsMainThread() {
66 return PP_FromBool(PpapiGlobals::Get()->
67 GetMainThreadMessageLoop()->BelongsToCurrentThread());
70 const PPB_Core core_interface
= {
81 PPB_Core_Proxy::PPB_Core_Proxy(Dispatcher
* dispatcher
)
82 : InterfaceProxy(dispatcher
),
83 ppb_core_impl_(NULL
) {
84 if (!dispatcher
->IsPlugin()) {
85 ppb_core_impl_
= static_cast<const PPB_Core
*>(
86 dispatcher
->local_get_interface()(PPB_CORE_INTERFACE
));
90 PPB_Core_Proxy::~PPB_Core_Proxy() {
94 const PPB_Core
* PPB_Core_Proxy::GetPPB_Core_Interface() {
95 return &core_interface
;
98 bool PPB_Core_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
100 IPC_BEGIN_MESSAGE_MAP(PPB_Core_Proxy
, msg
)
101 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCore_AddRefResource
,
103 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCore_ReleaseResource
,
104 OnMsgReleaseResource
)
105 IPC_MESSAGE_UNHANDLED(handled
= false)
106 IPC_END_MESSAGE_MAP()
107 // TODO(brettw) handle bad messages!
111 void PPB_Core_Proxy::OnMsgAddRefResource(const HostResource
& resource
) {
112 ppb_core_impl_
->AddRefResource(resource
.host_resource());
115 void PPB_Core_Proxy::OnMsgReleaseResource(const HostResource
& resource
) {
116 ppb_core_impl_
->ReleaseResource(resource
.host_resource());