1/*
2    Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "qwebpreferences_p.h"
22
23#include "qquickwebview_p_p.h"
24#include "qwebpreferences_p_p.h"
25#include <WKPageGroup.h>
26#include <WKPreferences.h>
27#include <WKRetainPtr.h>
28#include <WKStringQt.h>
29
30QWebPreferences* QWebPreferencesPrivate::createPreferences(QQuickWebViewPrivate* webViewPrivate)
31{
32    QWebPreferences* prefs = new QWebPreferences;
33    prefs->d->webViewPrivate = webViewPrivate;
34    prefs->d->initializeDefaultFontSettings();
35    return prefs;
36}
37
38bool QWebPreferencesPrivate::testAttribute(QWebPreferencesPrivate::WebAttribute attr) const
39{
40    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
41    switch (attr) {
42    case AutoLoadImages:
43        return WKPreferencesGetLoadsImagesAutomatically(preferencesRef);
44#if ENABLE(FULLSCREEN_API)
45    case FullScreenEnabled:
46        return WKPreferencesGetFullScreenEnabled(preferencesRef);
47#endif
48    case JavascriptEnabled:
49        return WKPreferencesGetJavaScriptEnabled(preferencesRef);
50    case PluginsEnabled:
51        return WKPreferencesGetPluginsEnabled(preferencesRef);
52    case OfflineWebApplicationCacheEnabled:
53        return WKPreferencesGetOfflineWebApplicationCacheEnabled(preferencesRef);
54    case LocalStorageEnabled:
55        return WKPreferencesGetLocalStorageEnabled(preferencesRef);
56    case XSSAuditingEnabled:
57        return WKPreferencesGetXSSAuditorEnabled(preferencesRef);
58    case PrivateBrowsingEnabled:
59        return WKPreferencesGetPrivateBrowsingEnabled(preferencesRef);
60    case DnsPrefetchEnabled:
61        return WKPreferencesGetDNSPrefetchingEnabled(preferencesRef);
62    case FrameFlatteningEnabled:
63        return WKPreferencesGetFrameFlatteningEnabled(preferencesRef);
64    case DeveloperExtrasEnabled:
65        return WKPreferencesGetDeveloperExtrasEnabled(preferencesRef);
66#if ENABLE(WEBGL)
67    case WebGLEnabled:
68        return WKPreferencesGetWebGLEnabled(preferencesRef);
69#if ENABLE(CSS_SHADERS)
70    case CSSCustomFilterEnabled:
71        return WKPreferencesGetCSSCustomFilterEnabled(preferencesRef);
72#endif
73#endif
74#if ENABLE(WEB_AUDIO)
75    case WebAudioEnabled:
76        return WKPreferencesGetWebAudioEnabled(preferencesRef);
77#endif
78    case CaretBrowsingEnabled:
79        return WKPreferencesGetCaretBrowsingEnabled(preferencesRef);
80    case NotificationsEnabled:
81        return WKPreferencesGetNotificationsEnabled(preferencesRef);
82    case UniversalAccessFromFileURLsAllowed:
83        return WKPreferencesGetUniversalAccessFromFileURLsAllowed(preferencesRef);
84    case FileAccessFromFileURLsAllowed:
85        return WKPreferencesGetFileAccessFromFileURLsAllowed(preferencesRef);
86    default:
87        ASSERT_NOT_REACHED();
88        return false;
89    }
90}
91
92void QWebPreferencesPrivate::setAttribute(QWebPreferencesPrivate::WebAttribute attr, bool enable)
93{
94    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
95    switch (attr) {
96    case AutoLoadImages:
97        WKPreferencesSetLoadsImagesAutomatically(preferencesRef, enable);
98        break;
99#if ENABLE(FULLSCREEN_API)
100    case FullScreenEnabled:
101        WKPreferencesSetFullScreenEnabled(preferencesRef, enable);
102        break;
103#endif
104    case JavascriptEnabled:
105        WKPreferencesSetJavaScriptEnabled(preferencesRef, enable);
106        break;
107    case PluginsEnabled:
108        WKPreferencesSetPluginsEnabled(preferencesRef, enable);
109        break;
110    case OfflineWebApplicationCacheEnabled:
111        WKPreferencesSetOfflineWebApplicationCacheEnabled(preferencesRef, enable);
112        break;
113    case LocalStorageEnabled:
114        WKPreferencesSetLocalStorageEnabled(preferencesRef, enable);
115        break;
116    case XSSAuditingEnabled:
117        WKPreferencesSetXSSAuditorEnabled(preferencesRef, enable);
118        break;
119    case PrivateBrowsingEnabled:
120        WKPreferencesSetPrivateBrowsingEnabled(preferencesRef, enable);
121        break;
122    case DnsPrefetchEnabled:
123        WKPreferencesSetDNSPrefetchingEnabled(preferencesRef, enable);
124        break;
125    case FrameFlatteningEnabled:
126        WKPreferencesSetFrameFlatteningEnabled(preferencesRef, enable);
127    case DeveloperExtrasEnabled:
128        WKPreferencesSetDeveloperExtrasEnabled(preferencesRef, enable);
129        break;
130#if ENABLE(WEBGL)
131    case WebGLEnabled:
132        WKPreferencesSetWebGLEnabled(preferencesRef, enable);
133        break;
134#if ENABLE(CSS_SHADERS)
135    case CSSCustomFilterEnabled:
136        WKPreferencesSetCSSCustomFilterEnabled(preferencesRef, enable);
137        break;
138#endif
139#endif
140#if ENABLE(WEB_AUDIO)
141    case WebAudioEnabled:
142        WKPreferencesSetWebAudioEnabled(preferencesRef, enable);
143        break;
144#endif
145    case CaretBrowsingEnabled:
146        // FIXME: Caret browsing doesn't make much sense in touch mode.
147        WKPreferencesSetCaretBrowsingEnabled(preferencesRef, enable);
148        break;
149    case NotificationsEnabled:
150        WKPreferencesSetNotificationsEnabled(preferencesRef, enable);
151        break;
152    case UniversalAccessFromFileURLsAllowed:
153        WKPreferencesSetUniversalAccessFromFileURLsAllowed(preferencesRef, enable);
154        break;
155    case FileAccessFromFileURLsAllowed:
156        WKPreferencesSetFileAccessFromFileURLsAllowed(preferencesRef, enable);
157        break;
158    default:
159        ASSERT_NOT_REACHED();
160    }
161}
162
163void QWebPreferencesPrivate::initializeDefaultFontSettings()
164{
165    setFontSize(MinimumFontSize, 0);
166    setFontSize(DefaultFontSize, 16);
167    setFontSize(DefaultFixedFontSize, 13);
168
169    QFont defaultFont;
170    defaultFont.setStyleHint(QFont::Serif);
171    setFontFamily(StandardFont, defaultFont.defaultFamily());
172    setFontFamily(SerifFont, defaultFont.defaultFamily());
173
174    defaultFont.setStyleHint(QFont::Fantasy);
175    setFontFamily(FantasyFont, defaultFont.defaultFamily());
176
177    defaultFont.setStyleHint(QFont::Cursive);
178    setFontFamily(CursiveFont, defaultFont.defaultFamily());
179
180    defaultFont.setStyleHint(QFont::SansSerif);
181    setFontFamily(SansSerifFont, defaultFont.defaultFamily());
182
183    defaultFont.setStyleHint(QFont::Monospace);
184    setFontFamily(FixedFont, defaultFont.defaultFamily());
185}
186
187void QWebPreferencesPrivate::setFontFamily(QWebPreferencesPrivate::FontFamily which, const QString& family)
188{
189    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
190    WKRetainPtr<WKStringRef> familyRef = adoptWK(WKStringCreateWithQString(family));
191    switch (which) {
192    case StandardFont:
193        WKPreferencesSetStandardFontFamily(preferencesRef, familyRef.get());
194        break;
195    case FixedFont:
196        WKPreferencesSetFixedFontFamily(preferencesRef, familyRef.get());
197        break;
198    case SerifFont:
199        WKPreferencesSetSerifFontFamily(preferencesRef, familyRef.get());
200        break;
201    case SansSerifFont:
202        WKPreferencesSetSansSerifFontFamily(preferencesRef, familyRef.get());
203        break;
204    case CursiveFont:
205        WKPreferencesSetCursiveFontFamily(preferencesRef, familyRef.get());
206        break;
207    case FantasyFont:
208        WKPreferencesSetFantasyFontFamily(preferencesRef, familyRef.get());
209        break;
210    default:
211        break;
212    }
213}
214
215QString QWebPreferencesPrivate::fontFamily(QWebPreferencesPrivate::FontFamily which) const
216{
217    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
218    switch (which) {
219    case StandardFont:
220        return adoptToQString(WKPreferencesCopyStandardFontFamily(preferencesRef));
221    case FixedFont:
222        return adoptToQString(WKPreferencesCopyFixedFontFamily(preferencesRef));
223    case SerifFont:
224        return adoptToQString(WKPreferencesCopySerifFontFamily(preferencesRef));
225    case SansSerifFont:
226        return adoptToQString(WKPreferencesCopySansSerifFontFamily(preferencesRef));
227    case CursiveFont:
228        return adoptToQString(WKPreferencesCopyCursiveFontFamily(preferencesRef));
229    case FantasyFont:
230        return adoptToQString(WKPreferencesCopyFantasyFontFamily(preferencesRef));
231    default:
232        return QString();
233    }
234}
235
236void QWebPreferencesPrivate::setFontSize(QWebPreferencesPrivate::FontSizeType type, unsigned size)
237{
238    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
239    switch (type) {
240    case MinimumFontSize:
241        WKPreferencesSetMinimumFontSize(preferencesRef, static_cast<uint32_t>(size));
242        break;
243    case DefaultFontSize:
244        WKPreferencesSetDefaultFontSize(preferencesRef, static_cast<uint32_t>(size));
245        break;
246    case DefaultFixedFontSize:
247        WKPreferencesSetDefaultFixedFontSize(preferencesRef, static_cast<uint32_t>(size));
248        break;
249    default:
250        ASSERT_NOT_REACHED();
251    }
252}
253
254unsigned QWebPreferencesPrivate::fontSize(QWebPreferencesPrivate::FontSizeType type) const
255{
256    WKPreferencesRef preferencesRef = WKPageGroupGetPreferences(webViewPrivate->pageGroup.get());
257    switch (type) {
258    case MinimumFontSize:
259        return static_cast<unsigned>(WKPreferencesGetMinimumFontSize(preferencesRef));
260    case DefaultFontSize:
261        return static_cast<unsigned>(WKPreferencesGetDefaultFontSize(preferencesRef));
262    case DefaultFixedFontSize:
263        return static_cast<unsigned>(WKPreferencesGetDefaultFixedFontSize(preferencesRef));
264    default:
265        ASSERT_NOT_REACHED();
266        return false;
267    }
268}
269
270QWebPreferences::QWebPreferences()
271    : d(new QWebPreferencesPrivate)
272{
273}
274
275QWebPreferences::~QWebPreferences()
276{
277    delete d;
278}
279
280bool QWebPreferences::autoLoadImages() const
281{
282    return d->testAttribute(QWebPreferencesPrivate::AutoLoadImages);
283}
284
285void QWebPreferences::setAutoLoadImages(bool enable)
286{
287    d->setAttribute(QWebPreferencesPrivate::AutoLoadImages, enable);
288    emit autoLoadImagesChanged();
289}
290
291bool QWebPreferences::fullScreenEnabled() const
292{
293#if ENABLE(FULLSCREEN_API)
294    return d->testAttribute(QWebPreferencesPrivate::FullScreenEnabled);
295#else
296    return false;
297#endif
298}
299
300void QWebPreferences::setFullScreenEnabled(bool enable)
301{
302#if ENABLE(FULLSCREEN_API)
303    d->setAttribute(QWebPreferencesPrivate::FullScreenEnabled, enable);
304    emit fullScreenEnabledChanged();
305#else
306    UNUSED_PARAM(enable);
307#endif
308}
309
310bool QWebPreferences::javascriptEnabled() const
311{
312    return d->testAttribute(QWebPreferencesPrivate::JavascriptEnabled);
313}
314
315void QWebPreferences::setJavascriptEnabled(bool enable)
316{
317    d->setAttribute(QWebPreferencesPrivate::JavascriptEnabled, enable);
318    emit javascriptEnabledChanged();
319}
320
321bool QWebPreferences::pluginsEnabled() const
322{
323    return d->testAttribute(QWebPreferencesPrivate::PluginsEnabled);
324}
325
326void QWebPreferences::setPluginsEnabled(bool enable)
327{
328    d->setAttribute(QWebPreferencesPrivate::PluginsEnabled, enable);
329    emit pluginsEnabledChanged();
330}
331
332bool QWebPreferences::offlineWebApplicationCacheEnabled() const
333{
334    return d->testAttribute(QWebPreferencesPrivate::OfflineWebApplicationCacheEnabled);
335}
336
337void QWebPreferences::setOfflineWebApplicationCacheEnabled(bool enable)
338{
339    d->setAttribute(QWebPreferencesPrivate::OfflineWebApplicationCacheEnabled, enable);
340    emit offlineWebApplicationCacheEnabledChanged();
341}
342
343bool QWebPreferences::localStorageEnabled() const
344{
345    return d->testAttribute(QWebPreferencesPrivate::LocalStorageEnabled);
346}
347
348void QWebPreferences::setLocalStorageEnabled(bool enable)
349{
350    d->setAttribute(QWebPreferencesPrivate::LocalStorageEnabled, enable);
351    emit localStorageEnabledChanged();
352}
353
354bool QWebPreferences::xssAuditingEnabled() const
355{
356    return d->testAttribute(QWebPreferencesPrivate::XSSAuditingEnabled);
357}
358
359void QWebPreferences::setXssAuditingEnabled(bool enable)
360{
361    d->setAttribute(QWebPreferencesPrivate::XSSAuditingEnabled, enable);
362    emit xssAuditingEnabledChanged();
363}
364
365bool QWebPreferences::privateBrowsingEnabled() const
366{
367    return d->testAttribute(QWebPreferencesPrivate::PrivateBrowsingEnabled);
368}
369
370void QWebPreferences::setPrivateBrowsingEnabled(bool enable)
371{
372    d->setAttribute(QWebPreferencesPrivate::PrivateBrowsingEnabled, enable);
373    emit privateBrowsingEnabledChanged();
374}
375
376bool QWebPreferences::dnsPrefetchEnabled() const
377{
378    return d->testAttribute(QWebPreferencesPrivate::DnsPrefetchEnabled);
379}
380
381void QWebPreferences::setDnsPrefetchEnabled(bool enable)
382{
383    d->setAttribute(QWebPreferencesPrivate::DnsPrefetchEnabled, enable);
384    emit dnsPrefetchEnabledChanged();
385}
386
387bool QWebPreferences::developerExtrasEnabled() const
388{
389    return d->testAttribute(QWebPreferencesPrivate::DeveloperExtrasEnabled);
390}
391
392void QWebPreferences::setDeveloperExtrasEnabled(bool enable)
393{
394    d->setAttribute(QWebPreferencesPrivate::DeveloperExtrasEnabled, enable);
395    emit developerExtrasEnabledChanged();
396}
397
398bool QWebPreferences::navigatorQtObjectEnabled() const
399{
400    return d->webViewPrivate->navigatorQtObjectEnabled();
401}
402
403void QWebPreferences::setNavigatorQtObjectEnabled(bool enable)
404{
405    if (enable == navigatorQtObjectEnabled())
406        return;
407    d->webViewPrivate->setNavigatorQtObjectEnabled(enable);
408    emit navigatorQtObjectEnabledChanged();
409}
410
411bool QWebPreferences::frameFlatteningEnabled() const
412{
413    return d->testAttribute(QWebPreferencesPrivate::FrameFlatteningEnabled);
414}
415
416void QWebPreferences::setFrameFlatteningEnabled(bool enable)
417{
418    d->setAttribute(QWebPreferencesPrivate::FrameFlatteningEnabled, enable);
419    emit frameFlatteningEnabledChanged();
420}
421
422QString QWebPreferences::standardFontFamily() const
423{
424    return d->fontFamily(QWebPreferencesPrivate::StandardFont);
425}
426
427void QWebPreferences::setStandardFontFamily(const QString& family)
428{
429    d->setFontFamily(QWebPreferencesPrivate::StandardFont, family);
430    emit standardFontFamilyChanged();
431}
432
433QString QWebPreferences::fixedFontFamily() const
434{
435    return d->fontFamily(QWebPreferencesPrivate::FixedFont);
436}
437
438void QWebPreferences::setFixedFontFamily(const QString& family)
439{
440    d->setFontFamily(QWebPreferencesPrivate::FixedFont, family);
441    emit fixedFontFamilyChanged();
442}
443
444QString QWebPreferences::serifFontFamily() const
445{
446    return d->fontFamily(QWebPreferencesPrivate::SerifFont);
447}
448
449void QWebPreferences::setSerifFontFamily(const QString& family)
450{
451    d->setFontFamily(QWebPreferencesPrivate::SerifFont, family);
452    emit serifFontFamilyChanged();
453}
454
455QString QWebPreferences::sansSerifFontFamily() const
456{
457    return d->fontFamily(QWebPreferencesPrivate::SansSerifFont);
458}
459
460void QWebPreferences::setSansSerifFontFamily(const QString& family)
461{
462    d->setFontFamily(QWebPreferencesPrivate::SansSerifFont, family);
463    emit sansSerifFontFamilyChanged();
464}
465
466QString QWebPreferences::cursiveFontFamily() const
467{
468    return d->fontFamily(QWebPreferencesPrivate::CursiveFont);
469}
470
471void QWebPreferences::setCursiveFontFamily(const QString& family)
472{
473    d->setFontFamily(QWebPreferencesPrivate::CursiveFont, family);
474    emit cursiveFontFamilyChanged();
475}
476
477QString QWebPreferences::fantasyFontFamily() const
478{
479    return d->fontFamily(QWebPreferencesPrivate::FantasyFont);
480}
481
482void QWebPreferences::setFantasyFontFamily(const QString& family)
483{
484    d->setFontFamily(QWebPreferencesPrivate::FantasyFont, family);
485    emit fantasyFontFamilyChanged();
486}
487
488unsigned QWebPreferences::minimumFontSize() const
489{
490    return d->fontSize(QWebPreferencesPrivate::MinimumFontSize);
491}
492
493void QWebPreferences::setMinimumFontSize(unsigned size)
494{
495    d->setFontSize(QWebPreferencesPrivate::MinimumFontSize, size);
496    emit minimumFontSizeChanged();
497}
498
499unsigned QWebPreferences::defaultFontSize() const
500{
501    return d->fontSize(QWebPreferencesPrivate::DefaultFontSize);
502}
503
504void QWebPreferences::setDefaultFontSize(unsigned size)
505{
506    d->setFontSize(QWebPreferencesPrivate::DefaultFontSize, size);
507    emit defaultFontSizeChanged();
508}
509
510unsigned QWebPreferences::defaultFixedFontSize() const
511{
512    return d->fontSize(QWebPreferencesPrivate::DefaultFixedFontSize);
513}
514
515void QWebPreferences::setDefaultFixedFontSize(unsigned size)
516{
517    d->setFontSize(QWebPreferencesPrivate::DefaultFixedFontSize, size);
518    emit defaultFixedFontSizeChanged();
519}
520
521bool QWebPreferences::webGLEnabled() const
522{
523#if ENABLE(WEBGL)
524    return d->testAttribute(QWebPreferencesPrivate::WebGLEnabled);
525#else
526    return false;
527#endif
528}
529
530void QWebPreferences::setWebGLEnabled(bool enable)
531{
532#if ENABLE(WEBGL)
533    d->setAttribute(QWebPreferencesPrivate::WebGLEnabled, enable);
534    emit webGLEnabledChanged();
535#else
536    UNUSED_PARAM(enable);
537#endif
538}
539
540bool QWebPreferences::webAudioEnabled() const
541{
542#if ENABLE(WEB_AUDIO)
543    return d->testAttribute(QWebPreferencesPrivate::WebAudioEnabled);
544#else
545    return false;
546#endif
547}
548
549void QWebPreferences::setWebAudioEnabled(bool enable)
550{
551#if ENABLE(WEB_AUDIO)
552    d->setAttribute(QWebPreferencesPrivate::WebAudioEnabled, enable);
553    emit webAudioEnabledChanged();
554#else
555    UNUSED_PARAM(enable);
556#endif
557}
558
559bool QWebPreferences::caretBrowsingEnabled() const
560{
561    return d->testAttribute(QWebPreferencesPrivate::CaretBrowsingEnabled);
562}
563
564void QWebPreferences::setCaretBrowsingEnabled(bool enable)
565{
566    d->setAttribute(QWebPreferencesPrivate::CaretBrowsingEnabled, enable);
567    emit caretBrowsingEnabledChanged();
568}
569
570bool QWebPreferences::notificationsEnabled() const
571{
572    return d->testAttribute(QWebPreferencesPrivate::NotificationsEnabled);
573}
574
575void QWebPreferences::setNotificationsEnabled(bool enable)
576{
577    d->setAttribute(QWebPreferencesPrivate::NotificationsEnabled, enable);
578    emit notificationsEnabledChanged();
579}
580
581bool QWebPreferences::universalAccessFromFileURLsAllowed() const
582{
583    return d->testAttribute(QWebPreferencesPrivate::UniversalAccessFromFileURLsAllowed);
584}
585
586void QWebPreferences::setUniversalAccessFromFileURLsAllowed(bool enable)
587{
588    if (universalAccessFromFileURLsAllowed() == enable)
589        return;
590    d->setAttribute(QWebPreferencesPrivate::UniversalAccessFromFileURLsAllowed, enable);
591    emit universalAccessFromFileURLsAllowedChanged();
592}
593
594bool QWebPreferences::fileAccessFromFileURLsAllowed() const
595{
596    return d->testAttribute(QWebPreferencesPrivate::FileAccessFromFileURLsAllowed);
597}
598
599void QWebPreferences::setFileAccessFromFileURLsAllowed(bool enable)
600{
601    if (fileAccessFromFileURLsAllowed() == enable)
602        return;
603    d->setAttribute(QWebPreferencesPrivate::FileAccessFromFileURLsAllowed, enable);
604    emit fileAccessFromFileURLsAllowedChanged();
605}
606
607QWebPreferencesPrivate* QWebPreferencesPrivate::get(QWebPreferences* preferences)
608{
609    return preferences->d;
610}
611