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"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/threading/thread.h"
11 #include "chrome/browser/importer/external_process_importer_bridge.h"
12 #include "chrome/browser/importer/importer.h"
13 #include "chrome/browser/importer/profile_import_process_messages.h"
14 #include "content/public/utility/utility_thread.h"
18 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {}
20 ProfileImportHandler::~ProfileImportHandler() {}
22 bool ProfileImportHandler::OnMessageReceived(const IPC::Message
& message
) {
24 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler
, message
)
25 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport
, OnImportStart
)
26 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport
, OnImportCancel
)
27 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished
,
29 IPC_MESSAGE_UNHANDLED(handled
= false)
34 void ProfileImportHandler::OnImportStart(
35 const importer::SourceProfile
& source_profile
,
37 const base::DictionaryValue
& localized_strings
) {
38 bridge_
= new ExternalProcessImporterBridge(
40 content::UtilityThread::Get(),
41 base::MessageLoopProxy::current().get());
42 importer_
= importer::CreateImporterByType(source_profile
.importer_type
);
43 if (!importer_
.get()) {
44 Send(new ProfileImportProcessHostMsg_Import_Finished(
45 false, "Importer could not be created."));
49 items_to_import_
= items
;
51 // Create worker thread in which importer runs.
52 import_thread_
.reset(new base::Thread("import_thread"));
54 import_thread_
->init_com_with_mta(false);
56 if (!import_thread_
->Start()) {
60 import_thread_
->message_loop()->PostTask(
61 FROM_HERE
, base::Bind(&Importer::StartImport
, importer_
.get(),
62 source_profile
, items
, bridge_
));
65 void ProfileImportHandler::OnImportCancel() {
69 void ProfileImportHandler::OnImportItemFinished(uint16 item
) {
70 items_to_import_
^= item
; // Remove finished item from mask.
71 // If we've finished with all items, notify the browser process.
72 if (items_to_import_
== 0) {
73 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string()));
78 void ProfileImportHandler::ImporterCleanup() {
82 import_thread_
.reset();
83 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
87 bool ProfileImportHandler::Send(IPC::Message
* message
) {
88 return content::UtilityThread::Get()->Send(message
);