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