Make C SPI objects and modern API objects toll-free bridged
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Feb 2015 21:25:08 +0000 (21:25 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Feb 2015 21:25:08 +0000 (21:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=141808

Reviewed by Tim Horton.

* Shared/API/APIObject.h:
(API::Object::wrap):
(API::Object::unwrap):
Add default implementations of wrap and unwrap. These just cast.

* Shared/API/c/WKArray.cpp:
(WKArrayCreate):
(WKArrayCreateAdoptingValues):
(WKArrayGetItemAtIndex):
* Shared/API/c/WKDictionary.cpp:
(WKDictionaryGetItemForKey):
Add toImpl and toAPI where necessary.

* Shared/API/c/WKSharedAPICast.h:
Add API::Object::wrap in toAPI and unwrap in toImpl.

* Shared/API/c/WKType.cpp:
(WKGetTypeID):
(WKRetain):
(WKRelease):
Add missing toImpl calls.

* Shared/Cocoa/APIObject.mm:
(API::Object::wrap):
Return the APIObject Objective-C wrapper.

(API::Object::unwrap):
Return the API::Object from the Objective-C object.

* Shared/Cocoa/WKObject.h:
Add wrap and unwrap declarations.

* UIProcess/API/C/WKPage.cpp:
* UIProcess/API/C/WKPageConfigurationRef.cpp:
* UIProcess/API/C/WKPageGroup.cpp:
* UIProcess/API/C/WKUserContentControllerRef.cpp:
Add missing includes; toImpl now requires a complete type.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@180362 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
Source/WebKit2/ChangeLog
Source/WebKit2/Shared/API/APIObject.h
Source/WebKit2/Shared/API/c/WKArray.cpp
Source/WebKit2/Shared/API/c/WKDictionary.cpp
Source/WebKit2/Shared/API/c/WKSharedAPICast.h
Source/WebKit2/Shared/API/c/WKType.cpp
Source/WebKit2/Shared/Cocoa/APIObject.mm
Source/WebKit2/Shared/Cocoa/WKObject.h
Source/WebKit2/UIProcess/API/C/WKPage.cpp
Source/WebKit2/UIProcess/API/C/WKPageConfigurationRef.cpp
Source/WebKit2/UIProcess/API/C/WKPageGroup.cpp
Source/WebKit2/UIProcess/API/C/WKUserContentControllerRef.cpp
Source/WebKit2/UIProcess/API/ios/WKViewIOS.mm

index 6198d39..c279f6e 100644 (file)
@@ -1,3 +1,48 @@
+2015-02-19  Anders Carlsson  <andersca@apple.com>
+
+        Make C SPI objects and modern API objects toll-free bridged
+        https://bugs.webkit.org/show_bug.cgi?id=141808
+
+        Reviewed by Tim Horton.
+
+        * Shared/API/APIObject.h:
+        (API::Object::wrap):
+        (API::Object::unwrap):
+        Add default implementations of wrap and unwrap. These just cast.
+
+        * Shared/API/c/WKArray.cpp:
+        (WKArrayCreate):
+        (WKArrayCreateAdoptingValues):
+        (WKArrayGetItemAtIndex):
+        * Shared/API/c/WKDictionary.cpp:
+        (WKDictionaryGetItemForKey):
+        Add toImpl and toAPI where necessary.
+
+        * Shared/API/c/WKSharedAPICast.h:
+        Add API::Object::wrap in toAPI and unwrap in toImpl.
+
+        * Shared/API/c/WKType.cpp:
+        (WKGetTypeID):
+        (WKRetain):
+        (WKRelease):
+        Add missing toImpl calls.
+
+        * Shared/Cocoa/APIObject.mm:
+        (API::Object::wrap):
+        Return the APIObject Objective-C wrapper.
+
+        (API::Object::unwrap):
+        Return the API::Object from the Objective-C object.
+
+        * Shared/Cocoa/WKObject.h:
+        Add wrap and unwrap declarations.
+
+        * UIProcess/API/C/WKPage.cpp:
+        * UIProcess/API/C/WKPageConfigurationRef.cpp:
+        * UIProcess/API/C/WKPageGroup.cpp:
+        * UIProcess/API/C/WKUserContentControllerRef.cpp:
+        Add missing includes; toImpl now requires a complete type.
+
 2015-02-19  Timothy Horton  <timothy_horton@apple.com>
 
         Remove unused forward declaration from WKWebViewInternal.h
index ea4cd1a..a2293ea 100644 (file)
@@ -205,6 +205,9 @@ public:
     void deref();
 #endif // DELEGATE_REF_COUNTING_TO_COCOA
 
+    static void* wrap(API::Object*);
+    static API::Object* unwrap(void*);
+
 protected:
     Object();
 
@@ -243,6 +246,18 @@ protected:
 #endif
 };
 
+#if !DELEGATE_REF_COUNTING_TO_COCOA
+inline void* Object::wrap(API::Object* object)
+{
+    return static_cast<void*>(object);
+}
+
+inline API::Object* Object::unwrap(void* object)
+{
+    return static_cast<API::Object*>(object);
+}
+#endif
+
 } // namespace Object
 
 #undef DELEGATE_REF_COUNTING_TO_COCOA
