OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_CORE_H_ | 5 #ifndef PPAPI_CPP_CORE_H_ |
6 #define PPAPI_CPP_CORE_H_ | 6 #define PPAPI_CPP_CORE_H_ |
7 | 7 |
8 #include "ppapi/c/ppb_core.h" | 8 #include "ppapi/c/ppb_core.h" |
9 | 9 |
| 10 /// @file |
| 11 /// This file defines APIs related to memory management, time, and threads. |
| 12 |
10 namespace pp { | 13 namespace pp { |
11 | 14 |
12 class CompletionCallback; | 15 class CompletionCallback; |
13 class Module; | 16 class Module; |
14 | 17 |
15 // Simple wrapper around the PPB_Core interface. Some of these wrappers add | 18 /// APIs related to memory management, time, and threads. |
16 // nothing over the C interface, but some allow the use of C++ arguments. | |
17 class Core { | 19 class Core { |
18 public: | 20 public: |
19 // Note that we explicitly don't expose Resource& versions of this function | 21 // Note that we explicitly don't expose Resource& versions of this function |
20 // since Resource will normally manage the refcount properly. These should | 22 // since Resource will normally manage the refcount properly. These should |
21 // be called only when doing manual management on raw PP_Resource handles, | 23 // be called only when doing manual management on raw PP_Resource handles, |
22 // which should be fairly rare. | 24 // which should be fairly rare. |
| 25 |
| 26 /// A function that increments the reference count for the provided resource. |
| 27 /// |
| 28 /// @param[in] resource A PP_Resource containing the resource. |
23 void AddRefResource(PP_Resource resource) { | 29 void AddRefResource(PP_Resource resource) { |
24 interface_->AddRefResource(resource); | 30 interface_->AddRefResource(resource); |
25 } | 31 } |
| 32 |
| 33 /// A function that decrements the reference count for the provided resource. |
| 34 /// The resource will be deallocated if the reference count reaches zero. |
| 35 /// |
| 36 /// @param[in] resource A PP_Resource containing the resource. |
26 void ReleaseResource(PP_Resource resource) { | 37 void ReleaseResource(PP_Resource resource) { |
27 interface_->ReleaseResource(resource); | 38 interface_->ReleaseResource(resource); |
28 } | 39 } |
29 | 40 |
| 41 /// A function that allocates memory. |
| 42 /// |
| 43 /// @param[in] @param[in] num_bytes A number of bytes to allocate. |
| 44 /// @return A pointer to the memory if successful, NULL If the |
| 45 /// allocation fails. |
30 void* MemAlloc(uint32_t num_bytes) { | 46 void* MemAlloc(uint32_t num_bytes) { |
31 return interface_->MemAlloc(num_bytes); | 47 return interface_->MemAlloc(num_bytes); |
32 } | 48 } |
| 49 |
| 50 /// A function that deallocates memory. |
| 51 /// |
| 52 /// @param[in] ptr A pointer to the memory to deallocate. It is safe to |
| 53 /// pass NULL to this function. |
33 void MemFree(void* ptr) { | 54 void MemFree(void* ptr) { |
34 interface_->MemFree(ptr); | 55 interface_->MemFree(ptr); |
35 } | 56 } |
36 | 57 |
| 58 /// A function that that returns the "wall clock time" according to the |
| 59 /// browser. |
| 60 /// |
| 61 /// @return A PP_Time containing the "wall clock time" according to the |
| 62 /// browser. |
37 PP_Time GetTime() { | 63 PP_Time GetTime() { |
38 return interface_->GetTime(); | 64 return interface_->GetTime(); |
39 } | 65 } |
40 | 66 |
| 67 /// A function that that returns the "tick time" according to the browser. |
| 68 /// This clock is used by the browser when passing some event times to the |
| 69 /// plugin (e.g., via the PP_InputEvent::time_stamp_seconds field). It is |
| 70 /// not correlated to any actual wall clock time (like GetTime()). Because |
| 71 /// of this, it will not change if the user changes their computer clock. |
| 72 /// |
| 73 /// @return A PP_TimeTicks containing the "tick time" according to the |
| 74 /// browser. |
41 PP_TimeTicks GetTimeTicks() { | 75 PP_TimeTicks GetTimeTicks() { |
42 return interface_->GetTimeTicks(); | 76 return interface_->GetTimeTicks(); |
43 } | 77 } |
44 | 78 |
| 79 /// A function that schedules work to be executed on the main pepper thread |
| 80 /// after the specified delay. The delay may be 0 to specify a call back as |
| 81 /// soon as possible. |
| 82 /// |
| 83 /// The |result| parameter will just be passed as the second argument to the |
| 84 /// callback. Many applications won't need this, but it allows a plugin to |
| 85 /// emulate calls of some callbacks which do use this value. |
| 86 /// |
| 87 /// NOTE: CallOnMainThread, even when used from the main thread with a delay |
| 88 /// of 0 milliseconds, will never directly invoke the callback. Even in this |
| 89 /// case, the callback will be scheduled asynchronously. |
| 90 /// |
| 91 /// NOTE: If the browser is shutting down or if the plugin has no instances, |
| 92 /// then the callback function may not be called. |
| 93 /// |
| 94 /// @param[in] delay_in_milliseconds An int32_t delay in milliseconds. |
| 95 /// @param[in] callback A CompletionCallback callback function that the |
| 96 /// browser will call after the specified delay. |
| 97 /// @param[in] result An int32_t that the browser will pass to the given |
| 98 /// CompletionCallback. |
45 void CallOnMainThread(int32_t delay_in_milliseconds, | 99 void CallOnMainThread(int32_t delay_in_milliseconds, |
46 const CompletionCallback& callback, | 100 const CompletionCallback& callback, |
47 int32_t result = 0); | 101 int32_t result = 0); |
48 | 102 |
| 103 |
| 104 /// A function that returns true if the current thread is the main pepper |
| 105 /// thread. |
| 106 /// |
| 107 /// This function is useful for implementing sanity checks, and deciding if |
| 108 /// dispatching using CallOnMainThread() is required. |
| 109 /// |
| 110 /// @return A PP_BOOL containing PP_TRUE if the current thread is the main |
| 111 /// pepper thread, otherwise PP_FALSE. |
49 bool IsMainThread(); | 112 bool IsMainThread(); |
50 | 113 |
51 private: | 114 private: |
52 // Allow Module to construct. | 115 // Allow Module to construct. |
53 friend class Module; | 116 friend class Module; |
54 | 117 |
55 // Only module should make this class so this constructor is private. | 118 // Only module should make this class so this constructor is private. |
56 Core(const PPB_Core* inter) : interface_(inter) {} | 119 Core(const PPB_Core* inter) : interface_(inter) {} |
57 | 120 |
58 // Copy and assignment are disallowed. | 121 // Copy and assignment are disallowed. |
59 Core(const Core& other); | 122 Core(const Core& other); |
60 Core& operator=(const Core& other); | 123 Core& operator=(const Core& other); |
61 | 124 |
62 const PPB_Core* interface_; | 125 const PPB_Core* interface_; |
63 }; | 126 }; |
64 | 127 |
65 } // namespace pp | 128 } // namespace pp |
66 | 129 |
67 #endif // PPAPI_CPP_CORE_H_ | 130 #endif // PPAPI_CPP_CORE_H_ |
OLD | NEW |