Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / chrome / utility / profile_import_handler.cc
blob79cf0bed8fac2f310f589a2bc01f2af4c706746b
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/browser/importer/external_process_importer_bridge.h"
12 #include "chrome/browser/importer/importer.h"
13 #include "chrome/browser/importer/profile_import_process_messages.h"
14 #include "content/public/utility/utility_thread.h"
16 namespace chrome {
18 ProfileImportHandler::ProfileImportHandler() : items_to_import_(0) {}
20 ProfileImportHandler::~ProfileImportHandler() {}
22 bool ProfileImportHandler::OnMessageReceived(const IPC::Message& message) {
23 bool handled = true;
24 IPC_BEGIN_MESSAGE_MAP(ProfileImportHandler, message)
25 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport, OnImportStart)
26 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport, OnImportCancel)
27 IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished,
28 OnImportItemFinished)
29 IPC_MESSAGE_UNHANDLED(handled = false)
30 IPC_END_MESSAGE_MAP()
31 return handled;
34 void ProfileImportHandler::OnImportStart(
35 const importer::SourceProfile& source_profile,
36 uint16 items,
37 const base::DictionaryValue& localized_strings) {
38 bridge_ = new ExternalProcessImporterBridge(
39 localized_strings,
40 content::UtilityThread::Get(),
41 base::MessageLoopProxy::current().get());
42 importer_ = importer::CreateImporterByType(source_profile.importer_type);
43 if (!importer_.get()) {
44 Send(new ProfileImportProcessHostMsg_Import_Finished(
45 false, "Importer could not be created."));
46 return;
49 items_to_import_ = items;
51 // Create worker thread in which importer runs.
52 import_thread_.reset(new base::Thread("import_thread"));
53 #if defined(OS_WIN)
54 import_thread_->init_com_with_mta(false);
55 #endif
56 if (!import_thread_->Start()) {
57 NOTREACHED();
58 ImporterCleanup();
60 import_thread_->message_loop()->PostTask(
61 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(),
62 source_profile, items, bridge_));
65 void ProfileImportHandler::OnImportCancel() {
66 ImporterCleanup();
69 void ProfileImportHandler::OnImportItemFinished(uint16 item) {
70 items_to_import_ ^= item; // Remove finished item from mask.
71 // If we've finished with all items, notify the browser process.
72 if (items_to_import_ == 0) {
73 Send(new ProfileImportProcessHostMsg_Import_Finished(true, std::string()));
74 ImporterCleanup();
78 void ProfileImportHandler::ImporterCleanup() {
79 importer_->Cancel();
80 importer_ = NULL;
81 bridge_ = NULL;
82 import_thread_.reset();
83 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
86 // static
87 bool ProfileImportHandler::Send(IPC::Message* message) {
88 return content::UtilityThread::Get()->Send(message);
91 } // namespace chrome