[email protected] | 25651387 | 2012-01-05 15:41:52 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d989cce | 2011-12-08 04:51:14 | [diff] [blame] | 5 | #include "ppapi/shared_impl/ppb_var_shared.h" |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 6 | |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 9 | #include "ppapi/c/ppb_var.h" |
[email protected] | 9c6e0de | 2012-01-27 04:55:55 | [diff] [blame] | 10 | #include "ppapi/c/ppb_var_array_buffer.h" |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 11 | #include "ppapi/c/pp_var.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 12 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 67c125d | 2011-10-11 18:58:22 | [diff] [blame] | 13 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 4d5a4e0 | 2014-02-13 14:50:00 | [diff] [blame] | 14 | #include "ppapi/shared_impl/resource_tracker.h" |
| 15 | #include "ppapi/shared_impl/resource_var.h" |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 16 | #include "ppapi/shared_impl/var.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 17 | #include "ppapi/shared_impl/var_tracker.h" |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 18 | |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 19 | using ppapi::PpapiGlobals; |
| 20 | using ppapi::StringVar; |
| 21 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 22 | namespace ppapi { |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 23 | namespace { |
| 24 | |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 25 | // PPB_Var methods ------------------------------------------------------------- |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 26 | |
| 27 | void AddRefVar(PP_Var var) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 28 | ProxyAutoLock lock; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 29 | PpapiGlobals::Get()->GetVarTracker()->AddRefVar(var); |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void ReleaseVar(PP_Var var) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 33 | ProxyAutoLock lock; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 34 | PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var); |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 37 | PP_Var VarFromUtf8(const char* data, uint32_t len) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 38 | ProxyAutoLock lock; |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 39 | return StringVar::StringToPPVar(data, len); |
| 40 | } |
| 41 | |
| 42 | PP_Var VarFromUtf8_1_0(PP_Module /*module*/, const char* data, uint32_t len) { |
| 43 | return VarFromUtf8(data, len); |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const char* VarToUtf8(PP_Var var, uint32_t* len) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 47 | ProxyAutoLock lock; |
[email protected] | 28cfaed0 | 2011-08-22 22:15:58 | [diff] [blame] | 48 | StringVar* str = StringVar::FromPPVar(var); |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 49 | if (str) { |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 50 | *len = static_cast<uint32_t>(str->value().size()); |
| 51 | return str->value().c_str(); |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 52 | } |
| 53 | *len = 0; |
| 54 | return NULL; |
| 55 | } |
| 56 | |
[email protected] | 4d5a4e0 | 2014-02-13 14:50:00 | [diff] [blame] | 57 | PP_Resource VarToResource(PP_Var var) { |
| 58 | ProxyAutoLock lock; |
| 59 | ResourceVar* resource = ResourceVar::FromPPVar(var); |
| 60 | if (!resource) |
| 61 | return 0; |
| 62 | PP_Resource pp_resource = resource->GetPPResource(); |
| 63 | PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource); |
| 64 | return pp_resource; |
| 65 | } |
| 66 | |
| 67 | PP_Var VarFromResource(PP_Resource resource) { |
| 68 | ProxyAutoLock lock; |
| 69 | return PpapiGlobals::Get()->GetVarTracker()->MakeResourcePPVar(resource); |
| 70 | } |
| 71 | |
[email protected] | 665b5c54 | 2014-02-22 08:06:26 | [diff] [blame] | 72 | const PPB_Var var_interface = {&AddRefVar, &ReleaseVar, &VarFromUtf8, |
| 73 | &VarToUtf8, &VarToResource, &VarFromResource}; |
[email protected] | 4d5a4e0 | 2014-02-13 14:50:00 | [diff] [blame] | 74 | |
[email protected] | 665b5c54 | 2014-02-22 08:06:26 | [diff] [blame] | 75 | const PPB_Var_1_1 var_interface1_1 = {&AddRefVar, &ReleaseVar, |
| 76 | &VarFromUtf8, &VarToUtf8}; |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 77 | |
[email protected] | 665b5c54 | 2014-02-22 08:06:26 | [diff] [blame] | 78 | const PPB_Var_1_0 var_interface1_0 = {&AddRefVar, &ReleaseVar, |
| 79 | &VarFromUtf8_1_0, &VarToUtf8}; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 80 | |
[email protected] | 9c6e0de | 2012-01-27 04:55:55 | [diff] [blame] | 81 | // PPB_VarArrayBuffer methods -------------------------------------------------- |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 82 | |
| 83 | PP_Var CreateArrayBufferVar(uint32_t size_in_bytes) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 84 | ProxyAutoLock lock; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 85 | return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| 86 | size_in_bytes); |
| 87 | } |
| 88 | |
[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 89 | PP_Bool ByteLength(PP_Var array, uint32_t* byte_length) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 90 | ProxyAutoLock lock; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 91 | ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); |
| 92 | if (!buffer) |
[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 93 | return PP_FALSE; |
| 94 | *byte_length = buffer->ByteLength(); |
| 95 | return PP_TRUE; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 25651387 | 2012-01-05 15:41:52 | [diff] [blame] | 98 | void* Map(PP_Var array) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 99 | ProxyAutoLock lock; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 100 | ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); |
| 101 | if (!buffer) |
| 102 | return NULL; |
| 103 | return buffer->Map(); |
| 104 | } |
| 105 | |
[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 106 | void Unmap(PP_Var array) { |
[email protected] | 712835ff | 2013-02-26 04:54:22 | [diff] [blame] | 107 | ProxyAutoLock lock; |
[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 108 | ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(array); |
| 109 | if (buffer) |
| 110 | buffer->Unmap(); |
| 111 | } |
| 112 | |
[email protected] | 9c6e0de | 2012-01-27 04:55:55 | [diff] [blame] | 113 | const PPB_VarArrayBuffer_1_0 var_arraybuffer_interface = { |
[email protected] | 665b5c54 | 2014-02-22 08:06:26 | [diff] [blame] | 114 | &CreateArrayBufferVar, &ByteLength, &Map, &Unmap}; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 115 | |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 116 | } // namespace |
| 117 | |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 118 | // static |
[email protected] | 4d5a4e0 | 2014-02-13 14:50:00 | [diff] [blame] | 119 | const PPB_Var_1_2* PPB_Var_Shared::GetVarInterface1_2() { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 120 | return &var_interface; |
[email protected] | 0925622c | 2011-06-08 20:22:02 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 123 | // static |
[email protected] | 4d5a4e0 | 2014-02-13 14:50:00 | [diff] [blame] | 124 | const PPB_Var_1_1* PPB_Var_Shared::GetVarInterface1_1() { |
| 125 | return &var_interface1_1; |
| 126 | } |
| 127 | |
| 128 | // static |
[email protected] | d989cce | 2011-12-08 04:51:14 | [diff] [blame] | 129 | const PPB_Var_1_0* PPB_Var_Shared::GetVarInterface1_0() { |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 130 | return &var_interface1_0; |
| 131 | } |
| 132 | |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 133 | // static |
[email protected] | 9c6e0de | 2012-01-27 04:55:55 | [diff] [blame] | 134 | const PPB_VarArrayBuffer_1_0* PPB_Var_Shared::GetVarArrayBufferInterface1_0() { |
| 135 | return &var_arraybuffer_interface; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 138 | } // namespace ppapi |