Skip to content

Commit

Permalink
Added API for use_legacy_component and callback to the AdblockService
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Jan 16, 2023
1 parent c2b1d3f commit 8457b3a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 47 deletions.
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[] =
"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(
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

0 comments on commit 8457b3a

Please sign in to comment.