Another round of removing use of OwnPtr, PassOwnPtr, and deleteOwnedPtr
[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_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.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().setMediaPlaybackAllowsAirPlay(false);
206 #endif
207 }
208
209 void InternalSettings::resetToConsistentState()
210 {
211     page()->setPageScaleFactor(1, IntPoint(0, 0));
212     page()->setCanStartMedia(true);
213
214     m_backup.restoreTo(*settings());
215     m_backup = Backup(*settings());
216
217     InternalSettingsGenerated::resetToConsistentState();
218 }
219
220 Settings* InternalSettings::settings() const
221 {
222     if (!page())
223         return 0;
224     return &page()->settings();
225 }
226
227 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
228 {
229 #if ENABLE(TOUCH_EVENTS)
230     InternalSettingsGuardForSettings();
231     settings()->setTouchEventEmulationEnabled(enabled);
232 #else
233     UNUSED_PARAM(enabled);
234     UNUSED_PARAM(ec);
235 #endif
236 }
237
238 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
239 {
240     InternalSettingsGuardForSettings();
241     UScriptCode code = scriptNameToCode(script);
242     if (code == USCRIPT_INVALID_CODE)
243         return;
244     m_backup.m_standardFontFamilies.add(code, settings()->standardFontFamily(code));
245     settings()->setStandardFontFamily(family, code);
246 }
247
248 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
249 {
250     InternalSettingsGuardForSettings();
251     UScriptCode code = scriptNameToCode(script);
252     if (code == USCRIPT_INVALID_CODE)
253         return;
254     m_backup.m_serifFontFamilies.add(code, settings()->serifFontFamily(code));
255     settings()->setSerifFontFamily(family, code);
256 }
257
258 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
259 {
260     InternalSettingsGuardForSettings();
261     UScriptCode code = scriptNameToCode(script);
262     if (code == USCRIPT_INVALID_CODE)
263         return;
264     m_backup.m_sansSerifFontFamilies.add(code, settings()->sansSerifFontFamily(code));
265     settings()->setSansSerifFontFamily(family, code);
266 }
267
268 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
269 {
270     InternalSettingsGuardForSettings();
271     UScriptCode code = scriptNameToCode(script);
272     if (code == USCRIPT_INVALID_CODE)
273         return;
274     m_backup.m_fixedFontFamilies.add(code, settings()->fixedFontFamily(code));
275     settings()->setFixedFontFamily(family, code);
276 }
277
278 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
279 {
280     InternalSettingsGuardForSettings();
281     UScriptCode code = scriptNameToCode(script);
282     if (code == USCRIPT_INVALID_CODE)
283         return;
284     m_backup.m_cursiveFontFamilies.add(code, settings()->cursiveFontFamily(code));
285     settings()->setCursiveFontFamily(family, code);
286 }
287
288 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
289 {
290     InternalSettingsGuardForSettings();
291     UScriptCode code = scriptNameToCode(script);
292     if (code == USCRIPT_INVALID_CODE)
293         return;
294     m_backup.m_fantasyFontFamilies.add(code, settings()->fantasyFontFamily(code));
295     settings()->setFantasyFontFamily(family, code);
296 }
297
298 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
299 {
300     InternalSettingsGuardForSettings();
301     UScriptCode code = scriptNameToCode(script);
302     if (code == USCRIPT_INVALID_CODE)
303         return;
304     m_backup.m_pictographFontFamilies.add(code, settings()->pictographFontFamily(code));
305     settings()->setPictographFontFamily(family, code);
306 }
307
308 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
309 {
310 #if ENABLE(TEXT_AUTOSIZING)
311     InternalSettingsGuardForSettings();
312     settings()->setTextAutosizingEnabled(enabled);
313 #else
314     UNUSED_PARAM(enabled);
315     UNUSED_PARAM(ec);
316 #endif
317 }
318
319 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
320 {
321 #if ENABLE(TEXT_AUTOSIZING)
322     InternalSettingsGuardForSettings();
323     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
324 #else
325     UNUSED_PARAM(width);
326     UNUSED_PARAM(height);
327     UNUSED_PARAM(ec);
328 #endif
329 }
330
331 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
332 {
333     InternalSettingsGuardForSettings();
334     settings()->setMediaTypeOverride(mediaType);
335 }
336
337 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
338 {
339 #if ENABLE(TEXT_AUTOSIZING)
340     InternalSettingsGuardForSettings();
341     settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
342 #else
343     UNUSED_PARAM(fontScaleFactor);
344     UNUSED_PARAM(ec);
345 #endif
346 }
347
348 void InternalSettings::setCSSShapesEnabled(bool enabled, ExceptionCode& ec)
349 {
350     UNUSED_PARAM(ec);
351     RuntimeEnabledFeatures::sharedFeatures().setCSSShapesEnabled(enabled);
352 }
353
354 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
355 {
356     InternalSettingsGuardForSettings();
357     m_page->setCanStartMedia(enabled);
358 }
359
360 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
361 {
362     InternalSettingsGuardForSettings();
363     if (equalIgnoringCase(editingBehavior, "win"))
364         settings()->setEditingBehaviorType(EditingWindowsBehavior);
365     else if (equalIgnoringCase(editingBehavior, "mac"))
366         settings()->setEditingBehaviorType(EditingMacBehavior);
367     else if (equalIgnoringCase(editingBehavior, "unix"))
368         settings()->setEditingBehaviorType(EditingUnixBehavior);
369     else if (equalIgnoringCase(editingBehavior, "ios"))
370         settings()->setEditingBehaviorType(EditingIOSBehavior);
371     else
372         ec = SYNTAX_ERR;
373 }
374
375 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
376 {
377     InternalSettingsGuardForSettings();
378
379 #if ENABLE(VIDEO_TRACK)
380     if (!page())
381         return;
382     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
383
384     if (equalIgnoringCase(kind, "Subtitles"))
385         captionPreferences->setUserPrefersSubtitles(enabled);
386     else if (equalIgnoringCase(kind, "Captions"))
387         captionPreferences->setUserPrefersCaptions(enabled);
388     else if (equalIgnoringCase(kind, "TextDescriptions"))
389         captionPreferences->setUserPrefersTextDescriptions(enabled);
390     else
391         ec = SYNTAX_ERR;
392 #else
393     UNUSED_PARAM(kind);
394     UNUSED_PARAM(enabled);
395 #endif
396 }
397
398 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
399 {
400     InternalSettingsGuardForSettingsReturn(false);
401
402 #if ENABLE(VIDEO_TRACK)
403     if (!page())
404         return false;
405     CaptionUserPreferences* captionPreferences = page()->group().captionPreferences();
406
407     if (equalIgnoringCase(kind, "Subtitles"))
408         return captionPreferences->userPrefersSubtitles();
409     if (equalIgnoringCase(kind, "Captions"))
410         return captionPreferences->userPrefersCaptions();
411     if (equalIgnoringCase(kind, "TextDescriptions"))
412         return captionPreferences->userPrefersTextDescriptions();
413
414     ec = SYNTAX_ERR;
415     return false;
416 #else
417     UNUSED_PARAM(kind);
418     return false;
419 #endif
420 }
421
422 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
423 {
424     InternalSettingsGuardForSettings();
425
426     if (mode == "AllowAll")
427         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
428     else if (mode == "BlockThirdParty")
429         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
430     else if (mode == "BlockAll")
431         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
432     else
433         ec = SYNTAX_ERR;
434 }
435
436 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
437 {
438     RuntimeEnabledFeatures::sharedFeatures().setLangAttributeAwareFormControlUIEnabled(enabled);
439 }
440
441 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
442 {
443     InternalSettingsGuardForSettings();
444     settings()->setImagesEnabled(enabled);
445 }
446
447 void InternalSettings::setMinimumTimerInterval(double intervalInSeconds, ExceptionCode& ec)
448 {
449     InternalSettingsGuardForSettings();
450     settings()->setMinimumDOMTimerInterval(intervalInSeconds);
451 }
452
453 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionCode& ec)
454 {
455     InternalSettingsGuardForSettings();
456     settings()->setDefaultVideoPosterURL(url);
457 }
458
459 void InternalSettings::setTimeWithoutMouseMovementBeforeHidingControls(double time, ExceptionCode& ec)
460 {
461     InternalSettingsGuardForSettings();
462     settings()->setTimeWithoutMouseMovementBeforeHidingControls(time);
463 }
464
465 void InternalSettings::setUseLegacyBackgroundSizeShorthandBehavior(bool enabled, ExceptionCode& ec)
466 {
467     InternalSettingsGuardForSettings();
468     settings()->setUseLegacyBackgroundSizeShorthandBehavior(enabled);
469 }
470
471 void InternalSettings::setAutoscrollForDragAndDropEnabled(bool enabled, ExceptionCode& ec)
472 {
473     InternalSettingsGuardForSettings();
474     settings()->setAutoscrollForDragAndDropEnabled(enabled);
475 }
476
477 void InternalSettings::setFontFallbackPrefersPictographs(bool preferPictographs, ExceptionCode& ec)
478 {
479     InternalSettingsGuardForSettings();
480     settings()->setFontFallbackPrefersPictographs(preferPictographs);
481 }
482
483 void InternalSettings::setPluginReplacementEnabled(bool enabled)
484 {
485     RuntimeEnabledFeatures::sharedFeatures().setPluginReplacementEnabled(enabled);
486 }
487
488 void InternalSettings::setBackgroundShouldExtendBeyondPage(bool hasExtendedBackground, ExceptionCode& ec)
489 {
490     InternalSettingsGuardForSettings();
491     settings()->setBackgroundShouldExtendBeyondPage(hasExtendedBackground);
492 }
493
494 void InternalSettings::setShouldConvertPositionStyleOnCopy(bool convert, ExceptionCode& ec)
495 {
496     InternalSettingsGuardForSettings();
497     settings()->setShouldConvertPositionStyleOnCopy(convert);
498 }
499
500 void InternalSettings::setScrollingTreeIncludesFrames(bool enabled, ExceptionCode& ec)
501 {
502     InternalSettingsGuardForSettings();
503     settings()->setScrollingTreeIncludesFrames(enabled);
504 }
505
506 // If you add to this list, make sure that you update the Backup class for test reproducability!
507
508 }