Use WTF::Function instead of std::function in NetworkProcess code
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / soup / NetworkProcessSoup.cpp
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2013 Comapny 100 Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "NetworkProcess.h"
29
30 #include "NetworkCache.h"
31 #include "NetworkProcessCreationParameters.h"
32 #include "ResourceCachesToClear.h"
33 #include "WebCookieManager.h"
34 #include <WebCore/CertificateInfo.h>
35 #include <WebCore/FileSystem.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/RAMSize.h>
42 #include <wtf/glib/GRefPtr.h>
43 #include <wtf/glib/GUniquePtr.h>
44 #include <wtf/text/CString.h>
45 #include <wtf/text/StringBuilder.h>
46
47 using namespace WebCore;
48
49 namespace WebKit {
50
51 static CString buildAcceptLanguages(const Vector<String>& languages)
52 {
53     size_t languagesCount = languages.size();
54
55     // Ignore "C" locale.
56     size_t cLocalePosition = languages.find("c");
57     if (cLocalePosition != notFound)
58         languagesCount--;
59
60     // Fallback to "en" if the list is empty.
61     if (!languagesCount)
62         return "en";
63
64     // Calculate deltas for the quality values.
65     int delta;
66     if (languagesCount < 10)
67         delta = 10;
68     else if (languagesCount < 20)
69         delta = 5;
70     else
71         delta = 1;
72
73     // Set quality values for each language.
74     StringBuilder builder;
75     for (size_t i = 0; i < languages.size(); ++i) {
76         if (i == cLocalePosition)
77             continue;
78
79         if (i)
80             builder.appendLiteral(", ");
81
82         builder.append(languages[i]);
83
84         int quality = 100 - i * delta;
85         if (quality > 0 && quality < 100) {
86             char buffer[8];
87             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
88             builder.append(String::format(";q=%s", buffer));
89         }
90     }
91
92     return builder.toString().utf8();
93 }
94
95 void NetworkProcess::userPreferredLanguagesChanged(const Vector<String>& languages)
96 {
97     auto acceptLanguages = buildAcceptLanguages(languages);
98     SoupNetworkSession::setInitialAcceptLanguages(acceptLanguages);
99     NetworkStorageSession::forEach([&acceptLanguages](const WebCore::NetworkStorageSession& session) {
100         if (auto* soupSession = session.soupNetworkSession())
101             soupSession->setAcceptLanguages(acceptLanguages);
102     });
103 }
104
105 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
106 {
107     if (parameters.proxySettings.mode != SoupNetworkProxySettings::Mode::Default)
108         setNetworkProxySettings(parameters.proxySettings);
109
110     ASSERT(!parameters.diskCacheDirectory.isEmpty());
111     m_diskCacheDirectory = parameters.diskCacheDirectory;
112
113     SoupNetworkSession::clearOldSoupCache(WebCore::directoryName(m_diskCacheDirectory));
114
115     OptionSet<NetworkCache::Cache::Option> cacheOptions;
116     if (parameters.shouldEnableNetworkCacheEfficacyLogging)
117         cacheOptions |= NetworkCache::Cache::Option::EfficacyLogging;
118 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
119     if (parameters.shouldEnableNetworkCacheSpeculativeRevalidation)
120         cacheOptions |= NetworkCache::Cache::Option::SpeculativeRevalidation;
121 #endif
122
123     NetworkCache::singleton().initialize(m_diskCacheDirectory, cacheOptions);
124
125     if (!parameters.cookiePersistentStoragePath.isEmpty()) {
126         supplement<WebCookieManager>()->setCookiePersistentStorage(parameters.cookiePersistentStoragePath,
127             parameters.cookiePersistentStorageType);
128     }
129     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy, 0);
130
131     if (!parameters.languages.isEmpty())
132         userPreferredLanguagesChanged(parameters.languages);
133
134     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
135 }
136
137 void NetworkProcess::platformSetURLCacheSize(unsigned, uint64_t)
138 {
139 }
140
141 void NetworkProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
142 {
143     SoupNetworkSession::setShouldIgnoreTLSErrors(ignoreTLSErrors);
144 }
145
146 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
147 {
148     SoupNetworkSession::allowSpecificHTTPSCertificateForHost(certificateInfo, host);
149 }
150
151 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
152 {
153     if (cachesToClear == InMemoryResourceCachesOnly)
154         return;
155
156     clearDiskCache(std::chrono::system_clock::time_point::min(), [] { });
157 }
158
159 void NetworkProcess::clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler)
160 {
161     NetworkCache::singleton().clear(modifiedSince, WTFMove(completionHandler));
162 }
163
164 void NetworkProcess::platformTerminate()
165 {
166     notImplemented();
167 }
168
169 void NetworkProcess::setNetworkProxySettings(const SoupNetworkProxySettings& settings)
170 {
171     SoupNetworkSession::setProxySettings(settings);
172     NetworkStorageSession::forEach([](const NetworkStorageSession& session) {
173         if (auto* soupSession = session.soupNetworkSession())
174             soupSession->setupProxy();
175     });
176 }
177
178 } // namespace WebKit