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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright 2017 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/win/conflicts/module_database.h"
#include <tuple>
#include <utility>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/lazy_thread_pool_task_runner.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/win/conflicts/module_database_observer.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
namespace {
ModuleDatabase* g_module_database = nullptr;
} // namespace
// static
constexpr base::TimeDelta ModuleDatabase::kIdleTimeout;
ModuleDatabase::ModuleDatabase()
: idle_timer_(FROM_HERE,
kIdleTimeout,
base::BindRepeating(&ModuleDatabase::OnDelayExpired,
base::Unretained(this))),
has_started_processing_(false),
shell_extensions_enumerated_(false),
ime_enumerated_(false),
// ModuleDatabase owns |module_inspector_|, so it is safe to use
// base::Unretained().
module_inspector_(base::BindRepeating(&ModuleDatabase::OnModuleInspected,
base::Unretained(this))) {
AddObserver(&module_inspector_);
AddObserver(&third_party_metrics_);
}
ModuleDatabase::~ModuleDatabase() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (this == g_module_database)
g_module_database = nullptr;
}
// static
scoped_refptr<base::SequencedTaskRunner> ModuleDatabase::GetTaskRunner() {
static base::LazyThreadPoolSequencedTaskRunner g_module_database_task_runner =
LAZY_THREAD_POOL_SEQUENCED_TASK_RUNNER_INITIALIZER(
base::TaskTraits(base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN));
return g_module_database_task_runner.Get();
}
// static
ModuleDatabase* ModuleDatabase::GetInstance() {
DCHECK(GetTaskRunner()->RunsTasksInCurrentSequence());
return g_module_database;
}
// static
void ModuleDatabase::SetInstance(
std::unique_ptr<ModuleDatabase> module_database) {
DCHECK_EQ(nullptr, g_module_database);
// This is deliberately leaked. It can be cleaned up by manually deleting the
// ModuleDatabase.
g_module_database = module_database.release();
}
bool ModuleDatabase::IsIdle() {
return has_started_processing_ && RegisteredModulesEnumerated() &&
!idle_timer_.IsRunning() && module_inspector_.IsIdle();
}
void ModuleDatabase::OnShellExtensionEnumerated(const base::FilePath& path,
uint32_t size_of_image,
uint32_t time_date_stamp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
idle_timer_.Reset();
ModuleInfo* module_info = nullptr;
FindOrCreateModuleInfo(path, size_of_image, time_date_stamp, &module_info);
module_info->second.module_properties |=
ModuleInfoData::kPropertyShellExtension;
}
void ModuleDatabase::OnShellExtensionEnumerationFinished() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!shell_extensions_enumerated_);
shell_extensions_enumerated_ = true;
if (RegisteredModulesEnumerated())
OnRegisteredModulesEnumerated();
}
void ModuleDatabase::OnImeEnumerated(const base::FilePath& path,
uint32_t size_of_image,
uint32_t time_date_stamp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
idle_timer_.Reset();
ModuleInfo* module_info = nullptr;
FindOrCreateModuleInfo(path, size_of_image, time_date_stamp, &module_info);
module_info->second.module_properties |= ModuleInfoData::kPropertyIme;
}
void ModuleDatabase::OnImeEnumerationFinished() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!ime_enumerated_);
ime_enumerated_ = true;
if (RegisteredModulesEnumerated())
OnRegisteredModulesEnumerated();
}
void ModuleDatabase::OnModuleLoad(content::ProcessType process_type,
const base::FilePath& module_path,
uint32_t module_size,
uint32_t module_time_date_stamp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(process_type == content::PROCESS_TYPE_BROWSER ||
process_type == content::PROCESS_TYPE_RENDERER)
<< "The current logic in ModuleBlocklistCacheUpdater does not support "
"other process types yet. See https://crbug.com/662084 for details.";
ModuleInfo* module_info = nullptr;
bool new_module = FindOrCreateModuleInfo(
module_path, module_size, module_time_date_stamp, &module_info);
uint32_t old_module_properties = module_info->second.module_properties;
// Mark the module as loaded.
module_info->second.module_properties |=
ModuleInfoData::kPropertyLoadedModule;
// Update the list of process types that this module has been seen in.
module_info->second.process_types |= ProcessTypeToBit(process_type);
// Some observers care about a known module that is just now loading. Also
// making sure that the module is ready to be sent to observers.
bool is_known_module_loading =
!new_module &&
old_module_properties != module_info->second.module_properties;
bool ready_for_notification =
module_info->second.inspection_result && RegisteredModulesEnumerated();
if (is_known_module_loading && ready_for_notification) {
for (auto& observer : observer_list_) {
observer.OnKnownModuleLoaded(module_info->first, module_info->second);
}
}
}
// static
void ModuleDatabase::HandleModuleLoadEvent(content::ProcessType process_type,
const base::FilePath& module_path,
uint32_t module_size,
uint32_t module_time_date_stamp) {
GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(
[](content::ProcessType process_type,
const base::FilePath& module_path, uint32_t module_size,
uint32_t module_time_date_stamp) {
ModuleDatabase::GetInstance()->OnModuleLoad(
process_type, module_path, module_size, module_time_date_stamp);
},
process_type, module_path, module_size, module_time_date_stamp));
}
void ModuleDatabase::OnModuleBlocked(const base::FilePath& module_path,
uint32_t module_size,
uint32_t module_time_date_stamp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ModuleInfo* module_info = nullptr;
FindOrCreateModuleInfo(module_path, module_size, module_time_date_stamp,
&module_info);
module_info->second.module_properties |= ModuleInfoData::kPropertyBlocked;
}
void ModuleDatabase::OnModuleAddedToBlocklist(const base::FilePath& module_path,
uint32_t module_size,
uint32_t module_time_date_stamp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto iter = modules_.find(
ModuleInfoKey(module_path, module_size, module_time_date_stamp));
// Only known modules should be added to the blocklist.
CHECK(iter != modules_.end());
iter->second.module_properties |= ModuleInfoData::kPropertyAddedToBlocklist;
}
void ModuleDatabase::AddObserver(ModuleDatabaseObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer_list_.AddObserver(observer);
// If the registered modules enumeration is not finished yet, the |observer|
// will be notified later in OnRegisteredModulesEnumerated().
if (!RegisteredModulesEnumerated())
return;
NotifyLoadedModules(observer);
if (IsIdle())
observer->OnModuleDatabaseIdle();
}
void ModuleDatabase::RemoveObserver(ModuleDatabaseObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer_list_.RemoveObserver(observer);
}
void ModuleDatabase::StartInspection() {
module_inspector_.StartInspection();
}
bool ModuleDatabase::FindOrCreateModuleInfo(
const base::FilePath& module_path,
uint32_t module_size,
uint32_t module_time_date_stamp,
ModuleDatabase::ModuleInfo** module_info) {
auto result = modules_.emplace(
std::piecewise_construct,
std::forward_as_tuple(module_path, module_size, module_time_date_stamp),
std::forward_as_tuple());
// New modules must be inspected.
bool new_module = result.second;
if (new_module) {
has_started_processing_ = true;
idle_timer_.Reset();
module_inspector_.AddModule(result.first->first);
}
*module_info = &(*result.first);
return new_module;
}
bool ModuleDatabase::RegisteredModulesEnumerated() {
return shell_extensions_enumerated_ && ime_enumerated_;
}
void ModuleDatabase::OnRegisteredModulesEnumerated() {
for (auto& observer : observer_list_)
NotifyLoadedModules(&observer);
if (IsIdle())
EnterIdleState();
}
void ModuleDatabase::OnModuleInspected(
const ModuleInfoKey& module_key,
ModuleInspectionResult inspection_result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = modules_.find(module_key);
if (it == modules_.end())
return;
it->second.inspection_result = std::move(inspection_result);
if (RegisteredModulesEnumerated()) {
for (auto& observer : observer_list_) {
observer.OnNewModuleFound(it->first, it->second);
}
}
// Notify the observers if this was the last outstanding module inspection and
// the delay has already expired.
if (IsIdle())
EnterIdleState();
}
void ModuleDatabase::OnDelayExpired() {
// Notify the observers if there are no outstanding module inspections.
if (IsIdle())
EnterIdleState();
}
void ModuleDatabase::EnterIdleState() {
for (auto& observer : observer_list_)
observer.OnModuleDatabaseIdle();
}
void ModuleDatabase::NotifyLoadedModules(ModuleDatabaseObserver* observer) {
for (const auto& module : modules_) {
if (module.second.inspection_result)
observer->OnNewModuleFound(module.first, module.second);
}
}
|