OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/zipfile_installer.h" | 5 #include "chrome/browser/extensions/zipfile_installer.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "chrome/browser/extensions/extension_error_reporter.h" | 9 #include "chrome/browser/extensions/extension_error_reporter.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/unpacked_installer.h" | 11 #include "chrome/browser/extensions/unpacked_installer.h" |
13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/utility_process_host.h" | 15 #include "extensions/common/extension_unpacker.mojom.h" |
17 #include "extensions/common/extension_utility_messages.h" | |
18 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
19 | 17 |
20 using content::BrowserThread; | |
21 using content::UtilityProcessHost; | |
22 | |
23 namespace { | 18 namespace { |
24 | 19 |
25 const char kExtensionHandlerTempDirError[] = | 20 const char kExtensionHandlerTempDirError[] = |
26 "Could not create temporary directory for zipped extension."; | 21 "Could not create temporary directory for zipped extension."; |
| 22 const char kExtensionHandlerFileUnzipError[] = |
| 23 "Could not unzip extension for install."; |
27 | 24 |
28 } // namespace | 25 } // namespace |
29 | 26 |
30 namespace extensions { | 27 namespace extensions { |
31 | 28 |
32 ZipFileInstaller::ZipFileInstaller(ExtensionService* extension_service) | 29 // static |
33 : be_noisy_on_failure_(true), | 30 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create( |
34 extension_service_weak_(extension_service->AsWeakPtr()) { | 31 ExtensionService* service) { |
| 32 DCHECK(service); |
| 33 return make_scoped_refptr(new ZipFileInstaller(service)); |
35 } | 34 } |
36 | 35 |
37 void ZipFileInstaller::LoadFromZipFile(const base::FilePath& path) { | 36 void ZipFileInstaller::LoadFromZipFile(const base::FilePath& zip_file) { |
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
39 zip_path_ = path; | 38 DCHECK(!zip_file.empty()); |
40 BrowserThread::PostTask(BrowserThread::FILE, | 39 |
41 FROM_HERE, | 40 zip_file_ = zip_file; |
42 base::Bind(&ZipFileInstaller::PrepareTempDir, this)); | 41 |
| 42 content::BrowserThread::PostTask( |
| 43 content::BrowserThread::FILE, FROM_HERE, |
| 44 base::Bind(&ZipFileInstaller::PrepareTempDir, this, zip_file_)); |
43 } | 45 } |
44 | 46 |
45 void ZipFileInstaller::PrepareTempDir() { | 47 ZipFileInstaller::ZipFileInstaller(ExtensionService* service) |
46 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 48 : be_noisy_on_failure_(true), |
47 base::FilePath temp_dir; | 49 extension_service_weak_(service->AsWeakPtr()) {} |
48 PathService::Get(base::DIR_TEMP, &temp_dir); | 50 |
49 base::FilePath new_temp_dir; | 51 ZipFileInstaller::~ZipFileInstaller() = default; |
50 if (!base::CreateTemporaryDirInDir( | 52 |
51 temp_dir, | 53 void ZipFileInstaller::PrepareTempDir(const base::FilePath& zip_file) { |
52 zip_path_.RemoveExtension().BaseName().value() + | 54 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
53 FILE_PATH_LITERAL("_"), | 55 |
54 &new_temp_dir)) { | 56 base::FilePath dir_temp; |
55 OnUnzipFailed(std::string(kExtensionHandlerTempDirError)); | 57 base::PathService::Get(base::DIR_TEMP, &dir_temp); |
| 58 |
| 59 base::FilePath::StringType dir_name = |
| 60 zip_file.RemoveExtension().BaseName().value() + FILE_PATH_LITERAL("_"); |
| 61 |
| 62 base::FilePath unzip_dir; |
| 63 if (!base::CreateTemporaryDirInDir(dir_temp, dir_name, &unzip_dir)) { |
| 64 content::BrowserThread::PostTask( |
| 65 content::BrowserThread::UI, FROM_HERE, |
| 66 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, |
| 67 std::string(kExtensionHandlerTempDirError))); |
56 return; | 68 return; |
57 } | 69 } |
58 BrowserThread::PostTask( | 70 |
59 BrowserThread::IO, | 71 content::BrowserThread::PostTask( |
60 FROM_HERE, | 72 content::BrowserThread::UI, FROM_HERE, |
61 base::Bind(&ZipFileInstaller::StartWorkOnIOThread, this, new_temp_dir)); | 73 base::Bind(&ZipFileInstaller::UnzipOnUIThread, this, unzip_dir)); |
62 } | 74 } |
63 | 75 |
64 ZipFileInstaller::~ZipFileInstaller() { | 76 void ZipFileInstaller::UnzipOnUIThread(const base::FilePath& unzip_dir) { |
| 77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 78 DCHECK(!utility_process_mojo_client_); |
| 79 |
| 80 temp_dir_ = unzip_dir; |
| 81 |
| 82 utility_process_mojo_client_ = base::MakeUnique< |
| 83 content::UtilityProcessMojoClient<mojom::ExtensionUnpacker>>( |
| 84 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_ZIP_FILE_INSTALLER_NAME)); |
| 85 utility_process_mojo_client_->set_error_callback( |
| 86 base::Bind(&ZipFileInstaller::UnzipDone, this, false)); |
| 87 |
| 88 utility_process_mojo_client_->set_exposed_directory(temp_dir_); |
| 89 |
| 90 utility_process_mojo_client_->Start(); |
| 91 |
| 92 utility_process_mojo_client_->service()->Unzip( |
| 93 zip_file_, temp_dir_, base::Bind(&ZipFileInstaller::UnzipDone, this)); |
65 } | 94 } |
66 | 95 |
67 // static | 96 void ZipFileInstaller::UnzipDone(bool success) { |
68 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create( | 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
69 ExtensionService* extension_service) { | |
70 DCHECK(extension_service); | |
71 return scoped_refptr<ZipFileInstaller>( | |
72 new ZipFileInstaller(extension_service)); | |
73 } | |
74 | 98 |
75 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) { | 99 utility_process_mojo_client_.reset(); |
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)); | |
84 } | |
85 | 100 |
86 void ZipFileInstaller::ReportSuccessOnUIThread( | 101 if (!success) { |
87 const base::FilePath& unzipped_path) { | 102 ReportErrorOnUIThread(kExtensionHandlerFileUnzipError); |
88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 return; |
89 if (extension_service_weak_.get()) | 104 } |
90 UnpackedInstaller::Create(extension_service_weak_.get()) | 105 |
91 ->Load(unzipped_path); | 106 if (extension_service_weak_) |
| 107 UnpackedInstaller::Create(extension_service_weak_.get())->Load(temp_dir_); |
92 } | 108 } |
93 | 109 |
94 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) { | 110 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) { |
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
96 if (extension_service_weak_.get()) { | 112 |
| 113 if (extension_service_weak_) { |
97 ExtensionErrorReporter::GetInstance()->ReportLoadError( | 114 ExtensionErrorReporter::GetInstance()->ReportLoadError( |
98 zip_path_, | 115 zip_file_, error, extension_service_weak_->profile(), |
99 error, | |
100 extension_service_weak_->profile(), | |
101 be_noisy_on_failure_); | 116 be_noisy_on_failure_); |
102 } | 117 } |
103 } | 118 } |
104 | 119 |
105 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath& unzipped_path) { | |
106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
107 BrowserThread::PostTask( | |
108 BrowserThread::UI, | |
109 FROM_HERE, | |
110 base::Bind( | |
111 &ZipFileInstaller::ReportSuccessOnUIThread, this, unzipped_path)); | |
112 } | |
113 | |
114 void ZipFileInstaller::OnUnzipFailed(const std::string& error) { | |
115 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
116 BrowserThread::PostTask( | |
117 BrowserThread::UI, | |
118 FROM_HERE, | |
119 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, error)); | |
120 } | |
121 | |
122 bool ZipFileInstaller::OnMessageReceived(const IPC::Message& message) { | |
123 bool handled = true; | |
124 IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller, message) | |
125 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Succeeded, | |
126 OnUnzipSucceeded) | |
127 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Failed, OnUnzipFailed) | |
128 IPC_MESSAGE_UNHANDLED(handled = false) | |
129 IPC_END_MESSAGE_MAP() | |
130 return handled; | |
131 } | |
132 | |
133 } // namespace extensions | 120 } // namespace extensions |
OLD | NEW |