Work towards having frames in the scrolling tree
[WebKit-https.git] / Source / WebCore / testing / InternalSettings.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26
27 #include "config.h"
28 #include "InternalSettings.h"
29
30 #include "CaptionUserPreferences.h"
31 #include "Document.h"
32 #include "ExceptionCode.h"
33 #include "FrameView.h"
34 #include "Language.h"
35 #include "LocaleToScriptMapping.h"
36 #include "MainFrame.h"
37 #include "Page.h"
38 #include "PageGroup.h"
39 #include "RuntimeEnabledFeatures.h"
40 #include "Settings.h"
41 #include "Supplementable.h"
42 #include "TextRun.h"
43
44 #if ENABLE(INPUT_TYPE_COLOR)
45 #include "ColorChooser.h"
46 #endif
47
48 #define InternalSettingsGuardForSettingsReturn(returnValue) \
49     if (!settings()) { \
50         ec = INVALID_ACCESS_ERR; \
51         return returnValue; \
52     }
53
54 #define InternalSettingsGuardForSettings()  \
55     if (!settings()) { \
56         ec = INVALID_ACCESS_ERR; \
57         return; \
58     }
59
60 #define InternalSettingsGuardForPage() \
61     if (!page()) { \
62         ec = INVALID_ACCESS_ERR; \
63         return; \
64     }
65
66 namespace WebCore {
67
68 InternalSettings::Backup::Backup(Settings& settings)
69     : m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::sharedFeatures().cssExclusionsEnabled())
70     , m_originalCSSShapesEnabled(RuntimeEnabledFeatures::sharedFeatures().cssShapesEnabled())
71     , m_originalEditingBehavior(settings.editingBehaviorType())
72 #if ENABLE(TEXT_AUTOSIZING)
73     , m_originalTextAutosizingEnabled(settings.textAutosizingEnabled())
74     , m_originalTextAutosizingWindowSizeOverride(settings.textAutosizingWindowSizeOverride())
75     , m_originalTextAutosizingFontScaleFactor(settings.textAutosizingFontScaleFactor())
76 #endif
77     , m_originalMediaTypeOverride(settings.mediaTypeOverride())
78     , m_originalCanvasUsesAcceleratedDrawing(settings.canvasUsesAcceleratedDrawing())
79     , m_originalMockScrollbarsEnabled(settings.mockScrollbarsEnabled())
80     , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::sharedFeatures().langAttributeAwareFormControlUIEnabled())
81     , m_imagesEnabled(settings.areImagesEnabled())
82     , m_minimumTimerInterval(settings.minDOMTimerInterval())
83 #if ENABLE(VIDEO_TRACK)
84     , m_shouldDisplaySubtitles(settings.shouldDisplaySubtitles())
85     , m_shouldDisplayCaptions(settings.shouldDisplayCaptions())
86     , m_shouldDisplayTextDescriptions(settings.shouldDisplayTextDescriptions())
87 #endif
88     , m_defaultVideoPosterURL(settings.defaultVideoPosterURL())
89     , m_originalTimeWithoutMouseMovementBeforeHidingControls(settings.timeWithoutMouseMovementBeforeHidingControls())
90     , m_useLegacyBackgroundSizeShorthandBehavior(settings.useLegacyBackgroundSizeShorthandBehavior())
91     , m_autoscrollForDragAndDropEnabled(settings.autoscrollForDragAndDropEnabled())
92     , m_pluginReplacementEnabled(RuntimeEnabledFeatures::sharedFeatures().pluginReplacementEnabled())
93     , m_shouldConvertPositionStyleOnCopy(settings.shouldConvertPositionStyleOnCopy())
94 {
95 }
96
97 void InternalSettings::Backup::restoreTo(Settings& settings)
98 {
99     RuntimeEnabledFeatures::sharedFeatures().setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
100     RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(m_originalCSSShapesEnabled);
101     settings.setEditingBehaviorType(m_originalEditingBehavior);
102
103     for (const auto& standardFont : m_standardFontFamilies)
104         settings.setStandardFontFamily(standardFont.value, static_cast<UScriptCode>(standardFont.key));
105     m_standardFontFamilies.clear();
106
107     for (const auto& fixedFont : m_fixedFontFamilies)
108         settings.setFixedFontFamily(fixedFont.value, static_cast<UScriptCode>(fixedFont.key));
109     m_fixedFontFamilies.clear();
110
111     for (const auto& serifFont : m_serifFontFamilies)
112         settings.setSerifFontFamily(serifFont.value, static_cast<UScriptCode>(serifFont.key));
113     m_serifFontFamilies.clear();
114
115     for (const auto& sansSerifFont : m_sansSerifFontFamilies)
116         settings.setSansSerifFontFamily(sansSerifFont.value, static_cast<UScriptCode>(sansSerifFont.key));
117     m_sansSerifFontFamilies.clear();
118
119     for (const auto& cursiveFont : m_cursiveFontFamilies)
120         settings.setCursiveFontFamily(cursiveFont.value, static_cast<UScriptCode>(cursiveFont.key));
121     m_cursiveFontFamilies.clear();
122
123     for (const auto& fantasyFont : m_fantasyFontFamilies)
124         settings.setFantasyFontFamily(fantasyFont.value, static_cast<UScriptCode>(fantasyFont.key));
125     m_fantasyFontFamilies.clear();
126
127     for (const auto& pictographFont : m_pictographFontFamilies)
128         settings.setPictographFontFamily(pictographFont.value, static_cast<UScriptCode>(pictographFont.key));
129     m_pictographFontFamilies.clear();
130
131 #if ENABLE(TEXT_AUTOSIZING)
132     settings.setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
133     settings.setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
134     settings.setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
135 #endif
136     settings.setMediaTypeOverride(m_originalMediaTypeOverride);
137     settings.setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
138     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
139     settings.setImagesEnabled(m_imagesEnabled);
140     settings.setMinDOMTimerInterval(m_minimumTimerInterval);
141 #if ENABLE(VIDEO_TRACK)
142     settings.setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
143     settings.setShouldDisplayCaptions(m_shouldDisplayCaptions);
144     settings.setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
145 #endif
146     settings.setDefaultVideoPosterURL(m_defaultVideoPosterURL);
147     settings.setTimeWithoutMouseMovementBeforeHidingControls(m_originalTimeWithoutMouseMovementBeforeHidingControls);
148     settings.setUseLegacyBackgroundSizeShorthandBehavior(m_useLegacyBackgroundSizeShorthandBehavior);
149     settings.setAutoscrollForDragAndDropEnabled(m_autoscrollForDragAndDropEnabled);
150     settings.setShouldConvertPositionStyleOnCopy(m_shouldConvertPositionStyleOnCopy);
151     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(m_pluginReplacementEnabled);
152 }
153
154 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
155 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
156 // Instead, we manually make InternalSettings supplement Page.
157 class InternalSettingsWrapper : public Supplement<Page> {
158 public:
159     explicit InternalSettingsWrapper(Page* page)
160         : m_internalSettings(InternalSettings::create(page)) { }
161     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
162 #if !ASSERT_DISABLED
163     virtual bool isRefCountedWrapper() const override { return true; }
164 #endif
165     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
166
167 private:
168     RefPtr<InternalSettings> m_internalSettings;
169 };
170
171 const char* InternalSettings::supplementName()
172 {
173     return "InternalSettings";
174 }
175
176 InternalSettings* InternalSettings::from(Page* page)
177 {
178     if (!Supplement<Page>::from(page, supplementName()))
179         Supplement<Page>::provideTo(page, supplementName(), std::make_unique<InternalSettingsWrapper>(page));
180     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
181 }
182
183 InternalSettings::~InternalSettings()
184 {
185 }
186
187 InternalSettings::InternalSettings(Page* page)
188     : InternalSettingsGenerated(page)
189     , m_page(page)
190     , m_backup(page->settings())
191 {
192 }
193
194 void InternalSettings::resetToConsistentState()
195 {
196     page()->setPageScaleFactor(1, IntPoint(0, 0));
197     page()->setCanStartMedia(true);
198
199     m_backup.restoreTo(*settings());
200     m_backup = Backup(*settings());
201
202     InternalSettingsGenerated::resetToConsistentState();
203 }
204
205 Settings* InternalSettings::settings() const
206 {
207     if (!page())
208         return 0;
209     return &page()->settings();
210 }
211
212 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
213 {
214 #if ENABLE(TOUCH_EVENTS)
215     InternalSettingsGuardForSettings();
216     settings()->setTouchEventEmulationEnabled(enabled);
217 #else
218     UNUSED_PARAM(enabled);
219     UNUSED_PARAM(ec);
220 #endif
221 }
222
223 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
224 {
225     InternalSettingsGuardForSettings();
226     UScriptCode code = scriptNameToCode(script);
227     if (code == USCRIPT_INVALID_CODE)
228         return;
229     m_backup.m_standardFontFamilies.add(code, settings()->standardFontFamily(code));
230     settings()->setStandardFontFamily(family, code);
231 }
232
233 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
234 {
235     InternalSettingsGuardForSettings();
236     UScriptCode code = scriptNameToCode(script);
237     if (code == USCRIPT_INVALID_CODE)
238         return;
239     m_backup.m_serifFontFamilies.add(code, settings()->serifFontFamily(code));
240     settings()->setSerifFontFamily(family, code);
241 }
242
243 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
244 {
245     InternalSettingsGuardForSettings();
246     UScriptCode code = scriptNameToCode(script);
247     if (code == USCRIPT_INVALID_CODE)
248         return;
249     m_backup.m_sansSerifFontFamilies.add(code, settings()->sansSerifFontFamily(code));
250     settings()->setSansSerifFontFamily(family, code);
251 }
252
253 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
254 {
255     InternalSettingsGuardForSettings();
256     UScriptCode code = scriptNameToCode(script);
257     if (code == USCRIPT_INVALID_CODE)
258         return;
259     m_backup.m_fixedFontFamilies.add(code, settings()->fixedFontFamily(code));
260     settings()->setFixedFontFamily(family, code);
261 }
262
263 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
264 {
265     InternalSettingsGuardForSettings();
266     UScriptCode code = scriptNameToCode(script);
267     if (code == USCRIPT_INVALID_CODE)
268         return;
269     m_backup.m_cursiveFontFamilies.add(code, settings()->cursiveFontFamily(code));
270     settings()->setCursiveFontFamily(family, code);
271 }
272
273 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
274 {
275     InternalSettingsGuardForSettings();
276     UScriptCode code = scriptNameToCode(script);
277     if (code == USCRIPT_INVALID_CODE)
278         return;
279     m_backup.m_fantasyFontFamilies.add(code, settings()->fantasyFontFamily(code));
280     settings()->setFantasyFontFamily(family, code);
281 }
282
283 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
284 {
285     InternalSettingsGuardForSettings();
286     UScriptCode code = scriptNameToCode(script);
287     if (code == USCRIPT_INVALID_CODE)
288         return;
289     m_backup.m_pictographFontFamilies.add(code, settings()->pictographFontFamily(code));
290     settings()->setPictographFontFamily(family, code);
291 }
292
293 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
294 {
295 #if ENABLE(TEXT_AUTOSIZING)
296     InternalSettingsGuardForSettings();
297     settings()->setTextAutosizingEnabled(enabled);
298 #else
299     UNUSED_PARAM(enabled);
300     UNUSED_PARAM(ec);
301 #endif
302 }
303
304 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
305 {
306 #if ENABLE(TEXT_AUTOSIZING)
307     InternalSettingsGuardForSettings();
308     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
309 #else
310     UNUSED_PARAM(width);
311     UNUSED_PARAM(height);
312     UNUSED_PARAM(ec);
313 #endif
314 }
315
316 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
317 {
318     InternalSettingsGuardForSettings();
319     settings()->setMediaTypeOverride(mediaType);
320 }
321
322 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
323 {
324 #if ENABLE(TEXT_AUTOSIZING)
325     InternalSettingsGuardForSettings();
326     settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
327 #else
328     UNUSED_PARAM(fontScaleFactor);
329     UNUSED_PARAM(ec);
330 #endif
331 }
332
333 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
334 {
335     UNUSED_PARAM(ec);
336     RuntimeEnabledFeatures::sharedFeatures().setCSSExclusionsEnabled(enabled);
337 }
338
339 void InternalSettings::setCSSShapesEnabled(bool enabled, ExceptionCode& ec)
340 {
341     UNUSED_PARAM(ec);
342     RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(enabled);
343 }
344
345 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
346 {
347     InternalSettingsGuardForSettings();
348     m_page->setCanStartMedia(enabled);
349 }
350
351 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
352 {
353     InternalSettingsGuardForSettings();
354     if (equalIgnoringCase(editingBehavior, "win"))
355         settings()->setEditingBehaviorType(EditingWindowsBehavior);
356     else if (equalIgnoringCase(editingBehavior, "mac"))
357         settings()->setEditingBehaviorType(EditingMacBehavior);
358     else if (equalIgnoringCase(editingBehavior, "unix"))
359         settings()->setEditingBehaviorType(EditingUnixBehavior);
360     else
361         ec = SYNTAX_ERR;
362 }
363
364 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
365 {
366     InternalSettingsGuardForSettings();
367
368 #if ENABLE(VIDEO_TRACK)
369     if (!page())
370         return;
371     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
372
373     if (equalIgnoringCase(kind, "Subtitles"))
374         captionPreferences->setUserPrefersSubtitles(enabled);
375     else if (equalIgnoringCase(kind, "Captions"))
376         captionPreferences->setUserPrefersCaptions(enabled);
377     else if (equalIgnoringCase(kind, "TextDescriptions"))
378         captionPreferences->setUserPrefersTextDescriptions(enabled);
379     else
380         ec = SYNTAX_ERR;
381 #else
382     UNUSED_PARAM(kind);
383     UNUSED_PARAM(enabled);
384 #endif
385 }
386
387 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
388 {
389     InternalSettingsGuardForSettingsReturn(false);
390
391 #if ENABLE(VIDEO_TRACK)
392     if (!page())
393         return false;
394     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
395
396     if (equalIgnoringCase(kind, "Subtitles"))
397         return captionPreferences->userPrefersSubtitles();
398     if (equalIgnoringCase(kind, "Captions"))
399         return captionPreferences->userPrefersCaptions();
400     if (equalIgnoringCase(kind, "TextDescriptions"))
401         return captionPreferences->userPrefersTextDescriptions();
402
403     ec = SYNTAX_ERR;
404     return false;
405 #else
406     UNUSED_PARAM(kind);
407     return false;
408 #endif
409 }
410
411 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
412 {
413     InternalSettingsGuardForSettings();
414
415     if (mode == "AllowAll")
416         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
417     else if (mode == "BlockThirdParty")
418         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
419     else if (mode == "BlockAll")
420         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
421     else
422         ec = SYNTAX_ERR;
423 }
424
425 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
426 {
427     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(enabled);
428 }
429
430 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
431 {
432     InternalSettingsGuardForSettings();
433     settings()->setImagesEnabled(enabled);
434 }
435
436 void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
437 {
438     InternalSettingsGuardForSettings();
439     settings()->setMinDOMTimerInterval(intervalInSeconds);
440 }
441
442 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
443 {
444     InternalSettingsGuardForSettings();
445     settings()->setDefaultVideoPosterURL(url);
446 }
447
448 void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
449 {
450     InternalSettingsGuardForSettings();
451     settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
452 }
453
454 void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
455 {
456     InternalSettingsGuardForSettings();
457     settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
458 }
459
460 void InternalSettings::setAutoscrollForDragAndDropEnabled(bool enabled, ExceptionCode& ec)
461 {
462     InternalSettingsGuardForSettings();
463     settings()->setAutoscrollForDragAndDropEnabled(enabled);
464 }
465
466 void InternalSettings::setFontFallbackPrefersPictographs(bool preferPictographs, ExceptionCode& ec)
467 {
468     InternalSettingsGuardForSettings();
469     settings()->setFontFallbackPrefersPictographs(preferPictographs);
470 }
471
472 void InternalSettings::setPluginReplacementEnabled(bool enabled)
473 {
474     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(enabled);
475 }
476
477 void InternalSettings::setBackgroundShouldExtendBeyondPage(bool hasExtendedBackground, ExceptionCode& ec)
478 {
479     InternalSettingsGuardForSettings();
480     settings()->setBackgroundShouldExtendBeyondPage(hasExtendedBackground);
481 }
482
483 void InternalSettings::setShouldConvertPositionStyleOnCopy(bool convert, ExceptionCode& ec)
484 {
485     InternalSettingsGuardForSettings();
486     settings()->setShouldConvertPositionStyleOnCopy(convert);
487 }
488
489 void InternalSettings::setScrollingTreeIncludesFrames(bool enabled, ExceptionCode& ec)
490 {
491     InternalSettingsGuardForSettings();
492     settings()->setScrollingTreeIncludesFrames(enabled);
493 }
494
495 }