OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/safe_browsing/sandboxed_dmg_analyzer_mac.h" | 5 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/task_scheduler/post_task.h" | 10 #include "base/task_scheduler/post_task.h" |
11 #include "chrome/common/chrome_utility_messages.h" | |
12 #include "chrome/common/safe_browsing/zip_analyzer_results.h" | 11 #include "chrome/common/safe_browsing/zip_analyzer_results.h" |
13 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
14 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/child_process_data.h" | |
16 #include "content/public/browser/render_process_host.h" | |
17 #include "ipc/ipc_message_macros.h" | |
18 #include "ipc/ipc_platform_file.h" | |
19 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
20 | 15 |
21 using content::BrowserThread; | |
22 | |
23 namespace safe_browsing { | 16 namespace safe_browsing { |
24 | 17 |
25 SandboxedDMGAnalyzer::SandboxedDMGAnalyzer(const base::FilePath& dmg_file, | 18 SandboxedDMGAnalyzer::SandboxedDMGAnalyzer(const base::FilePath& dmg_file, |
26 const ResultsCallback& callback) | 19 const ResultCallback& callback) |
27 : file_path_(dmg_file), callback_(callback), callback_called_(false) { | 20 : file_path_(dmg_file), callback_(callback) { |
| 21 DCHECK(callback); |
28 } | 22 } |
29 | 23 |
30 SandboxedDMGAnalyzer::~SandboxedDMGAnalyzer() {} | 24 void SandboxedDMGAnalyzer::Start() { |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
31 | 26 |
32 void SandboxedDMGAnalyzer::Start() { | |
33 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
34 base::PostTaskWithTraits( | 27 base::PostTaskWithTraits( |
35 FROM_HERE, base::TaskTraits() | 28 FROM_HERE, |
36 .MayBlock() | 29 base::TaskTraits() |
37 .WithPriority(base::TaskPriority::BACKGROUND) | 30 .MayBlock() |
38 .WithShutdownBehavior( | 31 .WithPriority(base::TaskPriority::BACKGROUND) |
39 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | 32 .WithShutdownBehavior( |
40 base::Bind(&SandboxedDMGAnalyzer::OpenDMGFile, this)); | 33 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 34 base::Bind(&SandboxedDMGAnalyzer::PrepareFileToAnalyze, this)); |
41 } | 35 } |
42 | 36 |
43 void SandboxedDMGAnalyzer::OpenDMGFile() { | 37 SandboxedDMGAnalyzer::~SandboxedDMGAnalyzer() = default; |
44 file_.Initialize(file_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); | 38 |
45 if (!file_.IsValid()) { | 39 void SandboxedDMGAnalyzer::PrepareFileToAnalyze() { |
46 DLOG(ERROR) << "Could not open DMG file at path " << file_path_.value(); | 40 base::File file(file_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); |
47 BrowserThread::PostTask( | 41 |
48 BrowserThread::IO, FROM_HERE, | 42 if (!file.IsValid()) { |
49 base::Bind(&SandboxedDMGAnalyzer::OnAnalysisFinished, this, | 43 DLOG(ERROR) << "Could not open file: " << file_path_.value(); |
50 zip_analyzer::Results())); | 44 ReportFileFailure(); |
51 return; | 45 return; |
52 } | 46 } |
53 | 47 |
54 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 48 content::BrowserThread::PostTask( |
55 base::Bind(&SandboxedDMGAnalyzer::StartAnalysis, | 49 content::BrowserThread::UI, FROM_HERE, |
56 this)); | 50 base::Bind(&SandboxedDMGAnalyzer::AnalyzeFile, this, |
| 51 base::Passed(&file))); |
57 } | 52 } |
58 | 53 |
59 void SandboxedDMGAnalyzer::StartAnalysis() { | 54 void SandboxedDMGAnalyzer::ReportFileFailure() { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 55 DCHECK(!utility_process_mojo_client_); |
61 | 56 |
62 content::UtilityProcessHost* utility_process_host = | 57 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
63 content::UtilityProcessHost::Create( | 58 base::Bind(callback_, Results())); |
64 this, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); | |
65 | |
66 utility_process_host->SetName(l10n_util::GetStringUTF16( | |
67 IDS_UTILITY_PROCESS_SAFE_BROWSING_ZIP_FILE_ANALYZER_NAME)); | |
68 utility_process_host->Send( | |
69 new ChromeUtilityMsg_AnalyzeDmgFileForDownloadProtection( | |
70 IPC::TakePlatformFileForTransit(std::move(file_)))); | |
71 } | 59 } |
72 | 60 |
73 void SandboxedDMGAnalyzer::OnProcessCrashed(int exit_code) { | 61 void SandboxedDMGAnalyzer::AnalyzeFile(base::File file) { |
74 OnAnalysisFinished(zip_analyzer::Results()); | 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 63 DCHECK(!utility_process_mojo_client_); |
| 64 |
| 65 utility_process_mojo_client_ = base::MakeUnique< |
| 66 content::UtilityProcessMojoClient<chrome::mojom::SafeArchiveAnalyzer>>( |
| 67 l10n_util::GetStringUTF16( |
| 68 IDS_UTILITY_PROCESS_SAFE_BROWSING_ZIP_FILE_ANALYZER_NAME)); |
| 69 utility_process_mojo_client_->set_error_callback( |
| 70 base::Bind(&SandboxedDMGAnalyzer::AnalyzeFileDone, this, Results())); |
| 71 |
| 72 utility_process_mojo_client_->Start(); |
| 73 |
| 74 utility_process_mojo_client_->service()->AnalyzeDmgFile( |
| 75 std::move(file), |
| 76 base::Bind(&SandboxedDMGAnalyzer::AnalyzeFileDone, this)); |
75 } | 77 } |
76 | 78 |
77 void SandboxedDMGAnalyzer::OnProcessLaunchFailed(int error_code) { | 79 void SandboxedDMGAnalyzer::AnalyzeFileDone(const Results& results) { |
78 OnAnalysisFinished(zip_analyzer::Results()); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
79 } | |
80 | 81 |
81 bool SandboxedDMGAnalyzer::OnMessageReceived(const IPC::Message& message) { | 82 utility_process_mojo_client_.reset(); |
82 bool handled = true; | 83 callback_.Run(results); |
83 IPC_BEGIN_MESSAGE_MAP(SandboxedDMGAnalyzer, message) | |
84 IPC_MESSAGE_HANDLER( | |
85 ChromeUtilityHostMsg_AnalyzeDmgFileForDownloadProtection_Finished, | |
86 OnAnalysisFinished) | |
87 IPC_MESSAGE_UNHANDLED(handled = false) | |
88 IPC_END_MESSAGE_MAP() | |
89 return handled; | |
90 } | |
91 | |
92 void SandboxedDMGAnalyzer::OnAnalysisFinished( | |
93 const zip_analyzer::Results& results) { | |
94 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
95 if (callback_called_) | |
96 return; | |
97 | |
98 callback_called_ = true; | |
99 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
100 base::Bind(callback_, results)); | |
101 } | 84 } |
102 | 85 |
103 } // namespace safe_browsing | 86 } // namespace safe_browsing |
OLD | NEW |