File: safari_data_importer.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (301 lines) | stat: -rw-r--r-- 11,763 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/user_data_importer/utility/safari_data_importer.h"

#include "base/containers/span_rust.h"
#include "base/files/file_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "components/password_manager/core/browser/ui/saved_passwords_presenter.h"
#include "components/user_data_importer/utility/safari_data_import_manager.h"
#include "components/user_data_importer/utility/zip_ffi_glue.rs.h"

namespace {

std::u16string RustStringToUTF16(const rust::String& rust_string) {
  return base::UTF8ToUTF16(
      std::string_view(rust_string.data(), rust_string.length()));
}

autofill::CreditCard ConvertToAutofillCreditCard(
    const user_data_importer::PaymentCardEntry& card,
    const std::string& app_locale) {
  autofill::CreditCard credit_card;

  credit_card.SetNumber(RustStringToUTF16(card.card_number));
  credit_card.SetNickname(RustStringToUTF16(card.card_name));
  credit_card.SetExpirationMonth(card.card_expiration_month);
  credit_card.SetExpirationYear(card.card_expiration_year);

  // Import all cards as local cards initially. Adding other card types
  // (server, etc) is too complex for an import flow.
  credit_card.set_record_type(autofill::CreditCard::RecordType::kLocalCard);

  credit_card.SetInfo(
      autofill::CREDIT_CARD_NAME_FULL,
      base::UTF8ToUTF16(std::string_view(card.cardholder_name.data(),
                                         card.cardholder_name.length())),
      app_locale);

  return credit_card;
}

}  // namespace

namespace user_data_importer {

SafariDataImporter::SafariDataImporter(
    password_manager::SavedPasswordsPresenter* presenter,
    std::unique_ptr<SafariDataImportManager> manager,
    std::string app_locale)
    : password_importer_(std::make_unique<password_manager::PasswordImporter>(
          presenter,
          /*user_confirmation_required=*/true)),
      task_runner_(base::SequencedTaskRunner::GetCurrentDefault()),
      manager_(std::move(manager)),
      app_locale_(std::move(app_locale)) {}

SafariDataImporter::~SafariDataImporter() = default;

void SafariDataImporter::StartImport(const base::FilePath& path,
                                     PasswordImportCallback passwords_callback,
                                     ImportCallback bookmarks_callback,
                                     ImportCallback history_callback,
                                     ImportCallback payment_cards_callback) {
  std::string zip_filename = path.MaybeAsASCII();
  if (zip_filename.empty()) {
    // Nothing to import, early exit.
    PasswordImportResults results;
    PostCallback(std::move(passwords_callback), std::move(results));
    PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
    PostCallback(std::move(history_callback), /*number_of_imports=*/0);
    PostCallback(std::move(payment_cards_callback), /*number_of_imports=*/0);
    return;
  }

  base::ThreadPool::PostTask(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
      base::BindOnce(&SafariDataImporter::ImportInWorkerThread,
                     base::Unretained(this), std::move(zip_filename),
                     std::move(passwords_callback),
                     std::move(bookmarks_callback), std::move(history_callback),
                     std::move(payment_cards_callback)));
}

void SafariDataImporter::ContinueImport(
    const std::vector<int>& selected_password_ids,
    PasswordImportCallback passwords_callback,
    ImportCallback bookmarks_callback,
    ImportCallback history_callback,
    ImportCallback payment_cards_callback) {
  // TODO(crbug.com/407587751): Launch task on task_runner_.
  password_importer_->ContinueImport(selected_password_ids,
                                     std::move(passwords_callback));

  // TODO(crbug.com/407587751): Import other types here.
  PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
  PostCallback(std::move(payment_cards_callback), /*number_of_imports=*/0);

  base::ThreadPool::PostTask(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
      base::BindOnce(&SafariDataImporter::ImportHistory, base::Unretained(this),
                     std::move(history_callback))
          .Then(base::BindOnce(&SafariDataImporter::CloseZipFileArchive,
                               base::Unretained(this))));
}

// Called after calling "Import" in order to cancel the import process.
void SafariDataImporter::CancelImport() {
  // TODO(crbug.com/407587751): Notify password_importer_.

  CloseZipFileArchive();
}

void SafariDataImporter::CloseZipFileArchive() {
  zip_file_archive_.reset();
}

bool SafariDataImporter::CreateZipFileArchive(std::string zip_filename) {
  const std::vector<uint8_t> zip_filename_span(zip_filename.begin(),
                                               zip_filename.end());
  rust::Slice<const uint8_t> rs_zip_filename =
      base::SpanToRustSlice(zip_filename_span);
  rust::Box<ResultOfZipFileArchive> result_of_zip_file_archive =
      new_archive(rs_zip_filename);

  if (result_of_zip_file_archive->err()) {
    return false;
  }

  zip_file_archive_.emplace(result_of_zip_file_archive->unwrap());
  return true;
}

std::string SafariDataImporter::Unzip(FileType filetype) {
  std::string output_bytes;
  if (!zip_file_archive_ ||
      !(*zip_file_archive_)->unzip(filetype, output_bytes)) {
    return std::string();
  }
  return output_bytes;
}

size_t SafariDataImporter::UncompressedFileSize(FileType filetype) {
  return zip_file_archive_ ? (*zip_file_archive_)->get_file_size(filetype) : 0u;
}

void SafariDataImporter::ImportInWorkerThread(
    std::string zip_filename,
    PasswordImportCallback passwords_callback,
    ImportCallback bookmarks_callback,
    ImportCallback history_callback,
    ImportCallback payment_cards_callback) {
  if (!CreateZipFileArchive(std::move(zip_filename))) {
    // Nothing to import, early exit.
    PasswordImportResults results;
    PostCallback(std::move(passwords_callback), std::move(results));
    PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
    PostCallback(std::move(history_callback), /*number_of_imports=*/0);
    PostCallback(std::move(payment_cards_callback), /*number_of_imports=*/0);
    return;
  }

  // Passwords import may require conflict resolution, so it is done first.
  LaunchImportPasswordsTask(std::move(passwords_callback));

  // Launch payment cards and bookmarks import processes.
  LaunchImportPaymentCardsTask(std::move(payment_cards_callback));
  LaunchImportBookmarksTask(std::move(bookmarks_callback));

  // History import may require synchronously reading from the file, so it is
  // done last in this thread.
  StartImportHistory(std::move(history_callback));
}

void SafariDataImporter::LaunchImportBookmarksTask(
    ImportCallback bookmarks_callback) {
  std::string html_data = Unzip(FileType::Bookmarks);
  if (html_data.empty()) {
    PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
  } else {
    task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&SafariDataImporter::ImportBookmarks,
                                  base::Unretained(this), std::move(html_data),
                                  std::move(bookmarks_callback)));
  }
}

