[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] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 0910bae | 2014-06-10 17:53:21 | [diff] [blame^] | 10 | #include "base/files/scoped_file.h" |
[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 15 | #include "base/values.h" |
| 16 | #include "base/version.h" |
| 17 | #include "base/win/registry.h" |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 18 | #include "chrome/browser/extensions/external_provider_impl.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 21 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 22 | using content::BrowserThread; |
| 23 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 26 | // The Registry subkey that contains information about external extensions. |
| 27 | const char kRegistryExtensions[] = "Software\\Google\\Chrome\\Extensions"; |
| 28 | |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 29 | // Registry value of the key that defines the installation parameter. |
| 30 | const wchar_t kRegistryExtensionInstallParam[] = L"install_parameter"; |
| 31 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 32 | // Registry value of the key that defines the path to the .crx file. |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 33 | const wchar_t kRegistryExtensionPath[] = L"path"; |
| 34 | |
| 35 | // Registry value of that key that defines the current version of the .crx file. |
| 36 | const wchar_t kRegistryExtensionVersion[] = L"version"; |
| 37 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 38 | // Registry value of the key that defines an external update URL. |
| 39 | const wchar_t kRegistryExtensionUpdateUrl[] = L"update_url"; |
| 40 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 41 | bool CanOpenFileForReading(const base::FilePath& path) { |
[email protected] | 0910bae | 2014-06-10 17:53:21 | [diff] [blame^] | 42 | base::ScopedFILE file_handle(base::OpenFile(path, "rb")); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 43 | return file_handle.get() != NULL; |
| 44 | } |
| 45 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 46 | } // namespace |
| 47 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 48 | namespace extensions { |
| 49 | |
| 50 | void ExternalRegistryLoader::StartLoading() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 51 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 | BrowserThread::PostTask( |
| 53 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 54 | base::Bind(&ExternalRegistryLoader::LoadOnFileThread, this)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 57 | void ExternalRegistryLoader::LoadOnFileThread() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 58 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 59 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 60 | scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 61 | |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 62 | // A map of IDs, to weed out duplicates between HKCU and HKLM. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 63 | std::set<base::string16> keys; |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 64 | base::win::RegistryKeyIterator iterator_machine_key( |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 65 | HKEY_LOCAL_MACHINE, base::ASCIIToWide(kRegistryExtensions).c_str()); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 66 | for (; iterator_machine_key.Valid(); ++iterator_machine_key) |
| 67 | keys.insert(iterator_machine_key.Name()); |
| 68 | base::win::RegistryKeyIterator iterator_user_key( |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 69 | HKEY_CURRENT_USER, base::ASCIIToWide(kRegistryExtensions).c_str()); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 70 | for (; iterator_user_key.Valid(); ++iterator_user_key) |
| 71 | keys.insert(iterator_user_key.Name()); |
| 72 | |
| 73 | // Iterate over the keys found, first trying HKLM, then HKCU, as per Windows |
| 74 | // policy conventions. We only fall back to HKCU if the HKLM key cannot be |
| 75 | // opened, not if the data within the key is invalid, for example. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 76 | for (std::set<base::string16>::const_iterator it = keys.begin(); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 77 | it != keys.end(); ++it) { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 78 | base::win::RegKey key; |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 79 | base::string16 key_path = base::ASCIIToWide(kRegistryExtensions); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 80 | key_path.append(L"\\"); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 81 | key_path.append(*it); |
| 82 | if (key.Open(HKEY_LOCAL_MACHINE, |
| 83 | key_path.c_str(), KEY_READ) != ERROR_SUCCESS) { |
| 84 | if (key.Open(HKEY_CURRENT_USER, |
| 85 | key_path.c_str(), KEY_READ) != ERROR_SUCCESS) { |
| 86 | LOG(ERROR) << "Unable to read registry key at path (HKLM & HKCU): " |
| 87 | << key_path << "."; |
| 88 | continue; |
| 89 | } |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 90 | } |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 91 | |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 92 | std::string id = base::UTF16ToASCII(*it); |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 93 | StringToLowerASCII(&id); |
| 94 | if (!Extension::IdIsValid(id)) { |
| 95 | LOG(ERROR) << "Invalid id value " << id |
| 96 | << " for key " << key_path << "."; |
| 97 | continue; |
| 98 | } |
| 99 | |
[email protected] | d8fd0fd | 2014-03-24 13:16:06 | [diff] [blame] | 100 | base::string16 extension_dist_id; |
| 101 | if (key.ReadValue(kRegistryExtensionInstallParam, &extension_dist_id) == |
| 102 | ERROR_SUCCESS) { |
| 103 | prefs->SetString(id + "." + ExternalProviderImpl::kInstallParam, |
| 104 | base::UTF16ToASCII(extension_dist_id)); |
| 105 | } |
| 106 | |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 107 | // If there is an update URL present, copy it to prefs and ignore |
| 108 | // path and version keys for this entry. |
| 109 | base::string16 extension_update_url; |
| 110 | if (key.ReadValue(kRegistryExtensionUpdateUrl, &extension_update_url) |
| 111 | == ERROR_SUCCESS) { |
| 112 | prefs->SetString( |
| 113 | id + "." + ExternalProviderImpl::kExternalUpdateUrl, |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 114 | base::UTF16ToASCII(extension_update_url)); |
[email protected] | 5873a5a | 2013-12-16 19:53:00 | [diff] [blame] | 115 | continue; |
| 116 | } |
| 117 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 118 | base::string16 extension_path_str; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 119 | if (key.ReadValue(kRegistryExtensionPath, &extension_path_str) |
| 120 | != ERROR_SUCCESS) { |
| 121 | // TODO(erikkay): find a way to get this into about:extensions |
| 122 | LOG(ERROR) << "Missing value " << kRegistryExtensionPath |
| 123 | << " for key " << key_path << "."; |
| 124 | continue; |
| 125 | } |
| 126 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 127 | base::FilePath extension_path(extension_path_str); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 128 | if (!extension_path.IsAbsolute()) { |
| 129 | LOG(ERROR) << "File path " << extension_path_str |
| 130 | << " needs to be absolute in key " |
| 131 | << key_path; |
| 132 | continue; |
| 133 | } |
| 134 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 135 | if (!base::PathExists(extension_path)) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 136 | LOG(ERROR) << "File " << extension_path_str |
| 137 | << " for key " << key_path |
| 138 | << " does not exist or is not readable."; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | if (!CanOpenFileForReading(extension_path)) { |
| 143 | LOG(ERROR) << "File " << extension_path_str |
| 144 | << " for key " << key_path << " can not be read. " |
| 145 | << "Check that users who should have the extension " |
| 146 | << "installed have permission to read it."; |
| 147 | continue; |
| 148 | } |
| 149 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 150 | base::string16 extension_version; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 151 | if (key.ReadValue(kRegistryExtensionVersion, &extension_version) |
| 152 | != ERROR_SUCCESS) { |
| 153 | // TODO(erikkay): find a way to get this into about:extensions |
| 154 | LOG(ERROR) << "Missing value " << kRegistryExtensionVersion |
| 155 | << " for key " << key_path << "."; |
| 156 | continue; |
| 157 | } |
| 158 | |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 159 | Version version(base::UTF16ToASCII(extension_version)); |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 160 | if (!version.IsValid()) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 161 | LOG(ERROR) << "Invalid version value " << extension_version |
| 162 | << " for key " << key_path << "."; |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | prefs->SetString( |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 167 | id + "." + ExternalProviderImpl::kExternalVersion, |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 168 | base::UTF16ToASCII(extension_version)); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 169 | prefs->SetString( |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 170 | id + "." + ExternalProviderImpl::kExternalCrx, |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 171 | extension_path_str); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | prefs_.reset(prefs.release()); |
[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 175 | HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWin", |
| 176 | base::TimeTicks::Now() - start_time); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 177 | BrowserThread::PostTask( |
| 178 | BrowserThread::UI, FROM_HERE, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 179 | base::Bind(&ExternalRegistryLoader::LoadFinished, this)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 180 | } |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 181 | |
| 182 | } // namespace extensions |