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. 4 5 #include "chrome/utility/profile_import_handler.h" 6 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" 16 17 namespace { 18 19 bool Send(IPC::Message* message) { 20 return content::UtilityThread::Get()->Send(message); 21 } 22 23 } // namespace 24 25 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {} 26 27 ProfileImportHandler::~ProfileImportHandler() {} 28 29 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) { 30 bool handled = true; 31 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message) 32 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart) 33 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel) 34 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, 35 OnImportItemFinished) 36 IPC_MESSAGE_UNHANDLED(handled = false) 37 IPC_END_MESSAGE_MAP() 38 return handled; 39 } 40 41 void ProfileImportHandler::OnImportStart( 42 const importer::SourceProfile& source_profile, 43 uint16 items, 44 const base::DictionaryValue& localized_strings) { 45 bridge_ = new ExternalProcessImporterBridge( 46 localized_strings, 47 content::UtilityThread::Get(), 48 base::MessageLoopProxy::current().get()); 49 importer_ = importer::CreateImporterByType(source_profile.importer_type); 50 if (!importer_.get()) { 51 Send(new ProfileImportProcessHostMsg_Import_Finished( 52 false, "Importer could not be created.")); 53 return; 54 } 55 56 items_to_import_ = items; 57 58 // Create worker thread in which importer runs. 59 import_thread_.reset(new base::Thread("import_thread")); 60 #if defined(OS_WIN) 61 import_thread_->init_com_with_mta(false); 62 #endif 63 if (!import_thread_->Start()) { 64 NOTREACHED(); 65 ImporterCleanup(); 66 } 67 import_thread_->message_loop()->PostTask( 68 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(), 69 source_profile, items, bridge_)); 70 } 71 72 void ProfileImportHandler::OnImportCancel() { 73 ImporterCleanup(); 74 } 75 76 void ProfileImportHandler::OnImportItemFinished(uint16 item) { 77 items_to_import_ ^= item; // Remove finished item from mask. 78 // If we've finished with all items, notify the browser process. 79 if (items_to_import_ == 0) { 80 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string())); 81 ImporterCleanup(); 82 } 83 } 84 85 void ProfileImportHandler::ImporterCleanup() { 86 importer_->Cancel(); 87 importer_ = NULL; 88 bridge_ = NULL; 89 import_thread_.reset(); 90 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); 91 } 92