-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Tab Page: Componentize BrandedWallpaper and NewTabPageBrandedView…
…Counter
- Loading branch information
Showing
6 changed files
with
246 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
browser/ui/webui/new_tab_page/new_tab_page_branded_view_counter.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
Oops, something went wrong.