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#import "config.h"
27#import "WebContext.h"
28
29#import "PluginProcessManager.h"
30#import "SandboxUtilities.h"
31#import "TextChecker.h"
32#import "WKBrowsingContextControllerInternal.h"
33#import "WKBrowsingContextControllerInternal.h"
34#import "WebKitSystemInterface.h"
35#import "WebMemoryPressureHandlerIOS.h"
36#import "WebPageGroup.h"
37#import "WebPreferencesKeys.h"
38#import "WebProcessCreationParameters.h"
39#import "WebProcessMessages.h"
40#import "WindowServerConnection.h"
41#import <WebCore/Color.h>
42#import <WebCore/FileSystem.h>
43#import <WebCore/NotImplemented.h>
44#import <WebCore/PlatformPasteboard.h>
45#import <WebCore/SharedBuffer.h>
46#import <sys/param.h>
47
48#if ENABLE(NETWORK_PROCESS)
49#import "NetworkProcessCreationParameters.h"
50#import "NetworkProcessProxy.h"
51#endif
52
53#if PLATFORM(IOS)
54#import <WebCore/RuntimeApplicationChecksIOS.h>
55#else
56#import <QuartzCore/CARemoteLayerServer.h>
57#endif
58
59#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
60
61#if __has_include(<CFNetwork/CFURLProtocolPriv.h>)
62#include <CFNetwork/CFURLProtocolPriv.h>
63#else
64extern "C" Boolean _CFNetworkIsKnownHSTSHostWithSession(CFURLRef url, CFURLStorageSessionRef session);
65extern "C" void _CFNetworkResetHSTSHostsWithSession(CFURLStorageSessionRef session);
66#endif
67
68#endif
69
70using namespace WebCore;
71
72NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
73NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
74NSString *WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey";
75NSString *WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey = @"WebKitKerningAndLigaturesEnabledByDefault";
76NSString *WebKitJSCJITEnabledDefaultsKey = @"WebKitJSCJITEnabledDefaultsKey";
77NSString *WebKitJSCFTLJITEnabledDefaultsKey = @"WebKitJSCFTLJITEnabledDefaultsKey";
78
79#if !PLATFORM(IOS)
80static NSString *WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification = @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
81#endif
82
83// FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
84NSString *WebIconDatabaseDirectoryDefaultsKey = @"WebIconDatabaseDirectoryDefaultsKey";
85
86#if ENABLE(NETWORK_PROCESS)
87static NSString * const WebKit2HTTPProxyDefaultsKey = @"WebKit2HTTPProxy";
88static NSString * const WebKit2HTTPSProxyDefaultsKey = @"WebKit2HTTPSProxy";
89#endif
90
91namespace WebKit {
92
93NSString *SchemeForCustomProtocolRegisteredNotificationName = @"WebKitSchemeForCustomProtocolRegisteredNotification";
94NSString *SchemeForCustomProtocolUnregisteredNotificationName = @"WebKitSchemeForCustomProtocolUnregisteredNotification";
95
96static void registerUserDefaultsIfNeeded()
97{
98    static bool didRegister;
99    if (didRegister)
100        return;
101
102    didRegister = true;
103    NSMutableDictionary *registrationDictionary = [NSMutableDictionary dictionary];
104
105    [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitJSCJITEnabledDefaultsKey];
106    [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitJSCFTLJITEnabledDefaultsKey];
107
108#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
109    [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey];
110#endif
111
112    [[NSUserDefaults standardUserDefaults] registerDefaults:registrationDictionary];
113}
114
115void WebContext::updateProcessSuppressionState() const
116{
117#if ENABLE(NETWORK_PROCESS)
118    if (m_usesNetworkProcess && m_networkProcess)
119        m_networkProcess->setProcessSuppressionEnabled(processSuppressionEnabled());
120#endif
121#if ENABLE(NETSCAPE_PLUGIN_API)
122    PluginProcessManager::shared().setProcessSuppressionEnabled(processSuppressionPreferenceIsEnabledForAllContexts());
123#endif
124}
125
126NSMutableDictionary *WebContext::ensureBundleParameters()
127{
128    if (!m_bundleParameters)
129        m_bundleParameters = adoptNS([[NSMutableDictionary alloc] init]);
130
131    return m_bundleParameters.get();
132}
133
134void WebContext::platformInitialize()
135{
136    registerUserDefaultsIfNeeded();
137    registerNotificationObservers();
138
139#if PLATFORM(IOS)
140    WebKit::WebMemoryPressureHandler::shared();
141#endif
142}
143
144String WebContext::platformDefaultApplicationCacheDirectory() const
145{
146    NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
147    if (!appName)
148        appName = [[NSProcessInfo processInfo] processName];
149#if PLATFORM(IOS)
150    // This quirk used to make these apps share application cache storage, but doesn't accomplish that any more.
151    // Preserving it avoids the need to migrate data when upgrading.
152    if (applicationIsMobileSafari() || applicationIsWebApp())
153        appName = @"com.apple.WebAppCache";
154#endif
155
156    ASSERT(appName);
157
158#if PLATFORM(IOS)
159    NSString *cacheDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
160#else
161    char cacheDirectory[MAXPATHLEN];
162    size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
163    if (!cacheDirectoryLen)
164        return String();
165
166    NSString *cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
167#endif
168    NSString* cachePath = [cacheDir stringByAppendingPathComponent:appName];
169    return stringByResolvingSymlinksInPath([cachePath stringByStandardizingPath]);
170}
171
172void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
173{
174    parameters.presenterApplicationPid = getpid();
175
176#if PLATFORM(MAC)
177    parameters.accessibilityEnhancedUserInterfaceEnabled = [[NSApp accessibilityAttributeValue:@"AXEnhancedUserInterface"] boolValue];
178#else
179    parameters.accessibilityEnhancedUserInterfaceEnabled = false;
180#endif
181
182    NSURLCache *urlCache = [NSURLCache sharedURLCache];
183    parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
184    parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
185
186#if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
187    parameters.shouldForceScreenFontSubstitution = [[NSUserDefaults standardUserDefaults] boolForKey:@"NSFontDefaultScreenFontSubstitutionEnabled"];
188#endif
189    parameters.shouldEnableKerningAndLigaturesByDefault = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey];
190    parameters.shouldEnableJIT = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitJSCJITEnabledDefaultsKey];
191    parameters.shouldEnableFTLJIT = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitJSCFTLJITEnabledDefaultsKey];
192    parameters.shouldEnableMemoryPressureReliefLogging = [[NSUserDefaults standardUserDefaults] boolForKey:@"LogMemoryJetsamDetails"];
193
194#if HAVE(HOSTED_CORE_ANIMATION)
195#if !PLATFORM(IOS)
196    mach_port_t renderServerPort = [[CARemoteLayerServer sharedServer] serverPort];
197    if (renderServerPort != MACH_PORT_NULL)
198        parameters.acceleratedCompositingPort = IPC::MachPort(renderServerPort, MACH_MSG_TYPE_COPY_SEND);
199#endif
200#endif
201
202    // FIXME: This should really be configurable; we shouldn't just blindly allow read access to the UI process bundle.
203    parameters.uiProcessBundleResourcePath = [[NSBundle mainBundle] resourcePath];
204    SandboxExtension::createHandle(parameters.uiProcessBundleResourcePath, SandboxExtension::ReadOnly, parameters.uiProcessBundleResourcePathExtensionHandle);
205
206    parameters.uiProcessBundleIdentifier = String([[NSBundle mainBundle] bundleIdentifier]);
207
208#if ENABLE(NETWORK_PROCESS)
209    if (!m_usesNetworkProcess) {
210#endif
211#if ENABLE(CUSTOM_PROTOCOLS)
212        for (const auto& scheme : globalURLSchemesWithCustomProtocolHandlers())
213            parameters.urlSchemesRegisteredForCustomProtocols.append(scheme);
214#endif
215#if ENABLE(NETWORK_PROCESS)
216    }
217#endif
218
219    if (m_bundleParameters) {
220        auto data = adoptNS([[NSMutableData alloc] init]);
221        auto keyedArchiver = adoptNS([[NSKeyedArchiver alloc] initForWritingWithMutableData:data.get()]);
222
223        [keyedArchiver setRequiresSecureCoding:YES];
224
225        @try {
226            [keyedArchiver encodeObject:m_bundleParameters.get() forKey:@"parameters"];
227            [keyedArchiver finishEncoding];
228        } @catch (NSException *exception) {
229            LOG_ERROR("Failed to encode bundle parameters: %@", exception);
230        }
231
232        parameters.bundleParameterData = API::Data::createWithoutCopying((const unsigned char*)[data bytes], [data length], [] (unsigned char*, const void* data) {
233            [(NSData *)data release];
234        }, data.leakRef());
235    }
236}
237
238#if ENABLE(NETWORK_PROCESS)
239void WebContext::platformInitializeNetworkProcess(NetworkProcessCreationParameters& parameters)
240{
241    NSURLCache *urlCache = [NSURLCache sharedURLCache];
242    parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
243    parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
244
245    parameters.parentProcessName = [[NSProcessInfo processInfo] processName];
246    parameters.uiProcessBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
247
248#if ENABLE(CUSTOM_PROTOCOLS)
249    for (const auto& scheme : globalURLSchemesWithCustomProtocolHandlers())
250        parameters.urlSchemesRegisteredForCustomProtocols.append(scheme);
251#endif
252
253    parameters.httpProxy = [[NSUserDefaults standardUserDefaults] stringForKey:WebKit2HTTPProxyDefaultsKey];
254    parameters.httpsProxy = [[NSUserDefaults standardUserDefaults] stringForKey:WebKit2HTTPSProxyDefaultsKey];
255}
256#endif
257
258void WebContext::platformInvalidateContext()
259{
260    unregisterNotificationObservers();
261}
262
263String WebContext::platformDefaultDiskCacheDirectory() const
264{
265    RetainPtr<NSString> cachePath = adoptNS((NSString *)WKCopyFoundationCacheDirectory());
266    if (!cachePath)
267        cachePath = @"~/Library/Caches/com.apple.WebKit.WebProcess";
268    return stringByResolvingSymlinksInPath([cachePath stringByStandardizingPath]);
269}
270
271String WebContext::platformDefaultCookieStorageDirectory() const
272{
273#if PLATFORM(IOS)
274    String path = pathForProcessContainer();
275    if (path.isEmpty())
276        path = NSHomeDirectory();
277
278    path = path + "/Library/Cookies";
279    path = stringByResolvingSymlinksInPath(path);
280    return path;
281#else
282    notImplemented();
283    return [@"" stringByStandardizingPath];
284#endif
285}
286
287#if PLATFORM(IOS)
288String WebContext::openGLCacheDirectory() const
289{
290    String path = pathForProcessContainer();
291    if (path.isEmpty())
292        path = NSHomeDirectory();
293
294    path = path + "/Library/Caches/com.apple.WebKit.WebContent/com.apple.opengl/";
295    return stringByResolvingSymlinksInPath(path);
296}
297
298String WebContext::parentBundleDirectory() const
299{
300    return [[[NSBundle mainBundle] bundlePath] stringByStandardizingPath];
301}
302
303String WebContext::networkingHSTSDatabasePath() const
304{
305    String path = pathForProcessContainer();
306    if (path.isEmpty())
307        path = NSHomeDirectory();
308
309    path = path + "/Library/Caches/com.apple.WebKit.Networking/";
310    path = stringByResolvingSymlinksInPath(path);
311
312    NSError *error = nil;
313    NSString* nsPath = path;
314    if (![[NSFileManager defaultManager] createDirectoryAtPath:nsPath withIntermediateDirectories:YES attributes:nil error:&error]) {
315        NSLog(@"could not create \"%@\", error %@", nsPath, error);
316        return String();
317    }
318
319    return path + "/HSTS.plist";
320}
321
322String WebContext::webContentHSTSDatabasePath() const
323{
324    String path = pathForProcessContainer();
325    if (path.isEmpty())
326        path = NSHomeDirectory();
327
328    path = path + "/Library/Caches/com.apple.WebKit.WebContent/";
329    path = stringByResolvingSymlinksInPath(path);
330
331    NSError *error = nil;
332    NSString* nsPath = path;
333    if (![[NSFileManager defaultManager] createDirectoryAtPath:nsPath withIntermediateDirectories:YES attributes:nil error:&error]) {
334        NSLog(@"could not create \"%@\", error %@", nsPath, error);
335        return String();
336    }
337
338    return path + "/HSTS.plist";
339}
340
341String WebContext::containerTemporaryDirectory() const
342{
343    String path = NSTemporaryDirectory();
344    return stringByResolvingSymlinksInPath(path);
345}
346#endif
347
348String WebContext::platformDefaultWebSQLDatabaseDirectory()
349{
350    NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebDatabaseDirectoryDefaultsKey];
351    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
352        databasesDirectory = @"~/Library/WebKit/Databases";
353    return stringByResolvingSymlinksInPath([databasesDirectory stringByStandardizingPath]);
354}
355
356String WebContext::platformDefaultIndexedDBDatabaseDirectory()
357{
358    // Indexed databases exist in a subdirectory of the "database directory path."
359    // Currently, the top level of that directory contains entities related to WebSQL databases.
360    // We should fix this, and move WebSQL into a subdirectory (https://bugs.webkit.org/show_bug.cgi?id=124807)
361    // In the meantime, an entity name prefixed with three underscores will not conflict with any WebSQL entities.
362    return pathByAppendingComponent(platformDefaultWebSQLDatabaseDirectory(), "___IndexedDB");
363}
364
365String WebContext::platformDefaultIconDatabasePath() const
366{
367    // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
368    NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebIconDatabaseDirectoryDefaultsKey];
369    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
370        databasesDirectory = @"~/Library/Icons/WebpageIcons.db";
371    return stringByResolvingSymlinksInPath([databasesDirectory stringByStandardizingPath]);
372}
373
374String WebContext::platformDefaultLocalStorageDirectory()
375{
376    NSString *localStorageDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebStorageDirectoryDefaultsKey];
377    if (!localStorageDirectory || ![localStorageDirectory isKindOfClass:[NSString class]])
378        localStorageDirectory = @"~/Library/WebKit/LocalStorage";
379    return stringByResolvingSymlinksInPath([localStorageDirectory stringByStandardizingPath]);
380}
381
382bool WebContext::omitPDFSupport()
383{
384    // Since this is a "secret default" we don't bother registering it.
385    return [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitOmitPDFSupport"];
386}
387
388void WebContext::getPasteboardTypes(const String& pasteboardName, Vector<String>& pasteboardTypes)
389{
390    PlatformPasteboard(pasteboardName).getTypes(pasteboardTypes);
391}
392
393void WebContext::getPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, Vector<String>& pathnames)
394{
395    PlatformPasteboard(pasteboardName).getPathnamesForType(pathnames, pasteboardType);
396}
397
398void WebContext::getPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, String& string)
399{
400    string = PlatformPasteboard(pasteboardName).stringForType(pasteboardType);
401}
402
403void WebContext::getPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, SharedMemory::Handle& handle, uint64_t& size)
404{
405    RefPtr<SharedBuffer> buffer = PlatformPasteboard(pasteboardName).bufferForType(pasteboardType);
406    if (!buffer)
407        return;
408    size = buffer->size();
409    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(size);
410    if (!sharedMemoryBuffer)
411        return;
412    memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
413    sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
414}
415
416void WebContext::pasteboardCopy(const String& fromPasteboard, const String& toPasteboard, uint64_t& newChangeCount)
417{
418    newChangeCount = PlatformPasteboard(toPasteboard).copy(fromPasteboard);
419}
420
421void WebContext::getPasteboardChangeCount(const String& pasteboardName, uint64_t& changeCount)
422{
423    changeCount = PlatformPasteboard(pasteboardName).changeCount();
424}
425
426void WebContext::getPasteboardUniqueName(String& pasteboardName)
427{
428    pasteboardName = PlatformPasteboard::uniqueName();
429}
430
431void WebContext::getPasteboardColor(const String& pasteboardName, WebCore::Color& color)
432{
433    color = PlatformPasteboard(pasteboardName).color();
434}
435
436void WebContext::getPasteboardURL(const String& pasteboardName, WTF::String& urlString)
437{
438    urlString = PlatformPasteboard(pasteboardName).url().string();
439}
440
441void WebContext::addPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes, uint64_t& newChangeCount)
442{
443    newChangeCount = PlatformPasteboard(pasteboardName).addTypes(pasteboardTypes);
444}
445
446void WebContext::setPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes, uint64_t& newChangeCount)
447{
448    newChangeCount = PlatformPasteboard(pasteboardName).setTypes(pasteboardTypes);
449}
450
451void WebContext::setPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, const Vector<String>& pathnames, uint64_t& newChangeCount)
452{
453    newChangeCount = PlatformPasteboard(pasteboardName).setPathnamesForType(pathnames, pasteboardType);
454}
455
456void WebContext::setPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, const String& string, uint64_t& newChangeCount)
457{
458    newChangeCount = PlatformPasteboard(pasteboardName).setStringForType(string, pasteboardType);
459}
460
461void WebContext::setPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, const SharedMemory::Handle& handle, uint64_t size, uint64_t& newChangeCount)
462{
463    if (handle.isNull()) {
464        newChangeCount = PlatformPasteboard(pasteboardName).setBufferForType(0, pasteboardType);
465        return;
466    }
467    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
468    RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
469    newChangeCount = PlatformPasteboard(pasteboardName).setBufferForType(buffer, pasteboardType);
470}
471
472#if PLATFORM(IOS)
473void WebContext::writeWebContentToPasteboard(const WebCore::PasteboardWebContent& content)
474{
475    PlatformPasteboard().write(content);
476}
477
478void WebContext::writeImageToPasteboard(const WebCore::PasteboardImage& pasteboardImage)
479{
480    PlatformPasteboard().write(pasteboardImage);
481}
482
483void WebContext::writeStringToPasteboard(const String& pasteboardType, const String& text)
484{
485    PlatformPasteboard().write(pasteboardType, text);
486}
487
488void WebContext::readStringFromPasteboard(uint64_t index, const String& pasteboardType, WTF::String& value)
489{
490    value = PlatformPasteboard().readString(index, pasteboardType);
491}
492
493void WebContext::readURLFromPasteboard(uint64_t index, const String& pasteboardType, String& url)
494{
495    url = PlatformPasteboard().readURL(index, pasteboardType);
496}
497
498void WebContext::readBufferFromPasteboard(uint64_t index, const String& pasteboardType, SharedMemory::Handle& handle, uint64_t& size)
499{
500    RefPtr<SharedBuffer> buffer = PlatformPasteboard().readBuffer(index, pasteboardType);
501    if (!buffer)
502        return;
503    size = buffer->size();
504    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(size);
505    if (!sharedMemoryBuffer)
506        return;
507    memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
508    sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
509}
510
511void WebContext::getPasteboardItemsCount(uint64_t& itemsCount)
512{
513    itemsCount = PlatformPasteboard().count();
514}
515
516#endif
517
518bool WebContext::processSuppressionEnabled() const
519{
520    for (const auto& process : m_processes) {
521        if (!process->allPagesAreProcessSuppressible())
522            return false;
523    }
524    return true;
525}
526
527bool WebContext::processSuppressionIsEnabledForAllContexts()
528{
529    for (const auto* context : WebContext::allContexts()) {
530        if (!context->processSuppressionEnabled())
531            return false;
532    }
533    return true;
534}
535
536bool WebContext::processSuppressionPreferenceIsEnabledForAllContexts()
537{
538    for (const auto* context : WebContext::allContexts()) {
539        if (!context->m_defaultPageGroup->preferences().store().getBoolValueForKey(WebPreferencesKey::pageVisibilityBasedProcessSuppressionEnabledKey()))
540            return false;
541    }
542    return true;
543}
544
545void WebContext::registerNotificationObservers()
546{
547#if !PLATFORM(IOS)
548    // Listen for enhanced accessibility changes and propagate them to the WebProcess.
549    m_enhancedAccessibilityObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
550        setEnhancedAccessibility([[[note userInfo] objectForKey:@"AXEnhancedUserInterface"] boolValue]);
551    }];
552
553    m_automaticTextReplacementNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSSpellCheckerDidChangeAutomaticTextReplacementNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
554        TextChecker::didChangeAutomaticTextReplacementEnabled();
555        textCheckerStateChanged();
556    }];
557
558    m_automaticSpellingCorrectionNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSSpellCheckerDidChangeAutomaticSpellingCorrectionNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
559        TextChecker::didChangeAutomaticSpellingCorrectionEnabled();
560        textCheckerStateChanged();
561    }];
562
563#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
564    m_automaticQuoteSubstitutionNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSSpellCheckerDidChangeAutomaticQuoteSubstitutionNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
565        TextChecker::didChangeAutomaticQuoteSubstitutionEnabled();
566        textCheckerStateChanged();
567    }];
568
569    m_automaticDashSubstitutionNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSSpellCheckerDidChangeAutomaticDashSubstitutionNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
570        TextChecker::didChangeAutomaticDashSubstitutionEnabled();
571        textCheckerStateChanged();
572    }];
573#endif
574#endif // !PLATFORM(IOS)
575}
576
577void WebContext::unregisterNotificationObservers()
578{
579#if !PLATFORM(IOS)
580    [[NSNotificationCenter defaultCenter] removeObserver:m_enhancedAccessibilityObserver.get()];
581    [[NSNotificationCenter defaultCenter] removeObserver:m_automaticTextReplacementNotificationObserver.get()];
582    [[NSNotificationCenter defaultCenter] removeObserver:m_automaticSpellingCorrectionNotificationObserver.get()];
583#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
584    [[NSNotificationCenter defaultCenter] removeObserver:m_automaticQuoteSubstitutionNotificationObserver.get()];
585    [[NSNotificationCenter defaultCenter] removeObserver:m_automaticDashSubstitutionNotificationObserver.get()];
586#endif
587#endif // !PLATFORM(IOS)
588}
589
590#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
591static CFURLStorageSessionRef privateBrowsingSession()
592{
593    static CFURLStorageSessionRef session;
594    static dispatch_once_t once;
595    dispatch_once(&once, ^{
596        NSString *identifier = [NSString stringWithFormat:@"%@.PrivateBrowsing", [[NSBundle mainBundle] bundleIdentifier]];
597
598        session = WKCreatePrivateStorageSession((CFStringRef)identifier);
599    });
600
601    return session;
602}
603#endif
604
605bool WebContext::isURLKnownHSTSHost(const String& urlString, bool privateBrowsingEnabled) const
606{
607#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
608    RetainPtr<CFURLRef> url = URL(URL(), urlString).createCFURL();
609
610    return _CFNetworkIsKnownHSTSHostWithSession(url.get(), privateBrowsingEnabled ? privateBrowsingSession() : nullptr);
611#else
612    return false;
613#endif
614}
615
616void WebContext::resetHSTSHosts()
617{
618#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
619    _CFNetworkResetHSTSHostsWithSession(nullptr);
620    _CFNetworkResetHSTSHostsWithSession(privateBrowsingSession());
621#endif
622}
623
624int networkProcessLatencyQOS()
625{
626    static int qos = [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitNetworkProcessLatencyQOS"];
627    return qos;
628}
629
630int networkProcessThroughputQOS()
631{
632    static int qos = [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitNetworkProcessThroughputQOS"];
633    return qos;
634}
635
636int webProcessLatencyQOS()
637{
638    static int qos = [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitWebProcessLatencyQOS"];
639    return qos;
640}
641
642int webProcessThroughputQOS()
643{
644    static int qos = [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitWebProcessThroughputQOS"];
645    return qos;
646}
647
648} // namespace WebKit
649