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 ContextMenuListener,
113 CookieManager,
114 CustomHeaderFields,
115 InternalDebugFeature,
116 Download,
117 ExperimentalFeature,
118 FormSubmissionListener,
119 Frame,
120 FrameInfo,
121 FramePolicyListener,
122 FullScreenManager,
123 GeolocationManager,
124 GeolocationPermissionRequest,
125 HTTPCookieStore,
126 HitTestResult,
127 GeolocationPosition,
128 GrammarDetail,
129 IconDatabase,
130 Inspector,
131 KeyValueStorageManager,
132 MediaCacheManager,
133 Navigation,
134 NavigationAction,
135 NavigationData,
136 NavigationResponse,
137 Notification,
138 NotificationManager,
139 NotificationPermissionRequest,
140 OpenPanelParameters,
141 OpenPanelResultListener,
142 OriginDataManager,
143 Page,
144 PageConfiguration,
145 PageGroup,
146 ProcessPool,
147 ProcessPoolConfiguration,
148 PluginSiteDataManager,
149 Preferences,
150 RequestStorageAccessConfirmResultListener,
151 ResourceLoadStatisticsStore,
152 RunBeforeUnloadConfirmPanelResultListener,
153 RunJavaScriptAlertResultListener,
154 RunJavaScriptConfirmResultListener,
155 RunJavaScriptPromptResultListener,
156 TextChecker,
157 URLSchemeTask,
158 UserContentController,
159 UserContentWorld,
160 UserInitiatedAction,
161 UserMediaPermissionCheck,
162 UserMediaPermissionRequest,
163 ViewportAttributes,
164 VisitedLinkStore,
165 WebResourceLoadStatisticsManager,
166 WebsiteDataRecord,
167 WebsiteDataStore,
168 WebsiteDataStoreConfiguration,
169 WebsitePolicies,
170 WindowFeatures,
171
172#if ENABLE(MEDIA_SESSION)
173 MediaSessionFocusManager,
174 MediaSessionMetadata,
175#endif
176
177 // Bundle types
178 Bundle,
179 BundleBackForwardList,
180 BundleBackForwardListItem,
181 BundleCSSStyleDeclarationHandle,
182 BundleDOMWindowExtension,
183 BundleFrame,
184 BundleHitTestResult,
185 BundleInspector,
186 BundleNavigationAction,
187 BundleNodeHandle,
188 BundlePage,
189 BundlePageBanner,
190 BundlePageGroup,
191 BundlePageOverlay,
192 BundleRangeHandle,
193 BundleScriptWorld,
194
195 // Platform specific
196 EditCommandProxy,
197 ObjCObjectGraph,
198 View,
199#if USE(SOUP)
200 SoupRequestManager,
201 SoupCustomProtocolRequestManager,
202#endif
203 };
204
205 virtual ~Object()
206 {
207 }
208
209 virtual Type type() const = 0;
210
211#if DELEGATE_REF_COUNTING_TO_COCOA
212#ifdef __OBJC__
213 template<typename T, typename... Args>
214 static void constructInWrapper(NSObject <WKObject> *wrapper, Args&&... args)
215 {
216 Object* object = new (&wrapper._apiObject) T(std::forward<Args>(args)...);
217 object->m_wrapper = wrapper;
218 }
219#endif
220
221 NSObject *wrapper() const { return m_wrapper; }
222
223 void ref() const;
224 void deref() const;
225#endif // DELEGATE_REF_COUNTING_TO_COCOA
226
227 static void* wrap(API::Object*);
228 static API::Object* unwrap(void*);
229
230#if PLATFORM(COCOA) && defined(__OBJC__)
231 static API::Object& fromWKObjectExtraSpace(id <WKObject>);
232#endif
233
234protected:
235 Object();
236
237#if DELEGATE_REF_COUNTING_TO_COCOA
238 static void* newObject(size_t, Type);
239
240private:
241 // Derived classes must override operator new and call newObject().
242 void* operator new(size_t) = delete;
243
244 NSObject *m_wrapper;
245#endif // DELEGATE_REF_COUNTING_TO_COCOA
246};
247
248template <Object::Type ArgumentType>
249class ObjectImpl : public Object {
250public:
251 static const Type APIType = ArgumentType;
252
253 virtual ~ObjectImpl()
254 {
255 }
256
257protected:
258 friend class Object;
259
260 ObjectImpl()
261 {
262 }
263
264 Type type() const override { return APIType; }
265
266#if DELEGATE_REF_COUNTING_TO_COCOA
267 void* operator new(size_t size) { return newObject(size, APIType); }
268 void* operator new(size_t, void* value) { return value; }
269#endif
270};
271
272#if !DELEGATE_REF_COUNTING_TO_COCOA
273inline void* Object::wrap(API::Object* object)
274{
275 return static_cast<void*>(object);
276}
277
278inline API::Object* Object::unwrap(void* object)
279{
280 return static_cast<API::Object*>(object);
281}
282#endif
283
284} // namespace Object
285
286#undef DELEGATE_REF_COUNTING_TO_COCOA
287