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

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: comment Created 4 years, 8 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 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/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/task_runner.h"
14 #include "base/task_runner_util.h"
15 #include "base/values.h"
16 #include "components/autofill/core/common/password_form.h"
17 #include "components/password_manager/core/browser/import/csv_reader.h"
18
19 namespace password_manager {
20
21 namespace {
22
23 using autofill::PasswordForm;
24
25 // PasswordReaderBase ---------------------------------------------------------
26
27 // Interface for writing a list of passwords into various formats.
28 class PasswordReaderBase {
29 public:
30 virtual ~PasswordReaderBase() = 0;
31
32 // Deserializes a list of passwords from |input| into |passwords|.
33 virtual PasswordImporter::Result DeserializePasswords(
34 const std::string& input,
35 std::vector<PasswordForm>* passwords) = 0;
36 };
37
38 PasswordReaderBase::~PasswordReaderBase() {}
39
40 // PasswordCSVReader ----------------------------------------------------------
41
42 class PasswordCSVReader : public PasswordReaderBase {
43 public:
44 static const base::FilePath::CharType kFileExtension[];
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<std::map<std::string, std::string>> 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 (const auto& record : records) {
68 PasswordForm form;
69 if (RecordToPasswordForm(record, &form))
70 passwords->push_back(form);
71 }
72 return PasswordImporter::SUCCESS;
73 }
74
75 private:
76 std::string url_field_name_;
77 std::string username_field_name_;
78 std::string password_field_name_;
79
80 const std::vector<std::string> GetURLFieldNames() {
81 std::vector<std::string> url_names;
82 url_names.push_back("url");
83 url_names.push_back("website");
84 url_names.push_back("origin");
85 url_names.push_back("hostname");
86 return url_names;
87 }
88
89 const std::vector<std::string> GetUsernameFieldNames() {
90 std::vector<std::string> username_names;
91 username_names.push_back("username");
92 username_names.push_back("user");
93 username_names.push_back("login");
94 username_names.push_back("account");
95 return username_names;
96 }
97
98 const std::vector<std::string> GetPasswordFieldNames() {
99 std::vector<std::string> password_names;
100 password_names.push_back("password");
101 return password_names;
102 }
103
104 bool RecordToPasswordForm(const std::map<std::string, std::string>& record,
105 PasswordForm* form) {
106 if (!record.count(url_field_name_) || !record.count(username_field_name_) ||
107 !record.count(password_field_name_)) {
108 return false;
109 }
110 form->origin = GURL(record.at(url_field_name_));
111 form->signon_realm = form->origin.GetOrigin().spec();
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 auto it = std::find_if(header.begin(), header.end(),
121 [&options](const std::string& str) {
122 return std::count(options.begin(), options.end(),
123 base::ToLowerASCII(str));
124 });
125
126 if (it == header.end()) {
127 return false;
128 }
129
130 field_name = *it;
131 return true;
132 }
133
134 DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader);
135 };
136
137 const base::FilePath::CharType PasswordCSVReader::kFileExtension[] =
138 FILE_PATH_LITERAL("csv");
139
140 // Helpers --------------------------------------------------------------------
141
142 // Reads and returns the contents of the file at |path| as a string, or returns
143 // a scoped point containing a NULL if there was an error.
144 scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) {
145 scoped_ptr<std::string> contents(new std::string);
146 if (!base::ReadFileToString(path, contents.get()))
147 return scoped_ptr<std::string>();
148 return contents;
149 }
150
151 // Parses passwords from |input| using |password_reader| and synchronously calls
152 // |completion| with the results.
153 static void ParsePasswords(
154 scoped_ptr<PasswordReaderBase> password_reader,
155 const PasswordImporter::CompletionCallback& completion,
156 scoped_ptr<std::string> input) {
157 std::vector<PasswordForm> passwords;
158 PasswordImporter::Result result = PasswordImporter::IO_ERROR;
159 if (input)
160 result = password_reader->DeserializePasswords(*input, &passwords);
161 completion.Run(result, passwords);
162 }
163
164 } // namespace
165
166 // static
167 void PasswordImporter::Import(
168 const base::FilePath& path,
169 scoped_refptr<base::TaskRunner> blocking_task_runner,
170 const CompletionCallback& completion) {
171 // Currently, CSV is the only supported format.
172 scoped_ptr<PasswordReaderBase> password_reader(new PasswordCSVReader);
173 base::PostTaskAndReplyWithResult(
174 blocking_task_runner.get(), FROM_HERE,
175 base::Bind(&ReadFileToString, path),
176 base::Bind(&ParsePasswords, base::Passed(&password_reader), completion));
177 }
178
179 // static
180 std::vector<std::vector<base::FilePath::StringType>>
181 PasswordImporter::GetSupportedFileExtensions() {
182 std::vector<std::vector<base::FilePath::StringType>> extensions;
183 extensions.resize(1);
184 extensions[0].push_back(PasswordCSVReader::kFileExtension);
185 return extensions;
186 }
187
188 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698