index 8fb0fcf..fb2db0e 100644 (file)
@@ -42,7 +42,7 @@ WKArrayRef WKArrayCreate(WKTypeRef* values, size_t numberOfValues)
     elements.reserveInitialCapacity(numberOfValues);
 
     for (size_t i = 0; i < numberOfValues; ++i)
-        elements.uncheckedAppend(const_cast<API::Object*>(static_cast<const API::Object*>(values[i])));
+        elements.uncheckedAppend(toImpl(values[i]));
 
     return toAPI(API::Array::create(WTF::move(elements)).leakRef());
 }
@@ -53,14 +53,14 @@ WKArrayRef WKArrayCreateAdoptingValues(WKTypeRef* values, size_t numberOfValues)
     elements.reserveInitialCapacity(numberOfValues);
 
     for (size_t i = 0; i < numberOfValues; ++i)
-        elements.uncheckedAppend(adoptRef(const_cast<API::Object*>(static_cast<const API::Object*>(values[i]))));
+        elements.uncheckedAppend(adoptRef(toImpl(values[i])));
 
     return toAPI(API::Array::create(WTF::move(elements)).leakRef());
 }
 
 WKTypeRef WKArrayGetItemAtIndex(WKArrayRef arrayRef, size_t index)
 {
-    return toImpl(arrayRef)->at(index);
+    return toAPI(toImpl(arrayRef)->at(index));
 }
 
 size_t WKArrayGetSize(WKArrayRef arrayRef)
