forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_inspector_win.cc
83 lines (66 loc) · 2.77 KB
/
module_inspector_win.cc
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/conflicts/module_inspector_win.h"
#include <utility>
#include "base/bind.h"
#include "base/task_scheduler/post_task.h"
namespace {
StringMapping GetPathMapping() {
return GetEnvironmentVariablesMapping({
L"LOCALAPPDATA", L"ProgramFiles", L"ProgramData", L"USERPROFILE",
L"SystemRoot", L"TEMP", L"TMP", L"CommonProgramFiles",
});
}
// Wrapper function for InspectModule() that takes the StringMapping via a
// scoped_refptr. This saves a copy per invocation.
std::unique_ptr<ModuleInspectionResult> InspectModuleOnBlockingSequence(
scoped_refptr<base::RefCountedData<StringMapping>> env_variable_mapping,
const ModuleInfoKey& module_key) {
return InspectModule(env_variable_mapping->data, module_key);
}
} // namespace
ModuleInspector::ModuleInspector(
const OnModuleInspectedCallback& on_module_inspected_callback)
: on_module_inspected_callback_(on_module_inspected_callback),
inspection_task_priority_(base::TaskPriority::BACKGROUND),
path_mapping_(base::MakeRefCounted<base::RefCountedData<StringMapping>>(
GetPathMapping())),
weak_ptr_factory_(this) {}
ModuleInspector::~ModuleInspector() = default;
void ModuleInspector::AddModule(const ModuleInfoKey& module_key) {
DCHECK(thread_checker_.CalledOnValidThread());
queue_.push(module_key);
if (queue_.size() == 1)
StartInspectingModule();
}
void ModuleInspector::IncreaseInspectionPriority() {
DCHECK(thread_checker_.CalledOnValidThread());
// Modify the TaskPriority so that future inspections are done faster.
inspection_task_priority_ = base::TaskPriority::USER_VISIBLE;
}
bool ModuleInspector::IsIdle() {
return queue_.empty();
}
void ModuleInspector::StartInspectingModule() {
ModuleInfoKey module_key = queue_.front();
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), inspection_task_priority_,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&InspectModuleOnBlockingSequence, path_mapping_,
module_key),
base::BindOnce(&ModuleInspector::OnInspectionFinished,
weak_ptr_factory_.GetWeakPtr(), module_key));
}
void ModuleInspector::OnInspectionFinished(
const ModuleInfoKey& module_key,
std::unique_ptr<ModuleInspectionResult> inspection_result) {
// Pop first, because the callback may want to know if there is any work left
// to be done, which is caracterized by a non-empty queue.
queue_.pop();
on_module_inspected_callback_.Run(module_key, std::move(inspection_result));
// Continue the work.
if (!queue_.empty())
StartInspectingModule();
}