Index: components/password_manager/core/browser/import/password_importer.cc |
diff --git a/components/password_manager/core/browser/import/password_importer.cc b/components/password_manager/core/browser/import/password_importer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ecf103a546f34df3a95950bd217c2feb5e595623 |
--- /dev/null |
+++ b/components/password_manager/core/browser/import/password_importer.cc |
@@ -0,0 +1,231 @@ |
+// 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/import/password_importer.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/json/json_reader.h" |
+#include "base/location.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "base/task_runner.h" |
+#include "base/task_runner_util.h" |
+#include "base/values.h" |
+#include "components/autofill/core/common/password_form.h" |
+#include "components/password_manager/core/browser/import/csv_reader.h" |
+ |
+namespace password_manager { |
+ |
+namespace { |
+ |
+using autofill::PasswordForm; |
+typedef std::map<std::string, std::string> ColumnNameToValueMap; |
Garrett Casto
2015/06/23 23:42:36
Seems like this typedef should really be in csv_re
xunlu
2015/06/25 17:12:16
Done.
|
+ |
+// PasswordReaderBase --------------------------------------------------------- |
+ |
+// Interface for writing a list of passwords into various formats. |
+class PasswordReaderBase { |
+ public: |
+ virtual ~PasswordReaderBase() = 0; |
+ |
+ // Deserializes a list of passwords from |input| into |passwords|. |
+ virtual PasswordImporter::Result DeserializePasswords( |
+ const std::string& input, |
+ std::vector<PasswordForm>* passwords) = 0; |
+}; |
+ |
+PasswordReaderBase::~PasswordReaderBase() { |
+} |
+ |
+// PasswordCSVReader ---------------------------------------------------------- |
+ |
+class PasswordCSVReader : public PasswordReaderBase { |
+ public: |
+ static const base::FilePath::CharType kFileExtension[]; |
+ |
+ PasswordCSVReader() {} |
+ |
+ // PasswordWriterBase: |
+ PasswordImporter::Result DeserializePasswords( |
+ const std::string& input, |
+ std::vector<PasswordForm>* passwords) override { |
+ std::vector<std::string> header; |
+ std::vector<ColumnNameToValueMap> records; |
+ if (!ReadCSV(input, &header, &records)) |
+ return PasswordImporter::SYNTAX_ERROR; |
+ |
+ if (!GetActualFieldName(header, kUrlFieldNames, url_field_name_) || |
+ !GetActualFieldName(header, kUsernameFieldNames, |
+ username_field_name_) || |
+ !GetActualFieldName(header, kPasswordFieldNames, password_field_name_)) |
+ return PasswordImporter::SEMANTICAL_ERROR; |
Garrett Casto
2015/06/23 23:42:36
This should be SEMANTIC_ERROR. Semantical isn't ac
xunlu
2015/06/25 17:12:16
Done.
|
+ |
+ passwords->clear(); |
+ passwords->reserve(records.size()); |
+ |
+ typedef std::vector<ColumnNameToValueMap>::const_iterator |
+ ConstRecordIterator; |
Garrett Casto
2015/06/23 23:42:36
I wouldn't use a typedef just for a one time use.
xunlu
2015/06/25 17:12:16
Done.
|
+ for (ConstRecordIterator it = records.begin(); it != records.end(); ++it) { |
+ PasswordForm form; |
+ if (RecordToPasswordForm(*it, &form)) |
+ passwords->push_back(form); |
+ } |
+ return PasswordImporter::SUCCESS; |
+ } |
+ |
+ private: |
+ static const std::vector<std::string> kUrlFieldNames; |
+ static const std::vector<std::string> kUsernameFieldNames; |
+ static const std::vector<std::string> kPasswordFieldNames; |
+ |
+ std::string url_field_name_; |
+ std::string username_field_name_; |
+ std::string password_field_name_; |
+ |
+ bool RecordToPasswordForm(const ColumnNameToValueMap& record, |
+ PasswordForm* form) { |
+ if (!record.count(url_field_name_) || !record.count(username_field_name_) || |
+ !record.count(password_field_name_)) |
Garrett Casto
2015/06/23 23:42:36
Generally we use braces when the "if" statement is
xunlu
2015/06/25 17:12:16
Done.
|
+ return false; |
+ form->origin = GURL(record.at(url_field_name_)); |
+ form->username_value = base::UTF8ToUTF16(record.at(username_field_name_)); |
+ form->password_value = base::UTF8ToUTF16(record.at(password_field_name_)); |
+ return true; |
+ } |
+ |
+ bool GetActualFieldName(const std::vector<std::string>& header, |
+ const std::vector<std::string>& options, |
+ std::string& field_name) { |
+ std::vector<std::string>::const_iterator it = header.begin(); |
+ for (; it != header.end(); ++it) { |
+ if (std::count(options.begin(), options.end(), |
+ base::StringToLowerASCII(*it))) { |
+ field_name = *it; |
+ break; |
+ } |
+ } |
+ return !(it == header.end()); |
+ } |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader); |
+}; |
+ |
+const base::FilePath::CharType PasswordCSVReader::kFileExtension[] = ".csv"; |
Garrett Casto
2015/06/23 23:42:36
I'm pretty sure that this needs to be " = FILE_PAT
xunlu
2015/06/25 17:12:16
Done.
|
+const std::vector<std::string> PasswordCSVReader::kUrlFieldNames = {"url", |
+ "website", |
+ "origin", |
+ "hostname"}; |
+const std::vector<std::string> PasswordCSVReader::kUsernameFieldNames = |
+ {"username", "user", "login", "account"}; |
+const std::vector<std::string> PasswordCSVReader::kPasswordFieldNames = { |
+ "password"}; |
+ |
+// PasswordJSONReader --------------------------------------------------------- |
Garrett Casto
2015/06/23 23:42:36
Assuming we aren't going to support this functiona
xunlu
2015/06/25 17:12:16
Done.
|
+ |
+class PasswordJSONReader : public PasswordReaderBase { |
+ public: |
+ static const base::FilePath::CharType kFileExtension[]; |
+ |
+ PasswordJSONReader() {} |
+ |
+ // PasswordReaderBase: |
+ PasswordImporter::Result DeserializePasswords( |
+ const std::string& input, |
+ std::vector<PasswordForm>* passwords) override { |
+ scoped_ptr<base::Value> parsed_json(base::JSONReader::Read(input)); |
+ if (!parsed_json) |
+ return PasswordImporter::SYNTAX_ERROR; |
+ |
+ const base::ListValue* password_dictionaries; |
+ if (!parsed_json->GetAsList(&password_dictionaries)) |
+ return PasswordImporter::SEMANTICAL_ERROR; |
+ |
+ for (base::ListValue::const_iterator it = password_dictionaries->begin(); |
+ it != password_dictionaries->end(); ++it) { |
+ PasswordForm form; |
+ const base::DictionaryValue* password_dict; |
+ if ((*it)->GetAsDictionary(&password_dict) && |
+ DictionaryToPasswordForm(*password_dict, &form)) |
+ passwords->push_back(form); |
+ } |
+ return PasswordImporter::SUCCESS; |
+ } |
+ |
+ private: |
+ bool DictionaryToPasswordForm(const base::DictionaryValue& dictionary, |
+ PasswordForm* form) { |
+ std::string origin; |
+ if (!dictionary.GetString("url", &origin)) |
+ return false; |
+ form->origin = GURL(origin); |
+ if (!dictionary.GetString("username", &form->username_value)) |
+ return false; |
+ if (!dictionary.GetString("password", &form->password_value)) |
+ return false; |
+ return true; |
+ } |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PasswordJSONReader); |
+}; |
+ |
+const base::FilePath::CharType PasswordJSONReader::kFileExtension[] = ".json"; |
+ |
+// Helpers -------------------------------------------------------------------- |
+ |
+// Reads and returns the contents of the file at |path| as a string, or returns |
+// a scoped point containing a NULL if there was an error. |
+scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) { |
+ scoped_ptr<std::string> contents(new std::string); |
+ if (!base::ReadFileToString(path, contents.get())) |
+ return scoped_ptr<std::string>(); |
+ return contents.Pass(); |
+} |
+ |
+// Parses passwords from |input| using |password_reader| and synchronously calls |
+// |completion| with the results. |
+static void ParsePasswords( |
+ scoped_ptr<PasswordReaderBase> password_reader, |
+ const PasswordImporter::CompletionCallback& completion, |
+ scoped_ptr<std::string> input) { |
+ std::vector<PasswordForm> passwords; |
+ PasswordImporter::Result result = PasswordImporter::IO_ERROR; |
+ if (input) |
+ result = password_reader->DeserializePasswords(*input, &passwords); |
+ completion.Run(result, passwords); |
+} |
+ |
+} // namespace |
+ |
+// static |
+void PasswordImporter::Import( |
+ const base::FilePath& path, |
+ scoped_refptr<base::TaskRunner> blocking_task_runner, |
+ const CompletionCallback& completion) { |
+ scoped_ptr<PasswordReaderBase> password_reader; |
+ if (path.Extension() == PasswordCSVReader::kFileExtension) { |
+ password_reader.reset(new PasswordCSVReader); |
+ } else if (path.Extension() == PasswordJSONReader::kFileExtension) { |
+ password_reader.reset(new PasswordJSONReader); |
+ } else { |
+ NOTREACHED() << "Unsupported extension: " << path.Extension(); |
+ } |
+ |
+ base::PostTaskAndReplyWithResult( |
+ blocking_task_runner.get(), FROM_HERE, |
+ base::Bind(&ReadFileToString, path), |
+ base::Bind(&ParsePasswords, base::Passed(&password_reader), completion)); |
+} |
+ |
+// static |
+std::vector<std::string> PasswordImporter::GetSupportedFileExtensions() { |
+ std::vector<std::string> extensions; |
+ extensions.push_back(PasswordCSVReader::kFileExtension); |
+ // TODO(xunlu) re-enable JSON when we are ready support it |
+ // extensions.push_back(PasswordJSONReader::kFileExtension); |
+ return extensions; |
+} |
+ |
+} // namespace password_manager |