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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/private/ppb_x509_certificate_private_shared.h"
7 #include "base/logging.h"
8 #include "ppapi/shared_impl/ppapi_globals.h"
9 #include "ppapi/shared_impl/var.h"
10 #include "ppapi/shared_impl/var_tracker.h"
14 PPB_X509Certificate_Fields::PPB_X509Certificate_Fields() {}
16 PPB_X509Certificate_Fields::PPB_X509Certificate_Fields(
17 const PPB_X509Certificate_Fields
& fields
) {
18 scoped_ptr
<base::ListValue
> new_values(fields
.values_
.DeepCopy());
19 values_
.Swap(new_values
.get());
22 void PPB_X509Certificate_Fields::SetField(
23 PP_X509Certificate_Private_Field field
,
25 uint32_t index
= static_cast<uint32_t>(field
);
26 bool success
= values_
.Set(index
, value
);
30 PP_Var
PPB_X509Certificate_Fields::GetFieldAsPPVar(
31 PP_X509Certificate_Private_Field field
) const {
32 uint32_t index
= static_cast<uint32_t>(field
);
33 const base::Value
* value
;
34 bool success
= values_
.Get(index
, &value
);
36 // Our list received might be smaller than the number of fields, so just
37 // return null if the index is OOB.
41 switch (value
->GetType()) {
42 case base::Value::TYPE_NULL
:
44 case base::Value::TYPE_BOOLEAN
: {
46 value
->GetAsBoolean(&val
);
47 return PP_MakeBool(PP_FromBool(val
));
49 case base::Value::TYPE_INTEGER
: {
51 value
->GetAsInteger(&val
);
52 return PP_MakeInt32(val
);
54 case base::Value::TYPE_DOUBLE
: {
56 value
->GetAsDouble(&val
);
57 return PP_MakeDouble(val
);
59 case base::Value::TYPE_STRING
: {
61 value
->GetAsString(&val
);
62 return StringVar::StringToPPVar(val
);
64 case base::Value::TYPE_BINARY
: {
65 const base::BinaryValue
* binary
=
66 static_cast<const base::BinaryValue
*>(value
);
67 uint32_t size
= static_cast<uint32_t>(binary
->GetSize());
68 const char* buffer
= binary
->GetBuffer();
70 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(size
,
74 case base::Value::TYPE_DICTIONARY
:
75 case base::Value::TYPE_LIST
:
80 // Should not reach here.
82 return PP_MakeUndefined();
85 //------------------------------------------------------------------------------
87 PPB_X509Certificate_Private_Shared::PPB_X509Certificate_Private_Shared(
88 ResourceObjectType type
,
90 : Resource(type
, instance
) {}
92 PPB_X509Certificate_Private_Shared::PPB_X509Certificate_Private_Shared(
93 ResourceObjectType type
,
95 const PPB_X509Certificate_Fields
& fields
)
96 : Resource(type
, instance
),
97 fields_(new PPB_X509Certificate_Fields(fields
)) {
100 PPB_X509Certificate_Private_Shared::~PPB_X509Certificate_Private_Shared() {
103 thunk::PPB_X509Certificate_Private_API
*
104 PPB_X509Certificate_Private_Shared::AsPPB_X509Certificate_Private_API() {
108 PP_Bool
PPB_X509Certificate_Private_Shared::Initialize(const char* bytes
,
110 // The certificate should be immutable once initialized.
114 if (!bytes
|| length
== 0)
117 std::vector
<char> der(bytes
, bytes
+ length
);
118 scoped_ptr
<PPB_X509Certificate_Fields
> fields(
119 new PPB_X509Certificate_Fields());
120 bool success
= ParseDER(der
, fields
.get());
122 fields_
.swap(fields
);
128 PP_Var
PPB_X509Certificate_Private_Shared::GetField(
129 PP_X509Certificate_Private_Field field
) {
131 return PP_MakeUndefined();
133 return fields_
->GetFieldAsPPVar(field
);
136 bool PPB_X509Certificate_Private_Shared::ParseDER(
137 const std::vector
<char>& der
,
138 PPB_X509Certificate_Fields
* result
) {
139 // A concrete PPB_X509Certificate_Private_Shared should only ever be
140 // constructed by passing in PPB_X509Certificate_Fields, in which case it is
141 // already initialized.