Remove some autogenerated settings from InternalSettings.idl
[WebKit-https.git] / Source / WebCore / testing / InternalSettings.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "InternalSettings.h"
28
29 #include "Document.h"
30 #include "ExceptionCode.h"
31 #include "Frame.h"
32 #include "FrameView.h"
33 #include "Language.h"
34 #include "LocaleToScriptMapping.h"
35 #include "Page.h"
36 #include "RuntimeEnabledFeatures.h"
37 #include "Settings.h"
38 #include "Supplementable.h"
39 #include "TextRun.h"
40
41 #if ENABLE(INPUT_TYPE_COLOR)
42 #include "ColorChooser.h"
43 #endif
44
45 #define InternalSettingsGuardForSettingsReturn(returnValue) \
46     if (!settings()) { \
47         ec = INVALID_ACCESS_ERR; \
48         return returnValue; \
49     }
50
51 #define InternalSettingsGuardForSettings()  \
52     if (!settings()) { \
53         ec = INVALID_ACCESS_ERR; \
54         return; \
55     }
56
57 #define InternalSettingsGuardForPage() \
58     if (!page()) { \
59         ec = INVALID_ACCESS_ERR; \
60         return; \
61     }
62
63 namespace WebCore {
64
65 InternalSettings::Backup::Backup(Settings* settings)
66     : m_originalFixedElementsLayoutRelativeToFrame(settings->fixedElementsLayoutRelativeToFrame())
67     , m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::cssExclusionsEnabled())
68     , m_originalCSSVariablesEnabled(settings->cssVariablesEnabled())
69 #if ENABLE(SHADOW_DOM)
70     , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::shadowDOMEnabled())
71     , m_originalAuthorShadowDOMForAnyElementEnabled(RuntimeEnabledFeatures::authorShadowDOMForAnyElementEnabled())
72 #endif
73 #if ENABLE(STYLE_SCOPED)
74     , m_originalStyleScoped(RuntimeEnabledFeatures::styleScopedEnabled())
75 #endif
76     , m_originalEditingBehavior(settings->editingBehaviorType())
77     , m_originalUnifiedSpellCheckerEnabled(settings->unifiedTextCheckerEnabled())
78     , m_originalFixedPositionCreatesStackingContext(settings->fixedPositionCreatesStackingContext())
79     , m_originalSyncXHRInDocumentsEnabled(settings->syncXHRInDocumentsEnabled())
80     , m_originalWindowFocusRestricted(settings->windowFocusRestricted())
81     , m_originalDeviceSupportsTouch(settings->deviceSupportsTouch())
82     , m_originalDeviceSupportsMouse(settings->deviceSupportsMouse())
83 #if ENABLE(TEXT_AUTOSIZING)
84     , m_originalTextAutosizingEnabled(settings->textAutosizingEnabled())
85     , m_originalTextAutosizingWindowSizeOverride(settings->textAutosizingWindowSizeOverride())
86     , m_originalTextAutosizingFontScaleFactor(settings->textAutosizingFontScaleFactor())
87 #endif
88     , m_originalResolutionOverride(settings->resolutionOverride())
89     , m_originalMediaTypeOverride(settings->mediaTypeOverride())
90 #if ENABLE(DIALOG_ELEMENT)
91     , m_originalDialogElementEnabled(RuntimeEnabledFeatures::dialogElementEnabled())
92 #endif
93     , m_originalCanvasUsesAcceleratedDrawing(settings->canvasUsesAcceleratedDrawing())
94     , m_originalMockScrollbarsEnabled(settings->mockScrollbarsEnabled())
95     , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::langAttributeAwareFormControlUIEnabled())
96     , m_imagesEnabled(settings->areImagesEnabled())
97 #if ENABLE(VIDEO_TRACK)
98     , m_shouldDisplaySubtitles(settings->shouldDisplaySubtitles())
99     , m_shouldDisplayCaptions(settings->shouldDisplayCaptions())
100     , m_shouldDisplayTextDescriptions(settings->shouldDisplayTextDescriptions())
101 #endif
102 {
103 }
104
105 void InternalSettings::Backup::restoreTo(Settings* settings)
106 {
107     settings->setFixedElementsLayoutRelativeToFrame(m_originalFixedElementsLayoutRelativeToFrame);
108     RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
109     settings->setCSSVariablesEnabled(m_originalCSSVariablesEnabled);
110 #if ENABLE(SHADOW_DOM)
111     RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
112     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(m_originalAuthorShadowDOMForAnyElementEnabled);
113 #endif
114 #if ENABLE(STYLE_SCOPED)
115     RuntimeEnabledFeatures::setStyleScopedEnabled(m_originalStyleScoped);
116 #endif
117     settings->setEditingBehaviorType(m_originalEditingBehavior);
118     settings->setUnifiedTextCheckerEnabled(m_originalUnifiedSpellCheckerEnabled);
119     settings->setFixedPositionCreatesStackingContext(m_originalFixedPositionCreatesStackingContext);
120     settings->setSyncXHRInDocumentsEnabled(m_originalSyncXHRInDocumentsEnabled);
121     settings->setWindowFocusRestricted(m_originalWindowFocusRestricted);
122     settings->setDeviceSupportsTouch(m_originalDeviceSupportsTouch);
123     settings->setDeviceSupportsMouse(m_originalDeviceSupportsMouse);
124 #if ENABLE(TEXT_AUTOSIZING)
125     settings->setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
126     settings->setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
127     settings->setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
128 #endif
129     settings->setResolutionOverride(m_originalResolutionOverride);
130     settings->setMediaTypeOverride(m_originalMediaTypeOverride);
131 #if ENABLE(DIALOG_ELEMENT)
132     RuntimeEnabledFeatures::setDialogElementEnabled(m_originalDialogElementEnabled);
133 #endif
134     settings->setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
135     settings->setMockScrollbarsEnabled(m_originalMockScrollbarsEnabled);
136     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
137     settings->setImagesEnabled(m_imagesEnabled);
138 #if ENABLE(VIDEO_TRACK)
139     settings->setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
140     settings->setShouldDisplayCaptions(m_shouldDisplayCaptions);
141     settings->setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
142 #endif
143 }
144
145 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
146 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
147 // Instead, we manually make InternalSettings supplement Page.
148 class InternalSettingsWrapper : public Supplement<Page> {
149 public:
150     explicit InternalSettingsWrapper(Page* page)
151         : m_internalSettings(InternalSettings::create(page)) { }
152     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
153 #if !ASSERT_DISABLED
154     virtual bool isRefCountedWrapper() const OVERRIDE { return true; }
155 #endif
156     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
157
158 private:
159     RefPtr<InternalSettings> m_internalSettings;
160 };
161
162 InternalSettings* InternalSettings::from(Page* page)
163 {
164     DEFINE_STATIC_LOCAL(AtomicString, name, ("InternalSettings", AtomicString::ConstructFromLiteral));
165     if (!Supplement<Page>::from(page, name))
166         Supplement<Page>::provideTo(page, name, adoptPtr(new InternalSettingsWrapper(page)));
167     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, name))->internalSettings();
168 }
169
170 InternalSettings::~InternalSettings()
171 {
172 }
173
174 InternalSettings::InternalSettings(Page* page)
175     : InternalSettingsGenerated(page)
176     , m_page(page)
177     , m_backup(page->settings())
178 {
179 }
180
181 void InternalSettings::resetToConsistentState()
182 {
183     page()->setPageScaleFactor(1, IntPoint(0, 0));
184     page()->setCanStartMedia(true);
185
186     m_backup.restoreTo(settings());
187     m_backup = Backup(settings());
188
189     InternalSettingsGenerated::resetToConsistentState();
190 }
191
192 Settings* InternalSettings::settings() const
193 {
194     if (!page())
195         return 0;
196     return page()->settings();
197 }
198
199 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
200 {
201     InternalSettingsGuardForSettings();
202     settings()->setMockScrollbarsEnabled(enabled);
203 }
204
205 void InternalSettings::setFixedElementsLayoutRelativeToFrame(bool enabled, ExceptionCode& ec)
206 {
207     InternalSettingsGuardForSettings();
208     settings()->setFixedElementsLayoutRelativeToFrame(enabled);
209 }
210
211 void InternalSettings::setUnifiedTextCheckingEnabled(bool enabled, ExceptionCode& ec)
212 {
213     InternalSettingsGuardForSettings();
214     settings()->setUnifiedTextCheckerEnabled(enabled);
215 }
216
217 bool InternalSettings::unifiedTextCheckingEnabled(ExceptionCode& ec)
218 {
219     InternalSettingsGuardForSettingsReturn(false);
220     return settings()->unifiedTextCheckerEnabled();
221 }
222
223 void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
224 {
225 #if ENABLE(SHADOW_DOM)
226     UNUSED_PARAM(ec);
227     RuntimeEnabledFeatures::setShadowDOMEnabled(enabled);
228 #else
229     // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
230     // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
231     if (enabled)
232         ec = INVALID_ACCESS_ERR;
233 #endif
234 }
235
236 void InternalSettings::setAuthorShadowDOMForAnyElementEnabled(bool isEnabled)
237 {
238 #if ENABLE(SHADOW_DOM)
239     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(isEnabled);
240 #else
241     UNUSED_PARAM(isEnabled);
242 #endif
243 }
244
245 void InternalSettings::setStyleScopedEnabled(bool enabled)
246 {
247 #if ENABLE(STYLE_SCOPED)
248     RuntimeEnabledFeatures::setStyleScopedEnabled(enabled);
249 #else
250     UNUSED_PARAM(enabled);
251 #endif
252 }
253
254 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
255 {
256 #if ENABLE(TOUCH_EVENTS)
257     InternalSettingsGuardForSettings();
258     settings()->setTouchEventEmulationEnabled(enabled);
259 #else
260     UNUSED_PARAM(enabled);
261     UNUSED_PARAM(ec);
262 #endif
263 }
264
265 void InternalSettings::setDeviceSupportsTouch(bool enabled, ExceptionCode& ec)
266 {
267     InternalSettingsGuardForSettings();
268     settings()->setDeviceSupportsTouch(enabled);
269 }
270
271 void InternalSettings::setDeviceSupportsMouse(bool enabled, ExceptionCode& ec)
272 {
273     InternalSettingsGuardForSettings();
274     settings()->setDeviceSupportsMouse(enabled);
275 }
276
277 typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
278 static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
279 {
280     UScriptCode code = scriptNameToCode(script);
281     if (code != USCRIPT_INVALID_CODE)
282         (settings->*setter)(family, code);
283 }
284
285 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
286 {
287     InternalSettingsGuardForSettings();
288     setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
289 }
290
291 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
292 {
293     InternalSettingsGuardForSettings();
294     setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
295 }
296
297 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
298 {
299     InternalSettingsGuardForSettings();
300     setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
301 }
302
303 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
304 {
305     InternalSettingsGuardForSettings();
306     setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
307 }
308
309 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
310 {
311     InternalSettingsGuardForSettings();
312     setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
313 }
314
315 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
316 {
317     InternalSettingsGuardForSettings();
318     setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
319 }
320
321 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
322 {
323     InternalSettingsGuardForSettings();
324     setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
325 }
326
327 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
328 {
329 #if ENABLE(TEXT_AUTOSIZING)
330     InternalSettingsGuardForSettings();
331     settings()->setTextAutosizingEnabled(enabled);
332 #else
333     UNUSED_PARAM(enabled);
334     UNUSED_PARAM(ec);
335 #endif
336 }
337
338 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
339 {
340 #if ENABLE(TEXT_AUTOSIZING)
341     InternalSettingsGuardForSettings();
342     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
343 #else
344     UNUSED_PARAM(width);
345     UNUSED_PARAM(height);
346     UNUSED_PARAM(ec);
347 #endif
348 }
349
350 void InternalSettings::setResolutionOverride(int dotsPerCSSInchHorizontally, int dotsPerCSSInchVertically, ExceptionCode& ec)
351 {
352     InternalSettingsGuardForSettings();
353     // An empty size resets the override.
354     settings()->setResolutionOverride(IntSize(dotsPerCSSInchHorizontally, dotsPerCSSInchVertically));
355 }
356
357 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
358 {
359     InternalSettingsGuardForSettings();
360     settings()->setMediaTypeOverride(mediaType);
361 }
362
363 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
364 {
365 #if ENABLE(TEXT_AUTOSIZING)
366     InternalSettingsGuardForSettings();
367     settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
368 #else
369     UNUSED_PARAM(fontScaleFactor);
370     UNUSED_PARAM(ec);
371 #endif
372 }
373
374 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
375 {
376 #if ENABLE(SMOOTH_SCROLLING)
377     InternalSettingsGuardForSettings();
378     settings()->setEnableScrollAnimator(enabled);
379 #else
380     UNUSED_PARAM(enabled);
381     UNUSED_PARAM(ec);
382 #endif
383 }
384
385 bool InternalSettings::scrollAnimatorEnabled(ExceptionCode& ec)
386 {
387 #if ENABLE(SMOOTH_SCROLLING)
388     InternalSettingsGuardForSettingsReturn(false);
389     return settings()->scrollAnimatorEnabled();
390 #else
391     UNUSED_PARAM(ec);
392     return false;
393 #endif
394 }
395
396 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
397 {
398     UNUSED_PARAM(ec);
399     RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
400 }
401
402 void InternalSettings::setCSSVariablesEnabled(bool enabled, ExceptionCode& ec)
403 {
404     InternalSettingsGuardForSettings();
405     settings()->setCSSVariablesEnabled(enabled);
406 }
407
408 bool InternalSettings::cssVariablesEnabled(ExceptionCode& ec)
409 {
410     InternalSettingsGuardForSettingsReturn(false);
411     return settings()->cssVariablesEnabled();
412 }
413
414 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
415 {
416     InternalSettingsGuardForSettings();
417     m_page->setCanStartMedia(enabled);
418 }
419
420 void InternalSettings::setMediaPlaybackRequiresUserGesture(bool enabled, ExceptionCode& ec)
421 {
422     InternalSettingsGuardForSettings();
423     settings()->setMediaPlaybackRequiresUserGesture(enabled);
424 }
425
426 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
427 {
428     InternalSettingsGuardForSettings();
429     if (equalIgnoringCase(editingBehavior, "win"))
430         settings()->setEditingBehaviorType(EditingWindowsBehavior);
431     else if (equalIgnoringCase(editingBehavior, "mac"))
432         settings()->setEditingBehaviorType(EditingMacBehavior);
433     else if (equalIgnoringCase(editingBehavior, "unix"))
434         settings()->setEditingBehaviorType(EditingUnixBehavior);
435     else
436         ec = SYNTAX_ERR;
437 }
438
439 void InternalSettings::setFixedPositionCreatesStackingContext(bool creates, ExceptionCode& ec)
440 {
441     InternalSettingsGuardForSettings();
442     settings()->setFixedPositionCreatesStackingContext(creates);
443 }
444
445 void InternalSettings::setSyncXHRInDocumentsEnabled(bool enabled, ExceptionCode& ec)
446 {
447     InternalSettingsGuardForSettings();
448     settings()->setSyncXHRInDocumentsEnabled(enabled);
449 }
450
451 void InternalSettings::setWindowFocusRestricted(bool restricted, ExceptionCode& ec)
452 {
453     InternalSettingsGuardForSettings();
454     settings()->setWindowFocusRestricted(restricted);
455 }
456
457 void InternalSettings::setDialogElementEnabled(bool enabled, ExceptionCode& ec)
458 {
459     UNUSED_PARAM(ec);
460 #if ENABLE(DIALOG_ELEMENT)
461     RuntimeEnabledFeatures::setDialogElementEnabled(enabled);
462 #else
463     UNUSED_PARAM(enabled);
464 #endif
465 }
466
467 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
468 {
469     InternalSettingsGuardForSettings();
470
471 #if ENABLE(VIDEO_TRACK)
472     if (equalIgnoringCase(kind, "Subtitles"))
473         settings()->setShouldDisplaySubtitles(enabled);
474     else if (equalIgnoringCase(kind, "Captions"))
475         settings()->setShouldDisplayCaptions(enabled);
476     else if (equalIgnoringCase(kind, "TextDescriptions"))
477         settings()->setShouldDisplayTextDescriptions(enabled);
478     else
479         ec = SYNTAX_ERR;
480 #else
481     UNUSED_PARAM(kind);
482     UNUSED_PARAM(enabled);
483 #endif
484 }
485
486 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
487 {
488     InternalSettingsGuardForSettingsReturn(false);
489
490 #if ENABLE(VIDEO_TRACK)
491     if (equalIgnoringCase(kind, "Subtitles"))
492         return settings()->shouldDisplaySubtitles();
493     if (equalIgnoringCase(kind, "Captions"))
494         return settings()->shouldDisplayCaptions();
495     if (equalIgnoringCase(kind, "TextDescriptions"))
496         return settings()->shouldDisplayTextDescriptions();
497
498     ec = SYNTAX_ERR;
499     return false;
500 #else
501     UNUSED_PARAM(kind);
502     return false;
503 #endif
504 }
505
506 void InternalSettings::setMemoryInfoEnabled(bool enabled, ExceptionCode& ec)
507 {
508     InternalSettingsGuardForSettings();
509     settings()->setMemoryInfoEnabled(enabled);
510 }
511
512 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
513 {
514     InternalSettingsGuardForSettings();
515
516     if (mode == "AllowAll")
517         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
518     else if (mode == "BlockThirdParty")
519         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
520     else if (mode == "BlockAll")
521         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
522     else
523         ec = SYNTAX_ERR;
524 }
525
526 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
527 {
528     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(enabled);
529 }
530
531 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
532 {
533     InternalSettingsGuardForSettings();
534     settings()->setImagesEnabled(enabled);
535 }
536
537 }