| 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "content/public/browser/utility_process_host.h" | 13 #include "chrome/common/safe_archive_analyzer.mojom.h" |
| 14 #include "content/public/browser/utility_process_host_client.h" | 14 #include "content/public/browser/utility_process_mojo_client.h" |
| 15 | 15 |
| 16 namespace safe_browsing { | 16 namespace safe_browsing { |
| 17 | 17 |
| 18 namespace zip_analyzer { | 18 // This class is used to analyze DMG files in a sandboxed utility process |
| 19 struct Results; | 19 // for file download protection. This class lives on the UI thread, which |
| 20 } | 20 // is where the result callback will be invoked. |
| 21 class SandboxedDMGAnalyzer |
| 22 : public base::RefCountedThreadSafe<SandboxedDMGAnalyzer> { |
| 23 public: |
| 24 using Results = zip_analyzer::Results; |
| 21 | 25 |
| 22 // This class is used to analyze DMG files in a sandboxed utility process for | 26 using ResultCallback = base::Callback<void(const Results&)>; |
| 23 // file download protection. This class must be created on the UI thread, and | |
| 24 // which is where the result callback will be invoked. | |
| 25 class SandboxedDMGAnalyzer : public content::UtilityProcessHostClient { | |
| 26 public: | |
| 27 // Callback that is invoked when the analysis results are ready. | |
| 28 using ResultsCallback = base::Callback<void(const zip_analyzer::Results&)>; | |
| 29 | 27 |
| 30 SandboxedDMGAnalyzer(const base::FilePath& dmg_file, | 28 SandboxedDMGAnalyzer(const base::FilePath& dmg_file, |
| 31 const ResultsCallback& callback); | 29 const ResultCallback& callback); |
| 32 | 30 |
| 33 // Begins the analysis. This must be called on the UI thread. | 31 // Starts the analysis. Must be called on the UI thread. |
| 34 void Start(); | 32 void Start(); |
| 35 | 33 |
| 36 private: | 34 private: |
| 37 ~SandboxedDMGAnalyzer() override; | 35 friend class base::RefCountedThreadSafe<SandboxedDMGAnalyzer>; |
| 38 | 36 |
| 39 // Called on the blocking pool, this opens the DMG file, in preparation for | 37 ~SandboxedDMGAnalyzer(); |
| 40 // sending the FD to the utility process. | |
| 41 void OpenDMGFile(); | |
| 42 | 38 |
| 43 // Called on the IO thread, this starts the utility process. | 39 // Prepare the file for analysis. |
| 44 void StartAnalysis(); | 40 void PrepareFileToAnalyze(); |
| 45 | 41 |
| 46 // content::UtilityProcessHostClient: | 42 // If file preparation failed, analysis has failed: report failure. |
| 47 void OnProcessCrashed(int exit_code) override; | 43 void ReportFileFailure(); |
| 48 void OnProcessLaunchFailed(int error_code) override; | |
| 49 bool OnMessageReceived(const IPC::Message& message) override; | |
| 50 | 44 |
| 51 // Message handler to receive the results of the analysis. Invokes the | 45 // Starts the utility process and sends it a file analyze request. |
| 52 // |callback_|. | 46 void AnalyzeFile(base::File file); |
| 53 void OnAnalysisFinished(const zip_analyzer::Results& results); | |
| 54 | 47 |
| 55 const base::FilePath file_path_; // The path of the DMG file. | 48 // The response containing the file analyze results. |
| 56 base::File file_; // The opened file handle for |file_path_|. | 49 void AnalyzeFileDone(const Results& results); |
| 57 | 50 |
| 58 const ResultsCallback callback_; // Result callback. | 51 // The file path of the file to analyze. |
| 59 bool callback_called_; // Whether |callback_| has already been invoked. | 52 const base::FilePath file_path_; |
| 53 |
| 54 // Utility client used to send analyze tasks to the utility process. |
| 55 std::unique_ptr< |
| 56 content::UtilityProcessMojoClient<chrome::mojom::SafeArchiveAnalyzer>> |
| 57 utility_process_mojo_client_; |
| 58 |
| 59 // Callback invoked on the UI thread with the file analyze results. |
| 60 const ResultCallback callback_; |
| 60 | 61 |
| 61 DISALLOW_COPY_AND_ASSIGN(SandboxedDMGAnalyzer); | 62 DISALLOW_COPY_AND_ASSIGN(SandboxedDMGAnalyzer); |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 } // namespace safe_browsing | 65 } // namespace safe_browsing |
| 65 | 66 |
| 66 #endif // CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ | 67 #endif // CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_DMG_ANALYZER_MAC_H_ |
| OLD | NEW |