Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 // Currently, CSV is the only supported format. | |
| 103 scoped_ptr<PasswordWriterBase> password_writer(new PasswordCSVWriter); | |
|
vabr (Chromium)
2016/03/22 15:28:28
nit: This even does not have to be a scoped_ptr, j
xunlu
2016/03/23 04:50:51
Done.
| |
| 104 | |
| 105 std::string serialized_passwords = | |
| 106 password_writer->SerializePasswords(passwords); | |
| 107 | |
| 108 blocking_task_runner->PostTask( | |
| 109 FROM_HERE, base::Bind(&WriteToFile, path, serialized_passwords)); | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 std::vector<std::vector<base::FilePath::StringType>> | |
| 114 PasswordExporter::GetSupportedFileExtensions() { | |
| 115 std::vector<std::vector<base::FilePath::StringType>> extensions; | |
| 116 extensions.resize(1); | |
| 117 extensions[0].push_back(PasswordCSVWriter::kFileExtension); | |
| 118 return extensions; | |
| 119 } | |
| 120 | |
| 121 } // namespace password_manager | |
| OLD | NEW |