From ae75e27948e370458aabc6e9941295336cbc4d0b Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 12 Jun 2024 19:30:35 +0100 Subject: [PATCH] Network: Remove code for compatibility with unsupported platforms --- modules/juce_core/native/juce_Network_mac.mm | 257 +------------------ 1 file changed, 1 insertion(+), 256 deletions(-) diff --git a/modules/juce_core/native/juce_Network_mac.mm b/modules/juce_core/native/juce_Network_mac.mm index 6b37d41579..6d338c8a1e 100644 --- a/modules/juce_core/native/juce_Network_mac.mm +++ b/modules/juce_core/native/juce_Network_mac.mm @@ -162,246 +162,6 @@ private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (URLConnectionStateBase) }; -#if JUCE_MAC -// This version is only used for backwards-compatibility with older OSX targets, -// so we'll turn off deprecation warnings. This code will be removed at some point -// in the future. -JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated") -class URLConnectionStatePreYosemite final : public URLConnectionStateBase -{ -public: - URLConnectionStatePreYosemite (NSURLRequest* req, const int maxRedirects) - : URLConnectionStateBase (req, maxRedirects) - { - static DelegateClass cls; - delegate = [cls.createInstance() init]; - DelegateClass::setState (delegate, this); - } - - ~URLConnectionStatePreYosemite() override - { - stop(); - - [connection release]; - [request release]; - [headers release]; - [delegate release]; - [data release]; - } - - bool start (WebInputStream& inputStream, WebInputStream::Listener* listener) override - { - startThread(); - - while (isThreadRunning() && ! initialised) - { - if (listener != nullptr) - if (! listener->postDataSendProgress (inputStream, (int) latestTotalBytes, (int) [[request HTTPBody] length])) - return false; - - Thread::sleep (1); - } - - return connection != nil && ! hasFailed; - } - - void stop() - { - { - const ScopedLock dLock (dataLock); - const ScopedLock connectionLock (createConnectionLock); - - hasBeenCancelled = true; - - if (connection != nil) - [connection cancel]; - } - - stopThread (10000); - } - - void cancel() override - { - hasFinished = hasFailed = true; - stop(); - } - - int read (char* dest, int numBytes) override - { - int numDone = 0; - - while (numBytes > 0) - { - const ScopedLock sl (dataLock); - auto available = jmin (numBytes, (int) [data length]); - - if (available > 0) - { - [data getBytes: dest length: (NSUInteger) available]; - [data replaceBytesInRange: NSMakeRange (0, (NSUInteger) available) withBytes: nil length: 0]; - - numDone += available; - numBytes -= available; - dest += available; - } - else - { - if (hasFailed || hasFinished) - break; - - const ScopedUnlock sul (dataLock); - Thread::sleep (1); - } - } - - return numDone; - } - - void didReceiveResponse (NSURLResponse* response) - { - { - const ScopedLock sl (dataLock); - [data setLength: 0]; - } - - contentLength = [response expectedContentLength]; - - [headers release]; - headers = nil; - - if ([response isKindOfClass: [NSHTTPURLResponse class]]) - { - NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response; - headers = [[httpResponse allHeaderFields] retain]; - statusCode = (int) [httpResponse statusCode]; - } - - initialised = true; - } - - NSURLRequest* willSendRequest (NSURLRequest* newRequest, NSURLResponse* redirectResponse) - { - if (redirectResponse != nullptr) - { - if (numRedirects >= numRedirectsToFollow) - return nil; // Cancel redirect and allow connection to continue - - ++numRedirects; - } - - return newRequest; - } - - void didFailWithError ([[maybe_unused]] NSError* error) - { - DBG (nsStringToJuce ([error description])); - nsUrlErrorCode = [error code]; - hasFailed = true; - initialised = true; - signalThreadShouldExit(); - } - - void didReceiveData (NSData* newData) - { - const ScopedLock sl (dataLock); - [data appendData: newData]; - initialised = true; - } - - void didSendBodyData (NSInteger totalBytesWritten, NSInteger /*totalBytesExpected*/) - { - latestTotalBytes = static_cast (totalBytesWritten); - } - - void finishedLoading() - { - hasFinished = true; - initialised = true; - signalThreadShouldExit(); - } - - void run() override - { - { - const ScopedLock lock (createConnectionLock); - - if (hasBeenCancelled) - return; - - connection = [[NSURLConnection alloc] initWithRequest: request - delegate: delegate]; - } - - while (! threadShouldExit()) - { - JUCE_AUTORELEASEPOOL - { - [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]]; - } - } - } - -private: - //============================================================================== - struct DelegateClass final : public ObjCClass - { - DelegateClass() : ObjCClass ("JUCENetworkDelegate_") - { - addIvar ("state"); - - addMethod (@selector (connection:didReceiveResponse:), didReceiveResponse); - addMethod (@selector (connection:didFailWithError:), didFailWithError); - addMethod (@selector (connection:didReceiveData:), didReceiveData); - addMethod (@selector (connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:), - connectionDidSendBodyData); - addMethod (@selector (connectionDidFinishLoading:), connectionDidFinishLoading); - addMethod (@selector (connection:willSendRequest:redirectResponse:), willSendRequest); - - registerClass(); - } - - static void setState (id self, URLConnectionStatePreYosemite* state) { object_setInstanceVariable (self, "state", state); } - static URLConnectionStatePreYosemite* getState (id self) { return getIvar (self, "state"); } - - private: - static void didReceiveResponse (id self, SEL, NSURLConnection*, NSURLResponse* response) - { - getState (self)->didReceiveResponse (response); - } - - static void didFailWithError (id self, SEL, NSURLConnection*, NSError* error) - { - getState (self)->didFailWithError (error); - } - - static void didReceiveData (id self, SEL, NSURLConnection*, NSData* newData) - { - getState (self)->didReceiveData (newData); - } - - static NSURLRequest* willSendRequest (id self, SEL, NSURLConnection*, NSURLRequest* request, NSURLResponse* response) - { - return getState (self)->willSendRequest (request, response); - } - - static void connectionDidSendBodyData (id self, SEL, NSURLConnection*, NSInteger, NSInteger totalBytesWritten, NSInteger totalBytesExpected) - { - getState (self)->didSendBodyData (totalBytesWritten, totalBytesExpected); - } - - static void connectionDidFinishLoading (id self, SEL, NSURLConnection*) - { - getState (self)->finishedLoading(); - } - }; - - NSURLConnection* connection = nil; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (URLConnectionStatePreYosemite) -}; -JUCE_END_IGNORE_WARNINGS_GCC_LIKE -#endif - //============================================================================== class API_AVAILABLE (macos (10.9)) URLConnectionState final : public URLConnectionStateBase { @@ -982,17 +742,7 @@ public: if (! connection->start (owner, webInputListener)) { - const auto errorCode = connection->getErrorCode(); connection.reset(); - - if (@available (macOS 10.10, *)) - return false; - - // Workaround for macOS versions below 10.10 where HTTPS POST requests with keep-alive - // fail with the NSURLErrorNetworkConnectionLost error code. - if (numRetries == 0 && errorCode == NSURLErrorNetworkConnectionLost) - return connect (webInputListener, ++numRetries); - return false; } @@ -1157,12 +907,7 @@ private: // Workaround for an Apple bug. See https://github.com/AFNetworking/AFNetworking/issues/2334 [req HTTPBody]; - if (@available (macOS 10.10, *)) - connection = std::make_unique (req, numRedirectsToFollow); - #if JUCE_MAC - else - connection = std::make_unique (req, numRedirectsToFollow); - #endif + connection = std::make_unique (req, numRedirectsToFollow); } } }