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 using chrome::mojom::ThreadSafeProfileImportObserverPtr; |
21 | |
22 bool Send(IPC::Message* message) { | |
23 return content::UtilityThread::Get()->Send(message); | |
24 } | |
25 | |
26 } // namespace | |
27 | 21 |
28 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {} | 22 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {} |
29 | 23 |
30 ProfileImportHandler::~ProfileImportHandler() {} | 24 ProfileImportHandler::~ProfileImportHandler() {} |
31 | 25 |
32 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) { | 26 // static |
33 bool handled = true; | 27 void ProfileImportHandler::Create( |
34 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message) | 28 mojo::InterfaceRequest<chrome::mojom::ProfileImport> request) { |
35 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart) | 29 mojo::MakeStrongBinding(base::MakeUnique<ProfileImportHandler>(), |
36 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel) | 30 std::move(request)); |
37 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, | |
38 OnImportItemFinished) | |
39 IPC_MESSAGE_UNHANDLED(handled = false) | |
40 IPC_END_MESSAGE_MAP() | |
41 return handled; | |
42 } | 31 } |
43 | 32 |
44 void ProfileImportHandler::OnImportStart( | 33 void ProfileImportHandler::StartImport( |
45 const importer::SourceProfile& source_profile, | 34 const importer::SourceProfile& source_profile, |
46 uint16_t items, | 35 uint16_t items, |
47 const base::DictionaryValue& localized_strings) { | 36 const base::DictionaryValue& localized_strings, |
| 37 chrome::mojom::ProfileImportObserverPtr observer) { |
48 content::UtilityThread::Get()->EnsureBlinkInitialized(); | 38 content::UtilityThread::Get()->EnsureBlinkInitialized(); |
49 bridge_ = new ExternalProcessImporterBridge( | |
50 localized_strings, | |
51 content::UtilityThread::Get(), | |
52 base::ThreadTaskRunnerHandle::Get().get()); | |
53 importer_ = importer::CreateImporterByType(source_profile.importer_type); | 39 importer_ = importer::CreateImporterByType(source_profile.importer_type); |
54 if (!importer_.get()) { | 40 if (!importer_.get()) { |
55 Send(new ProfileImportProcessHostMsg_Import_Finished( | 41 observer->OnImportFinished(false, "Importer could not be created."); |
56 false, "Importer could not be created.")); | |
57 return; | 42 return; |
58 } | 43 } |
59 | 44 |
60 items_to_import_ = items; | 45 items_to_import_ = items; |
61 | 46 |
62 // Create worker thread in which importer runs. | 47 // Create worker thread in which importer runs. |
63 import_thread_.reset(new base::Thread("import_thread")); | 48 import_thread_.reset(new base::Thread("import_thread")); |
64 #if defined(OS_WIN) | 49 #if defined(OS_WIN) |
65 import_thread_->init_com_with_mta(false); | 50 import_thread_->init_com_with_mta(false); |
66 #endif | 51 #endif |
67 if (!import_thread_->Start()) { | 52 if (!import_thread_->Start()) { |
68 NOTREACHED(); | 53 NOTREACHED(); |
69 ImporterCleanup(); | 54 ImporterCleanup(); |
70 } | 55 } |
| 56 bridge_ = new ExternalProcessImporterBridge( |
| 57 localized_strings, |
| 58 ThreadSafeProfileImportObserverPtr::Create(std::move(observer))); |
71 import_thread_->task_runner()->PostTask( | 59 import_thread_->task_runner()->PostTask( |
72 FROM_HERE, base::Bind(&Importer::StartImport, importer_, | 60 FROM_HERE, base::Bind(&Importer::StartImport, importer_, |
73 source_profile, items, base::RetainedRef(bridge_))); | 61 source_profile, items, base::RetainedRef(bridge_))); |
74 } | 62 } |
75 | 63 |
76 void ProfileImportHandler::OnImportCancel() { | 64 void ProfileImportHandler::CancelImport() { |
77 ImporterCleanup(); | 65 ImporterCleanup(); |
78 } | 66 } |
79 | 67 |
80 void ProfileImportHandler::OnImportItemFinished(uint16_t item) { | 68 void ProfileImportHandler::ReportImportItemFinished(importer::ImportItem item) { |
81 items_to_import_ ^= item; // Remove finished item from mask. | 69 items_to_import_ ^= item; // Remove finished item from mask. |
82 // 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())); | |
85 ImporterCleanup(); | 71 ImporterCleanup(); |
86 } | 72 } |
87 } | 73 } |
88 | 74 |
89 void ProfileImportHandler::ImporterCleanup() { | 75 void ProfileImportHandler::ImporterCleanup() { |
90 importer_->Cancel(); | 76 importer_->Cancel(); |
91 importer_ = NULL; | 77 importer_ = NULL; |
92 bridge_ = NULL; | 78 bridge_ = NULL; |
93 import_thread_.reset(); | 79 import_thread_.reset(); |
94 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 80 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); |
95 } | 81 } |
OLD | NEW |