Index: components/password_manager/core/browser/export/password_exporter.cc |
diff --git a/components/password_manager/core/browser/export/password_exporter.cc b/components/password_manager/core/browser/export/password_exporter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..91073c2c73f9371225ffc67f347330d1a44d1273 |
--- /dev/null |
+++ b/components/password_manager/core/browser/export/password_exporter.cc |
@@ -0,0 +1,140 @@ |
+// 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.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/password_manager/core/browser/export/password_exporter.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_util.h" |
+#include "base/location.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "base/task_runner.h" |
+#include "base/values.h" |
+#include "components/autofill/core/common/password_form.h" |
+#include "components/password_manager/core/browser/export/csv_writer.h" |
+ |
+namespace password_manager { |
+ |
+namespace { |
+ |
+using autofill::PasswordForm; |
+ |
+// PasswordWriterBase --------------------------------------------------------- |
+ |
+// Interface for writing a list of passwords into various formats. |
+class PasswordWriterBase { |
+ public: |
+ virtual ~PasswordWriterBase() = 0; |
+ |
+ // Serializes the list of |passwords|. |
+ virtual std::string SerializePasswords( |
+ std::vector<scoped_ptr<PasswordForm>>& passwords) = 0; |
+}; |
+ |
+PasswordWriterBase::~PasswordWriterBase() {} |
+ |
+// PasswordCSVWriter ---------------------------------------------------------- |
+ |
+class PasswordCSVWriter : public PasswordWriterBase { |
+ public: |
+ static const base::FilePath::CharType kFileExtension[]; |
+ |
+ PasswordCSVWriter() {} |
+ |
+ // PasswordWriterBase: |
+ std::string SerializePasswords( |
+ std::vector<scoped_ptr<PasswordForm>>& passwords) override { |
+ std::vector<std::string> header; |
+ header.push_back(kTitleFieldName); |
+ header.push_back(kUrlFieldName); |
+ header.push_back(kUsernameFieldName); |
+ header.push_back(kPasswordFieldName); |
+ |
+ std::vector<ColumnNameToValueMap> records; |
+ records.reserve(passwords.size()); |
+ for (const auto& it : passwords) { |
+ records.push_back(PasswordFormToRecord(it.get())); |
+ } |
+ std::string result; |
+ WriteCSV(header, records, &result); |
+ return result; |
+ } |
+ |
+ private: |
+ static const char kUrlFieldName[]; |
+ static const char kUsernameFieldName[]; |
+ static const char kPasswordFieldName[]; |
+ static const char kTitleFieldName[]; |
+ |
+ ColumnNameToValueMap PasswordFormToRecord(const PasswordForm* form) { |
+ ColumnNameToValueMap record; |
+ record[kUrlFieldName] = (form->origin).spec(); |
+ record[kUsernameFieldName] = base::UTF16ToUTF8(form->username_value); |
+ record[kPasswordFieldName] = base::UTF16ToUTF8(form->password_value); |
+ record[kTitleFieldName] = form->origin.host(); |
+ return record; |
+ } |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PasswordCSVWriter); |
+}; |
+ |
+const base::FilePath::CharType PasswordCSVWriter::kFileExtension[] = |
+ FILE_PATH_LITERAL("csv"); |
+const char PasswordCSVWriter::kUrlFieldName[] = "url"; |
+const char PasswordCSVWriter::kUsernameFieldName[] = "username"; |
+const char PasswordCSVWriter::kPasswordFieldName[] = "password"; |
+const char PasswordCSVWriter::kTitleFieldName[] = "name"; |
+ |
+// Helper --------------------------------------------------------------------- |
+ |
+void WriteToFile(const base::FilePath& path, const std::string& data) { |
+ base::WriteFile(path, data.c_str(), data.size()); |
+} |
+ |
+} // namespace |
+ |
+// static |
+void PasswordExporter::Export( |
+ const base::FilePath& path, |
+ std::vector<scoped_ptr<PasswordForm>> passwords, |
+ scoped_refptr<base::TaskRunner> blocking_task_runner, |
+ const base::Closure& completion) { |
+ scoped_ptr<PasswordWriterBase> password_writer(new PasswordCSVWriter); |
+ |
+ bool is_extension_valid = false; |
+ if (!path.Extension().empty() && |
+ path.Extension().substr(1) == PasswordCSVWriter::kFileExtension) |
+ is_extension_valid = true; |
+ |
+ std::string serialized_passwords = |
+ password_writer->SerializePasswords(passwords); |
+ |
+ if (completion.is_null()) { |
+ blocking_task_runner->PostTask( |
+ FROM_HERE, |
+ base::Bind(&WriteToFile, |
+ 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.
|
+ FILE_PATH_LITERAL("csv")), |
+ serialized_passwords)); |
+ |
+ } else { |
+ blocking_task_runner->PostTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(&WriteToFile, |
+ is_extension_valid ? path : path.ReplaceExtension( |
+ FILE_PATH_LITERAL("csv")), |
+ serialized_passwords), |
+ completion); |
+ } |
+} |
+ |
+// static |
+std::vector<std::vector<base::FilePath::StringType>> |
+PasswordExporter::GetSupportedFileExtensions() { |
+ std::vector<std::vector<base::FilePath::StringType>> extensions; |
+ extensions.resize(1); |
+ extensions[0].push_back(PasswordCSVWriter::kFileExtension); |
+ return extensions; |
+} |
+ |
+} // namespace password_manager |