Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
vabr (Chromium)
2016/03/10 10:53:32
2016
xunlu
2016/03/16 07:23:59
Done.
| |
| 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<ColumnNameToValueMap> 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 ColumnNameToValueMap PasswordFormToRecord(const PasswordForm* form) { | |
| 70 ColumnNameToValueMap record; | |
| 71 record[kUrlFieldName] = (form->origin).spec(); | |
| 72 record[kUsernameFieldName] = base::UTF16ToUTF8(form->username_value); | |
| 73 record[kPasswordFieldName] = base::UTF16ToUTF8(form->password_value); | |
| 74 record[kTitleFieldName] = form->origin.host(); | |
| 75 return record; | |
| 76 } | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(PasswordCSVWriter); | |
| 79 }; | |
| 80 | |
| 81 const base::FilePath::CharType PasswordCSVWriter::kFileExtension[] = | |
| 82 FILE_PATH_LITERAL("csv"); | |
| 83 const char PasswordCSVWriter::kUrlFieldName[] = "url"; | |
| 84 const char PasswordCSVWriter::kUsernameFieldName[] = "username"; | |
| 85 const char PasswordCSVWriter::kPasswordFieldName[] = "password"; | |
| 86 const char PasswordCSVWriter::kTitleFieldName[] = "name"; | |
| 87 | |
| 88 // Helper --------------------------------------------------------------------- | |
| 89 | |
| 90 void WriteToFile(const base::FilePath& path, const std::string& data) { | |
| 91 base::WriteFile(path, data.c_str(), data.size()); | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 | |
| 96 // static | |
| 97 void PasswordExporter::Export( | |
| 98 const base::FilePath& path, | |
| 99 std::vector<scoped_ptr<PasswordForm>> passwords, | |
| 100 scoped_refptr<base::TaskRunner> blocking_task_runner, | |
| 101 const base::Closure& completion) { | |
| 102 scoped_ptr<PasswordWriterBase> password_writer(new PasswordCSVWriter); | |
| 103 | |
| 104 bool is_extension_valid = false; | |
| 105 if (!path.Extension().empty() && | |
| 106 path.Extension().substr(1) == PasswordCSVWriter::kFileExtension) | |
| 107 is_extension_valid = true; | |
| 108 | |
| 109 std::string serialized_passwords = | |
| 110 password_writer->SerializePasswords(passwords); | |
| 111 | |
| 112 if (completion.is_null()) { | |
| 113 blocking_task_runner->PostTask( | |
| 114 FROM_HERE, | |
| 115 base::Bind(&WriteToFile, | |
| 116 is_extension_valid ? path : path.ReplaceExtension( | |
|
vabr (Chromium)
2016/03/10 10:53:32
Do I understand correctly that if the user selects
xunlu
2016/03/16 07:23:59
In the exporter dialog UI there is a drop down for
vabr (Chromium)
2016/03/16 17:48:28
Confusions are one thing, but an overwritten conte
xunlu
2016/03/18 21:15:30
I vaguely remember that we were going to use the e
vabr (Chromium)
2016/03/21 14:01:30
I acknowledge that if support for other formats is
xunlu
2016/03/22 07:17:15
Acknowledged.
| |
| 117 FILE_PATH_LITERAL("csv")), | |
| 118 serialized_passwords)); | |
| 119 | |
| 120 } else { | |
| 121 blocking_task_runner->PostTaskAndReply( | |
| 122 FROM_HERE, | |
| 123 base::Bind(&WriteToFile, | |
| 124 is_extension_valid ? path : path.ReplaceExtension( | |
| 125 FILE_PATH_LITERAL("csv")), | |
| 126 serialized_passwords), | |
| 127 completion); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 // static | |
| 132 std::vector<std::vector<base::FilePath::StringType>> | |
| 133 PasswordExporter::GetSupportedFileExtensions() { | |
| 134 std::vector<std::vector<base::FilePath::StringType>> extensions; | |
| 135 extensions.resize(1); | |
| 136 extensions[0].push_back(PasswordCSVWriter::kFileExtension); | |
| 137 return extensions; | |
| 138 } | |
| 139 | |
| 140 } // namespace password_manager | |
| OLD | NEW |