1/*
2 * Copyright (C) 2010-2017 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#pragma once
27
28#include <wtf/RefCounted.h>
29#include <wtf/RefPtr.h>
30#include <wtf/ThreadSafeRefCounted.h>
31
32#if PLATFORM(COCOA)
33#include "WKFoundation.h"
34#ifdef __OBJC__
35#include "WKObject.h"
36#endif
37#endif
38
39#define DELEGATE_REF_COUNTING_TO_COCOA PLATFORM(COCOA)
40
41#if DELEGATE_REF_COUNTING_TO_COCOA
42OBJC_CLASS NSObject;
43#endif
44
45namespace API {
46
47class Object
48#if !DELEGATE_REF_COUNTING_TO_COCOA
49 : public ThreadSafeRefCounted<Object>
50#endif
51{
52public:
53 enum class Type {
54 // Base types
55 Null = 0,
56 Array,
57 AuthenticationChallenge,
58 AuthenticationDecisionListener,
59 CertificateInfo,
60 Connection,
61 ContextMenuItem,
62 Credential,
63 Data,
64 Dictionary,
65 Error,
66 FrameHandle,
67 Image,
68 PageGroupData,
69 PageHandle,
70 PageGroupHandle,
71 ProtectionSpace,
72 RenderLayer,
73 RenderObject,
74 SecurityOrigin,
75 SessionState,
76 SerializedScriptValue,
77 String,
78 URL,
79 URLRequest,
80 URLResponse,
81 UserContentURLPattern,
82 UserScript,
83 UserStyleSheet,
84 WebArchive,
85 WebArchiveResource,
86
87 // Base numeric types
88 Boolean,
89 Double,
90 UInt64,
91 Int64,
92
93 // Geometry types
94 Point,
95 Size,
96 Rect,
97
98 // UIProcess types
99 ApplicationCacheManager,
100#if ENABLE(APPLICATION_MANIFEST)
101 ApplicationManifest,
102#endif
103 Attachment,
104 AutomationSession,
105 BackForwardList,
106 BackForwardListItem,
107 CacheManager,
108 ColorPickerResultListener,
109 ContentRuleList,
110 ContentRuleListAction,
111 ContentRuleListStore,
112#if PLATFORM(IOS_FAMILY)
113 ContextMenuElementInfo,
114#endif
115 ContextMenuListener,
116 CookieManager,
117 CustomHeaderFields,
118 InternalDebugFeature,
119 Download,
120 ExperimentalFeature,
121 FormSubmissionListener,
122 Frame,
123 FrameInfo,
124 FramePolicyListener,
125 FullScreenManager,
126 GeolocationManager,
127 GeolocationPermissionRequest,
128 HTTPCookieStore,
129 HitTestResult,
130 GeolocationPosition,
131 GrammarDetail,
132 IconDatabase,
133 Inspector,
134 KeyValueStorageManager,
135 MediaCacheManager,
136 Navigation,
137 NavigationAction,
138 NavigationData,
139 NavigationResponse,
140 Notification,
141 NotificationManager,
142 NotificationPermissionRequest,
143 OpenPanelParameters,
144 OpenPanelResultListener,
145 OriginDataManager,
146 Page,
147 PageConfiguration,
148 PageGroup,
149 ProcessPool,
150 ProcessPoolConfiguration,
151 PluginSiteDataManager,
152 Preferences,
153 RequestStorageAccessConfirmResultListener,
154 ResourceLoadStatisticsStore,
155 RunBeforeUnloadConfirmPanelResultListener,
156 RunJavaScriptAlertResultListener,
157 RunJavaScriptConfirmResultListener,
158 RunJavaScriptPromptResultListener,
159 TextChecker,
160 URLSchemeTask,
161 UserContentController,
162 UserContentWorld,
163 UserInitiatedAction,
164 UserMediaPermissionCheck,
165 UserMediaPermissionRequest,
166 ViewportAttributes,
167 VisitedLinkStore,
168 WebResourceLoadStatisticsManager,
169 WebsiteDataRecord,
170 WebsiteDataStore,
171 WebsiteDataStoreConfiguration,
172 WebsitePolicies,
173 WindowFeatures,
174
175#if ENABLE(MEDIA_SESSION)
176 MediaSessionFocusManager,
177 MediaSessionMetadata,
178#endif
179
180 // Bundle types
181 Bundle,
182 BundleBackForwardList,
183 BundleBackForwardListItem,
184 BundleCSSStyleDeclarationHandle,
185 BundleDOMWindowExtension,
186 BundleFrame,
187 BundleHitTestResult,
188 BundleInspector,
189 BundleNavigationAction,
190 BundleNodeHandle,
191 BundlePage,
192 BundlePageBanner,
193 BundlePageGroup,
194 BundlePageOverlay,
195 BundleRangeHandle,
196 BundleScriptWorld,
197
198 // Platform specific
199 EditCommandProxy,
200 ObjCObjectGraph,
201 View,
202#if USE(SOUP)
203 SoupRequestManager,
204 SoupCustomProtocolRequestManager,
205#endif
206 };
207
208 virtual ~Object()
209 {
210 }
211
212 virtual Type type() const = 0;
213
214#if DELEGATE_REF_COUNTING_TO_COCOA
215#ifdef __OBJC__
216 template<typename T, typename... Args>
217 static void constructInWrapper(NSObject <WKObject> *wrapper, Args&&... args)
218 {
219 Object* object = new (&wrapper._apiObject) T(std::forward<Args>(args)...);
220 object->m_wrapper = wrapper;
221 }
222#endif
223
224 NSObject *wrapper() const { return m_wrapper; }
225
226 void ref() const;
227 void deref() const;
228#endif // DELEGATE_REF_COUNTING_TO_COCOA
229
230 static void* wrap(API::Object*);
231 static API::Object* unwrap(void*);
232
233#if PLATFORM(COCOA) && defined(__OBJC__)
234 static API::Object& fromWKObjectExtraSpace(id <WKObject>);
235#endif
236
237protected:
238 Object();
239
240#if DELEGATE_REF_COUNTING_TO_COCOA
241 static void* newObject(size_t, Type);
242
243private:
244 // Derived classes must override operator new and call newObject().
245 void* operator new(size_t) = delete;
246
247 NSObject *m_wrapper;
248#endif // DELEGATE_REF_COUNTING_TO_COCOA
249};
250
251template <Object::Type ArgumentType>
252class ObjectImpl : public Object {
253public:
254 static const Type APIType = ArgumentType;
255
256 virtual ~ObjectImpl()
257 {
258 }
259
260protected:
261 friend class Object;
262
263 ObjectImpl()
264 {
265 }
266
267 Type type() const override { return APIType; }
268
269#if DELEGATE_REF_COUNTING_TO_COCOA
270 void* operator new(size_t size) { return newObject(size, APIType); }
271 void* operator new(size_t, void* value) { return value; }
272#endif
273};
274
275#if !DELEGATE_REF_COUNTING_TO_COCOA
276inline void* Object::wrap(API::Object* object)
277{
278 return static_cast<void*>(object);
279}
280
281inline API::Object* Object::unwrap(void* object)
282{
283 return static_cast<API::Object*>(object);
284}
285#endif
286
287} // namespace Object
288
289#undef DELEGATE_REF_COUNTING_TO_COCOA
290