[Mac] Update AirPlay handling
[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     , 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_mediaPlaybackAllowsAirPlay(settings.mediaPlaybackAllowsAirPlay())
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.setMinDOMTimerInterval(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 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
170 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
171 // Instead, we manually make InternalSettings supplement Page.
172 class InternalSettingsWrapper : public Supplement<Page> {
173 public:
174     explicit InternalSettingsWrapper(Page* page)
175         : m_internalSettings(InternalSettings::create(page)) { }
176     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
177 #if !ASSERT_DISABLED
178     virtual bool isRefCountedWrapper() const override { return true; }
179 #endif
180     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
181
182 private:
183     RefPtr<InternalSettings> m_internalSettings;
184 };
185
186 const char* InternalSettings::supplementName()
187 {
188     return "InternalSettings";
189 }
190
191 InternalSettings* InternalSettings::from(Page* page)
192 {
193     if (!Supplement<Page>::from(page, supplementName()))
194         Supplement<Page>::provideTo(page, supplementName(), std::make_unique<InternalSettingsWrapper>(page));
195     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
196 }
197
198 InternalSettings::~InternalSettings()
199 {
200 }
201
202 InternalSettings::InternalSettings(Page* page)
203     : InternalSettingsGenerated(page)
204     , m_page(page)
205     , m_backup(page->settings())
206 {
207 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
208     page->settings().setMediaPlaybackAllowsAirPlay(false);
209 #endif
210 }
211
212 void InternalSettings::resetToConsistentState()
213 {
214     page()->setPageScaleFactor(1, IntPoint(0, 0));
215     page()->setCanStartMedia(true);
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::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
364 {
365     InternalSettingsGuardForSettings();
366     if (equalIgnoringCase(editingBehavior, "win"))
367         settings()->setEditingBehaviorType(EditingWindowsBehavior);
368     else if (equalIgnoringCase(editingBehavior, "mac"))
369         settings()->setEditingBehaviorType(EditingMacBehavior);
370     else if (equalIgnoringCase(editingBehavior, "unix"))
371         settings()->setEditingBehaviorType(EditingUnixBehavior);
372     else if (equalIgnoringCase(editingBehavior, "ios"))
373         settings()->setEditingBehaviorType(EditingIOSBehavior);
374     else
375         ec = SYNTAX_ERR;
376 }
377
378 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
379 {
380     InternalSettingsGuardForSettings();
381
382 #if ENABLE(VIDEO_TRACK)
383     if (!page())
384         return;
385     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
386
387     if (equalIgnoringCase(kind, "Subtitles"))
388         captionPreferences->setUserPrefersSubtitles(enabled);
389     else if (equalIgnoringCase(kind, "Captions"))
390         captionPreferences->setUserPrefersCaptions(enabled);
391     else if (equalIgnoringCase(kind, "TextDescriptions"))
392         captionPreferences->setUserPrefersTextDescriptions(enabled);
393     else
394         ec = SYNTAX_ERR;
395 #else
396     UNUSED_PARAM(kind);
397     UNUSED_PARAM(enabled);
398 #endif
399 }
400
401 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
402 {
403     InternalSettingsGuardForSettingsReturn(false);
404
405 #if ENABLE(VIDEO_TRACK)
406     if (!page())
407         return false;
408     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
409
410     if (equalIgnoringCase(kind, "Subtitles"))
411         return captionPreferences->userPrefersSubtitles();
412     if (equalIgnoringCase(kind, "Captions"))
413         return captionPreferences->userPrefersCaptions();
414     if (equalIgnoringCase(kind, "TextDescriptions"))
415         return captionPreferences->userPrefersTextDescriptions();
416
417     ec = SYNTAX_ERR;
418     return false;
419 #else
420     UNUSED_PARAM(kind);
421     return false;
422 #endif
423 }
424
425 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
426 {
427     InternalSettingsGuardForSettings();
428
429     if (mode == "AllowAll")
430         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
431     else if (mode == "BlockThirdParty")
432         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
433     else if (mode == "BlockAll")
434         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
435     else
436         ec = SYNTAX_ERR;
437 }
438
439 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
440 {
441     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(enabled);
442 }
443
444 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
445 {
446     InternalSettingsGuardForSettings();
447     settings()->setImagesEnabled(enabled);
448 }
449
450 void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
451 {
452     InternalSettingsGuardForSettings();
453     settings()->setMinDOMTimerInterval(intervalInSeconds);
454 }
455
456 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
457 {
458     InternalSettingsGuardForSettings();
459     settings()->setDefaultVideoPosterURL(url);
460 }
461
462 void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
463 {
464     InternalSettingsGuardForSettings();
465     settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
466 }
467
468 void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
469 {
470     InternalSettingsGuardForSettings();
471     settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
472 }
473
474 void InternalSettings::setAutoscrollForDragAndDropEnabled(bool enabled, ExceptionCode& ec)
475 {
476     InternalSettingsGuardForSettings();
477     settings()->setAutoscrollForDragAndDropEnabled(enabled);
478 }
479
480 void InternalSettings::setFontFallbackPrefersPictographs(bool preferPictographs, ExceptionCode& ec)
481 {
482     InternalSettingsGuardForSettings();
483     settings()->setFontFallbackPrefersPictographs(preferPictographs);
484 }
485
486 void InternalSettings::setPluginReplacementEnabled(bool enabled)
487 {
488     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(enabled);
489 }
490
491 void InternalSettings::setBackgroundShouldExtendBeyondPage(bool hasExtendedBackground, ExceptionCode& ec)
492 {
493     InternalSettingsGuardForSettings();
494     settings()->setBackgroundShouldExtendBeyondPage(hasExtendedBackground);
495 }
496
497 void InternalSettings::setShouldConvertPositionStyleOnCopy(bool convert, ExceptionCode& ec)
498 {
499     InternalSettingsGuardForSettings();
500     settings()->setShouldConvertPositionStyleOnCopy(convert);
501 }
502
503 void InternalSettings::setScrollingTreeIncludesFrames(bool enabled, ExceptionCode& ec)
504 {
505     InternalSettingsGuardForSettings();
506     settings()->setScrollingTreeIncludesFrames(enabled);
507 }
508
509 // If you add to this list, make sure that you update the Backup class for test reproducability!
510
511 }