Skip to content

Commit

Permalink
New Tab Page: Componentize BrandedWallpaper and NewTabPageBrandedView…
Browse files Browse the repository at this point in the history
…Counter
  • Loading branch information
petemill committed Dec 12, 2019
1 parent c3a5802 commit 7c7eb7a
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 112 deletions.
4 changes: 4 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ source_set("ui") {
"webui/webcompat_reporter_ui.h",
"webui/brave_new_tab_message_handler.cc",
"webui/brave_new_tab_message_handler.h",
"webui/new_tab_page/branded_wallpaper.cc",
"webui/new_tab_page/branded_wallpaper.h",
"webui/new_tab_page/new_tab_page_branded_view_counter.cc",
"webui/new_tab_page/new_tab_page_branded_view_counter.h",
"webui/brave_new_tab_ui.cc",
"webui/brave_new_tab_ui.h",
"webui/brave_web_ui_controller_factory.cc",
Expand Down
122 changes: 10 additions & 112 deletions browser/ui/webui/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,123 +9,20 @@
#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/no_destructor.h"
#include "base/values.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/search_engines/search_engine_provider_util.h"
#include "brave/browser/ui/webui/brave_new_tab_ui.h"
#include "brave/browser/ui/webui/new_tab_page/branded_wallpaper.h"
#include "brave/browser/ui/webui/new_tab_page/new_tab_page_branded_view_counter.h" // NOLINT
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_ui_data_source.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" // NOLINT
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"

namespace {

struct BrandedWallpaperLogo {
std::string imageUrl;
std::string altText;
std::string companyName;
std::string destinationUrl;
};

struct BrandedWallpaper {
std::string wallpaperImageUrl;
struct BrandedWallpaperLogo logo;
};

const BrandedWallpaper kDemoWallpaper = {
"ntp-dummy-brandedwallpaper-background.jpg",
{
"ntp-dummy-brandedwallpaper-logo.png",
"Technikke: For music lovers.",
"Technikke",
"https://brave.com"
}
};

constexpr int kInitialCountToBrandedWallpaper = 1;
constexpr int kRegularCountToBrandedWallpaper = 3;
class NewTabPageViewCounter : public KeyedService {
public:
explicit NewTabPageViewCounter(Profile* profile): profile_(profile) {}

void RegisterPageView() {
// Don't do any counting if we will never be showing the data
if (!GetHasBrandedWallpaper()) {
return;
}
this->count_to_branded_wallpaper_--;
if (this->count_to_branded_wallpaper_ < 0)
this->count_to_branded_wallpaper_ = kRegularCountToBrandedWallpaper;
}

bool GetShouldShowBrandedWallpaper() {
return GetHasBrandedWallpaper() && (
this->count_to_branded_wallpaper_ == 0);
}

const BrandedWallpaper* GetBrandedWallpaper() {
// TODO(petemill): lookup flag to choose between demo and real
return &kDemoWallpaper;
}

private:
bool GetHasBrandedWallpaper() {
// If user has opted-out, never return data
if (!profile_->GetPrefs()->
GetBoolean(kNewTabPageShowBrandedBackgroundImage)) {
return false;
}
// TODO(petemill): lookup flag to always use demo wallpaper
// or check real data source for actual data.
return true;
}

Profile* profile_;
int count_to_branded_wallpaper_ = kInitialCountToBrandedWallpaper;

DISALLOW_COPY_AND_ASSIGN(NewTabPageViewCounter);
};

class NewTabPageViewCounterFactory : public BrowserContextKeyedServiceFactory {
public:
// Returns the CaptivePortalService for |profile|.
static NewTabPageViewCounter* GetForProfile(Profile* profile) {
return static_cast<NewTabPageViewCounter*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}

static NewTabPageViewCounterFactory* GetInstance() {
static base::NoDestructor<NewTabPageViewCounterFactory> instance;
return instance.get();
}
NewTabPageViewCounterFactory() : BrowserContextKeyedServiceFactory(
"NewTabPageViewCounter",
BrowserContextDependencyManager::GetInstance()) { }
~NewTabPageViewCounterFactory() override { }

private:
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* browser_context) const override {
return new NewTabPageViewCounter(
Profile::FromBrowserContext(browser_context));
}
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

DISALLOW_COPY_AND_ASSIGN(NewTabPageViewCounterFactory);
};

bool IsPrivateNewTab(Profile* profile) {
return brave::IsTorProfile(profile) || profile->IsIncognitoProfile();
}
Expand All @@ -135,10 +32,10 @@ base::DictionaryValue GetBrandedWallpaperDictionary(
base::DictionaryValue data;
data.SetString("wallpaperImageUrl", wallpaper->wallpaperImageUrl);
auto logo_data = std::make_unique<base::DictionaryValue>();
logo_data->SetString("image", wallpaper->logo.imageUrl);
logo_data->SetString("companyName", wallpaper->logo.companyName);
logo_data->SetString("alt", wallpaper->logo.altText);
logo_data->SetString("destintationUrl", wallpaper->logo.destinationUrl);
logo_data->SetString("image", wallpaper->logo->imageUrl);
logo_data->SetString("companyName", wallpaper->logo->companyName);
logo_data->SetString("alt", wallpaper->logo->altText);
logo_data->SetString("destinationUrl", wallpaper->logo->destinationUrl);
data.SetDictionary("logo", std::move(logo_data));
return data;
}
Expand Down Expand Up @@ -386,20 +283,21 @@ void BraveNewTabMessageHandler::HandleRegisterNewTabPageView(
AllowJavascript();
// Decrement original value only if there's actual branded content

NewTabPageViewCounterFactory::GetForProfile(profile_)->RegisterPageView();
NewTabPageBrandedViewCounter::GetForProfile(profile_)
->RegisterPageView();
}

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

auto* service = NewTabPageViewCounterFactory::GetForProfile(profile_);
auto* service = NewTabPageBrandedViewCounter::GetForProfile(profile_);
if (!service->GetShouldShowBrandedWallpaper()) {
ResolveJavascriptCallback(args->GetList()[0], base::Value());
return;
}
auto data = GetBrandedWallpaperDictionary(
service->GetBrandedWallpaper());
&service->GetBrandedWallpaper());
ResolveJavascriptCallback(args->GetList()[0], data);
}

