[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] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 10 | #include "base/memory/scoped_handle.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 | |
| 29 | // Registry value of of that key that defines the path to the .crx file. |
| 30 | const wchar_t kRegistryExtensionPath[] = L"path"; |
| 31 | |
| 32 | // Registry value of that key that defines the current version of the .crx file. |
| 33 | const wchar_t kRegistryExtensionVersion[] = L"version"; |
| 34 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 35 | bool CanOpenFileForReading(const base::FilePath& path) { |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 36 | ScopedStdioHandle file_handle(base::OpenFile(path, "rb")); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 37 | return file_handle.get() != NULL; |
| 38 | } |
| 39 | |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 40 | } // namespace |
| 41 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 42 | namespace extensions { |
| 43 | |
| 44 | void ExternalRegistryLoader::StartLoading() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 45 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 46 | BrowserThread::PostTask( |
| 47 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 48 | base::Bind(&ExternalRegistryLoader::LoadOnFileThread, this)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 51 | void ExternalRegistryLoader::LoadOnFileThread() { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 52 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 53 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 54 | scoped_ptr<DictionaryValue> prefs(new DictionaryValue); |
| 55 | |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 56 | // A map of IDs, to weed out duplicates between HKCU and HKLM. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame^] | 57 | std::set<base::string16> keys; |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 58 | base::win::RegistryKeyIterator iterator_machine_key( |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 59 | HKEY_LOCAL_MACHINE, ASCIIToWide(kRegistryExtensions).c_str()); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 60 | for (; iterator_machine_key.Valid(); ++iterator_machine_key) |
| 61 | keys.insert(iterator_machine_key.Name()); |
| 62 | base::win::RegistryKeyIterator iterator_user_key( |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 63 | HKEY_CURRENT_USER, ASCIIToWide(kRegistryExtensions).c_str()); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 64 | for (; iterator_user_key.Valid(); ++iterator_user_key) |
| 65 | keys.insert(iterator_user_key.Name()); |
| 66 | |
| 67 | // Iterate over the keys found, first trying HKLM, then HKCU, as per Windows |
| 68 | // policy conventions. We only fall back to HKCU if the HKLM key cannot be |
| 69 | // opened, not if the data within the key is invalid, for example. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame^] | 70 | for (std::set<base::string16>::const_iterator it = keys.begin(); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 71 | it != keys.end(); ++it) { |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 72 | base::win::RegKey key; |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 73 | base::string16 key_path = ASCIIToWide(kRegistryExtensions); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 74 | key_path.append(L"\\"); |
[email protected] | 1358b28 | 2011-09-15 15:35:14 | [diff] [blame] | 75 | key_path.append(*it); |
| 76 | if (key.Open(HKEY_LOCAL_MACHINE, |
| 77 | key_path.c_str(), KEY_READ) != ERROR_SUCCESS) { |
| 78 | if (key.Open(HKEY_CURRENT_USER, |
| 79 | key_path.c_str(), KEY_READ) != ERROR_SUCCESS) { |
| 80 | LOG(ERROR) << "Unable to read registry key at path (HKLM & HKCU): " |
| 81 | << key_path << "."; |
| 82 | continue; |
| 83 | } |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 84 | } |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 85 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 86 | base::string16 extension_path_str; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 87 | if (key.ReadValue(kRegistryExtensionPath, &extension_path_str) |
| 88 | != ERROR_SUCCESS) { |
| 89 | // TODO(erikkay): find a way to get this into about:extensions |
| 90 | LOG(ERROR) << "Missing value " << kRegistryExtensionPath |
| 91 | << " for key " << key_path << "."; |
| 92 | continue; |
| 93 | } |
| 94 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 95 | base::FilePath extension_path(extension_path_str); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 96 | if (!extension_path.IsAbsolute()) { |
| 97 | LOG(ERROR) << "File path " << extension_path_str |
| 98 | << " needs to be absolute in key " |
| 99 | << key_path; |
| 100 | continue; |
| 101 | } |
| 102 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 103 | if (!base::PathExists(extension_path)) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 104 | LOG(ERROR) << "File " << extension_path_str |
| 105 | << " for key " << key_path |
| 106 | << " does not exist or is not readable."; |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | if (!CanOpenFileForReading(extension_path)) { |
| 111 | LOG(ERROR) << "File " << extension_path_str |
| 112 | << " for key " << key_path << " can not be read. " |
| 113 | << "Check that users who should have the extension " |
| 114 | << "installed have permission to read it."; |
| 115 | continue; |
| 116 | } |
| 117 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 118 | base::string16 extension_version; |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 119 | if (key.ReadValue(kRegistryExtensionVersion, &extension_version) |
| 120 | != ERROR_SUCCESS) { |
| 121 | // TODO(erikkay): find a way to get this into about:extensions |
| 122 | LOG(ERROR) << "Missing value " << kRegistryExtensionVersion |
| 123 | << " for key " << key_path << "."; |
| 124 | continue; |
| 125 | } |
| 126 | |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 127 | std::string id = WideToASCII(*it); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 128 | StringToLowerASCII(&id); |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 129 | if (!Extension::IdIsValid(id)) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 130 | LOG(ERROR) << "Invalid id value " << id |
| 131 | << " for key " << key_path << "."; |
| 132 | continue; |
| 133 | } |
| 134 | |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 135 | Version version(WideToASCII(extension_version)); |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 136 | if (!version.IsValid()) { |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 137 | LOG(ERROR) << "Invalid version value " << extension_version |
| 138 | << " for key " << key_path << "."; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | prefs->SetString( |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 143 | id + "." + ExternalProviderImpl::kExternalVersion, |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 144 | WideToASCII(extension_version)); |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 145 | prefs->SetString( |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 146 | id + "." + ExternalProviderImpl::kExternalCrx, |
[email protected] | b967a1b | 2011-08-17 14:39:58 | [diff] [blame] | 147 | extension_path_str); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | prefs_.reset(prefs.release()); |
[email protected] | f4091a3 | 2012-06-05 22:21:57 | [diff] [blame] | 151 | HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWin", |
| 152 | base::TimeTicks::Now() - start_time); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 153 | BrowserThread::PostTask( |
| 154 | BrowserThread::UI, FROM_HERE, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 155 | base::Bind(&ExternalRegistryLoader::LoadFinished, this)); |
[email protected] | 8e4560b6 | 2011-01-14 10:09:14 | [diff] [blame] | 156 | } |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 157 | |
| 158 | } // namespace extensions |