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

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: address comments. Created 4 years, 9 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/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 // PasswordWriterBase ---------------------------------------------------------
23
24 // Interface for writing a list of passwords into various formats.
25 class PasswordWriterBase {
26 public:
27 virtual ~PasswordWriterBase() = 0;
28
29 // Serializes the list of |passwords|.
30 virtual std::string SerializePasswords(
31 std::vector<scoped_ptr<PasswordForm>>& passwords) = 0;
32 };
33
34 PasswordWriterBase::~PasswordWriterBase() {}
35
36 // PasswordCSVWriter ----------------------------------------------------------
37
38 class PasswordCSVWriter : public PasswordWriterBase {
39 public:
40 static const base::FilePath::CharType kFileExtension[];
41
42 PasswordCSVWriter() {}
43
44 // PasswordWriterBase:
45 std::string SerializePasswords(
46 std::vector<scoped_ptr<PasswordForm>>& passwords) override {
47 std::vector<std::string> header;
48 header.push_back(kTitleFieldName);
49 header.push_back(kUrlFieldName);
50 header.push_back(kUsernameFieldName);
51 header.push_back(kPasswordFieldName);
52
53 std::vector<std::map<std::string, std::string>> records;
54 records.reserve(passwords.size());
55 for (const auto& it : passwords) {
56 records.push_back(PasswordFormToRecord(it.get()));
57 }
58 std::string result;
59 WriteCSV(header, records, &result);
60 return result;
61 }
62
63 private:
64 static const char kUrlFieldName[];
65 static const char kUsernameFieldName[];
66 static const char kPasswordFieldName[];
67 static const char kTitleFieldName[];
68
69 std::map<std::string, std::string> PasswordFormToRecord(
70 const PasswordForm* form) {
71 std::map<std::string, std::string> record;
72 record[kUrlFieldName] = (form->origin).spec();
73 record[kUsernameFieldName] = base::UTF16ToUTF8(form->username_value);
74 record[kPasswordFieldName] = base::UTF16ToUTF8(form->password_value);
75 record[kTitleFieldName] = form->origin.host();
76 return record;
77 }
78
79 DISALLOW_COPY_AND_ASSIGN(PasswordCSVWriter);
80 };
81
82 const base::FilePath::CharType PasswordCSVWriter::kFileExtension[] =
83 FILE_PATH_LITERAL("csv");
84 const char PasswordCSVWriter::kUrlFieldName[] = "url";
85 const char PasswordCSVWriter::kUsernameFieldName[] = "username";
86 const char PasswordCSVWriter::kPasswordFieldName[] = "password";
87 const char PasswordCSVWriter::kTitleFieldName[] = "name";
88
89 // Helper ---------------------------------------------------------------------
90
91 void WriteToFile(const base::FilePath& path, const std::string& data) {
92 base::WriteFile(path, data.c_str(), data.size());
93 }
94
95 } // namespace
96
97 // static
98 void PasswordExporter::Export(
99 const base::FilePath& path,
100 std::vector<scoped_ptr<PasswordForm>> passwords,
101 scoped_refptr<base::TaskRunner> blocking_task_runner) {
102 scoped_ptr<PasswordWriterBase> password_writer;
103 if (!path.Extension().empty() &&
104 path.Extension().substr(1) == PasswordCSVWriter::kFileExtension) {
vabr (Chromium) 2016/03/21 14:01:30 I suggest going even further and just creating the
xunlu 2016/03/22 07:17:15 Done.
105 password_writer.reset(new PasswordCSVWriter);
106 } else {
107 // Using PasswordCSVWriter as default;
108 password_writer.reset(new PasswordCSVWriter);
109 }
110
111 std::string serialized_passwords =
112 password_writer->SerializePasswords(passwords);
113
114 blocking_task_runner->PostTask(
115 FROM_HERE, base::Bind(&WriteToFile, path, serialized_passwords));
116 }
117
118 // static
119 std::vector<std::vector<base::FilePath::StringType>>
120 PasswordExporter::GetSupportedFileExtensions() {
121 std::vector<std::vector<base::FilePath::StringType>> extensions;
122 extensions.resize(1);
123 extensions[0].push_back(PasswordCSVWriter::kFileExtension);
124 return extensions;
125 }
126
127 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698