Deprecate ObjC SPI in Deprecated Xcode group
[WebKit-https.git] / Source / WebKit / UIProcess / Cocoa / WebProcessProxyCocoa.mm
1 /*
2  * Copyright (C) 2014-2016 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 "WebProcessProxy.h"
28
29 #import "ObjCObjectGraph.h"
30 #import "SandboxUtilities.h"
31 #import "WKBrowsingContextControllerInternal.h"
32 #import "WKBrowsingContextHandleInternal.h"
33 #import "WKTypeRefWrapper.h"
34 #import "WebProcessMessages.h"
35 #import "WebProcessPool.h"
36 #import <sys/sysctl.h>
37 #import <wtf/NeverDestroyed.h>
38
39 namespace WebKit {
40
41 const HashSet<String>& WebProcessProxy::platformPathsWithAssumedReadAccess()
42 {
43     static NeverDestroyed<HashSet<String>> platformPathsWithAssumedReadAccess(std::initializer_list<String> {
44         [NSBundle bundleWithIdentifier:@"com.apple.WebCore"].resourcePath.stringByStandardizingPath,
45         [NSBundle bundleWithIdentifier:@"com.apple.WebKit"].resourcePath.stringByStandardizingPath
46     });
47
48     return platformPathsWithAssumedReadAccess;
49 }
50
51 RefPtr<ObjCObjectGraph> WebProcessProxy::transformHandlesToObjects(ObjCObjectGraph& objectGraph)
52 {
53     struct Transformer final : ObjCObjectGraph::Transformer {
54         Transformer(WebProcessProxy& webProcessProxy)
55             : m_webProcessProxy(webProcessProxy)
56         {
57         }
58
59         bool shouldTransformObject(id object) const override
60         {
61 #if WK_API_ENABLED
62             if (dynamic_objc_cast<WKBrowsingContextHandle>(object))
63                 return true;
64
65             ALLOW_DEPRECATED_DECLARATIONS_BEGIN
66             if (dynamic_objc_cast<WKTypeRefWrapper>(object))
67                 return true;
68             ALLOW_DEPRECATED_DECLARATIONS_END
69 #endif
70             return false;
71         }
72
73         RetainPtr<id> transformObject(id object) const override
74         {
75 #if WK_API_ENABLED
76             if (auto* handle = dynamic_objc_cast<WKBrowsingContextHandle>(object)) {
77                 if (auto* webPageProxy = m_webProcessProxy.webPage(handle.pageID)) {
78                     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
79                     return [WKBrowsingContextController _browsingContextControllerForPageRef:toAPI(webPageProxy)];
80                     ALLOW_DEPRECATED_DECLARATIONS_END
81                 }
82
83                 return [NSNull null];
84             }
85
86             ALLOW_DEPRECATED_DECLARATIONS_BEGIN
87             if (auto* wrapper = dynamic_objc_cast<WKTypeRefWrapper>(object))
88                 return adoptNS([[WKTypeRefWrapper alloc] initWithObject:toAPI(m_webProcessProxy.transformHandlesToObjects(toImpl(wrapper.object)).get())]);
89             ALLOW_DEPRECATED_DECLARATIONS_END
90 #endif
91             return object;
92         }
93
94         WebProcessProxy& m_webProcessProxy;
95     };
96
97     return ObjCObjectGraph::create(ObjCObjectGraph::transform(objectGraph.rootObject(), Transformer(*this)).get());
98 }
99
100 RefPtr<ObjCObjectGraph> WebProcessProxy::transformObjectsToHandles(ObjCObjectGraph& objectGraph)
101 {
102     struct Transformer final : ObjCObjectGraph::Transformer {
103         bool shouldTransformObject(id object) const override
104         {
105 #if WK_API_ENABLED
106             ALLOW_DEPRECATED_DECLARATIONS_BEGIN
107             if (dynamic_objc_cast<WKBrowsingContextController>(object))
108                 return true;
109             if (dynamic_objc_cast<WKTypeRefWrapper>(object))
110                 return true;
111             ALLOW_DEPRECATED_DECLARATIONS_END
112 #endif
113             return false;
114         }
115
116         RetainPtr<id> transformObject(id object) const override
117         {
118 #if WK_API_ENABLED
119             ALLOW_DEPRECATED_DECLARATIONS_BEGIN
120             if (auto* controller = dynamic_objc_cast<WKBrowsingContextController>(object))
121                 return controller.handle;
122             if (auto* wrapper = dynamic_objc_cast<WKTypeRefWrapper>(object))
123                 return adoptNS([[WKTypeRefWrapper alloc] initWithObject:toAPI(transformObjectsToHandles(toImpl(wrapper.object)).get())]);
124             ALLOW_DEPRECATED_DECLARATIONS_END
125 #endif
126
127             return object;
128         }
129     };
130
131     return ObjCObjectGraph::create(ObjCObjectGraph::transform(objectGraph.rootObject(), Transformer()).get());
132 }
133
134 bool WebProcessProxy::platformIsBeingDebugged() const
135 {
136     // If the UI process is sandboxed, it cannot find out whether other processes are being debugged.
137     if (currentProcessIsSandboxed())
138         return false;
139
140     struct kinfo_proc info;
141     int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, processIdentifier() };
142     size_t size = sizeof(info);
143     if (sysctl(mib, WTF_ARRAY_LENGTH(mib), &info, &size, nullptr, 0) == -1)
144         return false;
145
146     return info.kp_proc.p_flag & P_TRACED;
147 }
148
149 static Vector<String>& mediaTypeCache()
150 {
151     ASSERT(RunLoop::isMain());
152     static NeverDestroyed<Vector<String>> typeCache;
153     return typeCache;
154 }
155
156 void WebProcessProxy::cacheMediaMIMETypes(const Vector<String>& types)
157 {
158     if (!mediaTypeCache().isEmpty())
159         return;
160
161     mediaTypeCache() = types;
162     for (auto& process : processPool().processes()) {
163         if (process != this)
164             cacheMediaMIMETypesInternal(types);
165     }
166 }
167
168 void WebProcessProxy::cacheMediaMIMETypesInternal(const Vector<String>& types)
169 {
170     if (!mediaTypeCache().isEmpty())
171         return;
172
173     mediaTypeCache() = types;
174     send(Messages::WebProcess::SetMediaMIMETypes(types), 0);
175 }
176
177 Vector<String> WebProcessProxy::mediaMIMETypes()
178 {
179     return mediaTypeCache();
180 }
181
182 }