WebPlatformStrategies.cpp 15.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2010, 2011, 2012 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

26
#include "config.h"
27 28
#include "WebPlatformStrategies.h"

29 30
#if USE(PLATFORM_STRATEGIES)

31
#include "BlockingResponseMap.h"
32 33
#include "DataReference.h"
#include "NetworkResourceLoadParameters.h"
34
#include "PluginInfoStore.h"
35
#include "StorageNamespaceImpl.h"
36
#include "WebContextMessages.h"
37
#include "WebCookieManager.h"
38
#include "WebCoreArgumentCoders.h"
39
#include "WebErrors.h"
40
#include "WebPage.h"
41
#include "WebProcess.h"
42
#include "WebProcessProxyMessages.h"
43
#include <WebCore/Color.h>
44
#include <WebCore/KURL.h>
45
#include <WebCore/LoaderStrategy.h>
46
#include <WebCore/NetworkStorageSession.h>
47
#include <WebCore/NetworkingContext.h>
48
#include <WebCore/Page.h>
49
#include <WebCore/PlatformCookieJar.h>
50
#include <WebCore/PlatformPasteboard.h>
51
#include <WebCore/ResourceError.h>
52
#include <WebCore/StorageNamespace.h>
53
#include <wtf/Atomics.h>
54

55
#if ENABLE(NETWORK_PROCESS)
56
#include "BlobRegistryProxy.h"
57 58
#include "NetworkConnectionToWebProcessMessages.h"
#include "NetworkProcessConnection.h"
59
#include "WebResourceLoadScheduler.h"
60 61
#endif

62
// FIXME: Remove this once it works well enough to be the default.
63
#define ENABLE_UI_PROCESS_STORAGE 0
64

65 66 67 68 69 70 71 72 73 74 75
using namespace WebCore;

namespace WebKit {

void WebPlatformStrategies::initialize()
{
    DEFINE_STATIC_LOCAL(WebPlatformStrategies, platformStrategies, ());
    setPlatformStrategies(&platformStrategies);
}

WebPlatformStrategies::WebPlatformStrategies()
76
#if ENABLE(NETSCAPE_PLUGIN_API)
77 78
    : m_pluginCacheIsPopulated(false)
    , m_shouldRefreshPlugins(false)
79
#endif // ENABLE(NETSCAPE_PLUGIN_API)
80 81 82
{
}

83 84 85 86
CookiesStrategy* WebPlatformStrategies::createCookiesStrategy()
{
    return this;
}
87

88 89 90 91 92
DatabaseStrategy* WebPlatformStrategies::createDatabaseStrategy()
{
    return this;
}

93 94 95 96 97
LoaderStrategy* WebPlatformStrategies::createLoaderStrategy()
{
    return this;
}

98 99 100 101 102
PasteboardStrategy* WebPlatformStrategies::createPasteboardStrategy()
{
    return this;
}

103 104 105 106 107
PluginStrategy* WebPlatformStrategies::createPluginStrategy()
{
    return this;
}

108
SharedWorkerStrategy* WebPlatformStrategies::createSharedWorkerStrategy()
109 110 111 112
{
    return this;
}

113 114 115 116 117
StorageStrategy* WebPlatformStrategies::createStorageStrategy()
{
    return this;
}

118
VisitedLinkStrategy* WebPlatformStrategies::createVisitedLinkStrategy()
119 120 121 122
{
    return this;
}

123 124
// CookiesStrategy

125
String WebPlatformStrategies::cookiesForDOM(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
126 127 128 129
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
        String result;
130
        if (!WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::CookiesForDOM(session.isPrivateBrowsingSession(), firstParty, url), Messages::NetworkConnectionToWebProcess::CookiesForDOM::Reply(result), 0))
131 132 133 134 135
            return String();
        return result;
    }
#endif

136
    return WebCore::cookiesForDOM(session, firstParty, url);
137 138
}

