Commit ac8353dc authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[Passwords] Simplify CSVPasswordSequence

This change simplifies the NameToLabel helper inside CSVPasswordSequence
to use the new base::fixed_flat_map and return a simple pointer instead
of a base::Optional.

Bug: 934326
Change-Id: I1325a7470151ee31af37dcc21919d696d829edbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2544606Reviewed-by: default avatarFriedrich [CET] <fhorschig@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828807}
parent 5f174dae
...@@ -8,10 +8,9 @@ ...@@ -8,10 +8,9 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/containers/flat_map.h" #include "base/containers/fixed_flat_map.h"
#include "base/containers/flat_set.h" #include "base/containers/flat_set.h"
#include "base/no_destructor.h" #include "base/strings/string_piece.h"
#include "base/optional.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "components/password_manager/core/browser/import/csv_field_parser.h" #include "components/password_manager/core/browser/import/csv_field_parser.h"
#include "components/password_manager/core/browser/import/csv_password.h" #include "components/password_manager/core/browser/import/csv_password.h"
...@@ -21,13 +20,13 @@ namespace password_manager { ...@@ -21,13 +20,13 @@ namespace password_manager {
namespace { namespace {
// Given a CSV column |name|, returns the matching CSVPassword::Label or nothing // Given a CSV column |name|, returns a pointer to the matching
// if the column name is not recognised. // CSVPassword::Label or nullptr if the column name is not recognised.
base::Optional<CSVPassword::Label> NameToLabel(base::StringPiece name) { const CSVPassword::Label* NameToLabel(base::StringPiece name) {
using Label = CSVPassword::Label; using Label = CSVPassword::Label;
// Recognised column names for origin URL, usernames and passwords. // Recognised column names for origin URL, usernames and passwords.
static const base::NoDestructor<base::flat_map<base::StringPiece, Label>> static constexpr auto kLabelMap =
kLabelMap({ base::MakeFixedFlatMap<base::StringPiece, Label>({
{"url", Label::kOrigin}, {"url", Label::kOrigin},
{"website", Label::kOrigin}, {"website", Label::kOrigin},
{"origin", Label::kOrigin}, {"origin", Label::kOrigin},
...@@ -40,9 +39,8 @@ base::Optional<CSVPassword::Label> NameToLabel(base::StringPiece name) { ...@@ -40,9 +39,8 @@ base::Optional<CSVPassword::Label> NameToLabel(base::StringPiece name) {
{"password", Label::kPassword}, {"password", Label::kPassword},
}); });
auto it = kLabelMap->find(base::ToLowerASCII(name)); auto* it = kLabelMap.find(base::ToLowerASCII(name));
return it != kLabelMap->end() ? base::make_optional(it->second) return it != kLabelMap.end() ? &it->second : nullptr;
: base::nullopt;
} }
} // namespace } // namespace
...@@ -65,10 +63,9 @@ CSVPasswordSequence::CSVPasswordSequence(std::string csv) ...@@ -65,10 +63,9 @@ CSVPasswordSequence::CSVPasswordSequence(std::string csv)
result_ = CSVPassword::Status::kSyntaxError; result_ = CSVPassword::Status::kSyntaxError;
return; return;
} }
base::Optional<CSVPassword::Label> label = NameToLabel(name);
if (label) { if (const CSVPassword::Label* label = NameToLabel(name))
map_[col_index] = *label; map_[col_index] = *label;
}
} }
// Check that each of the three labels is assigned to exactly one column. // Check that each of the three labels is assigned to exactly one column.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment