Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / extensions / zipfile_installer.cc
blob6f64b606f53ade82df31f9d14f932919ba535981
1 // Copyright 2014 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/browser/extensions/zipfile_installer.h"
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/extensions/extension_error_reporter.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/unpacked_installer.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using content::BrowserThread;
20 using content::UtilityProcessHost;
22 namespace {
24 const char kExtensionHandlerTempDirError[] =
25 "Could not create temporary directory for zipped extension.";
27 } // namespace
29 namespace extensions {
31 ZipFileInstaller::ZipFileInstaller(ExtensionService* extension_service)
32 : be_noisy_on_failure_(true),
33 extension_service_weak_(extension_service->AsWeakPtr()) {
36 void ZipFileInstaller::LoadFromZipFile(const base::FilePath& path) {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38 zip_path_ = path;
39 BrowserThread::PostTask(BrowserThread::FILE,
40 FROM_HERE,
41 base::Bind(&ZipFileInstaller::PrepareTempDir, this));
44 void ZipFileInstaller::PrepareTempDir() {
45 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
46 base::FilePath temp_dir;
47 PathService::Get(base::DIR_TEMP, &temp_dir);
48 base::FilePath new_temp_dir;
49 if (!base::CreateTemporaryDirInDir(
50 temp_dir,
51 zip_path_.RemoveExtension().BaseName().value() +
52 FILE_PATH_LITERAL("_"),
53 &new_temp_dir)) {
54 OnUnzipFailed(std::string(kExtensionHandlerTempDirError));
55 return;
57 BrowserThread::PostTask(
58 BrowserThread::IO,
59 FROM_HERE,
60 base::Bind(&ZipFileInstaller::StartWorkOnIOThread, this, new_temp_dir));
63 ZipFileInstaller::~ZipFileInstaller() {
66 // static
67 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create(
68 ExtensionService* extension_service) {
69 DCHECK(extension_service);
70 return scoped_refptr<ZipFileInstaller>(
71 new ZipFileInstaller(extension_service));
74 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) {
75 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
76 UtilityProcessHost* host =
77 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get());
78 host->SetName(l10n_util::GetStringUTF16(
79 IDS_UTILITY_PROCESS_ZIP_FILE_INSTALLER_NAME));
80 host->SetExposedDir(temp_dir);
81 host->Send(new ChromeUtilityMsg_UnzipToDir(zip_path_, temp_dir));
84 void ZipFileInstaller::ReportSuccessOnUIThread(
85 const base::FilePath& unzipped_path) {
86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 if (extension_service_weak_.get())
88 UnpackedInstaller::Create(extension_service_weak_.get())
89 ->Load(unzipped_path);
92 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) {
93 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94 if (extension_service_weak_.get()) {
95 ExtensionErrorReporter::GetInstance()->ReportLoadError(
96 zip_path_,
97 error,
98 extension_service_weak_->profile(),
99 be_noisy_on_failure_);
103 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath& unzipped_path) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105 BrowserThread::PostTask(
106 BrowserThread::UI,
107 FROM_HERE,
108 base::Bind(
109 &ZipFileInstaller::ReportSuccessOnUIThread, this, unzipped_path));
112 void ZipFileInstaller::OnUnzipFailed(const std::string& error) {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
114 BrowserThread::PostTask(
115 BrowserThread::UI,
116 FROM_HERE,
117 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, error));
120 bool ZipFileInstaller::OnMessageReceived(const IPC::Message& message) {
121 bool handled = true;
122 IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller, message)
123 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Succeeded,
124 OnUnzipSucceeded)
125 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Failed, OnUnzipFailed)
126 IPC_MESSAGE_UNHANDLED(handled = false)
127 IPC_END_MESSAGE_MAP()
128 return handled;
131 } // namespace extensions