WebCoreSupport needs objects each of which follows major WebCore objects
[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 #include "TextRun.h"
41
42 #if ENABLE(INPUT_TYPE_COLOR)
43 #include "ColorChooser.h"
44 #endif
45
46 #define InternalSettingsGuardForSettingsReturn(returnValue) \
47     if (!settings()) { \
48         ec = INVALID_ACCESS_ERR; \
49         return returnValue; \
50     }
51
52 #define InternalSettingsGuardForSettings()  \
53     if (!settings()) { \
54         ec = INVALID_ACCESS_ERR; \
55         return; \
56     }
57
58 #define InternalSettingsGuardForPageReturn(returnValue) \
59     if (!page()) { \
60         ec = INVALID_ACCESS_ERR; \
61         return returnValue; \
62     }
63
64 #define InternalSettingsGuardForPage() \
65     if (!page()) { \
66         ec = INVALID_ACCESS_ERR; \
67         return; \
68     }
69
70 namespace WebCore {
71
72 InternalSettings::Backup::Backup(Page* page, Settings* settings)
73     : m_originalPasswordEchoDurationInSeconds(settings->passwordEchoDurationInSeconds())
74     , m_originalPasswordEchoEnabled(settings->passwordEchoEnabled())
75     , m_originalCSSExclusionsEnabled(RuntimeEnabledFeatures::cssExclusionsEnabled())
76 #if ENABLE(SHADOW_DOM)
77     , m_originalShadowDOMEnabled(RuntimeEnabledFeatures::shadowDOMEnabled())
78 #endif
79     , m_originalEditingBehavior(settings->editingBehaviorType())
80     , m_originalFixedPositionCreatesStackingContext(settings->fixedPositionCreatesStackingContext())
81     , m_originalSyncXHRInDocumentsEnabled(settings->syncXHRInDocumentsEnabled())
82 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
83     , m_originalJavaScriptProfilingEnabled(page->inspectorController() && page->inspectorController()->profilerEnabled())
84 #endif
85     , m_originalWindowFocusRestricted(settings->windowFocusRestricted())
86     , m_originalDeviceSupportsTouch(settings->deviceSupportsTouch())
87     , m_originalDeviceSupportsMouse(settings->deviceSupportsMouse())
88 #if ENABLE(TEXT_AUTOSIZING)
89     , m_originalTextAutosizingEnabled(settings->textAutosizingEnabled())
90     , m_originalTextAutosizingWindowSizeOverride(settings->textAutosizingWindowSizeOverride())
91 #endif
92 {
93 }
94
95
96 void InternalSettings::Backup::restoreTo(Page* page, Settings* settings)
97 {
98     settings->setPasswordEchoDurationInSeconds(m_originalPasswordEchoDurationInSeconds);
99     settings->setPasswordEchoEnabled(m_originalPasswordEchoEnabled);
100     RuntimeEnabledFeatures::setCSSExclusionsEnabled(m_originalCSSExclusionsEnabled);
101 #if ENABLE(SHADOW_DOM)
102     RuntimeEnabledFeatures::setShadowDOMEnabled(m_originalShadowDOMEnabled);
103 #endif
104     settings->setEditingBehaviorType(m_originalEditingBehavior);
105     settings->setFixedPositionCreatesStackingContext(m_originalFixedPositionCreatesStackingContext);
106     settings->setSyncXHRInDocumentsEnabled(m_originalSyncXHRInDocumentsEnabled);
107 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
108     if (page->inspectorController())
109         page->inspectorController()->setProfilerEnabled(m_originalJavaScriptProfilingEnabled);
110 #endif
111     settings->setWindowFocusRestricted(m_originalWindowFocusRestricted);
112     settings->setDeviceSupportsTouch(m_originalDeviceSupportsTouch);
113     settings->setDeviceSupportsMouse(m_originalDeviceSupportsMouse);
114 #if ENABLE(TEXT_AUTOSIZING)
115     settings->setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
116     settings->setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
117 #endif
118 }
119
120 InternalSettings* InternalSettings::from(Page* page)
121 {
122     DEFINE_STATIC_LOCAL(AtomicString, name, ("InternalSettings"));
123     if (!SuperType::from(page, name))
124         SuperType::provideTo(page, name, adoptRef(new InternalSettings(page)));
125     return static_cast<InternalSettings*>(SuperType::from(page, name));
126 }
127
128 InternalSettings::~InternalSettings()
129 {
130 }
131
132 InternalSettings::InternalSettings(Page* page)
133     : m_page(page)
134     , m_backup(page, page->settings())
135 {
136 }
137
138 void InternalSettings::reset()
139 {
140     TextRun::setAllowsRoundingHacks(false);
141     setUserPreferredLanguages(Vector<String>());
142     page()->setPagination(Page::Pagination());
143     page()->setPageScaleFactor(1, IntPoint(0, 0));
144
145     m_backup.restoreTo(page(), settings());
146     m_backup = Backup(page(), settings());
147 }
148
149 Settings* InternalSettings::settings() const
150 {
151     if (!page())
152         return 0;
153     return page()->settings();
154 }
155
156 void InternalSettings::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
157 {
158 #if ENABLE(INSPECTOR)
159     if (!page() || !page()->inspectorController()) {
160         ec = INVALID_ACCESS_ERR;
161         return;
162     }
163     page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
164 #else
165     UNUSED_PARAM(maximumResourcesContentSize);
166     UNUSED_PARAM(maximumSingleResourceContentSize);
167     UNUSED_PARAM(ec);
168 #endif
169 }
170
171 void InternalSettings::setForceCompositingMode(bool enabled, ExceptionCode& ec)
172 {
173     InternalSettingsGuardForSettings();
174     settings()->setForceCompositingMode(enabled);
175 }
176
177 void InternalSettings::setAcceleratedFiltersEnabled(bool enabled, ExceptionCode& ec)
178 {
179     InternalSettingsGuardForSettings();
180     settings()->setAcceleratedFiltersEnabled(enabled);
181 }
182
183 void InternalSettings::setEnableCompositingForFixedPosition(bool enabled, ExceptionCode& ec)
184 {
185     InternalSettingsGuardForSettings();
186     settings()->setAcceleratedCompositingForFixedPositionEnabled(enabled);
187 }
188
189 void InternalSettings::setEnableCompositingForScrollableFrames(bool enabled, ExceptionCode& ec)
190 {
191     InternalSettingsGuardForSettings();
192     settings()->setAcceleratedCompositingForScrollableFramesEnabled(enabled);
193 }
194
195 void InternalSettings::setAcceleratedDrawingEnabled(bool enabled, ExceptionCode& ec)
196 {
197     InternalSettingsGuardForSettings();
198     settings()->setAcceleratedDrawingEnabled(enabled);
199 }
200
201 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
202 {
203     InternalSettingsGuardForSettings();
204     settings()->setMockScrollbarsEnabled(enabled);
205 }
206
207 void InternalSettings::setPasswordEchoEnabled(bool enabled, ExceptionCode& ec)
208 {
209     InternalSettingsGuardForSettings();
210     settings()->setPasswordEchoEnabled(enabled);
211 }
212
213 void InternalSettings::setPasswordEchoDurationInSeconds(double durationInSeconds, ExceptionCode& ec)
214 {
215     InternalSettingsGuardForSettings();
216     settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
217 }
218
219 void InternalSettings::setFixedElementsLayoutRelativeToFrame(bool enabled, ExceptionCode& ec)
220 {
221     InternalSettingsGuardForSettings();
222     settings()->setFixedElementsLayoutRelativeToFrame(enabled);
223 }
224
225 void InternalSettings::setUnifiedTextCheckingEnabled(bool enabled, ExceptionCode& ec)
226 {
227     InternalSettingsGuardForSettings();
228     settings()->setUnifiedTextCheckerEnabled(enabled);
229 }
230
231 bool InternalSettings::unifiedTextCheckingEnabled(ExceptionCode& ec)
232 {
233     InternalSettingsGuardForSettingsReturn(false);
234     return settings()->unifiedTextCheckerEnabled();
235 }
236
237 void InternalSettings::setPageScaleFactor(float scaleFactor, int x, int y, ExceptionCode& ec)
238 {
239     InternalSettingsGuardForPage();
240     page()->setPageScaleFactor(scaleFactor, IntPoint(x, y));
241 }
242
243 void InternalSettings::setShadowDOMEnabled(bool enabled, ExceptionCode& ec)
244 {
245 #if ENABLE(SHADOW_DOM)
246     UNUSED_PARAM(ec);
247     RuntimeEnabledFeatures::setShadowDOMEnabled(enabled);
248 #else
249     // Even SHADOW_DOM is off, InternalSettings allows setShadowDOMEnabled(false) to
250     // have broader test coverage. But it cannot be setShadowDOMEnabled(true).
251     if (enabled)
252         ec = INVALID_ACCESS_ERR;
253 #endif
254 }
255
256 void InternalSettings::setTouchEventEmulationEnabled(bool enabled, ExceptionCode& ec)
257 {
258 #if ENABLE(TOUCH_EVENTS)
259     InternalSettingsGuardForSettings();
260     settings()->setTouchEventEmulationEnabled(enabled);
261 #else
262     UNUSED_PARAM(enabled);
263     UNUSED_PARAM(ec);
264 #endif
265 }
266
267 void InternalSettings::setDeviceSupportsTouch(bool enabled, ExceptionCode& ec)
268 {
269     InternalSettingsGuardForSettings();
270     settings()->setDeviceSupportsTouch(enabled);
271 }
272
273 void InternalSettings::setDeviceSupportsMouse(bool enabled, ExceptionCode& ec)
274 {
275     InternalSettingsGuardForSettings();
276     settings()->setDeviceSupportsMouse(enabled);
277 }
278
279 typedef void (Settings::*SetFontFamilyFunction)(const AtomicString&, UScriptCode);
280 static void setFontFamily(Settings* settings, const String& family, const String& script, SetFontFamilyFunction setter)
281 {
282     UScriptCode code = scriptNameToCode(script);
283     if (code != USCRIPT_INVALID_CODE)
284         (settings->*setter)(family, code);
285 }
286
287 void InternalSettings::setStandardFontFamily(const String& family, const String& script, ExceptionCode& ec)
288 {
289     InternalSettingsGuardForSettings();
290     setFontFamily(settings(), family, script, &Settings::setStandardFontFamily);
291 }
292
293 void InternalSettings::setSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
294 {
295     InternalSettingsGuardForSettings();
296     setFontFamily(settings(), family, script, &Settings::setSerifFontFamily);
297 }
298
299 void InternalSettings::setSansSerifFontFamily(const String& family, const String& script, ExceptionCode& ec)
300 {
301     InternalSettingsGuardForSettings();
302     setFontFamily(settings(), family, script, &Settings::setSansSerifFontFamily);
303 }
304
305 void InternalSettings::setFixedFontFamily(const String& family, const String& script, ExceptionCode& ec)
306 {
307     InternalSettingsGuardForSettings();
308     setFontFamily(settings(), family, script, &Settings::setFixedFontFamily);
309 }
310
311 void InternalSettings::setCursiveFontFamily(const String& family, const String& script, ExceptionCode& ec)
312 {
313     InternalSettingsGuardForSettings();
314     setFontFamily(settings(), family, script, &Settings::setCursiveFontFamily);
315 }
316
317 void InternalSettings::setFantasyFontFamily(const String& family, const String& script, ExceptionCode& ec)
318 {
319     InternalSettingsGuardForSettings();
320     setFontFamily(settings(), family, script, &Settings::setFantasyFontFamily);
321 }
322
323 void InternalSettings::setPictographFontFamily(const String& family, const String& script, ExceptionCode& ec)
324 {
325     InternalSettingsGuardForSettings();
326     setFontFamily(settings(), family, script, &Settings::setPictographFontFamily);
327 }
328
329 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionCode& ec)
330 {
331 #if ENABLE(TEXT_AUTOSIZING)
332     InternalSettingsGuardForSettings();
333     settings()->setTextAutosizingEnabled(enabled);
334 #else
335     UNUSED_PARAM(enabled);
336     UNUSED_PARAM(ec);
337 #endif
338 }
339
340 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionCode& ec)
341 {
342 #if ENABLE(TEXT_AUTOSIZING)
343     InternalSettingsGuardForSettings();
344     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
345 #else
346     UNUSED_PARAM(width);
347     UNUSED_PARAM(height);
348     UNUSED_PARAM(ec);
349 #endif
350 }
351
352 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
353 {
354 #if ENABLE(SMOOTH_SCROLLING)
355     InternalSettingsGuardForSettings();
356     settings()->setEnableScrollAnimator(enabled);
357 #else
358     UNUSED_PARAM(enabled);
359     UNUSED_PARAM(ec);
360 #endif
361 }
362
363 bool InternalSettings::scrollAnimatorEnabled(ExceptionCode& ec)
364 {
365 #if ENABLE(SMOOTH_SCROLLING)
366     InternalSettingsGuardForSettingsReturn(false);
367     return settings()->scrollAnimatorEnabled();
368 #else
369     UNUSED_PARAM(ec);
370     return false;
371 #endif
372 }
373
374 void InternalSettings::setCSSExclusionsEnabled(bool enabled, ExceptionCode& ec)
375 {
376     UNUSED_PARAM(ec);
377     RuntimeEnabledFeatures::setCSSExclusionsEnabled(enabled);
378 }
379
380 void InternalSettings::setCSSVariablesEnabled(bool enabled, ExceptionCode& ec)
381 {
382     InternalSettingsGuardForSettings();
383     settings()->setCSSVariablesEnabled(enabled);
384 }
385
386 bool InternalSettings::cssVariablesEnabled(ExceptionCode& ec)
387 {
388     InternalSettingsGuardForSettingsReturn(false);
389     return settings()->cssVariablesEnabled();
390 }
391
392 void InternalSettings::setMediaPlaybackRequiresUserGesture(bool enabled, ExceptionCode& ec)
393 {
394     InternalSettingsGuardForSettings();
395     settings()->setMediaPlaybackRequiresUserGesture(enabled);
396 }
397
398 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionCode& ec)
399 {
400     InternalSettingsGuardForSettings();
401     if (equalIgnoringCase(editingBehavior, "win"))
402         settings()->setEditingBehaviorType(EditingWindowsBehavior);
403     else if (equalIgnoringCase(editingBehavior, "mac"))
404         settings()->setEditingBehaviorType(EditingMacBehavior);
405     else if (equalIgnoringCase(editingBehavior, "unix"))
406         settings()->setEditingBehaviorType(EditingUnixBehavior);
407     else
408         ec = SYNTAX_ERR;
409 }
410
411 void InternalSettings::setFixedPositionCreatesStackingContext(bool creates, ExceptionCode& ec)
412 {
413     InternalSettingsGuardForSettings();
414     settings()->setFixedPositionCreatesStackingContext(creates);
415 }
416
417 void InternalSettings::setSyncXHRInDocumentsEnabled(bool enabled, ExceptionCode& ec)
418 {
419     InternalSettingsGuardForSettings();
420     settings()->setSyncXHRInDocumentsEnabled(enabled);
421 }
422
423 void InternalSettings::setJavaScriptProfilingEnabled(bool enabled, ExceptionCode& ec)
424 {
425 #if ENABLE(INSPECTOR)
426     if (!page() || !page()->inspectorController()) {
427         ec = INVALID_ACCESS_ERR;
428         return;
429     }
430
431     page()->inspectorController()->setProfilerEnabled(enabled);
432 #else
433     UNUSED_PARAM(enabled);
434     UNUSED_PARAM(ec);
435     return;
436 #endif
437 }
438
439 void InternalSettings::setWindowFocusRestricted(bool restricted, ExceptionCode& ec)
440 {
441     InternalSettingsGuardForSettings();
442     settings()->setWindowFocusRestricted(restricted);
443 }
444
445 void InternalSettings::allowRoundingHacks() const
446 {
447     TextRun::setAllowsRoundingHacks(true);
448 }
449
450 Vector<String> InternalSettings::userPreferredLanguages() const
451 {
452     return WebCore::userPreferredLanguages();
453 }
454
455 void InternalSettings::setUserPreferredLanguages(const Vector<String>& languages)
456 {
457     WebCore::overrideUserPreferredLanguages(languages);
458 }
459
460 void InternalSettings::setShouldDisplayTrackKind(const String& kind, bool enabled, ExceptionCode& ec)
461 {
462     InternalSettingsGuardForSettings();
463
464 #if ENABLE(VIDEO_TRACK)
465     if (equalIgnoringCase(kind, "Subtitles"))
466         settings()->setShouldDisplaySubtitles(enabled);
467     else if (equalIgnoringCase(kind, "Captions"))
468         settings()->setShouldDisplayCaptions(enabled);
469     else if (equalIgnoringCase(kind, "TextDescriptions"))
470         settings()->setShouldDisplayTextDescriptions(enabled);
471     else
472         ec = SYNTAX_ERR;
473 #else
474     UNUSED_PARAM(kind);
475     UNUSED_PARAM(enabled);
476 #endif
477 }
478
479 bool InternalSettings::shouldDisplayTrackKind(const String& kind, ExceptionCode& ec)
480 {
481     InternalSettingsGuardForSettingsReturn(false);
482
483 #if ENABLE(VIDEO_TRACK)
484     if (equalIgnoringCase(kind, "Subtitles"))
485         return settings()->shouldDisplaySubtitles();
486     if (equalIgnoringCase(kind, "Captions"))
487         return settings()->shouldDisplayCaptions();
488     if (equalIgnoringCase(kind, "TextDescriptions"))
489         return settings()->shouldDisplayTextDescriptions();
490
491     ec = SYNTAX_ERR;
492     return false;
493 #else
494     UNUSED_PARAM(kind);
495     return false;
496 #endif
497 }
498
499 void InternalSettings::setPagination(const String& mode, int gap, ExceptionCode& ec)
500 {
501     if (!page()) {
502         ec = INVALID_ACCESS_ERR;
503         return;
504     }
505
506     Page::Pagination pagination;
507     if (mode == "Unpaginated")
508         pagination.mode = Page::Pagination::Unpaginated;
509     else if (mode == "LeftToRightPaginated")
510         pagination.mode = Page::Pagination::LeftToRightPaginated;
511     else if (mode == "RightToLeftPaginated")
512         pagination.mode = Page::Pagination::RightToLeftPaginated;
513     else if (mode == "TopToBottomPaginated")
514         pagination.mode = Page::Pagination::TopToBottomPaginated;
515     else if (mode == "BottomToTopPaginated")
516         pagination.mode = Page::Pagination::BottomToTopPaginated;
517     else {
518         ec = SYNTAX_ERR;
519         return;
520     }
521
522     pagination.gap = gap;
523     page()->setPagination(pagination);
524 }
525
526 String InternalSettings::configurationForViewport(float devicePixelRatio, int deviceWidth, int deviceHeight, int availableWidth, int availableHeight, ExceptionCode& ec)
527 {
528     if (!page()) {
529         ec = INVALID_ACCESS_ERR;
530         return String();
531     }
532
533     const int defaultLayoutWidthForNonMobilePages = 980;
534
535     ViewportArguments arguments = page()->viewportArguments();
536     ViewportAttributes attributes = computeViewportAttributes(arguments, defaultLayoutWidthForNonMobilePages, deviceWidth, deviceHeight, devicePixelRatio, IntSize(availableWidth, availableHeight));
537     restrictMinimumScaleFactorToViewportSize(attributes, IntSize(availableWidth, availableHeight));
538     restrictScaleFactorToInitialScaleIfNotUserScalable(attributes);
539
540     return "viewport size " + String::number(attributes.layoutSize.width()) + "x" + String::number(attributes.layoutSize.height()) + " scale " + String::number(attributes.initialScale) + " with limits [" + String::number(attributes.minimumScale) + ", " + String::number(attributes.maximumScale) + "] and userScalable " + (attributes.userScalable ? "true" : "false");
541 }
542
543
544 }