1 // Copyright 2018 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_CHROME_CLEANER_LOGGING_LOGGING_SERVICE_API_H_
6 #define CHROME_CHROME_CLEANER_LOGGING_LOGGING_SERVICE_API_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/callback_forward.h"
12 #include "base/values.h"
13 #include "chrome/chrome_cleaner/logging/utils.h"
14 #include "chrome/chrome_cleaner/os/disk_util_types.h"
15 #include "chrome/chrome_cleaner/os/process.h"
16 #include "chrome/chrome_cleaner/pup_data/pup_data.h"
17 #include "chrome/chrome_cleaner/settings/settings_types.h"
18 #include "components/chrome_cleaner/public/constants/result_codes.h"
19 
20 namespace base {
21 class FilePath;
22 }  // namespace base
23 
24 namespace chrome_cleaner {
25 
26 class RegistryLogger;
27 
28 namespace internal {
29 
30 struct FileInformation;
31 struct RegistryValue;
32 
33 }  // namespace internal
34 
35 // Manage where the logs are sent, and expose an API for more specific logging.
36 class LoggingServiceAPI {
37  public:
38   typedef base::RepeatingCallback<void(bool)> UploadResultCallback;
39 
~LoggingServiceAPI()40   virtual ~LoggingServiceAPI() {}
41 
42   // Return the singleton instance which will get destroyed by the AtExitMgr.
43   // The instance must have been initialized with |Initialize| before
44   // being used, and |Terminate| must be called before releasing (or destroying)
45   // the object.
46   // Tests can force their own logging service implemenation with
47   // SetInstanceForTesting().
48   static LoggingServiceAPI* GetInstance();
49 
50   // Sets the logging service instance for testing purposes only.
51   // Using |logging_service| as null resets GetInstance() to its default
52   // behaviour.
53   // Recommended usage:
54   //   void SetUp() override {
55   //     LoggingServiceAPI::SetInstance(&my_logging_service_);
56   //     LoggingServiceAPI::GetInstance()->Initialize(&my_registry_logger_);
57   //   }
58   //   void TearDown() override {
59   //     LoggingServiceAPI::GetInstance()->Terminate();
60   //     LoggingServiceAPI::SetInstance(nullptr);
61   //   }
62   // Note: when my_logging_service_ is a MockLoggingService, Initialize() and
63   //       Terminate() don't need to be called.
64   static void SetInstanceForTesting(LoggingServiceAPI* logging_service);
65 
66   // All of the following functions must be called from the main UI thread:
67 
68   // Start and stop intercepting logs. The function |Terminate| disables uploads
69   // and flushes current content of the logging report. |registry_logger| is
70   // needed to make sure there are no logs upload scheduled tasks left active
71   // when logs upload is disabled. It can be nullptr when caller is positive
72   // that there are none, i.e., when initializing for the first time.
73   virtual void Initialize(RegistryLogger* registry_logger) = 0;
74   virtual void Terminate() = 0;
75 
76   // Send the current state of |chrome_cleaner_report_| to the Safe Browsing API
77   // and clear it. |registry_logger| is specified by the caller so that it can
78   // be mocked for testing.
79   virtual void SendLogsToSafeBrowsing(const UploadResultCallback& done_callback,
80                                       RegistryLogger* registry_logger) = 0;
81 
82   // Cancel all current and future waits, to speed up system shutdown.
83   virtual void CancelWaitForShutdown() = 0;
84 
85   // Enable / disable uploads of logs. |registry_logger| is needed to make sure
86   // there are no logs upload scheduled tasks left active when logs upload is
87   // disabled. It can be nullptr when caller is positive that there are none,
88   // i.e., when enabling for the first time.
89   virtual void EnableUploads(bool enabled, RegistryLogger* registry_logger) = 0;
90 
91   // Schedule a task to upload logs in case we fail to progress beyond the point
92   // from where this is called, which can be identified by |result_code|. These
93   // fall back logs use the current state of raw log lines without flushing
94   // them, and use |result_code| as the protobuf exit code.
95   virtual void ScheduleFallbackLogsUpload(RegistryLogger* registry_logger,
96                                           ResultCode result_code) = 0;
97 
98   // All of the following functions can be called from any thread:
99 
100   virtual bool uploads_enabled() const = 0;
101 
102   // Set |detailed_system_report| to |chrome_cleaner_report_|.
103   virtual void SetDetailedSystemReport(bool detailed_system_report) = 0;
104   virtual bool detailed_system_report_enabled() const = 0;
105 
106   // Add |found_uws_name| to |chrome_cleaner_report_|.
107   virtual void AddFoundUwS(const std::string& found_uws_name) = 0;
108 
109   // Add |found_uws| to |chrome_cleaner_report_| with the detail level of the
110   // UwS set according to |flags|.
111   // Can be called from any thread.
112   virtual void AddDetectedUwS(const PUPData::PUP* found_uws,
113                               UwSDetectedFlags flags) = 0;
114 
115   // Adds a converted UwS proto to the list of matched UwS.
116   virtual void AddDetectedUwS(const UwS& uws) = 0;
117 
118   // Set |exit_code| to |chrome_cleaner_report_|. Can be called from any thread.
119   // Must not be called except when really done.
120   virtual void SetExitCode(ResultCode exit_code) = 0;
121 
122   // Add a loaded module to the system report.
123   virtual void AddLoadedModule(
124       const std::wstring& name,
125       ModuleHost host,
126       const internal::FileInformation& file_information) = 0;
127 
128   // Add a running service to the system report.
129   virtual void AddService(
130       const std::wstring& display_name,
131       const std::wstring& service_name,
132       const internal::FileInformation& file_information) = 0;
133 
134   // Add an installed program to the system report.
135   virtual void AddInstalledProgram(const base::FilePath& folder_path) = 0;
136 
137   // Add a running process to the system report.
138   virtual void AddProcess(
139       const std::wstring& name,
140       const internal::FileInformation& file_information) = 0;
141 
142   // Add a registry value |registry_value| which may have |file_informations|
143   // associated with it to the system report.
144   virtual void AddRegistryValue(
145       const internal::RegistryValue& registry_value,
146       const std::vector<internal::FileInformation>& file_informations) = 0;
147 
148   // Add a layered service provider to the system report.
149   virtual void AddLayeredServiceProvider(
150       const std::vector<std::wstring>& guids,
151       const internal::FileInformation& file_information) = 0;
152 
153   // Set the WinInetProxy settings of the system report.
154   virtual void SetWinInetProxySettings(const std::wstring& config,
155                                        const std::wstring& bypass,
156                                        const std::wstring& auto_config_url,
157                                        bool autodetect) = 0;
158 
159   // Set the WinHttpProxy settings of the system report.
160   virtual void SetWinHttpProxySettings(const std::wstring& config,
161                                        const std::wstring& bypass) = 0;
162 
163   // Add an installed extension to the system report.
164   virtual void AddInstalledExtension(
165       const std::wstring& extension_id,
166       ExtensionInstallMethod install_method,
167       const std::vector<internal::FileInformation>& extension_files) = 0;
168 
169   // Add a scheduled task to the system report.
170   virtual void AddScheduledTask(
171       const std::wstring& name,
172       const std::wstring& description,
173       const std::vector<internal::FileInformation>& actions) = 0;
174 
175   // Add a ShortcutData to the system report.
176   virtual void AddShortcutData(
177       const std::wstring& lnk_path,
178       const std::wstring& executable_path,
179       const std::string& executable_hash,
180       const std::vector<std::wstring>& command_line_arguments) = 0;
181 
182   // Set |found_modified_shortcuts| in the |reporter_logs|.
183   virtual void SetFoundModifiedChromeShortcuts(
184       bool found_modified_shortcuts) = 0;
185 
186   // Set |scanned_locations| in the reporter log.
187   virtual void SetScannedLocations(
188       const std::vector<UwS::TraceLocation>& scanned_locations) = 0;
189 
190   // Log resource usage of a Chrome Cleanup process identified by
191   // |process_type|.
192   virtual void LogProcessInformation(SandboxType process_type,
193                                      const SystemResourceUsage& usage) = 0;
194 
195   // Returns whether all files detected for removable UwS were successfully
196   // deleted.
197   virtual bool AllExpectedRemovalsConfirmed() const = 0;
198 
199   // Return a raw representation of the current state of the report.
200   virtual std::string RawReportContent() = 0;
201 
202   // Read the content of |log_file| as a protocol buffer and replace current
203   // state with it. Return false on failures.
204   virtual bool ReadContentFromFile(const base::FilePath& log_file) = 0;
205 
206   // If in debug mode or switch --dump-raw-logs is present, save the serialized
207   // report proto to |executable||tag|.pb, where |executable| is the current
208   // binary name.
209   virtual void MaybeSaveLogsToFile(const std::wstring& tag);
210 
211  private:
212   static LoggingServiceAPI* logging_service_for_testing_;
213 };
214 
215 }  // namespace chrome_cleaner
216 
217 #endif  // CHROME_CHROME_CLEANER_LOGGING_LOGGING_SERVICE_API_H_
218