Skip to content

Commit

Permalink
Import Data from Safari iOS
Browse files Browse the repository at this point in the history
Add Password Importer
Add History Importer
  • Loading branch information
Brandon-T committed Dec 10, 2024
1 parent f6ac250 commit 67c2c7b
Show file tree
Hide file tree
Showing 42 changed files with 3,208 additions and 268 deletions.
63 changes: 63 additions & 0 deletions components/password_manager/core/browser/import/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2024 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

import("//build/config/ios/rules.gni")
import("//ios/build/config.gni")

source_set("importer") {
sources = [
"safari_import_results.cc",
"safari_import_results.h",
"safari_password_importer.cc",
"safari_password_importer.h",
]
public_deps = [
":csv",
"//base",
"//components/password_manager/core/browser:password_form",
]

deps = [
"//brave/components/password_manager/services/csv_password:lib",
"//brave/components/password_manager/services/csv_password/public/mojom:mojom",
"//build:blink_buildflags",
"//components/password_manager/core/browser/ui",
"//components/password_manager/core/common:constants",
"//components/password_manager/core/common:features",
"//components/sync/base:base",
]

if (use_blink) {
deps +=
[ "//brave/components/password_manager/services/csv_password:service" ]
}

configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

source_set("csv") {
sources = [
"csv_safari_password.cc",
"csv_safari_password.h",
"csv_safari_password_iterator.cc",
"csv_safari_password_iterator.h",
"csv_safari_password_sequence.cc",
"csv_safari_password_sequence.h",
]
public_deps = [
"//base",
"//components/password_manager/core/browser:password_form",
"//components/password_manager/core/browser/import:csv",
]

deps = [
"//build:blink_buildflags",
"//components/affiliations/core/browser:affiliations",
"//components/password_manager/core/browser/form_parsing",
"//url",
]

configs += [ "//build/config/compiler:wexit_time_destructors" ]
}
156 changes: 156 additions & 0 deletions components/password_manager/core/browser/import/csv_safari_password.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/components/password_manager/core/browser/import/csv_safari_password.h"

#include <string_view>
#include <utility>

#include "base/check_op.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/password_manager/core/browser/form_parsing/form_data_parser.h"
#include "components/password_manager/core/browser/import/csv_field_parser.h"
#include "url/gurl.h"

namespace password_manager {

namespace {

std::string ConvertUTF8(std::string_view str) {
std::string str_copy(str);
base::ReplaceSubstringsAfterOffset(&str_copy, 0, "\"\"", "\"");
return str_copy;
}

} // namespace

CSVSafariPassword::CSVSafariPassword() : status_(Status::kSemanticError) {}

CSVSafariPassword::CSVSafariPassword(std::string title,
GURL url,
std::string username,
std::string password,
std::string notes,
GURL otp_auth_url,
Status status)
: title_(title),
url_(std::move(url)),
username_(std::move(username)),
password_(std::move(password)),
notes_(std::move(notes)),
otp_auth_url_(otp_auth_url),
status_(status) {}

CSVSafariPassword::CSVSafariPassword(std::string title,
std::string invalid_url,
std::string username,
std::string password,
std::string notes,
GURL otp_auth_url,
Status status)
: title_(title),
url_(base::unexpected(std::move(invalid_url))),
username_(std::move(username)),
password_(std::move(password)),
notes_(std::move(notes)),
otp_auth_url_(otp_auth_url),
status_(status) {}

CSVSafariPassword::CSVSafariPassword(const ColumnMap& map,
std::string_view row) {
if (row.empty()) {
status_ = Status::kSemanticError;
return;
}

size_t field_idx = 0;
CSVFieldParser parser(row);
status_ = Status::kOK;

while (parser.HasMoreFields()) {
std::string_view field;
if (!parser.NextField(&field)) {
status_ = Status::kSyntaxError;
return;
}
auto meaning_it = map.find(field_idx++);
if (meaning_it == map.end()) {
continue;
}
switch (meaning_it->second) {
case Label::kTitle:
title_ = ConvertUTF8(field);
break;
case Label::kURL: {
GURL gurl = GURL(field);
if (!gurl.is_valid()) {
url_ = base::unexpected(ConvertUTF8(field));
} else {
url_ = gurl;
}
break;
}
case Label::kUsername:
username_ = ConvertUTF8(field);
break;
case Label::kPassword:
password_ = ConvertUTF8(field);
break;
case Label::kNotes:
notes_ = ConvertUTF8(field);
break;
case Label::kOTPAuthURL: {
GURL gurl = GURL(field);
if (!gurl.is_valid()) {
otp_auth_url_ = base::unexpected(ConvertUTF8(field));
} else {
otp_auth_url_ = gurl;
}
break;
}
}
}
}

CSVSafariPassword::CSVSafariPassword(const CSVSafariPassword&) = default;
CSVSafariPassword::CSVSafariPassword(CSVSafariPassword&&) = default;
CSVSafariPassword& CSVSafariPassword::operator=(const CSVSafariPassword&) =
default;
CSVSafariPassword& CSVSafariPassword::operator=(CSVSafariPassword&&) = default;
CSVSafariPassword::~CSVSafariPassword() = default;

const std::string& CSVSafariPassword::GetTitle() const {
return title_;
}

CSVSafariPassword::Status CSVSafariPassword::GetParseStatus() const {
return status_;
}

const std::string& CSVSafariPassword::GetPassword() const {
return password_;
}

const std::string& CSVSafariPassword::GetUsername() const {
return username_;
}

const std::string& CSVSafariPassword::GetNotes() const {
return notes_;
}

const base::expected<GURL, std::string>& CSVSafariPassword::GetURL() const {
return url_;
}

const base::expected<GURL, std::string>& CSVSafariPassword::GetOTPAuthURL()
const {
return otp_auth_url_;
}

} // namespace password_manager
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_SAFARI_PASSWORD_H_
#define BRAVE_COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_SAFARI_PASSWORD_H_

#include <stddef.h>

#include <string>
#include <string_view>

#include "base/containers/flat_map.h"
#include "base/types/expected.h"
#include "url/gurl.h"

namespace password_manager {

class CSVSafariPassword {
public:
enum class Label { kTitle, kURL, kUsername, kPassword, kNotes, kOTPAuthURL };
using ColumnMap = base::flat_map<size_t, Label>;

// Status describes parsing errors.
enum class Status {
kOK = 0,
kSyntaxError = 1,
kSemanticError = 2,
};

CSVSafariPassword();
explicit CSVSafariPassword(const ColumnMap& map, std::string_view csv_row);
explicit CSVSafariPassword(std::string title,
GURL url,
std::string username,
std::string password,
std::string notes,
GURL otp_auth_url,
Status status);
// This constructor creates a valid CSVPassword but with an invalid_url, i.e.
// the url is not a valid GURL.
explicit CSVSafariPassword(std::string title,
std::string invalid_url,
std::string username,
std::string password,
std::string note,
GURL otp_auth_url,
Status status);
CSVSafariPassword(const CSVSafariPassword&);
CSVSafariPassword(CSVSafariPassword&&);
CSVSafariPassword& operator=(const CSVSafariPassword&);
CSVSafariPassword& operator=(CSVSafariPassword&&);
~CSVSafariPassword();

friend bool operator==(const CSVSafariPassword&,
const CSVSafariPassword&) = default;

Status GetParseStatus() const;

const std::string& GetTitle() const;

const std::string& GetPassword() const;

const std::string& GetUsername() const;

const std::string& GetNotes() const;

const base::expected<GURL, std::string>& GetURL() const;

const base::expected<GURL, std::string>& GetOTPAuthURL() const;

private:
std::string title_;
base::expected<GURL, std::string> url_ = base::unexpected("");
std::string username_;
std::string password_;
std::string notes_;
base::expected<GURL, std::string> otp_auth_url_ = base::unexpected("");

Status status_;
};

} // namespace password_manager

#endif // BRAVE_COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_IMPORT_CSV_SAFARI_PASSWORD_H_
Loading

0 comments on commit 67c2c7b

Please sign in to comment.