Add a separate class for networking related storage
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / PlatformStrategiesQt.cpp
1 /*
2  * Copyright (C) 2007 Staikos Computing Services Inc. <info@staikos.net>
3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
4  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
5  * Copyright (C) 2010 Apple Inc. All rights reserved.
6  * Copyright (C) 2010 INdT - Instituto Nokia de Tecnologia
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "PlatformStrategiesQt.h"
32
33 #include "Chrome.h"
34 #include "ChromeClientQt.h"
35 #include "QWebPageAdapter.h"
36 #include "qwebhistoryinterface.h"
37 #include "qwebpluginfactory.h"
38
39 #include <IntSize.h>
40 #include <NotImplemented.h>
41 #include <Page.h>
42 #include <PageGroup.h>
43 #include <PlatformCookieJar.h>
44 #include <PluginDatabase.h>
45 #include <QCoreApplication>
46 #include <QLocale>
47 #include <wtf/MathExtras.h>
48
49 using namespace WebCore;
50
51 void PlatformStrategiesQt::initialize()
52 {
53     DEFINE_STATIC_LOCAL(PlatformStrategiesQt, platformStrategies, ());
54     Q_UNUSED(platformStrategies);
55 }
56
57 PlatformStrategiesQt::PlatformStrategiesQt()
58 {
59     setPlatformStrategies(this);
60 }
61
62
63 CookiesStrategy* PlatformStrategiesQt::createCookiesStrategy()
64 {
65     return this;
66 }
67
68 DatabaseStrategy* PlatformStrategiesQt::createDatabaseStrategy()
69 {
70     return this;
71 }
72
73 LoaderStrategy* PlatformStrategiesQt::createLoaderStrategy()
74 {
75     return this;
76 }
77
78 PasteboardStrategy* PlatformStrategiesQt::createPasteboardStrategy()
79 {
80     return 0;
81 }
82
83 PluginStrategy* PlatformStrategiesQt::createPluginStrategy()
84 {
85     return this;
86 }
87
88 SharedWorkerStrategy* PlatformStrategiesQt::createSharedWorkerStrategy()
89 {
90     return this;
91 }
92
93 VisitedLinkStrategy* PlatformStrategiesQt::createVisitedLinkStrategy()
94 {
95     return this;
96 }
97
98 void PlatformStrategiesQt::notifyCookiesChanged()
99 {
100 }
101
102 String PlatformStrategiesQt::cookiesForDOM(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
103 {
104     return WebCore::cookiesForDOM(session, firstParty, url);
105 }
106
107 void PlatformStrategiesQt::setCookiesFromDOM(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url, const String& cookieString)
108 {
109     WebCore::setCookiesFromDOM(session, firstParty, url, cookieString);
110 }
111
112 bool PlatformStrategiesQt::cookiesEnabled(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
113 {
114     return WebCore::cookiesEnabled(session, firstParty, url);
115 }
116
117 String PlatformStrategiesQt::cookieRequestHeaderFieldValue(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
118 {
119     return WebCore::cookieRequestHeaderFieldValue(session, firstParty, url);
120 }
121
122 bool PlatformStrategiesQt::getRawCookies(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url, Vector<Cookie>& rawCookies)
123 {
124     return WebCore::getRawCookies(session, firstParty, url, rawCookies);
125 }
126
127 void PlatformStrategiesQt::deleteCookie(const NetworkStorageSession& session, const KURL& url, const String& cookieName)
128 {
129     WebCore::deleteCookie(session, url, cookieName);
130 }
131
132 void PlatformStrategiesQt::refreshPlugins()
133 {
134     PluginDatabase::installedPlugins()->refresh();
135 }
136
137 void PlatformStrategiesQt::getPluginInfo(const WebCore::Page* page, Vector<WebCore::PluginInfo>& outPlugins)
138 {
139     QWebPageAdapter* qPage = static_cast<ChromeClientQt*>(page->chrome()->client())->m_webPage;
140     QWebPluginFactory* factory;
141     if (qPage && (factory = qPage->pluginFactory)) {
142
143         QList<QWebPluginFactory::Plugin> qplugins = factory->plugins();
144         for (int i = 0; i < qplugins.count(); ++i) {
145             const QWebPluginFactory::Plugin& qplugin = qplugins.at(i);
146             PluginInfo info;
147             info.name = qplugin.name;
148             info.desc = qplugin.description;
149
150             for (int j = 0; j < qplugin.mimeTypes.count(); ++j) {
151                 const QWebPluginFactory::MimeType& mimeType = qplugin.mimeTypes.at(j);
152
153                 MimeClassInfo mimeInfo;
154                 mimeInfo.type = mimeType.name;
155                 mimeInfo.desc = mimeType.description;
156                 for (int k = 0; k < mimeType.fileExtensions.count(); ++k)
157                     mimeInfo.extensions.append(mimeType.fileExtensions.at(k));
158
159                 info.mimes.append(mimeInfo);
160             }
161             outPlugins.append(info);
162         }
163     }
164
165     PluginDatabase* db = PluginDatabase::installedPlugins();
166     const Vector<PluginPackage*> &plugins = db->plugins();
167
168     outPlugins.resize(plugins.size());
169
170     for (int i = 0; i < plugins.size(); ++i) {
171         PluginInfo info;
172         PluginPackage* package = plugins[i];
173
174         info.name = package->name();
175         info.file = package->fileName();
176         info.desc = package->description();
177
178         const MIMEToDescriptionsMap& mimeToDescriptions = package->mimeToDescriptions();
179         MIMEToDescriptionsMap::const_iterator end = mimeToDescriptions.end();
180         for (MIMEToDescriptionsMap::const_iterator it = mimeToDescriptions.begin(); it != end; ++it) {
181             MimeClassInfo mime;
182
183             mime.type = it->key;
184             mime.desc = it->value;
185             mime.extensions = package->mimeToExtensions().get(mime.type);
186
187             info.mimes.append(mime);
188         }
189
190         outPlugins.append(info);
191     }
192
193 }
194
195 // VisitedLinkStrategy
196
197 bool PlatformStrategiesQt::isLinkVisited(Page* page, LinkHash hash, const KURL& baseURL, const AtomicString& attributeURL)
198 {
199     ASSERT(hash);
200
201     Vector<UChar, 512> url;
202     visitedURL(baseURL, attributeURL, url);
203
204     // If the Qt4.4 interface for the history is used, we will have to fallback
205     // to the old global history.
206     QWebHistoryInterface* iface = QWebHistoryInterface::defaultInterface();
207     if (iface)
208         return iface->historyContains(QString(reinterpret_cast<QChar*>(url.data()), url.size()));
209
210     return page->group().isLinkVisited(hash);
211 }
212
213 void PlatformStrategiesQt::addVisitedLink(Page* page, LinkHash hash)
214 {
215     page->group().addVisitedLinkHash(hash);
216 }