File: external_registry_loader_win.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (287 lines) | stat: -rw-r--r-- 10,653 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/external_registry_loader_win.h"

#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version.h"
#include "base/win/registry.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace {

// The Registry subkey that contains information about external extensions.
const wchar_t kRegistryExtensions[] = L"Software\\Google\\Chrome\\Extensions";

// Registry value of the key that defines the installation parameter.
const wchar_t kRegistryExtensionInstallParam[] = L"install_parameter";

// Registry value of the key that defines the path to the .crx file.
const wchar_t kRegistryExtensionPath[] = L"path";

// Registry value of that key that defines the current version of the .crx file.
const wchar_t kRegistryExtensionVersion[] = L"version";

// Registry value of the key that defines an external update URL.
const wchar_t kRegistryExtensionUpdateUrl[] = L"update_url";

bool CanOpenFileForReading(const base::FilePath& path) {
  // Note: Because this ScopedFILE is used on the stack and not passed around
  // threads/sequences, this method doesn't require callers to run on tasks with
  // BLOCK_SHUTDOWN. SKIP_ON_SHUTDOWN is enough and safe because it guarantees
  // that if a task starts, it will always finish, and will block shutdown at
  // that point.
  base::ScopedFILE file_handle(base::OpenFile(path, "rb"));
  return file_handle.get() != NULL;
}

std::string MakePrefName(const std::string& extension_id,
                         const std::string& pref_name) {
  return base::StringPrintf("%s.%s", extension_id.c_str(), pref_name.c_str());
}

}  // namespace

namespace extensions {

ExternalRegistryLoader::ExternalRegistryLoader()
    : attempted_watching_registry_(false) {}

ExternalRegistryLoader::~ExternalRegistryLoader() = default;

void ExternalRegistryLoader::StartLoading() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  GetOrCreateTaskRunner()->PostTask(
      FROM_HERE,
      base::BindOnce(&ExternalRegistryLoader::LoadOnBlockingThread, this));
}

base::Value::Dict ExternalRegistryLoader::LoadPrefsOnBlockingThread() {
  base::Value::Dict prefs;

  // A map of IDs, to weed out duplicates between HKCU and HKLM.
  std::set<std::wstring> keys;
  base::win::RegistryKeyIterator iterator_machine_key(
      HKEY_LOCAL_MACHINE,
      kRegistryExtensions,
      KEY_WOW64_32KEY);
  for (; iterator_machine_key.Valid(); ++iterator_machine_key)
    keys.insert(iterator_machine_key.Name());
  base::win::RegistryKeyIterator iterator_user_key(
      HKEY_CURRENT_USER, kRegistryExtensions);
  for (; iterator_user_key.Valid(); ++iterator_user_key)
    keys.insert(iterator_user_key.Name());

  // Iterate over the keys found, first trying HKLM, then HKCU, as per Windows
  // policy conventions. We only fall back to HKCU if the HKLM key cannot be
  // opened, not if the data within the key is invalid, for example.
  for (auto it = keys.begin(); it != keys.end(); ++it) {
    base::win::RegKey key;
    std::wstring key_path = kRegistryExtensions;
    key_path.append(L"\\");
    key_path.append(*it);
    if (key.Open(HKEY_LOCAL_MACHINE,
                 key_path.c_str(),
                 KEY_READ | KEY_WOW64_32KEY) != ERROR_SUCCESS &&
        key.Open(HKEY_CURRENT_USER, key_path.c_str(), KEY_READ) !=
            ERROR_SUCCESS) {
      LOG(ERROR) << "Unable to read registry key at path (HKLM & HKCU): "
                 << key_path << ".";
      continue;
    }

    std::string id = base::ToLowerASCII(base::WideToASCII(*it));
    if (!crx_file::id_util::IdIsValid(id)) {
      LOG(ERROR) << "Invalid id value " << id
                 << " for key " << key_path << ".";
      continue;
    }

    std::wstring extension_dist_id;
    if (key.ReadValue(kRegistryExtensionInstallParam, &extension_dist_id) ==
        ERROR_SUCCESS) {
      prefs.SetByDottedPath(
          MakePrefName(id, ExternalProviderImpl::kInstallParam),
          base::WideToASCII(extension_dist_id));
    }

    // If there is an update URL present, copy it to prefs and ignore
    // path and version keys for this entry.
    std::wstring extension_update_url;
    if (key.ReadValue(kRegistryExtensionUpdateUrl, &extension_update_url)
        == ERROR_SUCCESS) {
      prefs.SetByDottedPath(
          MakePrefName(id, ExternalProviderImpl::kExternalUpdateUrl),
          base::WideToASCII(extension_update_url));
      continue;
    }

    std::wstring extension_path_str;
    if (key.ReadValue(kRegistryExtensionPath, &extension_path_str)
        != ERROR_SUCCESS) {
      // TODO(erikkay): find a way to get this into about:extensions
      LOG(ERROR) << "Missing value " << kRegistryExtensionPath
                 << " for key " << key_path << ".";
      continue;
    }

    base::FilePath extension_path(extension_path_str);
    if (!extension_path.IsAbsolute()) {
      LOG(ERROR) << "File path " << extension_path_str
                 << " needs to be absolute in key "
                 << key_path;
      continue;
    }

    if (!base::PathExists(extension_path)) {
      LOG(ERROR) << "File " << extension_path_str
                 << " for key " << key_path
                 << " does not exist or is not readable.";
      continue;
    }

    if (!CanOpenFileForReading(extension_path)) {
      LOG(ERROR) << "File " << extension_path_str
                 << " for key " << key_path << " can not be read. "
                 << "Check that users who should have the extension "
                 << "installed have permission to read it.";
      continue;
    }

    std::wstring extension_version;
    if (key.ReadValue(kRegistryExtensionVersion, &extension_version)
        != ERROR_SUCCESS) {
      // TODO(erikkay): find a way to get this into about:extensions
      LOG(ERROR) << "Missing value " << kRegistryExtensionVersion
                 << " for key " << key_path << ".";
      continue;
    }

    base::Version version(base::WideToASCII(extension_version));
    if (!version.IsValid()) {
      LOG(ERROR) << "Invalid version value " << extension_version
                 << " for key " << key_path << ".";
      continue;
    }

    prefs.SetByDottedPath(
        MakePrefName(id, ExternalProviderImpl::kExternalVersion),
        base::WideToASCII(extension_version));
    prefs.SetByDottedPath(MakePrefName(id, ExternalProviderImpl::kExternalCrx),
                          base::AsString16(extension_path_str));
    prefs.SetByDottedPath(
        MakePrefName(id, ExternalProviderImpl::kMayBeUntrusted), true);
  }

  return prefs;
}

