Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/utility/profile_import_handler.h" | 5 #include "chrome/utility/profile_import_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/common/importer/profile_import_process_messages.h" | |
| 15 #include "chrome/utility/importer/external_process_importer_bridge.h" | 14 #include "chrome/utility/importer/external_process_importer_bridge.h" |
| 16 #include "chrome/utility/importer/importer.h" | 15 #include "chrome/utility/importer/importer.h" |
| 17 #include "chrome/utility/importer/importer_creator.h" | 16 #include "chrome/utility/importer/importer_creator.h" |
| 18 #include "content/public/utility/utility_thread.h" | 17 #include "content/public/utility/utility_thread.h" |
| 18 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 19 | 19 |
| 20 namespace { | 20 // static |
| 21 | 21 void ProfileImportHandler::Create( |
| 22 bool Send(IPC::Message* message) { | 22 mojo::InterfaceRequest<chrome::mojom::ProfileImport> request) { |
| 23 return content::UtilityThread::Get()->Send(message); | 23 mojo::MakeStrongBinding(base::MakeUnique<ProfileImportHandler>(), |
| 24 std::move(request)); | |
| 24 } | 25 } |
| 25 | 26 |
| 26 } // namespace | |
| 27 | |
| 28 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {} | 27 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {} |
| 29 | 28 |
| 30 ProfileImportHandler::~ProfileImportHandler() {} | 29 ProfileImportHandler::~ProfileImportHandler() {} |
| 31 | 30 |
| 32 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) { | 31 void ProfileImportHandler::StartImport( |
| 33 bool handled = true; | |
| 34 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message) | |
| 35 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart) | |
| 36 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel) | |
| 37 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, | |
| 38 OnImportItemFinished) | |
| 39 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 40 IPC_END_MESSAGE_MAP() | |
| 41 return handled; | |
| 42 } | |
| 43 | |
| 44 void ProfileImportHandler::OnImportStart( | |
| 45 const importer::SourceProfile& source_profile, | 32 const importer::SourceProfile& source_profile, |
| 46 uint16_t items, | 33 uint16_t items, |
| 47 const base::DictionaryValue& localized_strings) { | 34 const base::DictionaryValue& localized_strings, |
| 35 chrome::mojom::ProfileImportObserverPtr observer) { | |
| 36 observer_ = std::move(observer); | |
| 48 content::UtilityThread::Get()->EnsureBlinkInitialized(); | 37 content::UtilityThread::Get()->EnsureBlinkInitialized(); |
| 49 bridge_ = new ExternalProcessImporterBridge( | 38 bridge_ = new ExternalProcessImporterBridge( |
| 50 localized_strings, | 39 localized_strings, base::ThreadTaskRunnerHandle::Get().get(), |
| 51 content::UtilityThread::Get(), | 40 observer_.get()); |
| 52 base::ThreadTaskRunnerHandle::Get().get()); | |
| 53 importer_ = importer::CreateImporterByType(source_profile.importer_type); | 41 importer_ = importer::CreateImporterByType(source_profile.importer_type); |
| 54 if (!importer_.get()) { | 42 if (!importer_.get()) { |
| 55 Send(new ProfileImportProcessHostMsg_Import_Finished( | 43 observer_->OnImportFinished(false, "Importer could not be created."); |
|
Sam McNally
2016/11/07 06:31:08
Consider splitting OnImportFinished into its own m
tibell
2016/11/07 23:36:17
Forgot to address this one. We discussed this offl
| |
| 56 false, "Importer could not be created.")); | |
| 57 return; | 44 return; |
| 58 } | 45 } |
| 59 | 46 |
| 60 items_to_import_ = items; | 47 items_to_import_ = items; |
| 61 | 48 |
| 62 // Create worker thread in which importer runs. | 49 // Create worker thread in which importer runs. |
| 63 import_thread_.reset(new base::Thread("import_thread")); | 50 import_thread_.reset(new base::Thread("import_thread")); |
| 64 #if defined(OS_WIN) | 51 #if defined(OS_WIN) |
| 65 import_thread_->init_com_with_mta(false); | 52 import_thread_->init_com_with_mta(false); |
| 66 #endif | 53 #endif |
| 67 if (!import_thread_->Start()) { | 54 if (!import_thread_->Start()) { |
| 68 NOTREACHED(); | 55 NOTREACHED(); |
| 69 ImporterCleanup(); | 56 ImporterCleanup(); |
| 70 } | 57 } |
| 71 import_thread_->task_runner()->PostTask( | 58 import_thread_->task_runner()->PostTask( |
| 72 FROM_HERE, base::Bind(&Importer::StartImport, importer_, | 59 FROM_HERE, base::Bind(&Importer::StartImport, importer_, |
| 73 source_profile, items, base::RetainedRef(bridge_))); | 60 source_profile, items, base::RetainedRef(bridge_))); |
| 74 } | 61 } |
| 75 | 62 |
| 76 void ProfileImportHandler::OnImportCancel() { | 63 void ProfileImportHandler::CancelImport() { |
| 77 ImporterCleanup(); | 64 ImporterCleanup(); |
| 78 } | 65 } |
| 79 | 66 |
| 80 void ProfileImportHandler::OnImportItemFinished(uint16_t item) { | 67 void ProfileImportHandler::ReportImportItemFinished(importer::ImportItem item) { |
| 81 items_to_import_ ^= item; // Remove finished item from mask. | 68 items_to_import_ ^= item; // Remove finished item from mask. |
| 82 // If we've finished with all items, notify the browser process. | 69 // If we've finished with all items, notify the browser process. |
| 83 if (items_to_import_ == 0) { | 70 if (items_to_import_ == 0) { |
| 84 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string())); | 71 observer_->OnImportFinished(true, std::string()); |
| 85 ImporterCleanup(); | 72 ImporterCleanup(); |
| 86 } | 73 } |
| 87 } | 74 } |
| 88 | 75 |
| 89 void ProfileImportHandler::ImporterCleanup() { | 76 void ProfileImportHandler::ImporterCleanup() { |
| 90 importer_->Cancel(); | 77 importer_->Cancel(); |
| 91 importer_ = NULL; | 78 importer_ = NULL; |
| 92 bridge_ = NULL; | 79 bridge_ = NULL; |
| 93 import_thread_.reset(); | 80 import_thread_.reset(); |
| 94 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 81 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
| 95 } | 82 } |
| OLD | NEW |