#ifndef CHROME_BROWSER_ASH_CUSTOMIZATION_CUSTOMIZATION_DOCUMENT_H_
#define CHROME_BROWSER_ASH_CUSTOMIZATION_CUSTOMIZATION_DOCUMENT_H_
#include <stddef.h>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "url/gurl.h"
class PrefRegistrySimple;
class Profile;
namespace base {
class FilePath;
}
namespace extensions {
class ExternalLoader;
}
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace ash {
namespace system {
class StatisticsProvider;
}
class CustomizationWallpaperDownloader;
class ServicesCustomizationExternalLoader;
void InitStartupCustomizationDocumentForTesting(const std::string& manifest);
class CustomizationDocument {
public:
CustomizationDocument(const CustomizationDocument&) = delete;
CustomizationDocument& operator=(const CustomizationDocument&) = delete;
virtual ~CustomizationDocument();
bool IsReady() const { return root_.get(); }
protected:
explicit CustomizationDocument(const std::string& accepted_version);
virtual bool LoadManifestFromFile(const base::FilePath& manifest_path);
virtual bool LoadManifestFromString(const std::string& manifest);
std::string GetLocaleSpecificString(const std::string& locale,
const std::string& dictionary_name,
const std::string& entry_name) const;
std::unique_ptr<base::Value::Dict> root_;
std::string accepted_version_;
};
class StartupCustomizationDocument : public CustomizationDocument {
public:
static StartupCustomizationDocument* GetInstance();
StartupCustomizationDocument(const StartupCustomizationDocument&) = delete;
StartupCustomizationDocument& operator=(const StartupCustomizationDocument&) =
delete;
std::string GetEULAPage(const std::string& locale) const;
const std::string& initial_locale() const { return initial_locale_; }
const std::vector<std::string>& configured_locales() const;
const std::string& initial_locale_default() const;
const std::string& initial_timezone() const { return initial_timezone_; }
const std::string& keyboard_layout() const { return keyboard_layout_; }
private:
FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, Basic);
FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, VPD);
FRIEND_TEST_ALL_PREFIXES(StartupCustomizationDocumentTest, BadManifest);
FRIEND_TEST_ALL_PREFIXES(ServicesCustomizationDocumentTest, MultiLanguage);
friend class OobeLocalizationTest;
friend void InitStartupCustomizationDocumentForTesting(
const std::string& manifest);
friend struct base::DefaultSingletonTraits<StartupCustomizationDocument>;
StartupCustomizationDocument();
StartupCustomizationDocument(system::StatisticsProvider* provider,
const std::string& manifest);
~StartupCustomizationDocument() override;
void Init(system::StatisticsProvider* provider);
std::string initial_locale_;
std::vector<std::string> configured_locales_;
std::string initial_timezone_;
std::string keyboard_layout_;
};
class ServicesCustomizationDocument : public CustomizationDocument {
public:
static ServicesCustomizationDocument* GetInstance();
ServicesCustomizationDocument(const ServicesCustomizationDocument&) = delete;
ServicesCustomizationDocument& operator=(
const ServicesCustomizationDocument&) = delete;
static void RegisterPrefs(PrefRegistrySimple* registry);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
static constexpr char kManifestUrl[] =
"https://ssl.gstatic.com/chrome/chromeos-customization/%s.json";
static bool WasOOBECustomizationApplied();
void EnsureCustomizationApplied();
base::OnceClosure EnsureCustomizationAppliedClosure();
void StartFetching();
bool ApplyOOBECustomization();
bool GetDefaultWallpaperUrl(GURL* out_url) const;
std::optional<base::Value::Dict> GetDefaultApps() const;
::extensions::ExternalLoader* CreateExternalLoader(Profile* profile);
std::string GetOemAppsFolderName(const std::string& locale) const;
static void InitializeForTesting(
scoped_refptr<network::SharedURLLoaderFactory> factory);
static void ShutdownForTesting();
static base::FilePath GetCustomizedWallpaperCacheDir();
static base::FilePath GetCustomizedWallpaperDownloadedFileName();
CustomizationWallpaperDownloader* wallpaper_downloader_for_testing() {
return wallpaper_downloader_.get();
}
private:
friend struct base::DefaultSingletonTraits<ServicesCustomizationDocument>;
FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest,
OEMWallpaperIsPresent);
FRIEND_TEST_ALL_PREFIXES(CustomizationWallpaperDownloaderBrowserTest,
OEMWallpaperRetryFetch);
typedef std::vector<base::WeakPtr<ServicesCustomizationExternalLoader> >
ExternalLoaders;
class ApplyingTask;
ServicesCustomizationDocument();
explicit ServicesCustomizationDocument(const std::string& manifest);
~ServicesCustomizationDocument() override;
static void SetApplied(bool val);
bool LoadManifestFromString(const std::string& manifest) override;
void OnSimpleLoaderComplete(std::optional<std::string> response_body);
void StartFileFetch();
void DoStartFileFetch();
void OnManifestRead(const std::string& manifest);
void OnManifestLoaded();
static base::Value::Dict GetDefaultAppsInProviderFormat(
const base::Value::Dict& root);
void UpdateCachedManifest(Profile* profile);
void OnCustomizationNotFound();
void SetOemFolderName(Profile* profile, const base::Value::Dict& root);
std::string GetOemAppsFolderNameImpl(const std::string& locale,
const base::Value::Dict& root) const;
void StartOEMWallpaperDownload(const GURL& wallpaper_url,
std::unique_ptr<ApplyingTask> applying);
void CheckAndApplyWallpaper();
void OnCheckedWallpaperCacheExists(std::unique_ptr<bool> exists,
std::unique_ptr<ApplyingTask> applying);
void ApplyWallpaper(bool default_wallpaper_file_exists,
std::unique_ptr<ApplyingTask> applying);
void OnOEMWallpaperDownloaded(std::unique_ptr<ApplyingTask> applying,
bool success,
const GURL& wallpaper_url);
void ApplyingTaskStarted();
void ApplyingTaskFinished(bool success);
GURL url_;
std::unique_ptr<network::SimpleURLLoader> url_loader_;
int num_retries_;
bool load_started_;
std::optional<base::TimeDelta> custom_network_delay_ = std::nullopt;
ExternalLoaders external_loaders_;
std::unique_ptr<CustomizationWallpaperDownloader> wallpaper_downloader_;
size_t apply_tasks_started_;
size_t apply_tasks_finished_;
size_t apply_tasks_success_;
base::WeakPtrFactory<ServicesCustomizationDocument> weak_ptr_factory_{this};
};
}
#endif