Expand Down
12 changes: 12 additions & 0 deletions browser/ui/webui/new_tab_page/branded_wallpaper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2019 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/browser/ui/webui/new_tab_page/branded_wallpaper.h"

BrandedWallpaperLogo::BrandedWallpaperLogo() { }
BrandedWallpaperLogo::~BrandedWallpaperLogo() { }

BrandedWallpaper::BrandedWallpaper() { }
BrandedWallpaper::~BrandedWallpaper() { }
32 changes: 32 additions & 0 deletions browser/ui/webui/new_tab_page/branded_wallpaper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2019 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_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRANDED_WALLPAPER_H_
#define BRAVE_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRANDED_WALLPAPER_H_

#include <string>

class BrandedWallpaperLogo {
public:
BrandedWallpaperLogo();
~BrandedWallpaperLogo();

std::string imageUrl;
std::string altText;
std::string companyName;
std::string destinationUrl;
};

class BrandedWallpaper {
public:
BrandedWallpaper();
~BrandedWallpaper();

std::string wallpaperImageUrl;
std::unique_ptr<BrandedWallpaperLogo> logo;
};

#endif // BRAVE_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRANDED_WALLPAPER_H_

126 changes: 126 additions & 0 deletions browser/ui/webui/new_tab_page/new_tab_page_branded_view_counter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2019 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 <memory>

#include "base/bind.h"
#include "base/no_destructor.h"
#include "brave/browser/ui/webui/new_tab_page/new_tab_page_branded_view_counter.h" // NOLINT
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.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" // NOLINT
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"

