2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Comapny 100 Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
28 #include "NetworkProcess.h"
30 #include "NetworkCache.h"
31 #include "NetworkProcessCreationParameters.h"
32 #include "ResourceCachesToClear.h"
33 #include "WebCookieManager.h"
34 #include "WebKitCachedResolver.h"
35 #include <WebCore/CertificateInfo.h>
36 #include <WebCore/NetworkStorageSession.h>
37 #include <WebCore/NotImplemented.h>
38 #include <WebCore/ResourceHandle.h>
39 #include <WebCore/SoupNetworkSession.h>
40 #include <libsoup/soup.h>
41 #include <wtf/FileSystem.h>
42 #include <wtf/RAMSize.h>
43 #include <wtf/glib/GRefPtr.h>
44 #include <wtf/glib/GUniquePtr.h>
45 #include <wtf/text/CString.h>
46 #include <wtf/text/StringBuilder.h>
49 using namespace WebCore;
51 static CString buildAcceptLanguages(const Vector<String>& languages)
53 size_t languagesCount = languages.size();
56 size_t cLocalePosition = languages.find("c");
57 if (cLocalePosition != notFound)
60 // Fallback to "en" if the list is empty.
64 // Calculate deltas for the quality values.
66 if (languagesCount < 10)
68 else if (languagesCount < 20)
73 // Set quality values for each language.
74 StringBuilder builder;
75 for (size_t i = 0; i < languages.size(); ++i) {
76 if (i == cLocalePosition)
80 builder.appendLiteral(",");
82 builder.append(languages[i]);
84 int quality = 100 - i * delta;
85 if (quality > 0 && quality < 100) {
86 builder.appendLiteral(";q=");
88 g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
89 builder.append(buffer);
93 return builder.toString().utf8();
96 void NetworkProcess::userPreferredLanguagesChanged(const Vector<String>& languages)
98 auto acceptLanguages = buildAcceptLanguages(languages);
99 SoupNetworkSession::setInitialAcceptLanguages(acceptLanguages);
100 forEachNetworkStorageSession([&acceptLanguages](const auto& session) {
101 session.soupNetworkSession().setAcceptLanguages(acceptLanguages);
105 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
107 if (parameters.proxySettings.mode != SoupNetworkProxySettings::Mode::Default)
108 setNetworkProxySettings(parameters.proxySettings);
110 ASSERT(!parameters.diskCacheDirectory.isEmpty());
111 m_diskCacheDirectory = parameters.diskCacheDirectory;
113 GRefPtr<GResolver> cachedResolver = adoptGRef(webkitCachedResolverNew(adoptGRef(g_resolver_get_default())));
114 g_resolver_set_default(cachedResolver.get());
116 SoupNetworkSession::clearOldSoupCache(FileSystem::directoryName(m_diskCacheDirectory));
118 OptionSet<NetworkCache::Cache::Option> cacheOptions { NetworkCache::Cache::Option::RegisterNotify };
119 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
120 if (parameters.shouldEnableNetworkCacheSpeculativeRevalidation)
121 cacheOptions.add(NetworkCache::Cache::Option::SpeculativeRevalidation);
124 m_cache = NetworkCache::Cache::open(*this, m_diskCacheDirectory, cacheOptions);
126 supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy, OptionalCallbackID());
128 if (!parameters.languages.isEmpty())
129 userPreferredLanguagesChanged(parameters.languages);
131 setIgnoreTLSErrors(parameters.ignoreTLSErrors);
134 std::unique_ptr<WebCore::NetworkStorageSession> NetworkProcess::platformCreateDefaultStorageSession() const
136 return std::make_unique<WebCore::NetworkStorageSession>(PAL::SessionID::defaultSessionID(), std::make_unique<SoupNetworkSession>(PAL::SessionID::defaultSessionID()));
139 void NetworkProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
141 SoupNetworkSession::setShouldIgnoreTLSErrors(ignoreTLSErrors);
144 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
146 SoupNetworkSession::allowSpecificHTTPSCertificateForHost(certificateInfo, host);
149 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
151 if (cachesToClear == InMemoryResourceCachesOnly)
154 clearDiskCache(-WallTime::infinity(), [] { });
157 void NetworkProcess::clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&& completionHandler)
163 m_cache->clear(modifiedSince, WTFMove(completionHandler));
166 void NetworkProcess::platformTerminate()
171 void NetworkProcess::setNetworkProxySettings(const SoupNetworkProxySettings& settings)
173 SoupNetworkSession::setProxySettings(settings);
174 forEachNetworkStorageSession([](const auto& session) {
175 session.soupNetworkSession().setupProxy();
179 void NetworkProcess::platformPrepareToSuspend(CompletionHandler<void()>&& completionHandler)
185 void NetworkProcess::platformProcessDidResume()
190 void NetworkProcess::platformProcessDidTransitionToForeground()
195 void NetworkProcess::platformProcessDidTransitionToBackground()
200 } // namespace WebKit