| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/conflicts/module_info_util_win.h" | 5 #include "chrome/browser/conflicts/module_info_util_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <tlhelp32.h> | 9 #include <tlhelp32.h> |
| 10 #include <wincrypt.h> | 10 #include <wincrypt.h> |
| 11 #include <wintrust.h> | 11 #include <wintrust.h> |
| 12 | 12 |
| 13 // This must be after wincrypt and wintrust. | 13 // This must be after wincrypt and wintrust. |
| 14 #include <mscat.h> | 14 #include <mscat.h> |
| 15 | 15 |
| 16 #include <limits> |
| 16 #include <memory> | 17 #include <memory> |
| 17 #include <vector> | 18 #include <string> |
| 18 | 19 |
| 20 #include "base/environment.h" |
| 21 #include "base/i18n/case_conversion.h" |
| 19 #include "base/scoped_generic.h" | 22 #include "base/scoped_generic.h" |
| 23 #include "base/strings/string_util.h" |
| 24 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/win/scoped_handle.h" | 25 #include "base/win/scoped_handle.h" |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 // Helper for scoped tracking an HCERTSTORE. | 29 // Helper for scoped tracking an HCERTSTORE. |
| 25 struct ScopedHCERTSTORETraits { | 30 struct ScopedHCERTSTORETraits { |
| 26 static HCERTSTORE InvalidValue() { return nullptr; } | 31 static HCERTSTORE InvalidValue() { return nullptr; } |
| 27 static void Free(HCERTSTORE store) { ::CertCloseStore(store, 0); } | 32 static void Free(HCERTSTORE store) { ::CertCloseStore(store, 0); } |
| 28 }; | 33 }; |
| 29 using ScopedHCERTSTORE = | 34 using ScopedHCERTSTORE = |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 return; | 236 return; |
| 232 | 237 |
| 233 base::string16 subject = GetSubjectNameInFile(filename); | 238 base::string16 subject = GetSubjectNameInFile(filename); |
| 234 if (subject.empty()) | 239 if (subject.empty()) |
| 235 return; | 240 return; |
| 236 | 241 |
| 237 certificate_info->type = CertificateType::CERTIFICATE_IN_FILE; | 242 certificate_info->type = CertificateType::CERTIFICATE_IN_FILE; |
| 238 certificate_info->path = filename; | 243 certificate_info->path = filename; |
| 239 certificate_info->subject = subject; | 244 certificate_info->subject = subject; |
| 240 } | 245 } |
| 246 |
| 247 StringMapping GetEnvironmentVariablesMapping( |
| 248 const std::vector<base::string16>& environment_variables) { |
| 249 std::unique_ptr<base::Environment> environment(base::Environment::Create()); |
| 250 |
| 251 StringMapping string_mapping; |
| 252 for (const base::string16& variable : environment_variables) { |
| 253 std::string value; |
| 254 if (environment->GetVar(base::UTF16ToASCII(variable).c_str(), &value)) { |
| 255 value = base::TrimString(value, "\\", base::TRIM_TRAILING).as_string(); |
| 256 string_mapping.push_back( |
| 257 std::make_pair(base::i18n::ToLower(base::UTF8ToUTF16(value)), |
| 258 L"%" + base::i18n::ToLower(variable) + L"%")); |
| 259 } |
| 260 } |
| 261 |
| 262 return string_mapping; |
| 263 } |
| 264 |
| 265 void CollapseMatchingPrefixInPath(const StringMapping& prefix_mapping, |
| 266 base::string16* path) { |
| 267 const base::string16 path_copy = *path; |
| 268 DCHECK_EQ(base::i18n::ToLower(path_copy), path_copy); |
| 269 |
| 270 size_t min_length = std::numeric_limits<size_t>::max(); |
| 271 for (const auto& mapping : prefix_mapping) { |
| 272 DCHECK_EQ(base::i18n::ToLower(mapping.first), mapping.first); |
| 273 if (base::StartsWith(path_copy, mapping.first, |
| 274 base::CompareCase::SENSITIVE)) { |
| 275 // Make sure the matching prefix is a full path component. |
| 276 if (path_copy[mapping.first.length()] != '\\' && |
| 277 path_copy[mapping.first.length()] != '\0') { |
| 278 continue; |
| 279 } |
| 280 |
| 281 base::string16 collapsed_path = path_copy; |
| 282 base::ReplaceFirstSubstringAfterOffset(&collapsed_path, 0, mapping.first, |
| 283 mapping.second); |
| 284 size_t length = collapsed_path.length() - mapping.second.length(); |
| 285 if (length < min_length) { |
| 286 *path = collapsed_path; |
| 287 min_length = length; |
| 288 } |
| 289 } |
| 290 } |
| 291 } |
| OLD | NEW |