| /* | |
| * Copyright (C) 2005-2017 Apple Inc. All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions | |
| * are met: | |
| * | |
| * 1. Redistributions of source code must retain the above copyright | |
| * notice, this list of conditions and the following disclaimer. | |
| * 2. Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * 3. Neither the name of Apple Inc. ("Apple") nor the names of | |
| * its contributors may be used to endorse or promote products derived | |
| * from this software without specific prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| #import "WebResourceInternal.h" | |
| #import "WebFrameInternal.h" | |
| #import "WebKitLogging.h" | |
| #import "WebKitVersionChecks.h" | |
| #import "WebNSDictionaryExtras.h" | |
| #import "WebNSObjectExtras.h" | |
| #import "WebNSURLExtras.h" | |
| #import <JavaScriptCore/InitializeThreading.h> | |
| #import <WebCore/ArchiveResource.h> | |
| #import <WebCore/LegacyWebArchive.h> | |
| #import <WebCore/ThreadCheck.h> | |
| #import <WebCore/WebCoreJITOperations.h> | |
| #import <WebCore/WebCoreMainThread.h> | |
| #import <WebCore/WebCoreObjCExtras.h> | |
| #import <WebCore/WebCoreURLResponse.h> | |
| #import <pal/text/TextEncoding.h> | |
| #import <wtf/MainThread.h> | |
| #import <wtf/RefPtr.h> | |
| #import <wtf/RunLoop.h> | |
| #import <wtf/RuntimeApplicationChecks.h> | |
| #import <wtf/cocoa/TypeCastsCocoa.h> | |
| static NSString * const WebResourceDataKey = @"WebResourceData"; | |
| static NSString * const WebResourceFrameNameKey = @"WebResourceFrameName"; | |
| static NSString * const WebResourceMIMETypeKey = @"WebResourceMIMEType"; | |
| static NSString * const WebResourceURLKey = @"WebResourceURL"; | |
| static NSString * const WebResourceTextEncodingNameKey = @"WebResourceTextEncodingName"; | |
| static NSString * const WebResourceResponseKey = @"WebResourceResponse"; | |
| @interface WebResourcePrivate : NSObject { | |
| @public | |
| RefPtr<WebCore::ArchiveResource> coreResource; | |
| } | |
| - (instancetype)initWithCoreResource:(Ref<WebCore::ArchiveResource>&&)coreResource; | |
| @end | |
| @implementation WebResourcePrivate | |
| + (void)initialize | |
| { | |
| WebCore::initializeMainThreadIfNeeded(); | |
| } | |
| - (instancetype)init | |
| { | |
| self = [super init]; | |
| return self; | |
| } | |
| - (instancetype)initWithCoreResource:(Ref<WebCore::ArchiveResource>&&)passedResource | |
| { | |
| self = [super init]; | |
| if (!self) | |
| return nil; | |
| coreResource = WTF::move(passedResource); | |
| return self; | |
| } | |
| - (void)dealloc | |
| { | |
| if (WebCoreObjCScheduleDeallocateOnMainThread([WebResourcePrivate class], self)) | |
| return; | |
| [super dealloc]; | |
| } | |
| @end | |
| @implementation WebResource { | |
| RetainPtr<WebResourcePrivate> _private; | |
| } | |
| - (instancetype)init | |
| { | |
| self = [super init]; | |
| if (!self) | |
| return nil; | |
| _private = adoptNS([[WebResourcePrivate alloc] init]); | |
| return self; | |
| } | |
| - (instancetype)initWithData:(NSData *)data URL:(NSURL *)URL MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName frameName:(NSString *)frameName | |
| { | |
| return [self _initWithData:data URL:URL MIMEType:MIMEType textEncodingName:textEncodingName frameName:frameName response:nil copyData:YES]; | |
| } | |
| - (instancetype)initWithCoder:(NSCoder *)decoder | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| self = [super init]; | |
| if (!self) | |
| return nil; | |
| RetainPtr<NSData> data; | |
| RetainPtr<NSURL> url; | |
| RetainPtr<NSString> mimeType; | |
| RetainPtr<NSString> textEncoding; | |
| RetainPtr<NSString> frameName; | |
| RetainPtr<NSURLResponse> response; | |
| @try { | |
| data = dynamic_objc_cast<NSData>([decoder decodeObjectForKey:WebResourceDataKey]); | |
| url = dynamic_objc_cast<NSURL>([decoder decodeObjectForKey:WebResourceURLKey]); | |
| mimeType = dynamic_objc_cast<NSString>([decoder decodeObjectForKey:WebResourceMIMETypeKey]); | |
| textEncoding = dynamic_objc_cast<NSString>([decoder decodeObjectForKey:WebResourceTextEncodingNameKey]); | |
| frameName = dynamic_objc_cast<NSString>([decoder decodeObjectForKey:WebResourceFrameNameKey]); | |
| response = dynamic_objc_cast<NSURLResponse>([decoder decodeObjectForKey:WebResourceResponseKey]); | |
| } @catch(id) { | |
| [self release]; | |
| return nil; | |
| } | |
| auto coreResource = WebCore::ArchiveResource::create(WebCore::SharedBuffer::create(data.get()), url.get(), mimeType.get(), textEncoding.get(), frameName.get(), response.get()); | |
| if (!coreResource) { | |
| [self release]; | |
| return nil; | |
| } | |
| _private = adoptNS([[WebResourcePrivate alloc] initWithCoreResource:coreResource.releaseNonNull()]); | |
| return self; | |
| } | |
| - (void)encodeWithCoder:(NSCoder *)encoder | |
| { | |
| RefPtr resource = _private->coreResource; | |
| RetainPtr<NSData> data; | |
| RetainPtr<NSURL> url; | |
| RetainPtr<NSString> mimeType; | |
| RetainPtr<NSString> textEncoding; | |
| RetainPtr<NSString> frameName; | |
| RetainPtr<NSURLResponse> response; | |
| if (resource) { | |
| data = protect(resource->data())->makeContiguous()->createNSData(); | |
| url = resource->url().createNSURL(); | |
| mimeType = resource->mimeType().createNSString(); | |
| textEncoding = resource->textEncoding().createNSString(); | |
| frameName = resource->frameName().createNSString(); | |
| response = resource->response().nsURLResponse(); | |
| } | |
| [encoder encodeObject:data.get() forKey:WebResourceDataKey]; | |
| [encoder encodeObject:url.get() forKey:WebResourceURLKey]; | |
| [encoder encodeObject:mimeType.get() forKey:WebResourceMIMETypeKey]; | |
| [encoder encodeObject:textEncoding.get() forKey:WebResourceTextEncodingNameKey]; | |
| [encoder encodeObject:frameName.get() forKey:WebResourceFrameNameKey]; | |
| [encoder encodeObject:response.get() forKey:WebResourceResponseKey]; | |
| } | |
| - (id)copyWithZone:(NSZone *)zone | |
| { | |
| return [self retain]; | |
| } | |
| - (NSData *)data | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return protect(protect(_private->coreResource)->data())->makeContiguous()->createNSData().autorelease(); | |
| } | |
| - (NSURL *)URL | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return _private->coreResource->url().createNSURL().autorelease(); | |
| } | |
| - (NSString *)MIMEType | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return _private->coreResource->mimeType().createNSString().autorelease(); | |
| } | |
| - (NSString *)textEncodingName | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return _private->coreResource->textEncoding().createNSString().autorelease(); | |
| } | |
| - (NSString *)frameName | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return _private->coreResource->frameName().createNSString().autorelease(); | |
| } | |
| - (NSString *)description | |
| { | |
| return [NSString stringWithFormat:@"<%@ %@>", NSStringFromClass(self.class), self.URL]; | |
| } | |
| @end | |
| @implementation WebResource (WebResourceInternal) | |
| - (id)_initWithCoreResource:(Ref<WebCore::ArchiveResource>&&)coreResource | |
| { | |
| self = [super init]; | |
| if (!self) | |
| return nil; | |
| _private = adoptNS([[WebResourcePrivate alloc] initWithCoreResource:WTF::move(coreResource)]); | |
| return self; | |
| } | |
| - (std::reference_wrapper<WebCore::ArchiveResource>)_coreResource | |
| { | |
| return *_private->coreResource; | |
| } | |
| @end | |
| @implementation WebResource (WebResourcePrivate) | |
| // SPI for Mail (5066325) | |
| // FIXME: This "ignoreWhenUnarchiving" concept is an ugly one - can we find a cleaner solution for those who need this SPI? | |
| - (void)_ignoreWhenUnarchiving | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return; | |
| _private->coreResource->ignoreWhenUnarchiving(); | |
| } | |
| - (id)_initWithData:(NSData *)data | |
| URL:(NSURL *)URL | |
| MIMEType:(NSString *)MIMEType | |
| textEncodingName:(NSString *)textEncodingName | |
| frameName:(NSString *)frameName | |
| response:(NSURLResponse *)response | |
| copyData:(BOOL)copyData | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| self = [super init]; | |
| if (!self) | |
| return nil; | |
| if (!data || !URL || !MIMEType) { | |
| [self release]; | |
| return nil; | |
| } | |
| auto coreResource = WebCore::ArchiveResource::create(WebCore::SharedBuffer::create(copyData ? checked_objc_cast<NSData>(adoptNS([data copy]).get()) : data), URL, MIMEType, textEncodingName, frameName, response); | |
| if (!coreResource) { | |
| [self release]; | |
| return nil; | |
| } | |
| _private = adoptNS([[WebResourcePrivate alloc] initWithCoreResource:coreResource.releaseNonNull()]); | |
| return self; | |
| } | |
| - (id)_initWithData:(NSData *)data URL:(NSURL *)URL response:(NSURLResponse *)response | |
| { | |
| // Pass NO for copyData since the data doesn't need to be copied since we know that callers will no longer modify it. | |
| // Copying it will also cause a performance regression. | |
| return [self _initWithData:data | |
| URL:URL | |
| MIMEType:[response MIMEType] | |
| textEncodingName:[response textEncodingName] | |
| frameName:nil | |
| response:response | |
| copyData:NO]; | |
| } | |
| - (NSString *)_suggestedFilename | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| if (!_private->coreResource) | |
| return nil; | |
| return _private->coreResource->response().suggestedFilename().createNSString().autorelease(); | |
| } | |
| #if !PLATFORM(IOS_FAMILY) | |
| - (NSFileWrapper *)_fileWrapperRepresentation | |
| { | |
| auto wrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:[self data]]); | |
| NSString *filename = [self _suggestedFilename]; | |
| if (!filename || ![filename length]) | |
| filename = [[self URL] _webkit_suggestedFilenameWithMIMEType:[self MIMEType]]; | |
| [wrapper setPreferredFilename:filename]; | |
| return wrapper.autorelease(); | |
| } | |
| #endif | |
| - (NSURLResponse *)_response | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| RetainPtr<NSURLResponse> response; | |
| if (_private->coreResource) | |
| response = _private->coreResource->response().nsURLResponse(); | |
| return response ? response.autorelease() : adoptNS([[NSURLResponse alloc] init]).autorelease(); | |
| } | |
| - (NSString *)_stringValue | |
| { | |
| WebCoreThreadViolationCheckRoundTwo(); | |
| PAL::TextEncoding encoding; | |
| if (_private->coreResource) | |
| encoding = _private->coreResource->textEncoding(); | |
| if (!encoding.isValid()) | |
| encoding = PAL::WindowsLatin1Encoding(); | |
| RefPtr coreData = _private->coreResource ? &protect(_private->coreResource)->data() : nullptr; | |
| if (!coreData) | |
| return @""; | |
| return encoding.decode(coreData->makeContiguous()->span()).createNSString().autorelease(); | |
| } | |
| @end |