File: conflicts_data_fetcher.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (129 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 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/ui/webui/conflicts/conflicts_data_fetcher.h"

#include <string>
#include <string_view>
#include <utility>

#include "base/strings/string_util.h"
#include "chrome/browser/win/conflicts/module_database.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

namespace {

// Converts the process_types bit field to a simple string representation where
// each process type is represented by a letter. E.g. B for browser process.
// Full process names are not used in order to save horizontal space in the
// conflicts UI.
std::string GetProcessTypesString(const ModuleInfoData& module_data) {
  uint32_t process_types = module_data.process_types;

  if (!process_types) {
    return "None";
  }

  std::string result;
  if (process_types & ProcessTypeToBit(content::PROCESS_TYPE_BROWSER)) {
    result.append("B");
  }
  if (process_types & ProcessTypeToBit(content::PROCESS_TYPE_RENDERER)) {
    result.append("R");
  }
  // TODO(pmonette): Add additional process types as more get supported.

  return result;
}

}  // namespace

ConflictsDataFetcher::~ConflictsDataFetcher() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (module_list_.has_value()) {
    ModuleDatabase::GetInstance()->RemoveObserver(this);
  }
}

// static
ConflictsDataFetcher::UniquePtr ConflictsDataFetcher::Create(
    OnConflictsDataFetchedCallback on_conflicts_data_fetched_callback) {
  return std::unique_ptr<ConflictsDataFetcher, base::OnTaskRunnerDeleter>(
      new ConflictsDataFetcher(std::move(on_conflicts_data_fetched_callback)),
      base::OnTaskRunnerDeleter(ModuleDatabase::GetTaskRunner()));
}

ConflictsDataFetcher::ConflictsDataFetcher(
    OnConflictsDataFetchedCallback on_conflicts_data_fetched_callback)
    : on_conflicts_data_fetched_callback_(
          std::move(on_conflicts_data_fetched_callback)) {
  DETACH_FROM_SEQUENCE(sequence_checker_);

  ModuleDatabase::GetTaskRunner()->PostTask(
      FROM_HERE,
      base::BindOnce(
          &ConflictsDataFetcher::InitializeOnModuleDatabaseTaskRunner,
          base::Unretained(this)));
}

void ConflictsDataFetcher::InitializeOnModuleDatabaseTaskRunner() {
  GetListOfModules();
}

void ConflictsDataFetcher::GetListOfModules() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // The request is handled asynchronously, filling up the |module_list_|,
  // and will callback via OnModuleDatabaseIdle() on completion.
  module_list_ = base::Value::List();

  auto* module_database = ModuleDatabase::GetInstance();
  module_database->StartInspection();
  module_database->AddObserver(this);
}

void ConflictsDataFetcher::OnNewModuleFound(const ModuleInfoKey& module_key,
                                            const ModuleInfoData& module_data) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(module_list_);

  base::Value::Dict data;

  std::string type_string;
  if (module_data.module_properties & ModuleInfoData::kPropertyShellExtension) {
    type_string = "Shell extension";
  }
  data.Set("type_description", type_string);

  const auto& inspection_result = *module_data.inspection_result;
  data.Set("location", inspection_result.location);
  data.Set("name", inspection_result.basename);
  data.Set("product_name", inspection_result.product_name);
  data.Set("description", inspection_result.description);
  data.Set("version", inspection_result.version);
  data.Set("digital_signer", inspection_result.certificate_info.subject);
  data.Set("code_id", GenerateCodeId(module_key));
  data.Set("process_types", GetProcessTypesString(module_data));

  module_list_->Append(std::move(data));
}

void ConflictsDataFetcher::OnModuleDatabaseIdle() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(module_list_);

  ModuleDatabase::GetInstance()->RemoveObserver(this);

  base::Value::Dict results;
  results.Set("moduleCount", static_cast<int>(module_list_->size()));
  results.Set("moduleList", std::move(*module_list_));
  module_list_ = std::nullopt;

  // The third-party features are always disabled on Chromium builds.
  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(std::move(on_conflicts_data_fetched_callback_),
                                std::move(results)));
}