139
void WebPlatformStrategies::setCookiesFromDOM(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url, const String& cookieString)
140 141 142
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
143
        WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::SetCookiesFromDOM(session.isPrivateBrowsingSession(), firstParty, url, cookieString), 0);
144 145 146 147
        return;
    }
#endif

148
    WebCore::setCookiesFromDOM(session, firstParty, url, cookieString);
149 150
}

151
bool WebPlatformStrategies::cookiesEnabled(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
152 153 154 155
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
        bool result;
156
        if (!WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::CookiesEnabled(session.isPrivateBrowsingSession(), firstParty, url), Messages::NetworkConnectionToWebProcess::CookiesEnabled::Reply(result), 0))
157 158 159 160 161
            return false;
        return result;
    }
#endif

162
    return WebCore::cookiesEnabled(session, firstParty, url);
163 164
}

165
String WebPlatformStrategies::cookieRequestHeaderFieldValue(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url)
166 167 168 169
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
        String result;
170
        if (!WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::CookieRequestHeaderFieldValue(session.isPrivateBrowsingSession(), firstParty, url), Messages::NetworkConnectionToWebProcess::CookieRequestHeaderFieldValue::Reply(result), 0))
171 172 173 174 175
            return String();
        return result;
    }
#endif

176
    return WebCore::cookieRequestHeaderFieldValue(session, firstParty, url);
177 178
}

179
bool WebPlatformStrategies::getRawCookies(const NetworkStorageSession& session, const KURL& firstParty, const KURL& url, Vector<Cookie>& rawCookies)
180 181 182
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
183
        if (!WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::GetRawCookies(session.isPrivateBrowsingSession(), firstParty, url), Messages::NetworkConnectionToWebProcess::GetRawCookies::Reply(rawCookies), 0))
184 185 186 187 188
            return false;
        return true;
    }
#endif

189
    return WebCore::getRawCookies(session, firstParty, url, rawCookies);
190 191
}

192
void WebPlatformStrategies::deleteCookie(const NetworkStorageSession& session, const KURL& url, const String& cookieName)
193 194 195
{
#if ENABLE(NETWORK_PROCESS)
    if (WebProcess::shared().usesNetworkProcess()) {
196
        WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::DeleteCookie(session.isPrivateBrowsingSession(), url, cookieName), 0);
197 198 199 200
        return;
    }
#endif

201
    WebCore::deleteCookie(session, url, cookieName);
202 203
}

204 205
// DatabaseStrategy

206
#if ENABLE(SQL_DATABASE)
207 208 209 210
AbstractDatabaseServer* WebPlatformStrategies::getDatabaseServer()
{
    return DatabaseStrategy::getDatabaseServer(); // Use the default for now.
}
211
#endif
212

213 214 215 216 217
// LoaderStrategy

#if ENABLE(NETWORK_PROCESS)
ResourceLoadScheduler* WebPlatformStrategies::resourceLoadScheduler()
{
218 219 220 221 222 223 224 225 226
    static ResourceLoadScheduler* scheduler;
    if (!scheduler) {
        if (WebProcess::shared().usesNetworkProcess())
            scheduler = &WebProcess::shared().webResourceLoadScheduler();
        else
            scheduler = WebCore::resourceLoadScheduler();
    }
    
    return scheduler;
227
}
228

229
void WebPlatformStrategies::loadResourceSynchronously(NetworkingContext* context, unsigned long resourceLoadIdentifier, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
230
{
weinig@apple.com's avatar
weinig@apple.com committed
231
    if (!WebProcess::shared().usesNetworkProcess()) {
232
        LoaderStrategy::loadResourceSynchronously(context, resourceLoadIdentifier, request, storedCredentials, error, response, data);
weinig@apple.com's avatar
weinig@apple.com committed
233 234 235
        return;
    }

236 237
    CoreIPC::DataReference dataReference;

238
    NetworkResourceLoadParameters loadParameters(resourceLoadIdentifier, 0, 0, request, ResourceLoadPriorityHighest, SniffContent, storedCredentials, context->storageSession().isPrivateBrowsingSession(), context->shouldClearReferrerOnHTTPSToHTTPRedirect());
239 240 241 242 243 244 245 246 247 248 249 250
    if (!WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad(loadParameters), Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::Reply(error, response, dataReference), 0)) {
        response = ResourceResponse();
        error = internalError(request.url());
        data.resize(0);

        return;
    }

    data.resize(dataReference.size());
    memcpy(data.data(), dataReference.data(), dataReference.size());
}

