[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 5 | #include "chrome/browser/extensions/external_registry_loader_win.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 6 | |
[email protected] | 8e6ac4b | 2011-10-17 19:04:31 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
[email protected] | 0910bae | 2014-06-10 17:53:21 | [diff] [blame] | 10 | #include "base/files/scoped_file.h" |
asvitkine | aa06031 | 2016-09-01 22:44:13 | [diff] [blame^] | 11 | #include "base/metrics/histogram_macros.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 16 | #include "base/values.h" |
| 17 | #include "base/version.h" |
| 18 | #include "base/win/registry.h" |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 19 | #include "chrome/browser/extensions/external_provider_impl.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 20 | #include "components/crx_file/id_util.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 22 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 23 | using content::BrowserThread; |
| 24 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 25 | namespace { |
| 26 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 27 | // The Registry subkey that contains information about external extensions. |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 28 | const base::char16 kRegistryExtensions[] = |
| 29 | L"Software\\Google\\Chrome\\Extensions"; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 30 | |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 31 | // Registry value of the key that defines the installation parameter. |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 32 | const base::char16 kRegistryExtensionInstallParam[] = L"install_parameter"; |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 33 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 34 | // Registry value of the key that defines the path to the .crx file. |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 35 | const base::char16 kRegistryExtensionPath[] = L"path"; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 36 | |
| 37 | // Registry value of that key that defines the current version of the .crx file. |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 38 | const base::char16 kRegistryExtensionVersion[] = L"version"; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 39 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 40 | // Registry value of the key that defines an external update URL. |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 41 | const base::char16 kRegistryExtensionUpdateUrl[] = L"update_url"; |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 42 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 43 | bool CanOpenFileForReading(const base::FilePath& path) { |
[email protected] | 0910bae | 2014-06-10 17:53:21 | [diff] [blame] | 44 | base::ScopedFILE file_handle(base::OpenFile(path, "rb")); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 45 | return file_handle.get() != NULL; |
| 46 | } |
| 47 | |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 48 | std::string MakePrefName(const std::string& extension_id, |
| 49 | const std::string& pref_name) { |
| 50 | return base::StringPrintf("%s.%s", extension_id.c_str(), pref_name.c_str()); |
| 51 | } |
| 52 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 53 | } // namespace |
| 54 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 55 | namespace extensions { |
| 56 | |
| 57 | void ExternalRegistryLoader::StartLoading() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 58 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 | BrowserThread::PostTask( |
| 60 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 61 | base::Bind(&ExternalRegistryLoader::LoadOnFileThread, this)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 62 | } |
| 63 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 64 | std::unique_ptr<base::DictionaryValue> |
lazyboy | e863417 | 2016-01-28 00:10:48 | [diff] [blame] | 65 | ExternalRegistryLoader::LoadPrefsOnFileThread() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 66 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 67 | std::unique_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 68 | |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 69 | // A map of IDs, to weed out duplicates between HKCU and HKLM. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 70 | std::set<base::string16> keys; |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 71 | base::win::RegistryKeyIterator iterator_machine_key( |
wfh | 9353beb9 | 2014-10-09 04:31:38 | [diff] [blame] | 72 | HKEY_LOCAL_MACHINE, |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 73 | kRegistryExtensions, |
wfh | 9353beb9 | 2014-10-09 04:31:38 | [diff] [blame] | 74 | KEY_WOW64_32KEY); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 75 | for (; iterator_machine_key.Valid(); ++iterator_machine_key) |
| 76 | keys.insert(iterator_machine_key.Name()); |
| 77 | base::win::RegistryKeyIterator iterator_user_key( |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 78 | HKEY_CURRENT_USER, kRegistryExtensions); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 79 | for (; iterator_user_key.Valid(); ++iterator_user_key) |
| 80 | keys.insert(iterator_user_key.Name()); |
| 81 | |
| 82 | // Iterate over the keys found, first trying HKLM, then HKCU, as per Windows |
| 83 | // policy conventions. We only fall back to HKCU if the HKLM key cannot be |
| 84 | // opened, not if the data within the key is invalid, for example. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 85 | for (std::set<base::string16>::const_iterator it = keys.begin(); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 86 | it != keys.end(); ++it) { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 87 | base::win::RegKey key; |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 88 | base::string16 key_path = kRegistryExtensions; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 89 | key_path.append(L"\\"); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 90 | key_path.append(*it); |
| 91 | if (key.Open(HKEY_LOCAL_MACHINE, |
wfh | 9353beb9 | 2014-10-09 04:31:38 | [diff] [blame] | 92 | key_path.c_str(), |
| 93 | KEY_READ | KEY_WOW64_32KEY) != ERROR_SUCCESS && |
| 94 | key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ) != |
| 95 | ERROR_SUCCESS) { |
| 96 | LOG(ERROR) << "Unable to read registry key at path (HKLM & HKCU): " |
| 97 | << key_path << "."; |
| 98 | continue; |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 99 | } |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 100 | |
brettw | fce8d19 | 2015-08-10 19:07:51 | [diff] [blame] | 101 | std::string id = base::ToLowerASCII(base::UTF16ToASCII(*it)); |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 102 | if (!crx_file::id_util::IdIsValid(id)) { |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 103 | LOG(ERROR) << "Invalid id value " << id |
| 104 | << " for key " << key_path << "."; |
| 105 | continue; |
| 106 | } |
| 107 | |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 108 | base::string16 extension_dist_id; |
| 109 | if (key.ReadValue(kRegistryExtensionInstallParam, &extension_dist_id) == |
| 110 | ERROR_SUCCESS) { |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 111 | prefs->SetString(MakePrefName(id, ExternalProviderImpl::kInstallParam), |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 112 | base::UTF16ToASCII(extension_dist_id)); |
| 113 | } |
| 114 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 115 | // If there is an update URL present, copy it to prefs and ignore |
| 116 | // path and version keys for this entry. |
| 117 | base::string16 extension_update_url; |
| 118 | if (key.ReadValue(kRegistryExtensionUpdateUrl, &extension_update_url) |
| 119 | == ERROR_SUCCESS) { |
| 120 | prefs->SetString( |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 121 | MakePrefName(id, ExternalProviderImpl::kExternalUpdateUrl), |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 122 | base::UTF16ToASCII(extension_update_url)); |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 123 | continue; |
| 124 | } |
| 125 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 126 | base::string16 extension_path_str; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 127 | if (key.ReadValue(kRegistryExtensionPath, &extension_path_str) |
| 128 | != ERROR_SUCCESS) { |
| 129 | // TODO(erikkay): find a way to get this into about:extensions |
| 130 | LOG(ERROR) << "Missing value " << kRegistryExtensionPath |
| 131 | << " for key " << key_path << "."; |
| 132 | continue; |
| 133 | } |
| 134 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 135 | base::FilePath extension_path(extension_path_str); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 136 | if (!extension_path.IsAbsolute()) { |
| 137 | LOG(ERROR) << "File path " << extension_path_str |
| 138 | << " needs to be absolute in key " |
| 139 | << key_path; |
| 140 | continue; |
| 141 | } |
| 142 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 143 | if (!base::PathExists(extension_path)) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 144 | LOG(ERROR) << "File " << extension_path_str |
| 145 | << " for key " << key_path |
| 146 | << " does not exist or is not readable."; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | if (!CanOpenFileForReading(extension_path)) { |
| 151 | LOG(ERROR) << "File " << extension_path_str |
| 152 | << " for key " << key_path << " can not be read. " |
| 153 | << "Check that users who should have the extension " |
| 154 | << "installed have permission to read it."; |
| 155 | continue; |
| 156 | } |
| 157 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 158 | base::string16 extension_version; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 159 | if (key.ReadValue(kRegistryExtensionVersion, &extension_version) |
| 160 | != ERROR_SUCCESS) { |
| 161 | // TODO(erikkay): find a way to get this into about:extensions |
| 162 | LOG(ERROR) << "Missing value " << kRegistryExtensionVersion |
| 163 | << " for key " << key_path << "."; |
| 164 | continue; |
| 165 | } |
| 166 | |
pwnall | cbd7319 | 2016-08-22 18:59:17 | [diff] [blame] | 167 | base::Version version(base::UTF16ToASCII(extension_version)); |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 168 | if (!version.IsValid()) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 169 | LOG(ERROR) << "Invalid version value " << extension_version |
| 170 | << " for key " << key_path << "."; |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | prefs->SetString( |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 175 | MakePrefName(id, ExternalProviderImpl::kExternalVersion), |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 176 | base::UTF16ToASCII(extension_version)); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 177 | prefs->SetString( |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 178 | MakePrefName(id, ExternalProviderImpl::kExternalCrx), |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 179 | extension_path_str); |
[email protected] | 2b6a580 | 2014-08-16 07:58:08 | [diff] [blame] | 180 | prefs->SetBoolean( |
| 181 | MakePrefName(id, ExternalProviderImpl::kMayBeUntrusted), |
| 182 | true); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 183 | } |
| 184 | |
lazyboy | e863417 | 2016-01-28 00:10:48 | [diff] [blame] | 185 | return prefs; |
| 186 | } |
| 187 | |
| 188 | void ExternalRegistryLoader::LoadOnFileThread() { |
| 189 | base::TimeTicks start_time = base::TimeTicks::Now(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 190 | std::unique_ptr<base::DictionaryValue> initial_prefs = |
| 191 | LoadPrefsOnFileThread(); |
lazyboy | e863417 | 2016-01-28 00:10:48 | [diff] [blame] | 192 | prefs_.reset(initial_prefs.release()); |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 193 | LOCAL_HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWin", |
| 194 | base::TimeTicks::Now() - start_time); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 195 | BrowserThread::PostTask( |
| 196 | BrowserThread::UI, FROM_HERE, |
lazyboy | e863417 | 2016-01-28 00:10:48 | [diff] [blame] | 197 | base::Bind(&ExternalRegistryLoader::CompleteLoadAndStartWatchingRegistry, |
| 198 | this)); |
| 199 | } |
| 200 | |
| 201 | void ExternalRegistryLoader::CompleteLoadAndStartWatchingRegistry() { |
| 202 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 203 | LoadFinished(); |
| 204 | |
| 205 | // Start watching registry. |
| 206 | if (hklm_key_.Create(HKEY_LOCAL_MACHINE, kRegistryExtensions, |
| 207 | KEY_NOTIFY | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
| 208 | base::win::RegKey::ChangeCallback callback = |
| 209 | base::Bind(&ExternalRegistryLoader::OnRegistryKeyChanged, |
| 210 | base::Unretained(this), base::Unretained(&hklm_key_)); |
| 211 | hklm_key_.StartWatching(callback); |
| 212 | } else { |
| 213 | LOG(WARNING) << "Error observing HKLM."; |
| 214 | } |
| 215 | |
| 216 | if (hkcu_key_.Create(HKEY_CURRENT_USER, kRegistryExtensions, KEY_NOTIFY) == |
| 217 | ERROR_SUCCESS) { |
| 218 | base::win::RegKey::ChangeCallback callback = |
| 219 | base::Bind(&ExternalRegistryLoader::OnRegistryKeyChanged, |
| 220 | base::Unretained(this), base::Unretained(&hkcu_key_)); |
| 221 | hkcu_key_.StartWatching(callback); |
| 222 | } else { |
| 223 | LOG(WARNING) << "Error observing HKCU."; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void ExternalRegistryLoader::OnRegistryKeyChanged(base::win::RegKey* key) { |
| 228 | // |OnRegistryKeyChanged| is removed as an observer when the ChangeCallback is |
| 229 | // called, so we need to re-register. |
| 230 | key->StartWatching(base::Bind(&ExternalRegistryLoader::OnRegistryKeyChanged, |
| 231 | base::Unretained(this), base::Unretained(key))); |
| 232 | |
| 233 | BrowserThread::PostTask( |
| 234 | BrowserThread::FILE, FROM_HERE, |
| 235 | base::Bind(&ExternalRegistryLoader::UpdatePrefsOnFileThread, this)); |
| 236 | } |
| 237 | |
| 238 | void ExternalRegistryLoader::UpdatePrefsOnFileThread() { |
| 239 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 240 | base::TimeTicks start_time = base::TimeTicks::Now(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 241 | std::unique_ptr<base::DictionaryValue> prefs = LoadPrefsOnFileThread(); |
lazyboy | e863417 | 2016-01-28 00:10:48 | [diff] [blame] | 242 | LOCAL_HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWinUpdate", |
| 243 | base::TimeTicks::Now() - start_time); |
| 244 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 245 | base::Bind(&ExternalRegistryLoader::OnUpdated, this, |
| 246 | base::Passed(&prefs))); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 247 | } |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 248 | |
| 249 | } // namespace extensions |