Skip to content

Commit 24303f6

Browse files
authored
Support OnDemandUpdate with multiple components at once. (#23169)
* Support OnDemandUpdate with multiple components at once. * Review fixes. * Mark two missed components as Brave.
1 parent c745791 commit 24303f6

32 files changed

+362
-122
lines changed

browser/brave_browser_main_parts.cc

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/command_line.h"
1212
#include "brave/browser/browsing_data/brave_clear_browsing_data.h"
1313
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
14+
#include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h"
1415
#include "brave/components/brave_rewards/common/rewards_flags.h"
1516
#include "brave/components/brave_rewards/common/rewards_util.h"
1617
#include "brave/components/brave_sync/features.h"
@@ -20,8 +21,10 @@
2021
#include "brave/components/tor/buildflags/buildflags.h"
2122
#include "brave/components/translate/core/common/brave_translate_features.h"
2223
#include "build/build_config.h"
24+
#include "chrome/browser/browser_process.h"
2325
#include "chrome/browser/profiles/profile.h"
2426
#include "chrome/common/chrome_features.h"
27+
#include "components/component_updater/component_updater_service.h"
2528
#include "components/prefs/pref_service.h"
2629
#include "components/sync/base/command_line_switches.h"
2730
#include "components/translate/core/browser/translate_language_list.h"
@@ -60,16 +63,20 @@
6063
#include "brave/browser/android/preferences/features.h"
6164
#endif
6265

63-
#if BUILDFLAG(ENABLE_TOR) || !BUILDFLAG(IS_ANDROID)
64-
#include "chrome/browser/browser_process.h"
65-
#endif
66-
6766
#if BUILDFLAG(ETHEREUM_REMOTE_CLIENT_ENABLED) && BUILDFLAG(ENABLE_EXTENSIONS)
6867
#include "brave/browser/extensions/brave_component_loader.h"
6968
#include "chrome/browser/extensions/extension_service.h"
7069
#include "extensions/browser/extension_system.h"
7170
#endif
7271

72+
int BraveBrowserMainParts::PreMainMessageLoopRun() {
73+
brave_component_updater::BraveOnDemandUpdater::GetInstance()
74+
->RegisterOnDemandUpdater(
75+
&g_browser_process->component_updater()->GetOnDemandUpdater());
76+
77+
return ChromeBrowserMainParts::PreMainMessageLoopRun();
78+
}
79+
7380
void BraveBrowserMainParts::PreBrowserStart() {
7481
#if BUILDFLAG(ENABLE_SPEEDREADER)
7582
// Register() must be called after the SerializedNavigationDriver is

browser/brave_browser_main_parts.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class BraveBrowserMainParts : public ChromeBrowserMainParts {
1616
BraveBrowserMainParts& operator=(const BraveBrowserMainParts&) = delete;
1717
~BraveBrowserMainParts() override = default;
1818

19+
int PreMainMessageLoopRun() override;
1920
void PreBrowserStart() override;
2021
void PostBrowserStart() override;
2122
void PreShutdown() override;

browser/brave_browser_process_impl.cc

-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "brave/common/brave_channel_info.h"
2626
#include "brave/components/brave_ads/browser/component_updater/resource_component.h"
2727
#include "brave/components/brave_component_updater/browser/brave_component_updater_delegate.h"
28-
#include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h"
2928
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
3029
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
3130
#include "brave/components/brave_shields/content/browser/ad_block_service.h"
@@ -53,7 +52,6 @@
5352
#include "chrome/common/channel_info.h"
5453
#include "chrome/common/chrome_paths.h"
5554
#include "chrome/common/pref_names.h"
56-
#include "components/component_updater/component_updater_service.h"
5755
#include "components/component_updater/timer_update_scheduler.h"
5856
#include "content/public/browser/browser_thread.h"
5957
#include "content/public/browser/child_process_security_policy.h"
@@ -153,9 +151,6 @@ void BraveBrowserProcessImpl::Init() {
153151
content::ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
154152
ipfs::kIPNSScheme);
155153
#endif
156-
brave_component_updater::BraveOnDemandUpdater::GetInstance()
157-
->RegisterOnDemandUpdateCallback(
158-
base::BindRepeating(&component_updater::BraveOnDemandUpdate));
159154
UpdateBraveDarkMode();
160155
pref_change_registrar_.Add(
161156
kBraveDarkMode,

chromium_src/chrome/browser/component_updater/component_updater_utils.cc

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55

66
#include "src/chrome/browser/component_updater/component_updater_utils.cc"
77

8-
#include "base/functional/callback.h"
9-
#include "chrome/browser/browser_process.h"
10-
#include "components/component_updater/component_updater_service.h"
8+
#include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h"
119

1210
namespace component_updater {
1311

1412
void BraveOnDemandUpdate(const std::string& component_id) {
15-
component_updater::ComponentUpdateService* cus =
16-
g_browser_process->component_updater();
17-
cus->GetOnDemandUpdater().OnDemandUpdate(
18-
component_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
19-
component_updater::Callback());
13+
brave_component_updater::BraveOnDemandUpdater::GetInstance()->OnDemandUpdate(
14+
component_id);
2015
}
2116

2217
} // namespace component_updater

chromium_src/components/component_updater/component_installer.cc

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
namespace component_updater {
1717

18+
bool ComponentInstallerPolicy::IsBraveComponent() const {
19+
return false;
20+
}
21+
1822
void ComponentInstaller::Register(ComponentUpdateService* cus,
1923
base::OnceClosure callback) {
2024
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
@@ -59,4 +63,8 @@ void ComponentInstaller::Register(
5963
registered_version, max_previous_product_version);
6064
}
6165

66+
bool ComponentInstaller::IsBraveComponent() const {
67+
return installer_policy_->IsBraveComponent();
68+
}
69+
6270
} // namespace component_updater

chromium_src/components/component_updater/component_installer.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
77
#define BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
88

9-
// Prevent CrxInstaller::OnUpdateError from being redefined by the below
10-
// #define.
119
#include "components/update_client/update_client.h"
1210

1311
// We can't redefine Register() here because that would change two methods at
@@ -22,10 +20,16 @@
2220
const base::Version& registered_version = base::Version(kNullVersion), \
2321
const base::Version& max_previous_product_version = \
2422
base::Version(kNullVersion)); \
23+
bool IsBraveComponent() const override; \
2524
void OnUpdateError
2625

26+
#define AllowUpdatesOnMeteredConnections \
27+
IsBraveComponent() const; \
28+
virtual bool AllowUpdatesOnMeteredConnections
29+
2730
#include "src/components/component_updater/component_installer.h" // IWYU pragma: export
2831

2932
#undef OnUpdateError
33+
#undef AllowUpdatesOnMeteredConnections
3034

3135
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
6+
#include "components/component_updater/component_updater_service.h"
7+
8+
#include "base/notreached.h"
9+
#include "components/component_updater/component_updater_service_internal.h"
10+
11+
#include "src/components/component_updater/component_updater_service.cc"
12+
13+
namespace component_updater {
14+
15+
void CrxUpdateService::OnDemandUpdate(const std::vector<std::string>& ids,
16+
Priority priority,
17+
Callback callback) {
18+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
19+
20+
for (const auto& id : ids) {
21+
if (!GetComponent(id)) {
22+
if (callback) {
23+
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
24+
FROM_HERE, base::BindOnce(std::move(callback),
25+
update_client::Error::INVALID_ARGUMENT));
26+
}
27+
return;
28+
}
29+
}
30+
31+
auto crx_data_callback = base::BindOnce(&CrxUpdateService::GetCrxComponents,
32+
base::Unretained(this));
33+
auto update_complete_callback = base::BindOnce(
34+
&CrxUpdateService::OnUpdateComplete, base::Unretained(this),
35+
std::move(callback), base::TimeTicks::Now());
36+
37+
update_client_->Update(ids, std::move(crx_data_callback), {},
38+
priority == Priority::FOREGROUND,
39+
std::move(update_complete_callback));
40+
}
41+
42+
void OnDemandUpdater::OnDemandUpdate(const std::vector<std::string>& ids,
43+
Priority priority,
44+
Callback callback) {
45+
NOTREACHED_NORETURN();
46+
}
47+
48+
} // namespace component_updater

chromium_src/components/component_updater/component_updater_service.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ class BraveComponentUpdaterAndroid;
1414
}
1515
} // namespace chrome
1616

