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

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: comments 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/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 ScopedVector<PasswordForm> passwords) = 0;
32 };
33
34 PasswordWriterBase::~PasswordWriterBase() {
35 }
36
37 // PasswordCSVWriter ----------------------------------------------------------
38
39 class PasswordCSVWriter : public PasswordWriterBase {
40 public:
41 static const base::FilePath::CharType kFileExtension[];
42
43 PasswordCSVWriter() {}
44
45 // PasswordWriterBase:
46 std::string SerializePasswords(
47 ScopedVector<PasswordForm> passwords) override {
48 std::vector<std::string> header;
49 header.push_back(kTitleFieldName);
50 header.push_back(kUrlFieldName);
51 header.push_back(kUsernameFieldName);
52 header.push_back(kPasswordFieldName);
53
54 std::vector<ColumnNameToValueMap> records;
55 records.reserve(passwords.size());
56 for (ScopedVector<PasswordForm>::const_iterator it = passwords.begin();
57 it != passwords.end(); ++it) {
58 records.push_back(PasswordFormToRecord(*it));
59 }
60 std::string result;
61 WriteCSV(header, records, &result);
62 return result;
63 }
64
65 private:
66 static const char kUrlFieldName[];
67 static const char kUsernameFieldName[];
68 static const char kPasswordFieldName[];
69 static const char kTitleFieldName[];
70
71 ColumnNameToValueMap PasswordFormToRecord(PasswordForm* form) {
72 ColumnNameToValueMap record;
73 record[kUrlFieldName] = (form->origin).spec();
74 record[kUsernameFieldName] = base::UTF16ToUTF8(form->username_value);
75 record[kPasswordFieldName] = base::UTF16ToUTF8(form->password_value);
76 record[kTitleFieldName] = form->origin.host();
77 return record;
78 }
79
80 DISALLOW_COPY_AND_ASSIGN(PasswordCSVWriter);
81 };
82
83 const base::FilePath::CharType PasswordCSVWriter::kFileExtension[] =
84 FILE_PATH_LITERAL("csv");
85 const char PasswordCSVWriter::kUrlFieldName[] = "url";
86 const char PasswordCSVWriter::kUsernameFieldName[] = "username";
87 const char PasswordCSVWriter::kPasswordFieldName[] = "password";
88 const char PasswordCSVWriter::kTitleFieldName[] = "name";
89
90 // Helper ---------------------------------------------------------------------
91
92 void WriteToFile(const base::FilePath& path, const std::string& data) {
93 base::WriteFile(path, data.c_str(), data.size());
94 }
95
96 } // namespace
97
98 // static
99 void PasswordExporter::Export(
100 const base::FilePath& path,
101 ScopedVector<PasswordForm> passwords,
102 scoped_refptr<base::TaskRunner> blocking_task_runner,
103 const base::Closure& completion) {
104 scoped_ptr<PasswordWriterBase> password_writer(new PasswordCSVWriter);
105
106 bool is_extension_valid = false;
107 if (!path.Extension().empty() &&
108 path.Extension().substr(1) == PasswordCSVWriter::kFileExtension)
109 is_extension_valid = true;
110
111 std::string serialized_passwords =
112 password_writer->SerializePasswords(passwords.Pass());
113 blocking_task_runner->PostTaskAndReply(
114 FROM_HERE,
115 base::Bind(&WriteToFile,
116 is_extension_valid ? path : path.ReplaceExtension(
117 FILE_PATH_LITERAL("csv")),
118 serialized_passwords),
119 completion);
120 }
121
122 // static
123 std::vector<std::vector<base::FilePath::StringType>>
124 PasswordExporter::GetSupportedFileExtensions() {
125 std::vector<std::vector<base::FilePath::StringType>> extensions;
126 extensions.resize(1);
127 extensions[0].push_back(PasswordCSVWriter::kFileExtension);
128 return extensions;
129 }
130
131 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698