 Chromium Code Reviews
 Chromium Code Reviews Issue 1193143003:
  Enable import/export of passwords into/from Password Manager  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1193143003:
  Enable import/export of passwords into/from Password Manager  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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..513a5f3cc88e3ccd7a2bdc3a8afbd08bf60c35bd | 
| --- /dev/null | 
| +++ b/components/password_manager/core/browser/export/password_exporter.cc | 
| @@ -0,0 +1,169 @@ | 
| +// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// 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_path.h" | 
| +#include "base/files/file_util.h" | 
| +#include "base/json/json_writer.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; | 
| + | 
| +typedef std::map<std::string, std::string> ColumnNameToValueMap; | 
| + | 
| +// 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( | 
| + ScopedVector<PasswordForm> passwords) = 0; | 
| +}; | 
| + | 
| +PasswordWriterBase::~PasswordWriterBase() { | 
| +} | 
| + | 
| +// PasswordCSVWriter ---------------------------------------------------------- | 
| + | 
| +class PasswordCSVWriter : public PasswordWriterBase { | 
| + public: | 
| + static const base::FilePath::CharType kFileExtension[]; | 
| + | 
| + PasswordCSVWriter() {} | 
| + | 
| + // PasswordWriterBase: | 
| + std::string SerializePasswords( | 
| + ScopedVector<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 (ScopedVector<PasswordForm>::const_iterator it = passwords.begin(); | 
| + it != passwords.end(); ++it) { | 
| + records.push_back(PasswordFormToRecord(*it)); | 
| + } | 
| + 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(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[] = ".csv"; | 
| +const char PasswordCSVWriter::kUrlFieldName[] = "url"; | 
| +const char PasswordCSVWriter::kUsernameFieldName[] = "username"; | 
| +const char PasswordCSVWriter::kPasswordFieldName[] = "password"; | 
| +const char PasswordCSVWriter::kTitleFieldName[] = "name"; | 
| + | 
| +// PasswordJSONWriter --------------------------------------------------------- | 
| 
Garrett Casto
2015/06/23 23:42:36
Same as the importer, I wouldn't add this if we ar
 
xunlu
2015/06/25 17:12:16
Done.
 | 
| + | 
| +class PasswordJSONWriter : public PasswordWriterBase { | 
| + public: | 
| + static const base::FilePath::CharType kFileExtension[]; | 
| + | 
| + PasswordJSONWriter() {} | 
| + | 
| + // PasswordWriterBase: | 
| + std::string SerializePasswords( | 
| + ScopedVector<PasswordForm> passwords) override { | 
| + scoped_ptr<base::ListValue> password_dictionaries(new base::ListValue); | 
| + for (std::vector<PasswordForm*>::const_iterator it = passwords.begin(); | 
| + it != passwords.end(); ++it) { | 
| + password_dictionaries->Append(PasswordFormToDictionary(*it).release()); | 
| + } | 
| + | 
| + std::string result; | 
| + if (!base::JSONWriter::Write(*password_dictionaries.get(), &result)) | 
| + NOTREACHED(); | 
| + return result; | 
| + } | 
| + | 
| + private: | 
| + scoped_ptr<base::DictionaryValue> PasswordFormToDictionary( | 
| + PasswordForm* form) { | 
| + scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); | 
| + dictionary->SetString("url", form->origin.spec()); | 
| + dictionary->SetString("username", form->username_value); | 
| + dictionary->SetString("password", form->password_value); | 
| + return dictionary.Pass(); | 
| + } | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(PasswordJSONWriter); | 
| +}; | 
| + | 
| +const base::FilePath::CharType PasswordJSONWriter::kFileExtension[] = ".json"; | 
| + | 
| +// 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, | 
| + ScopedVector<PasswordForm> passwords, | 
| + scoped_refptr<base::TaskRunner> blocking_task_runner, | 
| + const base::Closure& completion) { | 
| + scoped_ptr<PasswordWriterBase> password_writer; | 
| + if (path.Extension() == PasswordCSVWriter::kFileExtension) | 
| + password_writer.reset(new PasswordCSVWriter); | 
| + else if (path.Extension() == PasswordJSONWriter::kFileExtension) | 
| + password_writer.reset(new PasswordJSONWriter); | 
| + else | 
| + NOTREACHED(); | 
| + | 
| + std::string serialized_passwords = | 
| + password_writer->SerializePasswords(passwords.Pass()); | 
| + blocking_task_runner->PostTaskAndReply( | 
| + FROM_HERE, base::Bind(&WriteToFile, path, serialized_passwords), | 
| + completion); | 
| +} | 
| + | 
| +// static | 
| +std::vector<std::string> GetSupportedFileExtensions() { | 
| + std::vector<std::string> extensions; | 
| + extensions.push_back(PasswordCSVWriter::kFileExtension); | 
| + // TODO(xunlu) re-enable JSON when we are ready support it | 
| + // extensions.push_back(PasswordJSONWriter::kFileExtension); | 
| + return extensions; | 
| +} | 
| + | 
| +} // namespace password_manager |