blob: 040869a9e6fcbf95d6989e7bf2f9652035d302d1 [file] [log] [blame]
[email protected]f4091a32012-06-05 22:21:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8e4560b62011-01-14 10:09:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5df038b2012-07-16 19:03:275#include "chrome/browser/extensions/external_registry_loader_win.h"
[email protected]8e4560b62011-01-14 10:09:146
[email protected]8e6ac4b2011-10-17 19:04:317#include "base/bind.h"
[email protected]b967a1b2011-08-17 14:39:588#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]b967a1b2011-08-17 14:39:5810#include "base/memory/scoped_handle.h"
[email protected]f4091a32012-06-05 22:21:5711#include "base/metrics/histogram.h"
[email protected]46acbf12013-06-10 18:43:4212#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1813#include "base/strings/utf_string_conversions.h"
[email protected]41a17c52013-06-28 00:27:5314#include "base/time/time.h"
[email protected]8e4560b62011-01-14 10:09:1415#include "base/values.h"
16#include "base/version.h"
17#include "base/win/registry.h"
[email protected]5df038b2012-07-16 19:03:2718#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]c38831a12011-10-28 12:44:4919#include "content/public/browser/browser_thread.h"
[email protected]e4452d32013-11-15 23:07:4120#include "extensions/common/extension.h"
[email protected]8e4560b62011-01-14 10:09:1421
[email protected]631bb742011-11-02 11:29:3922using content::BrowserThread;
23
[email protected]8e4560b62011-01-14 10:09:1424namespace {
25
[email protected]8e4560b62011-01-14 10:09:1426// The Registry subkey that contains information about external extensions.
27const char kRegistryExtensions[] = "Software\\Google\\Chrome\\Extensions";
28
[email protected]5873a5a2013-12-16 19:53:0029// Registry value of the key that defines the path to the .crx file.
[email protected]8e4560b62011-01-14 10:09:1430const wchar_t kRegistryExtensionPath[] = L"path";
31
32// Registry value of that key that defines the current version of the .crx file.
33const wchar_t kRegistryExtensionVersion[] = L"version";
34
[email protected]5873a5a2013-12-16 19:53:0035// Registry value of the key that defines an external update URL.
36const wchar_t kRegistryExtensionUpdateUrl[] = L"update_url";
37
[email protected]650b2d52013-02-10 03:41:4538bool CanOpenFileForReading(const base::FilePath& path) {
[email protected]7600d0b2013-12-08 21:43:3039 ScopedStdioHandle file_handle(base::OpenFile(path, "rb"));
[email protected]b967a1b2011-08-17 14:39:5840 return file_handle.get() != NULL;
41}
42
[email protected]8e4560b62011-01-14 10:09:1443} // namespace
44
[email protected]5df038b2012-07-16 19:03:2745namespace extensions {
46
47void ExternalRegistryLoader::StartLoading() {
[email protected]8e4560b62011-01-14 10:09:1448 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 BrowserThread::PostTask(
50 BrowserThread::FILE, FROM_HERE,
[email protected]5df038b2012-07-16 19:03:2751 base::Bind(&ExternalRegistryLoader::LoadOnFileThread, this));
[email protected]8e4560b62011-01-14 10:09:1452}
53
[email protected]5df038b2012-07-16 19:03:2754void ExternalRegistryLoader::LoadOnFileThread() {
[email protected]8e4560b62011-01-14 10:09:1455 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]f4091a32012-06-05 22:21:5756 base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]8e4560b62011-01-14 10:09:1457 scoped_ptr<DictionaryValue> prefs(new DictionaryValue);
58
[email protected]1358b282011-09-15 15:35:1459 // A map of IDs, to weed out duplicates between HKCU and HKLM.
[email protected]d2065e062013-12-12 23:49:5260 std::set<base::string16> keys;
[email protected]1358b282011-09-15 15:35:1461 base::win::RegistryKeyIterator iterator_machine_key(
[email protected]47e870b2013-02-24 21:14:5362 HKEY_LOCAL_MACHINE, ASCIIToWide(kRegistryExtensions).c_str());
[email protected]1358b282011-09-15 15:35:1463 for (; iterator_machine_key.Valid(); ++iterator_machine_key)
64 keys.insert(iterator_machine_key.Name());
65 base::win::RegistryKeyIterator iterator_user_key(
[email protected]47e870b2013-02-24 21:14:5366 HKEY_CURRENT_USER, ASCIIToWide(kRegistryExtensions).c_str());
[email protected]1358b282011-09-15 15:35:1467 for (; iterator_user_key.Valid(); ++iterator_user_key)
68 keys.insert(iterator_user_key.Name());
69
70 // Iterate over the keys found, first trying HKLM, then HKCU, as per Windows
71 // policy conventions. We only fall back to HKCU if the HKLM key cannot be
72 // opened, not if the data within the key is invalid, for example.
[email protected]d2065e062013-12-12 23:49:5273 for (std::set<base::string16>::const_iterator it = keys.begin();
[email protected]1358b282011-09-15 15:35:1474 it != keys.end(); ++it) {
[email protected]8e4560b62011-01-14 10:09:1475 base::win::RegKey key;
[email protected]439f1e32013-12-09 20:09:0976 base::string16 key_path = ASCIIToWide(kRegistryExtensions);
[email protected]8e4560b62011-01-14 10:09:1477 key_path.append(L"\\");
[email protected]1358b282011-09-15 15:35:1478 key_path.append(*it);
79 if (key.Open(HKEY_LOCAL_MACHINE,
80 key_path.c_str(), KEY_READ) != ERROR_SUCCESS) {
81 if (key.Open(HKEY_CURRENT_USER,
82 key_path.c_str(), KEY_READ) != ERROR_SUCCESS) {
83 LOG(ERROR) << "Unable to read registry key at path (HKLM & HKCU): "
84 << key_path << ".";
85 continue;
86 }
[email protected]8e4560b62011-01-14 10:09:1487 }
[email protected]b967a1b2011-08-17 14:39:5888
[email protected]5873a5a2013-12-16 19:53:0089 std::string id = WideToASCII(*it);
90 StringToLowerASCII(&id);
91 if (!Extension::IdIsValid(id)) {
92 LOG(ERROR) << "Invalid id value " << id
93 << " for key " << key_path << ".";
94 continue;
95 }
96
97 // If there is an update URL present, copy it to prefs and ignore
98 // path and version keys for this entry.
99 base::string16 extension_update_url;
100 if (key.ReadValue(kRegistryExtensionUpdateUrl, &extension_update_url)
101 == ERROR_SUCCESS) {
102 prefs->SetString(
103 id + "." + ExternalProviderImpl::kExternalUpdateUrl,
104 WideToASCII(extension_update_url));
105 continue;
106 }
107
[email protected]439f1e32013-12-09 20:09:09108 base::string16 extension_path_str;
[email protected]b967a1b2011-08-17 14:39:58109 if (key.ReadValue(kRegistryExtensionPath, &extension_path_str)
110 != ERROR_SUCCESS) {
111 // TODO(erikkay): find a way to get this into about:extensions
112 LOG(ERROR) << "Missing value " << kRegistryExtensionPath
113 << " for key " << key_path << ".";
114 continue;
115 }
116
[email protected]650b2d52013-02-10 03:41:45117 base::FilePath extension_path(extension_path_str);
[email protected]b967a1b2011-08-17 14:39:58118 if (!extension_path.IsAbsolute()) {
119 LOG(ERROR) << "File path " << extension_path_str
120 << " needs to be absolute in key "
121 << key_path;
122 continue;
123 }
124
[email protected]7567484142013-07-11 17:36:07125 if (!base::PathExists(extension_path)) {
[email protected]b967a1b2011-08-17 14:39:58126 LOG(ERROR) << "File " << extension_path_str
127 << " for key " << key_path
128 << " does not exist or is not readable.";
129 continue;
130 }
131
132 if (!CanOpenFileForReading(extension_path)) {
133 LOG(ERROR) << "File " << extension_path_str
134 << " for key " << key_path << " can not be read. "
135 << "Check that users who should have the extension "
136 << "installed have permission to read it.";
137 continue;
138 }
139
[email protected]439f1e32013-12-09 20:09:09140 base::string16 extension_version;
[email protected]b967a1b2011-08-17 14:39:58141 if (key.ReadValue(kRegistryExtensionVersion, &extension_version)
142 != ERROR_SUCCESS) {
143 // TODO(erikkay): find a way to get this into about:extensions
144 LOG(ERROR) << "Missing value " << kRegistryExtensionVersion
145 << " for key " << key_path << ".";
146 continue;
147 }
148
[email protected]47e870b2013-02-24 21:14:53149 Version version(WideToASCII(extension_version));
[email protected]12126d372012-07-11 18:40:53150 if (!version.IsValid()) {
[email protected]b967a1b2011-08-17 14:39:58151 LOG(ERROR) << "Invalid version value " << extension_version
152 << " for key " << key_path << ".";
153 continue;
154 }
155
156 prefs->SetString(
[email protected]5df038b2012-07-16 19:03:27157 id + "." + ExternalProviderImpl::kExternalVersion,
[email protected]47e870b2013-02-24 21:14:53158 WideToASCII(extension_version));
[email protected]b967a1b2011-08-17 14:39:58159 prefs->SetString(
[email protected]5df038b2012-07-16 19:03:27160 id + "." + ExternalProviderImpl::kExternalCrx,
[email protected]b967a1b2011-08-17 14:39:58161 extension_path_str);
[email protected]8e4560b62011-01-14 10:09:14162 }
163
164 prefs_.reset(prefs.release());
[email protected]f4091a32012-06-05 22:21:57165 HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWin",
166 base::TimeTicks::Now() - start_time);
[email protected]8e4560b62011-01-14 10:09:14167 BrowserThread::PostTask(
168 BrowserThread::UI, FROM_HERE,
[email protected]5df038b2012-07-16 19:03:27169 base::Bind(&ExternalRegistryLoader::LoadFinished, this));
[email protected]8e4560b62011-01-14 10:09:14170}
[email protected]5df038b2012-07-16 19:03:27171
172} // namespace extensions