#ifndef CHROME_BROWSER_BROWSER_SWITCHER_BROWSER_SWITCHER_PREFS_H_
#define CHROME_BROWSER_BROWSER_SWITCHER_BROWSER_SWITCHER_PREFS_H_
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "base/callback_list.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/policy/core/common/policy_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "url/gurl.h"
namespace user_prefs {
class PrefRegistrySyncable;
}
class PrefService;
class Profile;
namespace browser_switcher {
class NoCopyUrl {
public:
explicit NoCopyUrl(const GURL& original);
NoCopyUrl(const NoCopyUrl&) = delete;
const GURL& original() const { return *original_; }
std::string_view host_and_port() const { return host_and_port_; }
std::string_view spec() const { return original_->spec(); }
std::string_view spec_without_port() const { return spec_without_port_; }
private:
const raw_ref<const GURL> original_;
std::string host_and_port_;
std::string spec_without_port_;
};
struct RawRuleSet {
RawRuleSet();
RawRuleSet(std::vector<std::string>&& sitelist,
std::vector<std::string>&& greylist);
RawRuleSet(RawRuleSet&&);
~RawRuleSet();
RawRuleSet& operator=(RawRuleSet&&);
std::vector<std::string> sitelist;
std::vector<std::string> greylist;
};
class Rule {
public:
explicit Rule(std::string_view original_rule);
virtual ~Rule() = default;
virtual bool Matches(const NoCopyUrl& no_copy_url) const = 0;
virtual bool IsValid() const = 0;
virtual std::string ToString() const = 0;
int priority() const { return priority_; }
bool inverted() const { return inverted_; }
private:
int priority_;
bool inverted_;
};
struct RuleSet {
RuleSet();
RuleSet(RuleSet&&);
~RuleSet();
std::vector<std::unique_ptr<Rule>> sitelist;
std::vector<std::unique_ptr<Rule>> greylist;
};
enum class ParsingMode {
kDefault = 0,
kIESiteListMode = 1,
kMaxValue = kIESiteListMode,
};
class BrowserSwitcherPrefs : public KeyedService,
public policy::PolicyService::Observer {
private:
using PrefsChangedSignature = void(BrowserSwitcherPrefs*,
const std::vector<std::string>&);
public:
using PrefsChangedCallback = base::RepeatingCallback<PrefsChangedSignature>;
BrowserSwitcherPrefs() = delete;
explicit BrowserSwitcherPrefs(Profile* profile);
BrowserSwitcherPrefs(const BrowserSwitcherPrefs&) = delete;
BrowserSwitcherPrefs& operator=(const BrowserSwitcherPrefs&) = delete;
~BrowserSwitcherPrefs() override;
void Shutdown() override;
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
bool IsEnabled() const;
const std::string& GetAlternativeBrowserPath() const;
const std::vector<std::string>& GetAlternativeBrowserParameters() const;
bool KeepLastTab() const;
int GetDelay() const;
ParsingMode GetParsingMode() const;
const RuleSet& GetRules() const;
RawRuleSet GetCachedExternalSitelist() const;
void SetCachedExternalSitelist(const RawRuleSet& sitelist);
RawRuleSet GetCachedExternalGreylist() const;
void SetCachedExternalGreylist(const RawRuleSet& greylist);
#if BUILDFLAG(IS_WIN)
RawRuleSet GetCachedIeemSitelist() const;
void SetCachedIeemSitelist(const RawRuleSet& sitelist);
#endif
GURL GetExternalSitelistUrl() const;
GURL GetExternalGreylistUrl() const;
#if BUILDFLAG(IS_WIN)
bool UseIeSitelist() const;
const base::FilePath& GetChromePath() const;
const std::vector<std::string>& GetChromeParameters() const;
#endif
void OnPolicyUpdated(const policy::PolicyNamespace& ns,
const policy::PolicyMap& previous,
const policy::PolicyMap& current) override;
base::CallbackListSubscription RegisterPrefsChangedCallback(
PrefsChangedCallback cb);
protected:
BrowserSwitcherPrefs(PrefService* prefs,
policy::PolicyService* policy_service);
private:
void RunCallbacksIfDirty();
void MarkDirty(const std::string& pref_name);
void AlternativeBrowserPathChanged();
void AlternativeBrowserParametersChanged();
void ParsingModeChanged();
void UrlListChanged();
void GreylistChanged();
#if BUILDFLAG(IS_WIN)
void ChromePathChanged();
void ChromeParametersChanged();
#endif
const raw_ptr<policy::PolicyService> policy_service_;
const raw_ptr<PrefService> prefs_;
PrefChangeRegistrar filtering_change_registrar_;
PrefChangeRegistrar notifying_change_registrar_;
std::string alt_browser_path_;
std::vector<std::string> alt_browser_params_;
ParsingMode parsing_mode_ = ParsingMode::kDefault;
#if BUILDFLAG(IS_WIN)
base::FilePath chrome_path_;
std::vector<std::string> chrome_params_;
#endif
RuleSet rules_;
std::vector<std::string> dirty_prefs_;
base::RepeatingCallbackList<PrefsChangedSignature> callback_list_;
base::WeakPtrFactory<BrowserSwitcherPrefs> weak_ptr_factory_{this};
};
namespace prefs {
extern const char kEnabled[];
extern const char kDelay[];
extern const char kAlternativeBrowserPath[];
extern const char kAlternativeBrowserParameters[];
extern const char kKeepLastTab[];
extern const char kParsingMode[];
extern const char kUrlList[];
extern const char kUrlGreylist[];
extern const char kExternalSitelistUrl[];
extern const char kCachedExternalSitelist[];
extern const char kCachedExternalSitelistGreylist[];
extern const char kExternalGreylistUrl[];
extern const char kCachedExternalGreylist[];
#if BUILDFLAG(IS_WIN)
extern const char kUseIeSitelist[];
extern const char kCachedIeSitelist[];
extern const char kCachedIeSitelistGreylist[];
extern const char kChromePath[];
extern const char kChromeParameters[];
#endif
}
}
#endif