Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: components/password_manager/core/browser/import/password_importer.cc

Issue 1193143003: Enable import/export of passwords into/from Password Manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix problem revealed through git-cl-try Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_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
40 // PasswordCSVReader ----------------------------------------------------------
41
42 class PasswordCSVReader : public PasswordReaderBase {
43 public:
44 static const base::FilePath::StringType kFileExtension;
Garrett Casto 2015/06/26 21:13:48 Keep this as CharType[].
xunlu 2015/06/30 23:06:09 Done.
45
46 PasswordCSVReader() {}
47
48 // PasswordWriterBase:
49 PasswordImporter::Result DeserializePasswords(
50 const std::string& input,
51 std::vector<PasswordForm>* passwords) override {
52 std::vector<std::string> header;
53 std::vector<ColumnNameToValueMap> records;
54 if (!ReadCSV(input, &header, &records))
55 return PasswordImporter::SYNTAX_ERROR;
56
57 if (!GetActualFieldName(header, GetURLFieldNames(), url_field_name_) ||
58 !GetActualFieldName(header, GetUsernameFieldNames(),
59 username_field_name_) ||
60 !GetActualFieldName(header, GetPasswordFieldNames(),
61 password_field_name_))
62 return PasswordImporter::SEMANTIC_ERROR;
63
64 passwords->clear();
65 passwords->reserve(records.size());
66
67 for (std::vector<ColumnNameToValueMap>::const_iterator it = records.begin();
68 it != records.end(); ++it) {
69 PasswordForm form;
70 if (RecordToPasswordForm(*it, &form))
71 passwords->push_back(form);
72 }
73 return PasswordImporter::SUCCESS;
74 }
75
76 private:
77 std::string url_field_name_;
78 std::string username_field_name_;
79 std::string password_field_name_;
80
81 const std::vector<std::string> GetURLFieldNames() {
82 std::vector<std::string> url_names;
83 url_names.push_back("url");
84 url_names.push_back("website");
85 url_names.push_back("origin");
86 url_names.push_back("hostname");
87 return url_names;
88 }
89
90 const std::vector<std::string> GetUsernameFieldNames() {
91 std::vector<std::string> username_names;
92 username_names.push_back("username");
93 username_names.push_back("user");
94 username_names.push_back("login");
95 username_names.push_back("account");
96 return username_names;
97 }
98
99 const std::vector<std::string> GetPasswordFieldNames() {
100 std::vector<std::string> password_names;
101 password_names.push_back("password");
102 return password_names;
103 }
104
105 bool RecordToPasswordForm(const ColumnNameToValueMap& record,
106 PasswordForm* form) {
107 if (!record.count(url_field_name_) || !record.count(username_field_name_) ||
108 !record.count(password_field_name_)) {
109 return false;
110 }
111 form->origin = GURL(record.at(url_field_name_));
112 form->username_value = base::UTF8ToUTF16(record.at(username_field_name_));
113 form->password_value = base::UTF8ToUTF16(record.at(password_field_name_));
114 return true;
115 }
116
117 bool GetActualFieldName(const std::vector<std::string>& header,
118 const std::vector<std::string>& options,
119 std::string& field_name) {
120 std::vector<std::string>::const_iterator it = header.begin();
121 for (; it != header.end(); ++it) {
122 if (std::count(options.begin(), options.end(),
123 base::StringToLowerASCII(*it))) {
124 field_name = *it;
125 break;
126 }
127 }
128 return !(it == header.end());
129 }
130
131 DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader);
132 };
133
134 const base::FilePath::StringType PasswordCSVReader::kFileExtension =
135 FILE_PATH_LITERAL("csv");
136
137 // Helpers --------------------------------------------------------------------
138
139 // Reads and returns the contents of the file at |path| as a string, or returns
140 // a scoped point containing a NULL if there was an error.
141 scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) {
142 scoped_ptr<std::string> contents(new std::string);
143 if (!base::ReadFileToString(path, contents.get()))
144 return scoped_ptr<std::string>();
145 return contents.Pass();
146 }
147
148 // Parses passwords from |input| using |password_reader| and synchronously calls
149 // |completion| with the results.
150 static void ParsePasswords(
151 scoped_ptr<PasswordReaderBase> password_reader,
152 const PasswordImporter::CompletionCallback& completion,
153 scoped_ptr<std::string> input) {
154 std::vector<PasswordForm> passwords;
155 PasswordImporter::Result result = PasswordImporter::IO_ERROR;
156 if (input)
157 result = password_reader->DeserializePasswords(*input, &passwords);
158 completion.Run(result, passwords);
159 }
160
161 } // namespace
162
163 // static
164 void PasswordImporter::Import(
165 const base::FilePath& path,
166 scoped_refptr<base::TaskRunner> blocking_task_runner,
167 const CompletionCallback& completion) {
168 scoped_ptr<PasswordReaderBase> password_reader;
169 if (path.Extension().substr(1) == PasswordCSVReader::kFileExtension) {
170 password_reader.reset(new PasswordCSVReader);
171 } else {
172 NOTREACHED() << "Unsupported extension: " << path.Extension();
173 }
174
175 base::PostTaskAndReplyWithResult(
176 blocking_task_runner.get(), FROM_HERE,
177 base::Bind(&ReadFileToString, path),
178 base::Bind(&ParsePasswords, base::Passed(&password_reader), completion));
179 }
180
181 // static
182 std::vector<std::vector<base::FilePath::StringType>>
183 PasswordImporter::GetSupportedFileExtensions() {
184 std::vector<std::vector<base::FilePath::StringType>> extensions;
185 extensions.resize(1);
Garrett Casto 2015/06/26 21:13:48 No resize.
xunlu 2015/06/30 23:06:09 See similar comments in password_exporter.cc
186 extensions[0].push_back(PasswordCSVReader::kFileExtension);
187 return extensions;
188 }
189
190 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698