OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_util.h" | |
9 #include "base/location.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/task_runner.h" | |
13 #include "base/task_runner_util.h" | |
14 #include "base/values.h" | |
15 #include "components/autofill/core/common/password_form.h" | |
16 #include "components/password_manager/core/browser/import/csv_reader.h" | |
17 | |
18 namespace password_manager { | |
19 | |
20 namespace { | |
21 | |
22 using autofill::PasswordForm; | |
23 | |
24 // PasswordReaderBase --------------------------------------------------------- | |
25 | |
26 // Interface for writing a list of passwords into various formats. | |
27 class PasswordReaderBase { | |
28 public: | |
29 virtual ~PasswordReaderBase() = 0; | |
30 | |
31 // Deserializes a list of passwords from |input| into |passwords|. | |
32 virtual PasswordImporter::Result DeserializePasswords( | |
33 const std::string& input, | |
34 std::vector<PasswordForm>* passwords) = 0; | |
35 }; | |
36 | |
37 PasswordReaderBase::~PasswordReaderBase() {} | |
38 | |
39 // PasswordCSVReader ---------------------------------------------------------- | |
40 | |
41 class PasswordCSVReader : public PasswordReaderBase { | |
42 public: | |
43 static const base::FilePath::CharType kFileExtension[]; | |
44 | |
45 PasswordCSVReader() {} | |
46 | |
47 // PasswordWriterBase: | |
48 PasswordImporter::Result DeserializePasswords( | |
49 const std::string& input, | |
50 std::vector<PasswordForm>* passwords) override { | |
51 std::vector<std::string> header; | |
52 std::vector<std::map<std::string, std::string>> records; | |
53 if (!ReadCSV(input, &header, &records)) | |
54 return PasswordImporter::SYNTAX_ERROR; | |
55 | |
56 if (!GetActualFieldName(header, GetURLFieldNames(), url_field_name_) || | |
57 !GetActualFieldName(header, GetUsernameFieldNames(), | |
58 username_field_name_) || | |
59 !GetActualFieldName(header, GetPasswordFieldNames(), | |
60 password_field_name_)) | |
61 return PasswordImporter::SEMANTIC_ERROR; | |
62 | |
63 passwords->clear(); | |
64 passwords->reserve(records.size()); | |
65 | |
66 for (const auto& record : records) { | |
67 PasswordForm form; | |
68 if (RecordToPasswordForm(record, &form)) | |
69 passwords->push_back(form); | |
70 } | |
71 return PasswordImporter::SUCCESS; | |
72 } | |
73 | |
74 private: | |
75 std::string url_field_name_; | |
76 std::string username_field_name_; | |
77 std::string password_field_name_; | |
78 | |
79 const std::vector<std::string> GetURLFieldNames() { | |
80 std::vector<std::string> url_names; | |
81 url_names.push_back("url"); | |
82 url_names.push_back("website"); | |
83 url_names.push_back("origin"); | |
84 url_names.push_back("hostname"); | |
85 return url_names; | |
86 } | |
87 | |
88 const std::vector<std::string> GetUsernameFieldNames() { | |
89 std::vector<std::string> username_names; | |
90 username_names.push_back("username"); | |
91 username_names.push_back("user"); | |
92 username_names.push_back("login"); | |
93 username_names.push_back("account"); | |
94 return username_names; | |
95 } | |
96 | |
97 const std::vector<std::string> GetPasswordFieldNames() { | |
98 std::vector<std::string> password_names; | |
99 password_names.push_back("password"); | |
100 return password_names; | |
101 } | |
102 | |
103 bool RecordToPasswordForm(const std::map<std::string, std::string>& record, | |
104 PasswordForm* form) { | |
105 if (!record.count(url_field_name_) || !record.count(username_field_name_) || | |
106 !record.count(password_field_name_)) { | |
107 return false; | |
108 } | |
109 form->origin = GURL(record.at(url_field_name_)); | |
110 form->signon_realm = form->origin.GetOrigin().spec(); | |
111 form->username_value = base::UTF8ToUTF16(record.at(username_field_name_)); | |
112 form->password_value = base::UTF8ToUTF16(record.at(password_field_name_)); | |
113 return true; | |
114 } | |
115 | |
116 bool GetActualFieldName(const std::vector<std::string>& header, | |
117 const std::vector<std::string>& options, | |
118 std::string& field_name) { | |
119 auto it = std::find_if(header.begin(), header.end(), | |
120 [&options](const std::string& str) { | |
121 return std::count(options.begin(), options.end(), | |
122 base::ToLowerASCII(str)); | |
123 }); | |
124 | |
125 if (it == header.end()) { | |
126 return false; | |
127 } | |
128 | |
129 field_name = *it; | |
130 return true; | |
131 } | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader); | |
134 }; | |
135 | |
136 const base::FilePath::CharType PasswordCSVReader::kFileExtension[] = | |
137 FILE_PATH_LITERAL("csv"); | |
138 | |
139 // Helpers -------------------------------------------------------------------- | |
140 | |
141 // Reads and returns the contents of the file at |path| as a string, or returns | |
142 // a scoped point containing a NULL if there was an error. | |
143 scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) { | |
144 scoped_ptr<std::string> contents(new std::string); | |
145 if (!base::ReadFileToString(path, contents.get())) | |
146 return scoped_ptr<std::string>(); | |
147 return contents; | |
148 } | |
149 | |
150 // Parses passwords from |input| using |password_reader| and synchronously calls | |
151 // |completion| with the results. | |
152 static void ParsePasswords( | |
153 scoped_ptr<PasswordReaderBase> password_reader, | |
154 const PasswordImporter::CompletionCallback& completion, | |
155 scoped_ptr<std::string> input) { | |
156 std::vector<PasswordForm> passwords; | |
157 PasswordImporter::Result result = PasswordImporter::IO_ERROR; | |
158 if (input) | |
159 result = password_reader->DeserializePasswords(*input, &passwords); | |
160 completion.Run(result, passwords); | |
161 } | |
162 | |
163 } // namespace | |
164 | |
165 // static | |
166 void PasswordImporter::Import( | |
167 const base::FilePath& path, | |
168 scoped_refptr<base::TaskRunner> blocking_task_runner, | |
169 const CompletionCallback& completion) { | |
170 // Currently, CSV is the only supported format. | |
171 scoped_ptr<PasswordReaderBase> password_reader(new PasswordCSVReader); | |
vabr (Chromium)
2016/03/22 15:28:28
nit: Also here, it might be enough to create the r
xunlu
2016/03/23 04:50:52
I'm not sure about this one because this variable
vabr (Chromium)
2016/03/23 08:25:48
Ah, sorry, my bad, this needs to be a scoped_ptr b
| |
172 base::PostTaskAndReplyWithResult( | |
173 blocking_task_runner.get(), FROM_HERE, | |
174 base::Bind(&ReadFileToString, path), | |
175 base::Bind(&ParsePasswords, base::Passed(&password_reader), completion)); | |
176 } | |
177 | |
178 // static | |
179 std::vector<std::vector<base::FilePath::StringType>> | |
180 PasswordImporter::GetSupportedFileExtensions() { | |
181 std::vector<std::vector<base::FilePath::StringType>> extensions; | |
182 extensions.resize(1); | |
183 extensions[0].push_back(PasswordCSVReader::kFileExtension); | |
184 return extensions; | |
185 } | |
186 | |
187 } // namespace password_manager | |
OLD | NEW |