OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "ppapi/proxy/ppb_core_proxy.h" |
| 6 |
| 7 #include <stdlib.h> // For malloc |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.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/ppapi_messages.h" |
| 18 |
| 19 namespace pp { |
| 20 namespace proxy { |
| 21 |
| 22 namespace { |
| 23 |
| 24 base::MessageLoopProxy* GetMainThreadMessageLoop() { |
| 25 static scoped_refptr<base::MessageLoopProxy> proxy( |
| 26 base::MessageLoopProxy::CreateForCurrentThread()); |
| 27 return proxy.get(); |
| 28 } |
| 29 |
| 30 void AddRefResource(PP_Resource resource) { |
| 31 PluginDispatcher::Get()->plugin_resource_tracker()->AddRefResource(resource); |
| 32 } |
| 33 |
| 34 void ReleaseResource(PP_Resource resource) { |
| 35 PluginDispatcher::Get()->plugin_resource_tracker()->ReleaseResource(resource); |
| 36 } |
| 37 |
| 38 void* MemAlloc(size_t num_bytes) { |
| 39 return malloc(num_bytes); |
| 40 } |
| 41 |
| 42 void MemFree(void* ptr) { |
| 43 free(ptr); |
| 44 } |
| 45 |
| 46 double GetTime() { |
| 47 return base::Time::Now().ToDoubleT(); |
| 48 } |
| 49 |
| 50 double GetTimeTicks() { |
| 51 // TODO(brettw) http://code.google.com/p/chromium/issues/detail?id=57448 |
| 52 // This should be a tick timer rather than wall clock time, but needs to |
| 53 // match message times, which also currently use wall clock time. |
| 54 return GetTime(); |
| 55 } |
| 56 |
| 57 void CallOnMainThread(int delay_in_ms, |
| 58 PP_CompletionCallback callback, |
| 59 int32_t result) { |
| 60 GetMainThreadMessageLoop()->PostDelayedTask( |
| 61 FROM_HERE, |
| 62 NewRunnableFunction(callback.func, callback.user_data, result), |
| 63 delay_in_ms); |
| 64 } |
| 65 |
| 66 bool IsMainThread() { |
| 67 return GetMainThreadMessageLoop()->BelongsToCurrentThread(); |
| 68 } |
| 69 |
| 70 const PPB_Core core_interface = { |
| 71 &AddRefResource, |
| 72 &ReleaseResource, |
| 73 &MemAlloc, |
| 74 &MemFree, |
| 75 &GetTime, |
| 76 &GetTimeTicks, |
| 77 &CallOnMainThread, |
| 78 &IsMainThread |
| 79 }; |
| 80 |
| 81 } // namespace |
| 82 |
| 83 PPB_Core_Proxy::PPB_Core_Proxy(Dispatcher* dispatcher, |
| 84 const void* target_interface) |
| 85 : InterfaceProxy(dispatcher, target_interface) { |
| 86 } |
| 87 |
| 88 PPB_Core_Proxy::~PPB_Core_Proxy() { |
| 89 } |
| 90 |
| 91 const void* PPB_Core_Proxy::GetSourceInterface() const { |
| 92 return &core_interface; |
| 93 } |
| 94 |
| 95 InterfaceID PPB_Core_Proxy::GetInterfaceId() const { |
| 96 return INTERFACE_ID_PPB_CORE; |
| 97 } |
| 98 |
| 99 void PPB_Core_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 100 IPC_BEGIN_MESSAGE_MAP(PPB_Core_Proxy, msg) |
| 101 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCore_AddRefResource, |
| 102 OnMsgAddRefResource) |
| 103 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCore_ReleaseResource, |
| 104 OnMsgReleaseResource) |
| 105 IPC_END_MESSAGE_MAP() |
| 106 // TODO(brettw) handle bad messages! |
| 107 } |
| 108 |
| 109 void PPB_Core_Proxy::OnMsgAddRefResource(PP_Resource resource) { |
| 110 ppb_core_target()->AddRefResource(resource); |
| 111 } |
| 112 |
| 113 void PPB_Core_Proxy::OnMsgReleaseResource(PP_Resource resource) { |
| 114 ppb_core_target()->ReleaseResource(resource); |
| 115 } |
| 116 |
| 117 } // namespace proxy |
| 118 } // namespace pp |
OLD | NEW |