Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] - Fix #27841: Added API for use_legacy_component and callback to the AdblockService #16693

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions components/brave_shields/browser/ad_block_component_installer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ const char kAdBlockIosDefaultDatComponentBase64PublicKey[] =
"5HcH/heRrB4MvrE1J76WF3fvZ03aHVcnlLtQeiNNOZ7VbBDXdie8Nomf/QswbBGa"
"VwIDAQAB";

const char kAdBlockIosComponentName[] = "Brave Ad Block Updater";
const char kAdBlockIosComponentId[] = "iodkpdagapdfkphljnddpjlldadblomo";
const char kAdBlockIosComponentBase64PublicKey[] =
Comment on lines +63 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it more obvious that this component isn't iOS specific?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or find a way to share the component defined in the ad_block_service.cc

Copy link
Contributor

@cuba cuba Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all temporary and can be removed once we switch to the non-legacy version
Edit: NVM, this component id is the new one that is not iOS specific

"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsD/B/MGdz0gh7WkcFARn"
"ZTBX9KAw2fuGeogijoI+fET38IK0L+P/trCT2NshqhRNmrDpLzV2+Dmes6PvkA+O"
"dQkUV6VbChJG+baTfr3Oo5PdE0WxmP9Xh8XD7p85DQrk0jJilKuElxpK7Yq0JhcT"
"Sc3XNHeTwBVqCnHwWZZ+XysYQfjuDQ0MgQpS/s7U04OZ63NIPe/iCQm32stvS/pE"
"ya7KdBZXgRBQ59U6M1n1Ikkp3vfECShbBld6VrrmNrl59yKWlEPepJ9oqUc2Wf2M"
"q+SDNXROG554RnU4BnDJaNETTkDTZ0Pn+rmLmp1qY5Si0yGsfHkrv3FS3vdxVozO"
"PQIDAQAB";

