b9c36667c947f295630398723ba9009fc750c4f6
[WebKit-https.git] / Source / WebKit2 / WebProcess / win / WebProcessWin.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebProcess.h"
28
29 #include "WebCookieManager.h"
30 #include "WebPage.h"
31 #include "WebProcessCreationParameters.h"
32 #include <WebCore/FileSystem.h>
33 #include <WebCore/MemoryCache.h>
34 #include <WebCore/PageCache.h>
35 #include <WebCore/ResourceHandle.h>
36 #include <WebCore/Settings.h>
37 #include <wtf/text/WTFString.h>
38
39 #if USE(CFNETWORK)
40 #include <CFNetwork/CFURLCachePriv.h>
41 #include <CFNetwork/CFURLProtocolPriv.h>
42 #include <WebCore/CookieStorageCFNet.h>
43 #include <WebKitSystemInterface/WebKitSystemInterface.h> 
44 #include <wtf/RetainPtr.h>
45 #endif
46
47 using namespace WebCore;
48 using namespace std;
49
50 namespace WebKit {
51
52 static uint64_t memorySize()
53 {
54     MEMORYSTATUSEX statex;
55     statex.dwLength = sizeof(statex);
56     GlobalMemoryStatusEx(&statex);
57     return statex.ullTotalPhys;
58 }
59
60 static uint64_t volumeFreeSize(CFStringRef cfstringPath)
61 {
62     WTF::String path(cfstringPath);
63     ULARGE_INTEGER freeBytesToCaller;
64     BOOL result = GetDiskFreeSpaceExW((LPCWSTR)path.charactersWithNullTermination(), &freeBytesToCaller, 0, 0);
65     if (!result)
66         return 0;
67     return freeBytesToCaller.QuadPart;
68 }
69
70 void WebProcess::platformSetCacheModel(CacheModel cacheModel)
71 {
72 #if USE(CFNETWORK)
73     RetainPtr<CFStringRef> cfurlCacheDirectory;
74 #if USE(CFURLSTORAGESESSIONS)
75     if (CFURLStorageSessionRef defaultStorageSession = ResourceHandle::defaultStorageSession())
76         cfurlCacheDirectory.adoptCF(wkCopyFoundationCacheDirectory(defaultStorageSession));
77     else
78 #endif
79         cfurlCacheDirectory.adoptCF(wkCopyFoundationCacheDirectory(0));
80
81     if (!cfurlCacheDirectory)
82         cfurlCacheDirectory.adoptCF(WebCore::localUserSpecificStorageDirectory().createCFString());
83
84     // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
85     // count doesn't align exactly to a megabyte boundary.
86     uint64_t memSize = memorySize() / 1024 / 1000;
87     uint64_t diskFreeSize = volumeFreeSize(cfurlCacheDirectory.get()) / 1024 / 1000;
88
89     unsigned cacheTotalCapacity = 0;
90     unsigned cacheMinDeadCapacity = 0;
91     unsigned cacheMaxDeadCapacity = 0;
92     double deadDecodedDataDeletionInterval = 0;
93     unsigned pageCacheCapacity = 0;
94     unsigned long urlCacheMemoryCapacity = 0;
95     unsigned long urlCacheDiskCapacity = 0;
96
97     calculateCacheSizes(cacheModel, memSize, diskFreeSize,
98         cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
99         pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);
100
101     memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
102     memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
103     pageCache()->setCapacity(pageCacheCapacity);
104
105     RetainPtr<CFURLCacheRef> cfurlCache;
106 #if USE(CFURLSTORAGESESSIONS)
107     if (CFURLStorageSessionRef defaultStorageSession = ResourceHandle::defaultStorageSession())
108         cfurlCache.adoptCF(wkCopyURLCache(defaultStorageSession));
109     else
110 #endif // USE(CFURLSTORAGESESSIONS)
111         cfurlCache.adoptCF(CFURLCacheCopySharedURLCache());
112
113     CFURLCacheSetMemoryCapacity(cfurlCache.get(), urlCacheMemoryCapacity);
114     CFURLCacheSetDiskCapacity(cfurlCache.get(), max<unsigned long>(urlCacheDiskCapacity, CFURLCacheDiskCapacity(cfurlCache.get()))); // Don't shrink a big disk cache, since that would cause churn.
115 #endif
116 }
117
118 void WebProcess::platformClearResourceCaches(ResourceCachesToClear cachesToClear)
119 {
120 #if USE(CFNETWORK)
121     if (cachesToClear == InMemoryResourceCachesOnly)
122         return;
123
124     RetainPtr<CFURLCacheRef> cache;
125 #if USE(CFURLSTORAGESESSIONS)
126     if (CFURLStorageSessionRef defaultStorageSession = ResourceHandle::defaultStorageSession())
127         cache.adoptCF(wkCopyURLCache(defaultStorageSession));
128     else
129 #endif // USE(CFURLSTORAGESESSIONS)
130         cache.adoptCF(CFURLCacheCopySharedURLCache());
131
132     CFURLCacheRemoveAllCachedResponses(cache.get());
133 #endif // USE(CFNETWORK)
134 }
135
136 void WebProcess::platformInitializeWebProcess(const WebProcessCreationParameters& parameters, CoreIPC::ArgumentDecoder*)
137 {
138     setShouldPaintNativeControls(parameters.shouldPaintNativeControls);
139
140 #if USE(CFNETWORK)
141 #if USE(CFURLSTORAGESESSIONS)
142     if (CFURLStorageSessionRef defaultStorageSession = wkDeserializeStorageSession(parameters.serializedDefaultStorageSession.get())) {
143         ResourceHandle::setDefaultStorageSession(defaultStorageSession);
144         return;
145     }
146 #endif // USE(CFURLSTORAGESESSIONS)
147
148     RetainPtr<CFStringRef> cachePath(AdoptCF, parameters.cfURLCachePath.createCFString());
149     if (!cachePath)
150         return;
151
152     CFIndex cacheDiskCapacity = parameters.cfURLCacheDiskCapacity;
153     CFIndex cacheMemoryCapacity = parameters.cfURLCacheMemoryCapacity;
154     RetainPtr<CFURLCacheRef> uiProcessCache(AdoptCF, CFURLCacheCreate(kCFAllocatorDefault, cacheMemoryCapacity, cacheDiskCapacity, cachePath.get()));
155     CFURLCacheSetSharedURLCache(uiProcessCache.get());
156 #endif // USE(CFNETWORK)
157
158     WebCookieManager::shared().setHTTPCookieAcceptPolicy(parameters.initialHTTPCookieAcceptPolicy);
159 }
160
161 void WebProcess::platformTerminate()
162 {
163 }
164
165 void WebProcess::setShouldPaintNativeControls(bool shouldPaintNativeControls)
166 {
167 #if USE(SAFARI_THEME)
168     Settings::setShouldPaintNativeControls(shouldPaintNativeControls);
169 #endif
170 }
171
172 struct EnumWindowsContext {
173     DWORD currentThreadID;
174     Vector<HWND>* windows;
175 };
176
177 static BOOL CALLBACK addWindowToVectorIfOwnedByCurrentThread(HWND window, LPARAM lParam)
178 {
179     EnumWindowsContext* context = reinterpret_cast<EnumWindowsContext*>(lParam);
180
181     if (::GetWindowThreadProcessId(window, 0) != context->currentThreadID)
182         return TRUE;
183
184     context->windows->append(window);
185     return TRUE;
186 }
187
188 Vector<HWND> WebProcess::windowsToReceiveSentMessagesWhileWaitingForSyncReply()
189 {
190     Vector<HWND> windows;
191
192     // Any non-message-only window created by this thread needs to receive sent messages while we
193     // wait for a sync reply. Otherwise we could deadlock with the UI process if, e.g., the focus
194     // window changes. See <http://webkit.org/b/58239>.
195
196     EnumWindowsContext context;
197     context.currentThreadID = ::GetCurrentThreadId();
198     context.windows = &windows;
199
200     // Start out with top-level windows created by this thread (like Flash's hidden
201     // SWFlash_PlaceholderX top-level windows).
202     ::EnumThreadWindows(context.currentThreadID, addWindowToVectorIfOwnedByCurrentThread, reinterpret_cast<LPARAM>(&context));
203
204     // Also include any descendants of those top-level windows.
205     size_t topLevelWindowCount = windows.size();
206     for (size_t i = 0; i < topLevelWindowCount; ++i)
207         ::EnumChildWindows(windows[i], addWindowToVectorIfOwnedByCurrentThread, reinterpret_cast<LPARAM>(&context));
208
209     // Also include any descendants of the WebPages' windows which we've created (e.g., for windowed plugins).
210     HashMap<uint64_t, RefPtr<WebPage> >::const_iterator::Values end = m_pageMap.end();
211     for (HashMap<uint64_t, RefPtr<WebPage> >::const_iterator::Values it = m_pageMap.begin(); it != end; ++it)
212         ::EnumChildWindows((*it)->nativeWindow(), addWindowToVectorIfOwnedByCurrentThread, reinterpret_cast<LPARAM>(&context));
213
214     return windows;
215 }
216
217 } // namespace WebKit