File: import_data_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (280 lines) | stat: -rw-r--r-- 9,534 bytes parent folder | download | duplicates (5)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/settings/import_data_handler.h"

#include <stddef.h>

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/importer/external_process_importer_host.h"
#include "chrome/browser/importer/importer_list.h"
#include "chrome/browser/importer/importer_uma.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
#include "ui/shell_dialogs/selected_file_info.h"

using content::BrowserThread;

namespace settings {

namespace {
const char kImportStatusInProgress[] = "inProgress";
const char kImportStatusSucceeded[] = "succeeded";
const char kImportStatusFailed[] = "failed";
}  // namespace

ImportDataHandler::ImportDataHandler() : importer_host_(nullptr) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

ImportDataHandler::~ImportDataHandler() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (importer_host_) {
    importer_host_->set_observer(nullptr);
  }

  if (select_file_dialog_.get()) {
    select_file_dialog_->ListenerDestroyed();
  }
}

void ImportDataHandler::RegisterMessages() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  web_ui()->RegisterMessageCallback(
      "initializeImportDialog",
      base::BindRepeating(&ImportDataHandler::HandleInitializeImportDialog,
                          base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "importData", base::BindRepeating(&ImportDataHandler::HandleImportData,
                                        base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "importFromBookmarksFile",
      base::BindRepeating(&ImportDataHandler::HandleImportFromBookmarksFile,
                          base::Unretained(this)));
}

void ImportDataHandler::OnJavascriptDisallowed() {
  // Cancels outstanding profile list detections.
  importer_list_.reset();

  // When the WebUI is unloading, we ignore all further updates from the host.
  // Because we're no longer listening to the `ImportEnded` callback, we must
  // also clear our pointer, as otherwise this can lead to a use-after-free
  // in the destructor. https://crbug.com/1302813.
  if (importer_host_) {
    importer_host_->set_observer(nullptr);
    importer_host_ = nullptr;
  }
}

void ImportDataHandler::StartImport(
    const user_data_importer::SourceProfile& source_profile,
    uint16_t imported_items) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!imported_items) {
    return;
  }

  // If another import is already ongoing, let it finish silently.
  if (importer_host_) {
    importer_host_->set_observer(nullptr);
  }

  FireWebUIListener("import-data-status-changed",
                    base::Value(kImportStatusInProgress));
  import_did_succeed_ = false;

  importer_host_ = new ExternalProcessImporterHost();
  importer_host_->set_observer(this);
  Profile* profile = Profile::FromWebUI(web_ui());
  importer_host_->StartImportSettings(source_profile, profile, imported_items,
                                      new ProfileWriter(profile));

  importer::LogImporterUseToMetrics("ImportDataHandler",
                                    source_profile.importer_type);
}

void ImportDataHandler::HandleImportData(const base::Value::List& args) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  const auto& list = args;
  CHECK_GE(list.size(), 2u);

  int browser_index = list[0].GetInt();

  const base::Value& types = list[1];
  CHECK(types.is_dict());

  if (!importer_list_loaded_ || browser_index < 0 ||
      browser_index >= static_cast<int>(importer_list_->count())) {
    // Prevent out-of-bounds access.
    return;
  }

  const base::Value::Dict& type_dict = types.GetDict();
  uint16_t selected_items = user_data_importer::NONE;
  if (*type_dict.FindBool(prefs::kImportDialogAutofillFormData)) {
    selected_items |= user_data_importer::AUTOFILL_FORM_DATA;
  }
  if (*type_dict.FindBool(prefs::kImportDialogBookmarks)) {
    selected_items |= user_data_importer::FAVORITES;
  }
  if (*type_dict.FindBool(prefs::kImportDialogHistory)) {
    selected_items |= user_data_importer::HISTORY;
  }
  if (*type_dict.FindBool(prefs::kImportDialogSavedPasswords)) {
    selected_items |= user_data_importer::PASSWORDS;
  }
  if (*type_dict.FindBool(prefs::kImportDialogSearchEngine)) {
    selected_items |= user_data_importer::SEARCH_ENGINES;
  }

  const user_data_importer::SourceProfile& source_profile =
      importer_list_->GetSourceProfileAt(browser_index);
  uint16_t supported_items = source_profile.services_supported;

  uint16_t imported_items = (selected_items & supported_items);
  if (imported_items) {
    StartImport(source_profile, imported_items);
  } else {
    LOG(WARNING) << "There were no settings to import from '"
                 << source_profile.importer_name << "'.";
  }
}

