| OLD | NEW |
| (Empty) | |
| 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 #include "chrome/browser/metrics/antivirus_metrics_provider_win.h" |
| 6 |
| 7 #include <iwscapi.h> |
| 8 #include <stddef.h> |
| 9 #include <windows.h> |
| 10 #include <wscapi.h> |
| 11 |
| 12 #include <string> |
| 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/callback.h" |
| 16 #include "base/feature_list.h" |
| 17 #include "base/file_version_info_win.h" |
| 18 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" |
| 20 #include "base/metrics/field_trial.h" |
| 21 #include "base/metrics/histogram.h" |
| 22 #include "base/path_service.h" |
| 23 #include "base/strings/string_util.h" |
| 24 #include "base/strings/sys_string_conversions.h" |
| 25 #include "base/task_runner_util.h" |
| 26 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/version.h" |
| 28 #include "base/win/scoped_bstr.h" |
| 29 #include "base/win/scoped_com_initializer.h" |
| 30 #include "base/win/scoped_comptr.h" |
| 31 #include "base/win/windows_version.h" |
| 32 #include "chrome/common/channel_info.h" |
| 33 #include "components/metrics/proto/system_profile.pb.h" |
| 34 #include "components/variations/metrics_util.h" |
| 35 #include "components/version_info/version_info.h" |
| 36 |
| 37 namespace { |
| 38 |
| 39 bool ShouldReportFullNames() { |
| 40 // The expectation is that this will be disabled for the majority of users, |
| 41 // but this allows a small group to be enabled on other channels if there are |
| 42 // a large percentage of hashes collected on these channels that are not |
| 43 // resolved to names previously collected on Canary channel. |
| 44 bool enabled = base::FeatureList::IsEnabled( |
| 45 AntiVirusMetricsProvider::kReportNamesFeature); |
| 46 |
| 47 if (chrome::GetChannel() == version_info::Channel::CANARY) |
| 48 return true; |
| 49 |
| 50 return enabled; |
| 51 } |
| 52 |
| 53 // Helper function for expanding all environment variables in |path|. |
| 54 std::wstring ExpandEnvironmentVariables(const std::wstring& path) { |
| 55 static const DWORD kMaxBuffer = 32 * 1024; // Max according to MSDN. |
| 56 std::wstring path_expanded; |
| 57 DWORD path_len = MAX_PATH; |
| 58 do { |
| 59 DWORD result = ExpandEnvironmentStrings( |
| 60 path.c_str(), base::WriteInto(&path_expanded, path_len), path_len); |
| 61 if (!result) { |
| 62 // Failed to expand variables. Return the original string. |
| 63 DPLOG(ERROR) << path; |
| 64 break; |
| 65 } |
| 66 if (result <= path_len) |
| 67 return path_expanded.substr(0, result - 1); |
| 68 path_len = result; |
| 69 } while (path_len < kMaxBuffer); |
| 70 |
| 71 return path; |
| 72 } |
| 73 |
| 74 // Helper function to take a |path| to a file, that might contain environment |
| 75 // strings, and read the file version information in |product_version|. Returns |
| 76 // true if it was possible to extract the file information correctly. |
| 77 bool GetProductVersion(std::wstring* path, std::string* product_version) { |
| 78 base::FilePath full_path(ExpandEnvironmentVariables(*path)); |
| 79 |
| 80 #if !defined(_WIN64) |
| 81 if (!base::PathExists(full_path)) { |
| 82 // On 32-bit builds, path might contain C:\Program Files (x86) instead of |
| 83 // C:\Program Files. |
| 84 base::ReplaceFirstSubstringAfterOffset(path, 0, L"%ProgramFiles%", |
| 85 L"%ProgramW6432%"); |
| 86 full_path = base::FilePath(ExpandEnvironmentVariables(*path)); |
| 87 } |
| 88 #endif // !defined(_WIN64) |
| 89 std::unique_ptr<FileVersionInfo> version_info( |
| 90 FileVersionInfo::CreateFileVersionInfo(full_path)); |
| 91 |
| 92 // It is not an error if the product version cannot be read, so continue in |
| 93 // this case. |
| 94 if (version_info.get()) { |
| 95 FileVersionInfoWin* version_info_win = |
| 96 static_cast<FileVersionInfoWin*>(version_info.get()); |
| 97 std::string version_str = |
| 98 base::SysWideToUTF8(version_info_win->product_version()); |
| 99 |
| 100 *product_version = std::move(version_str); |
| 101 return true; |
| 102 } |
| 103 |
| 104 return false; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 constexpr base::Feature AntiVirusMetricsProvider::kReportNamesFeature; |
| 110 |
| 111 AntiVirusMetricsProvider::AntiVirusMetricsProvider( |
| 112 scoped_refptr<base::TaskRunner> task_runner) |
| 113 : task_runner_(task_runner), weak_ptr_factory_(this) {} |
| 114 |
| 115 AntiVirusMetricsProvider::~AntiVirusMetricsProvider() {} |
| 116 |
| 117 void AntiVirusMetricsProvider::ProvideSystemProfileMetrics( |
| 118 metrics::SystemProfileProto* system_profile_proto) { |
| 119 for (const auto& av_product : av_products_) { |
| 120 metrics::SystemProfileProto_AntiVirusProduct* product = |
| 121 system_profile_proto->add_antivirus_product(); |
| 122 *product = av_product; |
| 123 } |
| 124 } |
| 125 |
| 126 void AntiVirusMetricsProvider::GetAntiVirusMetrics( |
| 127 const base::Closure& done_callback) { |
| 128 base::PostTaskAndReplyWithResult( |
| 129 task_runner_.get(), FROM_HERE, |
| 130 base::Bind(&AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread), |
| 131 base::Bind(&AntiVirusMetricsProvider::GotAntiVirusProducts, |
| 132 weak_ptr_factory_.GetWeakPtr(), done_callback)); |
| 133 } |
| 134 |
| 135 // static |
| 136 std::vector<AntiVirusMetricsProvider::AvProduct> |
| 137 AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread() { |
| 138 std::vector<AvProduct> av_products; |
| 139 |
| 140 ResultCode result = FillAntiVirusProducts(&av_products); |
| 141 |
| 142 UMA_HISTOGRAM_ENUMERATION("UMA.AntiVirusMetricsProvider.Result", |
| 143 result, |
| 144 RESULT_COUNT); |
| 145 |
| 146 return av_products; |
| 147 } |
| 148 |
| 149 void AntiVirusMetricsProvider::GotAntiVirusProducts( |
| 150 const base::Closure& done_callback, |
| 151 const std::vector<AvProduct>& av_products) { |
| 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 av_products_ = av_products; |
| 154 done_callback.Run(); |
| 155 } |
| 156 |
| 157 // static |
| 158 AntiVirusMetricsProvider::ResultCode |
| 159 AntiVirusMetricsProvider::FillAntiVirusProducts( |
| 160 std::vector<AvProduct>* products) { |
| 161 std::vector<AvProduct> result_list; |
| 162 base::ThreadRestrictions::AssertIOAllowed(); |
| 163 base::win::ScopedCOMInitializer com_initializer; |
| 164 |
| 165 if (!com_initializer.succeeded()) |
| 166 return RESULT_FAILED_TO_INITIALIZE_COM; |
| 167 |
| 168 base::win::ScopedComPtr<IWSCProductList> product_list; |
| 169 HRESULT result = |
| 170 CoCreateInstance(__uuidof(WSCProductList), NULL, CLSCTX_INPROC_SERVER, |
| 171 __uuidof(IWSCProductList), product_list.ReceiveVoid()); |
| 172 if (FAILED(result)) |
| 173 return RESULT_FAILED_TO_CREATE_INSTANCE; |
| 174 |
| 175 result = product_list->Initialize(WSC_SECURITY_PROVIDER_ANTIVIRUS); |
| 176 if (FAILED(result)) |
| 177 return RESULT_FAILED_TO_INITIALIZE_PRODUCT_LIST; |
| 178 |
| 179 LONG product_count; |
| 180 result = product_list->get_Count(&product_count); |
| 181 if (FAILED(result)) |
| 182 return RESULT_FAILED_TO_GET_PRODUCT_COUNT; |
| 183 |
| 184 for (LONG i = 0; i < product_count; i++) { |
| 185 IWscProduct* product = nullptr; |
| 186 result = product_list->get_Item(i, &product); |
| 187 if (FAILED(result)) |
| 188 return RESULT_FAILED_TO_GET_ITEM; |
| 189 |
| 190 static_assert(metrics::SystemProfileProto::AntiVirusState:: |
| 191 SystemProfileProto_AntiVirusState_STATE_ON == |
| 192 static_cast<metrics::SystemProfileProto::AntiVirusState>( |
| 193 WSC_SECURITY_PRODUCT_STATE_ON), |
| 194 "proto and API values must be the same"); |
| 195 static_assert(metrics::SystemProfileProto::AntiVirusState:: |
| 196 SystemProfileProto_AntiVirusState_STATE_OFF == |
| 197 static_cast<metrics::SystemProfileProto::AntiVirusState>( |
| 198 WSC_SECURITY_PRODUCT_STATE_OFF), |
| 199 "proto and API values must be the same"); |
| 200 static_assert(metrics::SystemProfileProto::AntiVirusState:: |
| 201 SystemProfileProto_AntiVirusState_STATE_SNOOZED == |
| 202 static_cast<metrics::SystemProfileProto::AntiVirusState>( |
| 203 WSC_SECURITY_PRODUCT_STATE_SNOOZED), |
| 204 "proto and API values must be the same"); |
| 205 static_assert(metrics::SystemProfileProto::AntiVirusState:: |
| 206 SystemProfileProto_AntiVirusState_STATE_EXPIRED == |
| 207 static_cast<metrics::SystemProfileProto::AntiVirusState>( |
| 208 WSC_SECURITY_PRODUCT_STATE_EXPIRED), |
| 209 "proto and API values must be the same"); |
| 210 |
| 211 AvProduct av_product; |
| 212 WSC_SECURITY_PRODUCT_STATE product_state; |
| 213 result = product->get_ProductState(&product_state); |
| 214 if (FAILED(result)) |
| 215 return RESULT_FAILED_TO_GET_PRODUCT_STATE; |
| 216 |
| 217 if (!metrics::SystemProfileProto_AntiVirusState_IsValid(product_state)) |
| 218 return RESULT_PRODUCT_STATE_INVALID; |
| 219 |
| 220 av_product.set_product_state( |
| 221 static_cast<metrics::SystemProfileProto::AntiVirusState>( |
| 222 product_state)); |
| 223 |
| 224 base::win::ScopedBstr product_name; |
| 225 result = product->get_ProductName(product_name.Receive()); |
| 226 if (FAILED(result)) |
| 227 return RESULT_FAILED_TO_GET_PRODUCT_NAME; |
| 228 std::string name = |
| 229 base::SysWideToUTF8(std::wstring(product_name, product_name.Length())); |
| 230 product_name.Release(); |
| 231 if (ShouldReportFullNames()) |
| 232 av_product.set_product_name(name); |
| 233 av_product.set_product_name_hash(metrics::HashName(name)); |
| 234 |
| 235 base::win::ScopedBstr remediation_path; |
| 236 result = product->get_RemediationPath(remediation_path.Receive()); |
| 237 if (FAILED(result)) |
| 238 return RESULT_FAILED_TO_GET_REMEDIATION_PATH; |
| 239 std::wstring path_str(remediation_path, remediation_path.Length()); |
| 240 remediation_path.Release(); |
| 241 |
| 242 std::string product_version; |
| 243 // Not a failure if the product version cannot be read from the file on |
| 244 // disk. |
| 245 if (GetProductVersion(&path_str, &product_version)) { |
| 246 if (ShouldReportFullNames()) |
| 247 av_product.set_product_version(product_version); |
| 248 av_product.set_product_version_hash(metrics::HashName(product_version)); |
| 249 } |
| 250 |
| 251 result_list.push_back(av_product); |
| 252 } |
| 253 |
| 254 *products = std::move(result_list); |
| 255 |
| 256 return RESULT_SUCCESS; |
| 257 } |
| OLD | NEW |