1 // Copyright 2019 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 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_PASSWORD_H_ 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_PASSWORD_H_ 7 8 #include <stddef.h> 9 10 #include "base/containers/flat_map.h" 11 #include "base/strings/string_piece.h" 12 #include "components/password_manager/core/browser/password_form.h" 13 14 namespace password_manager { 15 16 // CSVPassword is a wrapper around one CSV line representing a credential. 17 // For more details, see 18 // https://docs.google.com/document/d/1wsZBl93S_WGaXZqrqq5SP08LVZ0zDKf6e9nlptyl9AY/edit?usp=sharing. 19 class CSVPassword { 20 public: 21 enum class Label { kOrigin, kUsername, kPassword }; 22 using ColumnMap = base::flat_map<size_t, Label>; 23 24 // Status describes parsing errors. 25 enum class Status { kOK, kSyntaxError, kSemanticError }; 26 27 // Number of values in the Label enum. 28 static constexpr size_t kLabelCount = 3; 29 30 explicit CSVPassword(const ColumnMap& map, base::StringPiece csv_row); 31 CSVPassword(const CSVPassword&) = delete; 32 CSVPassword(CSVPassword&&) = delete; 33 CSVPassword& operator=(const CSVPassword&) = delete; 34 CSVPassword& operator=(CSVPassword&&) = delete; 35 ~CSVPassword(); 36 37 // Returns whether the associated CSV row can be parsed successfully. If 38 // returning success, it also stores the parsed result in |*form|. 39 Status Parse(PasswordForm* form) const; 40 // TryParse() returns the same value as Parse(). However, TryParse() does not 41 // attempt to create and store the corresponding PasswordForm anywhere. 42 // Therefore TryParse() is faster than Parse() and a better choice for only 43 // checking a correctness of a CSV serialization of a credential. 44 Status TryParse() const; 45 // Convenience wrapper around Parse() for cases known to be correctly 46 // parseable. 47 PasswordForm ParseValid() const; 48 49 private: 50 // ParseImpl is the common base of Parse() and TryParse(). 51 Status ParseImpl(PasswordForm* form) const; 52 53 // The members |map_| and |row_| are only modified in constructor or 54 // operator=(). 55 56 // |map_| stores the meaning of particular columns in the row. 57 const ColumnMap& map_; 58 // |row_| contains the CSV row from which the PasswordForm is parsed. 59 base::StringPiece row_; 60 }; 61 62 } // namespace password_manager 63 64 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_PASSWORD_H_ 65