OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_CHROME_CLEANER_RUNNER_WIN_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_CHROME_CLEANER_RUNNER_WIN_H_ |
| 7 |
| 8 #include <limits> |
| 9 #include <memory> |
| 10 #include <set> |
| 11 #include <string> |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/process/launch.h" |
| 18 #include "base/process/process.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 20 #include "chrome/browser/safe_browsing/chrome_cleaner/reporter_runner_win.h" |
| 21 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_chrome_prompt_impl.h" |
| 22 #include "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h" |
| 23 |
| 24 namespace safe_browsing { |
| 25 |
| 26 // Class responsible for launching the cleaner process and waiting for its |
| 27 // completion when the InBrowserCleanerUI feature is enabled. This object is |
| 28 // also responsible for starting the ChromePromptImpl object on the IO thread |
| 29 // and controlling its lifetime. |
| 30 // |
| 31 // Expected lifecycle of a ChromeCleanerRunner: |
| 32 // |
| 33 // - Instances are created via the static |
| 34 // RunChromeCleanerAndReplyWithExitCode() function. Instances will not be |
| 35 // destroyed before the Chrome Cleaner process has terminated _and_ a |
| 36 // Mojo connection-closed callback has been received. Destruction can happen |
| 37 // on any thread. |
| 38 // - The private LaunchAndWaitForExitOnBackgroundThread() function launches the |
| 39 // Chrome Cleaner process, creates a ChromePromptImpl object on the IO thread |
| 40 // and waits for the Cleaner process to terminate. The static |
| 41 // RunChromeCleanerAndReplyWithExitCode() function takes care of calling that |
| 42 // function correctly on a sequence with the correct traits. |
| 43 // - All callbacks registered with an instance are posted to run on the same |
| 44 // thread on which the instance was originally created. |
| 45 // - The ChromePromptImpl object is destroyed on the IO thread after a |
| 46 // connection-closed has been received from Mojo. The runner instance will |
| 47 // not be destroyed before the ChromePromptImpl object has been released. |
| 48 class ChromeCleanerRunner |
| 49 : public base::RefCountedThreadSafe<ChromeCleanerRunner> { |
| 50 public: |
| 51 enum class ChromeMetricsStatus { |
| 52 kEnabled, |
| 53 kDisabled, |
| 54 }; |
| 55 |
| 56 enum class CleanerLogsStatus { |
| 57 kUploadEnabled, |
| 58 kUploadDisabled, |
| 59 }; |
| 60 |
| 61 struct LaunchStatus { |
| 62 // If false, indicates that either the Chrome Cleaner process handle |
| 63 // returned by base::LaunchProcess() was invalid or that something went |
| 64 // wrong while waiting for the process to exit. |
| 65 bool process_ok; |
| 66 // The exit code from the Chrome Cleaner process. Should not be used if |
| 67 // |process_ok| is false. |
| 68 int exit_code; |
| 69 }; |
| 70 |
| 71 using ProcessDoneCallback = base::OnceCallback<void(LaunchStatus)>; |
| 72 |
| 73 // Executes the Chrome Cleaner in the background, initializes the Mojo IPC |
| 74 // between Chrome and the Chrome Cleaner process, and forwards Mojo callbacks |
| 75 // via the callbacks that are passed to it. |
| 76 // |
| 77 // All callbacks are posted to run on the same thread that this function is |
| 78 // called from. |
| 79 // |
| 80 // More details: |
| 81 // |
| 82 // This function will pass command line flags to the Chrome Cleaner executable |
| 83 // as appropriate based on the flags in |reporter_invocation| and the |
| 84 // |metrics_status| and |cleaner_logs_status| parameters. The Cleaner process |
| 85 // will communicate with Chrome via a Mojo IPC interface and any IPC requests |
| 86 // or notifications are passed to the caller via the |on_prompt_user| and |
| 87 // |on_connection_closed| callbacks. Finally, when the Chrome Cleaner process |
| 88 // terminates, a LaunchStatus is passed along to |on_process_done|. |
| 89 // |
| 90 // The details of the mojo interface are documented in |
| 91 // "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h". |
| 92 static void RunChromeCleanerAndReplyWithExitCode( |
| 93 const base::FilePath& executable_path, |
| 94 const SwReporterInvocation& reporter_invocation, |
| 95 ChromeMetricsStatus metrics_status, |
| 96 CleanerLogsStatus cleaner_logs_status, |
| 97 ChromePromptImpl::OnPromptUser on_prompt_user, |
| 98 base::OnceClosure on_connection_closed, |
| 99 ChromeCleanerRunner::ProcessDoneCallback on_process_done); |
| 100 |
| 101 private: |
| 102 friend class base::RefCountedThreadSafe<ChromeCleanerRunner>; |
| 103 |
| 104 ~ChromeCleanerRunner(); |
| 105 |
| 106 ChromeCleanerRunner(const base::FilePath& executable_path, |
| 107 const SwReporterInvocation& reporter_invocation, |
| 108 ChromeMetricsStatus metrics_status, |
| 109 CleanerLogsStatus cleaner_logs_status, |
| 110 ChromePromptImpl::OnPromptUser on_prompt_user, |
| 111 base::OnceClosure on_connection_closed, |
| 112 ChromeCleanerRunner::ProcessDoneCallback on_process_done, |
| 113 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 114 |
| 115 LaunchStatus LaunchAndWaitForExitOnBackgroundThread(); |
| 116 |
| 117 void CreateChromePromptImpl( |
| 118 chrome_cleaner::mojom::ChromePromptRequest chrome_prompt_request); |
| 119 void ReleaseChromePromptImpl(); |
| 120 |
| 121 // Callbacks received from the Mojo interface. |
| 122 void OnPromptUser(std::unique_ptr<std::set<base::FilePath>> files_to_delete, |
| 123 chrome_cleaner::mojom::ChromePrompt::PromptUserCallback |
| 124 prompt_user_callback); |
| 125 void OnConnectionClosed(); |
| 126 void OnProcessDone(LaunchStatus launch_status); |
| 127 |
| 128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 129 |
| 130 base::CommandLine command_line_; |
| 131 ChromePromptImpl::OnPromptUser on_prompt_user_; |
| 132 base::OnceClosure on_connection_closed_; |
| 133 ProcessDoneCallback on_process_done_; |
| 134 |
| 135 std::unique_ptr<ChromePromptImpl> chrome_prompt_impl_; |
| 136 }; |
| 137 |
| 138 // A delegate class used to override launching of the Cleaner proccess for |
| 139 // tests. |
| 140 class ChromeCleanerRunnerTestDelegate { |
| 141 public: |
| 142 virtual ~ChromeCleanerRunnerTestDelegate() = default; |
| 143 |
| 144 // Called instead of base::LaunchProcess() during testing. |
| 145 virtual base::Process LaunchTestProcess( |
| 146 const base::CommandLine& command_line, |
| 147 const base::LaunchOptions& launch_options) = 0; |
| 148 |
| 149 // Returns command line options that need to be set during testing. |
| 150 virtual base::CommandLine GetTestBaseCommandLine() = 0; |
| 151 }; |
| 152 |
| 153 void SetChromeCleanerRunnerTestDelegateForTesting( |
| 154 ChromeCleanerRunnerTestDelegate* test_delegate); |
| 155 |
| 156 } // namespace safe_browsing |
| 157 |
| 158 #endif // CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_CHROME_CLEANER_RUNNER_WIN
_H_ |
OLD | NEW |