dca7c41f43040a622d711ae8ad62530a1cd78dc9
[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 "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>
47
48 namespace WebKit {
49 using namespace WebCore;
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             builder.appendLiteral(";q=");
87             char buffer[8];
88             g_ascii_formatd(buffer, 8, "%.2f", quality / 100.0);
89             builder.append(buffer);
90         }
91     }
92
93     return builder.toString().utf8();
94 }
95
96 void NetworkProcess::userPreferredLanguagesChanged(const Vector<String>& languages)
97 {
98     auto acceptLanguages = buildAcceptLanguages(languages);
99     SoupNetworkSession::setInitialAcceptLanguages(acceptLanguages);
100     forEachNetworkStorageSession([&acceptLanguages](const auto& session) {
101         session.soupNetworkSession().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     GRefPtr<GResolver> cachedResolver = adoptGRef(webkitCachedResolverNew(adoptGRef(g_resolver_get_default())));
114     g_resolver_set_default(cachedResolver.get());
115
116     SoupNetworkSession::clearOldSoupCache(FileSystem::directoryName(m_diskCacheDirectory));
117
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);
122 #endif
123
124     m_cache = NetworkCache::Cache::open(*this, m_diskCacheDirectory, cacheOptions);
125
126     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy, OptionalCallbackID());
127
128     if (!parameters.languages.isEmpty())
129         userPreferredLanguagesChanged(parameters.languages);
130
131     setIgnoreTLSErrors(parameters.ignoreTLSErrors);
132 }
133
134 std::unique_ptr<WebCore::NetworkStorageSession> NetworkProcess::platformCreateDefaultStorageSession() const
135 {
136     return std::make_unique<WebCore::NetworkStorageSession>(PAL::SessionID::defaultSessionID(), std::make_unique<SoupNetworkSession>(PAL::SessionID::defaultSessionID()));
137 }
138
139 void NetworkProcess::setIgnoreTLSErrors(bool ignoreTLSErrors)
140 {
141     SoupNetworkSession::setShouldIgnoreTLSErrors(ignoreTLSErrors);
142 }
143
144 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
145 {
146     SoupNetworkSession::allowSpecificHTTPSCertificateForHost(certificateInfo, host);
147 }
148
149 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
150 {
151     if (cachesToClear == InMemoryResourceCachesOnly)
152         return;
153
154     clearDiskCache(-WallTime::infinity(), [] { });
155 }
156
157 void NetworkProcess::clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&& completionHandler)
158 {
159     if (!m_cache) {
160         completionHandler();
161         return;
162     }
163     m_cache->clear(modifiedSince, WTFMove(completionHandler));
164 }
165
166 void NetworkProcess::platformTerminate()
167 {
168     notImplemented();
169 }
170
171 void NetworkProcess::setNetworkProxySettings(const SoupNetworkProxySettings& settings)
172 {
173     SoupNetworkSession::setProxySettings(settings);
174     forEachNetworkStorageSession([](const auto& session) {
175         session.soupNetworkSession().setupProxy();
176     });
177 }
178
179 void NetworkProcess::platformPrepareToSuspend(CompletionHandler<void()>&& completionHandler)
180 {
181     notImplemented();
182     completionHandler();
183 }
184
185 void NetworkProcess::platformProcessDidResume()
186 {
187     notImplemented();
188 }
189
190 void NetworkProcess::platformProcessDidTransitionToForeground()
191 {
192     notImplemented();
193 }
194
195 void NetworkProcess::platformProcessDidTransitionToBackground()
196 {
197     notImplemented();
198 }
199
200 } // namespace WebKit