wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 1 | // Copyright 2016 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_METRICS_ANTIVIRUS_METRICS_PROVIDER_WIN_H_ |
| 6 | #define CHROME_BROWSER_METRICS_ANTIVIRUS_METRICS_PROVIDER_WIN_H_ |
| 7 | |
| 8 | #include "components/metrics/metrics_provider.h" |
| 9 | |
| 10 | #include <iwscapi.h> |
| 11 | #include <stddef.h> |
| 12 | |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "base/callback_forward.h" |
| 17 | #include "base/feature_list.h" |
| 18 | #include "base/gtest_prod_util.h" |
| 19 | #include "base/macros.h" |
| 20 | #include "base/memory/weak_ptr.h" |
pmonette | 7b9e781 | 2017-05-18 18:58:01 | [diff] [blame] | 21 | #include "base/sequence_checker.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 22 | #include "third_party/metrics_proto/system_profile.pb.h" |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 23 | |
| 24 | // AntiVirusMetricsProvider is responsible for adding antivirus information to |
| 25 | // the UMA system profile proto. |
| 26 | class AntiVirusMetricsProvider : public metrics::MetricsProvider { |
| 27 | public: |
| 28 | static constexpr base::Feature kReportNamesFeature = { |
| 29 | "ReportFullAVProductDetails", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 30 | |
pmonette | 7b9e781 | 2017-05-18 18:58:01 | [diff] [blame] | 31 | AntiVirusMetricsProvider(); |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 32 | |
| 33 | ~AntiVirusMetricsProvider() override; |
| 34 | |
| 35 | // metrics::MetricsDataProvider: |
Steven Holte | 915c99f | 2017-07-21 23:34:31 | [diff] [blame] | 36 | void AsyncInit(const base::Closure& done_callback) override; |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 37 | void ProvideSystemProfileMetrics( |
| 38 | metrics::SystemProfileProto* system_profile_proto) override; |
| 39 | |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 40 | private: |
| 41 | // This enum is reported via a histogram so new values should always be added |
| 42 | // at the end. |
| 43 | enum ResultCode { |
| 44 | RESULT_SUCCESS = 0, |
| 45 | RESULT_GENERIC_FAILURE = 1, |
| 46 | RESULT_FAILED_TO_INITIALIZE_COM = 2, |
| 47 | RESULT_FAILED_TO_CREATE_INSTANCE = 3, |
| 48 | RESULT_FAILED_TO_INITIALIZE_PRODUCT_LIST = 4, |
| 49 | RESULT_FAILED_TO_GET_PRODUCT_COUNT = 5, |
| 50 | RESULT_FAILED_TO_GET_ITEM = 6, |
| 51 | RESULT_FAILED_TO_GET_PRODUCT_STATE = 7, |
| 52 | RESULT_PRODUCT_STATE_INVALID = 8, |
| 53 | RESULT_FAILED_TO_GET_PRODUCT_NAME = 9, |
| 54 | RESULT_FAILED_TO_GET_REMEDIATION_PATH = 10, |
wfh | 6813e4f | 2016-07-01 22:03:45 | [diff] [blame] | 55 | RESULT_FAILED_TO_CONNECT_TO_WMI = 11, |
| 56 | RESULT_FAILED_TO_SET_SECURITY_BLANKET = 12, |
| 57 | RESULT_FAILED_TO_EXEC_WMI_QUERY = 13, |
| 58 | RESULT_FAILED_TO_ITERATE_RESULTS = 14, |
| 59 | RESULT_WSC_NOT_AVAILABLE = 15, |
| 60 | RESULT_COUNT = 16 |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | typedef metrics::SystemProfileProto::AntiVirusProduct AvProduct; |
| 64 | |
wfh | 6813e4f | 2016-07-01 22:03:45 | [diff] [blame] | 65 | // Query COM interface IWSCProductList for installed AV products. This |
| 66 | // interface is only available on Windows 8 and above. |
| 67 | static ResultCode FillAntiVirusProductsFromWSC( |
| 68 | std::vector<AvProduct>* products); |
wfh | 4b6a82030 | 2016-12-14 21:53:16 | [diff] [blame] | 69 | |
wfh | 6813e4f | 2016-07-01 22:03:45 | [diff] [blame] | 70 | // Query WMI ROOT\SecurityCenter2 for installed AV products. This interface is |
| 71 | // only available on Windows Vista and above. |
| 72 | static ResultCode FillAntiVirusProductsFromWMI( |
| 73 | std::vector<AvProduct>* products); |
wfh | 4b6a82030 | 2016-12-14 21:53:16 | [diff] [blame] | 74 | |
| 75 | // Query local machine configuration for other products that might not be |
| 76 | // registered in WMI or Security Center and add them to the product vector. |
| 77 | static void MaybeAddUnregisteredAntiVirusProducts( |
| 78 | std::vector<AvProduct>* products); |
| 79 | |
Robert Liao | a46c602 | 2017-10-31 18:28:09 | [diff] [blame] | 80 | static std::vector<AvProduct> GetAntiVirusProductsOnCOMSTAThread(); |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 81 | |
wfh | da07d58a | 2017-01-13 17:27:28 | [diff] [blame] | 82 | // Removes anything extraneous from the end of the product name such as |
| 83 | // versions, years, or anything containing numbers to make it more constant. |
| 84 | static std::string TrimVersionOfAvProductName(const std::string& av_product); |
| 85 | |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 86 | // Called when metrics are done being gathered from the FILE thread. |
| 87 | // |done_callback| is the callback that should be called once all metrics are |
| 88 | // gathered. |
| 89 | void GotAntiVirusProducts(const base::Closure& done_callback, |
| 90 | const std::vector<AvProduct>& av_products); |
| 91 | |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 92 | // Information on installed AntiVirus gathered. |
| 93 | std::vector<AvProduct> av_products_; |
| 94 | |
pmonette | 7b9e781 | 2017-05-18 18:58:01 | [diff] [blame] | 95 | SEQUENCE_CHECKER(sequence_checker_); |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 96 | base::WeakPtrFactory<AntiVirusMetricsProvider> weak_ptr_factory_; |
| 97 | |
wfh | 6813e4f | 2016-07-01 22:03:45 | [diff] [blame] | 98 | FRIEND_TEST_ALL_PREFIXES(AntiVirusMetricsProviderTest, GetMetricsFullName); |
wfh | da07d58a | 2017-01-13 17:27:28 | [diff] [blame] | 99 | FRIEND_TEST_ALL_PREFIXES(AntiVirusMetricsProviderSimpleTest, |
| 100 | StripProductVersion); |
wfh | 6813e4f | 2016-07-01 22:03:45 | [diff] [blame] | 101 | |
wfh | c768983fa | 2016-06-08 16:40:33 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(AntiVirusMetricsProvider); |
| 103 | }; |
| 104 | |
| 105 | #endif // CHROME_BROWSER_METRICS_ANTIVIRUS_METRICS_PROVIDER_WIN_H_ |