ebabfb072b49dd5b84de5ebddfbe273e40c121a4
[WebKit-https.git] / Source / WebKit / 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/NetworkStorageSession.h>
36 #include <WebCore/NotImplemented.h>
37 #include <WebCore/ResourceHandle.h>
38 #include <WebCore/SoupNetworkSession.h>
39 #include <libsoup/soup.h>
40 #include <wtf/FileSystem.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 namespace WebKit {
48 using namespace WebCore;
49
50 static CString buildAcceptLanguages(const Vector<String>& languages)
51 {
52     size_t languagesCount = languages.size();
53
54     // Ignore "C" locale.
55     size_t cLocalePosition = languages.find("c");
56     if (cLocalePosition != notFound)
57         languagesCount--;
58
59     // Fallback to "en" if the list is empty.
60     if (!languagesCount)
61         return "en";
62
63     // Calculate deltas for the quality values.
64     int delta;
65     if (languagesCount < 10)
66         delta = 10;
67     else if (languagesCount < 20)
68         delta = 5;
69     else
70         delta = 1;
71
72     // Set quality values for each language.
73     StringBuilder builder;
74     for (size_t i = 0; i < languages.size(); ++i) {
75         if (i == cLocalePosition)
76             continue;
77
78         if (i)
79             builder.appendLiteral(",");
80
81         builder.append(languages[i]);
82
83         int quality = 100 - i * delta;
84         if (quality > 0 && quality < 100) {
85             builder.appendLiteral(";q=");
86             char buffer[8];
87             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
88             builder.append(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     forEachNetworkStorageSession([&acceptLanguages](const auto& session) {
100         session.soupNetworkSession().setAcceptLanguages(acceptLanguages);
101     });
102 }
103
104 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
105 {
106     if (parameters.proxySettings.mode != SoupNetworkProxySettings::Mode::Default)
107         setNetworkProxySettings(parameters.proxySettings);
108
109     ASSERT(!parameters.diskCacheDirectory.isEmpty());
110     m_diskCacheDirectory = parameters.diskCacheDirectory;
111
112     SoupNetworkSession::clearOldSoupCache(FileSystem::directoryName(m_diskCacheDirectory));
113
114     OptionSet<NetworkCache::Cache::Option> cacheOptions { NetworkCache::Cache::Option::RegisterNotify };
115     if (parameters.shouldEnableNetworkCacheEfficacyLogging)
116         cacheOptions.add(NetworkCache::Cache::Option::EfficacyLogging);
117 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
118     if (parameters.shouldEnableNetworkCacheSpeculativeRevalidation)
119         cacheOptions.add(NetworkCache::Cache::Option::SpeculativeRevalidation);
120 #endif
121
122     m_cache = NetworkCache::Cache::open(*this, m_diskCacheDirectory, cacheOptions);
123
124     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy, OptionalCallbackID());
125
126     if (!parameters.languages.isEmpty())
127         userPreferredLanguagesChanged(parameters.languages);
128
129     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
130 }
131
132 std::unique_ptr<WebCore::NetworkStorageSession> NetworkProcess::platformCreateDefaultStorageSession() const
133 {
134     return std::make_unique<WebCore::NetworkStorageSession>(PAL::SessionID::defaultSessionID(), std::make_unique<SoupNetworkSession>(PAL::SessionID::defaultSessionID()));
135 }
136
137 void NetworkProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
138 {
139     SoupNetworkSession::setShouldIgnoreTLSErrors(ignoreTLSErrors);
140 }
141
142 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
143 {
144     SoupNetworkSession::allowSpecificHTTPSCertificateForHost(certificateInfo, host);
145 }
146
147 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
148 {
149     if (cachesToClear == InMemoryResourceCachesOnly)
150         return;
151
152     clearDiskCache(-WallTime::infinity(), [] { });
153 }
154
155 void NetworkProcess::clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&& completionHandler)
156 {
157     if (!m_cache) {
158         completionHandler();
159         return;
160     }
161     m_cache->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     forEachNetworkStorageSession([](const auto& session) {
173         session.soupNetworkSession().setupProxy();
174     });
175 }
176
177 void NetworkProcess::platformPrepareToSuspend(CompletionHandler<void()>&& completionHandler)
178 {
179     notImplemented();
180     completionHandler();
181 }
182
183 void NetworkProcess::platformProcessDidResume()
184 {
185     notImplemented();
186 }
187
188 void NetworkProcess::platformProcessDidTransitionToForeground()
189 {
190     notImplemented();
191 }
192
193 void NetworkProcess::platformProcessDidTransitionToBackground()
194 {
195     notImplemented();
196 }
197
198 } // namespace WebKit