Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // 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.
| |
| 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/import/password_importer.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "base/task_runner.h" | |
| 13 #include "base/task_runner_util.h" | |
| 14 #include "base/values.h" | |
| 15 #include "components/autofill/core/common/password_form.h" | |
| 16 #include "components/password_manager/core/browser/import/csv_reader.h" | |
| 17 | |
| 18 namespace password_manager { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 using autofill::PasswordForm; | |
| 23 | |
| 24 // PasswordReaderBase --------------------------------------------------------- | |
| 25 | |
| 26 // Interface for writing a list of passwords into various formats. | |
| 27 class PasswordReaderBase { | |
| 28 public: | |
| 29 virtual ~PasswordReaderBase() = 0; | |
| 30 | |
| 31 // Deserializes a list of passwords from |input| into |passwords|. | |
| 32 virtual PasswordImporter::Result DeserializePasswords( | |
| 33 const std::string& input, | |
| 34 std::vector<PasswordForm>* passwords) = 0; | |
| 35 }; | |
| 36 | |
| 37 PasswordReaderBase::~PasswordReaderBase() {} | |
| 38 | |
| 39 // PasswordCSVReader ---------------------------------------------------------- | |
| 40 | |
| 41 class PasswordCSVReader : public PasswordReaderBase { | |
| 42 public: | |
| 43 static const base::FilePath::CharType kFileExtension[]; | |
| 44 | |
| 45 PasswordCSVReader() {} | |
| 46 | |
| 47 // PasswordWriterBase: | |
| 48 PasswordImporter::Result DeserializePasswords( | |
| 49 const std::string& input, | |
| 50 std::vector<PasswordForm>* passwords) override { | |
| 51 std::vector<std::string> header; | |
| 52 std::vector<ColumnNameToValueMap> records; | |
| 53 if (!ReadCSV(input, &header, &records)) | |
| 54 return PasswordImporter::SYNTAX_ERROR; | |
| 55 | |
| 56 if (!GetActualFieldName(header, GetURLFieldNames(), url_field_name_) || | |
| 57 !GetActualFieldName(header, GetUsernameFieldNames(), | |
| 58 username_field_name_) || | |
| 59 !GetActualFieldName(header, GetPasswordFieldNames(), | |
| 60 password_field_name_)) | |
| 61 return PasswordImporter::SEMANTIC_ERROR; | |
| 62 | |
| 63 passwords->clear(); | |
| 64 passwords->reserve(records.size()); | |
| 65 | |
| 66 for (std::vector<ColumnNameToValueMap>::const_iterator it = records.begin(); | |
|
vabr (Chromium)
2016/03/10 10:53:32
nit: Consider a range-based for loop.
As a bonus,
xunlu
2016/03/16 07:23:59
Done.
vabr (Chromium)
2016/03/16 17:48:28
for (int i = 0...) is not a range-based for loop.
xunlu
2016/03/18 21:15:30
Done.
| |
| 67 it != records.end(); ++it) { | |
| 68 PasswordForm form; | |
| 69 if (RecordToPasswordForm(*it, &form)) | |
| 70 passwords->push_back(form); | |
| 71 } | |
| 72 return PasswordImporter::SUCCESS; | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 std::string url_field_name_; | |
| 77 std::string username_field_name_; | |
| 78 std::string password_field_name_; | |
| 79 | |
| 80 const std::vector<std::string> GetURLFieldNames() { | |
| 81 std::vector<std::string> url_names; | |
| 82 url_names.push_back("url"); | |
| 83 url_names.push_back("website"); | |
| 84 url_names.push_back("origin"); | |
| 85 url_names.push_back("hostname"); | |
| 86 return url_names; | |
| 87 } | |
| 88 | |
| 89 const std::vector<std::string> GetUsernameFieldNames() { | |
| 90 std::vector<std::string> username_names; | |
| 91 username_names.push_back("username"); | |
| 92 username_names.push_back("user"); | |
| 93 username_names.push_back("login"); | |
| 94 username_names.push_back("account"); | |
| 95 return username_names; | |
| 96 } | |
| 97 | |
| 98 const std::vector<std::string> GetPasswordFieldNames() { | |
| 99 std::vector<std::string> password_names; | |
| 100 password_names.push_back("password"); | |
| 101 return password_names; | |
| 102 } | |
| 103 | |
| 104 bool RecordToPasswordForm(const ColumnNameToValueMap& record, | |
| 105 PasswordForm* form) { | |
| 106 if (!record.count(url_field_name_) || !record.count(username_field_name_) || | |
| 107 !record.count(password_field_name_)) { | |
| 108 return false; | |
| 109 } | |
| 110 form->origin = GURL(record.at(url_field_name_)); | |
| 111 form->signon_realm = form->origin.GetOrigin().spec(); | |
| 112 form->username_value = base::UTF8ToUTF16(record.at(username_field_name_)); | |
| 113 form->password_value = base::UTF8ToUTF16(record.at(password_field_name_)); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool GetActualFieldName(const std::vector<std::string>& header, | |
| 118 const std::vector<std::string>& options, | |
| 119 std::string& field_name) { | |
| 120 std::vector<std::string>::const_iterator it = header.begin(); | |
|
vabr (Chromium)
2016/03/10 10:53:32
Please consider rewriting this with std::find_if a
xunlu
2016/03/16 07:23:59
Done.
| |
| 121 for (; it != header.end(); ++it) { | |
| 122 if (std::count(options.begin(), options.end(), base::ToLowerASCII(*it))) { | |
| 123 field_name = *it; | |
| 124 break; | |
| 125 } | |
| 126 } | |
| 127 return !(it == header.end()); | |
| 128 } | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(PasswordCSVReader); | |
| 131 }; | |
| 132 | |
| 133 const base::FilePath::CharType PasswordCSVReader::kFileExtension[] = | |
| 134 FILE_PATH_LITERAL("csv"); | |
| 135 | |
| 136 // Helpers -------------------------------------------------------------------- | |
| 137 | |
| 138 // Reads and returns the contents of the file at |path| as a string, or returns | |
| 139 // a scoped point containing a NULL if there was an error. | |
| 140 scoped_ptr<std::string> ReadFileToString(const base::FilePath& path) { | |
| 141 scoped_ptr<std::string> contents(new std::string); | |
| 142 if (!base::ReadFileToString(path, contents.get())) | |
| 143 return scoped_ptr<std::string>(); | |
| 144 return contents; | |
| 145 } | |
| 146 | |
| 147 // Parses passwords from |input| using |password_reader| and synchronously calls | |
| 148 // |completion| with the results. | |
| 149 static void ParsePasswords( | |
| 150 scoped_ptr<PasswordReaderBase> password_reader, | |
| 151 const PasswordImporter::CompletionCallback& completion, | |
| 152 scoped_ptr<std::string> input) { | |
| 153 std::vector<PasswordForm> passwords; | |
| 154 PasswordImporter::Result result = PasswordImporter::IO_ERROR; | |
| 155 if (input) | |
| 156 result = password_reader->DeserializePasswords(*input, &passwords); | |
| 157 completion.Run(result, passwords); | |
| 158 } | |
| 159 | |
| 160 } // namespace | |
| 161 | |
| 162 // static | |
| 163 void PasswordImporter::Import( | |
| 164 const base::FilePath& path, | |
| 165 scoped_refptr<base::TaskRunner> blocking_task_runner, | |
| 166 const CompletionCallback& completion) { | |
| 167 scoped_ptr<PasswordReaderBase> password_reader; | |
| 168 if (path.Extension().substr(1) == PasswordCSVReader::kFileExtension) { | |
| 169 password_reader.reset(new PasswordCSVReader); | |
| 170 } else { | |
| 171 NOTREACHED() << "Unsupported extension: " << path.Extension(); | |
|
vabr (Chromium)
2016/03/10 10:53:32
The extension is user-specified, so it can happen
xunlu
2016/03/16 07:23:59
Same concern as I explained in the Exporter commen
| |
| 172 } | |
| 173 | |
| 174 base::PostTaskAndReplyWithResult( | |
| 175 blocking_task_runner.get(), FROM_HERE, | |
| 176 base::Bind(&ReadFileToString, path), | |
| 177 base::Bind(&ParsePasswords, base::Passed(&password_reader), completion)); | |
| 178 } | |
| 179 | |
| 180 // static | |
| 181 std::vector<std::vector<base::FilePath::StringType>> | |
| 182 PasswordImporter::GetSupportedFileExtensions() { | |
| 183 std::vector<std::vector<base::FilePath::StringType>> extensions; | |
| 184 extensions.resize(1); | |
| 185 extensions[0].push_back(PasswordCSVReader::kFileExtension); | |
| 186 return extensions; | |
| 187 } | |
| 188 | |
| 189 } // namespace password_manager | |
| OLD | NEW |