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

Side by Side Diff: components/password_manager/core/browser/export/password_exporter.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, 6 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/export/password_exporter.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/task_runner.h"
12 #include "base/values.h"
13 #include "components/autofill/core/common/password_form.h"
14 #include "components/password_manager/core/browser/export/csv_writer.h"
15
16 namespace password_manager {
17
18 namespace {
19
20 using autofill::PasswordForm;
21
22 typedef std::map<std::string, std::string> ColumnNameToValueMap;
23
24 // PasswordWriterBase ---------------------------------------------------------
25
26 // Interface for writing a list of passwords into various formats.
27 class PasswordWriterBase {
28 public:
29 virtual ~PasswordWriterBase() = 0;
30
31 // Serializes the list of |passwords|.
32 virtual std::string SerializePasswords(
33 ScopedVector<PasswordForm> passwords) = 0;
34 };
35
36 PasswordWriterBase::~PasswordWriterBase() {
37 }
38
39 // PasswordCSVWriter ----------------------------------------------------------
40
41 class PasswordCSVWriter : public PasswordWriterBase {
42 public:
43 static const base::FilePath::StringType kFileExtension;
44
45 PasswordCSVWriter() {}
46
47 // PasswordWriterBase:
48 std::string SerializePasswords(
49 ScopedVector<PasswordForm> passwords) override {
50 std::vector<std::string> header;
51 header.push_back(kTitleFieldName);
52 header.push_back(kUrlFieldName);
53 header.push_back(kUsernameFieldName);
54 header.push_back(kPasswordFieldName);
55
56 std::vector<ColumnNameToValueMap> records;
57 records.reserve(passwords.size());
58 for (ScopedVector<PasswordForm>::const_iterator it = passwords.begin();
59 it != passwords.end(); ++it) {
60 records.push_back(PasswordFormToRecord(*it));
61 }
62 std::string result;
63 WriteCSV(header, records, &result);
64 return result;
65 }
66
67 private:
68 static const char kUrlFieldName[];
69 static const char kUsernameFieldName[];
70 static const char kPasswordFieldName[];
71 static const char kTitleFieldName[];
72
73 ColumnNameToValueMap PasswordFormToRecord(PasswordForm* form) {
74 ColumnNameToValueMap record;
75 record[kUrlFieldName] = (form->origin).spec();
76 record[kUsernameFieldName] = base::UTF16ToUTF8(form->username_value);
77 record[kPasswordFieldName] = base::UTF16ToUTF8(form->password_value);
78 record[kTitleFieldName] = form->origin.host();
79 return record;
80 }
81
82 DISALLOW_COPY_AND_ASSIGN(PasswordCSVWriter);
83 };
84
85 const base::FilePath::StringType PasswordCSVWriter::kFileExtension =
Garrett Casto 2015/06/26 21:13:48 The type was right before. We don't allow static v
xunlu 2015/06/30 23:06:09 Done.
86 FILE_PATH_LITERAL("csv");
87 const char PasswordCSVWriter::kUrlFieldName[] = "url";
88 const char PasswordCSVWriter::kUsernameFieldName[] = "username";
89 const char PasswordCSVWriter::kPasswordFieldName[] = "password";
90 const char PasswordCSVWriter::kTitleFieldName[] = "name";
91
92 // Helper ---------------------------------------------------------------------
93
94 void WriteToFile(const base::FilePath& path, const std::string& data) {
95 base::WriteFile(path, data.c_str(), data.size());
96 }
97
98 } // namespace
99
100 // static
101 void PasswordExporter::Export(
102 const base::FilePath& path,
103 ScopedVector<PasswordForm> passwords,
104 scoped_refptr<base::TaskRunner> blocking_task_runner,
105 const base::Closure& completion) {
106 scoped_ptr<PasswordWriterBase> password_writer;
107 if (path.Extension().substr(1) == PasswordCSVWriter::kFileExtension)
108 password_writer.reset(new PasswordCSVWriter);
109 else
110 NOTREACHED();
111
112 std::string serialized_passwords =
113 password_writer->SerializePasswords(passwords.Pass());
114 blocking_task_runner->PostTaskAndReply(
115 FROM_HERE, base::Bind(&WriteToFile, path, serialized_passwords),
116 completion);
117 }
118
119 // static
120 std::vector<std::vector<base::FilePath::StringType>>
121 PasswordExporter::GetSupportedFileExtensions() {
122 std::vector<std::vector<base::FilePath::StringType>> extensions;
123 extensions.resize(1);
Garrett Casto 2015/06/26 21:13:48 Don't resize here. If you care about performance a
xunlu 2015/06/30 23:06:09 If the resize is removed, the extensions[0] on the
124 extensions[0].push_back(PasswordCSVWriter::kFileExtension);
125 return extensions;
126 }
127
128 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698