Add runtime flag to enable/disable CSS variables (in addition to existing compile...
[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 "CachedResourceLoader.h"
30 #include "Document.h"
31 #include "ExceptionCode.h"
32 #include "Frame.h"
33 #include "FrameView.h"
34 #include "InspectorController.h"
35 #include "Language.h"
36 #include "LocaleToScriptMapping.h"
37 #include "Page.h"
38 #include "RuntimeEnabledFeatures.h"
39 #include "Settings.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 InternalSettingsGuardForFrame() \
58     if (!frame()) { \
59         ec = INVALID_ACCESS_ERR; \
60         return; \
61     }
62
63 #define InternalSettingsGuardForFrameView() \
64     if (!frame() || !frame()->view()) { \
65         ec = INVALID_ACCESS_ERR; \
66         return; \
67     }
68
69 #define InternalSettingsGuardForPageReturn(returnValue) \
70     if (!page()) { \
71         ec = INVALID_ACCESS_ERR; \
72         return returnValue; \
73     }
74
75 #define InternalSettingsGuardForPage() \
76     if (!page()) { \
77         ec = INVALID_ACCESS_ERR; \
78         return; \
79     }
80
81 namespace WebCore {
82
83
84 PassRefPtr<InternalSettings> InternalSettings::create(Frame* frame)
85 {
86     return adoptRef(new InternalSettings(frame));
87 }
88
89 InternalSettings::~InternalSettings()
90 {
91 }
92
93 InternalSettings::InternalSettings(Frame* frame)
94     : FrameDestructionObserver(frame)
95     , m_originalPasswordEchoDurationInSeconds(settings()->passwordEchoDurationInSeconds())
96     , m_originalPasswordEchoEnabled(settings()->passwordEchoEnabled())
97     , m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::cssExclusionsEnabled())
98 #if ENABLE(SHADOW_DOM)
99     , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::shadowDOMEnabled())
100 #endif
101     , m_originalEditingBehavior(settings()->editingBehaviorType())
102     , m_originalFixedPositionCreatesStackingContext(settings()->fixedPositionCreatesStackingContext())
103     , m_originalSyncXHRInDocumentsEnabled(settings()->syncXHRInDocumentsEnabled())
104 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
105     , m_originalJavaScriptProfilingEnabled(page() && page()->inspectorController() && page()->inspectorController()->profilerEnabled())
106 #endif
107     , m_originalWindowFocusRestricted(settings()->windowFocusRestricted())
108     , m_originalDeviceSupportsTouch(settings()->deviceSupportsTouch())
109     , m_originalDeviceSupportsMouse(settings()->deviceSupportsMouse())
110 {
111 }
112
113 void InternalSettings::restoreTo(Settings* settings)
114 {
115     settings->setPasswordEchoDurationInSeconds(m_originalPasswordEchoDurationInSeconds);
116     settings->setPasswordEchoEnabled(m_originalPasswordEchoEnabled);
117     RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
118 #if ENABLE(SHADOW_DOM)
119     RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
120 #endif
121     settings->setEditingBehaviorType(m_originalEditingBehavior);
122     settings->setFixedPositionCreatesStackingContext(m_originalFixedPositionCreatesStackingContext);
123     settings->setSyncXHRInDocumentsEnabled(m_originalSyncXHRInDocumentsEnabled);
124 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
125     if (page() && page()->inspectorController())
126         page()->inspectorController()->setProfilerEnabled(m_originalJavaScriptProfilingEnabled);
127 #endif
128     settings->setWindowFocusRestricted(m_originalWindowFocusRestricted);
129     settings->setDeviceSupportsTouch(m_originalDeviceSupportsTouch);
130     settings->setDeviceSupportsMouse(m_originalDeviceSupportsMouse);
131 }
132
133 Settings* InternalSettings::settings() const
134 {
135     if (!frame() || !frame()->page())
136         return 0;
137     return frame()->page()->settings();
138 }
139
140 Document* InternalSettings::document() const
141 {
142     return frame() ? frame()->document() : 0;
143 }
144
145 Page* InternalSettings::page() const
146 {
147     return document() ? document()->page() : 0;
148 }
149
150 void InternalSettings::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
151 {
152 #if ENABLE(INSPECTOR)
153     if (!page() || !page()->inspectorController()) {
154         ec = INVALID_ACCESS_ERR;
155         return;
156     }
157     page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
158 #else
159     UNUSED_PARAM(maximumResourcesContentSize);
160     UNUSED_PARAM(maximumSingleResourceContentSize);
161     UNUSED_PARAM(ec);
162 #endif
163 }
164
165 void InternalSettings::setForceCompositingMode(bool enabled, ExceptionCode& ec)
166 {
167     InternalSettingsGuardForSettings();
168     settings()->setForceCompositingMode(enabled);
169 }
170
171 void InternalSettings::setAcceleratedFiltersEnabled(bool enabled, ExceptionCode& ec)
172 {
173     InternalSettingsGuardForSettings();
174     settings()->setAcceleratedFiltersEnabled(enabled);
175 }
176
177 void InternalSettings::setEnableCompositingForFixedPosition(bool enabled, ExceptionCode& ec)
178 {
179     InternalSettingsGuardForSettings();
180     settings()->setAcceleratedCompositingForFixedPositionEnabled(enabled);
181 }
182
183 void InternalSettings::setEnableCompositingForScrollableFrames(bool enabled, ExceptionCode& ec)
184 {
185     InternalSettingsGuardForSettings();
186     settings()->setAcceleratedCompositingForScrollableFramesEnabled(enabled);
187 }
188
189 void InternalSettings::setAcceleratedDrawingEnabled(bool enabled, ExceptionCode& ec)
190 {
191     InternalSettingsGuardForSettings();
192     settings()->setAcceleratedDrawingEnabled(enabled);
193 }
194
195 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
196 {
197     InternalSettingsGuardForSettings();
198     settings()->setMockScrollbarsEnabled(enabled);
199 }
200
201 void InternalSettings::setPasswordEchoEnabled(bool enabled, ExceptionCode& ec)
202 {
203     InternalSettingsGuardForSettings();
204     settings()->setPasswordEchoEnabled(enabled);
205 }
206
207 void InternalSettings::setPasswordEchoDurationInSeconds(double durationInSeconds, ExceptionCode& ec)
208 {
209     InternalSettingsGuardForSettings();
210     settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
211 }
212
213 void InternalSettings::setFixedElementsLayoutRelativeToFrame(bool enabled, ExceptionCode& ec)
214 {
215     InternalSettingsGuardForFrameView();
216     settings()->setFixedElementsLayoutRelativeToFrame(enabled);
217 }
218
219 void InternalSettings::setUnifiedTextCheckingEnabled(bool enabled, ExceptionCode& ec)
220 {
221     InternalSettingsGuardForSettings();
222     settings()->setUnifiedTextCheckerEnabled(enabled);
223 }
224
225 bool InternalSettings::unifiedTextCheckingEnabled(ExceptionCode& ec)
226 {
227     InternalSettingsGuardForSettingsReturn(false);
228     return settings()->unifiedTextCheckerEnabled();
229 }
230
231 void InternalSettings::setPageScaleFactor(float scaleFactor, int x, int y, ExceptionCode& ec)
232 {
233     InternalSettingsGuardForPage();
234     page()->setPageScaleFactor(scaleFactor, IntPoint(x, y));
235 }
236
237 void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
238 {
239 #if ENABLE(SHADOW_DOM)
240     UNUSED_PARAM(ec);
241     RuntimeEnabledFeatures::setShadowDOMEnabled(enabled);
242 #else
243     // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
244     // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
245     if (enabled)
246         ec = INVALID_ACCESS_ERR;
247 #endif
248 }
249
250 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
251 {
252 #if ENABLE(TOUCH_EVENTS)
253     InternalSettingsGuardForSettings();
254     settings()->setTouchEventEmulationEnabled(enabled);
255 #else
256     UNUSED_PARAM(enabled);
257     UNUSED_PARAM(ec);
258 #endif
259 }
260
261 void InternalSettings::setDeviceSupportsTouch(bool enabled, ExceptionCode& ec)
262 {
263     InternalSettingsGuardForSettings();
264     settings()->setDeviceSupportsTouch(enabled);
265 }
266
267 void InternalSettings::setDeviceSupportsMouse(bool enabled, ExceptionCode& ec)
268 {
269     InternalSettingsGuardForSettings();
270     settings()->setDeviceSupportsMouse(enabled);
271 }
272
273 typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
274 static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
275 {
276     UScriptCode code = scriptNameToCode(script);
277     if (code != USCRIPT_INVALID_CODE)
278         (settings->*setter)(family, code);
279 }
280
281 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
282 {
283     InternalSettingsGuardForSettings();
284     setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
285 }
286
287 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
288 {
289     InternalSettingsGuardForSettings();
290     setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
291 }
292
293 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
294 {
295     InternalSettingsGuardForSettings();
296     setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
297 }
298
299 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
300 {
301     InternalSettingsGuardForSettings();
302     setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
303 }
304
305 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
306 {
307     InternalSettingsGuardForSettings();
308     setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
309 }
310
311 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
312 {
313     InternalSettingsGuardForSettings();
314     setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
315 }
316
317 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
318 {
319     InternalSettingsGuardForSettings();
320     setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
321 }
322
323 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
324 {
325 #if ENABLE(SMOOTH_SCROLLING)
326     InternalSettingsGuardForSettings();
327     settings()->setEnableScrollAnimator(enabled);
328 #else
329     UNUSED_PARAM(enabled);
330     UNUSED_PARAM(ec);
331 #endif
332 }
333
334 bool InternalSettings::scrollAnimatorEnabled(ExceptionCode& ec)
335 {
336 #if ENABLE(SMOOTH_SCROLLING)
337     InternalSettingsGuardForSettingsReturn(false);
338     return settings()->scrollAnimatorEnabled();
339 #else
340     UNUSED_PARAM(ec);
341     return false;
342 #endif
343 }
344
345 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
346 {
347     UNUSED_PARAM(ec);
348     RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
349 }
350
351 void InternalSettings::setCSSVariablesEnabled(bool enabled, ExceptionCode& ec)
352 {
353     InternalSettingsGuardForSettings();
354     settings()->setCSSVariablesEnabled(enabled);
355 }
356
357 bool InternalSettings::cssVariablesEnabled(ExceptionCode& ec)
358 {
359     InternalSettingsGuardForSettingsReturn(false);
360     return settings()->cssVariablesEnabled();
361 }
362
363 void InternalSettings::setMediaPlaybackRequiresUserGesture(bool enabled, ExceptionCode& ec)
364 {
365     InternalSettingsGuardForSettings();
366     settings()->setMediaPlaybackRequiresUserGesture(enabled);
367 }
368
369 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
370 {
371     InternalSettingsGuardForSettings();
372     if (equalIgnoringCase(editingBehavior, "win"))
373         settings()->setEditingBehaviorType(EditingWindowsBehavior);
374     else if (equalIgnoringCase(editingBehavior, "mac"))
375         settings()->setEditingBehaviorType(EditingMacBehavior);
376     else if (equalIgnoringCase(editingBehavior, "unix"))
377         settings()->setEditingBehaviorType(EditingUnixBehavior);
378     else
379         ec = SYNTAX_ERR;
380 }
381
382 void InternalSettings::setFixedPositionCreatesStackingContext(bool creates, ExceptionCode& ec)
383 {
384     InternalSettingsGuardForFrameView();
385     settings()->setFixedPositionCreatesStackingContext(creates);
386 }
387
388 void InternalSettings::setSyncXHRInDocumentsEnabled(bool enabled, ExceptionCode& ec)
389 {
390     InternalSettingsGuardForSettings();
391     settings()->setSyncXHRInDocumentsEnabled(enabled);
392 }
393
394 void InternalSettings::setJavaScriptProfilingEnabled(bool enabled, ExceptionCode& ec)
395 {
396 #if ENABLE(INSPECTOR)
397     if (!page() || !page()->inspectorController()) {
398         ec = INVALID_ACCESS_ERR;
399         return;
400     }
401
402     page()->inspectorController()->setProfilerEnabled(enabled);
403 #else
404     UNUSED_PARAM(enabled);
405     UNUSED_PARAM(ec);
406     return;
407 #endif
408 }
409
410 void InternalSettings::setWindowFocusRestricted(bool restricted, ExceptionCode& ec)
411 {
412     InternalSettingsGuardForSettings();
413     settings()->setWindowFocusRestricted(restricted);
414 }
415
416 }