|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 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. ``AS IS'' AND ANY |
| 14 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include "APISerializedScriptValue.h" |
| 28 | + |
| 29 | +#include "WKMutableArray.h" |
| 30 | +#include "WKMutableDictionary.h" |
| 31 | +#include "WKNumber.h" |
| 32 | +#include "WKString.h" |
| 33 | +#include <JavaScriptCore/JSRemoteInspector.h> |
| 34 | +#include <JavaScriptCore/JSRetainPtr.h> |
| 35 | + |
| 36 | +namespace API { |
| 37 | + |
| 38 | +static constexpr auto SharedJSContextWKMaxIdleTime = 10_s; |
| 39 | + |
| 40 | +class SharedJSContextWK { |
| 41 | +public: |
| 42 | + SharedJSContextWK() |
| 43 | + : m_timer(RunLoop::main(), this, &SharedJSContextWK::releaseContextIfNecessary) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + JSRetainPtr<JSGlobalContextRef> ensureContext() |
| 48 | + { |
| 49 | + m_lastUseTime = MonotonicTime::now(); |
| 50 | + if (!m_context) { |
| 51 | + bool inspectionPreviouslyFollowedInternalPolicies = JSRemoteInspectorGetInspectionFollowsInternalPolicies(); |
| 52 | + JSRemoteInspectorSetInspectionFollowsInternalPolicies(false); |
| 53 | + |
| 54 | + // FIXME: rdar://100738357 Remote Web Inspector: Remove use of JSRemoteInspectorGetInspectionEnabledByDefault |
| 55 | + // and JSRemoteInspectorSetInspectionEnabledByDefault once the default state is always false. |
| 56 | + ALLOW_DEPRECATED_DECLARATIONS_BEGIN |
| 57 | + bool previous = JSRemoteInspectorGetInspectionEnabledByDefault(); |
| 58 | + JSRemoteInspectorSetInspectionEnabledByDefault(false); |
| 59 | + m_context = adopt(JSGlobalContextCreate(nullptr)); |
| 60 | + JSRemoteInspectorSetInspectionEnabledByDefault(previous); |
| 61 | + ALLOW_DEPRECATED_DECLARATIONS_END |
| 62 | + |
| 63 | + JSRemoteInspectorSetInspectionFollowsInternalPolicies(inspectionPreviouslyFollowedInternalPolicies); |
| 64 | + |
| 65 | + m_timer.startOneShot(SharedJSContextWKMaxIdleTime); |
| 66 | + } |
| 67 | + return m_context; |
| 68 | + } |
| 69 | + |
| 70 | + void releaseContextIfNecessary() |
| 71 | + { |
| 72 | + auto idleTime = MonotonicTime::now() - m_lastUseTime; |
| 73 | + if (idleTime < SharedJSContextWKMaxIdleTime) { |
| 74 | + // We lazily restart the timer if needed every 10 seconds instead of doing so every time ensureContext() |
| 75 | + // is called, for performance reasons. |
| 76 | + m_timer.startOneShot(SharedJSContextWKMaxIdleTime - idleTime); |
| 77 | + return; |
| 78 | + } |
| 79 | + m_context.clear(); |
| 80 | + } |
| 81 | + |
| 82 | +private: |
| 83 | + JSRetainPtr<JSGlobalContextRef> m_context; |
| 84 | + RunLoop::Timer m_timer; |
| 85 | + MonotonicTime m_lastUseTime; |
| 86 | +}; |
| 87 | + |
| 88 | +static SharedJSContextWK& sharedContext() |
| 89 | +{ |
| 90 | + static MainThreadNeverDestroyed<SharedJSContextWK> sharedContext; |
| 91 | + return sharedContext.get(); |
| 92 | +} |
| 93 | + |
| 94 | +static WKRetainPtr<WKTypeRef> valueToWKObject(JSContextRef context, JSValueRef value) |
| 95 | +{ |
| 96 | + auto jsToWKString = [] (JSStringRef input) { |
| 97 | + size_t bufferSize = JSStringGetMaximumUTF8CStringSize(input); |
| 98 | + Vector<char> buffer(bufferSize); |
| 99 | + size_t utf8Length = JSStringGetUTF8CString(input, buffer.data(), bufferSize); |
| 100 | + ASSERT(buffer[utf8Length] == '\0'); |
| 101 | + return adoptWK(WKStringCreateWithUTF8CStringWithLength(buffer.data(), utf8Length - 1)); |
| 102 | + }; |
| 103 | + |
| 104 | + if (!JSValueIsObject(context, value)) { |
| 105 | + if (JSValueIsBoolean(context, value)) |
| 106 | + return adoptWK(WKBooleanCreate(JSValueToBoolean(context, value))); |
| 107 | + if (JSValueIsNumber(context, value)) |
| 108 | + return adoptWK(WKDoubleCreate(JSValueToNumber(context, value, nullptr))); |
| 109 | + if (JSValueIsString(context, value)) { |
| 110 | + JSStringRef jsString = JSValueToStringCopy(context, value, nullptr); |
| 111 | + WKRetainPtr result = jsToWKString(jsString); |
| 112 | + JSStringRelease(jsString); |
| 113 | + return result; |
| 114 | + } |
| 115 | + return nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + JSObjectRef object = JSValueToObject(context, value, nullptr); |
| 119 | + |
| 120 | + if (JSValueIsArray(context, value)) { |
| 121 | + JSStringRef jsString = JSStringCreateWithUTF8CString("length"); |
| 122 | + JSValueRef lengthPropertyName = JSValueMakeString(context, jsString); |
| 123 | + JSStringRelease(jsString); |
| 124 | + JSValueRef lengthValue = JSObjectGetPropertyForKey(context, object, lengthPropertyName, nullptr); |
| 125 | + double lengthDouble = JSValueToNumber(context, lengthValue, nullptr); |
| 126 | + if (lengthDouble < 0 || lengthDouble > static_cast<double>(std::numeric_limits<size_t>::max())) |
| 127 | + return nullptr; |
| 128 | + size_t length = lengthDouble; |
| 129 | + WKRetainPtr result = adoptWK(WKMutableArrayCreateWithCapacity(length)); |
| 130 | + for (size_t i = 0; i < length; ++i) |
| 131 | + WKArrayAppendItem(result.get(), valueToWKObject(context, JSObjectGetPropertyAtIndex(context, object, i, nullptr)).get()); |
| 132 | + return result; |
| 133 | + } |
| 134 | + |
| 135 | + JSPropertyNameArrayRef names = JSObjectCopyPropertyNames(context, object); |
| 136 | + size_t length = JSPropertyNameArrayGetCount(names); |
| 137 | + auto result = adoptWK(WKMutableDictionaryCreateWithCapacity(length)); |
| 138 | + for (size_t i = 0; i < length; i++) { |
| 139 | + JSStringRef jsKey = JSPropertyNameArrayGetNameAtIndex(names, i); |
| 140 | + WKRetainPtr key = jsToWKString(jsKey); |
| 141 | + WKRetainPtr value = valueToWKObject(context, JSObjectGetPropertyForKey(context, object, JSValueMakeString(context, jsKey), nullptr)); |
| 142 | + WKDictionarySetItem(result.get(), key.get(), value.get()); |
| 143 | + } |
| 144 | + JSPropertyNameArrayRelease(names); |
| 145 | + |
| 146 | + return result; |
| 147 | +} |
| 148 | + |
| 149 | +WKRetainPtr<WKTypeRef> SerializedScriptValue::deserializeWK(WebCore::SerializedScriptValue& serializedScriptValue) |
| 150 | +{ |
| 151 | + ASSERT(RunLoop::isMain()); |
| 152 | + JSRetainPtr context = sharedContext().ensureContext(); |
| 153 | + ASSERT(context); |
| 154 | + |
| 155 | + JSValueRef value = serializedScriptValue.deserialize(context.get(), nullptr); |
| 156 | + if (!value) |
| 157 | + return nullptr; |
| 158 | + |
| 159 | + return valueToWKObject(context.get(), value); |
| 160 | +} |
| 161 | + |
| 162 | +} // API |
0 commit comments