2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "InternalSettings.h"
30 #include "CaptionUserPreferences.h"
32 #include "ExceptionCode.h"
33 #include "FrameView.h"
35 #include "LocaleToScriptMapping.h"
36 #include "MainFrame.h"
38 #include "PageGroup.h"
39 #include "RuntimeEnabledFeatures.h"
41 #include "Supplementable.h"
44 #if ENABLE(INPUT_TYPE_COLOR)
45 #include "ColorChooser.h"
48 #define InternalSettingsGuardForSettingsReturn(returnValue) \
50 ec = INVALID_ACCESS_ERR; \
54 #define InternalSettingsGuardForSettings() \
56 ec = INVALID_ACCESS_ERR; \
60 #define InternalSettingsGuardForPage() \
62 ec = INVALID_ACCESS_ERR; \
68 InternalSettings::Backup::Backup(Settings& settings)
69 : m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::sharedFeatures().cssExclusionsEnabled())
70 , m_originalCSSShapesEnabled(RuntimeEnabledFeatures::sharedFeatures().cssShapesEnabled())
71 #if ENABLE(SHADOW_DOM)
72 , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::sharedFeatures().shadowDOMEnabled())
73 , m_originalAuthorShadowDOMForAnyElementEnabled(RuntimeEnabledFeatures::sharedFeatures().authorShadowDOMForAnyElementEnabled())
75 , m_originalEditingBehavior(settings.editingBehaviorType())
76 #if ENABLE(TEXT_AUTOSIZING)
77 , m_originalTextAutosizingEnabled(settings.textAutosizingEnabled())
78 , m_originalTextAutosizingWindowSizeOverride(settings.textAutosizingWindowSizeOverride())
79 , m_originalTextAutosizingFontScaleFactor(settings.textAutosizingFontScaleFactor())
81 , m_originalMediaTypeOverride(settings.mediaTypeOverride())
82 , m_originalCanvasUsesAcceleratedDrawing(settings.canvasUsesAcceleratedDrawing())
83 , m_originalMockScrollbarsEnabled(settings.mockScrollbarsEnabled())
84 , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::sharedFeatures().langAttributeAwareFormControlUIEnabled())
85 , m_imagesEnabled(settings.areImagesEnabled())
86 , m_minimumTimerInterval(settings.minDOMTimerInterval())
87 #if ENABLE(VIDEO_TRACK)
88 , m_shouldDisplaySubtitles(settings.shouldDisplaySubtitles())
89 , m_shouldDisplayCaptions(settings.shouldDisplayCaptions())
90 , m_shouldDisplayTextDescriptions(settings.shouldDisplayTextDescriptions())
92 , m_defaultVideoPosterURL(settings.defaultVideoPosterURL())
93 , m_originalTimeWithoutMouseMovementBeforeHidingControls(settings.timeWithoutMouseMovementBeforeHidingControls())
94 , m_useLegacyBackgroundSizeShorthandBehavior(settings.useLegacyBackgroundSizeShorthandBehavior())
95 , m_autoscrollForDragAndDropEnabled(settings.autoscrollForDragAndDropEnabled())
96 , m_pluginReplacementEnabled(RuntimeEnabledFeatures::sharedFeatures().pluginReplacementEnabled())
100 void InternalSettings::Backup::restoreTo(Settings& settings)
102 RuntimeEnabledFeatures::sharedFeatures().setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
103 RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(m_originalCSSShapesEnabled);
104 #if ENABLE(SHADOW_DOM)
105 RuntimeEnabledFeatures::sharedFeatures().setShadowDOMEnabled(m_originalShadowDOMEnabled);
106 RuntimeEnabledFeatures::sharedFeatures().setAuthorShadowDOMForAnyElementEnabled(m_originalAuthorShadowDOMForAnyElementEnabled);
108 settings.setEditingBehaviorType(m_originalEditingBehavior);
110 for (auto iter = m_standardFontFamilies.begin(); iter != m_standardFontFamilies.end(); ++iter)
111 settings.setStandardFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
112 m_standardFontFamilies.clear();
114 for (auto iter = m_fixedFontFamilies.begin(); iter != m_fixedFontFamilies.end(); ++iter)
115 settings.setFixedFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
116 m_fixedFontFamilies.clear();
118 for (auto iter = m_serifFontFamilies.begin(); iter != m_serifFontFamilies.end(); ++iter)
119 settings.setSerifFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
120 m_serifFontFamilies.clear();
122 for (auto iter = m_sansSerifFontFamilies.begin(); iter != m_sansSerifFontFamilies.end(); ++iter)
123 settings.setSansSerifFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
124 m_sansSerifFontFamilies.clear();
126 for (auto iter = m_cursiveFontFamilies.begin(); iter != m_cursiveFontFamilies.end(); ++iter)
127 settings.setCursiveFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
128 m_cursiveFontFamilies.clear();
130 for (auto iter = m_fantasyFontFamilies.begin(); iter != m_fantasyFontFamilies.end(); ++iter)
131 settings.setFantasyFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
132 m_fantasyFontFamilies.clear();
134 for (auto iter = m_pictographFontFamilies.begin(); iter != m_pictographFontFamilies.end(); ++iter)
135 settings.setPictographFontFamily(iter->value, static_cast<UScriptCode>(iter->key));
136 m_pictographFontFamilies.clear();
138 #if ENABLE(TEXT_AUTOSIZING)
139 settings.setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
140 settings.setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
141 settings.setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
143 settings.setMediaTypeOverride(m_originalMediaTypeOverride);
144 settings.setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
145 settings.setMockScrollbarsEnabled(m_originalMockScrollbarsEnabled);
146 RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
147 settings.setImagesEnabled(m_imagesEnabled);
148 settings.setMinDOMTimerInterval(m_minimumTimerInterval);
149 #if ENABLE(VIDEO_TRACK)
150 settings.setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
151 settings.setShouldDisplayCaptions(m_shouldDisplayCaptions);
152 settings.setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
154 settings.setDefaultVideoPosterURL(m_defaultVideoPosterURL);
155 settings.setTimeWithoutMouseMovementBeforeHidingControls(m_originalTimeWithoutMouseMovementBeforeHidingControls);
156 settings.setUseLegacyBackgroundSizeShorthandBehavior(m_useLegacyBackgroundSizeShorthandBehavior);
157 settings.setAutoscrollForDragAndDropEnabled(m_autoscrollForDragAndDropEnabled);
158 RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(m_pluginReplacementEnabled);
161 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
162 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
163 // Instead, we manually make InternalSettings supplement Page.
164 class InternalSettingsWrapper : public Supplement<Page> {
166 explicit InternalSettingsWrapper(Page* page)
167 : m_internalSettings(InternalSettings::create(page)) { }
168 virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
170 virtual bool isRefCountedWrapper() const override { return true; }
172 InternalSettings* internalSettings() const { return m_internalSettings.get(); }
175 RefPtr<InternalSettings> m_internalSettings;
178 const char* InternalSettings::supplementName()
180 return "InternalSettings";
183 InternalSettings* InternalSettings::from(Page* page)
185 if (!Supplement<Page>::from(page, supplementName()))
186 Supplement<Page>::provideTo(page, supplementName(), adoptPtr(new InternalSettingsWrapper(page)));
187 return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
190 InternalSettings::~InternalSettings()
194 InternalSettings::InternalSettings(Page* page)
195 : InternalSettingsGenerated(page)
197 , m_backup(page->settings())
201 void InternalSettings::resetToConsistentState()
203 page()->setPageScaleFactor(1, IntPoint(0, 0));
204 page()->setCanStartMedia(true);
206 m_backup.restoreTo(*settings());
207 m_backup = Backup(*settings());
209 InternalSettingsGenerated::resetToConsistentState();
212 Settings* InternalSettings::settings() const
216 return &page()->settings();
219 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
221 InternalSettingsGuardForSettings();
222 settings()->setMockScrollbarsEnabled(enabled);
225 static bool urlIsWhitelistedForSetShadowDOMEnabled(const String& url)
227 // This check is just for preventing fuzzers from crashing because of unintended API calls.
228 // You can list your test if needed.
229 return notFound != url.find("fast/dom/shadow/content-shadow-unknown.html")
230 || notFound != url.find("fast/dom/shadow/insertion-points-with-shadow-disabled.html");
233 void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
235 if (!urlIsWhitelistedForSetShadowDOMEnabled(page()->mainFrame().document()->url().string())) {
236 ec = INVALID_ACCESS_ERR;
240 #if ENABLE(SHADOW_DOM)
241 RuntimeEnabledFeatures::sharedFeatures().setShadowDOMEnabled(enabled);
243 // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
244 // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
246 ec = INVALID_ACCESS_ERR;
250 void InternalSettings::setAuthorShadowDOMForAnyElementEnabled(bool isEnabled)
252 #if ENABLE(SHADOW_DOM)
253 RuntimeEnabledFeatures::sharedFeatures().setAuthorShadowDOMForAnyElementEnabled(isEnabled);
255 UNUSED_PARAM(isEnabled);
259 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
261 #if ENABLE(TOUCH_EVENTS)
262 InternalSettingsGuardForSettings();
263 settings()->setTouchEventEmulationEnabled(enabled);
265 UNUSED_PARAM(enabled);
270 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
272 InternalSettingsGuardForSettings();
273 UScriptCode code = scriptNameToCode(script);
274 if (code == USCRIPT_INVALID_CODE)
276 m_backup.m_standardFontFamilies.add(code, settings()->standardFontFamily(code));
277 settings()->setStandardFontFamily(family, code);
280 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
282 InternalSettingsGuardForSettings();
283 UScriptCode code = scriptNameToCode(script);
284 if (code == USCRIPT_INVALID_CODE)
286 m_backup.m_serifFontFamilies.add(code, settings()->serifFontFamily(code));
287 settings()->setSerifFontFamily(family, code);
290 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
292 InternalSettingsGuardForSettings();
293 UScriptCode code = scriptNameToCode(script);
294 if (code == USCRIPT_INVALID_CODE)
296 m_backup.m_sansSerifFontFamilies.add(code, settings()->sansSerifFontFamily(code));
297 settings()->setSansSerifFontFamily(family, code);
300 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
302 InternalSettingsGuardForSettings();
303 UScriptCode code = scriptNameToCode(script);
304 if (code == USCRIPT_INVALID_CODE)
306 m_backup.m_fixedFontFamilies.add(code, settings()->fixedFontFamily(code));
307 settings()->setFixedFontFamily(family, code);
310 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
312 InternalSettingsGuardForSettings();
313 UScriptCode code = scriptNameToCode(script);
314 if (code == USCRIPT_INVALID_CODE)
316 m_backup.m_cursiveFontFamilies.add(code, settings()->cursiveFontFamily(code));
317 settings()->setCursiveFontFamily(family, code);
320 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
322 InternalSettingsGuardForSettings();
323 UScriptCode code = scriptNameToCode(script);
324 if (code == USCRIPT_INVALID_CODE)
326 m_backup.m_fantasyFontFamilies.add(code, settings()->fantasyFontFamily(code));
327 settings()->setFantasyFontFamily(family, code);
330 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
332 InternalSettingsGuardForSettings();
333 UScriptCode code = scriptNameToCode(script);
334 if (code == USCRIPT_INVALID_CODE)
336 m_backup.m_pictographFontFamilies.add(code, settings()->pictographFontFamily(code));
337 settings()->setPictographFontFamily(family, code);
340 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
342 #if ENABLE(TEXT_AUTOSIZING)
343 InternalSettingsGuardForSettings();
344 settings()->setTextAutosizingEnabled(enabled);
346 UNUSED_PARAM(enabled);
351 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
353 #if ENABLE(TEXT_AUTOSIZING)
354 InternalSettingsGuardForSettings();
355 settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
358 UNUSED_PARAM(height);
363 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
365 InternalSettingsGuardForSettings();
366 settings()->setMediaTypeOverride(mediaType);
369 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
371 #if ENABLE(TEXT_AUTOSIZING)
372 InternalSettingsGuardForSettings();
373 settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
375 UNUSED_PARAM(fontScaleFactor);
380 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
383 RuntimeEnabledFeatures::sharedFeatures().setCSSExclusionsEnabled(enabled);
386 void InternalSettings::setCSSShapesEnabled(bool enabled, ExceptionCode& ec)
389 RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(enabled);
392 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
394 InternalSettingsGuardForSettings();
395 m_page->setCanStartMedia(enabled);
398 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
400 InternalSettingsGuardForSettings();
401 if (equalIgnoringCase(editingBehavior, "win"))
402 settings()->setEditingBehaviorType(EditingWindowsBehavior);
403 else if (equalIgnoringCase(editingBehavior, "mac"))
404 settings()->setEditingBehaviorType(EditingMacBehavior);
405 else if (equalIgnoringCase(editingBehavior, "unix"))
406 settings()->setEditingBehaviorType(EditingUnixBehavior);
411 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
413 InternalSettingsGuardForSettings();
415 #if ENABLE(VIDEO_TRACK)
418 CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
420 if (equalIgnoringCase(kind, "Subtitles"))
421 captionPreferences->setUserPrefersSubtitles(enabled);
422 else if (equalIgnoringCase(kind, "Captions"))
423 captionPreferences->setUserPrefersCaptions(enabled);
424 else if (equalIgnoringCase(kind, "TextDescriptions"))
425 captionPreferences->setUserPrefersTextDescriptions(enabled);
430 UNUSED_PARAM(enabled);
434 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
436 InternalSettingsGuardForSettingsReturn(false);
438 #if ENABLE(VIDEO_TRACK)
441 CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
443 if (equalIgnoringCase(kind, "Subtitles"))
444 return captionPreferences->userPrefersSubtitles();
445 if (equalIgnoringCase(kind, "Captions"))
446 return captionPreferences->userPrefersCaptions();
447 if (equalIgnoringCase(kind, "TextDescriptions"))
448 return captionPreferences->userPrefersTextDescriptions();
458 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
460 InternalSettingsGuardForSettings();
462 if (mode == "AllowAll")
463 settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
464 else if (mode == "BlockThirdParty")
465 settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
466 else if (mode == "BlockAll")
467 settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
472 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
474 RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(enabled);
477 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
479 InternalSettingsGuardForSettings();
480 settings()->setImagesEnabled(enabled);
483 void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
485 InternalSettingsGuardForSettings();
486 settings()->setMinDOMTimerInterval(intervalInSeconds);
489 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
491 InternalSettingsGuardForSettings();
492 settings()->setDefaultVideoPosterURL(url);
495 void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
497 InternalSettingsGuardForSettings();
498 settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
501 void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
503 InternalSettingsGuardForSettings();
504 settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
507 void InternalSettings::setAutoscrollForDragAndDropEnabled(bool enabled, ExceptionCode& ec)
509 InternalSettingsGuardForSettings();
510 settings()->setAutoscrollForDragAndDropEnabled(enabled);
513 void InternalSettings::setFontFallbackPrefersPictographs(bool preferPictographs, ExceptionCode& ec)
515 InternalSettingsGuardForSettings();
516 settings()->setFontFallbackPrefersPictographs(preferPictographs);
519 void InternalSettings::setPluginReplacementEnabled(bool enabled)
521 RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(enabled);
524 void InternalSettings::setBackgroundShouldExtendBeyondPage(bool hasExtendedBackground, ExceptionCode& ec)
526 InternalSettingsGuardForSettings();
527 settings()->setBackgroundShouldExtendBeyondPage(hasExtendedBackground);