1 // Copyright (c) 2012 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/safe_browsing/sandboxed_zip_analyzer.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/common/chrome_utility_messages.h"
14 #include "chrome/common/safe_browsing/zip_analyzer_results.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/child_process_data.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/common/content_switches.h"
19 #include "ipc/ipc_message_macros.h"
20 #include "ipc/ipc_platform_file.h"
22 using content::BrowserThread
;
24 namespace safe_browsing
{
26 SandboxedZipAnalyzer::SandboxedZipAnalyzer(
27 const base::FilePath
& zip_file
,
28 const ResultCallback
& result_callback
)
29 : zip_file_name_(zip_file
),
30 callback_(result_callback
),
31 callback_called_(false) {
34 void SandboxedZipAnalyzer::Start() {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
36 // Starting the analyzer will block on opening the zip file, so run this
37 // on a worker thread. The task does not need to block shutdown.
38 if (!BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
40 base::Bind(&SandboxedZipAnalyzer::AnalyzeInSandbox
, this),
41 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN
)) {
46 SandboxedZipAnalyzer::~SandboxedZipAnalyzer() {
47 // If we're using UtilityProcessHost, we may not be destroyed on
48 // the UI or IO thread.
52 void SandboxedZipAnalyzer::CloseTemporaryFile() {
53 if (!temp_file_
.IsValid())
55 // Close the temporary file in the blocking pool since doing so will delete
57 if (!BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
58 FROM_HERE
, base::Bind(&base::File::Close
,
59 base::Owned(new base::File(temp_file_
.Pass()))),
60 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN
)) {
65 void SandboxedZipAnalyzer::AnalyzeInSandbox() {
66 // This zip file will be closed on the IO thread once it has been handed
67 // off to the child process.
68 zip_file_
.Initialize(zip_file_name_
,
69 base::File::FLAG_OPEN
| base::File::FLAG_READ
);
70 if (!zip_file_
.IsValid()) {
71 DVLOG(1) << "Could not open zip file: " << zip_file_name_
.value();
72 if (!BrowserThread::PostTask(
73 BrowserThread::IO
, FROM_HERE
,
74 base::Bind(&SandboxedZipAnalyzer::OnAnalyzeZipFileFinished
, this,
75 zip_analyzer::Results()))) {
81 // This temp file will be closed in the blocking pool when results from the
83 base::FilePath temp_path
;
84 if (base::CreateTemporaryFile(&temp_path
)) {
85 temp_file_
.Initialize(temp_path
, (base::File::FLAG_CREATE_ALWAYS
|
86 base::File::FLAG_READ
|
87 base::File::FLAG_WRITE
|
88 base::File::FLAG_TEMPORARY
|
89 base::File::FLAG_DELETE_ON_CLOSE
));
91 DVLOG_IF(1, !temp_file_
.IsValid())
92 << "Could not open temporary output file: " << temp_path
.value();
94 BrowserThread::PostTask(
95 BrowserThread::IO
, FROM_HERE
,
96 base::Bind(&SandboxedZipAnalyzer::StartProcessOnIOThread
, this));
99 bool SandboxedZipAnalyzer::OnMessageReceived(const IPC::Message
& message
) {
101 IPC_BEGIN_MESSAGE_MAP(SandboxedZipAnalyzer
, message
)
102 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted
,
103 OnUtilityProcessStarted
)
105 ChromeUtilityHostMsg_AnalyzeZipFileForDownloadProtection_Finished
,
106 OnAnalyzeZipFileFinished
)
107 IPC_MESSAGE_UNHANDLED(handled
= false)
108 IPC_END_MESSAGE_MAP()
112 void SandboxedZipAnalyzer::StartProcessOnIOThread() {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
114 utility_process_host_
= content::UtilityProcessHost::Create(
116 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get())
118 utility_process_host_
->Send(new ChromeUtilityMsg_StartupPing
);
119 // Wait for the startup notification before sending the main IPC to the
120 // utility process, so that we can dup the file handle.
123 void SandboxedZipAnalyzer::OnUtilityProcessStarted() {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
125 base::ProcessHandle utility_process
=
126 content::RenderProcessHost::run_renderer_in_process() ?
127 base::GetCurrentProcessHandle() :
128 utility_process_host_
->GetData().handle
;
130 if (utility_process
== base::kNullProcessHandle
) {
131 DLOG(ERROR
) << "Child process handle is null";
133 utility_process_host_
->Send(
134 new ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection(
135 IPC::TakeFileHandleForProcess(zip_file_
.Pass(), utility_process
),
136 IPC::GetFileHandleForProcess(temp_file_
.GetPlatformFile(),
138 false /* !close_source_handle */)));
141 void SandboxedZipAnalyzer::OnAnalyzeZipFileFinished(
142 const zip_analyzer::Results
& results
) {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
144 if (callback_called_
)
146 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
147 base::Bind(callback_
, results
));
148 callback_called_
= true;
149 CloseTemporaryFile();
152 } // namespace safe_browsing