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 "base/thread_task_runner_handle.h"
10 #include "chrome/browser/extensions/extension_error_reporter.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/profiles/profile.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 "extensions/common/extension_utility_messages.h"
18 #include "ui/base/l10n/l10n_util.h"
20 using content::BrowserThread
;
21 using content::UtilityProcessHost
;
25 const char kExtensionHandlerTempDirError
[] =
26 "Could not create temporary directory for zipped extension.";
30 namespace extensions
{
32 ZipFileInstaller::ZipFileInstaller(ExtensionService
* extension_service
)
33 : be_noisy_on_failure_(true),
34 extension_service_weak_(extension_service
->AsWeakPtr()) {
37 void ZipFileInstaller::LoadFromZipFile(const base::FilePath
& path
) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
40 BrowserThread::PostTask(BrowserThread::FILE,
42 base::Bind(&ZipFileInstaller::PrepareTempDir
, this));
45 void ZipFileInstaller::PrepareTempDir() {
46 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
47 base::FilePath temp_dir
;
48 PathService::Get(base::DIR_TEMP
, &temp_dir
);
49 base::FilePath new_temp_dir
;
50 if (!base::CreateTemporaryDirInDir(
52 zip_path_
.RemoveExtension().BaseName().value() +
53 FILE_PATH_LITERAL("_"),
55 OnUnzipFailed(std::string(kExtensionHandlerTempDirError
));
58 BrowserThread::PostTask(
61 base::Bind(&ZipFileInstaller::StartWorkOnIOThread
, this, new_temp_dir
));
64 ZipFileInstaller::~ZipFileInstaller() {
68 scoped_refptr
<ZipFileInstaller
> ZipFileInstaller::Create(
69 ExtensionService
* extension_service
) {
70 DCHECK(extension_service
);
71 return scoped_refptr
<ZipFileInstaller
>(
72 new ZipFileInstaller(extension_service
));
75 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath
& temp_dir
) {
76 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
77 UtilityProcessHost
* host
=
78 UtilityProcessHost::Create(this,
79 base::ThreadTaskRunnerHandle::Get().get());
80 host
->SetName(l10n_util::GetStringUTF16(
81 IDS_UTILITY_PROCESS_ZIP_FILE_INSTALLER_NAME
));
82 host
->SetExposedDir(temp_dir
);
83 host
->Send(new ExtensionUtilityMsg_UnzipToDir(zip_path_
, temp_dir
));
86 void ZipFileInstaller::ReportSuccessOnUIThread(
87 const base::FilePath
& unzipped_path
) {
88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
89 if (extension_service_weak_
.get())
90 UnpackedInstaller::Create(extension_service_weak_
.get())
91 ->Load(unzipped_path
);
94 void ZipFileInstaller::ReportErrorOnUIThread(const std::string
& error
) {
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
96 if (extension_service_weak_
.get()) {
97 ExtensionErrorReporter::GetInstance()->ReportLoadError(
100 extension_service_weak_
->profile(),
101 be_noisy_on_failure_
);
105 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath
& unzipped_path
) {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
107 BrowserThread::PostTask(
111 &ZipFileInstaller::ReportSuccessOnUIThread
, this, unzipped_path
));
114 void ZipFileInstaller::OnUnzipFailed(const std::string
& error
) {
115 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
116 BrowserThread::PostTask(
119 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread
, this, error
));
122 bool ZipFileInstaller::OnMessageReceived(const IPC::Message
& message
) {
124 IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller
, message
)
125 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Succeeded
,
127 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Failed
, OnUnzipFailed
)
128 IPC_MESSAGE_UNHANDLED(handled
= false)
129 IPC_END_MESSAGE_MAP()
133 } // namespace extensions