void ImportDataHandler::HandleInitializeImportDialog(
    const base::Value::List& args) {
  AllowJavascript();

  CHECK_EQ(1U, args.size());
  const std::string& callback_id = args[0].GetString();

  importer_list_ = std::make_unique<ImporterList>();
  importer_list_->DetectSourceProfiles(
      g_browser_process->GetApplicationLocale(),
      true,  // include_interactive_profiles
      base::BindOnce(&ImportDataHandler::SendBrowserProfileData,
                     base::Unretained(this), callback_id));
}

void ImportDataHandler::HandleImportFromBookmarksFile(
    const base::Value::List& args) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (select_file_dialog_) {
    return;
  }

  DCHECK(args.empty());
  select_file_dialog_ = ui::SelectFileDialog::Create(
      this,
      std::make_unique<ChromeSelectFilePolicy>(web_ui()->GetWebContents()));

  ui::SelectFileDialog::FileTypeInfo file_type_info;
  file_type_info.extensions.resize(1);
  file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));

  Browser* browser = chrome::FindBrowserWithTab(web_ui()->GetWebContents());

  select_file_dialog_->SelectFile(
      ui::SelectFileDialog::SELECT_OPEN_FILE, std::u16string(),
      base::FilePath(), &file_type_info, 0, base::FilePath::StringType(),
      browser->window()->GetNativeWindow(), nullptr);
}

void ImportDataHandler::SendBrowserProfileData(const std::string& callback_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  importer_list_loaded_ = true;

  base::Value::List browser_profiles;
  for (size_t i = 0; i < importer_list_->count(); ++i) {
    const user_data_importer::SourceProfile& source_profile =
        importer_list_->GetSourceProfileAt(i);
    uint16_t browser_services = source_profile.services_supported;

    base::Value::Dict browser_profile;
    browser_profile.Set("name", source_profile.importer_name);
    browser_profile.Set("index", static_cast<int>(i));
    browser_profile.Set("profileName", source_profile.profile);
    browser_profile.Set("history",
                        (browser_services & user_data_importer::HISTORY) != 0);
    browser_profile.Set(
        "favorites", (browser_services & user_data_importer::FAVORITES) != 0);
    browser_profile.Set(
        "passwords", (browser_services & user_data_importer::PASSWORDS) != 0);
    browser_profile.Set(
        "search", (browser_services & user_data_importer::SEARCH_ENGINES) != 0);
    browser_profile.Set(
        "autofillFormData",
        (browser_services & user_data_importer::AUTOFILL_FORM_DATA) != 0);

    browser_profiles.Append(std::move(browser_profile));
  }

  ResolveJavascriptCallback(base::Value(callback_id), browser_profiles);
}

void ImportDataHandler::ImportStarted() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void ImportDataHandler::ImportItemStarted(user_data_importer::ImportItem item) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // TODO(csilv): show progress detail in the web view.
}

void ImportDataHandler::ImportItemEnded(user_data_importer::ImportItem item) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // TODO(csilv): show progress detail in the web view.
  import_did_succeed_ = true;
}

void ImportDataHandler::ImportEnded() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  importer_host_->set_observer(nullptr);
  importer_host_ = nullptr;

  FireWebUIListener("import-data-status-changed",
                    base::Value(import_did_succeed_ ? kImportStatusSucceeded
                                                    : kImportStatusFailed));
}

void ImportDataHandler::FileSelected(const ui::SelectedFileInfo& file,
                                     int /*index*/) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  select_file_dialog_ = nullptr;

  user_data_importer::SourceProfile source_profile;
  source_profile.importer_type = user_data_importer::TYPE_BOOKMARKS_FILE;
  source_profile.source_path = file.path();

  StartImport(source_profile, user_data_importer::FAVORITES);
}

void ImportDataHandler::FileSelectionCanceled() {
  select_file_dialog_ = nullptr;
}

}  // namespace settings