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 "content/public/browser/browser_thread.h"
15 #include "content/public/browser/utility_process_host.h"
17 using content::BrowserThread
;
18 using content::UtilityProcessHost
;
22 const char kExtensionHandlerTempDirError
[] =
23 "Could not create temporary directory for zipped extension.";
27 namespace extensions
{
29 ZipFileInstaller::ZipFileInstaller(ExtensionService
* extension_service
)
30 : be_noisy_on_failure_(true),
31 extension_service_weak_(extension_service
->AsWeakPtr()) {
34 void ZipFileInstaller::LoadFromZipFile(const base::FilePath
& path
) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
37 BrowserThread::PostTask(BrowserThread::FILE,
39 base::Bind(&ZipFileInstaller::PrepareTempDir
, this));
42 void ZipFileInstaller::PrepareTempDir() {
43 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
44 base::FilePath temp_dir
;
45 PathService::Get(base::DIR_TEMP
, &temp_dir
);
46 base::FilePath new_temp_dir
;
47 if (!base::CreateTemporaryDirInDir(
49 zip_path_
.RemoveExtension().BaseName().value() +
50 FILE_PATH_LITERAL("_"),
52 OnUnzipFailed(std::string(kExtensionHandlerTempDirError
));
55 BrowserThread::PostTask(
58 base::Bind(&ZipFileInstaller::StartWorkOnIOThread
, this, new_temp_dir
));
61 ZipFileInstaller::~ZipFileInstaller() {
65 scoped_refptr
<ZipFileInstaller
> ZipFileInstaller::Create(
66 ExtensionService
* extension_service
) {
67 DCHECK(extension_service
);
68 return scoped_refptr
<ZipFileInstaller
>(
69 new ZipFileInstaller(extension_service
));
72 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath
& temp_dir
) {
73 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
74 UtilityProcessHost
* host
=
75 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get());
76 host
->SetExposedDir(temp_dir
);
77 host
->Send(new ChromeUtilityMsg_UnzipToDir(zip_path_
, temp_dir
));
80 void ZipFileInstaller::ReportSuccessOnUIThread(
81 const base::FilePath
& unzipped_path
) {
82 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
83 if (extension_service_weak_
.get())
84 UnpackedInstaller::Create(extension_service_weak_
.get())
85 ->Load(unzipped_path
);
88 void ZipFileInstaller::ReportErrorOnUIThread(const std::string
& error
) {
89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
90 if (extension_service_weak_
.get()) {
91 ExtensionErrorReporter::GetInstance()->ReportLoadError(
94 extension_service_weak_
->profile(),
95 be_noisy_on_failure_
);
99 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath
& unzipped_path
) {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
101 BrowserThread::PostTask(
105 &ZipFileInstaller::ReportSuccessOnUIThread
, this, unzipped_path
));
108 void ZipFileInstaller::OnUnzipFailed(const std::string
& error
) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
110 BrowserThread::PostTask(
113 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread
, this, error
));
116 bool ZipFileInstaller::OnMessageReceived(const IPC::Message
& message
) {
118 IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller
, message
)
119 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Succeeded
,
121 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Failed
, OnUnzipFailed
)
122 IPC_MESSAGE_UNHANDLED(handled
= false)
123 IPC_END_MESSAGE_MAP()
127 } // namespace extensions