OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 #include "ppapi/cpp/dev/var_array_buffer_dev.h" | 5 #include "ppapi/cpp/dev/var_array_buffer_dev.h" |
6 | 6 |
7 #include <limits> | |
8 | |
7 #include "ppapi/c/dev/ppb_var_array_buffer_dev.h" | 9 #include "ppapi/c/dev/ppb_var_array_buffer_dev.h" |
8 #include "ppapi/cpp/logging.h" | 10 #include "ppapi/cpp/logging.h" |
9 #include "ppapi/cpp/module_impl.h" | 11 #include "ppapi/cpp/module_impl.h" |
10 | 12 |
11 namespace pp { | 13 namespace pp { |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 template <> const char* interface_name<PPB_VarArrayBuffer_Dev>() { | 17 template <> const char* interface_name<PPB_VarArrayBuffer_Dev>() { |
16 return PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE; | 18 return PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE; |
17 } | 19 } |
18 | 20 |
19 } // namespace | 21 } // namespace |
20 | 22 |
21 VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var) | 23 VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var) : Var(var) { |
22 : Var(var), buffer_(NULL) { | 24 if (!var.is_array_buffer()) { |
23 if (var.is_array_buffer()) { | |
24 buffer_ = Map(); | |
25 } else { | |
26 PP_NOTREACHED(); | 25 PP_NOTREACHED(); |
27 var_ = PP_MakeNull(); | 26 var_ = PP_MakeNull(); |
28 } | 27 } |
29 } | 28 } |
30 | 29 |
31 VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes) | 30 VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes) { |
32 : buffer_(NULL) { | |
33 if (has_interface<PPB_VarArrayBuffer_Dev>()) { | 31 if (has_interface<PPB_VarArrayBuffer_Dev>()) { |
34 var_ = get_interface<PPB_VarArrayBuffer_Dev>()->Create(size_in_bytes); | 32 var_ = get_interface<PPB_VarArrayBuffer_Dev>()->Create(size_in_bytes); |
35 needs_release_ = true; | 33 needs_release_ = true; |
36 buffer_ = Map(); | |
37 } else { | 34 } else { |
38 PP_NOTREACHED(); | 35 PP_NOTREACHED(); |
39 var_ = PP_MakeNull(); | 36 var_ = PP_MakeNull(); |
40 } | 37 } |
41 } | 38 } |
42 | 39 |
43 uint32_t VarArrayBuffer_Dev::ByteLength() const { | 40 uint32_t VarArrayBuffer_Dev::ByteLength() const { |
44 if (has_interface<PPB_VarArrayBuffer_Dev>()) { | 41 uint32_t byte_length = std::numeric_limits<uint32_t>::max(); |
45 return get_interface<PPB_VarArrayBuffer_Dev>()->ByteLength(var_); | 42 if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>()) |
46 } | 43 get_interface<PPB_VarArrayBuffer_Dev>()->ByteLength(var_, &byte_length); |
47 PP_NOTREACHED(); | 44 else |
48 return 0; | 45 PP_NOTREACHED(); |
49 } | 46 return byte_length; |
50 | |
51 const void* VarArrayBuffer_Dev::Map() const { | |
52 return const_cast<VarArrayBuffer_Dev*>(this)->Map(); | |
53 } | 47 } |
54 | 48 |
55 void* VarArrayBuffer_Dev::Map() { | 49 void* VarArrayBuffer_Dev::Map() { |
56 // TODO(dmichael): This is not thread-safe. | 50 if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>()) |
57 if (buffer_) | 51 return get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_); |
58 return buffer_; | |
59 if (has_interface<PPB_VarArrayBuffer_Dev>()) { | |
60 buffer_ = get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_); | |
61 return buffer_; | |
62 } | |
63 PP_NOTREACHED(); | 52 PP_NOTREACHED(); |
64 return 0; | 53 return NULL; |
54 } | |
55 | |
56 void VarArrayBuffer_Dev::Unmap() { | |
57 if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>()) | |
58 get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_); | |
bbudge
2012/01/25 22:19:07
Shouldn't this call UnMap?
dmichael (off chromium)
2012/01/25 23:50:11
Unmap is only an optimization for apps that want t
| |
59 else | |
60 PP_NOTREACHED(); | |
65 } | 61 } |
66 | 62 |
67 } // namespace pp | 63 } // namespace pp |
OLD | NEW |