1/*
2 * Copyright (C) 2006 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#if PLATFORM(IOS)
27
28#import "WebUIKitSupport.h"
29
30#import <CoreFoundation/CFPriv.h>
31#import <WebCore/break_lines.h>
32#import <WebCore/ResourceRequest.h>
33#import <WebCore/TextBreakIterator.h>
34#import <WebCore/WebCoreSystemInterface.h>
35#import <WebCore/WebCoreThreadSystemInterface.h>
36#import "WebDatabaseManagerInternal.h"
37#import "WebKitSystemInterface.h"
38#import "WebLocalizableStrings.h"
39#import "WebPlatformStrategies.h"
40#import "WebSystemInterface.h"
41#import "WebViewPrivate.h"
42
43
44#import <runtime/InitializeThreading.h>
45
46using namespace WebCore;
47
48static inline bool linkedOnOrAfterIOS5()
49{
50    static bool s_linkedOnOrAfterIOS5 = iosExecutableWasLinkedOnOrAfterVersion(wkIOSSystemVersion_5_0);
51    return s_linkedOnOrAfterIOS5;
52}
53
54void WebKitInitialize(void)
55{
56    static bool webkitInitialized;
57    if (webkitInitialized)
58        return;
59
60    ASSERT(pthread_main_np());
61    webkitInitialized = true;
62    InitWebCoreThreadSystemInterface();
63    [WebView enableWebThread];
64    InitWebCoreSystemInterface();
65
66    // Initialize our platform strategies.
67    WebPlatformStrategies::initializeIfNecessary();
68
69    // We'd rather eat this cost at startup than slow down situations that need to be responsive.
70    // See <rdar://problem/6776301>.
71    LoadWebLocalizedStrings();
72    [WebView _setAllowsRoundingHacks:!linkedOnOrAfterIOS5()];
73    [WebView registerForMemoryNotifications];
74
75    // This needs to be called before any requests are made in the process, <rdar://problem/9691871>
76    WebCore::initializeHTTPConnectionSettingsOnStartup();
77    WebCore::enableURLSchemeCanonicalization(linkedOnOrAfterIOS5());
78}
79
80void WebKitSetIsClassic(BOOL flag)
81{
82    // FIXME: Remove this once it stops being called.
83}
84
85float WebKitGetMinimumZoomFontSize(void)
86{
87    return WKGetMinimumZoomFontSize();
88}
89
90int WebKitGetLastLineBreakInBuffer(UChar *characters, int position, int length)
91{
92    int lastBreakPos = position;
93    int breakPos = 0;
94    LazyLineBreakIterator breakIterator(String(characters, length));
95    while ((breakPos = nextBreakablePosition(breakIterator, breakPos)) < position)
96        lastBreakPos = breakPos++;
97    return lastBreakPos < position ? (NSUInteger)lastBreakPos : INT_MAX;
98}
99
100const char *WebKitPlatformSystemRootDirectory(void)
101{
102#if PLATFORM(IOS_SIMULATOR)
103    static const char *platformSystemRootDirectory = nil;
104    if (!platformSystemRootDirectory) {
105        char *simulatorRoot = getenv("IPHONE_SIMULATOR_ROOT");
106        platformSystemRootDirectory = simulatorRoot ? simulatorRoot : "/";
107    }
108    return platformSystemRootDirectory;
109#else
110    return "/";
111#endif
112}
113
114static void applicationDidEnterBackground(CFNotificationCenterRef, void*, CFStringRef, const void *, CFDictionaryRef)
115{
116    WebKitSetWebDatabasePaused(true);
117}
118
119static void applicationWillEnterForeground(CFNotificationCenterRef, void*, CFStringRef, const void*, CFDictionaryRef)
120{
121    WebKitSetWebDatabasePaused(false);
122}
123
124void WebKitSetBackgroundAndForegroundNotificationNames(NSString *didEnterBackgroundName, NSString *willEnterForegroundName)
125{
126    static bool initialized = false;
127    if (initialized)
128        return;
129    initialized = true;
130
131    CFNotificationCenterRef notificationCenter = CFNotificationCenterGetLocalCenter();
132    CFNotificationCenterAddObserver(notificationCenter, 0, applicationDidEnterBackground, (CFStringRef)didEnterBackgroundName, NULL, CFNotificationSuspensionBehaviorCoalesce);
133    CFNotificationCenterAddObserver(notificationCenter, 0, applicationWillEnterForeground, (CFStringRef)willEnterForegroundName, NULL, CFNotificationSuspensionBehaviorCoalesce);
134
135}
136
137static WebBackgroundTaskIdentifier invalidTaskIdentifier = 0;
138static StartBackgroundTaskBlock startBackgroundTaskBlock = 0;
139static EndBackgroundTaskBlock endBackgroundTaskBlock = 0;
140
141void WebKitSetInvalidWebBackgroundTaskIdentifier(WebBackgroundTaskIdentifier taskIdentifier)
142{
143    invalidTaskIdentifier = taskIdentifier;
144}
145
146void WebKitSetStartBackgroundTaskBlock(StartBackgroundTaskBlock startBlock)
147{
148    Block_release(startBackgroundTaskBlock);
149    startBackgroundTaskBlock = Block_copy(startBlock);
150}
151
152void WebKitSetEndBackgroundTaskBlock(EndBackgroundTaskBlock endBlock)
153{
154    Block_release(endBackgroundTaskBlock);
155    endBackgroundTaskBlock = Block_copy(endBlock);
156}
157
158WebBackgroundTaskIdentifier invalidWebBackgroundTaskIdentifier()
159{
160    return invalidTaskIdentifier;
161}
162
163WebBackgroundTaskIdentifier startBackgroundTask(VoidBlock expirationHandler)
164{
165    if (!startBackgroundTaskBlock)
166        return invalidTaskIdentifier;
167    return startBackgroundTaskBlock(expirationHandler);
168}
169
170void endBackgroundTask(WebBackgroundTaskIdentifier taskIdentifier)
171{
172    if (!endBackgroundTaskBlock)
173        return;
174    endBackgroundTaskBlock(taskIdentifier);
175}
176
177#endif // PLATFORM(IOS)
178