class AdBlockComponentInstallerPolicy
: public component_updater::ComponentInstallerPolicy {
public:
Expand Down Expand Up @@ -172,15 +183,20 @@ void OnRegistered(const std::string& component_id) {

void RegisterAdBlockIosDefaultDatComponent(
component_updater::ComponentUpdateService* cus,
bool use_legacy_component,
OnComponentReadyCallback callback) {
// In test, |cus| could be nullptr.
if (!cus)
return;

auto installer = base::MakeRefCounted<component_updater::ComponentInstaller>(
std::make_unique<AdBlockComponentInstallerPolicy>(
kAdBlockIosDefaultDatComponentBase64PublicKey,
kAdBlockIosDefaultDatComponentId, kAdBlockIosDefaultDatComponentName,
use_legacy_component ? kAdBlockIosDefaultDatComponentBase64PublicKey
: kAdBlockIosComponentBase64PublicKey,
use_legacy_component ? kAdBlockIosDefaultDatComponentId
: kAdBlockIosComponentId,
use_legacy_component ? kAdBlockIosDefaultDatComponentName
: kAdBlockIosComponentName,
callback));
installer->Register(
cus, base::BindOnce(&OnRegistered, kAdBlockIosDefaultDatComponentId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using OnComponentReadyCallback =

void RegisterAdBlockIosDefaultDatComponent(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove Dat from this name as it will not be a dat format if use_legacy_component is false

component_updater::ComponentUpdateService* cus,
bool use_legacy_component,
OnComponentReadyCallback callback);

void RegisterAdBlockDefaultResourceComponent(
Expand Down
2 changes: 0 additions & 2 deletions ios/app/brave_core_main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ - (void)registerComponentsForUpdate:

RegisterSafetyTipsComponent(cus);
brave_wallet::RegisterWalletDataFilesComponent(cus);

[self.adblockService registerDefaultShieldsComponent];
}

+ (void)setLogHandler:(BraveCoreLogHandler)logHandler {
Expand Down
1 change: 0 additions & 1 deletion ios/browser/api/brave_shields/adblock_service+private.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ComponentUpdateService;
@interface AdblockService (Private)
- (instancetype)initWithComponentUpdater:
(component_updater::ComponentUpdateService*)componentUpdaterService;
- (void)registerDefaultShieldsComponent;
@end

#endif // BRAVE_IOS_BROWSER_API_BRAVE_SHIELDS_ADBLOCK_SERVICE_PRIVATE_H_
17 changes: 12 additions & 5 deletions ios/browser/api/brave_shields/adblock_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ NS_ASSUME_NONNULL_BEGIN
OBJC_EXPORT
@interface AdblockService : NSObject

/// The main shields file install path (KVO compiliant)
@property(readonly, nullable) NSString* shieldsInstallPath;
/// Executed each time the main shields component is updated
@property(nonatomic, copy, nullable) void (^shieldsComponentReady)
(NSString* _Nullable installPath);

/// Regional filter lists
@property(readonly, nullable)
NSArray<AdblockFilterListCatalogEntry*>* regionalFilterLists;

/// Executed each time the main shields component is updated
@property(nonatomic, copy, nullable) void (^shieldsComponentReady)
(NSString* _Nullable installPath);
- (void)
registerDefaultShieldsComponentUsingLegacyComponent:(bool)useLegacyComponent
componentReady:
(void (^)(
NSString* _Nullable shieldsInstallPath,
NSArray<
AdblockFilterListCatalogEntry*>* _Nullable regionalFilterLists))
componentReady
NS_SWIFT_NAME(registerDefaultShieldsComponent(useLegacyComponent:componentReady:));

/// Registers a filter list with the component updater and calls
/// `componentReady` each time the component is updated
Expand Down
73 changes: 36 additions & 37 deletions ios/browser/api/brave_shields/adblock_service.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@interface AdblockService () {
component_updater::ComponentUpdateService* _cus; // NOT OWNED
}
@property(nonatomic, copy) NSString* shieldsInstallPath;
@property(nonatomic, copy)
NSArray<AdblockFilterListCatalogEntry*>* regionalFilterLists;
@end
Expand All @@ -32,45 +31,45 @@ - (instancetype)initWithComponentUpdater:
return self;
}

- (void)registerDefaultShieldsComponent {
- (void)
registerDefaultShieldsComponentUsingLegacyComponent:(bool)useLegacyComponent
componentReady:
(void (^)(
NSString* _Nullable shieldsInstallPath,
NSArray<
AdblockFilterListCatalogEntry*>* _Nullable regionalFilterLists))
componentReady {
__weak auto weakSelf = self;
brave_shields::RegisterAdBlockIosDefaultDatComponent(
_cus, base::BindRepeating(^(const base::FilePath& install_path) {
[weakSelf adblockComponentDidBecomeReady:install_path];
}));
}

- (void)adblockComponentDidBecomeReady:(base::FilePath)install_path {
// Update shields install path (w/ KVO)
[self willChangeValueForKey:@"shieldsInstallPath"];
self.shieldsInstallPath = base::SysUTF8ToNSString(install_path.value());
[self didChangeValueForKey:@"shieldsInstallPath"];

__weak auto weakSelf = self;
// Get filter lists from catalog
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&brave_component_updater::GetDATFileAsString,
install_path.AppendASCII("regional_catalog.json")),
base::BindOnce(^(const std::string& json) {
auto strongSelf = weakSelf;
if (!strongSelf) {
return;
}
auto catalog = brave_shields::FilterListCatalogFromJSON(json);

NSMutableArray* lists = [[NSMutableArray alloc] init];
for (const auto& entry : catalog) {
[lists
addObject:[[AdblockFilterListCatalogEntry alloc]
initWithFilterListCatalogEntry:
brave_shields::FilterListCatalogEntry(entry)]];
}
strongSelf.regionalFilterLists = lists;
_cus, useLegacyComponent,
base::BindRepeating(^(const base::FilePath& install_path) {
// Get filter lists from catalog
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&brave_component_updater::GetDATFileAsString,
install_path.AppendASCII("regional_catalog.json")),
base::BindOnce(^(const std::string& json) {
auto strongSelf = weakSelf;
if (!strongSelf) {
return;
}
auto catalog = brave_shields::FilterListCatalogFromJSON(json);

if (strongSelf.shieldsComponentReady) {
strongSelf.shieldsComponentReady(strongSelf.shieldsInstallPath);
}
NSMutableArray* lists = [[NSMutableArray alloc] init];
for (const auto& entry : catalog) {
[lists addObject:[[AdblockFilterListCatalogEntry alloc]
initWithFilterListCatalogEntry:
brave_shields::FilterListCatalogEntry(
entry)]];
}
strongSelf.regionalFilterLists = lists;
if (strongSelf.shieldsComponentReady) {
strongSelf.shieldsComponentReady(
base::SysUTF8ToNSString(install_path.value()));
}
componentReady(base::SysUTF8ToNSString(install_path.value()),
lists);
}));
}));
}

Expand Down