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 #ifndef CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_SW_REPORTER_INVOCATION_WIN_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_SW_REPORTER_INVOCATION_WIN_H_
7 
8 #include <stdint.h>
9 
10 #include <queue>
11 #include <string>
12 
13 #include "base/command_line.h"
14 #include "base/version.h"
15 #include "components/chrome_cleaner/public/constants/constants.h"
16 
17 namespace safe_browsing {
18 
19 // These values are used to send UMA information and are replicated in the
20 // enums.xml file, so the order MUST NOT CHANGE.
21 enum class SwReporterInvocationResult {
22   kUnspecified,
23   // Tried to start a new run, but a user-initiated run was already
24   // happening. The UI should never allow this to happen.
25   kNotScheduled,
26   // The reporter process timed-out while running.
27   kTimedOut,
28   // The on-demand reporter run failed to download a new version of the reporter
29   // component.
30   kComponentNotAvailable,
31   // The reporter failed to start.
32   kProcessFailedToLaunch,
33   // The reporter ended with a failure.
34   kGeneralFailure,
35   // The reporter ran successfully, but didn't find cleanable unwanted software.
36   kNothingFound,
37   // A periodic reporter sequence ran successfully and found cleanable unwanted
38   // software, but the user shouldn't be prompted at this time.
39   kCleanupNotOffered,
40   // The reporter ran successfully and found cleanable unwanted software, and
41   // a cleanup should be offered. A notification with this result should be
42   // immediately followed by an attempt to run the cleaner in scanning mode.
43   kCleanupToBeOffered,
44 
45   kMax,
46 };
47 
48 // Identifies if an invocation was created during periodic reporter runs
49 // or because the user explicitly initiated a cleanup. The invocation type
50 // controls whether a prompt dialog will be shown to the user and under what
51 // conditions logs may be uploaded to Google.
52 //
53 // These values are used to send UMA information and are replicated in the
54 // enums.xml file, so the order MUST NOT CHANGE.
55 enum class SwReporterInvocationType {
56   // Default value that should never be used for valid invocations.
57   kUnspecified,
58   // Periodic runs of the reporter are initiated by Chrome after startup.
59   // If removable unwanted software is found the user may be prompted to
60   // run the Chrome Cleanup tool. Logs from the software reporter will only
61   // be uploaded if the user has opted-into SBER2 and if unwanted software
62   // is found on the system. The cleaner process in scanning mode will not
63   // upload logs.
64   kPeriodicRun,
65   // User-initiated runs in which the user has opted-out of sending details
66   // to Google. Those runs are intended to be completely driven from the
67   // Settings page, so a prompt dialog will not be shown to the user if
68   // removable unwanted software is found. Logs will not be uploaded from the
69   // reporter, even if the user has opted into SBER2, and cleaner logs will not
70   // be uploaded.
71   kUserInitiatedWithLogsDisallowed,
72   // User-initiated runs in which the user has not opted-out of sending
73   // details to Google. Those runs are intended to be completely driven from
74   // the Settings page, so a prompt dialog will not be shown to the user if
75   // removable unwanted software is found. Logs will be uploaded from both
76   // the reporter and the cleaner in scanning mode (which will only run if
77   // unwanted software is found by the reporter).
78   kUserInitiatedWithLogsAllowed,
79 
80   kMax,
81 };
82 
83 // Parameters used to invoke the sw_reporter component.
84 class SwReporterInvocation {
85  public:
86   // Flags to control behaviours the Software Reporter should support by
87   // default. These flags are set in the Reporter installer, and experimental
88   // versions of the reporter will turn on the behaviours that are not yet
89   // supported.
90   using Behaviours = uint32_t;
91   enum : Behaviours {
92     BEHAVIOUR_LOG_EXIT_CODE_TO_PREFS = 0x2,
93     BEHAVIOUR_TRIGGER_PROMPT = 0x4,
94 
95     BEHAVIOURS_ENABLED_BY_DEFAULT =
96         BEHAVIOUR_LOG_EXIT_CODE_TO_PREFS | BEHAVIOUR_TRIGGER_PROMPT,
97   };
98 
99   explicit SwReporterInvocation(const base::CommandLine& command_line);
100   SwReporterInvocation(const SwReporterInvocation& invocation);
101   void operator=(const SwReporterInvocation& invocation);
102 
103   // Fluent interface methods, intended to be used during initialization.
104   // Sample usage:
105   //   auto invocation = SwReporterInvocation(command_line)
106   //       .WithSuffix("MySuffix")
107   //       .WithSupportedBehaviours(
108   //           SwReporterInvocation::Behaviours::BEHAVIOUR_TRIGGER_PROMPT);
109   SwReporterInvocation& WithSuffix(const std::string& suffix);
110   SwReporterInvocation& WithSupportedBehaviours(
111       Behaviours supported_behaviours);
112 
113   bool operator==(const SwReporterInvocation& other) const;
114 
115   const base::CommandLine& command_line() const;
116   base::CommandLine& mutable_command_line();
117 
118   Behaviours supported_behaviours() const;
119   bool BehaviourIsSupported(Behaviours intended_behaviour) const;
120 
121   // Experimental versions of the reporter will write metrics to registry keys
122   // ending in |suffix_|. Those metrics should be copied to UMA histograms also
123   // ending in |suffix_|. For the canonical version, |suffix_| will be empty.
124   std::string suffix() const;
125 
126   // Indicates if the invocation type allows logs to be uploaded by the
127   // reporter process.
128   bool reporter_logs_upload_enabled() const;
129   void set_reporter_logs_upload_enabled(bool reporter_logs_upload_enabled);
130 
131   // Indicates if the invocation type allows logs to be uploaded by the
132   // cleaner process in scanning mode.
133   bool cleaner_logs_upload_enabled() const;
134   void set_cleaner_logs_upload_enabled(bool cleaner_logs_upload_enabled);
135 
136   chrome_cleaner::ChromePromptValue chrome_prompt() const;
137   void set_chrome_prompt(chrome_cleaner::ChromePromptValue chrome_prompt);
138 
139  private:
140   base::CommandLine command_line_;
141 
142   Behaviours supported_behaviours_ = BEHAVIOURS_ENABLED_BY_DEFAULT;
143 
144   std::string suffix_;
145 
146   bool reporter_logs_upload_enabled_ = false;
147   bool cleaner_logs_upload_enabled_ = false;
148 
149   chrome_cleaner::ChromePromptValue chrome_prompt_ =
150       chrome_cleaner::ChromePromptValue::kUnspecified;
151 };
152 
153 class SwReporterInvocationSequence {
154  public:
155   using Queue = std::queue<SwReporterInvocation>;
156 
157   explicit SwReporterInvocationSequence(
158       const base::Version& version = base::Version());
159   SwReporterInvocationSequence(SwReporterInvocationSequence&& queue);
160   SwReporterInvocationSequence(
161       const SwReporterInvocationSequence& invocations_sequence);
162   virtual ~SwReporterInvocationSequence();
163 
164   void PushInvocation(const SwReporterInvocation& invocation);
165 
166   void operator=(SwReporterInvocationSequence&& queue);
167 
168   base::Version version() const;
169 
170   const Queue& container() const;
171   Queue& mutable_container();
172 
173  private:
174   base::Version version_;
175   Queue container_;
176 };
177 
178 }  // namespace safe_browsing
179 
180 #endif  // CHROME_BROWSER_SAFE_BROWSING_CHROME_CLEANER_SW_REPORTER_INVOCATION_WIN_H_
181