index d5f182f..4f3a60d 100644 (file)
@@ -48,7 +48,7 @@ WK_EXPORT WKDictionaryRef WKDictionaryCreate(const WKStringRef* keys, const WKTy
 
 WKTypeRef WKDictionaryGetItemForKey(WKDictionaryRef dictionaryRef, WKStringRef key)
 {
-    return toImpl(dictionaryRef)->get(toImpl(key)->string());
+    return toAPI(toImpl(dictionaryRef)->get(toImpl(key)->string()));
 }
 
 size_t WKDictionaryGetSize(WKDictionaryRef dictionaryRef)
index 16a3c53..940d726 100644 (file)
@@ -128,13 +128,13 @@ WK_ADD_API_MAPPING(WKObjCTypeWrapperRef, ObjCObjectGraph)
 template<typename T, typename APIType = typename ImplTypeInfo<T>::APIType>
 auto toAPI(T* t) -> APIType
 {
-    return reinterpret_cast<APIType>(t);
+    return reinterpret_cast<APIType>(API::Object::wrap(t));
 }
 
 template<typename T, typename ImplType = typename APITypeInfo<T>::ImplType>
 auto toImpl(T t) -> ImplType*
 {
-    return static_cast<ImplType*>(static_cast<void*>(const_cast<typename std::remove_const<typename std::remove_pointer<T>::type>::type*>(t)));
+    return static_cast<ImplType*>(API::Object::unwrap(static_cast<void*>(const_cast<typename std::remove_const<typename std::remove_pointer<T>::type>::type*>(t))));
 }
 
 template<typename ImplType, typename APIType = typename ImplTypeInfo<ImplType>::APIType>
index bb165e4..fd20e6f 100644 (file)
@@ -33,16 +33,17 @@ using namespace WebKit;
 
 WKTypeID WKGetTypeID(WKTypeRef typeRef)
 {
-    return toAPI(static_cast<API::Object*>(const_cast<void*>(typeRef))->type());
+    return toAPI(toImpl(typeRef)->type());
 }
 
 WKTypeRef WKRetain(WKTypeRef typeRef)
 {
-    static_cast<API::Object*>(const_cast<void*>(typeRef))->ref();
+    toImpl(typeRef)->ref();
+
     return typeRef;
 }
 
 void WKRelease(WKTypeRef typeRef)
 {
-    static_cast<API::Object*>(const_cast<void*>(typeRef))->deref();
+    toImpl(typeRef)->deref();
 }
index 7980107..8bf1a34 100644 (file)
@@ -233,6 +233,25 @@ void* Object::newObject(size_t size, Type type)
     return &object;
 }
 
+void* Object::wrap(API::Object* object)
+{
+    if (!object)
+        return nullptr;
+
+    return static_cast<void*>(object->wrapper());
+}
+
+API::Object* Object::unwrap(void* object)
+{
+    if (!object)
+        return nullptr;
+
+    ASSERT([(id)object conformsToProtocol:@protocol(WKObject)]);
+    ASSERT([(id)object respondsToSelector:@selector(_apiObject)]);
+
+    return &static_cast<id <WKObject>>(object)._apiObject;
+}
+
 } // namespace API
 
 #endif // WK_API_ENABLED
index 1f042a4..fc95ed8 100644 (file)
@@ -41,6 +41,9 @@ struct ObjectStorage {
     typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type data;
 };
 
+API::Object* unwrap(void*);
+void* wrap(API::Object*);
+
 }
 
 @protocol WKObject <NSObject>
index 4843407..5d6ae88 100644 (file)
@@ -34,7 +34,9 @@
 #include "APIFindClient.h"
 #include "APIFrameInfo.h"
 #include "APILoaderClient.h"
+#include "APINavigationAction.h"
 #include "APINavigationClient.h"
+#include "APINavigationResponse.h"
 #include "APIPolicyClient.h"
 #include "APISessionState.h"
 #include "APIUIClient.h"
index 4b9ada5..615f527 100644 (file)
@@ -28,6 +28,9 @@
 
 #include "APIPageConfiguration.h"
 #include "WKAPICast.h"
+#include "WebPageGroup.h"
+#include "WebProcessPool.h"
+#include "WebUserContentControllerProxy.h"
 
 using namespace WebKit;
 
index dad6ed1..bbcef97 100644 (file)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "WKPageGroup.h"
 
+#include "APIUserContentFilter.h"
 #include "WKAPICast.h"
 #include "WebPageGroup.h"
 #include "WebPreferences.h"
index 0a9b4d7..0cfa2f8 100644 (file)
@@ -26,6 +26,8 @@
 #include "config.h"
 #include "WKUserContentControllerRef.h"
 
+#include "APIUserContentFilter.h"
+#include "APIUserScript.h"
 #include "WKAPICast.h"
 #include "WebUserContentControllerProxy.h"
 
index 9756bbd..34fb80c 100644 (file)
@@ -36,6 +36,7 @@
 #import "WKContentView.h"
 #import "WKProcessGroupPrivate.h"
 #import "WKScrollView.h"
+#import "WebPageGroup.h"
 #import "WebPageProxy.h"
 #import "WebProcessPool.h"
 #import <UIKit/UIScreen.h>