Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / testing / InternalSettings.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "core/testing/InternalSettings.h"
29
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/frame/Settings.h"
33 #include "core/inspector/InspectorController.h"
34 #include "core/page/Page.h"
35 #include "platform/RuntimeEnabledFeatures.h"
36 #include "platform/Supplementable.h"
37 #include "platform/text/LocaleToScriptMapping.h"
38
39 #define InternalSettingsGuardForSettingsReturn(returnValue) \
40     if (!settings()) { \
41         exceptionState.throwDOMException(InvalidAccessError, "The settings object cannot be obtained."); \
42         return returnValue; \
43     }
44
45 #define InternalSettingsGuardForSettings()  \
46     if (!settings()) { \
47         exceptionState.throwDOMException(InvalidAccessError, "The settings object cannot be obtained."); \
48         return; \
49     }
50
51 #define InternalSettingsGuardForPage() \
52     if (!page()) { \
53         exceptionState.throwDOMException(InvalidAccessError, "The page object cannot be obtained."); \
54         return; \
55     }
56
57 namespace blink {
58
59 InternalSettings::Backup::Backup(Settings* settings)
60     : m_originalAuthorShadowDOMForAnyElementEnabled(RuntimeEnabledFeatures::authorShadowDOMForAnyElementEnabled())
61     , m_originalCSP(RuntimeEnabledFeatures::experimentalContentSecurityPolicyFeaturesEnabled())
62     , m_originalLaxMixedContentCheckingEnabled(RuntimeEnabledFeatures::laxMixedContentCheckingEnabled())
63     , m_originalOverlayScrollbarsEnabled(RuntimeEnabledFeatures::overlayScrollbarsEnabled())
64     , m_originalEditingBehavior(settings->editingBehaviorType())
65     , m_originalTextAutosizingEnabled(settings->textAutosizingEnabled())
66     , m_originalTextAutosizingWindowSizeOverride(settings->textAutosizingWindowSizeOverride())
67     , m_originalAccessibilityFontScaleFactor(settings->accessibilityFontScaleFactor())
68     , m_originalMediaTypeOverride(settings->mediaTypeOverride())
69     , m_originalMockScrollbarsEnabled(settings->mockScrollbarsEnabled())
70     , m_originalMockGestureTapHighlightsEnabled(settings->mockGestureTapHighlightsEnabled())
71     , m_langAttributeAwareFormControlUIEnabled(RuntimeEnabledFeatures::langAttributeAwareFormControlUIEnabled())
72     , m_imagesEnabled(settings->imagesEnabled())
73     , m_defaultVideoPosterURL(settings->defaultVideoPosterURL())
74     , m_originalLayerSquashingEnabled(settings->layerSquashingEnabled())
75     , m_originalPseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled(RuntimeEnabledFeatures::pseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled())
76 {
77 }
78
79 void InternalSettings::Backup::restoreTo(Settings* settings)
80 {
81     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(m_originalAuthorShadowDOMForAnyElementEnabled);
82     RuntimeEnabledFeatures::setExperimentalContentSecurityPolicyFeaturesEnabled(m_originalCSP);
83     RuntimeEnabledFeatures::setLaxMixedContentCheckingEnabled(m_originalLaxMixedContentCheckingEnabled);
84     RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(m_originalOverlayScrollbarsEnabled);
85     settings->setEditingBehaviorType(m_originalEditingBehavior);
86     settings->setTextAutosizingEnabled(m_originalTextAutosizingEnabled);
87     settings->setTextAutosizingWindowSizeOverride(m_originalTextAutosizingWindowSizeOverride);
88     settings->setAccessibilityFontScaleFactor(m_originalAccessibilityFontScaleFactor);
89     settings->setMediaTypeOverride(m_originalMediaTypeOverride);
90     settings->setMockScrollbarsEnabled(m_originalMockScrollbarsEnabled);
91     settings->setMockGestureTapHighlightsEnabled(m_originalMockGestureTapHighlightsEnabled);
92     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(m_langAttributeAwareFormControlUIEnabled);
93     settings->setImagesEnabled(m_imagesEnabled);
94     settings->setDefaultVideoPosterURL(m_defaultVideoPosterURL);
95     settings->setLayerSquashingEnabled(m_originalLayerSquashingEnabled);
96     settings->genericFontFamilySettings().reset();
97     RuntimeEnabledFeatures::setPseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled(m_originalPseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled);
98 }
99
100 #if ENABLE(OILPAN)
101 InternalSettings* InternalSettings::from(Page& page)
102 {
103     if (!HeapSupplement<Page>::from(page, supplementName()))
104         HeapSupplement<Page>::provideTo(page, supplementName(), new InternalSettings(page));
105     return static_cast<InternalSettings*>(HeapSupplement<Page>::from(page, supplementName()));
106 }
107 #else
108 // We can't use RefCountedSupplement because that would try to make InternalSettings RefCounted
109 // and InternalSettings is already RefCounted via its base class, InternalSettingsGenerated.
110 // Instead, we manually make InternalSettings supplement Page.
111 class InternalSettingsWrapper : public Supplement<Page> {
112 public:
113     explicit InternalSettingsWrapper(Page& page)
114         : m_internalSettings(InternalSettings::create(page)) { }
115     virtual ~InternalSettingsWrapper() { m_internalSettings->hostDestroyed(); }
116 #if ENABLE(ASSERT)
117     virtual bool isRefCountedWrapper() const OVERRIDE { return true; }
118 #endif
119     InternalSettings* internalSettings() const { return m_internalSettings.get(); }
120
121 private:
122     RefPtr<InternalSettings> m_internalSettings;
123 };
124
125 InternalSettings* InternalSettings::from(Page& page)
126 {
127     if (!Supplement<Page>::from(page, supplementName()))
128         Supplement<Page>::provideTo(page, supplementName(), adoptPtr(new InternalSettingsWrapper(page)));
129     return static_cast<InternalSettingsWrapper*>(Supplement<Page>::from(page, supplementName()))->internalSettings();
130 }
131 #endif
132
133 const char* InternalSettings::supplementName()
134 {
135     return "InternalSettings";
136 }
137
138 InternalSettings::~InternalSettings()
139 {
140 }
141
142 InternalSettings::InternalSettings(Page& page)
143     : InternalSettingsGenerated(&page)
144     , m_page(&page)
145     , m_backup(&page.settings())
146 {
147     ScriptWrappable::init(this);
148 }
149
150 void InternalSettings::resetToConsistentState()
151 {
152     page()->setPageScaleFactor(1, IntPoint(0, 0));
153
154     m_backup.restoreTo(settings());
155     m_backup = Backup(settings());
156     m_backup.m_originalTextAutosizingEnabled = settings()->textAutosizingEnabled();
157
158     InternalSettingsGenerated::resetToConsistentState();
159 }
160
161 Settings* InternalSettings::settings() const
162 {
163     if (!page())
164         return 0;
165     return &page()->settings();
166 }
167
168 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionState& exceptionState)
169 {
170     InternalSettingsGuardForSettings();
171     settings()->setMockScrollbarsEnabled(enabled);
172 }
173
174 void InternalSettings::setMockGestureTapHighlightsEnabled(bool enabled, ExceptionState& exceptionState)
175 {
176     InternalSettingsGuardForSettings();
177     settings()->setMockGestureTapHighlightsEnabled(enabled);
178 }
179
180 void InternalSettings::setAuthorShadowDOMForAnyElementEnabled(bool isEnabled)
181 {
182     RuntimeEnabledFeatures::setAuthorShadowDOMForAnyElementEnabled(isEnabled);
183 }
184
185 void InternalSettings::setExperimentalContentSecurityPolicyFeaturesEnabled(bool enabled)
186 {
187     RuntimeEnabledFeatures::setExperimentalContentSecurityPolicyFeaturesEnabled(enabled);
188 }
189
190 void InternalSettings::setLaxMixedContentCheckingEnabled(bool enabled)
191 {
192     RuntimeEnabledFeatures::setLaxMixedContentCheckingEnabled(enabled);
193 }
194
195 void InternalSettings::setPseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled(bool enabled)
196 {
197     RuntimeEnabledFeatures::setPseudoClassesInMatchingCriteriaInAuthorShadowTreesEnabled(enabled);
198 }
199
200 void InternalSettings::setOverlayScrollbarsEnabled(bool enabled)
201 {
202     RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(enabled);
203 }
204
205 void InternalSettings::setViewportEnabled(bool enabled, ExceptionState& exceptionState)
206 {
207     InternalSettingsGuardForSettings();
208     settings()->setViewportEnabled(enabled);
209 }
210
211 // FIXME: This is a temporary flag and should be removed once squashing is
212 // ready (crbug.com/261605).
213 void InternalSettings::setLayerSquashingEnabled(bool enabled, ExceptionState& exceptionState)
214 {
215     InternalSettingsGuardForSettings();
216     settings()->setLayerSquashingEnabled(enabled);
217 }
218
219 void InternalSettings::setStandardFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
220 {
221     InternalSettingsGuardForSettings();
222     UScriptCode code = scriptNameToCode(script);
223     if (code == USCRIPT_INVALID_CODE)
224         return;
225     if (settings()->genericFontFamilySettings().updateStandard(family, code))
226         settings()->notifyGenericFontFamilyChange();
227 }
228
229 void InternalSettings::setSerifFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
230 {
231     InternalSettingsGuardForSettings();
232     UScriptCode code = scriptNameToCode(script);
233     if (code == USCRIPT_INVALID_CODE)
234         return;
235     if (settings()->genericFontFamilySettings().updateSerif(family, code))
236         settings()->notifyGenericFontFamilyChange();
237 }
238
239 void InternalSettings::setSansSerifFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
240 {
241     InternalSettingsGuardForSettings();
242     UScriptCode code = scriptNameToCode(script);
243     if (code == USCRIPT_INVALID_CODE)
244         return;
245     if (settings()->genericFontFamilySettings().updateSansSerif(family, code))
246         settings()->notifyGenericFontFamilyChange();
247 }
248
249 void InternalSettings::setFixedFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
250 {
251     InternalSettingsGuardForSettings();
252     UScriptCode code = scriptNameToCode(script);
253     if (code == USCRIPT_INVALID_CODE)
254         return;
255     if (settings()->genericFontFamilySettings().updateFixed(family, code))
256         settings()->notifyGenericFontFamilyChange();
257 }
258
259 void InternalSettings::setCursiveFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
260 {
261     InternalSettingsGuardForSettings();
262     UScriptCode code = scriptNameToCode(script);
263     if (code == USCRIPT_INVALID_CODE)
264         return;
265     if (settings()->genericFontFamilySettings().updateCursive(family, code))
266         settings()->notifyGenericFontFamilyChange();
267 }
268
269 void InternalSettings::setFantasyFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
270 {
271     InternalSettingsGuardForSettings();
272     UScriptCode code = scriptNameToCode(script);
273     if (code == USCRIPT_INVALID_CODE)
274         return;
275     if (settings()->genericFontFamilySettings().updateFantasy(family, code))
276         settings()->notifyGenericFontFamilyChange();
277 }
278
279 void InternalSettings::setPictographFontFamily(const AtomicString& family, const String& script, ExceptionState& exceptionState)
280 {
281     InternalSettingsGuardForSettings();
282     UScriptCode code = scriptNameToCode(script);
283     if (code == USCRIPT_INVALID_CODE)
284         return;
285     if (settings()->genericFontFamilySettings().updatePictograph(family, code))
286         settings()->notifyGenericFontFamilyChange();
287 }
288
289 void InternalSettings::setTextAutosizingEnabled(bool enabled, ExceptionState& exceptionState)
290 {
291     InternalSettingsGuardForSettings();
292     settings()->setTextAutosizingEnabled(enabled);
293     m_page->inspectorController().setTextAutosizingEnabled(enabled);
294 }
295
296 void InternalSettings::setTextAutosizingWindowSizeOverride(int width, int height, ExceptionState& exceptionState)
297 {
298     InternalSettingsGuardForSettings();
299     settings()->setTextAutosizingWindowSizeOverride(IntSize(width, height));
300 }
301
302 void InternalSettings::setMediaTypeOverride(const String& mediaType, ExceptionState& exceptionState)
303 {
304     InternalSettingsGuardForSettings();
305     settings()->setMediaTypeOverride(mediaType);
306 }
307
308 void InternalSettings::setAccessibilityFontScaleFactor(float fontScaleFactor, ExceptionState& exceptionState)
309 {
310     InternalSettingsGuardForSettings();
311     settings()->setAccessibilityFontScaleFactor(fontScaleFactor);
312 }
313
314 void InternalSettings::setEditingBehavior(const String& editingBehavior, ExceptionState& exceptionState)
315 {
316     InternalSettingsGuardForSettings();
317     if (equalIgnoringCase(editingBehavior, "win"))
318         settings()->setEditingBehaviorType(EditingWindowsBehavior);
319     else if (equalIgnoringCase(editingBehavior, "mac"))
320         settings()->setEditingBehaviorType(EditingMacBehavior);
321     else if (equalIgnoringCase(editingBehavior, "unix"))
322         settings()->setEditingBehaviorType(EditingUnixBehavior);
323     else if (equalIgnoringCase(editingBehavior, "android"))
324         settings()->setEditingBehaviorType(EditingAndroidBehavior);
325     else
326         exceptionState.throwDOMException(SyntaxError, "The editing behavior type provided ('" + editingBehavior + "') is invalid.");
327 }
328
329 void InternalSettings::setLangAttributeAwareFormControlUIEnabled(bool enabled)
330 {
331     RuntimeEnabledFeatures::setLangAttributeAwareFormControlUIEnabled(enabled);
332 }
333
334 void InternalSettings::setImagesEnabled(bool enabled, ExceptionState& exceptionState)
335 {
336     InternalSettingsGuardForSettings();
337     settings()->setImagesEnabled(enabled);
338 }
339
340 void InternalSettings::setDefaultVideoPosterURL(const String& url, ExceptionState& exceptionState)
341 {
342     InternalSettingsGuardForSettings();
343     settings()->setDefaultVideoPosterURL(url);
344 }
345
346 void InternalSettings::trace(Visitor* visitor)
347 {
348     visitor->trace(m_page);
349     InternalSettingsGenerated::trace(visitor);
350 #if ENABLE(OILPAN)
351     HeapSupplement<Page>::trace(visitor);
352 #endif
353 }
354
355 }