Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(461)

Side by Side Diff: chrome/browser/extensions/zipfile_installer.cc

Issue 2697463002: Convert utility process extension Unpacker IPC to mojo (Closed)
Patch Set: Set the IPC enum traits limit to extensions::Manifest::NUM_LOCATIONS - 1. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/zipfile_installer.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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::PrepareUnzipDir, 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::PrepareUnzipDir(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::ReportFailure, 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::Unzip, this, unzip_dir));
62 } 74 }
63 75
64 ZipFileInstaller::~ZipFileInstaller() { 76 void ZipFileInstaller::Unzip(const base::FilePath& unzip_dir) {
77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
78 DCHECK(!utility_process_mojo_client_);
79
80 utility_process_mojo_client_ = base::MakeUnique<
81 content::UtilityProcessMojoClient<mojom::ExtensionUnpacker>>(
82 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_ZIP_FILE_INSTALLER_NAME));
83 utility_process_mojo_client_->set_error_callback(
84 base::Bind(&ZipFileInstaller::UnzipDone, this, unzip_dir, false));
85
86 utility_process_mojo_client_->set_exposed_directory(unzip_dir);
87
88 utility_process_mojo_client_->Start();
89
90 utility_process_mojo_client_->service()->Unzip(
91 zip_file_, unzip_dir,
92 base::Bind(&ZipFileInstaller::UnzipDone, this, unzip_dir));
65 } 93 }
66 94
67 // static 95 void ZipFileInstaller::UnzipDone(const base::FilePath& unzip_dir,
68 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create( 96 bool success) {
69 ExtensionService* extension_service) { 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
70 DCHECK(extension_service); 98
71 return scoped_refptr<ZipFileInstaller>( 99 utility_process_mojo_client_.reset();
72 new ZipFileInstaller(extension_service)); 100
101 if (!success) {
102 ReportFailure(kExtensionHandlerFileUnzipError);
103 return;
104 }
105
106 if (extension_service_weak_)
107 UnpackedInstaller::Create(extension_service_weak_.get())->Load(unzip_dir);
73 } 108 }
74 109
75 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) { 110 void ZipFileInstaller::ReportFailure(const std::string& error) {
76 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
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 112
86 void ZipFileInstaller::ReportSuccessOnUIThread( 113 if (extension_service_weak_) {
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);
92 }
93
94 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) {
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 if (extension_service_weak_.get()) {
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
OLDNEW
« no previous file with comments | « chrome/browser/extensions/zipfile_installer.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698