1 // Copyright 2015 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_dmg_analyzer_mac.h"
8 #include "chrome/common/chrome_utility_messages.h"
9 #include "chrome/common/safe_browsing/zip_analyzer_results.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/child_process_data.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "ipc/ipc_message_macros.h"
15 #include "ipc/ipc_platform_file.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using content::BrowserThread
;
20 namespace safe_browsing
{
22 SandboxedDMGAnalyzer::SandboxedDMGAnalyzer(const base::FilePath
& dmg_file
,
23 const ResultsCallback
& callback
)
24 : file_path_(dmg_file
), callback_(callback
), callback_called_(false) {
27 SandboxedDMGAnalyzer::~SandboxedDMGAnalyzer() {}
29 void SandboxedDMGAnalyzer::Start() {
30 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
31 if (!BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior(
32 FROM_HERE
, base::Bind(&SandboxedDMGAnalyzer::OpenDMGFile
, this),
33 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN
)) {
38 void SandboxedDMGAnalyzer::OpenDMGFile() {
39 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
41 file_
.Initialize(file_path_
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
42 if (!file_
.IsValid()) {
43 DLOG(ERROR
) << "Could not open DMG file at path " << file_path_
.value();
44 BrowserThread::PostTask(
45 BrowserThread::IO
, FROM_HERE
,
46 base::Bind(&SandboxedDMGAnalyzer::OnAnalysisFinished
, this,
47 zip_analyzer::Results()));
51 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
52 base::Bind(&SandboxedDMGAnalyzer::StartAnalysis
,
56 void SandboxedDMGAnalyzer::StartAnalysis() {
57 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
59 utility_process_host_
=
60 content::UtilityProcessHost::Create(this,
61 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))
64 utility_process_host_
->SetName(l10n_util::GetStringUTF16(
65 IDS_UTILITY_PROCESS_SAFE_BROWSING_ZIP_FILE_ANALYZER_NAME
));
66 utility_process_host_
->Send(new ChromeUtilityMsg_StartupPing
);
69 bool SandboxedDMGAnalyzer::OnMessageReceived(const IPC::Message
& message
) {
71 IPC_BEGIN_MESSAGE_MAP(SandboxedDMGAnalyzer
, message
)
72 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted
,
73 OnUtilityProcessStarted
)
75 ChromeUtilityHostMsg_AnalyzeDmgFileForDownloadProtection_Finished
,
77 IPC_MESSAGE_UNHANDLED(handled
= false)
82 void SandboxedDMGAnalyzer::OnUtilityProcessStarted() {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
85 base::ProcessHandle utility_process
=
86 content::RenderProcessHost::run_renderer_in_process() ?
87 base::GetCurrentProcessHandle() :
88 utility_process_host_
->GetData().handle
;
89 if (utility_process
== base::kNullProcessHandle
) {
90 DLOG(ERROR
) << "Child process handle is null";
93 utility_process_host_
->Send(
94 new ChromeUtilityMsg_AnalyzeDmgFileForDownloadProtection(
95 IPC::TakeFileHandleForProcess(file_
.Pass(), utility_process
)));
98 void SandboxedDMGAnalyzer::OnAnalysisFinished(
99 const zip_analyzer::Results
& results
) {
100 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
101 if (callback_called_
)
104 callback_called_
= true;
105 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
106 base::Bind(callback_
, results
));
109 } // namespace safe_browsing