Don't kill key events even when it's VKEY_UNKNOWN.
[chromium-blink-merge.git] / chromeos / network / certificate_pattern.cc
blobdf8c241be2df82576090f1533fd92115d3d8e802
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 "chromeos/network/certificate_pattern.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/onc/onc_constants.h"
11 namespace chromeos {
13 namespace {
15 // Keys for converting classes below to/from dictionaries.
16 const char kCommonNameKey[] = "CommonName";
17 const char kLocalityKey[] = "Locality";
18 const char kOrganizationKey[] = "Organization";
19 const char kOrganizationalUnitKey[] = "OrganizationalUnit";
20 const char kIssuerKey[] = "Issuer";
21 const char kSubjectKey[] = "Subject";
22 const char kEnrollmentUriKey[] = "EnrollmentURI";
24 bool GetAsListOfStrings(const base::Value& value,
25 std::vector<std::string>* result) {
26 const base::ListValue* list = NULL;
27 if (!value.GetAsList(&list))
28 return false;
29 result->clear();
30 result->reserve(list->GetSize());
31 for (size_t i = 0; i < list->GetSize(); i++) {
32 std::string item;
33 if (!list->GetString(i, &item))
34 return false;
35 result->push_back(item);
37 return true;
40 base::ListValue* CreateListFromStrings(
41 const std::vector<std::string>& strings) {
42 base::ListValue* new_list = new base::ListValue;
43 for (std::vector<std::string>::const_iterator iter = strings.begin();
44 iter != strings.end(); ++iter) {
45 new_list->Append(new base::StringValue(*iter));
47 return new_list;
50 } // namespace
52 ////////////////////////////////////////////////////////////////////////////////
53 // IssuerSubjectPattern
54 IssuerSubjectPattern::IssuerSubjectPattern(const std::string& common_name,
55 const std::string& locality,
56 const std::string& organization,
57 const std::string& organizational_unit)
58 : common_name_(common_name),
59 locality_(locality),
60 organization_(organization),
61 organizational_unit_(organizational_unit) { }
63 IssuerSubjectPattern::IssuerSubjectPattern() {}
65 IssuerSubjectPattern::~IssuerSubjectPattern() {}
67 bool IssuerSubjectPattern::Empty() const {
68 return common_name_.empty() &&
69 locality_.empty() &&
70 organization_.empty() &&
71 organizational_unit_.empty();
74 void IssuerSubjectPattern::Clear() {
75 common_name_.clear();
76 locality_.clear();
77 organization_.clear();
78 organizational_unit_.clear();
81 base::DictionaryValue* IssuerSubjectPattern::CreateAsDictionary() const {
82 base::DictionaryValue* dict = new base::DictionaryValue;
83 if (!common_name_.empty())
84 dict->SetString(kCommonNameKey, common_name_);
85 if (!locality_.empty())
86 dict->SetString(kLocalityKey, locality_);
87 if (!organization_.empty())
88 dict->SetString(kOrganizationKey, organization_);
89 if (!organizational_unit_.empty())
90 dict->SetString(kOrganizationalUnitKey, organizational_unit_);
91 return dict;
94 bool IssuerSubjectPattern::CopyFromDictionary(
95 const base::DictionaryValue& dict) {
96 Clear();
97 dict.GetString(kCommonNameKey, &common_name_);
98 dict.GetString(kLocalityKey, &locality_);
99 dict.GetString(kOrganizationKey, &organization_);
100 dict.GetString(kOrganizationalUnitKey, &organizational_unit_);
101 // If the dictionary wasn't empty, but we are, or vice versa, then something
102 // went wrong.
103 DCHECK(dict.empty() == Empty());
104 if (dict.empty() != Empty())
105 return false;
106 return true;
109 ////////////////////////////////////////////////////////////////////////////////
110 // CertificatePattern
112 CertificatePattern::CertificatePattern() {}
114 CertificatePattern::~CertificatePattern() {}
116 bool CertificatePattern::Empty() const {
117 return issuer_ca_pems_.empty() &&
118 issuer_.Empty() &&
119 subject_.Empty();
122 void CertificatePattern::Clear() {
123 issuer_ca_pems_.clear();
124 issuer_.Clear();
125 subject_.Clear();
126 enrollment_uri_list_.clear();
129 base::DictionaryValue* CertificatePattern::CreateAsDictionary() const {
130 base::DictionaryValue* dict = new base::DictionaryValue;
132 if (!issuer_ca_pems_.empty()) {
133 dict->Set(onc::certificate::kIssuerCAPEMs,
134 CreateListFromStrings(issuer_ca_pems_));
137 if (!issuer_.Empty())
138 dict->Set(kIssuerKey, issuer_.CreateAsDictionary());
140 if (!subject_.Empty())
141 dict->Set(kSubjectKey, subject_.CreateAsDictionary());
143 if (!enrollment_uri_list_.empty())
144 dict->Set(kEnrollmentUriKey, CreateListFromStrings(enrollment_uri_list_));
145 return dict;
148 bool CertificatePattern::CopyFromDictionary(const base::DictionaryValue &dict) {
149 const base::DictionaryValue* child_dict = NULL;
150 const base::ListValue* child_list = NULL;
151 Clear();
153 // All of these are optional.
154 if (dict.GetList(onc::certificate::kIssuerCAPEMs, &child_list) &&
155 child_list) {
156 if (!GetAsListOfStrings(*child_list, &issuer_ca_pems_))
157 return false;
159 if (dict.GetDictionary(kIssuerKey, &child_dict) && child_dict) {
160 if (!issuer_.CopyFromDictionary(*child_dict))
161 return false;
163 child_dict = NULL;
164 if (dict.GetDictionary(kSubjectKey, &child_dict) && child_dict) {
165 if (!subject_.CopyFromDictionary(*child_dict))
166 return false;
168 child_list = NULL;
169 if (dict.GetList(kEnrollmentUriKey, &child_list) && child_list) {
170 if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
171 return false;
174 // If we didn't copy anything from the dictionary, then it had better be
175 // empty.
176 DCHECK(dict.empty() == Empty());
177 if (dict.empty() != Empty())
178 return false;
180 return true;
183 } // namespace chromeos