WebKitTestRunner needs to support layoutTestController.setJavaScriptProfilingEnabled
[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 {
108 }
109
110 void InternalSettings::restoreTo(Settings* settings)
111 {
112     settings->setPasswordEchoDurationInSeconds(m_originalPasswordEchoDurationInSeconds);
113     settings->setPasswordEchoEnabled(m_originalPasswordEchoEnabled);
114     RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
115 #if ENABLE(SHADOW_DOM)
116     RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
117 #endif
118     settings->setEditingBehaviorType(m_originalEditingBehavior);
119     settings->setFixedPositionCreatesStackingContext(m_originalFixedPositionCreatesStackingContext);
120     settings->setSyncXHRInDocumentsEnabled(m_originalSyncXHRInDocumentsEnabled);
121 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
122     if (page() && page()->inspectorController())
123         page()->inspectorController()->setProfilerEnabled(m_originalJavaScriptProfilingEnabled);
124 #endif
125 }
126
127 Settings* InternalSettings::settings() const
128 {
129     if (!frame() || !frame()->page())
130         return 0;
131     return frame()->page()->settings();
132 }
133
134 Document* InternalSettings::document() const
135 {
136     return frame() ? frame()->document() : 0;
137 }
138
139 Page* InternalSettings::page() const
140 {
141     return document() ? document()->page() : 0;
142 }
143
144 void InternalSettings::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
145 {
146 #if ENABLE(INSPECTOR)
147     if (!page() || !page()->inspectorController()) {
148         ec = INVALID_ACCESS_ERR;
149         return;
150     }
151     page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
152 #else
153     UNUSED_PARAM(maximumResourcesContentSize);
154     UNUSED_PARAM(maximumSingleResourceContentSize);
155     UNUSED_PARAM(ec);
156 #endif
157 }
158
159 void InternalSettings::setForceCompositingMode(bool enabled, ExceptionCode& ec)
160 {
161     InternalSettingsGuardForSettings();
162     settings()->setForceCompositingMode(enabled);
163 }
164
165 void InternalSettings::setAcceleratedFiltersEnabled(bool enabled, ExceptionCode& ec)
166 {
167     InternalSettingsGuardForSettings();
168     settings()->setAcceleratedFiltersEnabled(enabled);
169 }
170
171 void InternalSettings::setEnableCompositingForFixedPosition(bool enabled, ExceptionCode& ec)
172 {
173     InternalSettingsGuardForSettings();
174     settings()->setAcceleratedCompositingForFixedPositionEnabled(enabled);
175 }
176
177 void InternalSettings::setEnableCompositingForScrollableFrames(bool enabled, ExceptionCode& ec)
178 {
179     InternalSettingsGuardForSettings();
180     settings()->setAcceleratedCompositingForScrollableFramesEnabled(enabled);
181 }
182
183 void InternalSettings::setAcceleratedDrawingEnabled(bool enabled, ExceptionCode& ec)
184 {
185     InternalSettingsGuardForSettings();
186     settings()->setAcceleratedDrawingEnabled(enabled);
187 }
188
189 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
190 {
191     InternalSettingsGuardForSettings();
192     settings()->setMockScrollbarsEnabled(enabled);
193 }
194
195 void InternalSettings::setPasswordEchoEnabled(bool enabled, ExceptionCode& ec)
196 {
197     InternalSettingsGuardForSettings();
198     settings()->setPasswordEchoEnabled(enabled);
199 }
200
201 void InternalSettings::setPasswordEchoDurationInSeconds(double durationInSeconds, ExceptionCode& ec)
202 {
203     InternalSettingsGuardForSettings();
204     settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
205 }
206
207 void InternalSettings::setFixedElementsLayoutRelativeToFrame(bool enabled, ExceptionCode& ec)
208 {
209     InternalSettingsGuardForFrameView();
210     settings()->setFixedElementsLayoutRelativeToFrame(enabled);
211 }
212
213 void InternalSettings::setUnifiedTextCheckingEnabled(bool enabled, ExceptionCode& ec)
214 {
215     InternalSettingsGuardForSettings();
216     settings()->setUnifiedTextCheckerEnabled(enabled);
217 }
218
219 bool InternalSettings::unifiedTextCheckingEnabled(ExceptionCode& ec)
220 {
221     InternalSettingsGuardForSettingsReturn(false);
222     return settings()->unifiedTextCheckerEnabled();
223 }
224
225 void InternalSettings::setPageScaleFactor(float scaleFactor, int x, int y, ExceptionCode& ec)
226 {
227     InternalSettingsGuardForPage();
228     page()->setPageScaleFactor(scaleFactor, IntPoint(x, y));
229 }
230
231 void InternalSettings::setPerTileDrawingEnabled(bool enabled, ExceptionCode& ec)
232 {
233     InternalSettingsGuardForSettings();
234     settings()->setPerTileDrawingEnabled(enabled);
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 typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
262 static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
263 {
264     UScriptCode code = scriptNameToCode(script);
265     if (code != USCRIPT_INVALID_CODE)
266         (settings->*setter)(family, code);
267 }
268
269 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
270 {
271     InternalSettingsGuardForSettings();
272     setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
273 }
274
275 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
276 {
277     InternalSettingsGuardForSettings();
278     setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
279 }
280
281 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
282 {
283     InternalSettingsGuardForSettings();
284     setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
285 }
286
287 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
288 {
289     InternalSettingsGuardForSettings();
290     setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
291 }
292
293 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
294 {
295     InternalSettingsGuardForSettings();
296     setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
297 }
298
299 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
300 {
301     InternalSettingsGuardForSettings();
302     setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
303 }
304
305 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
306 {
307     InternalSettingsGuardForSettings();
308     setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
309 }
310
311 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
312 {
313 #if ENABLE(SMOOTH_SCROLLING)
314     InternalSettingsGuardForSettings();
315     settings()->setEnableScrollAnimator(enabled);
316 #else
317     UNUSED_PARAM(enabled);
318     UNUSED_PARAM(ec);
319 #endif
320 }
321
322 bool InternalSettings::scrollAnimatorEnabled(ExceptionCode& ec)
323 {
324 #if ENABLE(SMOOTH_SCROLLING)
325     InternalSettingsGuardForSettingsReturn(false);
326     return settings()->scrollAnimatorEnabled();
327 #else
328     UNUSED_PARAM(ec);
329     return false;
330 #endif
331 }
332
333 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
334 {
335     UNUSED_PARAM(ec);
336     RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
337 }
338
339 void InternalSettings::setMediaPlaybackRequiresUserGesture(bool enabled, ExceptionCode& ec)
340 {
341     InternalSettingsGuardForSettings();
342     settings()->setMediaPlaybackRequiresUserGesture(enabled);
343 }
344
345 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
346 {
347     InternalSettingsGuardForSettings();
348     if (equalIgnoringCase(editingBehavior, "win"))
349         settings()->setEditingBehaviorType(EditingWindowsBehavior);
350     else if (equalIgnoringCase(editingBehavior, "mac"))
351         settings()->setEditingBehaviorType(EditingMacBehavior);
352     else if (equalIgnoringCase(editingBehavior, "unix"))
353         settings()->setEditingBehaviorType(EditingUnixBehavior);
354     else
355         ec = SYNTAX_ERR;
356 }
357
358 void InternalSettings::setFixedPositionCreatesStackingContext(bool creates, ExceptionCode& ec)
359 {
360     InternalSettingsGuardForFrameView();
361     settings()->setFixedPositionCreatesStackingContext(creates);
362 }
363
364 void InternalSettings::setSyncXHRInDocumentsEnabled(bool creates, ExceptionCode& ec)
365 {
366     InternalSettingsGuardForFrameView();
367     settings()->setSyncXHRInDocumentsEnabled(creates);
368 }
369
370 void InternalSettings::setJavaScriptProfilingEnabled(bool enabled, ExceptionCode& ec)
371 {
372 #if ENABLE(INSPECTOR)
373     if (!page() || !page()->inspectorController()) {
374         ec = INVALID_ACCESS_ERR;
375         return;
376     }
377
378     page()->inspectorController()->setProfilerEnabled(enabled);
379 #else
380     UNUSED_PARAM(enabled);
381     UNUSED_PARAM(ec);
382     return;
383 #endif
384 }
385
386 }