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 #ifndef WebProcess_h
27 #define WebProcess_h
28 
29 #include "CacheModel.h"
30 #include "ChildProcess.h"
31 #include "DrawingArea.h"
32 #include "ResourceCachesToClear.h"
33 #include "SandboxExtension.h"
34 #include "SharedMemory.h"
35 #include "TextCheckerState.h"
36 #include "VisitedLinkTable.h"
37 #include "WebGeolocationManager.h"
38 #include "WebIconDatabaseProxy.h"
39 #include "WebPageGroupProxy.h"
40 #include <WebCore/LinkHash.h>
41 #include <wtf/Forward.h>
42 #include <wtf/HashMap.h>
43 #include <wtf/HashSet.h>
44 
45 #if PLATFORM(QT)
46 class QNetworkAccessManager;
47 #endif
48 
49 #if ENABLE(PLUGIN_PROCESS)
50 #include "PluginProcessConnectionManager.h"
51 #endif
52 
53 namespace WebCore {
54     class IntSize;
55     class PageGroup;
56     class ResourceRequest;
57 }
58 
59 namespace WebKit {
60 
61 class InjectedBundle;
62 class WebFrame;
63 class WebPage;
64 struct WebPageCreationParameters;
65 struct WebPageGroupData;
66 struct WebPreferencesStore;
67 struct WebProcessCreationParameters;
68 
69 class WebProcess : public ChildProcess, private CoreIPC::Connection::QueueClient {
70 public:
71     static WebProcess& shared();
72 
73     void initialize(CoreIPC::Connection::Identifier, RunLoop*);
74 
connection()75     CoreIPC::Connection* connection() const { return m_connection.get(); }
runLoop()76     RunLoop* runLoop() const { return m_runLoop; }
77 
78     WebPage* webPage(uint64_t pageID) const;
79     void createWebPage(uint64_t pageID, const WebPageCreationParameters&);
80     void removeWebPage(uint64_t pageID);
81     WebPage* focusedWebPage() const;
82 
injectedBundle()83     InjectedBundle* injectedBundle() const { return m_injectedBundle.get(); }
84 
85     bool isSeparateProcess() const;
86 
87 #if USE(ACCELERATED_COMPOSITING) && PLATFORM(MAC)
compositingRenderServerPort()88     mach_port_t compositingRenderServerPort() const { return m_compositingRenderServerPort; }
89 #endif
90 
91     void addVisitedLink(WebCore::LinkHash);
92     bool isLinkVisited(WebCore::LinkHash) const;
93 
94     bool fullKeyboardAccessEnabled();
95 
96     WebFrame* webFrame(uint64_t) const;
97     void addWebFrame(uint64_t, WebFrame*);
98     void removeWebFrame(uint64_t);
99 
100     WebPageGroupProxy* webPageGroup(uint64_t pageGroupID);
101     WebPageGroupProxy* webPageGroup(const WebPageGroupData&);
102 #if PLATFORM(MAC)
presenterApplicationPid()103     pid_t presenterApplicationPid() const { return m_presenterApplicationPid; }
104 #endif
105 
106 #if PLATFORM(QT)
networkAccessManager()107     QNetworkAccessManager* networkAccessManager() { return m_networkAccessManager; }
108 #endif
109 
shouldUseCustomRepresentationForMIMEType(const String & mimeType)110     bool shouldUseCustomRepresentationForMIMEType(const String& mimeType) const { return m_mimeTypesWithCustomRepresentations.contains(mimeType); }
111 
112     // Text Checking
textCheckerState()113     const TextCheckerState& textCheckerState() const { return m_textCheckerState; }
114 
115     // Geolocation
geolocationManager()116     WebGeolocationManager& geolocationManager() { return m_geolocationManager; }
117 
118     void clearResourceCaches(ResourceCachesToClear = AllResourceCaches);
119 
localStorageDirectory()120     const String& localStorageDirectory() const { return m_localStorageDirectory; }
121 
122 #if ENABLE(PLUGIN_PROCESS)
pluginProcessConnectionManager()123     PluginProcessConnectionManager& pluginProcessConnectionManager() { return m_pluginProcessConnectionManager; }
124 #endif
125 
126 private:
127     WebProcess();
128 
129     void initializeWebProcess(const WebProcessCreationParameters&, CoreIPC::ArgumentDecoder*);
130     void platformInitializeWebProcess(const WebProcessCreationParameters&, CoreIPC::ArgumentDecoder*);
131     void platformTerminate();
132     void setShouldTrackVisitedLinks(bool);
133     void registerURLSchemeAsEmptyDocument(const String&);
134     void registerURLSchemeAsSecure(const String&) const;
135     void setDomainRelaxationForbiddenForURLScheme(const String&) const;
136     void setDefaultRequestTimeoutInterval(double);
137     void setAlwaysUsesComplexTextCodePath(bool);
138     void languageChanged(const String&) const;
139 #if PLATFORM(WIN)
140     void setShouldPaintNativeControls(bool);
141 #endif
142 
143     void setVisitedLinkTable(const SharedMemory::Handle&);
144     void visitedLinkStateChanged(const Vector<WebCore::LinkHash>& linkHashes);
145     void allVisitedLinkStateChanged();
146 
147     void setCacheModel(uint32_t);
148     void platformSetCacheModel(CacheModel);
149     static void calculateCacheSizes(CacheModel cacheModel, uint64_t memorySize, uint64_t diskFreeSize,
150         unsigned& cacheTotalCapacity, unsigned& cacheMinDeadCapacity, unsigned& cacheMaxDeadCapacity, double& deadDecodedDataDeletionInterval,
151         unsigned& pageCacheCapacity, unsigned long& urlCacheMemoryCapacity, unsigned long& urlCacheDiskCapacity);
152     void platformClearResourceCaches(ResourceCachesToClear);
153     void clearApplicationCache();
154 
155     void setEnhancedAccessibility(bool);
156 
157 #if !ENABLE(PLUGIN_PROCESS)
158     void getSitesWithPluginData(const Vector<String>& pluginPaths, uint64_t callbackID);
159     void clearPluginSiteData(const Vector<String>& pluginPaths, const Vector<String>& sites, uint64_t flags, uint64_t maxAgeInSeconds, uint64_t callbackID);
160 #endif
161 
162 #if ENABLE(PLUGIN_PROCESS)
163     void pluginProcessCrashed(const String& pluginPath);
164 #endif
165 
166     void startMemorySampler(const SandboxExtension::Handle&, const String&, const double);
167     void stopMemorySampler();
168 
169     void downloadRequest(uint64_t downloadID, uint64_t initiatingPageID, const WebCore::ResourceRequest&);
170     void cancelDownload(uint64_t downloadID);
171 
172     void setTextCheckerState(const TextCheckerState&);
173 
174     // ChildProcess
175     virtual bool shouldTerminate();
176     virtual void terminate();
177 
178     // CoreIPC::Connection::Client
179     virtual void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
180     virtual CoreIPC::SyncReplyMode didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
181     virtual void didClose(CoreIPC::Connection*);
182     virtual void didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID);
183     virtual void syncMessageSendTimedOut(CoreIPC::Connection*);
184 
185     // CoreIPC::Connection::QueueClient
186     virtual bool willProcessMessageOnClientRunLoop(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
187 
188 #if PLATFORM(WIN)
189     Vector<HWND> windowsToReceiveSentMessagesWhileWaitingForSyncReply();
190 #endif
191 
192     // Implemented in generated WebProcessMessageReceiver.cpp
193     bool willProcessWebProcessMessageOnClientRunLoop(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
194     void didReceiveWebProcessMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
195 
196     RefPtr<CoreIPC::Connection> m_connection;
197     HashMap<uint64_t, RefPtr<WebPage> > m_pageMap;
198     HashMap<uint64_t, RefPtr<WebPageGroupProxy> > m_pageGroupMap;
199     RefPtr<InjectedBundle> m_injectedBundle;
200 
201     bool m_inDidClose;
202 
203     RunLoop* m_runLoop;
204 
205     // FIXME: The visited link table should not be per process.
206     VisitedLinkTable m_visitedLinkTable;
207 
208     bool m_hasSetCacheModel;
209     CacheModel m_cacheModel;
210 
211 #if USE(ACCELERATED_COMPOSITING) && PLATFORM(MAC)
212     mach_port_t m_compositingRenderServerPort;
213 #endif
214 #if PLATFORM(MAC)
215     pid_t m_presenterApplicationPid;
216 #endif
217 
218 #if PLATFORM(QT)
219     QNetworkAccessManager* m_networkAccessManager;
220 #endif
221 
222     HashMap<uint64_t, WebFrame*> m_frameMap;
223 
224     HashSet<String, CaseFoldingHash> m_mimeTypesWithCustomRepresentations;
225 
226     TextCheckerState m_textCheckerState;
227     WebGeolocationManager m_geolocationManager;
228     WebIconDatabaseProxy m_iconDatabaseProxy;
229 
230     String m_localStorageDirectory;
231 
232 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
233     RefPtr<SandboxExtension> m_applicationCachePathExtension;
234 #endif
235 
236 #if ENABLE(PLUGIN_PROCESS)
237     PluginProcessConnectionManager m_pluginProcessConnectionManager;
238 #endif
239 };
240 
241 } // namespace WebKit
242 
243 #endif // WebProcess_h
244