Rename MediaPlaybackAllowsAirPlay
[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.minimumDOMTimerInterval())
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     , m_fontFallbackPrefersPictographs(settings.fontFallbackPrefersPictographs())
94     , m_backgroundShouldExtendBeyondPage(settings.backgroundShouldExtendBeyondPage())
95     , m_storageBlockingPolicy(settings.storageBlockingPolicy())
96     , m_scrollingTreeIncludesFrames(settings.scrollingTreeIncludesFrames())
97 #if ENABLE(TOUCH_EVENTS)
98     , m_touchEventEmulationEnabled(settings.isTouchEventEmulationEnabled())
99 #endif
100 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
101     , m_allowsAirPlayForMediaPlayback(settings.allowsAirPlayForMediaPlayback())
102 #endif
103 {
104 }
105
106 void InternalSettings::Backup::restoreTo(Settings& settings)
107 {
108     RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(m_originalCSSShapesEnabled);
109     settings.setEditingBehaviorType(m_originalEditingBehavior);
110
111     for (const auto& standardFont : m_standardFontFamilies)
112         settings.setStandardFontFamily(standardFont.value, static_cast<UScriptCode>(standardFont.key));
113     m_standardFontFamilies.clear();
114
115     for (const auto& fixedFont : m_fixedFontFamilies)
116         settings.setFixedFontFamily(fixedFont.value, static_cast<UScriptCode>(fixedFont.key));
117     m_fixedFontFamilies.clear();
118
119     for (const auto& serifFont : m_serifFontFamilies)
120         settings.setSerifFontFamily(serifFont.value, static_cast<UScriptCode>(serifFont.key));
121     m_serifFontFamilies.clear();
122
123     for (const auto& sansSerifFont : m_sansSerifFontFamilies)
124         settings.setSansSerifFontFamily(sansSerifFont.value, static_cast<UScriptCode>(sansSerifFont.key));
125     m_sansSerifFontFamilies.clear();
126
127     for (const auto& cursiveFont : m_cursiveFontFamilies)
128         settings.setCursiveFontFamily(cursiveFont.value, static_cast<UScriptCode>(cursiveFont.key));
129     m_cursiveFontFamilies.clear();
130
131     for (const auto& fantasyFont : m_fantasyFontFamilies)
132         settings.setFantasyFontFamily(fantasyFont.value, static_cast<UScriptCode>(fantasyFont.key));
133     m_fantasyFontFamilies.clear();
134
135     for (const auto& pictographFont : m_pictographFontFamilies)
136         settings.setPictographFontFamily(pictographFont.value, static_cast<UScriptCode>(pictographFont.key));
137     m_pictographFontFamilies.clear();
138
139 #if ENABLE(TEXT_AUTOSIZING)
140     settings.setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
141     settings.setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
142     settings.setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
143 #endif
144     settings.setMediaTypeOverride(m_originalMediaTypeOverride);
145     settings.setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
146     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
147     settings.setImagesEnabled(m_imagesEnabled);
148     settings.setMinimumDOMTimerInterval(m_minimumTimerInterval);
149 #if ENABLE(VIDEO_TRACK)
150     settings.setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
151     settings.setShouldDisplayCaptions(m_shouldDisplayCaptions);
152     settings.setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
153 #endif
154     settings.setDefaultVideoPosterURL(m_defaultVideoPosterURL);
155     settings.setTimeWithoutMouseMovementBeforeHidingControls(m_originalTimeWithoutMouseMovementBeforeHidingControls);
156     settings.setUseLegacyBackgroundSizeShorthandBehavior(m_useLegacyBackgroundSizeShorthandBehavior);
157     settings.setAutoscrollForDragAndDropEnabled(m_autoscrollForDragAndDropEnabled);
158     settings.setShouldConvertPositionStyleOnCopy(m_shouldConvertPositionStyleOnCopy);
159     settings.setFontFallbackPrefersPictographs(m_fontFallbackPrefersPictographs);
160     settings.setBackgroundShouldExtendBeyondPage(m_backgroundShouldExtendBeyondPage);
161     settings.setStorageBlockingPolicy(m_storageBlockingPolicy);
162     settings.setScrollingTreeIncludesFrames(m_scrollingTreeIncludesFrames);
163 #if ENABLE(TOUCH_EVENTS)
164     settings.setTouchEventEmulationEnabled(m_touchEventEmulationEnabled);
165 #endif
166     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(m_pluginReplacementEnabled);
167 }
168
169 class InternalSettingsWrapper : public Supplement<Page> {
170 public:
171     explicit InternalSettingsWrapper(Page* page)
172         : m_internalSettings(InternalSettings::create(page)) { }
173     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
174 #if !ASSERT_DISABLED
175     virtual bool isRefCountedWrapper() const override { return true; }
176 #endif
177     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
178
179 private:
180     RefPtr<InternalSettings> m_internalSettings;
181 };
182
183 const char* InternalSettings::supplementName()
184 {
185     return "InternalSettings";
186 }
187
188 InternalSettings* InternalSettings::from(Page* page)
189 {
190     if (!Supplement<Page>::from(page, supplementName()))
191         Supplement<Page>::provideTo(page, supplementName(), std::make_unique<InternalSettingsWrapper>(page));
192     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
193 }
194
195 InternalSettings::~InternalSettings()
196 {
197 }
198
199 InternalSettings::InternalSettings(Page* page)
200     : InternalSettingsGenerated(page)
201     , m_page(page)
202     , m_backup(page->settings())
203 {
204 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
205     page->settings().setAllowsAirPlayForMediaPlayback(false);
206 #endif
207 }
208
209 void InternalSettings::resetToConsistentState()
210 {
211     page()->setPageScaleFactor(1, IntPoint(0, 0));
212     page()->setCanStartMedia(true);
213 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
214     m_page->settings().setAllowsAirPlayForMediaPlayback(false);
215 #endif
216
217     m_backup.restoreTo(*settings());
218     m_backup = Backup(*settings());
219
220     InternalSettingsGenerated::resetToConsistentState();
221 }
222
223 Settings* InternalSettings::settings() const
224 {
225     if (!page())
226         return 0;
227     return &page()->settings();
228 }
229
230 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
231 {
232 #if ENABLE(TOUCH_EVENTS)
233     InternalSettingsGuardForSettings();
234     settings()->setTouchEventEmulationEnabled(enabled);
235 #else
236     UNUSED_PARAM(enabled);
237     UNUSED_PARAM(ec);
238 #endif
239 }
240
241 void InternalSettings::setStandardFontFamily(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_standardFontFamilies.add(code, settings()->standardFontFamily(code));
248     settings()->setStandardFontFamily(family, code);
249 }
250
251 void InternalSettings::setSerifFontFamily(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_serifFontFamilies.add(code, settings()->serifFontFamily(code));
258     settings()->setSerifFontFamily(family, code);
259 }
260
261 void InternalSettings::setSansSerifFontFamily(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_sansSerifFontFamilies.add(code, settings()->sansSerifFontFamily(code));
268     settings()->setSansSerifFontFamily(family, code);
269 }
270
271 void InternalSettings::setFixedFontFamily(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_fixedFontFamilies.add(code, settings()->fixedFontFamily(code));
278     settings()->setFixedFontFamily(family, code);
279 }
280
281 void InternalSettings::setCursiveFontFamily(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_cursiveFontFamilies.add(code, settings()->cursiveFontFamily(code));
288     settings()->setCursiveFontFamily(family, code);
289 }
290
291 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
292 {
293     InternalSettingsGuardForSettings();
294     UScriptCode code = scriptNameToCode(script);
295     if (code == USCRIPT_INVALID_CODE)
296         return;
297     m_backup.m_fantasyFontFamilies.add(code, settings()->fantasyFontFamily(code));
298     settings()->setFantasyFontFamily(family, code);
299 }
300
301 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
302 {
303     InternalSettingsGuardForSettings();
304     UScriptCode code = scriptNameToCode(script);
305     if (code == USCRIPT_INVALID_CODE)
306         return;
307     m_backup.m_pictographFontFamilies.add(code, settings()->pictographFontFamily(code));
308     settings()->setPictographFontFamily(family, code);
309 }
310
311 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
312 {
313 #if ENABLE(TEXT_AUTOSIZING)
314     InternalSettingsGuardForSettings();
315     settings()->setTextAutosizingEnabled(enabled);
316 #else
317     UNUSED_PARAM(enabled);
318     UNUSED_PARAM(ec);
319 #endif
320 }
321
322 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
323 {
324 #if ENABLE(TEXT_AUTOSIZING)
325     InternalSettingsGuardForSettings();
326     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
327 #else
328     UNUSED_PARAM(width);
329     UNUSED_PARAM(height);
330     UNUSED_PARAM(ec);
331 #endif
332 }
333
334 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
335 {
336     InternalSettingsGuardForSettings();
337     settings()->setMediaTypeOverride(mediaType);
338 }
339
340 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
341 {
342 #if ENABLE(TEXT_AUTOSIZING)
343     InternalSettingsGuardForSettings();
344     settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
345 #else
346     UNUSED_PARAM(fontScaleFactor);
347     UNUSED_PARAM(ec);
348 #endif
349 }
350
351 void InternalSettings::setCSSShapesEnabled(bool enabled, ExceptionCode& ec)
352 {
353     UNUSED_PARAM(ec);
354     RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(enabled);
355 }
356
357 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
358 {
359     InternalSettingsGuardForSettings();
360     m_page->setCanStartMedia(enabled);
361 }
362
363 void InternalSettings::setWirelessPlaybackDisabled(bool available)
364 {
365 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
366     m_page->settings().setAllowsAirPlayForMediaPlayback(available);
367 #else
368     UNUSED_PARAM(available);
369 #endif
370 }
371
372 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
373 {
374     InternalSettingsGuardForSettings();
375     if (equalIgnoringCase(editingBehavior, "win"))
376         settings()->setEditingBehaviorType(EditingWindowsBehavior);
377     else if (equalIgnoringCase(editingBehavior, "mac"))
378         settings()->setEditingBehaviorType(EditingMacBehavior);
379     else if (equalIgnoringCase(editingBehavior, "unix"))
380         settings()->setEditingBehaviorType(EditingUnixBehavior);
381     else if (equalIgnoringCase(editingBehavior, "ios"))
382         settings()->setEditingBehaviorType(EditingIOSBehavior);
383     else
384         ec = SYNTAX_ERR;
385 }
386
387 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
388 {
389     InternalSettingsGuardForSettings();
390
391 #if ENABLE(VIDEO_TRACK)
392     if (!page())
393         return;
394     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
395
396     if (equalIgnoringCase(kind, "Subtitles"))
397         captionPreferences->setUserPrefersSubtitles(enabled);
398     else if (equalIgnoringCase(kind, "Captions"))
399         captionPreferences->setUserPrefersCaptions(enabled);
400     else if (equalIgnoringCase(kind, "TextDescriptions"))
401         captionPreferences->setUserPrefersTextDescriptions(enabled);
402     else
403         ec = SYNTAX_ERR;
404 #else
405     UNUSED_PARAM(kind);
406     UNUSED_PARAM(enabled);
407 #endif
408 }
409
410 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
411 {
412     InternalSettingsGuardForSettingsReturn(false);
413
414 #if ENABLE(VIDEO_TRACK)
415     if (!page())
416         return false;
417     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
418
419     if (equalIgnoringCase(kind, "Subtitles"))
420         return captionPreferences->userPrefersSubtitles();
421     if (equalIgnoringCase(kind, "Captions"))
422         return captionPreferences->userPrefersCaptions();
423     if (equalIgnoringCase(kind, "TextDescriptions"))
424         return captionPreferences->userPrefersTextDescriptions();
425
426     ec = SYNTAX_ERR;
427     return false;
428 #else
429     UNUSED_PARAM(kind);
430     return false;
431 #endif
432 }
433
434 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
435 {
436     InternalSettingsGuardForSettings();
437
438     if (mode == "AllowAll")
439         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
440     else if (mode == "BlockThirdParty")
441         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
442     else if (mode == "BlockAll")
443         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
444     else
445         ec = SYNTAX_ERR;
446 }
447
448 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
449 {
450     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(enabled);
451 }
452
453 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
454 {
455     InternalSettingsGuardForSettings();
456     settings()->setImagesEnabled(enabled);
457 }
458
459 void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
460 {
461     InternalSettingsGuardForSettings();
462     settings()->setMinimumDOMTimerInterval(intervalInSeconds);
463 }
464
465 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
466 {
467     InternalSettingsGuardForSettings();
468     settings()->setDefaultVideoPosterURL(url);
469 }
470
471 void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
472 {
473     InternalSettingsGuardForSettings();
474     settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
475 }
476
477 void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
478 {
479     InternalSettingsGuardForSettings();
480     settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
481 }
482
483 void InternalSettings::setAutoscrollForDragAndDropEnabled(bool enabled, ExceptionCode& ec)
484 {
485     InternalSettingsGuardForSettings();
486     settings()->setAutoscrollForDragAndDropEnabled(enabled);
487 }
488
489 void InternalSettings::setFontFallbackPrefersPictographs(bool preferPictographs, ExceptionCode& ec)
490 {
491     InternalSettingsGuardForSettings();
492     settings()->setFontFallbackPrefersPictographs(preferPictographs);
493 }
494
495 void InternalSettings::setPluginReplacementEnabled(bool enabled)
496 {
497     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(enabled);
498 }
499
500 void InternalSettings::setBackgroundShouldExtendBeyondPage(bool hasExtendedBackground, ExceptionCode& ec)
501 {
502     InternalSettingsGuardForSettings();
503     settings()->setBackgroundShouldExtendBeyondPage(hasExtendedBackground);
504 }
505
506 void InternalSettings::setShouldConvertPositionStyleOnCopy(bool convert, ExceptionCode& ec)
507 {
508     InternalSettingsGuardForSettings();
509     settings()->setShouldConvertPositionStyleOnCopy(convert);
510 }
511
512 void InternalSettings::setScrollingTreeIncludesFrames(bool enabled, ExceptionCode& ec)
513 {
514     InternalSettingsGuardForSettings();
515     settings()->setScrollingTreeIncludesFrames(enabled);
516 }
517
518 // If you add to this list, make sure that you update the Backup class for test reproducability!
519
520 }