1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Defines the classes to realize the Font Settings Extension API as specified
// in the extension API JSON.
#ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/font_pref_change_notifier.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_event_histogram_value.h"
class Profile;
namespace content {
class BrowserContext;
}
namespace extensions {
// This class observes pref changed events on a profile and dispatches the
// corresponding extension API events to extensions.
class FontSettingsEventRouter {
public:
// Constructor for observing pref changed events on |profile|. Stores a
// pointer to |profile| but does not take ownership. |profile| must be
// non-NULL and remain alive for the lifetime of the instance.
explicit FontSettingsEventRouter(Profile* profile);
virtual ~FontSettingsEventRouter();
private:
// Observes browser pref |pref_name|. When a change is observed, dispatches
// event |event_name| to extensions. A JavaScript object is passed to the
// extension event function with the new value of the pref in property |key|.
void AddPrefToObserve(const char* pref_name,
events::HistogramValue histogram_value,
const char* event_name,
const char* key);
// Decodes a preference change for a font family map and invokes
// OnFontNamePrefChange with the right parameters.
void OnFontFamilyMapPrefChanged(const std::string& pref_name);
// Dispatches a changed event for the font setting for |generic_family| and
// |script| to extensions. The new value of the setting is the value of
// browser pref |pref_name|.
void OnFontNamePrefChanged(const std::string& pref_name,
const std::string& generic_family,
const std::string& script);
// Dispatches the setting changed event |event_name| to extensions. The new
// value of the setting is the value of browser pref |pref_name|. This value
// is passed in the JavaScript object argument to the extension event function
// under the key |key|.
void OnFontPrefChanged(events::HistogramValue histogram_value,
const std::string& event_name,
const std::string& key,
const std::string& pref_name);
// Manages pref observation registration.
PrefChangeRegistrar registrar_;
FontPrefChangeNotifier::Registrar font_change_registrar_;
// Weak, owns us (transitively via ExtensionService).
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter);
};
// The profile-keyed service that manages the font_settings extension API.
// This is not an EventRouter::Observer (and does not lazily initialize) because
// doing so caused a regression in perf tests. See crbug.com/163466.
class FontSettingsAPI : public BrowserContextKeyedAPI {
public:
explicit FontSettingsAPI(content::BrowserContext* context);
~FontSettingsAPI() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<FontSettingsAPI>* GetFactoryInstance();
private:
friend class BrowserContextKeyedAPIFactory<FontSettingsAPI>;
// BrowserContextKeyedAPI implementation.
static const char* service_name() {
return "FontSettingsAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
std::unique_ptr<FontSettingsEventRouter> font_settings_event_router_;
};
// fontSettings.clearFont API function.
class FontSettingsClearFontFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.clearFont", FONTSETTINGS_CLEARFONT)
protected:
// RefCounted types have non-public destructors, as with all extension
// functions in this file.
~FontSettingsClearFontFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
// fontSettings.getFont API function.
class FontSettingsGetFontFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.getFont", FONTSETTINGS_GETFONT)
protected:
~FontSettingsGetFontFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
// fontSettings.setFont API function.
class FontSettingsSetFontFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.setFont", FONTSETTINGS_SETFONT)
protected:
~FontSettingsSetFontFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
// fontSettings.getFontList API function.
class FontSettingsGetFontListFunction : public ChromeAsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.getFontList",
FONTSETTINGS_GETFONTLIST)
protected:
~FontSettingsGetFontListFunction() override {}
// ExtensionFunction:
bool RunAsync() override;
private:
void FontListHasLoaded(std::unique_ptr<base::ListValue> list);
bool CopyFontsToResult(base::ListValue* fonts);
};
// Base class for extension API functions that clear a browser font pref.
class ClearFontPrefExtensionFunction : public UIThreadExtensionFunction {
protected:
~ClearFontPrefExtensionFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
// Implementations should return the name of the preference to clear, like
// "webkit.webprefs.default_font_size".
virtual const char* GetPrefName() = 0;
};
// Base class for extension API functions that get a browser font pref.
class GetFontPrefExtensionFunction : public UIThreadExtensionFunction {
protected:
~GetFontPrefExtensionFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
// Implementations should return the name of the preference to get, like
// "webkit.webprefs.default_font_size".
virtual const char* GetPrefName() = 0;
// Implementations should return the key for the value in the extension API,
// like "pixelSize".
virtual const char* GetKey() = 0;
};
// Base class for extension API functions that set a browser font pref.
class SetFontPrefExtensionFunction : public UIThreadExtensionFunction {
protected:
~SetFontPrefExtensionFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
// Implementations should return the name of the preference to set, like
// "webkit.webprefs.default_font_size".
virtual const char* GetPrefName() = 0;
// Implementations should return the key for the value in the extension API,
// like "pixelSize".
virtual const char* GetKey() = 0;
};
// The following are get/set/clear API functions that act on a browser font
// pref.
class FontSettingsClearDefaultFontSizeFunction
: public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFontSize",
FONTSETTINGS_CLEARDEFAULTFONTSIZE)
protected:
~FontSettingsClearDefaultFontSizeFunction() override {}
// ClearFontPrefExtensionFunction:
const char* GetPrefName() override;
};
class FontSettingsGetDefaultFontSizeFunction
: public GetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFontSize",
FONTSETTINGS_GETDEFAULTFONTSIZE)
protected:
~FontSettingsGetDefaultFontSizeFunction() override {}
// GetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
class FontSettingsSetDefaultFontSizeFunction
: public SetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFontSize",
FONTSETTINGS_SETDEFAULTFONTSIZE)
protected:
~FontSettingsSetDefaultFontSizeFunction() override {}
// SetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
class FontSettingsClearDefaultFixedFontSizeFunction
: public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFixedFontSize",
FONTSETTINGS_CLEARDEFAULTFIXEDFONTSIZE)
protected:
~FontSettingsClearDefaultFixedFontSizeFunction() override {}
// ClearFontPrefExtensionFunction:
const char* GetPrefName() override;
};
class FontSettingsGetDefaultFixedFontSizeFunction
: public GetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFixedFontSize",
FONTSETTINGS_GETDEFAULTFIXEDFONTSIZE)
protected:
~FontSettingsGetDefaultFixedFontSizeFunction() override {}
// GetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
class FontSettingsSetDefaultFixedFontSizeFunction
: public SetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFixedFontSize",
FONTSETTINGS_SETDEFAULTFIXEDFONTSIZE)
protected:
~FontSettingsSetDefaultFixedFontSizeFunction() override {}
// SetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
class FontSettingsClearMinimumFontSizeFunction
: public ClearFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.clearMinimumFontSize",
FONTSETTINGS_CLEARMINIMUMFONTSIZE)
protected:
~FontSettingsClearMinimumFontSizeFunction() override {}
// ClearFontPrefExtensionFunction:
const char* GetPrefName() override;
};
class FontSettingsGetMinimumFontSizeFunction
: public GetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.getMinimumFontSize",
FONTSETTINGS_GETMINIMUMFONTSIZE)
protected:
~FontSettingsGetMinimumFontSizeFunction() override {}
// GetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
class FontSettingsSetMinimumFontSizeFunction
: public SetFontPrefExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("fontSettings.setMinimumFontSize",
FONTSETTINGS_SETMINIMUMFONTSIZE)
protected:
~FontSettingsSetMinimumFontSizeFunction() override {}
// SetFontPrefExtensionFunction:
const char* GetPrefName() override;
const char* GetKey() override;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
|