17-
namespace brave_shields {
18-
class AdBlockComponentFiltersProvider;
17+
namespace brave_component_updater {
18+
class BraveOnDemandUpdater;
1919
}
2020

2121
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ \
2222
friend class ::IPFSDOMHandler; \
2323
friend class ::chrome::android::BraveComponentUpdaterAndroid;
2424

25-
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER \
26-
private: \
27-
friend void BraveOnDemandUpdate(const std::string&); \
28-
friend class brave_shields::AdBlockComponentFiltersProvider; \
29-
\
25+
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER \
26+
private: \
27+
friend class brave_component_updater::BraveOnDemandUpdater; \
28+
\
29+
virtual void OnDemandUpdate(const std::vector<std::string>& ids, \
30+
Priority priority, Callback callback); \
31+
\
3032
public:
33+
3134
#include "src/components/component_updater/component_updater_service.h" // IWYU pragma: export
35+
3236
#undef BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER
3337
#undef BRAVE_COMPONENT_UPDATER_SERVICE_H_
3438

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
7+
#define BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_
8+
9+
#include "components/component_updater/component_updater_service.h"
10+
11+
#define OnDemandUpdate \
12+
OnDemandUpdate(const std::vector<std::string>& ids, Priority priority, \
13+
Callback callback) override; \
14+
void OnDemandUpdate
15+
16+
#include "src/components/component_updater/component_updater_service_internal.h" // IWYU pragma: export
17+
18+
#undef OnDemandUpdate
19+
20+
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_INTERNAL_H_

chromium_src/components/update_client/update_checker.cc

+48-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
* You can obtain one at http://mozilla.org/MPL/2.0/. */
55

6+
#include "components/update_client/update_checker.h"
7+
68
#include <optional>
79

10+
#include "base/ranges/algorithm.h"
11+
#include "components/update_client/update_client.h"
12+
813
#include "src/components/update_client/update_checker.cc"
914

1015
#if BUILDFLAG(WIDEVINE_ARM64_DLL_FIX)
@@ -14,6 +19,19 @@
1419

1520
namespace update_client {
1621

22+
namespace {
23+
24+
bool IsBraveComponent(const Component* component) {
25+
CHECK(component);
26+
const auto& crx_component = component->crx_component();
27+
if (!crx_component || !crx_component->installer) {
28+
return false;
29+
}
30+
return crx_component->installer->IsBraveComponent();
31+
}
32+
33+
} // namespace
34+
1735
SequentialUpdateChecker::SequentialUpdateChecker(
1836
scoped_refptr<Configurator> config,
1937
PersistedData* metadata)
@@ -39,6 +57,16 @@ void SequentialUpdateChecker::CheckForUpdates(
3957
additional_attributes_ = additional_attributes;
4058
update_check_callback_ = std::move(update_check_callback);
4159

60+
// Partition IDs for batch update checks. The order of
61+
// `components_to_check_for_updates` doesn't matter to the caller, as
62+
// post-update mapping is done via an Id->Component map, making this
63+
// rearrangement safe.
64+
base::ranges::stable_partition(
65+
update_context_->components_to_check_for_updates,
66+
[&](const std::string& id) {
67+
return IsBraveComponent(update_context_->components[id].get());
68+
});
69+
4270
for (const auto& id : update_context_->components_to_check_for_updates) {
4371
remaining_ids_.push_back(id);
4472
}
@@ -56,25 +84,36 @@ void SequentialUpdateChecker::CheckNext(
5684
DCHECK(!remaining_ids_.empty());
5785
DCHECK(update_context_);
5886

59-
const auto id = remaining_ids_.front();
60-
remaining_ids_.pop_front();
87+
// Support multiple checks in a single call, but only if they are all Brave.
88+
std::vector<std::string> ids;
89+
for (auto id_it = remaining_ids_.begin(); id_it != remaining_ids_.end();) {
90+
const auto& component = update_context_->components[*id_it];
91+
if (!ids.empty() && !IsBraveComponent(component.get())) {
92+
break;
93+
}
94+
ids.push_back(*id_it);
95+
id_it = remaining_ids_.erase(id_it);
96+
}
6197

6298
scoped_refptr<UpdateContext> context = new UpdateContext(
6399
update_context_->config, update_context_->crx_cache_,
64-
update_context_->is_foreground, update_context_->is_install, {id},
100+
update_context_->is_foreground, update_context_->is_install, ids,
65101
update_context_->crx_state_change_callback,
66102
update_context_->notify_observers_callback,
67103
// We don't pass a context callback here because UpdateChecker doesn't use
68104
// it. This is instead done by UpdateEngine, which calls us.
69105
base::DoNothing(), update_context_->persisted_data,
70106
/*is_update_check_only=*/false);
71107

72-
auto& component = context->components[id];
73-
auto& crx_component = update_context_->components[id]->crx_component();
74-
component->set_crx_component(*crx_component);
75-
component->set_previous_version(crx_component->version);
76-
component->set_previous_fp(crx_component->fingerprint);
77-
context->components_to_check_for_updates.push_back(id);
108+
DCHECK(!ids.empty());
109+
for (const auto& id : ids) {
110+
auto& component = context->components[id];
111+
auto& crx_component = update_context_->components[id]->crx_component();
112+
component->set_crx_component(*crx_component);
113+
component->set_previous_version(crx_component->version);
114+
component->set_previous_fp(crx_component->fingerprint);
115+
context->components_to_check_for_updates.push_back(id);
116+
}
78117

79118
update_checker_ = UpdateChecker::Create(config_, metadata_);
80119

chromium_src/components/update_client/update_client.cc

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
namespace update_client {
1515

16+
bool CrxInstaller::IsBraveComponent() const {
17+
return false;
18+
}
19+
1620
scoped_refptr<UpdateClient> UpdateClientFactory(
1721
scoped_refptr<Configurator> config) {
1822
VLOG(3) << "Brave UpdateClientFactory called";

chromium_src/components/update_client/update_client.h

+5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_
77
#define BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_
88

9+
#define GetInstalledFile(...) \
10+
IsBraveComponent() const; \
11+
virtual bool GetInstalledFile(__VA_ARGS__)
12+
913
#define UpdateClientFactory \
1014
UpdateClientFactory_ChromiumImpl(scoped_refptr<Configurator> config); \
1115
scoped_refptr<UpdateClient> UpdateClientFactory
1216

1317
#include "src/components/update_client/update_client.h" // IWYU pragma: export
1418

19+
#undef GetInstalledFile
1520
#undef UpdateClientFactory
1621

1722
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_

components/brave_component_updater/browser/BUILD.gn

+16
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,19 @@ static_library("browser") {
3535
"//crypto",
3636
]
3737
}
38+
39+
source_set("test_support") {
40+
testonly = true
41+
42+
sources = [
43+
"mock_on_demand_updater.cc",
44+
"mock_on_demand_updater.h",
45+
]
46+
47+
deps = [
48+
":browser",
49+
"//base",
50+
"//components/component_updater",
51+
"//testing/gmock",
52+
]
53+
}

0 commit comments

Comments
 (0)