initial import
[vuplus_webkit] / Source / WebKit / qt / Api / qwebsettings.cpp
1 /*
2     Copyright (C) 2008 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 "qwebsettings.h"
22
23 #include "qwebpage.h"
24 #include "qwebpage_p.h"
25 #include "qwebplugindatabase_p.h"
26
27 #include "AbstractDatabase.h"
28 #include "MemoryCache.h"
29 #include "CrossOriginPreflightResultCache.h"
30 #include "FontCache.h"
31 #if ENABLE(ICONDATABASE)
32 #include "IconDatabaseClientQt.h"
33 #endif
34 #include "Page.h"
35 #include "PageCache.h"
36 #include "Settings.h"
37 #include "KURL.h"
38 #include "PlatformString.h"
39 #include "IconDatabase.h"
40 #include "PluginDatabase.h"
41 #include "Image.h"
42 #include "IntSize.h"
43 #include "ApplicationCacheStorage.h"
44 #include "DatabaseTracker.h"
45 #include "FileSystem.h"
46
47 #include <QApplication>
48 #include <QDesktopServices>
49 #include <QDir>
50 #include <QHash>
51 #include <QSharedData>
52 #include <QUrl>
53 #include <QFileInfo>
54 #include <QStyle>
55
56 #include "NetworkStateNotifier.h"
57
58 void QWEBKIT_EXPORT qt_networkAccessAllowed(bool isAllowed)
59 {
60 #ifndef QT_NO_BEARERMANAGEMENT
61     WebCore::networkStateNotifier().setNetworkAccessAllowed(isAllowed);
62 #endif
63 }
64
65 class QWebSettingsPrivate {
66 public:
67     QWebSettingsPrivate(WebCore::Settings* wcSettings = 0)
68         : settings(wcSettings)
69     {
70     }
71
72     QHash<int, QString> fontFamilies;
73     QHash<int, int> fontSizes;
74     QHash<int, bool> attributes;
75     QUrl userStyleSheetLocation;
76     QString defaultTextEncoding;
77     QString localStoragePath;
78     QString offlineWebApplicationCachePath;
79     qint64 offlineStorageDefaultQuota;
80 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
81     QWebSettings::ThirdPartyCookiePolicy thirdPartyCookiePolicy;
82 #endif
83     void apply();
84     WebCore::Settings* settings;
85 };
86
87 typedef QHash<int, QPixmap> WebGraphicHash;
88 Q_GLOBAL_STATIC(WebGraphicHash, _graphics)
89
90 static void earlyClearGraphics()
91 {
92     _graphics()->clear();
93 }
94
95 static WebGraphicHash* graphics()
96 {
97     WebGraphicHash* hash = _graphics();
98
99     if (hash->isEmpty()) {
100
101         // prevent ~QPixmap running after ~QApplication (leaks native pixmaps)
102         qAddPostRoutine(earlyClearGraphics);
103
104         hash->insert(QWebSettings::MissingImageGraphic, QPixmap(QLatin1String(":webkit/resources/missingImage.png")));
105         hash->insert(QWebSettings::MissingPluginGraphic, QPixmap(QLatin1String(":webkit/resources/nullPlugin.png")));
106         hash->insert(QWebSettings::DefaultFrameIconGraphic, QPixmap(QLatin1String(":webkit/resources/urlIcon.png")));
107         hash->insert(QWebSettings::TextAreaSizeGripCornerGraphic, QPixmap(QLatin1String(":webkit/resources/textAreaResizeCorner.png")));
108         hash->insert(QWebSettings::DeleteButtonGraphic, QPixmap(QLatin1String(":webkit/resources/deleteButton.png")));
109         hash->insert(QWebSettings::InputSpeechButtonGraphic, QPixmap(QLatin1String(":webkit/resources/inputSpeech.png")));
110         hash->insert(QWebSettings::SearchCancelButtonGraphic, QApplication::style()->standardPixmap(QStyle::SP_DialogCloseButton));
111         hash->insert(QWebSettings::SearchCancelButtonPressedGraphic, QApplication::style()->standardPixmap(QStyle::SP_DialogCloseButton));
112     }
113
114     return hash;
115 }
116
117 Q_GLOBAL_STATIC(QList<QWebSettingsPrivate*>, allSettings);
118
119 void QWebSettingsPrivate::apply()
120 {
121     if (settings) {
122         settings->setTextAreasAreResizable(true);
123
124         QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
125
126         QString family = fontFamilies.value(QWebSettings::StandardFont,
127                                             global->fontFamilies.value(QWebSettings::StandardFont));
128         settings->setStandardFontFamily(family);
129
130         family = fontFamilies.value(QWebSettings::FixedFont,
131                                     global->fontFamilies.value(QWebSettings::FixedFont));
132         settings->setFixedFontFamily(family);
133
134         family = fontFamilies.value(QWebSettings::SerifFont,
135                                     global->fontFamilies.value(QWebSettings::SerifFont));
136         settings->setSerifFontFamily(family);
137
138         family = fontFamilies.value(QWebSettings::SansSerifFont,
139                                     global->fontFamilies.value(QWebSettings::SansSerifFont));
140         settings->setSansSerifFontFamily(family);
141
142         family = fontFamilies.value(QWebSettings::CursiveFont,
143                                     global->fontFamilies.value(QWebSettings::CursiveFont));
144         settings->setCursiveFontFamily(family);
145
146         family = fontFamilies.value(QWebSettings::FantasyFont,
147                                     global->fontFamilies.value(QWebSettings::FantasyFont));
148         settings->setFantasyFontFamily(family);
149
150         int size = fontSizes.value(QWebSettings::MinimumFontSize,
151                                    global->fontSizes.value(QWebSettings::MinimumFontSize));
152         settings->setMinimumFontSize(size);
153
154         size = fontSizes.value(QWebSettings::MinimumLogicalFontSize,
155                                    global->fontSizes.value(QWebSettings::MinimumLogicalFontSize));
156         settings->setMinimumLogicalFontSize(size);
157
158         size = fontSizes.value(QWebSettings::DefaultFontSize,
159                                    global->fontSizes.value(QWebSettings::DefaultFontSize));
160         settings->setDefaultFontSize(size);
161
162         size = fontSizes.value(QWebSettings::DefaultFixedFontSize,
163                                    global->fontSizes.value(QWebSettings::DefaultFixedFontSize));
164         settings->setDefaultFixedFontSize(size);
165
166         bool value = attributes.value(QWebSettings::AutoLoadImages,
167                                       global->attributes.value(QWebSettings::AutoLoadImages));
168         settings->setLoadsImagesAutomatically(value);
169
170         value = attributes.value(QWebSettings::JavascriptEnabled,
171                                       global->attributes.value(QWebSettings::JavascriptEnabled));
172         settings->setJavaScriptEnabled(value);
173 #if USE(ACCELERATED_COMPOSITING)
174         value = attributes.value(QWebSettings::AcceleratedCompositingEnabled,
175                                       global->attributes.value(QWebSettings::AcceleratedCompositingEnabled));
176
177         settings->setAcceleratedCompositingEnabled(value);
178         settings->setAcceleratedCompositingFor3DTransformsEnabled(value);
179         settings->setAcceleratedCompositingForAnimationEnabled(value);
180 #if USE(TEXTURE_MAPPER)
181         settings->setAcceleratedCompositingForVideoEnabled(false);
182         settings->setAcceleratedCompositingForPluginsEnabled(false);
183 #endif
184 #endif
185 #if ENABLE(WEBGL)
186         value = attributes.value(QWebSettings::WebGLEnabled,
187                                  global->attributes.value(QWebSettings::WebGLEnabled));
188
189         settings->setWebGLEnabled(value);
190 #if USE(ACCELERATED_COMPOSITING)
191         settings->setAcceleratedCompositingForCanvasEnabled(value);
192 #endif
193 #endif
194
195         value = attributes.value(QWebSettings::HyperlinkAuditingEnabled,
196                                  global->attributes.value(QWebSettings::HyperlinkAuditingEnabled));
197
198         settings->setHyperlinkAuditingEnabled(value);
199  
200         value = attributes.value(QWebSettings::JavascriptCanOpenWindows,
201                                       global->attributes.value(QWebSettings::JavascriptCanOpenWindows));
202         settings->setJavaScriptCanOpenWindowsAutomatically(value);
203
204         value = attributes.value(QWebSettings::JavascriptCanCloseWindows,
205                                       global->attributes.value(QWebSettings::JavascriptCanCloseWindows));
206         settings->setAllowScriptsToCloseWindows(value);
207
208         value = attributes.value(QWebSettings::JavaEnabled,
209                                       global->attributes.value(QWebSettings::JavaEnabled));
210         settings->setJavaEnabled(value);
211
212         value = attributes.value(QWebSettings::PluginsEnabled,
213                                       global->attributes.value(QWebSettings::PluginsEnabled));
214         settings->setPluginsEnabled(value);
215
216         value = attributes.value(QWebSettings::PrivateBrowsingEnabled,
217                                       global->attributes.value(QWebSettings::PrivateBrowsingEnabled));
218         settings->setPrivateBrowsingEnabled(value);
219
220         value = attributes.value(QWebSettings::SpatialNavigationEnabled,
221                                       global->attributes.value(QWebSettings::SpatialNavigationEnabled));
222         settings->setSpatialNavigationEnabled(value);
223
224         value = attributes.value(QWebSettings::JavascriptCanAccessClipboard,
225                                       global->attributes.value(QWebSettings::JavascriptCanAccessClipboard));
226         settings->setDOMPasteAllowed(value);
227         settings->setJavaScriptCanAccessClipboard(value);
228
229         value = attributes.value(QWebSettings::DeveloperExtrasEnabled,
230                                       global->attributes.value(QWebSettings::DeveloperExtrasEnabled));
231         settings->setDeveloperExtrasEnabled(value);
232
233         value = attributes.value(QWebSettings::FrameFlatteningEnabled,
234                                       global->attributes.value(QWebSettings::FrameFlatteningEnabled));
235         settings->setFrameFlatteningEnabled(value);
236
237         QUrl location = !userStyleSheetLocation.isEmpty() ? userStyleSheetLocation : global->userStyleSheetLocation;
238         settings->setUserStyleSheetLocation(WebCore::KURL(location));
239
240         QString encoding = !defaultTextEncoding.isEmpty() ? defaultTextEncoding: global->defaultTextEncoding;
241         settings->setDefaultTextEncodingName(encoding);
242
243         QString storagePath = !localStoragePath.isEmpty() ? localStoragePath : global->localStoragePath;
244         settings->setLocalStorageDatabasePath(storagePath);
245
246         value = attributes.value(QWebSettings::PrintElementBackgrounds,
247                                       global->attributes.value(QWebSettings::PrintElementBackgrounds));
248         settings->setShouldPrintBackgrounds(value);
249
250 #if ENABLE(DATABASE)
251         value = attributes.value(QWebSettings::OfflineStorageDatabaseEnabled,
252                                       global->attributes.value(QWebSettings::OfflineStorageDatabaseEnabled));
253         WebCore::AbstractDatabase::setIsAvailable(value);
254 #endif
255
256         value = attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled,
257                                       global->attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled));
258         settings->setOfflineWebApplicationCacheEnabled(value);
259
260         value = attributes.value(QWebSettings::LocalStorageEnabled,
261                                       global->attributes.value(QWebSettings::LocalStorageEnabled));
262         settings->setLocalStorageEnabled(value);
263
264         value = attributes.value(QWebSettings::LocalContentCanAccessRemoteUrls,
265                                       global->attributes.value(QWebSettings::LocalContentCanAccessRemoteUrls));
266         settings->setAllowUniversalAccessFromFileURLs(value);
267
268         value = attributes.value(QWebSettings::LocalContentCanAccessFileUrls,
269                                       global->attributes.value(QWebSettings::LocalContentCanAccessFileUrls));
270         settings->setAllowFileAccessFromFileURLs(value);
271
272         value = attributes.value(QWebSettings::XSSAuditingEnabled,
273                                       global->attributes.value(QWebSettings::XSSAuditingEnabled));
274         settings->setXSSAuditorEnabled(value);
275
276 #if ENABLE(TILED_BACKING_STORE)
277         value = attributes.value(QWebSettings::TiledBackingStoreEnabled,
278                                       global->attributes.value(QWebSettings::TiledBackingStoreEnabled));
279         settings->setTiledBackingStoreEnabled(value);
280 #endif
281
282         value = attributes.value(QWebSettings::SiteSpecificQuirksEnabled,
283                                       global->attributes.value(QWebSettings::SiteSpecificQuirksEnabled));
284         settings->setNeedsSiteSpecificQuirks(value);
285
286         settings->setUsesPageCache(WebCore::pageCache()->capacity());
287
288 #if ENABLE(PASSWORD_ECHO)
289         settings->setPasswordEchoEnabled(true);
290         settings->setPasswordEchoDurationInSeconds(1);
291 #endif
292     } else {
293         QList<QWebSettingsPrivate*> settings = *::allSettings();
294         for (int i = 0; i < settings.count(); ++i)
295             settings[i]->apply();
296     }
297 }
298
299 /*!
300     Returns the global settings object.
301
302     Any setting changed on the default object is automatically applied to all
303     QWebPage instances where the particular setting is not overriden already.
304 */
305 QWebSettings* QWebSettings::globalSettings()
306 {
307     static QWebSettings* global = 0;
308     if (!global)
309         global = new QWebSettings;
310     return global;
311 }
312
313 /*!
314     \class QWebSettings
315     \since 4.4
316     \brief The QWebSettings class provides an object to store the settings used
317     by QWebPage and QWebFrame.
318
319     \inmodule QtWebKit
320
321     Each QWebPage object has its own QWebSettings object, which configures the
322     settings for that page. If a setting is not configured, then it is looked
323     up in the global settings object, which can be accessed using
324     globalSettings().
325
326     QWebSettings allows configuration of browser properties, such as font sizes and
327     families, the location of a custom style sheet, and generic attributes like
328     JavaScript and plugins. Individual attributes are set using the setAttribute()
329     function. The \l{QWebSettings::WebAttribute}{WebAttribute} enum further describes
330     each attribute.
331
332     QWebSettings also configures global properties such as the web page memory
333     cache, icon database, local database storage and offline
334     applications storage.
335
336     \section1 Enabling Plugins
337
338     Support for browser plugins can enabled by setting the
339     \l{QWebSettings::PluginsEnabled}{PluginsEnabled} attribute. For many applications,
340     this attribute is enabled for all pages by setting it on the
341     \l{globalSettings()}{global settings object}. QtWebKit will always ignore this setting
342     when processing Qt plugins. The decision to allow a Qt plugin is made by the client
343     in its reimplementation of QWebPage::createPlugin().
344
345     \section1 Web Application Support
346
347     WebKit provides support for features specified in \l{HTML 5} that improve the
348     performance and capabilities of Web applications. These include client-side
349     (offline) storage and the use of a Web application cache.
350
351     Client-side (offline) storage is an improvement over the use of cookies to
352     store persistent data in Web applications. Applications can configure and
353     enable the use of an offline storage database by calling the
354     setOfflineStoragePath() with an appropriate file path, and can limit the quota
355     for each application by calling setOfflineStorageDefaultQuota().
356
357     \sa QWebPage::settings(), QWebView::settings(), {Web Browser}
358 */
359
360 /*!
361     \enum QWebSettings::FontFamily
362
363     This enum describes the generic font families defined by CSS 2.
364     For more information see the
365     \l{http://www.w3.org/TR/REC-CSS2/fonts.html#generic-font-families}{CSS standard}.
366
367     \value StandardFont
368     \value FixedFont
369     \value SerifFont
370     \value SansSerifFont
371     \value CursiveFont
372     \value FantasyFont
373 */
374
375 /*!
376     \enum QWebSettings::FontSize
377
378     This enum describes the font sizes configurable through QWebSettings.
379
380     \value MinimumFontSize The hard minimum font size.
381     \value MinimumLogicalFontSize The minimum logical font size that is applied
382         when zooming out with QWebFrame::setTextSizeMultiplier().
383     \value DefaultFontSize The default font size for regular text.
384     \value DefaultFixedFontSize The default font size for fixed-pitch text.
385 */
386
387 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
388 /*!
389     \enum QWebSettings::ThirdPartyCookiePolicy
390
391     This enum describes the policies configurable for accepting and sending
392     third-party cookies. These are cookies that are set or retrieved when fetching
393     a resource that is stored for a different registry-controlled domain from the page containing it.
394
395     \value AlwaysAllowThirdPartyCookies Allow third-party resources to set and retrieve cookies.
396     \value AlwaysBlockThirdPartyCookies Never allow third-party resources to set and retrieve cookies.
397     \value AllowThirdPartyWithExistingCookies If the cookie jar already contains cookies
398         from a third-party, allow it to set and retrieve new and existing cookies.
399
400     \since QtWebKit 2,3
401 */
402 #endif
403
404 /*!
405     \enum QWebSettings::WebGraphic
406
407     This enums describes the standard graphical elements used in webpages.
408
409     \value MissingImageGraphic The replacement graphic shown when an image could not be loaded.
410     \value MissingPluginGraphic The replacement graphic shown when a plugin could not be loaded.
411     \value DefaultFrameIconGraphic The default icon for QWebFrame::icon().
412     \value TextAreaSizeGripCornerGraphic The graphic shown for the size grip of text areas.
413     \value DeleteButtonGraphic The graphic shown for the WebKit-Editing-Delete-Button in Deletion UI.
414     \value InputSpeechButtonGraphic The graphic shown in input fields that support speech recognition.
415     \value SearchCancelButtonGraphic The graphic shown for clearing the text in a search field.
416     \value SearchCancelButtonPressedGraphic The graphic shown when SearchCancelButtonGraphic is pressed.
417 */
418
419 /*!
420     \enum QWebSettings::WebAttribute
421
422     This enum describes various attributes that are configurable through QWebSettings.
423
424     \value AutoLoadImages Specifies whether images are automatically loaded in
425         web pages. This is enabled by default.
426     \value DnsPrefetchEnabled Specifies whether QtWebkit will try to pre-fetch DNS entries to
427         speed up browsing. This only works as a global attribute. Only for Qt 4.6 and later. This is disabled by default.
428     \value JavascriptEnabled Enables or disables the running of JavaScript
429         programs. This is enabled by default
430     \value JavaEnabled Enables or disables Java applets.
431         Currently Java applets are not supported.
432     \value PluginsEnabled Enables or disables plugins in Web pages (e.g. using NPAPI). Qt plugins
433         with a mimetype such as "application/x-qt-plugin" are not affected by this setting. This is disabled by default.
434     \value PrivateBrowsingEnabled Private browsing prevents WebKit from
435         recording visited pages in the history and storing web page icons. This is disabled by default.
436     \value JavascriptCanOpenWindows Specifies whether JavaScript programs
437         can open new windows. This is disabled by default.
438     \value JavascriptCanCloseWindows Specifies whether JavaScript programs
439         can close windows. This is disabled by default.
440     \value JavascriptCanAccessClipboard Specifies whether JavaScript programs
441         can read or write to the clipboard. This is disabled by default.
442     \value DeveloperExtrasEnabled Enables extra tools for Web developers.
443         Currently this enables the "Inspect" element in the context menu as
444         well as the use of QWebInspector which controls the web inspector
445         for web site debugging. This is disabled by default.
446     \value SpatialNavigationEnabled Enables or disables the Spatial Navigation
447         feature, which consists in the ability to navigate between focusable
448         elements in a Web page, such as hyperlinks and form controls, by using
449         Left, Right, Up and Down arrow keys. For example, if a user presses the
450         Right key, heuristics determine whether there is an element he might be
451         trying to reach towards the right and which element he probably wants.
452         This is disabled by default.
453     \value LinksIncludedInFocusChain Specifies whether hyperlinks should be
454         included in the keyboard focus chain. This is enabled by default.
455     \value ZoomTextOnly Specifies whether the zoom factor on a frame applies
456         only to the text or to all content. This is disabled by default.
457     \value PrintElementBackgrounds Specifies whether the background color and images
458         are also drawn when the page is printed. This is enabled by default.
459     \value OfflineStorageDatabaseEnabled Specifies whether support for the HTML 5
460         offline storage feature is enabled or not. This is disabled by default.
461     \value OfflineWebApplicationCacheEnabled Specifies whether support for the HTML 5
462         web application cache feature is enabled or not. This is disabled by default.
463     \value LocalStorageEnabled Specifies whether support for the HTML 5
464         local storage feature is enabled or not. This is disabled by default.
465     \value LocalStorageDatabaseEnabled \e{This enum value is deprecated.} Use
466         QWebSettings::LocalStorageEnabled instead.
467     \value LocalContentCanAccessRemoteUrls Specifies whether locally loaded documents are
468         allowed to access remote urls. This is disabled by default. For more information
469         about security origins and local vs. remote content see QWebSecurityOrigin.
470     \value LocalContentCanAccessFileUrls Specifies whether locally loaded documents are
471         allowed to access other local urls. This is enabled by default. For more information
472         about security origins and local vs. remote content see QWebSecurityOrigin.
473     \value XSSAuditingEnabled Specifies whether load requests should be monitored for cross-site
474         scripting attempts. Suspicious scripts will be blocked and reported in the inspector's
475         JavaScript console. Enabling this feature might have an impact on performance
476         and it is disabled by default.
477     \value AcceleratedCompositingEnabled This feature, when used in conjunction with
478         QGraphicsWebView, accelerates animations of web content. CSS animations of the transform and
479         opacity properties will be rendered by composing the cached content of the animated elements.
480         This is enabled by default.
481     \value TiledBackingStoreEnabled This setting enables the tiled backing store feature
482         for a QGraphicsWebView. With the tiled backing store enabled, the web page contents in and around
483         the current visible area is speculatively cached to bitmap tiles. The tiles are automatically kept
484         in sync with the web page as it changes. Enabling tiling can significantly speed up painting heavy 
485         operations like scrolling. Enabling the feature increases memory consumption. It does not work well 
486         with contents using CSS fixed positioning (see also \l{QGraphicsWebView::}{resizesToContents} property).
487         \l{QGraphicsWebView::}{tiledBackingStoreFrozen} property allows application to temporarily
488         freeze the contents of the backing store. This is disabled by default.
489     \value FrameFlatteningEnabled With this setting each subframe is expanded to its contents.
490         On touch devices, it is desired to not have any scrollable sub parts of the page
491         as it results in a confusing user experience, with scrolling sometimes scrolling sub parts
492         and at other times scrolling the page itself. For this reason iframes and framesets are
493         barely usable on touch devices. This will flatten all the frames to become one scrollable page.
494         This is disabled by default.
495     \value SiteSpecificQuirksEnabled This setting enables WebKit's workaround for broken sites. It is
496         enabled by default.
497 */
498
499 /*!
500     \internal
501 */
502 QWebSettings::QWebSettings()
503     : d(new QWebSettingsPrivate)
504 {
505     // Initialize our global defaults
506     d->fontSizes.insert(QWebSettings::MinimumFontSize, 0);
507     d->fontSizes.insert(QWebSettings::MinimumLogicalFontSize, 0);
508     d->fontSizes.insert(QWebSettings::DefaultFontSize, 16);
509     d->fontSizes.insert(QWebSettings::DefaultFixedFontSize, 13);
510
511     QFont defaultFont;
512     defaultFont.setStyleHint(QFont::Serif);
513     d->fontFamilies.insert(QWebSettings::StandardFont, defaultFont.defaultFamily());
514     d->fontFamilies.insert(QWebSettings::SerifFont, defaultFont.defaultFamily());
515
516     defaultFont.setStyleHint(QFont::Fantasy);
517     d->fontFamilies.insert(QWebSettings::FantasyFont, defaultFont.defaultFamily());
518
519     defaultFont.setStyleHint(QFont::Cursive);
520     d->fontFamilies.insert(QWebSettings::CursiveFont, defaultFont.defaultFamily());
521
522     defaultFont.setStyleHint(QFont::SansSerif);
523     d->fontFamilies.insert(QWebSettings::SansSerifFont, defaultFont.defaultFamily());
524
525     defaultFont.setStyleHint(QFont::Monospace);
526     d->fontFamilies.insert(QWebSettings::FixedFont, defaultFont.defaultFamily());
527
528     d->attributes.insert(QWebSettings::AutoLoadImages, true);
529     d->attributes.insert(QWebSettings::DnsPrefetchEnabled, false);
530     d->attributes.insert(QWebSettings::JavascriptEnabled, true);
531     d->attributes.insert(QWebSettings::SpatialNavigationEnabled, false);
532     d->attributes.insert(QWebSettings::LinksIncludedInFocusChain, true);
533     d->attributes.insert(QWebSettings::ZoomTextOnly, false);
534     d->attributes.insert(QWebSettings::PrintElementBackgrounds, true);
535     d->attributes.insert(QWebSettings::OfflineStorageDatabaseEnabled, false);
536     d->attributes.insert(QWebSettings::OfflineWebApplicationCacheEnabled, false);
537     d->attributes.insert(QWebSettings::LocalStorageEnabled, false);
538     d->attributes.insert(QWebSettings::LocalContentCanAccessRemoteUrls, false);
539     d->attributes.insert(QWebSettings::LocalContentCanAccessFileUrls, true);
540     d->attributes.insert(QWebSettings::AcceleratedCompositingEnabled, true);
541     d->attributes.insert(QWebSettings::WebGLEnabled, false);
542     d->attributes.insert(QWebSettings::HyperlinkAuditingEnabled, false);
543     d->attributes.insert(QWebSettings::TiledBackingStoreEnabled, false);
544     d->attributes.insert(QWebSettings::FrameFlatteningEnabled, false);
545     d->attributes.insert(QWebSettings::SiteSpecificQuirksEnabled, true);
546     d->offlineStorageDefaultQuota = 5 * 1024 * 1024;
547     d->defaultTextEncoding = QLatin1String("iso-8859-1");
548 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
549     d->thirdPartyCookiePolicy = AlwaysAllowThirdPartyCookies;
550 #endif
551 }
552
553 /*!
554     \internal
555 */
556 QWebSettings::QWebSettings(WebCore::Settings* settings)
557     : d(new QWebSettingsPrivate(settings))
558 {
559     d->settings = settings;
560     d->apply();
561     allSettings()->append(d);
562 }
563
564 /*!
565     \internal
566 */
567 QWebSettings::~QWebSettings()
568 {
569     if (d->settings)
570         allSettings()->removeAll(d);
571
572     delete d;
573 }
574
575 /*!
576     Sets the font size for \a type to \a size.
577 */
578 void QWebSettings::setFontSize(FontSize type, int size)
579 {
580     d->fontSizes.insert(type, size);
581     d->apply();
582 }
583
584 /*!
585     Returns the default font size for \a type.
586 */
587 int QWebSettings::fontSize(FontSize type) const
588 {
589     int defaultValue = 0;
590     if (d->settings) {
591         QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
592         defaultValue = global->fontSizes.value(type);
593     }
594     return d->fontSizes.value(type, defaultValue);
595 }
596
597 /*!
598     Resets the font size for \a type to the size specified in the global
599     settings object.
600
601     This function has no effect on the global QWebSettings instance.
602 */
603 void QWebSettings::resetFontSize(FontSize type)
604 {
605     if (d->settings) {
606         d->fontSizes.remove(type);
607         d->apply();
608     }
609 }
610
611 /*!
612     Specifies the location of a user stylesheet to load with every web page.
613
614     The \a location must be either a path on the local filesystem, or a data URL
615     with UTF-8 and Base64 encoded data, such as:
616
617     "data:text/css;charset=utf-8;base64,cCB7IGJhY2tncm91bmQtY29sb3I6IHJlZCB9Ow=="
618
619     \note If the base64 data is not valid, the style will not be applied.
620
621     \sa userStyleSheetUrl()
622 */
623 void QWebSettings::setUserStyleSheetUrl(const QUrl& location)
624 {
625     d->userStyleSheetLocation = location;
626     d->apply();
627 }
628
629 /*!
630     Returns the location of the user stylesheet.
631
632     \sa setUserStyleSheetUrl()
633 */
634 QUrl QWebSettings::userStyleSheetUrl() const
635 {
636     return d->userStyleSheetLocation;
637 }
638
639 /*!
640     \since 4.6
641     Specifies the default text encoding system.
642
643     The \a encoding, must be a string describing an encoding such as "utf-8",
644     "iso-8859-1", etc. If left empty a default value will be used. For a more
645     extensive list of encoding names see \l{QTextCodec}
646
647     \sa defaultTextEncoding()
648 */
649 void QWebSettings::setDefaultTextEncoding(const QString& encoding)
650 {
651     d->defaultTextEncoding = encoding;
652     d->apply();
653 }
654
655 /*!
656     \since 4.6
657     Returns the default text encoding.
658
659     \sa setDefaultTextEncoding()
660 */
661 QString QWebSettings::defaultTextEncoding() const
662 {
663     return d->defaultTextEncoding;
664 }
665
666 /*!
667     Sets the path of the icon database to \a path. The icon database is used
668     to store "favicons" associated with web sites.
669
670     \a path must point to an existing directory.
671
672     Setting an empty path disables the icon database.
673
674     \sa iconDatabasePath(), clearIconDatabase()
675 */
676 void QWebSettings::setIconDatabasePath(const QString& path)
677 {
678 #if ENABLE(ICONDATABASE)
679     // Make sure that IconDatabaseClientQt is instantiated.
680     WebCore::IconDatabaseClientQt::instance();
681 #endif
682
683     WebCore::IconDatabase::delayDatabaseCleanup();
684
685     if (!path.isEmpty()) {
686         WebCore::iconDatabase().setEnabled(true);
687         QFileInfo info(path);
688         if (info.isDir() && info.isWritable())
689             WebCore::iconDatabase().open(path, WebCore::IconDatabase::defaultDatabaseFilename());
690     } else {
691         WebCore::iconDatabase().setEnabled(false);
692         WebCore::iconDatabase().close();
693     }
694 }
695
696 /*!
697     Returns the path of the icon database or an empty string if the icon
698     database is disabled.
699
700     \sa setIconDatabasePath(), clearIconDatabase()
701 */
702 QString QWebSettings::iconDatabasePath()
703 {
704     if (WebCore::iconDatabase().isEnabled() && WebCore::iconDatabase().isOpen())
705         return WebCore::iconDatabase().databasePath();
706     else
707         return QString();
708 }
709
710 /*!
711     Clears the icon database.
712 */
713 void QWebSettings::clearIconDatabase()
714 {
715     if (WebCore::iconDatabase().isEnabled() && WebCore::iconDatabase().isOpen())
716         WebCore::iconDatabase().removeAllIcons();
717 }
718
719 /*!
720     Returns the web site's icon for \a url.
721
722     If the web site does not specify an icon \bold OR if the icon is not in the
723     database, a null QIcon is returned.
724
725     \note The returned icon's size is arbitrary.
726
727     \sa setIconDatabasePath()
728 */
729 QIcon QWebSettings::iconForUrl(const QUrl& url)
730 {
731     WebCore::Image* image = WebCore::iconDatabase().synchronousIconForPageURL(WebCore::KURL(url).string(),
732                                 WebCore::IntSize(16, 16));
733     if (!image)
734         return QPixmap();
735
736     QPixmap* icon = image->nativeImageForCurrentFrame();
737     if (!icon)
738         return QPixmap();
739
740     return* icon;
741 }
742
743 /*
744     Returns the plugin database object.
745
746 QWebPluginDatabase *QWebSettings::pluginDatabase()
747 {
748     static QWebPluginDatabase* database = 0;
749     if (!database)
750         database = new QWebPluginDatabase();
751     return database;
752 }
753 */
754
755 /*!
756     Sets \a graphic to be drawn when QtWebKit needs to draw an image of the
757     given \a type.
758
759     For example, when an image cannot be loaded, the pixmap specified by
760     \l{QWebSettings::WebGraphic}{MissingImageGraphic} is drawn instead.
761
762     \sa webGraphic()
763 */
764 void QWebSettings::setWebGraphic(WebGraphic type, const QPixmap& graphic)
765 {
766     WebGraphicHash* h = graphics();
767     if (graphic.isNull())
768         h->remove(type);
769     else
770         h->insert(type, graphic);
771 }
772
773 /*!
774     Returns a previously set pixmap used to draw replacement graphics of the
775     specified \a type.
776
777     \sa setWebGraphic()
778 */
779 QPixmap QWebSettings::webGraphic(WebGraphic type)
780 {
781     return graphics()->value(type);
782 }
783
784 /*!
785     Frees up as much memory as possible by cleaning all memory caches such
786     as page, object and font cache.
787
788     \since 4.6
789  */
790 void QWebSettings::clearMemoryCaches()
791 {
792     // Turn the cache on and off.  Disabling the object cache will remove all
793     // resources from the cache.  They may still live on if they are referenced
794     // by some Web page though.
795     if (!WebCore::memoryCache()->disabled()) {
796         WebCore::memoryCache()->setDisabled(true);
797         WebCore::memoryCache()->setDisabled(false);
798     }
799
800     int pageCapacity = WebCore::pageCache()->capacity();
801     // Setting size to 0, makes all pages be released.
802     WebCore::pageCache()->setCapacity(0);
803     WebCore::pageCache()->releaseAutoreleasedPagesNow();
804     WebCore::pageCache()->setCapacity(pageCapacity);
805
806     // Invalidating the font cache and freeing all inactive font data.
807     WebCore::fontCache()->invalidate();
808
809     // Empty the Cross-Origin Preflight cache
810     WebCore::CrossOriginPreflightResultCache::shared().empty();
811 }
812
813 /*!
814     Sets the maximum number of pages to hold in the memory page cache to \a pages.
815
816     The Page Cache allows for a nicer user experience when navigating forth or back
817     to pages in the forward/back history, by pausing and resuming up to \a pages.
818
819     For more information about the feature, please refer to:
820
821     http://webkit.org/blog/427/webkit-page-cache-i-the-basics/
822 */
823 void QWebSettings::setMaximumPagesInCache(int pages)
824 {
825     QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
826     WebCore::pageCache()->setCapacity(qMax(0, pages));
827     global->apply();
828 }
829
830 /*!
831     Returns the maximum number of web pages that are kept in the memory cache.
832 */
833 int QWebSettings::maximumPagesInCache()
834 {
835     return WebCore::pageCache()->capacity();
836 }
837
838 /*!
839    Specifies the capacities for the memory cache for dead objects such as
840    stylesheets or scripts.
841
842    The \a cacheMinDeadCapacity specifies the \e minimum number of bytes that
843    dead objects should consume when the cache is under pressure.
844
845    \a cacheMaxDead is the \e maximum number of bytes that dead objects should
846    consume when the cache is \bold not under pressure.
847
848    \a totalCapacity specifies the \e maximum number of bytes that the cache
849    should consume \bold overall.
850
851    The cache is enabled by default. Calling setObjectCacheCapacities(0, 0, 0)
852    will disable the cache. Calling it with one non-zero enables it again.
853 */
854 void QWebSettings::setObjectCacheCapacities(int cacheMinDeadCapacity, int cacheMaxDead, int totalCapacity)
855 {
856     bool disableCache = !cacheMinDeadCapacity && !cacheMaxDead && !totalCapacity;
857     WebCore::memoryCache()->setDisabled(disableCache);
858
859     WebCore::memoryCache()->setCapacities(qMax(0, cacheMinDeadCapacity),
860                                     qMax(0, cacheMaxDead),
861                                     qMax(0, totalCapacity));
862 }
863
864 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
865 /*!
866     Sets the third-party cookie policy, the default is AlwaysAllowThirdPartyCookies.
867 */
868 void QWebSettings::setThirdPartyCookiePolicy(ThirdPartyCookiePolicy policy)
869 {
870     d->thirdPartyCookiePolicy = policy;
871 }
872
873 /*!
874     Returns the third-party cookie policy.
875 */
876 QWebSettings::ThirdPartyCookiePolicy QWebSettings::thirdPartyCookiePolicy() const
877 {
878     return d->thirdPartyCookiePolicy;
879 }
880 #endif
881
882 /*!
883     Sets the actual font family to \a family for the specified generic family,
884     \a which.
885 */
886 void QWebSettings::setFontFamily(FontFamily which, const QString& family)
887 {
888     d->fontFamilies.insert(which, family);
889     d->apply();
890 }
891
892 /*!
893     Returns the actual font family for the specified generic font family,
894     \a which.
895 */
896 QString QWebSettings::fontFamily(FontFamily which) const
897 {
898     QString defaultValue;
899     if (d->settings) {
900         QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
901         defaultValue = global->fontFamilies.value(which);
902     }
903     return d->fontFamilies.value(which, defaultValue);
904 }
905
906 /*!
907     Resets the actual font family specified by \a which to the one set
908     in the global QWebSettings instance.
909
910     This function has no effect on the global QWebSettings instance.
911 */
912 void QWebSettings::resetFontFamily(FontFamily which)
913 {
914     if (d->settings) {
915         d->fontFamilies.remove(which);
916         d->apply();
917     }
918 }
919
920 /*!
921     \fn void QWebSettings::setAttribute(WebAttribute attribute, bool on)
922
923     Enables or disables the specified \a attribute feature depending on the
924     value of \a on.
925 */
926 void QWebSettings::setAttribute(WebAttribute attr, bool on)
927 {
928     d->attributes.insert(attr, on);
929     d->apply();
930 }
931
932 /*!
933     \fn bool QWebSettings::testAttribute(WebAttribute attribute) const
934
935     Returns true if \a attribute is enabled; otherwise returns false.
936 */
937 bool QWebSettings::testAttribute(WebAttribute attr) const
938 {
939     bool defaultValue = false;
940     if (d->settings) {
941         QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
942         defaultValue = global->attributes.value(attr);
943     }
944     return d->attributes.value(attr, defaultValue);
945 }
946
947 /*!
948     \fn void QWebSettings::resetAttribute(WebAttribute attribute)
949
950     Resets the setting of \a attribute to the value specified in the
951     global QWebSettings instance.
952
953     This function has no effect on the global QWebSettings instance.
954
955     \sa globalSettings()
956 */
957 void QWebSettings::resetAttribute(WebAttribute attr)
958 {
959     if (d->settings) {
960         d->attributes.remove(attr);
961         d->apply();
962     }
963 }
964
965 /*!
966     \since 4.5
967
968     Sets \a path as the save location for HTML5 client-side database storage data.
969
970     \a path must point to an existing directory.
971
972     Setting an empty path disables the feature.
973
974     Support for client-side databases can enabled by setting the
975     \l{QWebSettings::OfflineStorageDatabaseEnabled}{OfflineStorageDatabaseEnabled} attribute.
976
977     \sa offlineStoragePath()
978 */
979 void QWebSettings::setOfflineStoragePath(const QString& path)
980 {
981 #if ENABLE(DATABASE)
982     WebCore::DatabaseTracker::tracker().setDatabaseDirectoryPath(path);
983 #endif
984 }
985
986 /*!
987     \since 4.5
988
989     Returns the path of the HTML5 client-side database storage or an empty string if the
990     feature is disabled.
991
992     \sa setOfflineStoragePath()
993 */
994 QString QWebSettings::offlineStoragePath()
995 {
996 #if ENABLE(DATABASE)
997     return WebCore::DatabaseTracker::tracker().databaseDirectoryPath();
998 #else
999     return QString();
1000 #endif
1001 }
1002
1003 /*!
1004     \since 4.5
1005
1006     Sets the value of the default quota for new offline storage databases
1007     to \a maximumSize.
1008 */
1009 void QWebSettings::setOfflineStorageDefaultQuota(qint64 maximumSize)
1010 {
1011     QWebSettings::globalSettings()->d->offlineStorageDefaultQuota = maximumSize;
1012 }
1013
1014 /*!
1015     \since 4.5
1016
1017     Returns the value of the default quota for new offline storage databases.
1018 */
1019 qint64 QWebSettings::offlineStorageDefaultQuota()
1020 {
1021     return QWebSettings::globalSettings()->d->offlineStorageDefaultQuota;
1022 }
1023
1024 /*!
1025     \since 4.6
1026
1027     Sets the path for HTML5 offline web application cache storage to \a path.
1028
1029     An application cache acts like an HTTP cache in some sense. For documents
1030     that use the application cache via JavaScript, the loader engine will
1031     first ask the application cache for the contents, before hitting the
1032     network.
1033
1034     The feature is described in details at:
1035     http://dev.w3.org/html5/spec/Overview.html#appcache
1036
1037     \a path must point to an existing directory.
1038
1039     Setting an empty path disables the feature.
1040
1041     Support for offline web application cache storage can enabled by setting the
1042     \l{QWebSettings::OfflineWebApplicationCacheEnabled}{OfflineWebApplicationCacheEnabled} attribute.
1043
1044     \sa offlineWebApplicationCachePath()
1045 */
1046 void QWebSettings::setOfflineWebApplicationCachePath(const QString& path)
1047 {
1048 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
1049     WebCore::cacheStorage().setCacheDirectory(path);
1050 #endif
1051 }
1052
1053 /*!
1054     \since 4.6
1055
1056     Returns the path of the HTML5 offline web application cache storage
1057     or an empty string if the feature is disabled.
1058
1059     \sa setOfflineWebApplicationCachePath()
1060 */
1061 QString QWebSettings::offlineWebApplicationCachePath()
1062 {
1063 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
1064     return WebCore::cacheStorage().cacheDirectory();
1065 #else
1066     return QString();
1067 #endif
1068 }
1069
1070 /*!
1071     \since 4.6
1072
1073     Sets the value of the quota for the offline web application cache
1074     to \a maximumSize.
1075 */
1076 void QWebSettings::setOfflineWebApplicationCacheQuota(qint64 maximumSize)
1077 {
1078 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
1079     WebCore::cacheStorage().empty();
1080     WebCore::cacheStorage().vacuumDatabaseFile();
1081     WebCore::cacheStorage().setMaximumSize(maximumSize);
1082 #endif
1083 }
1084
1085 /*!
1086     \since 4.6
1087
1088     Returns the value of the quota for the offline web application cache.
1089 */
1090 qint64 QWebSettings::offlineWebApplicationCacheQuota()
1091 {
1092 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
1093     return WebCore::cacheStorage().maximumSize();
1094 #else
1095     return 0;
1096 #endif
1097 }
1098
1099 /*!
1100     \since 4.6
1101
1102     Sets the path for HTML5 local storage to \a path.
1103     
1104     For more information on HTML5 local storage see the
1105     \l{http://www.w3.org/TR/webstorage/#the-localstorage-attribute}{Web Storage standard}.
1106     
1107     Support for local storage can enabled by setting the
1108     \l{QWebSettings::LocalStorageEnabled}{LocalStorageEnabled} attribute.     
1109
1110     \sa localStoragePath()
1111 */
1112 void QWebSettings::setLocalStoragePath(const QString& path)
1113 {
1114     d->localStoragePath = path;
1115     d->apply();
1116 }
1117
1118 /*!
1119     \since 4.6
1120
1121     Returns the path for HTML5 local storage.
1122     
1123     \sa setLocalStoragePath()
1124 */
1125 QString QWebSettings::localStoragePath() const
1126 {
1127     return d->localStoragePath;
1128 }
1129
1130 /*!
1131     \since 4.6
1132
1133     Enables WebKit data persistence and sets the path to \a path.
1134     If \a path is empty, the user-specific data location specified by
1135     \l{QDesktopServices::DataLocation}{DataLocation} will be used instead.
1136
1137     This method will simultaneously set and enable the iconDatabasePath(),
1138     localStoragePath(), offlineStoragePath() and offlineWebApplicationCachePath().
1139
1140     \sa localStoragePath()
1141 */
1142 void QWebSettings::enablePersistentStorage(const QString& path)
1143 {
1144 #ifndef QT_NO_DESKTOPSERVICES
1145     QString storagePath;
1146
1147     if (path.isEmpty()) {
1148
1149         storagePath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
1150         if (storagePath.isEmpty())
1151             storagePath = WebCore::pathByAppendingComponent(QDir::homePath(), QCoreApplication::applicationName());
1152     } else
1153         storagePath = path;
1154
1155     WebCore::makeAllDirectories(storagePath);
1156
1157     QWebSettings::setIconDatabasePath(storagePath);
1158     QWebSettings::setOfflineWebApplicationCachePath(storagePath);
1159     QWebSettings::setOfflineStoragePath(WebCore::pathByAppendingComponent(storagePath, "Databases"));
1160     QWebSettings::globalSettings()->setLocalStoragePath(WebCore::pathByAppendingComponent(storagePath, "LocalStorage"));
1161     QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
1162     QWebSettings::globalSettings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
1163     QWebSettings::globalSettings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
1164
1165 #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
1166     // All applications can share the common QtWebkit cache file(s).
1167     // Path is not configurable and uses QDesktopServices::CacheLocation by default.
1168     QString cachePath = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
1169     WebCore::makeAllDirectories(cachePath);
1170
1171     QFileInfo info(cachePath);
1172     if (info.isDir() && info.isWritable()) {
1173         WebCore::PluginDatabase::setPersistentMetadataCacheEnabled(true);
1174         WebCore::PluginDatabase::setPersistentMetadataCachePath(cachePath);
1175     }
1176 #endif
1177 #endif
1178 }
1179
1180 /*!
1181     \fn QWebSettingsPrivate* QWebSettings::handle() const
1182     \internal
1183 */