namespace {

std::unique_ptr<BrandedWallpaper> GetDemoWallpaper() {
auto demo = std::make_unique<BrandedWallpaper>();
demo->wallpaperImageUrl = "ntp-dummy-brandedwallpaper-background.jpg";
demo->logo = std::make_unique<BrandedWallpaperLogo>();
demo->logo->imageUrl = "ntp-dummy-brandedwallpaper-logo.png";
demo->logo->altText = "Technikke: For music lovers.";
demo->logo->companyName = "Technikke";
demo->logo->destinationUrl = "https://brave.com";
return demo;
}

class NewTabPageBrandedViewCounterFactory
: public BrowserContextKeyedServiceFactory {
public:
// Returns the CaptivePortalService for |profile|.
static NewTabPageBrandedViewCounter* GetForProfile(Profile* profile) {
return static_cast<NewTabPageBrandedViewCounter*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}

static NewTabPageBrandedViewCounterFactory* GetInstance() {
static base::NoDestructor<NewTabPageBrandedViewCounterFactory> instance;
return instance.get();
}
NewTabPageBrandedViewCounterFactory() : BrowserContextKeyedServiceFactory(
"NewTabPageBrandedViewCounter",
BrowserContextDependencyManager::GetInstance()) { }
~NewTabPageBrandedViewCounterFactory() override { }

private:
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* browser_context) const override {
return new NewTabPageBrandedViewCounter(
Profile::FromBrowserContext(browser_context));
}
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

DISALLOW_COPY_AND_ASSIGN(NewTabPageBrandedViewCounterFactory);
};

}

// static
NewTabPageBrandedViewCounter* NewTabPageBrandedViewCounter::GetForProfile(
Profile* profile) {
return NewTabPageBrandedViewCounterFactory::GetForProfile(profile);
}

NewTabPageBrandedViewCounter::NewTabPageBrandedViewCounter(Profile* profile)
: profile_(profile) {
// If we have a wallpaper, store it as private var.
// TODO(petemill): Update the private var when the data source gets
// new content, when we have a data source!
// Set demo wallpaper if a flag is set.
current_wallpaper_ = GetDemoWallpaper();
// Observe relevant preferences that affect whether we should show
// wallpaper or count views.
SetShouldShowFromPreferences();
PrefService* prefs = profile->GetPrefs();
pref_change_registrar_.Init(prefs);
pref_change_registrar_.Add(kNewTabPageShowBackgroundImage,
base::Bind(&NewTabPageBrandedViewCounter::SetShouldShowFromPreferences,
base::Unretained(this)));
pref_change_registrar_.Add(kNewTabPageShowBrandedBackgroundImage,
base::Bind(&NewTabPageBrandedViewCounter::SetShouldShowFromPreferences,
base::Unretained(this)));
}

NewTabPageBrandedViewCounter::~NewTabPageBrandedViewCounter() { }

void NewTabPageBrandedViewCounter::RegisterPageView() {
// Don't do any counting if we will never be showing the data
// since we want the count to start at the point of data being available
// or the user opt-in status changing.
if (!IsBrandedWallpaperActive()) {
return;
}
this->count_to_branded_wallpaper_--;
if (this->count_to_branded_wallpaper_ < 0)
this->count_to_branded_wallpaper_ = kRegularCountToBrandedWallpaper;
}

bool NewTabPageBrandedViewCounter::IsBrandedWallpaperActive() {
return (current_wallpaper_ != nullptr && has_user_opted_in_ == true);
}

bool NewTabPageBrandedViewCounter::GetShouldShowBrandedWallpaper() {
return IsBrandedWallpaperActive() && (
this->count_to_branded_wallpaper_ == 0);
}

const BrandedWallpaper& NewTabPageBrandedViewCounter::GetBrandedWallpaper() {
return *current_wallpaper_;
}

void NewTabPageBrandedViewCounter::SetShouldShowFromPreferences() {
auto* prefs = profile_->GetPrefs();
has_user_opted_in_ = (
prefs->GetBoolean(kNewTabPageShowBrandedBackgroundImage) &&
prefs->GetBoolean(kNewTabPageShowBackgroundImage));
}
Loading

0 comments on commit 7c7eb7a

Please sign in to comment.