Roll WebKit 141933:141963
[chromium-blink-merge.git] / chrome / utility / profile_import_handler.cc
blob473581ce493dc2d111f3c5c7cb312f9bde831f34
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_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, content::UtilityThread::Get(),
40 base::MessageLoopProxy::current());
41 importer_ = importer::CreateImporterByType(source_profile.importer_type);
42 if (!importer_) {
43 Send(new ProfileImportProcessHostMsg_Import_Finished(false,
44 "Importer could not be created."));
45 return;
48 items_to_import_ = items;
50 // Create worker thread in which importer runs.
51 import_thread_.reset(new base::Thread("import_thread"));
52 base::Thread::Options options;
53 options.message_loop_type = MessageLoop::TYPE_IO;
54 if (!import_thread_->StartWithOptions(options)) {
55 NOTREACHED();
56 ImporterCleanup();
58 import_thread_->message_loop()->PostTask(
59 FROM_HERE, base::Bind(&Importer::StartImport, importer_.get(),
60 source_profile, items, bridge_));
63 void ProfileImportHandler::OnImportCancel() {
64 ImporterCleanup();
67 void ProfileImportHandler::OnImportItemFinished(uint16 item) {
68 items_to_import_ ^= item; // Remove finished item from mask.
69 // If we've finished with all items, notify the browser process.
70 if (items_to_import_ == 0) {
71 Send(new ProfileImportProcessHostMsg_Import_Finished(true, ""));
72 ImporterCleanup();
76 void ProfileImportHandler::ImporterCleanup() {
77 importer_->Cancel();
78 importer_ = NULL;
79 bridge_ = NULL;
80 import_thread_.reset();
81 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
84 // static
85 bool ProfileImportHandler::Send(IPC::Message* message) {
86 return content::UtilityThread::Get()->Send(message);
89 } // namespace chrome