- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / network / certificate_pattern.cc
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 "chromeos/network/certificate_pattern.h"
6
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/onc/onc_constants.h"
10
11 namespace chromeos {
12
13 namespace {
14
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";
23
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);
36   }
37   return true;
38 }
39
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));
46   }
47   return new_list;
48 }
49
50 }  // namespace
51
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) { }
62
63 IssuerSubjectPattern::IssuerSubjectPattern() {}
64
65 IssuerSubjectPattern::~IssuerSubjectPattern() {}
66
67 bool IssuerSubjectPattern::Empty() const {
68   return common_name_.empty() &&
69          locality_.empty() &&
70          organization_.empty() &&
71          organizational_unit_.empty();
72 }
73
74 void IssuerSubjectPattern::Clear() {
75   common_name_.clear();
76   locality_.clear();
77   organization_.clear();
78   organizational_unit_.clear();
79 }
80
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;
92 }
93
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;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////
110 // CertificatePattern
111
112 CertificatePattern::CertificatePattern() {}
113
114 CertificatePattern::~CertificatePattern() {}
115
116 bool CertificatePattern::Empty() const {
117   return issuer_ca_pems_.empty() &&
118          issuer_.Empty() &&
119          subject_.Empty();
120 }
121
122 void CertificatePattern::Clear() {
123   issuer_ca_pems_.clear();
124   issuer_.Clear();
125   subject_.Clear();
126   enrollment_uri_list_.clear();
127 }
128
129 base::DictionaryValue* CertificatePattern::CreateAsDictionary() const {
130   base::DictionaryValue* dict = new base::DictionaryValue;
131
132   if (!issuer_ca_pems_.empty()) {
133     dict->Set(onc::certificate::kIssuerCAPEMs,
134               CreateListFromStrings(issuer_ca_pems_));
135   }
136
137   if (!issuer_.Empty())
138     dict->Set(kIssuerKey, issuer_.CreateAsDictionary());
139
140   if (!subject_.Empty())
141     dict->Set(kSubjectKey, subject_.CreateAsDictionary());
142
143   if (!enrollment_uri_list_.empty())
144     dict->Set(kEnrollmentUriKey, CreateListFromStrings(enrollment_uri_list_));
145   return dict;
146 }
147
148 bool CertificatePattern::CopyFromDictionary(const base::DictionaryValue &dict) {
149   const base::DictionaryValue* child_dict = NULL;
150   const base::ListValue* child_list = NULL;
151   Clear();
152
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;
158   }
159   if (dict.GetDictionary(kIssuerKey, &child_dict) && child_dict) {
160     if (!issuer_.CopyFromDictionary(*child_dict))
161       return false;
162   }
163   child_dict = NULL;
164   if (dict.GetDictionary(kSubjectKey, &child_dict) && child_dict) {
165     if (!subject_.CopyFromDictionary(*child_dict))
166       return false;
167   }
168   child_list = NULL;
169   if (dict.GetList(kEnrollmentUriKey, &child_list) && child_list) {
170     if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
171       return false;
172   }
173
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;
179
180   return true;
181 }
182
183 }  // namespace chromeos