| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // | |
| 5 // Browser-side interface to analyze zip files for SafeBrowsing download | |
| 6 // protection. The actual zip decoding is performed in a sandboxed utility | |
| 7 // process. | |
| 8 // | |
| 9 // This class lives on the UI thread. | |
| 10 | 4 |
| 11 #ifndef CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ |
| 12 #define CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ |
| 13 | 7 |
| 14 #include "base/callback.h" | 8 #include "base/callback.h" |
| 15 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 16 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 17 #include "base/macros.h" | 11 #include "base/macros.h" |
| 18 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/ref_counted.h" |
| 19 #include "content/public/browser/utility_process_host.h" | 13 #include "chrome/common/safe_archive_analyzer.mojom.h" |
| 20 #include "content/public/browser/utility_process_host_client.h" | 14 #include "content/public/browser/utility_process_mojo_client.h" |
| 21 | |
| 22 namespace IPC { | |
| 23 class Message; | |
| 24 } | |
| 25 | 15 |
| 26 namespace safe_browsing { | 16 namespace safe_browsing { |
| 27 namespace zip_analyzer { | |
| 28 struct Results; | |
| 29 } | |
| 30 | 17 |
| 31 class SandboxedZipAnalyzer : public content::UtilityProcessHostClient { | 18 // This class is used to analyze zip files in a sandboxed utility process |
| 19 // for file download protection. This class lives on the UI thread, which |
| 20 // is where the result callback will be invoked. |
| 21 class SandboxedZipAnalyzer |
| 22 : public base::RefCountedThreadSafe<SandboxedZipAnalyzer> { |
| 32 public: | 23 public: |
| 33 // Callback that is invoked when the analysis results are ready. | 24 using Results = zip_analyzer::Results; |
| 34 typedef base::Callback<void(const zip_analyzer::Results&)> ResultCallback; | 25 |
| 26 using ResultCallback = base::Callback<void(const Results&)>; |
| 35 | 27 |
| 36 SandboxedZipAnalyzer(const base::FilePath& zip_file, | 28 SandboxedZipAnalyzer(const base::FilePath& zip_file, |
| 37 const ResultCallback& result_callback); | 29 const ResultCallback& callback); |
| 38 | 30 |
| 39 // Posts a task to start the zip analysis in the utility process. | 31 // Starts the analysis. Must be called on the UI thread. |
| 40 void Start(); | 32 void Start(); |
| 41 | 33 |
| 42 private: | 34 private: |
| 43 ~SandboxedZipAnalyzer() override; | 35 friend class base::RefCountedThreadSafe<SandboxedZipAnalyzer>; |
| 44 | 36 |
| 45 // Posts a fire-and-forget task to close the temporary file in the blocking | 37 ~SandboxedZipAnalyzer(); |
| 46 // pool. | |
| 47 void CloseTemporaryFile(); | |
| 48 | 38 |
| 49 // Creates the sandboxed utility process and tells it to start analysis. | 39 // Prepare the file for analysis. |
| 50 // Runs on a worker thread. | 40 void PrepareFileToAnalyze(); |
| 51 void AnalyzeInSandbox(); | |
| 52 | 41 |
| 53 // content::UtilityProcessHostClient implementation. | 42 // If file preparation failed, analysis has failed: report failure. |
| 54 // These notifications run on the IO thread. | 43 void ReportFileFailure(); |
| 55 void OnProcessCrashed(int exit_code) override; | |
| 56 void OnProcessLaunchFailed(int error_code) override; | |
| 57 bool OnMessageReceived(const IPC::Message& message) override; | |
| 58 | 44 |
| 59 // Launches the utility process. Must run on the IO thread. | 45 // Starts the utility process and sends it a file analyze request. |
| 60 void StartProcessOnIOThread(); | 46 void AnalyzeFile(base::File file, base::File temp); |
| 61 | 47 |
| 62 // Notification from the utility process that the zip file has been analyzed, | 48 // The response containing the file analyze results. |
| 63 // with the given results. Runs on the IO thread. | 49 void AnalyzeFileDone(const Results& results); |
| 64 void OnAnalyzeZipFileFinished(const zip_analyzer::Results& results); | |
| 65 | 50 |
| 66 const base::FilePath zip_file_name_; | 51 // The file path of the file to analyze. |
| 67 // Once we have opened the file, we store the handle so that we can use it | 52 const base::FilePath file_path_; |
| 68 // once the utility process has launched. | |
| 69 base::File zip_file_; | |
| 70 | 53 |
| 71 // A temporary file to be used by the utility process for extracting files | 54 // Utility client used to send analyze tasks to the utility process. |
| 72 // from the archive. | 55 std::unique_ptr< |
| 73 base::File temp_file_; | 56 content::UtilityProcessMojoClient<chrome::mojom::SafeArchiveAnalyzer>> |
| 74 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 57 utility_process_mojo_client_; |
| 58 |
| 59 // Callback invoked on the UI thread with the file analyze results. |
| 75 const ResultCallback callback_; | 60 const ResultCallback callback_; |
| 76 // Initialized on the UI thread, but only accessed on the IO thread. | |
| 77 bool callback_called_; | |
| 78 | 61 |
| 79 DISALLOW_COPY_AND_ASSIGN(SandboxedZipAnalyzer); | 62 DISALLOW_COPY_AND_ASSIGN(SandboxedZipAnalyzer); |
| 80 }; | 63 }; |
| 81 | 64 |
| 82 } // namespace safe_browsing | 65 } // namespace safe_browsing |
| 83 | 66 |
| 84 #endif // CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ | 67 #endif // CHROME_BROWSER_SAFE_BROWSING_SANDBOXED_ZIP_ANALYZER_H_ |
| OLD | NEW |