251 252 253 254 255 256 257 258
#if ENABLE(BLOB)
BlobRegistry* WebPlatformStrategies::createBlobRegistry()
{
    if (!WebProcess::shared().usesNetworkProcess())
        return LoaderStrategy::createBlobRegistry();
    return new BlobRegistryProxy;    
}
#endif
259 260
#endif

261 262
// PluginStrategy

263 264
void WebPlatformStrategies::refreshPlugins()
{
265
#if ENABLE(NETSCAPE_PLUGIN_API)
266 267 268 269 270
    m_cachedPlugins.clear();
    m_pluginCacheIsPopulated = false;
    m_shouldRefreshPlugins = true;

    populatePluginCache();
271
#endif // ENABLE(NETSCAPE_PLUGIN_API)
272 273
}

274
void WebPlatformStrategies::getPluginInfo(const WebCore::Page*, Vector<WebCore::PluginInfo>& plugins)
275
{
276
#if ENABLE(NETSCAPE_PLUGIN_API)
277 278
    populatePluginCache();
    plugins = m_cachedPlugins;
279
#endif // ENABLE(NETSCAPE_PLUGIN_API)
280 281
}

282
#if ENABLE(NETSCAPE_PLUGIN_API)
283
void WebPlatformStrategies::populatePluginCache()
284
{
285 286
    if (m_pluginCacheIsPopulated)
        return;
287

288 289 290
    ASSERT(m_cachedPlugins.isEmpty());
    
    // FIXME: Should we do something in case of error here?
291 292
    if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::GetPlugins(m_shouldRefreshPlugins), Messages::WebProcessProxy::GetPlugins::Reply(m_cachedPlugins), 0))
        return;
293

294 295
    m_shouldRefreshPlugins = false;
    m_pluginCacheIsPopulated = true;
296
}
297
#endif // ENABLE(NETSCAPE_PLUGIN_API)
298

299 300
// StorageStrategy

301
PassRefPtr<StorageNamespace> WebPlatformStrategies::localStorageNamespace(PageGroup* pageGroup)
302
{
303
    return StorageStrategy::localStorageNamespace(pageGroup);
304 305
}

306
PassRefPtr<StorageNamespace> WebPlatformStrategies::sessionStorageNamespace(Page* page)
307
{
308
#if ENABLE(UI_PROCESS_STORAGE)
309
    return StorageNamespaceImpl::createSessionStorageNamespace(WebPage::fromCorePage(page));
310
#else
311
    return StorageStrategy::sessionStorageNamespace(page);
312
#endif
313 314
}

315
// VisitedLinkStrategy
316

317
bool WebPlatformStrategies::isLinkVisited(Page*, LinkHash linkHash, const KURL&, const AtomicString&)
318
{
319
    return WebProcess::shared().isLinkVisited(linkHash);
320 321
}

322
void WebPlatformStrategies::addVisitedLink(Page*, LinkHash linkHash)
323
{
324
    WebProcess::shared().addVisitedLink(linkHash);
325 326
}

327 328 329 330 331
#if PLATFORM(MAC)
// PasteboardStrategy

void WebPlatformStrategies::getTypes(Vector<String>& types, const String& pasteboardName)
{
332 333
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardTypes(pasteboardName),
                                                Messages::WebContext::GetPasteboardTypes::Reply(types), 0);
334 335 336 337
}

