Remove chrome://apps from chrome-urls on ChromeOS.
[chromium-blink-merge.git] / rlz / win / lib / registry_util.cc
blob8fba7dd47aa2f3b14368d62705786091154b5f76
1 // Copyright (c) 2012 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 // A helper library to keep track of a user's key by SID.
6 // Used by RLZ libary. Also to be used by SearchWithGoogle library.
8 #include "rlz/win/lib/registry_util.h"
10 #include "base/process/process_info.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/win/registry.h"
13 #include "base/win/windows_version.h"
14 #include "rlz/lib/assert.h"
15 #include "rlz/win/lib/process_info.h"
17 namespace rlz_lib {
19 bool RegKeyReadValue(const base::win::RegKey& key, const wchar_t* name,
20 char* value, size_t* value_size) {
21 value[0] = 0;
23 std::wstring value_string;
24 if (key.ReadValue(name, &value_string) != ERROR_SUCCESS) {
25 return false;
28 if (value_string.length() > *value_size) {
29 *value_size = value_string.length();
30 return false;
33 // Note that RLZ string are always ASCII by design.
34 strncpy(value, base::WideToUTF8(value_string).c_str(), *value_size);
35 value[*value_size - 1] = 0;
36 return true;
39 bool RegKeyWriteValue(base::win::RegKey* key, const wchar_t* name,
40 const char* value) {
41 base::string16 value_string(base::ASCIIToUTF16(value));
42 return key->WriteValue(name, value_string.c_str()) == ERROR_SUCCESS;
45 bool HasUserKeyAccess(bool write_access) {
46 // The caller is trying to access HKEY_CURRENT_USER. Test to see if we can
47 // read from there. Don't try HKEY_CURRENT_USER because this will cause
48 // problems in the unit tests: if we open HKEY_CURRENT_USER directly here,
49 // the overriding done for unit tests will no longer work. So we try subkey
50 // "Software" which is known to always exist.
51 base::win::RegKey key;
52 if (key.Open(HKEY_CURRENT_USER, L"Software", KEY_READ) != ERROR_SUCCESS)
53 ASSERT_STRING("Could not open HKEY_CURRENT_USER");
55 if (ProcessInfo::IsRunningAsSystem()) {
56 ASSERT_STRING("UserKey::HasAccess: No access as SYSTEM without SID set.");
57 return false;
60 if (write_access) {
61 if (base::win::GetVersion() < base::win::VERSION_VISTA)
62 return true;
64 if (base::GetCurrentProcessIntegrityLevel() <= base::LOW_INTEGRITY) {
65 ASSERT_STRING("UserKey::HasAccess: Cannot write from Low Integrity.");
66 return false;
69 return true;
72 } // namespace rlz_lib