1 // Copyright 2019 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 #include "chrome/chrome_cleaner/engines/target/engine_commands_impl.h"
6 
7 #include <string>
8 #include <utility>
9 
10 #include "base/macros.h"
11 #include "chrome/chrome_cleaner/crash/crash_keys.h"
12 #include "chrome/chrome_cleaner/engines/target/cleaner_engine_requests_proxy.h"
13 #include "chrome/chrome_cleaner/engines/target/engine_cleanup_results_proxy.h"
14 #include "chrome/chrome_cleaner/engines/target/engine_file_requests_proxy.h"
15 #include "chrome/chrome_cleaner/engines/target/engine_requests_proxy.h"
16 #include "chrome/chrome_cleaner/engines/target/engine_scan_results_proxy.h"
17 
18 namespace chrome_cleaner {
19 
20 namespace {
21 
22 constexpr char kStageCrashKey[] = "stage";
23 
24 class ScopedCrashStageRecorder {
25  public:
ScopedCrashStageRecorder(const std::string & stage)26   explicit ScopedCrashStageRecorder(const std::string& stage) : stage_(stage) {
27     SetCrashKey(kStageCrashKey, stage_);
28   }
29 
~ScopedCrashStageRecorder()30   ~ScopedCrashStageRecorder() {
31     stage_ += "-done";
32     SetCrashKey(kStageCrashKey, stage_);
33   }
34 
35  private:
36   std::string stage_;
37 
38   DISALLOW_COPY_AND_ASSIGN(ScopedCrashStageRecorder);
39 };
40 
41 }  // namespace
42 
EngineCommandsImpl(scoped_refptr<EngineDelegate> engine_delegate,mojo::PendingReceiver<mojom::EngineCommands> receiver,scoped_refptr<base::SingleThreadTaskRunner> task_runner,base::OnceClosure error_handler)43 EngineCommandsImpl::EngineCommandsImpl(
44     scoped_refptr<EngineDelegate> engine_delegate,
45     mojo::PendingReceiver<mojom::EngineCommands> receiver,
46     scoped_refptr<base::SingleThreadTaskRunner> task_runner,
47     base::OnceClosure error_handler)
48     : engine_delegate_(engine_delegate),
49       receiver_(this, std::move(receiver)),
50       task_runner_(task_runner) {
51   receiver_.set_disconnect_handler(std::move(error_handler));
52 }
53 
54 EngineCommandsImpl::~EngineCommandsImpl() = default;
55 
Initialize(mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,const base::FilePath & log_directory_path,InitializeCallback callback)56 void EngineCommandsImpl::Initialize(
57     mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,
58     const base::FilePath& log_directory_path,
59     InitializeCallback callback) {
60   ScopedCrashStageRecorder crash_stage(__func__);
61 
62   // Create proxies to pass requests to the broker process over Mojo.
63   scoped_refptr<EngineFileRequestsProxy> file_requests_proxy =
64       base::MakeRefCounted<EngineFileRequestsProxy>(std::move(file_requests),
65                                                     task_runner_);
66 
67   // This object is not retained because it outlives the callback: it's
68   // destroyed on this sequence, once the main thread returns, which should only
69   // happen after initialization has completed. If the broker process
70   // terminates, then this process will be also be terminated by the connection
71   // error handler, and there is not need to add complexity to handle it.
72   engine_delegate_->Initialize(
73       log_directory_path, file_requests_proxy,
74       base::BindOnce(&EngineCommandsImpl::PostInitializeCallback,
75                      base::Unretained(this), std::move(callback)));
76 }
77 
StartScan(const std::vector<UwSId> & enabled_uws,const std::vector<UwS::TraceLocation> & enabled_trace_locations,bool include_details,mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,mojo::PendingAssociatedRemote<mojom::EngineRequests> sandboxed_engine_requests,mojo::PendingAssociatedRemote<mojom::EngineScanResults> scan_results,StartScanCallback callback)78 void EngineCommandsImpl::StartScan(
79     const std::vector<UwSId>& enabled_uws,
80     const std::vector<UwS::TraceLocation>& enabled_trace_locations,
81     bool include_details,
82     mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,
83     mojo::PendingAssociatedRemote<mojom::EngineRequests>
84         sandboxed_engine_requests,
85     mojo::PendingAssociatedRemote<mojom::EngineScanResults> scan_results,
86     StartScanCallback callback) {
87   ScopedCrashStageRecorder crash_stage(__func__);
88 
89   // Create proxies to pass requests to the broker process over Mojo.
90   scoped_refptr<EngineFileRequestsProxy> file_requests_proxy =
91       base::MakeRefCounted<EngineFileRequestsProxy>(std::move(file_requests),
92                                                     task_runner_);
93 
94   scoped_refptr<EngineRequestsProxy> engine_requests_proxy =
95       base::MakeRefCounted<EngineRequestsProxy>(
96           std::move(sandboxed_engine_requests), task_runner_);
97 
98   // Create an EngineScanResults proxy to send results back over the
99   // Mojo connection.
100   scoped_refptr<EngineScanResultsProxy> scan_results_proxy =
101       base::MakeRefCounted<EngineScanResultsProxy>(std::move(scan_results),
102                                                    task_runner_);
103 
104   uint32_t result_code = engine_delegate_->StartScan(
105       enabled_uws, enabled_trace_locations, include_details,
106       file_requests_proxy, engine_requests_proxy, scan_results_proxy);
107   std::move(callback).Run(result_code);
108 }
109 
StartCleanup(const std::vector<UwSId> & enabled_uws,mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,mojo::PendingAssociatedRemote<mojom::EngineRequests> sandboxed_engine_requests,mojo::PendingAssociatedRemote<mojom::CleanerEngineRequests> sandboxed_cleaner_engine_requests,mojo::PendingAssociatedRemote<mojom::EngineCleanupResults> cleanup_results,StartCleanupCallback callback)110 void EngineCommandsImpl::StartCleanup(
111     const std::vector<UwSId>& enabled_uws,
112     mojo::PendingAssociatedRemote<mojom::EngineFileRequests> file_requests,
113     mojo::PendingAssociatedRemote<mojom::EngineRequests>
114         sandboxed_engine_requests,
115     mojo::PendingAssociatedRemote<mojom::CleanerEngineRequests>
116         sandboxed_cleaner_engine_requests,
117     mojo::PendingAssociatedRemote<mojom::EngineCleanupResults> cleanup_results,
118     StartCleanupCallback callback) {
119   ScopedCrashStageRecorder crash_stage(__func__);
120 
121   // Create proxies to pass requests to the broker process over Mojo.
122   scoped_refptr<EngineFileRequestsProxy> file_requests_proxy =
123       base::MakeRefCounted<EngineFileRequestsProxy>(std::move(file_requests),
124                                                     task_runner_);
125 
126   scoped_refptr<EngineRequestsProxy> engine_requests_proxy =
127       base::MakeRefCounted<EngineRequestsProxy>(
128           std::move(sandboxed_engine_requests), task_runner_);
129 
130   scoped_refptr<CleanerEngineRequestsProxy> cleaner_engine_requests_proxy =
131       base::MakeRefCounted<CleanerEngineRequestsProxy>(
132           std::move(sandboxed_cleaner_engine_requests), task_runner_);
133 
134   // Create an EngineCleanupResults proxy to send results back over the
135   // Mojo connection.
136   scoped_refptr<EngineCleanupResultsProxy> cleanup_results_proxy =
137       base::MakeRefCounted<EngineCleanupResultsProxy>(
138           std::move(cleanup_results), task_runner_);
139 
140   uint32_t result_code = engine_delegate_->StartCleanup(
141       enabled_uws, file_requests_proxy, engine_requests_proxy,
142       cleaner_engine_requests_proxy, cleanup_results_proxy);
143   std::move(callback).Run(result_code);
144 }
145 
Finalize(FinalizeCallback callback)146 void EngineCommandsImpl::Finalize(FinalizeCallback callback) {
147   ScopedCrashStageRecorder crash_stage(__func__);
148   uint32_t result_code = engine_delegate_->Finalize();
149   std::move(callback).Run(result_code);
150 }
151 
PostInitializeCallback(mojom::EngineCommands::InitializeCallback callback,uint32_t result_code)152 void EngineCommandsImpl::PostInitializeCallback(
153     mojom::EngineCommands::InitializeCallback callback,
154     uint32_t result_code) {
155   task_runner_->PostTask(FROM_HERE,
156                          base::BindOnce(std::move(callback), result_code));
157 }
158 
159 }  // namespace chrome_cleaner
160