Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 // State is stored in the Windows registry in a value of the key | |
| 6 // HKEY_CURRENT_USER\Software\<browser>\IncidentsSent for each profile. The | |
| 7 // value names are profile directory BaseNames. | |
| 8 | |
| 9 #include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h " | |
| 10 | |
| 11 #include <windows.h> | |
| 12 | |
| 13 #include "base/numerics/safe_conversions.h" | |
| 14 #include "base/win/registry.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/installer/util/browser_distribution.h" | |
| 17 | |
| 18 namespace safe_browsing { | |
| 19 namespace platform_state_store { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Returns the path to the registry key holding profile-specific state values. | |
| 24 base::string16 GetStateStoreKeyName() { | |
| 25 base::string16 key_name(L"Software\\"); | |
| 26 return key_name.append( | |
| 27 BrowserDistribution::GetDistribution()->GetInstallSubDir()) | |
| 28 .append(L"\\IncidentsSent"); | |
| 29 } | |
| 30 | |
| 31 // Returns the name of the registry value for |profile|'s state. | |
| 32 base::string16 GetValueNameForProfile(Profile* profile) { | |
| 33 return profile->GetPath().BaseName().value(); | |
| 34 } | |
| 35 | |
| 36 // Clears |profile|'s state. | |
| 37 PlatformStateStoreLoadResult ClearStoreData(Profile* profile) { | |
| 38 base::win::RegKey key; | |
| 39 | |
| 40 if (key.Open(HKEY_CURRENT_USER, GetStateStoreKeyName().c_str(), | |
| 41 KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_32KEY) == | |
| 42 ERROR_SUCCESS) { | |
| 43 base::string16 value_name(GetValueNameForProfile(profile)); | |
| 44 if (key.HasValue(value_name.c_str())) { | |
| 45 if (key.DeleteValue(value_name.c_str()) == ERROR_SUCCESS) | |
| 46 return PlatformStateStoreLoadResult::CLEARED_DATA; | |
| 47 return PlatformStateStoreLoadResult::DATA_CLEAR_FAILED; | |
| 48 } | |
| 49 } | |
| 50 return PlatformStateStoreLoadResult::CLEARED_NO_DATA; | |
|
Cait (Slow)
2015/08/31 17:55:29
Is the assumption here that if we failed to open t
grt (UTC plus 2)
2015/09/01 13:55:23
Yes. While Open() could fail for other reasons, no
| |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 PlatformStateStoreLoadResult ReadStoreData(Profile* profile, | |
| 56 std::string* data) { | |
| 57 // Clear any old state if this is a new profile. | |
| 58 if (profile->IsNewProfile()) { | |
| 59 data->clear(); | |
| 60 return ClearStoreData(profile); | |
| 61 } | |
| 62 | |
| 63 base::string16 value_name(GetValueNameForProfile(profile)); | |
| 64 base::win::RegKey key; | |
| 65 if (key.Open(HKEY_CURRENT_USER, GetStateStoreKeyName().c_str(), | |
| 66 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
| 67 while (true) { | |
| 68 void* buffer = data->empty() ? nullptr : &(*data)[0]; | |
| 69 DWORD buffer_size = base::saturated_cast<DWORD, size_t>(data->size()); | |
| 70 LONG result = | |
| 71 key.ReadValue(value_name.c_str(), buffer, &buffer_size, nullptr); | |
| 72 // Trim the output string and return if all data was read. | |
| 73 if (result == ERROR_SUCCESS && buffer_size <= data->size()) { | |
| 74 data->resize(buffer_size); | |
| 75 return PlatformStateStoreLoadResult::SUCCESS; | |
| 76 } | |
| 77 // Increase the buffer and retry if more space was needed. Otherwise bail. | |
| 78 if (buffer_size > data->size()) | |
| 79 data->resize(buffer_size); | |
| 80 else | |
| 81 return PlatformStateStoreLoadResult::READ_FAILED; | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 return PlatformStateStoreLoadResult::OPEN_FAILED; | |
| 86 } | |
| 87 | |
| 88 void WriteStoreData(Profile* profile, const std::string& data) { | |
| 89 base::win::RegKey key; | |
| 90 if (key.Create(HKEY_CURRENT_USER, GetStateStoreKeyName().c_str(), | |
| 91 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | |
| 92 if (data.empty()) { | |
| 93 key.DeleteValue(GetValueNameForProfile(profile).c_str()); | |
| 94 } else { | |
| 95 key.WriteValue(GetValueNameForProfile(profile).c_str(), &data[0], | |
| 96 base::saturated_cast<DWORD, size_t>(data.size()), | |
| 97 REG_BINARY); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 } // namespace platform_state_store | |
| 103 } // namespace safe_browsing | |
| OLD | NEW |