OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/profile_import/profile_import_thread.h" | 5 #include "chrome/profile_import/profile_import_thread.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/importer/importer.h" | 10 #include "chrome/browser/importer/importer.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 } | 25 } |
26 | 26 |
27 ProfileImportThread::ProfileImportThread() | 27 ProfileImportThread::ProfileImportThread() |
28 : bridge_(NULL), | 28 : bridge_(NULL), |
29 browser_type_(0), | 29 browser_type_(0), |
30 items_to_import_(0), | 30 items_to_import_(0), |
31 importer_(NULL) { | 31 importer_(NULL) { |
32 ChildProcess::current()->AddRefProcess(); // Balanced in Cleanup(). | 32 ChildProcess::current()->AddRefProcess(); // Balanced in Cleanup(). |
33 } | 33 } |
34 | 34 |
35 ProfileImportThread::~ProfileImportThread() {} | |
36 | |
37 void ProfileImportThread::OnControlMessageReceived(const IPC::Message& msg) { | 35 void ProfileImportThread::OnControlMessageReceived(const IPC::Message& msg) { |
38 IPC_BEGIN_MESSAGE_MAP(ProfileImportThread, msg) | 36 IPC_BEGIN_MESSAGE_MAP(ProfileImportThread, msg) |
39 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, | 37 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, |
40 OnImportStart) | 38 OnImportStart) |
41 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, | 39 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, |
42 OnImportCancel) | 40 OnImportCancel) |
43 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, | 41 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished, |
44 OnImportItemFinished) | 42 OnImportItemFinished) |
45 IPC_END_MESSAGE_MAP() | 43 IPC_END_MESSAGE_MAP() |
46 } | 44 } |
47 | 45 |
48 void ProfileImportThread::OnImportStart( | 46 void ProfileImportThread::OnImportStart( |
49 const ProfileInfo& profile_info, | 47 const ProfileInfo& profile_info, |
50 int items, | 48 int items, |
51 const DictionaryValue& localized_strings, | 49 const DictionaryValue& localized_strings, |
52 bool import_to_bookmark_bar) { | 50 bool import_to_bookmark_bar) { |
53 bridge_ = new ExternalProcessImporterBridge(this, localized_strings); | 51 bridge_ = new ExternalProcessImporterBridge(this, localized_strings); |
| 52 bridge_->AddRef(); // Balanced in Cleanup(). |
54 | 53 |
55 ImporterList importer_list; | 54 ImporterList importer_list; |
56 importer_ = importer_list.CreateImporterByType(profile_info.browser_type); | 55 importer_ = importer_list.CreateImporterByType(profile_info.browser_type); |
57 if (!importer_) { | 56 if (!importer_) { |
58 Send(new ProfileImportProcessHostMsg_Import_Finished(false, | 57 Send(new ProfileImportProcessHostMsg_Import_Finished(false, |
59 "Importer could not be created.")); | 58 "Importer could not be created.")); |
60 return; | 59 return; |
61 } | 60 } |
62 | 61 |
| 62 importer_->AddRef(); // Balanced in Cleanup(). |
63 importer_->set_import_to_bookmark_bar(import_to_bookmark_bar); | 63 importer_->set_import_to_bookmark_bar(import_to_bookmark_bar); |
64 items_to_import_ = items; | 64 items_to_import_ = items; |
65 | 65 |
66 // Create worker thread in which importer runs. | 66 // Create worker thread in which importer runs. |
67 import_thread_.reset(new base::Thread("import_thread")); | 67 import_thread_.reset(new base::Thread("import_thread")); |
68 base::Thread::Options options; | 68 base::Thread::Options options; |
69 options.message_loop_type = MessageLoop::TYPE_IO; | 69 options.message_loop_type = MessageLoop::TYPE_IO; |
70 if (!import_thread_->StartWithOptions(options)) { | 70 if (!import_thread_->StartWithOptions(options)) { |
71 NOTREACHED(); | 71 NOTREACHED(); |
72 Cleanup(); | 72 Cleanup(); |
73 } | 73 } |
74 import_thread_->message_loop()->PostTask( | 74 import_thread_->message_loop()->PostTask(FROM_HERE, |
75 FROM_HERE, | 75 NewRunnableMethod(importer_, &Importer::StartImport, |
76 NewRunnableMethod( | 76 profile_info, items, bridge_)); |
77 importer_.get(), | |
78 &Importer::StartImport, | |
79 profile_info, | |
80 items, | |
81 bridge_)); | |
82 } | 77 } |
83 | 78 |
84 void ProfileImportThread::OnImportCancel() { | 79 void ProfileImportThread::OnImportCancel() { |
85 Cleanup(); | 80 Cleanup(); |
86 } | 81 } |
87 | 82 |
88 void ProfileImportThread::OnImportItemFinished(uint16 item) { | 83 void ProfileImportThread::OnImportItemFinished(uint16 item) { |
89 items_to_import_ ^= item; // Remove finished item from mask. | 84 items_to_import_ ^= item; // Remove finished item from mask. |
90 // If we've finished with all items, notify the browser process. | 85 // If we've finished with all items, notify the browser process. |
91 if (items_to_import_ == 0) | 86 if (items_to_import_ == 0) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 std::vector<TemplateURL> urls; | 177 std::vector<TemplateURL> urls; |
183 for (size_t i = 0; i < template_urls.size(); ++i) { | 178 for (size_t i = 0; i < template_urls.size(); ++i) { |
184 urls.push_back(*template_urls[i]); | 179 urls.push_back(*template_urls[i]); |
185 } | 180 } |
186 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(urls, | 181 Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(urls, |
187 default_keyword_index, unique_on_host_and_path)); | 182 default_keyword_index, unique_on_host_and_path)); |
188 } | 183 } |
189 | 184 |
190 void ProfileImportThread::Cleanup() { | 185 void ProfileImportThread::Cleanup() { |
191 importer_->Cancel(); | 186 importer_->Cancel(); |
192 importer_ = NULL; | 187 importer_->Release(); |
193 bridge_ = NULL; | 188 bridge_->Release(); |
194 ChildProcess::current()->ReleaseProcess(); | 189 ChildProcess::current()->ReleaseProcess(); |
195 } | 190 } |
OLD | NEW |