OLD | NEW |
(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 |
| 114 if (completion.is_null()) { |
| 115 blocking_task_runner->PostTask( |
| 116 FROM_HERE, |
| 117 base::Bind(&WriteToFile, |
| 118 is_extension_valid ? path : path.ReplaceExtension( |
| 119 FILE_PATH_LITERAL("csv")), |
| 120 serialized_passwords)); |
| 121 |
| 122 } else { |
| 123 blocking_task_runner->PostTaskAndReply( |
| 124 FROM_HERE, |
| 125 base::Bind(&WriteToFile, |
| 126 is_extension_valid ? path : path.ReplaceExtension( |
| 127 FILE_PATH_LITERAL("csv")), |
| 128 serialized_passwords), |
| 129 completion); |
| 130 } |
| 131 } |
| 132 |
| 133 // static |
| 134 std::vector<std::vector<base::FilePath::StringType>> |
| 135 PasswordExporter::GetSupportedFileExtensions() { |
| 136 std::vector<std::vector<base::FilePath::StringType>> extensions; |
| 137 extensions.resize(1); |
| 138 extensions[0].push_back(PasswordCSVWriter::kFileExtension); |
| 139 return extensions; |
| 140 } |
| 141 |
| 142 } // namespace password_manager |
OLD | NEW |