a4dbb44bf25973cd1a2ee05e0f933fa89885b8c1
[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 "Page.h"
37 #include "Settings.h"
38
39 #if ENABLE(GESTURE_EVENTS)
40 #include "PlatformGestureEvent.h"
41 #endif
42
43 #if ENABLE(SMOOTH_SCROLLING)
44 #include "ScrollAnimator.h"
45 #endif
46
47 #if ENABLE(INPUT_COLOR)
48 #include "ColorChooser.h"
49 #endif
50
51 #define InternalSettingsGuardForSettingsReturn(returnValue) \
52     if (!settings()) { \
53         ec = INVALID_ACCESS_ERR; \
54         return returnValue; \
55     }
56
57 #define InternalSettingsGuardForSettings()  \
58     if (!settings()) { \
59         ec = INVALID_ACCESS_ERR; \
60         return; \
61     }
62
63 #define InternalSettingsGuardForFrame() \
64     if (!frame()) { \
65         ec = INVALID_ACCESS_ERR; \
66         return; \
67     }
68
69 #define InternalSettingsGuardForFrameView() \
70     if (!frame() || !frame()->view()) { \
71         ec = INVALID_ACCESS_ERR; \
72         return; \
73     }
74
75 #define InternalSettingsGuardForPageReturn(returnValue) \
76     if (!page()) { \
77         ec = INVALID_ACCESS_ERR; \
78         return returnValue; \
79     }
80
81 #define InternalSettingsGuardForPage() \
82     if (!page()) { \
83         ec = INVALID_ACCESS_ERR; \
84         return; \
85     }
86
87 namespace WebCore {
88
89
90 PassRefPtr<InternalSettings> InternalSettings::create(Frame* frame, InternalSettings* old)
91 {
92     return adoptRef(new InternalSettings(frame, old));
93 }
94
95 InternalSettings::~InternalSettings()
96 {
97 }
98
99 InternalSettings::InternalSettings(Frame* frame, InternalSettings* old)
100     : FrameDestructionObserver(frame)
101     , m_passwordEchoDurationInSecondsBackup(0)
102     , m_passwordEchoDurationInSecondsBackedUp(false)
103     , m_passwordEchoEnabledBackedUp(false)
104 {
105     if (old && settings()) {
106         if (old->m_passwordEchoDurationInSecondsBackedUp)
107             settings()->setPasswordEchoDurationInSeconds(old->m_passwordEchoDurationInSecondsBackup);
108         if (old->m_passwordEchoEnabledBackedUp)
109             settings()->setPasswordEchoEnabled(old->m_passwordEchoEnabledBackup);
110     }
111 }
112
113 Settings* InternalSettings::settings() const
114 {
115     if (!frame() || !frame()->page())
116         return 0;
117     return frame()->page()->settings();
118 }
119
120 Document* InternalSettings::document() const
121 {
122     return frame() ? frame()->document() : 0;
123 }
124
125 Page* InternalSettings::page() const
126 {
127     return document() ? document()->page() : 0;
128 }
129
130 void InternalSettings::setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionCode& ec)
131 {
132 #if ENABLE(INSPECTOR)
133     if (!page() || !page()->inspectorController()) {
134         ec = INVALID_ACCESS_ERR;
135         return;
136     }
137     page()->inspectorController()->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
138 #else
139     UNUSED_PARAM(maximumResourcesContentSize);
140     UNUSED_PARAM(maximumSingleResourceContentSize);
141     UNUSED_PARAM(ec);
142 #endif
143 }
144
145 void InternalSettings::setForceCompositingMode(bool enabled, ExceptionCode& ec)
146 {
147     InternalSettingsGuardForSettings();
148     settings()->setForceCompositingMode(enabled);
149 }
150
151 void InternalSettings::setAcceleratedFiltersEnabled(bool enabled, ExceptionCode& ec)
152 {
153     InternalSettingsGuardForSettings();
154     settings()->setAcceleratedFiltersEnabled(enabled);
155 }
156
157 void InternalSettings::setEnableCompositingForFixedPosition(bool enabled, ExceptionCode& ec)
158 {
159     InternalSettingsGuardForSettings();
160     settings()->setAcceleratedCompositingForFixedPositionEnabled(enabled);
161 }
162
163 void InternalSettings::setEnableCompositingForScrollableFrames(bool enabled, ExceptionCode& ec)
164 {
165     InternalSettingsGuardForSettings();
166     settings()->setAcceleratedCompositingForScrollableFramesEnabled(enabled);
167 }
168
169 void InternalSettings::setAcceleratedDrawingEnabled(bool enabled, ExceptionCode& ec)
170 {
171     InternalSettingsGuardForSettings();
172     settings()->setAcceleratedDrawingEnabled(enabled);
173 }
174
175 void InternalSettings::setEnableScrollAnimator(bool enabled, ExceptionCode& ec)
176 {
177     InternalSettingsGuardForSettings();
178 #if ENABLE(SMOOTH_SCROLLING)
179     settings()->setEnableScrollAnimator(enabled);
180 #else
181     UNUSED_PARAM(enabled);
182 #endif
183 }
184
185 void InternalSettings::setZoomAnimatorTransform(float scale, float tx, float ty, ExceptionCode& ec)
186 {
187     InternalSettingsGuardForFrame();
188
189 #if ENABLE(GESTURE_EVENTS)
190     PlatformGestureEvent pge(PlatformEvent::GestureDoubleTap, IntPoint(tx, ty), IntPoint(tx, ty), 0, scale, 0.f, 0, 0, 0, 0);
191     frame()->eventHandler()->handleGestureEvent(pge);
192 #else
193     UNUSED_PARAM(scale);
194     UNUSED_PARAM(tx);
195     UNUSED_PARAM(ty);
196 #endif
197 }
198
199 void InternalSettings::setZoomParameters(float scale, float x, float y, ExceptionCode& ec)
200 {
201     InternalSettingsGuardForFrameView();
202
203 #if ENABLE(SMOOTH_SCROLLING)
204     frame()->view()->scrollAnimator()->setZoomParametersForTest(scale, x, y);
205 #else
206     UNUSED_PARAM(scale);
207     UNUSED_PARAM(x);
208     UNUSED_PARAM(y);
209 #endif
210 }
211
212 void InternalSettings::setMockScrollbarsEnabled(bool enabled, ExceptionCode& ec)
213 {
214     InternalSettingsGuardForSettings();
215     settings()->setMockScrollbarsEnabled(enabled);
216 }
217
218 void InternalSettings::setPasswordEchoEnabled(bool enabled, ExceptionCode& ec)
219 {
220     InternalSettingsGuardForSettings();
221     if (!m_passwordEchoEnabledBackedUp) {
222         m_passwordEchoEnabledBackup = settings()->passwordEchoEnabled();
223         m_passwordEchoEnabledBackedUp = true;
224     }
225     settings()->setPasswordEchoEnabled(enabled);
226 }
227
228 void InternalSettings::setPasswordEchoDurationInSeconds(double durationInSeconds, ExceptionCode& ec)
229 {
230     InternalSettingsGuardForSettings();
231     if (!m_passwordEchoDurationInSecondsBackedUp) {
232         m_passwordEchoDurationInSecondsBackup = settings()->passwordEchoDurationInSeconds();
233         m_passwordEchoDurationInSecondsBackedUp = true;
234     }
235     settings()->setPasswordEchoDurationInSeconds(durationInSeconds);
236 }
237
238 void InternalSettings::setFixedElementsLayoutRelativeToFrame(bool enabled, ExceptionCode& ec)
239 {
240     InternalSettingsGuardForFrameView();
241     settings()->setFixedElementsLayoutRelativeToFrame(enabled);
242 }
243
244 void InternalSettings::setUnifiedTextCheckingEnabled(bool enabled, ExceptionCode& ec)
245 {
246     InternalSettingsGuardForSettings();
247     settings()->setUnifiedTextCheckerEnabled(enabled);
248 }
249
250 bool InternalSettings::unifiedTextCheckingEnabled(ExceptionCode& ec)
251 {
252     InternalSettingsGuardForSettingsReturn(false);
253     return settings()->unifiedTextCheckerEnabled();
254 }
255
256 float InternalSettings::pageScaleFactor(ExceptionCode& ec)
257 {
258     InternalSettingsGuardForPageReturn(0);
259     return page()->pageScaleFactor();
260 }
261
262 void InternalSettings::setPageScaleFactor(float scaleFactor, int x, int y, ExceptionCode& ec)
263 {
264     InternalSettingsGuardForPage();
265     page()->setPageScaleFactor(scaleFactor, IntPoint(x, y));
266 }
267
268 void InternalSettings::setPerTileDrawingEnabled(bool enabled, ExceptionCode& ec)
269 {
270     InternalSettingsGuardForSettings();
271     settings()->setPerTileDrawingEnabled(enabled);
272 }
273
274 }