| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "ppapi/shared_impl/private/ppb_flash_x509_certificate_shared.h" |
| 6 |
| 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" |
| 11 |
| 12 namespace ppapi { |
| 13 |
| 14 void PPB_X509Certificate_Fields::SetField(PP_Flash_X509Certificate_Field field, |
| 15 base::Value* value) { |
| 16 uint32_t index = static_cast<uint32_t>(field); |
| 17 bool success = values_.Set(index, value); |
| 18 DCHECK(success); |
| 19 } |
| 20 |
| 21 PP_Var PPB_X509Certificate_Fields::GetFieldAsPPVar( |
| 22 PP_Flash_X509Certificate_Field field) const { |
| 23 uint32_t index = static_cast<uint32_t>(field); |
| 24 base::Value* value; |
| 25 bool success = values_.Get(index, &value); |
| 26 if (!success) { |
| 27 // Our list received might be smaller than the number of fields, so just |
| 28 // return null if the index is OOB. |
| 29 return PP_MakeNull(); |
| 30 } |
| 31 |
| 32 switch (value->GetType()) { |
| 33 case Value::TYPE_NULL: |
| 34 return PP_MakeNull(); |
| 35 case Value::TYPE_BOOLEAN: { |
| 36 bool val; |
| 37 value->GetAsBoolean(&val); |
| 38 return PP_MakeBool(PP_FromBool(val)); |
| 39 } |
| 40 case Value::TYPE_INTEGER: { |
| 41 int val; |
| 42 value->GetAsInteger(&val); |
| 43 return PP_MakeInt32(val); |
| 44 } |
| 45 case Value::TYPE_DOUBLE: { |
| 46 double val; |
| 47 value->GetAsDouble(&val); |
| 48 return PP_MakeDouble(val); |
| 49 } |
| 50 case Value::TYPE_STRING: { |
| 51 std::string val; |
| 52 value->GetAsString(&val); |
| 53 return StringVar::StringToPPVar(val); |
| 54 } |
| 55 case Value::TYPE_BINARY: { |
| 56 const base::BinaryValue* binary = |
| 57 static_cast<const base::BinaryValue*>(value); |
| 58 uint32_t size = static_cast<uint32_t>(binary->GetSize()); |
| 59 const char* buffer = binary->GetBuffer(); |
| 60 PP_Var array_buffer = |
| 61 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(size, |
| 62 buffer); |
| 63 return array_buffer; |
| 64 } |
| 65 case Value::TYPE_DICTIONARY: |
| 66 case Value::TYPE_LIST: |
| 67 // Not handled. |
| 68 break; |
| 69 } |
| 70 |
| 71 // Should not reach here. |
| 72 CHECK(false); |
| 73 return PP_MakeUndefined(); |
| 74 } |
| 75 |
| 76 //------------------------------------------------------------------------------ |
| 77 |
| 78 PPB_Flash_X509Certificate_Shared::PPB_Flash_X509Certificate_Shared( |
| 79 ResourceObjectType type, |
| 80 PP_Instance instance) : |
| 81 Resource(type, instance), |
| 82 fields_(NULL) { |
| 83 } |
| 84 |
| 85 PPB_Flash_X509Certificate_Shared::PPB_Flash_X509Certificate_Shared( |
| 86 ResourceObjectType type, |
| 87 PP_Instance instance, |
| 88 PPB_X509Certificate_Fields* fields) : |
| 89 Resource(type, instance), |
| 90 fields_(fields) |
| 91 { |
| 92 } |
| 93 |
| 94 PPB_Flash_X509Certificate_Shared::~PPB_Flash_X509Certificate_Shared() { |
| 95 } |
| 96 |
| 97 thunk::PPB_Flash_X509Certificate_API* |
| 98 PPB_Flash_X509Certificate_Shared::AsPPB_Flash_X509Certificate_API() { |
| 99 return this; |
| 100 } |
| 101 |
| 102 PP_Bool PPB_Flash_X509Certificate_Shared::Initialize(const char* bytes, |
| 103 uint32_t length) { |
| 104 // The certificate should be immutable once initialized. |
| 105 if (fields_.get()) |
| 106 return PP_FALSE; |
| 107 |
| 108 if (!bytes || length == 0) |
| 109 return PP_FALSE; |
| 110 |
| 111 std::vector<char> der(bytes, bytes + length); |
| 112 scoped_ptr<PPB_X509Certificate_Fields> fields( |
| 113 new PPB_X509Certificate_Fields()); |
| 114 bool success = ParseDER(der, fields.get()); |
| 115 if (success) { |
| 116 fields_.swap(fields); |
| 117 return PP_TRUE; |
| 118 } |
| 119 return PP_FALSE; |
| 120 } |
| 121 |
| 122 PP_Var PPB_Flash_X509Certificate_Shared::GetField( |
| 123 PP_Flash_X509Certificate_Field field) { |
| 124 if (!fields_.get()) |
| 125 return PP_MakeUndefined(); |
| 126 |
| 127 return fields_->GetFieldAsPPVar(field); |
| 128 } |
| 129 |
| 130 bool PPB_Flash_X509Certificate_Shared::ParseDER( |
| 131 const std::vector<char>& der, |
| 132 PPB_X509Certificate_Fields* result) { |
| 133 // A concrete PPB_Flash_X509Certificate_Shared should only ever be constructed |
| 134 // by passing in PPB_X509Certificate_Fields, in which case it is already |
| 135 // initialized. |
| 136 CHECK(false); |
| 137 return false; |
| 138 } |
| 139 |
| 140 } // namespace ppapi |
| OLD | NEW |