Get foreground tab on Android
[chromium-blink-merge.git] / chrome / utility / profile_import_handler.cc
blob457269db73c1af2fe79b6bcc4e5367c07f56bd99
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/memory/ref_counted.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/threading/thread.h"
11 #include "chrome/common/importer/profile_import_process_messages.h"
12 #include "chrome/utility/importer/external_process_importer_bridge.h"
13 #include "chrome/utility/importer/importer.h"
14 #include "chrome/utility/importer/importer_creator.h"
15 #include "content/public/utility/utility_thread.h"
17 namespace chrome {
19 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {}
21 ProfileImportHandler::~ProfileImportHandler() {}
23 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) {
24 bool handled = true;
25 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message)
26 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart)
27 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel)
28 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished,
29 OnImportItemFinished)
30 IPC_MESSAGE_UNHANDLED(handled = false)
31 IPC_END_MESSAGE_MAP()
32 return handled;
35 void ProfileImportHandler::OnImportStart(
36 const importer::SourceProfile& source_profile,
37 uint16 items,
38 const base::DictionaryValue& localized_strings) {
39 bridge_ = new ExternalProcessImporterBridge(
40 localized_strings,
41 content::UtilityThread::Get(),
42 base::MessageLoopProxy::current().get());
43 importer_ = importer::CreateImporterByType(source_profile.importer_type);
44 if (!importer_.get()) {
45 Send(new ProfileImportProcessHostMsg_Import_Finished(
46 false, "Importer could not be created."));
47 return;
50 items_to_import_ = items;
52 // Create worker thread in which importer runs.
53 import_thread_.reset(new base::Thread("import_thread"));
54 #if defined(OS_WIN)
55 import_thread_->init_com_with_mta(false);
56 #endif
57 if (!import_thread_->Start()) {
58 NOTREACHED();
59 ImporterCleanup();
61 import_thread_->message_loop()->PostTask(
62 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(),
63 source_profile, items, bridge_));
66 void ProfileImportHandler::OnImportCancel() {
67 ImporterCleanup();
70 void ProfileImportHandler::OnImportItemFinished(uint16 item) {
71 items_to_import_ ^= item; // Remove finished item from mask.
72 // If we've finished with all items, notify the browser process.
73 if (items_to_import_ == 0) {
74 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string()));
75 ImporterCleanup();
79 void ProfileImportHandler::ImporterCleanup() {
80 importer_->Cancel();
81 importer_ = NULL;
82 bridge_ = NULL;
83 import_thread_.reset();
84 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
87 // static
88 bool ProfileImportHandler::Send(IPC::Message* message) {
89 return content::UtilityThread::Get()->Send(message);
92 } // namespace chrome