Skip to content

Commit

Permalink
Fixes ads are not shown due to compressed resources failing to load
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Sep 24, 2019
1 parent 6918ae2 commit 5856b58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
40 changes: 23 additions & 17 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1745,14 +1745,25 @@ const std::vector<std::string> AdsServiceImpl::GetUserModelLanguages() const {
return languages;
}

std::string AdsServiceImpl::LoadDataResourceAndDecompressIfNeeded(
const int id) const {
std::string data_resource;

auto& resource_bundle = ui::ResourceBundle::GetSharedInstance();
if (resource_bundle.IsGzipped(id)) {
data_resource = resource_bundle.DecompressDataResource(id);
} else {
data_resource = resource_bundle.GetRawDataResource(id).as_string();
}

return data_resource;
}

void AdsServiceImpl::LoadUserModelForLanguage(
const std::string& language,
ads::OnLoadCallback callback) const {
auto raw_data = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
GetUserModelResourceId(language));

std::string user_model;
raw_data.CopyToString(&user_model);
const auto resource_id = GetUserModelResourceId(language);
const auto user_model = LoadDataResourceAndDecompressIfNeeded(resource_id);
callback(ads::Result::SUCCESS, user_model);
}

Expand Down Expand Up @@ -1899,23 +1910,18 @@ void AdsServiceImpl::Reset(

const std::string AdsServiceImpl::LoadJsonSchema(
const std::string& name) {
base::StringPiece schema_raw =
ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
GetSchemaResourceId(name));

std::string schema;
schema_raw.CopyToString(&schema);
return schema;
const auto resource_id = GetSchemaResourceId(name);
return LoadDataResourceAndDecompressIfNeeded(resource_id);
}

void AdsServiceImpl::LoadSampleBundle(
ads::OnLoadSampleBundleCallback callback) {
base::StringPiece sample_bundle_raw =
ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_ADS_SAMPLE_BUNDLE);
const auto resource_id =
GetSchemaResourceId(ads::_bundle_schema_resource_name);

const auto sample_bundle =
LoadDataResourceAndDecompressIfNeeded(resource_id);

std::string sample_bundle;
sample_bundle_raw.CopyToString(&sample_bundle);
callback(ads::Result::SUCCESS, sample_bundle);
}

Expand Down
3 changes: 3 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ class AdsServiceImpl : public AdsService,

uint32_t next_timer_id();

std::string LoadDataResourceAndDecompressIfNeeded(
const int id) const;

bool connected();

// AdsClient implementation
Expand Down
6 changes: 3 additions & 3 deletions vendor/bat-native-ads/resources/bat_ads_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</outputs>
<release seq="1">
<includes>
<include name="IDR_ADS_CATALOG_SCHEMA" file="catalog-schema.json" type="BINDATA" />
<include name="IDR_ADS_BUNDLE_SCHEMA" file="bundle-schema.json" type="BINDATA" />
<include name="IDR_ADS_SAMPLE_BUNDLE" file="sample_bundle.json" type="BINDATA" />
<include name="IDR_ADS_CATALOG_SCHEMA" file="catalog-schema.json" type="BINDATA" compress="gzip" />
<include name="IDR_ADS_BUNDLE_SCHEMA" file="bundle-schema.json" type="BINDATA" compress="gzip" />
<include name="IDR_ADS_SAMPLE_BUNDLE" file="sample_bundle.json" type="BINDATA" compress="gzip" />
<include name="IDR_ADS_USER_MODEL_EN" file="user_models/languages/en/user_model.json" type="BINDATA" compress="gzip" />
<include name="IDR_ADS_USER_MODEL_DE" file="user_models/languages/de/user_model.json" type="BINDATA" compress="gzip" />
<include name="IDR_ADS_USER_MODEL_FR" file="user_models/languages/fr/user_model.json" type="BINDATA" compress="gzip" />
Expand Down

0 comments on commit 5856b58

Please sign in to comment.