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

[Uplift to 1.5] Make ntp si component support android #4973

Merged
merged 1 commit into from
Mar 18, 2020
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
9 changes: 7 additions & 2 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "brave/components/brave_shields/browser/referrer_whitelist_service.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "brave/components/ntp_sponsored_images/browser/features.h"
#include "brave/components/ntp_sponsored_images/browser/ntp_sponsored_images_service.h"
#include "brave/components/p3a/buildflags.h"
#include "brave/components/p3a/brave_histogram_rewrite.h"
Expand Down Expand Up @@ -77,6 +78,8 @@
#include "chrome/browser/ui/browser.h"
#endif

using brave_component_updater::BraveComponent;
using ntp_sponsored_images::features::kBraveNTPBrandedWallpaper;
using ntp_sponsored_images::NTPSponsoredImagesService;

namespace {
Expand Down Expand Up @@ -217,10 +220,12 @@ BraveBrowserProcessImpl::ad_block_regional_service_manager() {

NTPSponsoredImagesService*
BraveBrowserProcessImpl::ntp_sponsored_images_service() {
if (!base::FeatureList::IsEnabled(kBraveNTPBrandedWallpaper))
return nullptr;

if (!ntp_sponsored_images_service_) {
ntp_sponsored_images_service_ =
std::make_unique<NTPSponsoredImagesService>(
component_updater());
std::make_unique<NTPSponsoredImagesService>(component_updater());
}

return ntp_sponsored_images_service_.get();
Expand Down
15 changes: 8 additions & 7 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ namespace extensions {
class BraveTorClientUpdater;
}

using brave_component_updater::BraveComponent;
using ntp_sponsored_images::NTPSponsoredImagesService;

class BraveBrowserProcessImpl : public BrowserProcessImpl {
public:
explicit BraveBrowserProcessImpl(StartupData* startup_data);
Expand Down Expand Up @@ -96,7 +93,8 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
BraveWidevineBundleManager* brave_widevine_bundle_manager();
#endif
brave::BraveStatsUpdater* brave_stats_updater();
NTPSponsoredImagesService* ntp_sponsored_images_service();
ntp_sponsored_images::NTPSponsoredImagesService*
ntp_sponsored_images_service();

private:
// BrowserProcessImpl overrides:
Expand All @@ -112,13 +110,15 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
void UpdateBraveDarkMode();
void OnBraveDarkModeChanged();

BraveComponent::Delegate* brave_component_updater_delegate();
brave_component_updater::BraveComponent::Delegate*
brave_component_updater_delegate();

// local_data_files_service_ should always be first because it needs
// to be destroyed last
std::unique_ptr<brave_component_updater::LocalDataFilesService>
local_data_files_service_;
std::unique_ptr<BraveComponent::Delegate> brave_component_updater_delegate_;
std::unique_ptr<brave_component_updater::BraveComponent::Delegate>
brave_component_updater_delegate_;
std::unique_ptr<brave_shields::AdBlockService> ad_block_service_;
std::unique_ptr<brave_shields::AdBlockCustomFiltersService>
ad_block_custom_filters_service_;
Expand Down Expand Up @@ -151,7 +151,8 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
std::unique_ptr<BraveWidevineBundleManager> brave_widevine_bundle_manager_;
#endif
scoped_refptr<brave::BraveP3AService> brave_p3a_service_;
std::unique_ptr<NTPSponsoredImagesService> ntp_sponsored_images_service_;
std::unique_ptr<ntp_sponsored_images::NTPSponsoredImagesService>
ntp_sponsored_images_service_;

SEQUENCE_CHECKER(sequence_checker_);

Expand Down
4 changes: 3 additions & 1 deletion browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/ntp_sponsored_images/browser/ntp_background_images_utils.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -189,8 +190,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
// IPFS companion extension
registry->RegisterBooleanPref(kIPFSCompanionEnabled, false);

ntp_background_images::RegisterProfilePrefs(registry);

// New Tab Page
registry->RegisterBooleanPref(kNewTabPageShowBackgroundImage, true);
registry->RegisterBooleanPref(kNewTabPageShowClock, true);
registry->RegisterBooleanPref(kNewTabPageShowTopSites, true);
registry->RegisterBooleanPref(kNewTabPageShowStats, true);
Expand Down
47 changes: 25 additions & 22 deletions browser/ntp_sponsored_images/view_counter_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

#include "brave/browser/ntp_sponsored_images/view_counter_service_factory.h"

#include <memory>

#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
#include "brave/components/ntp_sponsored_images/browser/features.h"
#include "brave/components/ntp_sponsored_images/browser/ntp_sponsored_images_service.h"
#include "brave/components/ntp_sponsored_images/browser/ntp_sponsored_image_source.h"
#include "brave/components/ntp_sponsored_images/browser/view_counter_service.h"
#include "brave/components/ntp_sponsored_images/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h"
#include "content/public/browser/browser_context.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/url_data_source.h"

namespace ntp_sponsored_images {

// static
ViewCounterService* ViewCounterServiceFactory::GetForProfile(Profile* profile) {
if (base::FeatureList::IsEnabled(features::kBraveNTPBrandedWallpaper))
return static_cast<ViewCounterService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));

return nullptr;
return static_cast<ViewCounterService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}

// static
Expand All @@ -48,24 +48,27 @@ ViewCounterServiceFactory::~ViewCounterServiceFactory() {}

KeyedService* ViewCounterServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* browser_context) const {
Profile* profile = Profile::FromBrowserContext(browser_context);
// Only NTP in normal profile uses sponsored services.
if (browser_context->IsOffTheRecord())
return nullptr;

bool is_supported_locale = false;
auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile);
if (!ads_service) {
LOG(ERROR) << "Ads service was disabled at build time!";
} else {
is_supported_locale = ads_service->IsSupportedLocale();
}
if (auto* service = g_brave_browser_process->ntp_sponsored_images_service()) {
Profile* profile = Profile::FromBrowserContext(browser_context);
bool is_supported_locale = false;
if (auto* ads_service =
brave_ads::AdsServiceFactory::GetForProfile(profile)) {
is_supported_locale = ads_service->IsSupportedLocale();
}
content::URLDataSource::Add(
browser_context,
std::make_unique<NTPSponsoredImageSource>(service));

auto* service = g_brave_browser_process->ntp_sponsored_images_service();
service->AddDataSource(browser_context);
return new ViewCounterService(service,
profile->GetPrefs(),
is_supported_locale);
}

ViewCounterService* instance = new ViewCounterService(
service,
profile->GetPrefs(),
is_supported_locale);
return instance;
return nullptr;
}

void ViewCounterServiceFactory::RegisterProfilePrefs(
Expand Down
40 changes: 7 additions & 33 deletions browser/ui/webui/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
#include "brave/components/ntp_sponsored_images/browser/features.h"
#include "brave/components/ntp_sponsored_images/browser/ntp_sponsored_images_data.h"
#include "brave/components/ntp_sponsored_images/browser/view_counter_service.h"
#include "brave/components/ntp_sponsored_images/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
Expand All @@ -28,33 +27,15 @@
#include "components/prefs/pref_service.h"

using ntp_sponsored_images::features::kBraveNTPBrandedWallpaper;
using ntp_sponsored_images::NTPSponsoredImagesData;
using ntp_sponsored_images::ViewCounterService;
using ntp_sponsored_images::prefs::kNewTabPageShowBackgroundImage;
using ntp_sponsored_images::ViewCounterServiceFactory;

namespace {

bool IsPrivateNewTab(Profile* profile) {
return brave::IsTorProfile(profile) || profile->IsIncognitoProfile();
}

base::DictionaryValue GetBrandedWallpaperDictionary(
NTPSponsoredImagesData* wallpaper,
size_t wallpaper_image_index) {
DCHECK(wallpaper_image_index >= 0 &&
wallpaper_image_index < wallpaper->wallpaper_image_urls().size());

base::DictionaryValue data;
data.SetString("wallpaperImageUrl",
wallpaper->wallpaper_image_urls()[wallpaper_image_index]);
auto logo_data = std::make_unique<base::DictionaryValue>();
logo_data->SetString("image", wallpaper->logo_image_url());
logo_data->SetString("companyName", wallpaper->logo_company_name);
logo_data->SetString("alt", wallpaper->logo_alt_text);
logo_data->SetString("destinationUrl", wallpaper->logo_destination_url);
data.SetDictionary("logo", std::move(logo_data));
return data;
}

base::DictionaryValue GetStatsDictionary(PrefService* prefs) {
base::DictionaryValue stats_data;
stats_data.SetInteger(
Expand Down Expand Up @@ -326,25 +307,18 @@ void BraveNewTabMessageHandler::HandleRegisterNewTabPageView(
AllowJavascript();

// Decrement original value only if there's actual branded content
if (auto* service =
ntp_sponsored_images::ViewCounterServiceFactory::GetForProfile(profile_))
if (auto* service = ViewCounterServiceFactory::GetForProfile(profile_))
service->RegisterPageView();
}

void BraveNewTabMessageHandler::HandleGetBrandedWallpaperData(
const base::ListValue* args) {
AllowJavascript();

auto* service =
ntp_sponsored_images::ViewCounterServiceFactory::GetForProfile(profile_);
if (!service || !service->ShouldShowBrandedWallpaper()) {
ResolveJavascriptCallback(args->GetList()[0], base::Value());
return;
}
auto data = GetBrandedWallpaperDictionary(
service->current_wallpaper(),
service->GetWallpaperImageIndexToDisplay());
ResolveJavascriptCallback(args->GetList()[0], data);
auto* service = ViewCounterServiceFactory::GetForProfile(profile_);
ResolveJavascriptCallback(
args->GetList()[0],
service ? service->GetCurrentWallpaper() : base::Value());
}

void BraveNewTabMessageHandler::OnPrivatePropertiesChanged() {
Expand Down
2 changes: 0 additions & 2 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const char kWebTorrentEnabled[] = "brave.webtorrent_enabled";
const char kHangoutsEnabled[] = "brave.hangouts_enabled";
const char kHideBraveRewardsButton[] = "brave.hide_brave_rewards_button";
const char kIPFSCompanionEnabled[] = "brave.ipfs_companion_enabled";
const char kNewTabPageShowBackgroundImage[] =
"brave.new_tab_page.show_background_image";
const char kNewTabPageShowClock[] = "brave.new_tab_page.show_clock";
const char kNewTabPageShowTopSites[] = "brave.new_tab_page.show_top_sites";
const char kNewTabPageShowStats[] = "brave.new_tab_page.show_stats";
Expand Down
1 change: 0 additions & 1 deletion common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ extern const char kWebTorrentEnabled[];
extern const char kHangoutsEnabled[];
extern const char kHideBraveRewardsButton[];
extern const char kIPFSCompanionEnabled[];
extern const char kNewTabPageShowBackgroundImage[];
extern const char kNewTabPageShowClock[];
extern const char kNewTabPageShowTopSites[];
extern const char kNewTabPageShowStats[];
Expand Down
3 changes: 3 additions & 0 deletions components/ntp_sponsored_images/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ static_library("browser") {
sources = [
"features.cc",
"features.h",
"ntp_background_images_utils.cc",
"ntp_background_images_utils.h",
"ntp_sponsored_image_source.cc",
"ntp_sponsored_image_source.h",
"ntp_sponsored_images_component_installer.cc",
Expand Down Expand Up @@ -35,5 +37,6 @@ static_library("browser") {
"//content/public/browser",
"//content/public/common",
"//crypto",
"//ui/gfx/geometry",
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#include "brave/components/ntp_sponsored_images/browser/ntp_background_images_utils.h"

#include "brave/components/ntp_sponsored_images/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"

namespace ntp_background_images {

void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
ntp_sponsored_images::prefs::kNewTabPageShowBackgroundImage, true);
}

} // namespace ntp_background_images
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef BRAVE_COMPONENTS_NTP_SPONSORED_IMAGES_BROWSER_NTP_BACKGROUND_IMAGES_UTILS_H_
#define BRAVE_COMPONENTS_NTP_SPONSORED_IMAGES_BROWSER_NTP_BACKGROUND_IMAGES_UTILS_H_

namespace user_prefs {
class PrefRegistrySyncable;

} // namespace user_prefs
namespace ntp_background_images {

void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

} // namespace ntp_background_images

#endif // BRAVE_COMPONENTS_NTP_SPONSORED_IMAGES_BROWSER_NTP_BACKGROUND_IMAGES_UTILS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void NTPSponsoredImageSource::StartDataRequest(
} else {
DCHECK(IsWallpaperPath(path));
image_file_path =
images_data->wallpaper_image_files[GetWallpaperIndexFromPath(path)];
images_data->backgrounds[GetWallpaperIndexFromPath(path)].image_file;
}

base::PostTaskAndReplyWithResult(
Expand Down Expand Up @@ -127,7 +127,7 @@ int NTPSponsoredImageSource::GetWallpaperIndexFromPath(
if (!images_data)
return -1;

const int wallpaper_count = images_data->wallpaper_image_files.size();
const int wallpaper_count = images_data->backgrounds.size();
for (int i = 0; i < wallpaper_count; ++i) {
const std::string generated_path =
base::StringPrintf("%s%d.jpg", kWallpaperPathPrefix, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ std::vector<std::string>
return std::vector<std::string>();
}

} // namespace

void OnRegistered(const std::string& component_id) {
BraveOnDemandUpdater::GetInstance()->OnDemandUpdate(component_id);
}

} // namespace

void RegisterNTPSponsoredImagesComponent(
component_updater::ComponentUpdateService* cus,
const RegionalComponentData& data,
Expand Down
Loading