OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/private/network_list_private.h" | 5 #include "ppapi/cpp/private/network_list_private.h" |
6 | 6 |
7 #include "ppapi/cpp/array_output.h" | |
8 #include "ppapi/cpp/logging.h" | |
7 #include "ppapi/cpp/module_impl.h" | 9 #include "ppapi/cpp/module_impl.h" |
10 #include "ppapi/cpp/net_address.h" | |
8 #include "ppapi/cpp/var.h" | 11 #include "ppapi/cpp/var.h" |
9 | 12 |
10 namespace pp { | 13 namespace pp { |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 template <> const char* interface_name<PPB_NetworkList_Private>() { | 17 template <> const char* interface_name<PPB_NetworkList_Private>() { |
yzshen1
2013/09/03 17:47:32
nit: It is preferred to append the version name ex
Sergey Ulanov
2013/09/03 23:42:54
Done.
| |
15 return PPB_NETWORKLIST_PRIVATE_INTERFACE; | 18 return PPB_NETWORKLIST_PRIVATE_INTERFACE; |
yzshen1
2013/09/03 17:47:32
ditto.
Sergey Ulanov
2013/09/03 23:42:54
Done.
| |
16 } | 19 } |
17 | 20 |
21 // Used in GetIpAddresses(). | |
yzshen1
2013/09/03 17:47:32
maybe this could be removed?
Sergey Ulanov
2013/09/03 23:42:54
Yes. I added it before I found ResourceArrayOutput
| |
22 struct ResourceArray { | |
23 PP_Resource* resources; | |
24 int count; | |
25 }; | |
26 | |
27 void* AllocateResourceArray(void* data, uint32_t count, uint32_t size) { | |
28 ResourceArray* resource_array = reinterpret_cast<ResourceArray*>(data); | |
29 PP_DCHECK(size == sizeof(PP_Resource)); | |
30 if (size) { | |
31 resource_array->resources = new PP_Resource[count]; | |
32 if (resource_array->resources) | |
33 resource_array->count = count; | |
34 } else { | |
35 resource_array->resources = NULL; | |
36 } | |
37 return resource_array->resources; | |
38 } | |
39 | |
18 } // namespace | 40 } // namespace |
19 | 41 |
20 NetworkListPrivate::NetworkListPrivate() { | 42 NetworkListPrivate::NetworkListPrivate() { |
21 } | 43 } |
22 | 44 |
23 NetworkListPrivate::NetworkListPrivate(PassRef, PP_Resource resource) | 45 NetworkListPrivate::NetworkListPrivate(PassRef, PP_Resource resource) |
24 : Resource(PASS_REF, resource) { | 46 : Resource(PASS_REF, resource) { |
25 } | 47 } |
26 | 48 |
27 // static | 49 // static |
(...skipping 25 matching lines...) Expand all Loading... | |
53 | 75 |
54 PP_NetworkListState_Private NetworkListPrivate::GetState(uint32_t index) const { | 76 PP_NetworkListState_Private NetworkListPrivate::GetState(uint32_t index) const { |
55 if (!has_interface<PPB_NetworkList_Private>()) | 77 if (!has_interface<PPB_NetworkList_Private>()) |
56 return PP_NETWORKLIST_DOWN; | 78 return PP_NETWORKLIST_DOWN; |
57 return get_interface<PPB_NetworkList_Private>()->GetState( | 79 return get_interface<PPB_NetworkList_Private>()->GetState( |
58 pp_resource(), index); | 80 pp_resource(), index); |
59 } | 81 } |
60 | 82 |
61 void NetworkListPrivate::GetIpAddresses( | 83 void NetworkListPrivate::GetIpAddresses( |
62 uint32_t index, | 84 uint32_t index, |
63 std::vector<PP_NetAddress_Private>* addresses) const { | 85 std::vector<NetAddress>* addresses) const { |
64 if (!has_interface<PPB_NetworkList_Private>()) | 86 if (!has_interface<PPB_NetworkList_Private>()) |
65 return; | 87 return; |
66 | 88 |
yzshen1
2013/09/03 17:47:32
nit: check NULL for |addresses|?
Sergey Ulanov
2013/09/03 23:42:54
Done.
| |
67 // Most network interfaces don't have more than 3 network | 89 ResourceArrayOutputAdapter<NetAddress> adapter(addresses); |
68 // interfaces. | 90 get_interface<PPB_NetworkList_Private>()->GetIpAddresses( |
69 addresses->resize(3); | 91 pp_resource(), index, adapter.pp_array_output()); |
70 | |
71 int32_t result = get_interface<PPB_NetworkList_Private>()->GetIpAddresses( | |
72 pp_resource(), index, &addresses->front(), addresses->size()); | |
73 | |
74 if (result < 0) { | |
75 addresses->resize(0); | |
76 return; | |
77 } | |
78 | |
79 if (result <= static_cast<int32_t>(addresses->size())) { | |
80 addresses->resize(result); | |
81 return; | |
82 } | |
83 | |
84 addresses->resize(result); | |
85 result = get_interface<PPB_NetworkList_Private>()->GetIpAddresses( | |
86 pp_resource(), index, &addresses->front(), addresses->size()); | |
87 if (result < 0) { | |
88 addresses->resize(0); | |
89 } else if (result < static_cast<int32_t>(addresses->size())) { | |
90 addresses->resize(result); | |
91 } | |
92 } | 92 } |
93 | 93 |
94 std::string NetworkListPrivate::GetDisplayName(uint32_t index) const { | 94 std::string NetworkListPrivate::GetDisplayName(uint32_t index) const { |
95 if (!has_interface<PPB_NetworkList_Private>()) | 95 if (!has_interface<PPB_NetworkList_Private>()) |
96 return std::string(); | 96 return std::string(); |
97 Var result(PASS_REF, | 97 Var result(PASS_REF, |
98 get_interface<PPB_NetworkList_Private>()->GetDisplayName( | 98 get_interface<PPB_NetworkList_Private>()->GetDisplayName( |
99 pp_resource(), index)); | 99 pp_resource(), index)); |
100 return result.is_string() ? result.AsString() : std::string(); | 100 return result.is_string() ? result.AsString() : std::string(); |
101 } | 101 } |
102 | 102 |
103 uint32_t NetworkListPrivate::GetMTU(uint32_t index) const { | 103 uint32_t NetworkListPrivate::GetMTU(uint32_t index) const { |
104 if (!has_interface<PPB_NetworkList_Private>()) | 104 if (!has_interface<PPB_NetworkList_Private>()) |
105 return 0; | 105 return 0; |
106 return get_interface<PPB_NetworkList_Private>()->GetMTU( | 106 return get_interface<PPB_NetworkList_Private>()->GetMTU(pp_resource(), index); |
107 pp_resource(), index); | |
108 } | 107 } |
109 | 108 |
110 } // namespace pp | 109 } // namespace pp |
OLD | NEW |