Use Persistent::Reset.
[chromium-blink-merge.git] / chromeos / network / certificate_pattern.cc
blobf2048e07a3102122531087bc6eebdccff5b38c4d
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"
10 namespace chromeos {
12 namespace {
14 // Keys for converting classes below to/from dictionaries.
15 const char kCommonNameKey[] = "CommonName";
16 const char kLocalityKey[] = "Locality";
17 const char kOrganizationKey[] = "Organization";
18 const char kOrganizationalUnitKey[] = "OrganizationalUnit";
19 const char kIssuerCaRefKey[] = "IssuerCARef";
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 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_ref_list_.empty() &&
118 issuer_.Empty() &&
119 subject_.Empty();
122 void CertificatePattern::Clear() {
123 issuer_ca_ref_list_.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_ref_list_.empty())
133 dict->Set(kIssuerCaRefKey, CreateListFromStrings(issuer_ca_ref_list_));
135 if (!issuer_.Empty())
136 dict->Set(kIssuerKey, issuer_.CreateAsDictionary());
138 if (!subject_.Empty())
139 dict->Set(kSubjectKey, subject_.CreateAsDictionary());
141 if (!enrollment_uri_list_.empty())
142 dict->Set(kEnrollmentUriKey, CreateListFromStrings(enrollment_uri_list_));
143 return dict;
146 bool CertificatePattern::CopyFromDictionary(const base::DictionaryValue &dict) {
147 const base::DictionaryValue* child_dict = NULL;
148 const base::ListValue* child_list = NULL;
149 Clear();
151 // All of these are optional.
152 if (dict.GetList(kIssuerCaRefKey, &child_list) && child_list) {
153 if (!GetAsListOfStrings(*child_list, &issuer_ca_ref_list_))
154 return false;
156 if (dict.GetDictionary(kIssuerKey, &child_dict) && child_dict) {
157 if (!issuer_.CopyFromDictionary(*child_dict))
158 return false;
160 child_dict = NULL;
161 if (dict.GetDictionary(kSubjectKey, &child_dict) && child_dict) {
162 if (!subject_.CopyFromDictionary(*child_dict))
163 return false;
165 child_list = NULL;
166 if (dict.GetList(kEnrollmentUriKey, &child_list) && child_list) {
167 if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
168 return false;
171 // If we didn't copy anything from the dictionary, then it had better be
172 // empty.
173 DCHECK(dict.empty() == Empty());
174 if (dict.empty() != Empty())
175 return false;
177 return true;
180 } // namespace chromeos