void SafariDataImporter::LaunchImportPasswordsTask(
    PasswordImportCallback passwords_callback) {
  std::string csv_data = Unzip(FileType::Passwords);
  if (csv_data.empty()) {
    PasswordImportResults results;
    PostCallback(std::move(passwords_callback), std::move(results));
  } else {
    task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&SafariDataImporter::ImportPasswords,
                                  base::Unretained(this), std::move(csv_data),
                                  std::move(passwords_callback)));
  }
}

void SafariDataImporter::LaunchImportPaymentCardsTask(
    ImportCallback payment_cards_callback) {
  std::vector<PaymentCardEntry> payment_cards;
  if (!zip_file_archive_ ||
      !(*zip_file_archive_)->parse_payment_cards(payment_cards)) {
    PostCallback(std::move(payment_cards_callback), /*number_of_imports=*/0);
  } else {
    task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&SafariDataImporter::ImportPaymentCards,
                       base::Unretained(this), std::move(payment_cards),
                       std::move(payment_cards_callback)));
  }
}

void SafariDataImporter::PostCallback(auto callback, auto results) {
  // Post the callback back to the original task runner.
  task_runner_->PostTask(FROM_HERE,
                         base::BindOnce(
                             [](auto callback, auto results) {
                               std::move(callback).Run(std::move(results));
                             },
                             std::move(callback), std::move(results)));
}

void SafariDataImporter::ImportPasswords(
    std::string csv_data,
    PasswordImportCallback passwords_callback) {
  // TODO(crbug.com/407587751): Pick a store based on whether the user is
  // signed in to their account.
  password_manager::PasswordForm::Store to_store =
      password_manager::PasswordForm::Store::kAccountStore;

  password_importer_->Import(std::move(csv_data), to_store,
                             std::move(passwords_callback));
}

void SafariDataImporter::ImportPaymentCards(
    std::vector<PaymentCardEntry> payment_cards,
    ImportCallback payment_cards_callback) {
  if (payment_cards.empty()) {
    PostCallback(std::move(payment_cards_callback), /*number_of_imports=*/0);
    return;
  }

  cards_to_import_.clear();

  cards_to_import_.reserve(payment_cards.size());

  std::ranges::transform(payment_cards, std::back_inserter(cards_to_import_),
                         [this](const auto& card) {
                           return ConvertToAutofillCreditCard(card,
                                                              app_locale_);
                         });

  PostCallback(std::move(payment_cards_callback), cards_to_import_.size());
}

void SafariDataImporter::ImportBookmarks(std::string html_data,
                                         ImportCallback bookmarks_callback) {
  if (html_data.empty()) {
    PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
    return;
  }

  // TODO(crbug.com/407587751): Import bookmarks.
  PostCallback(std::move(bookmarks_callback), /*number_of_imports=*/0);
}

void SafariDataImporter::StartImportHistory(ImportCallback history_callback) {
  // This is an approximation of the number of bytes per URL entry in the
  // history file.
  static const size_t kBytesPerURL = 250;
  size_t file_size = UncompressedFileSize(FileType::History);
  size_t approximate_number_of_urls =
      (file_size > 0) ? (file_size / kBytesPerURL) + 1 : 0;
  PostCallback(std::move(history_callback),
               /*number_of_imports=*/approximate_number_of_urls);
}

void SafariDataImporter::ImportHistory(ImportCallback history_callback) {
  // Note: Because the history file can be very large, the parsing will happen
  // entirely in Rust, so that we can stream the unzipper's output to the JSON
  // parser's input.
  std::vector<HistoryEntry> history_entries;
  if (!zip_file_archive_ ||
      !(*zip_file_archive_)->parse_history(history_entries)) {
    PostCallback(std::move(history_callback), /*number_of_imports=*/0);
    return;
  }

  // TODO(crbug.com/407587751): Save imported history.

  PostCallback(std::move(history_callback), history_entries.size());
}

}  // namespace user_data_importer