Commit 427c8da4 authored by Kyle Milka's avatar Kyle Milka Committed by Commit Bot

[NTP] Add method to check if a custom background is set

Add a method to InstantService to check if a custom background
has been set. Clean up a few places that were checking the
pref directly.

Bug: 873699
Change-Id: I755c6d26cb5fc6cce1239fd796fbfc5e3fb45ce5
Reviewed-on: https://chromium-review.googlesource.com/c/1338091Reviewed-by: default avatarRamya Nagarajan <ramyan@chromium.org>
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610955}
parent 8cf3cc0c
......@@ -620,8 +620,6 @@ void InstantService::BuildThemeInfo() {
}
}
// TODO(crbug.com/863942): Should switching default search provider retain the
// copy of user uploaded photos?
void InstantService::ApplyOrResetCustomBackgroundThemeInfo() {
// Reset the pref if the feature is disabled.
if (!features::IsCustomBackgroundsEnabled()) {
......@@ -636,19 +634,8 @@ void InstantService::ApplyOrResetCustomBackgroundThemeInfo() {
}
// Attempt to get custom background URL from preferences.
const base::DictionaryValue* background_info =
pref_service_->GetDictionary(prefs::kNtpCustomBackgroundDict);
const base::Value* background_url =
background_info->FindKey(kNtpCustomBackgroundURL);
if (!background_url) {
ResetCustomBackgroundThemeInfo();
return;
}
// Verify that the custom background URL is valid.
GURL custom_background_url(
background_info->FindKey(kNtpCustomBackgroundURL)->GetString());
if (!custom_background_url.is_valid()) {
GURL custom_background_url;
if (!IsCustomBackgroundPrefValid(custom_background_url)) {
ResetCustomBackgroundThemeInfo();
return;
}
......@@ -745,6 +732,34 @@ void InstantService::FallbackToDefaultThemeInfo() {
theme_info_->custom_background_attribution_action_url = GURL();
}
bool InstantService::IsCustomBackgroundSet() {
GURL custom_background_url;
if (!IsCustomBackgroundPrefValid(custom_background_url))
return false;
if (IsLocalFileUrl(custom_background_url) &&
!pref_service_->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)) {
return false;
}
return true;
}
bool InstantService::IsCustomBackgroundPrefValid(GURL& custom_background_url) {
const base::DictionaryValue* background_info =
profile_->GetPrefs()->GetDictionary(prefs::kNtpCustomBackgroundDict);
if (!background_info)
return false;
const base::Value* background_url =
background_info->FindKey(kNtpCustomBackgroundURL);
if (!background_url)
return false;
custom_background_url = GURL(background_url->GetString());
return custom_background_url.is_valid();
}
void InstantService::RemoveLocalBackgroundImageCopy() {
base::FilePath path = profile_->GetPath().AppendASCII(
chrome::kChromeSearchLocalNtpBackgroundFilename);
......
......@@ -132,6 +132,9 @@ class InstantService : public KeyedService,
void AddValidBackdropUrlForTesting(const GURL& url) const;
// Check if a custom background has been set by the user.
bool IsCustomBackgroundSet();
private:
class SearchProviderObserver;
......@@ -178,6 +181,10 @@ class InstantService : public KeyedService,
void RemoveLocalBackgroundImageCopy();
// Returns false if the custom background pref cannot be parsed, otherwise
// returns true and sets custom_background_url to the value in the pref.
bool IsCustomBackgroundPrefValid(GURL& custom_background_url);
// Update the background pref to point to
// chrome-search://local-ntp/background.jpg
void SetBackgroundToLocalResource();
......
......@@ -10,6 +10,8 @@
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "chrome/browser/after_startup_task_utils.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/ntp_features.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/themes/theme_service.h"
......@@ -87,10 +89,6 @@ enum VoiceError {
VOICE_ERROR_MAX
};
// Key used in prefs::kNtpCustomBackgroundDict to save a background image URL.
// TODO(crbug.com/873699): Refactor customization check for better testability.
const char kNtpCustomBackgroundURL[] = "background_url";
// Logs BackgroundCustomization availability on the NTP,
void LogBackgroundCustomizationAvailability(
BackgroundCustomization availability) {
......@@ -490,15 +488,9 @@ bool NTPUserDataLogger::ThemeIsConfigured() const {
}
bool NTPUserDataLogger::CustomBackgroundIsConfigured() const {
const base::DictionaryValue* background_info =
profile_->GetPrefs()->GetDictionary(prefs::kNtpCustomBackgroundDict);
const base::Value* background_url =
background_info->FindKey(kNtpCustomBackgroundURL);
if (!background_url) {
return false;
}
GURL custom_background_url(background_url->GetString());
return custom_background_url.is_valid();
InstantService* instant_service =
InstantServiceFactory::GetForProfile(profile_);
return instant_service->IsCustomBackgroundSet();
}
void NTPUserDataLogger::EmitNtpStatistics(base::TimeDelta load_time) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment