Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / utility / profile_import_handler.cc
blob62ad7390e67b611a44ff6125bab2edf4d0e01932
1 // Copyright (c) 2012 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.
5 #include "chrome/utility/profile_import_handler.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "chrome/common/importer/profile_import_process_messages.h"
14 #include "chrome/utility/importer/external_process_importer_bridge.h"
15 #include "chrome/utility/importer/importer.h"
16 #include "chrome/utility/importer/importer_creator.h"
17 #include "content/public/utility/utility_thread.h"
19 namespace {
21 bool Send(IPC::Message* message) {
22 return content::UtilityThread::Get()->Send(message);
25 } // namespace
27 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {}
29 ProfileImportHandler::~ProfileImportHandler() {}
31 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) {
32 bool handled = true;
33 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message)
34 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart)
35 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel)
36 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished,
37 OnImportItemFinished)
38 IPC_MESSAGE_UNHANDLED(handled = false)
39 IPC_END_MESSAGE_MAP()
40 return handled;
43 void ProfileImportHandler::OnImportStart(
44 const importer::SourceProfile& source_profile,
45 uint16 items,
46 const base::DictionaryValue& localized_strings) {
47 bridge_ = new ExternalProcessImporterBridge(
48 localized_strings,
49 content::UtilityThread::Get(),
50 base::ThreadTaskRunnerHandle::Get().get());
51 importer_ = importer::CreateImporterByType(source_profile.importer_type);
52 if (!importer_.get()) {
53 Send(new ProfileImportProcessHostMsg_Import_Finished(
54 false, "Importer could not be created."));
55 return;
58 items_to_import_ = items;
60 // Create worker thread in which importer runs.
61 import_thread_.reset(new base::Thread("import_thread"));
62 #if defined(OS_WIN)
63 import_thread_->init_com_with_mta(false);
64 #endif
65 if (!import_thread_->Start()) {
66 NOTREACHED();
67 ImporterCleanup();
69 import_thread_->task_runner()->PostTask(
70 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(),
71 source_profile, items, bridge_));
74 void ProfileImportHandler::OnImportCancel() {
75 ImporterCleanup();
78 void ProfileImportHandler::OnImportItemFinished(uint16 item) {
79 items_to_import_ ^= item; // Remove finished item from mask.
80 // If we've finished with all items, notify the browser process.
81 if (items_to_import_ == 0) {
82 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string()));
83 ImporterCleanup();
87 void ProfileImportHandler::ImporterCleanup() {
88 importer_->Cancel();
89 importer_ = NULL;
90 bridge_ = NULL;
91 import_thread_.reset();
92 content::UtilityThread::Get()->ReleaseProcessIfNeeded();