Automatically generate Settings::unifiedTextCheckerEnabled
[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_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::cssExclusionsEnabled())
67     , m_originalCSSVariablesEnabled(settings->cssVariablesEnabled())
68 #if ENABLE(SHADOW_DOM)
69     , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::shadowDOMEnabled())
70     , m_originalAuthorShadowDOMForAnyElementEnabled(RuntimeEnabledFeatures::authorShadowDOMForAnyElementEnabled())
71 #endif
72 #if ENABLE(STYLE_SCOPED)
73     , m_originalStyleScoped(RuntimeEnabledFeatures::styleScopedEnabled())
74 #endif
75     , m_originalEditingBehavior(settings->editingBehaviorType())
76 #if ENABLE(TEXT_AUTOSIZING)
77     , m_originalTextAutosizingEnabled(settings->textAutosizingEnabled())
78     , m_originalTextAutosizingWindowSizeOverride(settings->textAutosizingWindowSizeOverride())
79     , m_originalTextAutosizingFontScaleFactor(settings->textAutosizingFontScaleFactor())
80 #endif
81     , m_originalResolutionOverride(settings->resolutionOverride())
82     , m_originalMediaTypeOverride(settings->mediaTypeOverride())
83 #if ENABLE(DIALOG_ELEMENT)
84     , m_originalDialogElementEnabled(RuntimeEnabledFeatures::dialogElementEnabled())
85 #endif
86     , m_originalCanvasUsesAcceleratedDrawing(settings->canvasUsesAcceleratedDrawing())
87     , m_originalMockScrollbarsEnabled(settings->mockScrollbarsEnabled())
88     , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::langAttributeAwareFormControlUIEnabled())
89     , m_imagesEnabled(settings->areImagesEnabled())
90 #if ENABLE(VIDEO_TRACK)
91     , m_shouldDisplaySubtitles(settings->shouldDisplaySubtitles())
92     , m_shouldDisplayCaptions(settings->shouldDisplayCaptions())
93     , m_shouldDisplayTextDescriptions(settings->shouldDisplayTextDescriptions())
94 #endif
95 {
96 }
97
98 void InternalSettings::Backup::restoreTo(Settings* settings)
99 {
100     RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
101     settings->setCSSVariablesEnabled(m_originalCSSVariablesEnabled);
102 #if ENABLE(SHADOW_DOM)
103     RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
104     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(m_originalAuthorShadowDOMForAnyElementEnabled);
105 #endif
106 #if ENABLE(STYLE_SCOPED)
107     RuntimeEnabledFeatures::setStyleScopedEnabled(m_originalStyleScoped);
108 #endif
109     settings->setEditingBehaviorType(m_originalEditingBehavior);
110 #if ENABLE(TEXT_AUTOSIZING)
111     settings->setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
112     settings->setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
113     settings->setTextAutosizingFontScaleFactor(m_originalTextAutosizingFontScaleFactor);
114 #endif
115     settings->setResolutionOverride(m_originalResolutionOverride);
116     settings->setMediaTypeOverride(m_originalMediaTypeOverride);
117 #if ENABLE(DIALOG_ELEMENT)
118     RuntimeEnabledFeatures::setDialogElementEnabled(m_originalDialogElementEnabled);
119 #endif
120     settings->setCanvasUsesAcceleratedDrawing(m_originalCanvasUsesAcceleratedDrawing);
121     settings->setMockScrollbarsEnabled(m_originalMockScrollbarsEnabled);
122     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
123     settings->setImagesEnabled(m_imagesEnabled);
124 #if ENABLE(VIDEO_TRACK)
125     settings->setShouldDisplaySubtitles(m_shouldDisplaySubtitles);
126     settings->setShouldDisplayCaptions(m_shouldDisplayCaptions);
127     settings->setShouldDisplayTextDescriptions(m_shouldDisplayTextDescriptions);
128 #endif
129 }
130
131 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
132 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
133 // Instead, we manually make InternalSettings supplement Page.
134 class InternalSettingsWrapper : public Supplement<Page> {
135 public:
136     explicit InternalSettingsWrapper(Page* page)
137         : m_internalSettings(InternalSettings::create(page)) { }
138     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
139 #if !ASSERT_DISABLED
140     virtual bool isRefCountedWrapper() const OVERRIDE { return true; }
141 #endif
142     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
143
144 private:
145     RefPtr<InternalSettings> m_internalSettings;
146 };
147
148 InternalSettings* InternalSettings::from(Page* page)
149 {
150     DEFINE_STATIC_LOCAL(AtomicString, name, ("InternalSettings", AtomicString::ConstructFromLiteral));
151     if (!Supplement<Page>::from(page, name))
152         Supplement<Page>::provideTo(page, name, adoptPtr(new InternalSettingsWrapper(page)));
153     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, name))->internalSettings();
154 }
155
156 InternalSettings::~InternalSettings()
157 {
158 }
159
160 InternalSettings::InternalSettings(Page* page)
161     : InternalSettingsGenerated(page)
162     , m_page(page)
163     , m_backup(page->settings())
164 {
165 }
166
167 void InternalSettings::resetToConsistentState()
168 {
169     page()->setPageScaleFactor(1, IntPoint(0, 0));
170     page()->setCanStartMedia(true);
171
172     m_backup.restoreTo(settings());
173     m_backup = Backup(settings());
174
175     InternalSettingsGenerated::resetToConsistentState();
176 }
177
178 Settings* InternalSettings::settings() const
179 {
180     if (!page())
181         return 0;
182     return page()->settings();
183 }
184
185 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
186 {
187     InternalSettingsGuardForSettings();
188     settings()->setMockScrollbarsEnabled(enabled);
189 }
190
191 void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
192 {
193 #if ENABLE(SHADOW_DOM)
194     UNUSED_PARAM(ec);
195     RuntimeEnabledFeatures::setShadowDOMEnabled(enabled);
196 #else
197     // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
198     // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
199     if (enabled)
200         ec = INVALID_ACCESS_ERR;
201 #endif
202 }
203
204 void InternalSettings::setAuthorShadowDOMForAnyElementEnabled(bool isEnabled)
205 {
206 #if ENABLE(SHADOW_DOM)
207     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(isEnabled);
208 #else
209     UNUSED_PARAM(isEnabled);
210 #endif
211 }
212
213 void InternalSettings::setStyleScopedEnabled(bool enabled)
214 {
215 #if ENABLE(STYLE_SCOPED)
216     RuntimeEnabledFeatures::setStyleScopedEnabled(enabled);
217 #else
218     UNUSED_PARAM(enabled);
219 #endif
220 }
221
222 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
223 {
224 #if ENABLE(TOUCH_EVENTS)
225     InternalSettingsGuardForSettings();
226     settings()->setTouchEventEmulationEnabled(enabled);
227 #else
228     UNUSED_PARAM(enabled);
229     UNUSED_PARAM(ec);
230 #endif
231 }
232
233 typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
234 static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
235 {
236     UScriptCode code = scriptNameToCode(script);
237     if (code != USCRIPT_INVALID_CODE)
238         (settings->*setter)(family, code);
239 }
240
241 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
242 {
243     InternalSettingsGuardForSettings();
244     setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
245 }
246
247 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
248 {
249     InternalSettingsGuardForSettings();
250     setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
251 }
252
253 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
254 {
255     InternalSettingsGuardForSettings();
256     setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
257 }
258
259 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
260 {
261     InternalSettingsGuardForSettings();
262     setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
263 }
264
265 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
266 {
267     InternalSettingsGuardForSettings();
268     setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
269 }
270
271 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
272 {
273     InternalSettingsGuardForSettings();
274     setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
275 }
276
277 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
278 {
279     InternalSettingsGuardForSettings();
280     setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
281 }
282
283 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
284 {
285 #if ENABLE(TEXT_AUTOSIZING)
286     InternalSettingsGuardForSettings();
287     settings()->setTextAutosizingEnabled(enabled);
288 #else
289     UNUSED_PARAM(enabled);
290     UNUSED_PARAM(ec);
291 #endif
292 }
293
294 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
295 {
296 #if ENABLE(TEXT_AUTOSIZING)
297     InternalSettingsGuardForSettings();
298     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
299 #else
300     UNUSED_PARAM(width);
301     UNUSED_PARAM(height);
302     UNUSED_PARAM(ec);
303 #endif
304 }
305
306 void InternalSettings::setResolutionOverride(int dotsPerCSSInchHorizontally, int dotsPerCSSInchVertically, ExceptionCode& ec)
307 {
308     InternalSettingsGuardForSettings();
309     // An empty size resets the override.
310     settings()->setResolutionOverride(IntSize(dotsPerCSSInchHorizontally, dotsPerCSSInchVertically));
311 }
312
313 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionCode& ec)
314 {
315     InternalSettingsGuardForSettings();
316     settings()->setMediaTypeOverride(mediaType);
317 }
318
319 void InternalSettings::setTextAutosizingFontScaleFactor(float fontScaleFactor, ExceptionCode& ec)
320 {
321 #if ENABLE(TEXT_AUTOSIZING)
322     InternalSettingsGuardForSettings();
323     settings()->setTextAutosizingFontScaleFactor(fontScaleFactor);
324 #else
325     UNUSED_PARAM(fontScaleFactor);
326     UNUSED_PARAM(ec);
327 #endif
328 }
329
330 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
331 {
332 #if ENABLE(SMOOTH_SCROLLING)
333     InternalSettingsGuardForSettings();
334     settings()->setEnableScrollAnimator(enabled);
335 #else
336     UNUSED_PARAM(enabled);
337     UNUSED_PARAM(ec);
338 #endif
339 }
340
341 bool InternalSettings::scrollAnimatorEnabled(ExceptionCode& ec)
342 {
343 #if ENABLE(SMOOTH_SCROLLING)
344     InternalSettingsGuardForSettingsReturn(false);
345     return settings()->scrollAnimatorEnabled();
346 #else
347     UNUSED_PARAM(ec);
348     return false;
349 #endif
350 }
351
352 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
353 {
354     UNUSED_PARAM(ec);
355     RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
356 }
357
358 void InternalSettings::setCSSVariablesEnabled(bool enabled, ExceptionCode& ec)
359 {
360     InternalSettingsGuardForSettings();
361     settings()->setCSSVariablesEnabled(enabled);
362 }
363
364 bool InternalSettings::cssVariablesEnabled(ExceptionCode& ec)
365 {
366     InternalSettingsGuardForSettingsReturn(false);
367     return settings()->cssVariablesEnabled();
368 }
369
370 void InternalSettings::setCanStartMedia(bool enabled, ExceptionCode& ec)
371 {
372     InternalSettingsGuardForSettings();
373     m_page->setCanStartMedia(enabled);
374 }
375
376 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
377 {
378     InternalSettingsGuardForSettings();
379     if (equalIgnoringCase(editingBehavior, "win"))
380         settings()->setEditingBehaviorType(EditingWindowsBehavior);
381     else if (equalIgnoringCase(editingBehavior, "mac"))
382         settings()->setEditingBehaviorType(EditingMacBehavior);
383     else if (equalIgnoringCase(editingBehavior, "unix"))
384         settings()->setEditingBehaviorType(EditingUnixBehavior);
385     else
386         ec = SYNTAX_ERR;
387 }
388
389 void InternalSettings::setDialogElementEnabled(bool enabled, ExceptionCode& ec)
390 {
391     UNUSED_PARAM(ec);
392 #if ENABLE(DIALOG_ELEMENT)
393     RuntimeEnabledFeatures::setDialogElementEnabled(enabled);
394 #else
395     UNUSED_PARAM(enabled);
396 #endif
397 }
398
399 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
400 {
401     InternalSettingsGuardForSettings();
402
403 #if ENABLE(VIDEO_TRACK)
404     if (equalIgnoringCase(kind, "Subtitles"))
405         settings()->setShouldDisplaySubtitles(enabled);
406     else if (equalIgnoringCase(kind, "Captions"))
407         settings()->setShouldDisplayCaptions(enabled);
408     else if (equalIgnoringCase(kind, "TextDescriptions"))
409         settings()->setShouldDisplayTextDescriptions(enabled);
410     else
411         ec = SYNTAX_ERR;
412 #else
413     UNUSED_PARAM(kind);
414     UNUSED_PARAM(enabled);
415 #endif
416 }
417
418 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
419 {
420     InternalSettingsGuardForSettingsReturn(false);
421
422 #if ENABLE(VIDEO_TRACK)
423     if (equalIgnoringCase(kind, "Subtitles"))
424         return settings()->shouldDisplaySubtitles();
425     if (equalIgnoringCase(kind, "Captions"))
426         return settings()->shouldDisplayCaptions();
427     if (equalIgnoringCase(kind, "TextDescriptions"))
428         return settings()->shouldDisplayTextDescriptions();
429
430     ec = SYNTAX_ERR;
431     return false;
432 #else
433     UNUSED_PARAM(kind);
434     return false;
435 #endif
436 }
437
438 void InternalSettings::setStorageBlockingPolicy(const String& mode, ExceptionCode& ec)
439 {
440     InternalSettingsGuardForSettings();
441
442     if (mode == "AllowAll")
443         settings()->setStorageBlockingPolicy(SecurityOrigin::AllowAllStorage);
444     else if (mode == "BlockThirdParty")
445         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockThirdPartyStorage);
446     else if (mode == "BlockAll")
447         settings()->setStorageBlockingPolicy(SecurityOrigin::BlockAllStorage);
448     else
449         ec = SYNTAX_ERR;
450 }
451
452 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
453 {
454     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(enabled);
455 }
456
457 void InternalSettings::setImagesEnabled(bool enabled, ExceptionCode& ec)
458 {
459     InternalSettingsGuardForSettings();
460     settings()->setImagesEnabled(enabled);
461 }
462
463 }