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