OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "components/password_manager/core/browser/import/password_importer.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/json/json_reader.h" | |
11 #include "base/location.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/task_runner.h" | |
15 #include "base/task_runner_util.h" | |
16 #include "base/values.h" | |
17 #include "components/autofill/core/common/password_form.h" | |
18 #include "components/password_manager/core/browser/import/csv_reader.h" | |
19 | |
20 namespace password_manager { | |
21 | |
22 namespace { | |
23 | |
24 using autofill::PasswordForm; | |
25 typedef std::map<std::string, std::string> ColumnNameToValueMap; | |
Garrett Casto
2015/06/23 23:42:36
Seems like this typedef should really be in csv_re
xunlu
2015/06/25 17:12:16
Done.
| |
26 | |
27 // PasswordReaderBase --------------------------------------------------------- | |
28 | |
29 // Interface for writing a list of passwords into various formats. | |
30 class PasswordReaderBase { | |
31 public: | |
32 virtual ~PasswordReaderBase() = 0; | |
33 | |
34 // Deserializes a list of passwords from |input| into |passwords|. | |
35 virtual PasswordImporter::Result DeserializePasswords( | |
36 const std::string& input, | |
37 std::vector<PasswordForm>* passwords) = 0; | |
38 }; | |
39 | |
40 PasswordReaderBase::~PasswordReaderBase() { | |
41 } | |
42 | |
43 // PasswordCSVReader ---------------------------------------------------------- | |
44 | |
45 class PasswordCSVReader : public PasswordReaderBase { | |
46 public: | |
47 static const base::FilePath::CharType kFileExtension[]; | |
48 | |
49 PasswordCSVReader() {} | |
50 | |
51 // PasswordWriterBase: | |
52 PasswordImporter::Result DeserializePasswords( | |
53 const std::string& input, | |
54 std::vector<PasswordForm>* passwords) override { | |
55 std::vector<std::string> header; | |
56 std::vector<ColumnNameToValueMap> records; | |
57 if (!ReadCSV(input, &header, &records)) | |
58 return PasswordImporter::SYNTAX_ERROR; | |
59 | |
60 if (!GetActualFieldName(header, kUrlFieldNames, url_field_name_) || | |
61 !GetActualFieldName(header, kUsernameFieldNames, | |
62 username_field_name_) || | |
63 !GetActualFieldName(header, kPasswordFieldNames, password_field_name_)) | |
64 return PasswordImporter::SEMANTICAL_ERROR; | |
Garrett Casto
2015/06/23 23:42:36
This should be SEMANTIC_ERROR. Semantical isn't ac
xunlu
2015/06/25 17:12:16
Done.
| |
65 | |
66 passwords->clear(); | |
67 passwords->reserve(records.size()); | |
68 | |
69 typedef std::vector<ColumnNameToValueMap>::const_iterator | |
70 ConstRecordIterator; | |
Garrett Casto
2015/06/23 23:42:36
I wouldn't use a typedef just for a one time use.
xunlu
2015/06/25 17:12:16
Done.
| |
71 for (ConstRecordIterator it = records.begin(); it != records.end(); ++it) { | |
72 PasswordForm form; | |
73 if (RecordToPasswordForm(*it, &form)) | |
74 passwords->push_back(form); | |
75 } | |
76 return PasswordImporter::SUCCESS; | |
77 } | |
78 | |
79 private: | |
80 static const std::vector<std::string> kUrlFieldNames; | |
81 static const std::vector<std::string> kUsernameFieldNames; | |
82 static const std::vector<std::string> kPasswordFieldNames; | |
83 | |
84 std::string url_field_name_; | |
85 std::string username_field_name_; | |
86 std::string password_field_name_; | |
87 | |
88 bool RecordToPasswordForm(const ColumnNameToValueMap& record, | |
89 PasswordForm* form) { | |
90 if (!record.count(url_field_name_) || !record.count(username_field_name_) || | |
91 !record.count(password_field_name_)) | |
Garrett Casto
2015/06/23 23:42:36
Generally we use braces when the "if" statement is
xunlu
2015/06/25 17:12:16
Done.
| |
92 return false; | |
93 form->origin = GURL(record.at(url_field_name_)); | |
94 form->username_value = base::UTF8ToUTF16(record.at(username_field_name_)); | |
95 form->password_value = base::UTF8ToUTF16(record.at(password_field_name_)); | |
96 return true; | |
97 } | |
98 | |
99 bool GetActualFieldName(const std::vector<std::string>& header, | |
100 const std::vector<std::string>& options, | |
101 std::string& field_name) { | |
102 std::vector<std::string>::const_iterator it = header.begin(); | |
103 for (; it != header.end(); ++it) { | |
104 if (std::count(options.begin(), options.end(), | |
105 base::StringToLowerASCII(*it))) { | |
106 field_name = *it; | |
107 break; | |
108 } | |
109 } | |
110 return !(it == header.end()); | |
111 } | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader); | |
114 }; | |
115 | |
116 const base::FilePath::CharType PasswordCSVReader::kFileExtension[] = ".csv"; | |
Garrett Casto
2015/06/23 23:42:36
I'm pretty sure that this needs to be " = FILE_PAT
xunlu
2015/06/25 17:12:16
Done.
| |
117 const std::vector<std::string> PasswordCSVReader::kUrlFieldNames = {"url", | |
118 "website", | |
119 "origin", | |
120 "hostname"}; | |
121 const std::vector<std::string> PasswordCSVReader::kUsernameFieldNames = | |
122 {"username", "user", "login", "account"}; | |
123 const std::vector<std::string> PasswordCSVReader::kPasswordFieldNames = { | |
124 "password"}; | |
125 | |
126 // PasswordJSONReader --------------------------------------------------------- | |
Garrett Casto
2015/06/23 23:42:36
Assuming we aren't going to support this functiona
xunlu
2015/06/25 17:12:16
Done.
| |
127 | |
128 class PasswordJSONReader : public PasswordReaderBase { | |
129 public: | |
130 static const base::FilePath::CharType kFileExtension[]; | |
131 | |
132 PasswordJSONReader() {} | |
133 | |
134 // PasswordReaderBase: | |
135 PasswordImporter::Result DeserializePasswords( | |
136 const std::string& input, | |
137 std::vector<PasswordForm>* passwords) override { | |
138 scoped_ptr<base::Value> parsed_json(base::JSONReader::Read(input)); | |
139 if (!parsed_json) | |
140 return PasswordImporter::SYNTAX_ERROR; | |
141 | |
142 const base::ListValue* password_dictionaries; | |
143 if (!parsed_json->GetAsList(&password_dictionaries)) | |
144 return PasswordImporter::SEMANTICAL_ERROR; | |
145 | |
146 for (base::ListValue::const_iterator it = password_dictionaries->begin(); | |
147 it != password_dictionaries->end(); ++it) { | |
148 PasswordForm form; | |
149 const base::DictionaryValue* password_dict; | |
150 if ((*it)->GetAsDictionary(&password_dict) && | |
151 DictionaryToPasswordForm(*password_dict, &form)) | |
152 passwords->push_back(form); | |
153 } | |
154 return PasswordImporter::SUCCESS; | |
155 } | |
156 | |
157 private: | |
158 bool DictionaryToPasswordForm(const base::DictionaryValue& dictionary, | |
159 PasswordForm* form) { | |
160 std::string origin; | |
161 if (!dictionary.GetString("url", &origin)) | |
162 return false; | |
163 form->origin = GURL(origin); | |
164 if (!dictionary.GetString("username", &form->username_value)) | |
165 return false; | |
166 if (!dictionary.GetString("password", &form->password_value)) | |
167 return false; | |
168 return true; | |
169 } | |
170 | |
171 DISALLOW_COPY_AND_ASSIGN(PasswordJSONReader); | |
172 }; | |
173 | |
174 const base::FilePath::CharType PasswordJSONReader::kFileExtension[] = ".json"; | |
175 | |
176 // Helpers -------------------------------------------------------------------- | |
177 | |
178 // Reads and returns the contents of the file at |path| as a string, or returns | |
179 // a scoped point containing a NULL if there was an error. | |
180 scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) { | |
181 scoped_ptr<std::string> contents(new std::string); | |
182 if (!base::ReadFileToString(path, contents.get())) | |
183 return scoped_ptr<std::string>(); | |
184 return contents.Pass(); | |
185 } | |
186 | |
187 // Parses passwords from |input| using |password_reader| and synchronously calls | |
188 // |completion| with the results. | |
189 static void ParsePasswords( | |
190 scoped_ptr<PasswordReaderBase> password_reader, | |
191 const PasswordImporter::CompletionCallback& completion, | |
192 scoped_ptr<std::string> input) { | |
193 std::vector<PasswordForm> passwords; | |
194 PasswordImporter::Result result = PasswordImporter::IO_ERROR; | |
195 if (input) | |
196 result = password_reader->DeserializePasswords(*input, &passwords); | |
197 completion.Run(result, passwords); | |
198 } | |
199 | |
200 } // namespace | |
201 | |
202 // static | |
203 void PasswordImporter::Import( | |
204 const base::FilePath& path, | |
205 scoped_refptr<base::TaskRunner> blocking_task_runner, | |
206 const CompletionCallback& completion) { | |
207 scoped_ptr<PasswordReaderBase> password_reader; | |
208 if (path.Extension() == PasswordCSVReader::kFileExtension) { | |
209 password_reader.reset(new PasswordCSVReader); | |
210 } else if (path.Extension() == PasswordJSONReader::kFileExtension) { | |
211 password_reader.reset(new PasswordJSONReader); | |
212 } else { | |
213 NOTREACHED() << "Unsupported extension: " << path.Extension(); | |
214 } | |
215 | |
216 base::PostTaskAndReplyWithResult( | |
217 blocking_task_runner.get(), FROM_HERE, | |
218 base::Bind(&ReadFileToString, path), | |
219 base::Bind(&ParsePasswords, base::Passed(&password_reader), completion)); | |
220 } | |
221 | |
222 // static | |
223 std::vector<std::string> PasswordImporter::GetSupportedFileExtensions() { | |
224 std::vector<std::string> extensions; | |
225 extensions.push_back(PasswordCSVReader::kFileExtension); | |
226 // TODO(xunlu) re-enable JSON when we are ready support it | |
227 // extensions.push_back(PasswordJSONReader::kFileExtension); | |
228 return extensions; | |
229 } | |
230 | |
231 } // namespace password_manager | |
OLD | NEW |