Delete SR component data caching logic

As we decided not show SR top sites in NTP after SR is terminated,
we don't need to cache it.

Delete getDefaultSuperReferralTopSites api and related code
This commit is contained in:
Simon Hong
2020-10-05 23:47:09 -07:00
committed by Brian Clifton
parent a64627affe
commit 5bd3df3298
18 changed files with 23 additions and 165 deletions
+2 -6
View File
@@ -239,13 +239,9 @@ BraveBrowserProcessImpl::ntp_background_images_service() {
return nullptr;
if (!ntp_background_images_service_) {
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
ntp_background_images_service_ =
std::make_unique<NTPBackgroundImagesService>(
component_updater(),
local_state(),
user_data_dir);
std::make_unique<NTPBackgroundImagesService>(component_updater(),
local_state());
ntp_background_images_service_->Init();
}
@@ -224,11 +224,6 @@ void BraveNewTabMessageHandler::RegisterMessages() {
base::BindRepeating(
&BraveNewTabMessageHandler::HandleGetBrandedWallpaperData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getDefaultSuperReferralTopSitesData",
base::BindRepeating(
&BraveNewTabMessageHandler::HandleGetDefaultSuperReferralTopSitesData,
base::Unretained(this)));
}
void BraveNewTabMessageHandler::OnJavascriptAllowed() {
@@ -436,16 +431,6 @@ void BraveNewTabMessageHandler::HandleGetBrandedWallpaperData(
service ? service->GetCurrentWallpaperForDisplay() : base::Value());
}
void BraveNewTabMessageHandler::HandleGetDefaultSuperReferralTopSitesData(
const base::ListValue* args) {
AllowJavascript();
auto* service = ViewCounterServiceFactory::GetForProfile(profile_);
ResolveJavascriptCallback(
args->GetList()[0],
service ? service->GetTopSites(true) : base::Value());
}
void BraveNewTabMessageHandler::OnPrivatePropertiesChanged() {
PrefService* prefs = profile_->GetPrefs();
auto data = GetPrivatePropertiesDictionary(prefs);
@@ -45,7 +45,6 @@ class BraveNewTabMessageHandler : public content::WebUIMessageHandler,
const base::ListValue* args);
void HandleRegisterNewTabPageView(const base::ListValue* args);
void HandleGetBrandedWallpaperData(const base::ListValue* args);
void HandleGetDefaultSuperReferralTopSitesData(const base::ListValue* args);
void OnStatsChanged();
void OnPreferencesChanged();
@@ -7,10 +7,6 @@ export function getBrandedWallpaper (): Promise<undefined | NewTab.BrandedWallpa
return window.cr.sendWithPromise<undefined | NewTab.BrandedWallpaper>('getBrandedWallpaperData')
}
export function getDefaultSuperReferralTopSites (): Promise<undefined | NewTab.DefaultSuperReferralTopSite[]> {
return window.cr.sendWithPromise<undefined | NewTab.DefaultSuperReferralTopSite[]>('getDefaultSuperReferralTopSitesData')
}
export function registerViewCount (): Promise<void> {
return window.cr.sendWithPromise<void>('registerNewTabPageView')
}
@@ -16,7 +16,6 @@ export type InitialData = {
privateTabData: privateTabDataAPI.PrivateTabData
torTabData: torTabDataAPI.TorTabData
topSites: chrome.topSites.MostVisitedURL[]
defaultSuperReferralTopSites: undefined | NewTab.DefaultSuperReferralTopSite[]
brandedWallpaperData: undefined | NewTab.BrandedWallpaper
togetherSupported: boolean
geminiSupported: boolean
@@ -48,7 +47,6 @@ export async function getInitialData (): Promise<InitialData> {
privateTabData,
torTabData,
topSites,
defaultSuperReferralTopSites,
brandedWallpaperData,
togetherSupported,
geminiSupported,
@@ -59,7 +57,6 @@ export async function getInitialData (): Promise<InitialData> {
privateTabDataAPI.getPrivateTabData(),
torTabDataAPI.getTorTabData(),
topSitesAPI.getTopSiteTiles(),
!isIncognito ? brandedWallpaper.getDefaultSuperReferralTopSites() : Promise.resolve(undefined),
!isIncognito ? brandedWallpaper.getBrandedWallpaper() : Promise.resolve(undefined),
new Promise((resolve) => {
if (!('braveTogether' in chrome)) {
@@ -88,7 +85,6 @@ export async function getInitialData (): Promise<InitialData> {
privateTabData,
torTabData,
topSites,
defaultSuperReferralTopSites,
brandedWallpaperData,
togetherSupported,
geminiSupported,
@@ -6,7 +6,6 @@
// Utils
import { debounce } from '../../common/debounce'
const keyName = 'grid-sites-data-v1'
const defaultSuperReferralTopSitesKeyName = 'default-super-referral-top-sites'
export const initialGridSitesState: NewTab.GridSitesState = {
gridSites: [],
@@ -40,10 +39,3 @@ export const debouncedSave = debounce<NewTab.GridSitesState>((data: NewTab.GridS
}
}, 50)
export const isDefaultSuperReferralTopSitesAddedToPinnedSites = (): boolean => {
return window.localStorage.getItem(defaultSuperReferralTopSitesKeyName) !== null
}
export const setDefaultSuperReferralTopSitesAddedToPinnedSites = () => {
window.localStorage.setItem(defaultSuperReferralTopSitesKeyName, 'set')
}
@@ -193,25 +193,6 @@ std::vector<TopSite> NTPBackgroundImagesData::GetTopSitesForWebUI() const {
return top_sites_for_webui;
}
base::Value NTPBackgroundImagesData::GetTopSites(bool for_webui) const {
base::Value top_sites_list_value(base::Value::Type::LIST);
int index = 0;
for (const auto& top_site : top_sites) {
base::Value top_site_value(base::Value::Type::DICTIONARY);
top_site_value.SetStringKey(for_webui ? "title" : "name", top_site.name);
top_site_value.SetStringKey(for_webui ? "url" : "destinationUrl",
top_site.destination_url);
top_site_value.SetStringKey(for_webui? "favicon" : "iconUrl",
GetURLPrefix() + top_site.image_path);
if (for_webui)
top_site_value.SetIntKey("pinnedIndex", index++);
if (!for_webui)
top_site_value.SetStringKey("backgroundColor", top_site.background_color);
top_sites_list_value.Append(std::move(top_site_value));
}
return top_sites_list_value;
}
std::string NTPBackgroundImagesData::logo_image_url() const {
return GetURLPrefix() + kLogoPath;
}
@@ -50,10 +50,6 @@ struct NTPBackgroundImagesData {
bool IsValid() const;
// Generate Value with background image at |index|.
base::Value GetBackgroundAt(size_t index);
// Returns empty list value if this data is for sponsored images wallpaper.
// Use different key string for webui. NTP WebUI uses different key name for
// top sites values.
base::Value GetTopSites(bool for_webui = false) const;
std::vector<TopSite> GetTopSitesForWebUI() const;
bool IsSuperReferral() const;
@@ -56,37 +56,12 @@ std::string GetMappingTableData(const base::FilePath& installed_dir) {
return contents;
}
void CacheSuperReferralData(const std::string& data_json,
const base::FilePath& installed_dir,
const base::FilePath& super_referral_cache_dir) {
NTPBackgroundImagesData data(data_json,
super_referral_cache_dir);
base::CreateDirectory(super_referral_cache_dir);
// Cache logo image
base::CopyFile(installed_dir.Append(data.logo_image_file.BaseName()),
data.logo_image_file);
// Cache topsite favicon images
for (const auto& top_site : data.top_sites) {
base::CopyFile(installed_dir.Append(top_site.image_file.BaseName()),
top_site.image_file);
}
// Cache background images
for (const auto& background : data.backgrounds) {
base::CopyFile(installed_dir.Append(background.image_file.BaseName()),
background.image_file);
}
}
// If registered component is for sponsored images wallpaper, it has photo.json
// in |installed_dir|. Otherwise, it has data.json for super referral.
// This methods cache super referral's favicon data because that favicon images
// could be used after campaign ends.
// And return manifest json string.
std::string HandleComponentData(
const base::FilePath& installed_dir,
const base::FilePath& super_referral_cache_dir,
bool is_super_referral) {
std::string HandleComponentData(const base::FilePath& installed_dir) {
base::FilePath json_path = installed_dir.AppendASCII(kNTPManifestFile);
std::string contents;
@@ -102,9 +77,6 @@ std::string HandleComponentData(
return contents;
}
if (is_super_referral)
CacheSuperReferralData(contents, installed_dir, super_referral_cache_dir);
return contents;
}
@@ -119,28 +91,21 @@ void NTPBackgroundImagesService::RegisterLocalStatePrefs(
prefs::kNewTabPageCachedSuperReferralComponentData, std::string());
registry->RegisterStringPref(
prefs::kNewTabPageCachedSuperReferralCode, std::string());
registry->RegisterListPref(
prefs::kNewTabPageCachedSuperReferralFaviconList);
registry->RegisterBooleanPref(
prefs::kNewTabPageGetInitialSRComponentInProgress, false);
}
NTPBackgroundImagesService::NTPBackgroundImagesService(
component_updater::ComponentUpdateService* cus,
PrefService* local_pref,
const base::FilePath& user_data_dir)
PrefService* local_pref)
: component_update_service_(cus),
local_pref_(local_pref),
super_referral_cache_dir_(
user_data_dir.AppendASCII("SuperReferralCache")),
weak_factory_(this) {
}
NTPBackgroundImagesService::~NTPBackgroundImagesService() = default;
void NTPBackgroundImagesService::Init() {
RestoreCachedTopSitesFaviconList();
// Flag override for testing or demo purposes
base::FilePath forced_local_path(
base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
@@ -221,8 +186,7 @@ void NTPBackgroundImagesService::CheckSuperReferralComponent() {
if (!cached_data.empty()) {
DVLOG(2) << __func__ << ": Initialized SR Data from cache.";
sr_images_data_.reset(
new NTPBackgroundImagesData(cached_data,
super_referral_cache_dir_));
new NTPBackgroundImagesData(cached_data, si_installed_dir_));
}
return;
}
@@ -470,8 +434,7 @@ void NTPBackgroundImagesService::OnComponentReady(
base::PostTaskAndReplyWithResult(
FROM_HERE, {base::ThreadPool(), base::MayBlock()},
base::BindOnce(&HandleComponentData, installed_dir,
super_referral_cache_dir_, is_super_referral),
base::BindOnce(&HandleComponentData, installed_dir),
base::BindOnce(&NTPBackgroundImagesService::OnGetComponentJsonData,
weak_factory_.GetWeakPtr(),
is_super_referral));
@@ -485,22 +448,14 @@ void NTPBackgroundImagesService::OnGetComponentJsonData(
prefs::kNewTabPageGetInitialSRComponentInProgress,
false);
sr_images_data_.reset(
new NTPBackgroundImagesData(json_string,
super_referral_cache_dir_));
new NTPBackgroundImagesData(json_string, sr_installed_dir_));
// |initial_sr_component_info_| has proper data only for initial component
// downloading. After that, it's empty. In test, it's also empty.
if (initial_sr_component_info_.is_dict()) {
local_pref_->Set(prefs::kNewTabPageCachedSuperReferralComponentInfo,
initial_sr_component_info_);
}
if (local_pref_->FindPreference(
prefs::kNewTabPageCachedSuperReferralFaviconList)->
IsDefaultValue() &&
sr_images_data_->IsValid()) {
// This is done only once because super referral will have same top sites
// list forever.
CacheTopSitesFaviconList();
}
CacheTopSitesFaviconList();
local_pref_->SetString(prefs::kNewTabPageCachedSuperReferralComponentData,
json_string);
} else {
@@ -534,33 +489,17 @@ void NTPBackgroundImagesService::MarkThisInstallIsNotSuperReferralForever() {
}
void NTPBackgroundImagesService::CacheTopSitesFaviconList() {
DCHECK(sr_images_data_->IsValid());
DCHECK(local_pref_->FindPreference(
prefs::kNewTabPageCachedSuperReferralFaviconList)->
IsDefaultValue());
if (!sr_images_data_->IsValid())
return;
base::Value list(base::Value::Type::LIST);
for (const auto& top_site : sr_images_data_->top_sites) {
const std::string file_path = super_referral_cache_dir_.Append(
const std::string file_path = sr_installed_dir_.Append(
top_site.image_file.BaseName()).AsUTF8Unsafe();
list.Append(file_path);
cached_top_site_favicon_list_.push_back(file_path);
top_site_favicon_list_.push_back(file_path);
}
local_pref_->Set(prefs::kNewTabPageCachedSuperReferralFaviconList, list);
}
void NTPBackgroundImagesService::RestoreCachedTopSitesFaviconList() {
if (local_pref_->FindPreference(
prefs::kNewTabPageCachedSuperReferralFaviconList)->
IsDefaultValue()) {
return;
}
const auto* value = local_pref_->Get(
prefs::kNewTabPageCachedSuperReferralFaviconList);
for (const auto& file : value->GetList())
cached_top_site_favicon_list_.push_back(file.GetString());
}
bool NTPBackgroundImagesService::IsValidSuperReferralComponentInfo(
@@ -579,8 +518,8 @@ bool NTPBackgroundImagesService::IsValidSuperReferralComponentInfo(
}
std::vector<std::string>
NTPBackgroundImagesService::GetCachedTopSitesFaviconList() const {
return cached_top_site_favicon_list_;
NTPBackgroundImagesService::GetTopSitesFaviconList() const {
return top_site_favicon_list_;
}
void NTPBackgroundImagesService::UnRegisterSuperReferralComponent() {
@@ -45,8 +45,7 @@ class NTPBackgroundImagesService {
NTPBackgroundImagesService(
component_updater::ComponentUpdateService* cus,
PrefService* local_pref,
const base::FilePath& user_data_dir);
PrefService* local_pref);
virtual ~NTPBackgroundImagesService();
NTPBackgroundImagesService(const NTPBackgroundImagesService&) = delete;
@@ -67,7 +66,7 @@ class NTPBackgroundImagesService {
std::string GetSuperReferralThemeName() const;
std::string GetSuperReferralCode() const;
std::vector<std::string> GetCachedTopSitesFaviconList() const;
std::vector<std::string> GetTopSitesFaviconList() const;
private:
friend class TestNTPBackgroundImagesService;
@@ -121,7 +120,6 @@ class NTPBackgroundImagesService {
const base::Value& component_info) const;
void CacheTopSitesFaviconList();
void RestoreCachedTopSitesFaviconList();
void CheckSIComponentUpdate(const std::string& component_id);
// virtual for test.
@@ -134,11 +132,10 @@ class NTPBackgroundImagesService {
virtual void MarkThisInstallIsNotSuperReferralForever();
base::RepeatingTimer si_update_check_timer_;
std::vector<std::string> cached_top_site_favicon_list_;
std::vector<std::string> top_site_favicon_list_;
bool test_data_used_ = false;
component_updater::ComponentUpdateService* component_update_service_;
PrefService* local_pref_;
const base::FilePath super_referral_cache_dir_;
base::FilePath si_installed_dir_;
base::FilePath sr_installed_dir_;
base::ObserverList<Observer>::Unchecked observer_list_;
@@ -125,8 +125,7 @@ class NTPBackgroundImagesServiceTest : public testing::Test {
}
void Init() {
service_.reset(new TestNTPBackgroundImagesService(
nullptr, &pref_service_, base::FilePath()));
service_.reset(new TestNTPBackgroundImagesService(nullptr, &pref_service_));
service_->Init();
}
@@ -178,7 +178,7 @@ bool NTPBackgroundImagesSource::IsTopSiteFaviconPath(
base::FilePath NTPBackgroundImagesSource::GetTopSiteFaviconFilePath(
const std::string& path) const {
std::vector<std::string> list = service_->GetCachedTopSitesFaviconList();
std::vector<std::string> list = service_->GetTopSitesFaviconList();
for (const auto& favicon_file : list) {
base::FilePath file_path = base::FilePath::FromUTF8Unsafe(favicon_file);
if (path.compare(kSuperReferralPath + file_path.BaseName().AsUTF8Unsafe())
@@ -26,8 +26,7 @@ class NTPBackgroundImagesSourceTest : public testing::Test {
auto* registry = local_pref_.registry();
NTPBackgroundImagesService::RegisterLocalStatePrefs(registry);
brave::RegisterPrefsForBraveReferralsService(registry);
service_.reset(new NTPBackgroundImagesService(
nullptr, &local_pref_, base::FilePath()));
service_.reset(new NTPBackgroundImagesService(nullptr, &local_pref_));
source_.reset(new NTPBackgroundImagesSource(service_.get()));
local_pref_.Set(prefs::kNewTabPageCachedSuperReferralComponentInfo,
base::Value(base::Value::Type::DICTIONARY));
@@ -104,17 +104,6 @@ std::vector<TopSite> ViewCounterService::GetTopSitesVectorForWebUI() const {
return {};
}
base::Value ViewCounterService::GetTopSites(bool for_webui) const {
#if BUILDFLAG(ENABLE_BRAVE_REFERRALS)
if (auto* data = GetCurrentBrandedWallpaperData()) {
if (data->IsSuperReferral())
return GetCurrentBrandedWallpaperData()->GetTopSites(for_webui);
}
#endif
return base::Value();
}
std::vector<TopSite> ViewCounterService::GetTopSitesVectorData() const {
#if BUILDFLAG(ENABLE_BRAVE_REFERRALS)
if (auto* data = GetCurrentBrandedWallpaperData())
@@ -51,7 +51,6 @@ class ViewCounterService : public KeyedService,
base::Value GetCurrentWallpaperForDisplay() const;
base::Value GetCurrentWallpaper() const;
base::Value GetTopSites(bool for_webui = false) const;
std::vector<TopSite> GetTopSitesVectorForWebUI() const;
std::vector<TopSite> GetTopSitesVectorData() const;
@@ -63,10 +63,8 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test {
brave::RegisterPrefsForBraveReferralsService(local_registry);
NTPBackgroundImagesService::RegisterLocalStatePrefs(local_registry);
service_ = std::make_unique<NTPBackgroundImagesService>(
nullptr,
&local_pref_,
base::FilePath());
service_ = std::make_unique<NTPBackgroundImagesService>(nullptr,
&local_pref_);
view_counter_ = std::make_unique<ViewCounterService>(
service_.get(), prefs(), true);
@@ -21,8 +21,6 @@ const char kNewTabPageCachedSuperReferralComponentInfo[] =
"brave.new_tab_page.cached_super_referral_component_info";
const char kNewTabPageCachedSuperReferralComponentData[] =
"brave.new_tab_page.cached_super_referral_component_data";
const char kNewTabPageCachedSuperReferralFaviconList[] =
"brave.new_tab_page.cached_super_referral_favicon_list";
const char kNewTabPageGetInitialSRComponentInProgress[] =
"brave.new_tab_page.get_initial_sr_component_in_progress";
const char kNewTabPageCachedSuperReferralCode[] =
@@ -20,7 +20,6 @@ extern const char kNewTabPageShowBackgroundImage[];
// Local prefs
extern const char kNewTabPageCachedSuperReferralComponentInfo[];
extern const char kNewTabPageCachedSuperReferralComponentData[];
extern const char kNewTabPageCachedSuperReferralFaviconList[];
extern const char kNewTabPageGetInitialSRComponentInProgress[];
extern const char kNewTabPageCachedSuperReferralCode[];