Skip to content

Migrate brave ads from rapidjson to base::Value #14176

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

Merged
merged 15 commits into from
Jul 22, 2022
Merged
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
28 changes: 16 additions & 12 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,8 @@ void RewardsDOMHandler::ToggleAdThumbUp(const base::Value::List& args) {

ads::AdContentInfo ad_content;
const base::Value& value = args[0];
ad_content.FromValue(value);
if (value.is_dict())
ad_content.FromValue(value.GetDict());

ads_service_->ToggleAdThumbUp(
ad_content.ToJson(), base::BindOnce(&RewardsDOMHandler::OnToggleAdThumbUp,
Expand All @@ -1316,8 +1317,8 @@ void RewardsDOMHandler::OnToggleAdThumbUp(const std::string& json) {
const bool success = ad_content.FromJson(json);
DCHECK(success);

const base::Value value = ad_content.ToValue();
CallJavascriptFunction("brave_rewards.onToggleAdThumbUp", value);
CallJavascriptFunction("brave_rewards.onToggleAdThumbUp",
base::Value(ad_content.ToValue()));
}

void RewardsDOMHandler::ToggleAdThumbDown(const base::Value::List& args) {
Expand All @@ -1331,7 +1332,8 @@ void RewardsDOMHandler::ToggleAdThumbDown(const base::Value::List& args) {

ads::AdContentInfo ad_content;
const base::Value& value = args[0];
ad_content.FromValue(value);
if (value.is_dict())
ad_content.FromValue(value.GetDict());

ads_service_->ToggleAdThumbDown(
ad_content.ToJson(),
Expand All @@ -1348,8 +1350,8 @@ void RewardsDOMHandler::OnToggleAdThumbDown(const std::string& json) {
const bool success = ad_content.FromJson(json);
DCHECK(success);

const base::Value value = ad_content.ToValue();
CallJavascriptFunction("brave_rewards.onToggleAdThumbDown", value);
CallJavascriptFunction("brave_rewards.onToggleAdThumbDown",
base::Value(ad_content.ToValue()));
}

void RewardsDOMHandler::ToggleAdOptIn(const base::Value::List& args) {
Expand Down Expand Up @@ -1421,7 +1423,8 @@ void RewardsDOMHandler::ToggleSavedAd(const base::Value::List& args) {

ads::AdContentInfo ad_content;
const base::Value& value = args[0];
ad_content.FromValue(value);
if (value.is_dict())
ad_content.FromValue(value.GetDict());

ads_service_->ToggleSavedAd(
ad_content.ToJson(), base::BindOnce(&RewardsDOMHandler::OnToggleSavedAd,
Expand All @@ -1437,8 +1440,8 @@ void RewardsDOMHandler::OnToggleSavedAd(const std::string& json) {
const bool success = ad_content.FromJson(json);
DCHECK(success);

const base::Value value = ad_content.ToValue();
CallJavascriptFunction("brave_rewards.onToggleSavedAd", value);
CallJavascriptFunction("brave_rewards.onToggleSavedAd",
base::Value(ad_content.ToValue()));
}

void RewardsDOMHandler::ToggleFlaggedAd(const base::Value::List& args) {
Expand All @@ -1452,7 +1455,8 @@ void RewardsDOMHandler::ToggleFlaggedAd(const base::Value::List& args) {

ads::AdContentInfo ad_content;
const base::Value& value = args[0];
ad_content.FromValue(value);
if (value.is_dict())
ad_content.FromValue(value.GetDict());

ads_service_->ToggleFlaggedAd(
ad_content.ToJson(), base::BindOnce(&RewardsDOMHandler::OnToggleFlaggedAd,
Expand All @@ -1468,8 +1472,8 @@ void RewardsDOMHandler::OnToggleFlaggedAd(const std::string& json) {
const bool success = ad_content.FromJson(json);
DCHECK(success);

const base::Value value = ad_content.ToValue();
CallJavascriptFunction("brave_rewards.onToggleFlaggedAd", value);
CallJavascriptFunction("brave_rewards.onToggleFlaggedAd",
base::Value(ad_content.ToValue()));
}

void RewardsDOMHandler::SaveAdsSetting(const base::Value::List& args) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void AdsService::RegisterProfilePrefs(
base::Time::Now());

registry->RegisterBooleanPref(ads::prefs::kHasMigratedClientState, false);
registry->RegisterBooleanPref(ads::prefs::kHasMigratedConfirmationState,
false);
registry->RegisterBooleanPref(ads::prefs::kHasMigratedConversionState, false);
registry->RegisterBooleanPref(ads::prefs::kHasMigratedRewardsState, false);

Expand Down
8 changes: 2 additions & 6 deletions components/brave_ads/browser/ads_service_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
#include <string>

#include "base/callback.h"
#include "base/values.h"
#include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h"

namespace base {
class DictionaryValue;
class ListValue;
} // namespace base

namespace ads {
struct NewTabPageAdInfo;
} // namespace ads
Expand All @@ -29,7 +25,7 @@ using GetStatementOfAccountsCallback = base::OnceCallback<
void(const bool, const double, const int, const double, const double)>;

using MaybeServeInlineContentAdCallback = base::OnceCallback<
void(const bool, const std::string&, const base::DictionaryValue&)>;
void(const bool, const std::string&, const base::Value::Dict&)>;

using TriggerSearchResultAdEventCallback =
base::OnceCallback<void(const bool,
Expand Down
36 changes: 15 additions & 21 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ void AdsServiceImpl::MaybeServeInlineContentAd(
const std::string& dimensions,
MaybeServeInlineContentAdCallback callback) {
if (!IsBatAdsBound()) {
std::move(callback).Run(false, "", base::DictionaryValue());
std::move(callback).Run(false, "", base::Value::Dict());
return;
}

Expand Down Expand Up @@ -2017,16 +2017,15 @@ void AdsServiceImpl::OnMaybeServeInlineContentAd(
const bool success,
const std::string& dimensions,
const std::string& json) {
base::DictionaryValue dictionary;
base::Value::Dict dict;

if (success) {
ads::InlineContentAdInfo ad;
ad.FromJson(json);

dictionary = ad.ToValue();
dict = ad.ToValue();
}

std::move(callback).Run(success, dimensions, dictionary);
std::move(callback).Run(success, dimensions, dict);
}

void AdsServiceImpl::OnTriggerSearchResultAdEvent(
Expand Down Expand Up @@ -2056,24 +2055,19 @@ void AdsServiceImpl::OnGetHistory(GetHistoryCallback callback,
base::ListValue list;

for (const auto& item : history.items) {
base::DictionaryValue history_item_dictionary;
base::DictionaryValue ad_content_dictionary = item.ad_content.ToValue();
history_item_dictionary.SetPath("adContent",
std::move(ad_content_dictionary));
base::DictionaryValue category_content_dictionary =
item.category_content.ToValue();
history_item_dictionary.SetPath("categoryContent",
std::move(category_content_dictionary));
base::ListValue history_item_list;
history_item_list.Append(std::move(history_item_dictionary));

base::DictionaryValue dictionary;
dictionary.SetStringKey("uuid", base::NumberToString(uuid++));
base::Value::Dict history_item_dict;
history_item_dict.Set("adContent", item.ad_content.ToValue());
history_item_dict.Set("categoryContent", item.category_content.ToValue());
base::Value::List history_item_list;
history_item_list.Append(std::move(history_item_dict));

base::Value::Dict dict;
dict.Set("uuid", base::NumberToString(uuid++));
const double js_time = item.created_at.ToJsTimeIgnoringNull();
dictionary.SetDoubleKey("timestampInMilliseconds", js_time);
dictionary.SetPath("adDetailRows", std::move(history_item_list));
dict.Set("timestampInMilliseconds", js_time);
dict.Set("adDetailRows", std::move(history_item_list));

list.Append(std::move(dictionary));
list.Append(std::move(dict));
}

std::move(callback).Run(list);
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/client/legacy_client_migration_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/client/legacy_client_migration_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/client/legacy_client_migration_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/database/database_migration_issue_17231_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/database/database_migration_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/locale/locale_manager_unittest.cc",
Expand Down
8 changes: 4 additions & 4 deletions components/brave_sync/qr_code_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ std::unique_ptr<QrCodeData> QrCodeData::FromJson(
return nullptr;
}

const std::string* version_value = value->FindStringKey("version");
const auto& root = value->GetDict();
const std::string* version_value = root.FindString("version");
if (!version_value) {
VLOG(1) << "Missing version";
return nullptr;
Expand All @@ -93,15 +94,14 @@ std::unique_ptr<QrCodeData> QrCodeData::FromJson(
}
qr_data->version = version;

const std::string* sync_code_hex_value =
value->FindStringKey("sync_code_hex");
const std::string* sync_code_hex_value = root.FindString("sync_code_hex");
if (!sync_code_hex_value) {
VLOG(1) << "Missing sync code hex";
return nullptr;
}
qr_data->sync_code_hex = *sync_code_hex_value;

const std::string* not_after_string = value->FindStringKey("not_after");
const std::string* not_after_string = root.FindString("not_after");
if (!not_after_string) {
VLOG(1) << "Missing not after time";
return nullptr;
Expand Down
20 changes: 10 additions & 10 deletions components/brave_today/browser/brave_news_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void BraveNewsController::GetDisplayAd(GetDisplayAdCallback callback) {
}
auto on_ad_received = base::BindOnce(
[](GetDisplayAdCallback callback, const bool success,
const std::string& dimensions, const base::DictionaryValue& ad_data) {
const std::string& dimensions, const base::Value::Dict& ad_data) {
if (!success) {
VLOG(1) << "GetDisplayAd: no ad";
std::move(callback).Run(nullptr);
Expand All @@ -334,16 +334,16 @@ void BraveNewsController::GetDisplayAd(GetDisplayAdCallback callback) {
// TODO(petemill): brave_ads seems to use mojom, perhaps we can receive
// and send to callback the actual typed mojom struct from brave_ads?
auto ad = mojom::DisplayAd::New();
ad->uuid = *ad_data.FindStringKey("uuid");
ad->creative_instance_id = *ad_data.FindStringKey("creativeInstanceId");
if (ad_data.HasKey("ctaText"))
ad->cta_text = *ad_data.FindStringKey("ctaText");
ad->dimensions = *ad_data.FindStringKey("dimensions");
ad->title = *ad_data.FindStringKey("title");
ad->description = *ad_data.FindStringKey("description");
ad->uuid = *ad_data.FindString("uuid");
ad->creative_instance_id = *ad_data.FindString("creativeInstanceId");
if (const auto* value = ad_data.FindString("ctaText"))
ad->cta_text = *value;
ad->dimensions = *ad_data.FindString("dimensions");
ad->title = *ad_data.FindString("title");
ad->description = *ad_data.FindString("description");
ad->image = mojom::Image::NewPaddedImageUrl(
GURL(*ad_data.FindStringKey("imageUrl")));
ad->target_url = GURL(*ad_data.FindStringKey("targetUrl"));
GURL(*ad_data.FindString("imageUrl")));
ad->target_url = GURL(*ad_data.FindString("targetUrl"));
std::move(callback).Run(std::move(ad));
},
std::move(callback));
Expand Down
4 changes: 4 additions & 0 deletions vendor/bat-native-ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ source_set("ads") {
"src/bat/ads/internal/legacy_migration/client/legacy_client_migration.h",
"src/bat/ads/internal/legacy_migration/client/legacy_client_migration_util.cc",
"src/bat/ads/internal/legacy_migration/client/legacy_client_migration_util.h",
"src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration.cc",
"src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration.h",
"src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_util.cc",
"src/bat/ads/internal/legacy_migration/confirmations/legacy_confirmation_migration_util.h",
"src/bat/ads/internal/legacy_migration/conversions/legacy_conversions_migration.cc",
"src/bat/ads/internal/legacy_migration/conversions/legacy_conversions_migration.h",
"src/bat/ads/internal/legacy_migration/database/database_constants.h",
Expand Down
10 changes: 3 additions & 7 deletions vendor/bat-native-ads/include/bat/ads/ad_content_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@

#include <string>

#include "base/values.h"
#include "bat/ads/ad_content_action_types.h"
#include "bat/ads/ad_type.h"
#include "bat/ads/confirmation_type.h"
#include "bat/ads/export.h"
#include "url/gurl.h"

namespace base {
class DictionaryValue;
class Value;
} // namespace base

namespace ads {

struct ADS_EXPORT AdContentInfo final {
Expand All @@ -33,8 +29,8 @@ struct ADS_EXPORT AdContentInfo final {
AdContentLikeActionType ToggleThumbUpActionType() const;
AdContentLikeActionType ToggleThumbDownActionType() const;

base::DictionaryValue ToValue() const;
bool FromValue(const base::Value& value);
base::Value::Dict ToValue() const;
bool FromValue(const base::Value::Dict& value);

std::string ToJson() const;
bool FromJson(const std::string& json);
Expand Down
13 changes: 3 additions & 10 deletions vendor/bat-native-ads/include/bat/ads/category_content_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

#include <string>

#include "base/values.h"
#include "bat/ads/category_content_action_types.h"
#include "bat/ads/export.h"

namespace base {
class DictionaryValue;
class Value;
} // namespace base

namespace ads {

struct ADS_EXPORT CategoryContentInfo final {
Expand All @@ -27,11 +23,8 @@ struct ADS_EXPORT CategoryContentInfo final {
bool operator==(const CategoryContentInfo& rhs) const;
bool operator!=(const CategoryContentInfo& rhs) const;

base::DictionaryValue ToValue() const;
bool FromValue(const base::Value& value);

std::string ToJson() const;
bool FromJson(const std::string& json);
base::Value::Dict ToValue() const;
void FromValue(const base::Value::Dict& value);

std::string category;
CategoryContentOptActionType opt_action_type =
Expand Down
4 changes: 4 additions & 0 deletions vendor/bat-native-ads/include/bat/ads/history_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include "base/values.h"
#include "bat/ads/export.h"
#include "bat/ads/history_item_info.h"

Expand All @@ -20,6 +21,9 @@ struct ADS_EXPORT HistoryInfo final {
HistoryInfo& operator=(const HistoryInfo& info);
~HistoryInfo();

base::Value::Dict ToValue() const;
bool FromValue(const base::Value::Dict& value);

std::string ToJson() const;
bool FromJson(const std::string& json);

Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ads/include/bat/ads/history_item_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ struct ADS_EXPORT HistoryItemInfo final {
bool operator==(const HistoryItemInfo& rhs) const;
bool operator!=(const HistoryItemInfo& rhs) const;

std::string ToJson() const;
bool FromJson(const std::string& json);
base::Value::Dict ToValue() const;
bool FromValue(const base::Value::Dict& value);

base::Time created_at;
AdContentInfo ad_content;
Expand Down
10 changes: 3 additions & 7 deletions vendor/bat-native-ads/include/bat/ads/inline_content_ad_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

#include <string>

#include "base/values.h"
#include "bat/ads/ad_info.h"
#include "bat/ads/export.h"
#include "url/gurl.h"

namespace base {
class DictionaryValue;
class Value;
} // namespace base

namespace ads {

struct ADS_EXPORT InlineContentAdInfo final : AdInfo {
Expand All @@ -30,8 +26,8 @@ struct ADS_EXPORT InlineContentAdInfo final : AdInfo {

bool IsValid() const;

base::DictionaryValue ToValue() const;
bool FromValue(const base::Value& value);
base::Value::Dict ToValue() const;
bool FromValue(const base::Value::Dict& value);

std::string ToJson() const;
bool FromJson(const std::string& json);
Expand Down
Loading