void ExternalRegistryLoader::LoadOnBlockingThread() {
  DCHECK(task_runner_);
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::TimeTicks start_time = base::TimeTicks::Now();
  base::Value::Dict prefs = LoadPrefsOnBlockingThread();
  LOCAL_HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWin",
                        base::TimeTicks::Now() - start_time);
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(
          &ExternalRegistryLoader::CompleteLoadAndStartWatchingRegistry, this,
          std::move(prefs)));
}

void ExternalRegistryLoader::CompleteLoadAndStartWatchingRegistry(
    base::Value::Dict prefs) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  LoadFinished(std::move(prefs));

  // Attempt to watch registry if we haven't already.
  if (attempted_watching_registry_)
    return;

  LONG result = ERROR_SUCCESS;
  if ((result = hklm_key_.Create(HKEY_LOCAL_MACHINE, kRegistryExtensions,
                                 KEY_NOTIFY | KEY_WOW64_32KEY)) ==
      ERROR_SUCCESS) {
    base::win::RegKey::ChangeCallback callback =
        base::BindOnce(&ExternalRegistryLoader::OnRegistryKeyChanged,
                       base::Unretained(this), base::Unretained(&hklm_key_));
    hklm_key_.StartWatching(std::move(callback));
  } else {
    LOG(WARNING) << "Error observing HKLM: " << result;
  }

  if ((result = hkcu_key_.Create(HKEY_CURRENT_USER, kRegistryExtensions,
                                 KEY_NOTIFY)) == ERROR_SUCCESS) {
    base::win::RegKey::ChangeCallback callback =
        base::BindOnce(&ExternalRegistryLoader::OnRegistryKeyChanged,
                       base::Unretained(this), base::Unretained(&hkcu_key_));
    hkcu_key_.StartWatching(std::move(callback));
  } else {
    LOG(WARNING) << "Error observing HKCU: " << result;
  }

  attempted_watching_registry_ = true;
}

void ExternalRegistryLoader::OnRegistryKeyChanged(base::win::RegKey* key) {
  // |OnRegistryKeyChanged| is removed as an observer when the ChangeCallback is
  // called, so we need to re-register.
  key->StartWatching(
      base::BindOnce(&ExternalRegistryLoader::OnRegistryKeyChanged,
                     base::Unretained(this), base::Unretained(key)));

  GetOrCreateTaskRunner()->PostTask(
      FROM_HERE,
      base::BindOnce(&ExternalRegistryLoader::UpatePrefsOnBlockingThread,
                     this));
}

scoped_refptr<base::SequencedTaskRunner>
ExternalRegistryLoader::GetOrCreateTaskRunner() {
  if (!task_runner_.get()) {
    task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
        {// Requires I/O for registry.
         base::MayBlock(),

         // Inherit priority.

         base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
  }
  return task_runner_;
}

void ExternalRegistryLoader::UpatePrefsOnBlockingThread() {
  DCHECK(task_runner_);
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::TimeTicks start_time = base::TimeTicks::Now();
  base::Value::Dict prefs = LoadPrefsOnBlockingThread();
  LOCAL_HISTOGRAM_TIMES("Extensions.ExternalRegistryLoaderWinUpdate",
                        base::TimeTicks::Now() - start_time);
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(&ExternalRegistryLoader::OnUpdated, this,
                                std::move(prefs)));
}

}  // namespace extensions