1 /*
2  * Copyright (C) 2010, 2011 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 WebContext_h
27 #define WebContext_h
28 
29 #include "APIObject.h"
30 #include "PluginInfoStore.h"
31 #include "ProcessModel.h"
32 #include "VisitedLinkProvider.h"
33 #include "WebContextInjectedBundleClient.h"
34 #include "WebDownloadClient.h"
35 #include "WebHistoryClient.h"
36 #include "WebProcessProxy.h"
37 #include <WebCore/LinkHash.h>
38 #include <wtf/Forward.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/PassRefPtr.h>
41 #include <wtf/RefPtr.h>
42 #include <wtf/text/StringHash.h>
43 #include <wtf/text/WTFString.h>
44 
45 namespace WebKit {
46 
47 class DownloadProxy;
48 class WebApplicationCacheManagerProxy;
49 class WebCookieManagerProxy;
50 class WebDatabaseManagerProxy;
51 class WebGeolocationManagerProxy;
52 class WebIconDatabase;
53 class WebKeyValueStorageManagerProxy;
54 class WebMediaCacheManagerProxy;
55 class WebPageGroup;
56 class WebPageProxy;
57 class WebResourceCacheManagerProxy;
58 struct WebProcessCreationParameters;
59 
60 class WebContext : public APIObject {
61 public:
62     static const Type APIType = TypeContext;
63 
64     static WebContext* sharedProcessContext();
65     static WebContext* sharedThreadContext();
66 
67     static PassRefPtr<WebContext> create(const String& injectedBundlePath);
68     virtual ~WebContext();
69 
70     static const Vector<WebContext*>& allContexts();
71 
72     void initializeInjectedBundleClient(const WKContextInjectedBundleClient*);
73     void initializeHistoryClient(const WKContextHistoryClient*);
74     void initializeDownloadClient(const WKContextDownloadClient*);
75 
processModel()76     ProcessModel processModel() const { return m_processModel; }
process()77     WebProcessProxy* process() const { return m_process.get(); }
78 
79     template<typename U> bool sendToAllProcesses(const U& message);
80     template<typename U> bool sendToAllProcessesRelaunchingThemIfNecessary(const U& message);
81 
82     void processDidFinishLaunching(WebProcessProxy*);
83 
84     // Disconnect the process from the context.
85     void disconnectProcess(WebProcessProxy*);
86 
87     PassRefPtr<WebPageProxy> createWebPage(PageClient*, WebPageGroup*);
88 
89     WebProcessProxy* relaunchProcessIfNecessary();
90 
injectedBundlePath()91     const String& injectedBundlePath() const { return m_injectedBundlePath; }
92 
93     DownloadProxy* download(WebPageProxy* initiatingPage, const WebCore::ResourceRequest&);
94 
setInjectedBundleInitializationUserData(PassRefPtr<APIObject> userData)95     void setInjectedBundleInitializationUserData(PassRefPtr<APIObject> userData) { m_injectedBundleInitializationUserData = userData; }
injectedBundleInitializationUserData()96     APIObject* injectedBundleInitializationUserData() const { return m_injectedBundleInitializationUserData.get(); }
97 
98     void postMessageToInjectedBundle(const String&, APIObject*);
99 
100     // InjectedBundle client
101     void didReceiveMessageFromInjectedBundle(const String&, APIObject*);
102     void didReceiveSynchronousMessageFromInjectedBundle(const String&, APIObject*, RefPtr<APIObject>& returnData);
103 
104     void populateVisitedLinks();
105 
106     void setAdditionalPluginsDirectory(const String&);
107 
pluginInfoStore()108     PluginInfoStore* pluginInfoStore() { return &m_pluginInfoStore; }
109     String applicationCacheDirectory();
110 
111     void setAlwaysUsesComplexTextCodePath(bool);
112 
113     void registerURLSchemeAsEmptyDocument(const String&);
114     void registerURLSchemeAsSecure(const String&);
115     void setDomainRelaxationForbiddenForURLScheme(const String&);
116 
117     void addVisitedLink(const String&);
118     void addVisitedLinkHash(WebCore::LinkHash);
119 
120     void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
121     CoreIPC::SyncReplyMode didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
122 
123     void setCacheModel(CacheModel);
cacheModel()124     CacheModel cacheModel() const { return m_cacheModel; }
125 
126     void setDefaultRequestTimeoutInterval(double);
127 
128     void startMemorySampler(const double interval);
129     void stopMemorySampler();
130 
131 #if PLATFORM(WIN)
132     void setShouldPaintNativeControls(bool);
133 
setInitialHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy)134     void setInitialHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy) { m_initialHTTPCookieAcceptPolicy = policy; }
135 #endif
136 
137     void setEnhancedAccessibility(bool);
138 
139     // Downloads.
140     DownloadProxy* createDownloadProxy();
downloadClient()141     WebDownloadClient& downloadClient() { return m_downloadClient; }
142     void downloadFinished(DownloadProxy*);
143 
144     static HashSet<String, CaseFoldingHash> pdfAndPostScriptMIMETypes();
145 
applicationCacheManagerProxy()146     WebApplicationCacheManagerProxy* applicationCacheManagerProxy() const { return m_applicationCacheManagerProxy.get(); }
cookieManagerProxy()147     WebCookieManagerProxy* cookieManagerProxy() const { return m_cookieManagerProxy.get(); }
databaseManagerProxy()148     WebDatabaseManagerProxy* databaseManagerProxy() const { return m_databaseManagerProxy.get(); }
geolocationManagerProxy()149     WebGeolocationManagerProxy* geolocationManagerProxy() const { return m_geolocationManagerProxy.get(); }
iconDatabase()150     WebIconDatabase* iconDatabase() const { return m_iconDatabase.get(); }
keyValueStorageManagerProxy()151     WebKeyValueStorageManagerProxy* keyValueStorageManagerProxy() const { return m_keyValueStorageManagerProxy.get(); }
mediaCacheManagerProxy()152     WebMediaCacheManagerProxy* mediaCacheManagerProxy() const { return m_mediaCacheManagerProxy.get(); }
pluginSiteDataManager()153     WebPluginSiteDataManager* pluginSiteDataManager() const { return m_pluginSiteDataManager.get(); }
resourceCacheManagerProxy()154     WebResourceCacheManagerProxy* resourceCacheManagerProxy() const { return m_resourceCacheManagerProxy.get(); }
155 
156     struct Statistics {
157         unsigned wkViewCount;
158         unsigned wkPageCount;
159         unsigned wkFrameCount;
160     };
161     static Statistics& statistics();
162 
setDatabaseDirectory(const String & dir)163     void setDatabaseDirectory(const String& dir) { m_overrideDatabaseDirectory = dir; }
164     void setIconDatabasePath(const String&);
setLocalStorageDirectory(const String & dir)165     void setLocalStorageDirectory(const String& dir) { m_overrideLocalStorageDirectory = dir; }
166 
167     void ensureWebProcess();
168 
169     bool shouldTerminate(WebProcessProxy*);
170 
disableProcessTermination()171     void disableProcessTermination() { m_processTerminationEnabled = false; }
172     void enableProcessTermination();
173 
174     // Defaults to false.
175     void setHTTPPipeliningEnabled(bool);
176     bool httpPipeliningEnabled();
177 
178 private:
179     WebContext(ProcessModel, const String& injectedBundlePath);
180 
type()181     virtual Type type() const { return APIType; }
182 
183     void platformInitializeWebProcess(WebProcessCreationParameters&);
184     void platformInvalidateContext();
185 
186     // History client
187     void didNavigateWithNavigationData(uint64_t pageID, const WebNavigationDataStore& store, uint64_t frameID);
188     void didPerformClientRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
189     void didPerformServerRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
190     void didUpdateHistoryTitle(uint64_t pageID, const String& title, const String& url, uint64_t frameID);
191 
192     // Plugins
193     void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins);
194     void getPluginPath(const String& mimeType, const String& urlString, String& pluginPath);
195 #if !ENABLE(PLUGIN_PROCESS)
196     void didGetSitesWithPluginData(const Vector<String>& sites, uint64_t callbackID);
197     void didClearPluginSiteData(uint64_t callbackID);
198 #endif
199 
200     // Implemented in generated WebContextMessageReceiver.cpp
201     void didReceiveWebContextMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
202     CoreIPC::SyncReplyMode didReceiveSyncWebContextMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
203 
204     static void languageChanged(void* context);
205     void languageChanged();
206 
207     String databaseDirectory() const;
208     String platformDefaultDatabaseDirectory() const;
209 
210     String iconDatabasePath() const;
211     String platformDefaultIconDatabasePath() const;
212 
213     String localStorageDirectory() const;
214     String platformDefaultLocalStorageDirectory() const;
215 
216     ProcessModel m_processModel;
217 
218     // FIXME: In the future, this should be one or more WebProcessProxies.
219     RefPtr<WebProcessProxy> m_process;
220 
221     RefPtr<WebPageGroup> m_defaultPageGroup;
222 
223     RefPtr<APIObject> m_injectedBundleInitializationUserData;
224     String m_injectedBundlePath;
225     WebContextInjectedBundleClient m_injectedBundleClient;
226 
227     WebHistoryClient m_historyClient;
228 
229     PluginInfoStore m_pluginInfoStore;
230     VisitedLinkProvider m_visitedLinkProvider;
231 
232     HashSet<String> m_schemesToRegisterAsEmptyDocument;
233     HashSet<String> m_schemesToRegisterAsSecure;
234     HashSet<String> m_schemesToSetDomainRelaxationForbiddenFor;
235 
236     bool m_alwaysUsesComplexTextCodePath;
237 
238     Vector<pair<String, RefPtr<APIObject> > > m_pendingMessagesToPostToInjectedBundle;
239 
240     CacheModel m_cacheModel;
241 
242     WebDownloadClient m_downloadClient;
243     HashMap<uint64_t, RefPtr<DownloadProxy> > m_downloads;
244 
245     bool m_memorySamplerEnabled;
246     double m_memorySamplerInterval;
247 
248     RefPtr<WebApplicationCacheManagerProxy> m_applicationCacheManagerProxy;
249     RefPtr<WebCookieManagerProxy> m_cookieManagerProxy;
250     RefPtr<WebDatabaseManagerProxy> m_databaseManagerProxy;
251     RefPtr<WebGeolocationManagerProxy> m_geolocationManagerProxy;
252     RefPtr<WebIconDatabase> m_iconDatabase;
253     RefPtr<WebKeyValueStorageManagerProxy> m_keyValueStorageManagerProxy;
254     RefPtr<WebMediaCacheManagerProxy> m_mediaCacheManagerProxy;
255     RefPtr<WebPluginSiteDataManager> m_pluginSiteDataManager;
256     RefPtr<WebResourceCacheManagerProxy> m_resourceCacheManagerProxy;
257 
258 #if PLATFORM(WIN)
259     bool m_shouldPaintNativeControls;
260     HTTPCookieAcceptPolicy m_initialHTTPCookieAcceptPolicy;
261 #endif
262 
263 #if PLATFORM(MAC)
264     RetainPtr<CFTypeRef> m_enhancedAccessibilityObserver;
265 #endif
266 
267     String m_overrideDatabaseDirectory;
268     String m_overrideIconDatabasePath;
269     String m_overrideLocalStorageDirectory;
270 
271     bool m_processTerminationEnabled;
272 };
273 
sendToAllProcesses(const U & message)274 template<typename U> inline bool WebContext::sendToAllProcesses(const U& message)
275 {
276     if (!m_process || !m_process->canSendMessage())
277         return false;
278 
279     return m_process->send(message, 0);
280 }
281 
sendToAllProcessesRelaunchingThemIfNecessary(const U & message)282 template<typename U> bool WebContext::sendToAllProcessesRelaunchingThemIfNecessary(const U& message)
283 {
284     relaunchProcessIfNecessary();
285 
286     return m_process->send(message, 0);
287 }
288 
289 } // namespace WebKit
290 
291 #endif // WebContext_h
292