PassRefPtr<WebCore::SharedBuffer> WebPlatformStrategies::bufferForType(const String& pasteboardType, const String& pasteboardName)
{
338 339 340 341 342 343 344 345
    SharedMemory::Handle handle;
    uint64_t size = 0;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardBufferForType(pasteboardName, pasteboardType),
                                                Messages::WebContext::GetPasteboardBufferForType::Reply(handle, size), 0);
    if (handle.isNull())
        return 0;
    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
    return SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
346 347 348 349
}

void WebPlatformStrategies::getPathnamesForType(Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
{
350 351
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardPathnamesForType(pasteboardName, pasteboardType),
                                                Messages::WebContext::GetPasteboardPathnamesForType::Reply(pathnames), 0);
352 353 354 355
}

String WebPlatformStrategies::stringForType(const String& pasteboardType, const String& pasteboardName)
{
356 357 358 359
    String value;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardStringForType(pasteboardName, pasteboardType),
                                                Messages::WebContext::GetPasteboardStringForType::Reply(value), 0);
    return value;
360 361 362 363
}

void WebPlatformStrategies::copy(const String& fromPasteboard, const String& toPasteboard)
{
364
    WebProcess::shared().connection()->send(Messages::WebContext::PasteboardCopy(fromPasteboard, toPasteboard), 0);
365 366
}

367 368
int WebPlatformStrategies::changeCount(const WTF::String &pasteboardName)
{
369 370 371 372
    uint64_t changeCount;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardChangeCount(pasteboardName),
                                                Messages::WebContext::GetPasteboardChangeCount::Reply(changeCount), 0);
    return changeCount;
373 374 375 376
}

String WebPlatformStrategies::uniqueName()
{
377 378 379 380
    String pasteboardName;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardUniqueName(),
                                                Messages::WebContext::GetPasteboardUniqueName::Reply(pasteboardName), 0);
    return pasteboardName;
381 382
}

383 384
Color WebPlatformStrategies::color(const String& pasteboardName)
{
385 386 387 388
    Color color;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardColor(pasteboardName),
                                                Messages::WebContext::GetPasteboardColor::Reply(color), 0);
    return color;
389 390
}

391 392 393 394 395 396 397 398
KURL WebPlatformStrategies::url(const String& pasteboardName)
{
    String urlString;
    WebProcess::shared().connection()->sendSync(Messages::WebContext::GetPasteboardURL(pasteboardName),
                                                Messages::WebContext::GetPasteboardURL::Reply(urlString), 0);
    return KURL(ParsedURLString, urlString);
}

399 400 401 402 403
void WebPlatformStrategies::addTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
{
    WebProcess::shared().connection()->send(Messages::WebContext::AddPasteboardTypes(pasteboardName, pasteboardTypes), 0);
}

404 405
void WebPlatformStrategies::setTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
{
406
    WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardTypes(pasteboardName, pasteboardTypes), 0);
407 408 409 410
}

void WebPlatformStrategies::setBufferForType(PassRefPtr<SharedBuffer> buffer, const String& pasteboardType, const String& pasteboardName)
{
411 412 413 414 415 416 417
    SharedMemory::Handle handle;
    if (buffer) {
        RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(buffer->size());
        memcpy(sharedMemoryBuffer->data(), buffer->data(), buffer->size());
        sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
    }
    WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardBufferForType(pasteboardName, pasteboardType, handle, buffer ? buffer->size() : 0), 0);
418 419 420 421
}

void WebPlatformStrategies::setPathnamesForType(const Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
{
422
    WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardPathnamesForType(pasteboardName, pasteboardType, pathnames), 0);
423 424 425 426
}

void WebPlatformStrategies::setStringForType(const String& string, const String& pasteboardType, const String& pasteboardName)
{
427
    WebProcess::shared().connection()->send(Messages::WebContext::SetPasteboardStringForType(pasteboardName, pasteboardType, string), 0);
428 429 430
}
#endif

431
} // namespace WebKit
432 433

#endif // USE(PLATFORM_STRATEGIES)