CrashTracer beneath JSC::MarkedBlock::specializedSweep
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / cf / WebCoreAVCFResourceLoader.cpp
1 /*
2  * Copyright (C) 2014 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 #include "config.h"
27 #include "WebCoreAVCFResourceLoader.h"
28
29 #if ENABLE(VIDEO) && USE(AVFOUNDATION) && HAVE(AVFOUNDATION_LOADER_DELEGATE)
30
31 #include "CachedRawResource.h"
32 #include "CachedResourceLoader.h"
33 #include "CachedResourceRequest.h"
34 #include "MediaPlayerPrivateAVFoundationCF.h"
35 #include "NotImplemented.h"
36 #include "ResourceLoaderOptions.h"
37 #include "SharedBuffer.h"
38 #include "SoftLinking.h"
39 #include <AVFoundationCF/AVFoundationCF.h>
40 #include <AVFoundationCF/AVCFAssetResourceLoader.h>
41 #include <wtf/text/CString.h>
42
43 // The softlink header files must be included after the AVCF and CoreMedia header files.
44 #include "AVFoundationCFSoftLinking.h"
45
46 namespace WebCore {
47
48 PassRefPtr<WebCoreAVCFResourceLoader> WebCoreAVCFResourceLoader::create(MediaPlayerPrivateAVFoundationCF* parent, AVCFAssetResourceLoadingRequestRef avRequest)
49 {
50     ASSERT(avRequest);
51     ASSERT(parent);
52     return adoptRef(new WebCoreAVCFResourceLoader(parent, avRequest));
53 }
54
55 WebCoreAVCFResourceLoader::WebCoreAVCFResourceLoader(MediaPlayerPrivateAVFoundationCF* parent, AVCFAssetResourceLoadingRequestRef avRequest)
56     : m_parent(parent)
57     , m_avRequest(avRequest)
58 {
59 }
60
61 WebCoreAVCFResourceLoader::~WebCoreAVCFResourceLoader()
62 {
63     stopLoading();
64 }
65
66 void WebCoreAVCFResourceLoader::startLoading()
67 {
68     if (m_resource || !m_parent)
69         return;
70
71     RetainPtr<CFURLRequestRef> urlRequest = AVCFAssetResourceLoadingRequestGetURLRequest(m_avRequest.get());
72
73     // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
74     CachedResourceRequest request(ResourceRequest(urlRequest.get()), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, ClientDidNotRequestCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
75
76     request.mutableResourceRequest().setPriority(ResourceLoadPriority::Low);
77     CachedResourceLoader* loader = m_parent->player()->cachedResourceLoader();
78     m_resource = loader ? loader->requestRawResource(request) : 0;
79     if (m_resource)
80         m_resource->addClient(this);
81     else {
82         LOG_ERROR("Failed to start load for media at url %s", URL(CFURLRequestGetURL(urlRequest.get())).string().ascii().data());
83         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
84         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
85     }
86 }
87
88 void WebCoreAVCFResourceLoader::stopLoading()
89 {
90     if (!m_resource)
91         return;
92
93     m_resource->removeClient(this);
94     m_resource = 0;
95
96     if (m_parent)
97         m_parent->didStopLoadingRequest(m_avRequest.get());
98 }
99
100 void WebCoreAVCFResourceLoader::invalidate()
101 {
102     if (!m_parent)
103         return;
104
105     m_parent = nullptr;
106
107     callOnMainThread([protectedThis = Ref<WebCoreAVCFResourceLoader>(*this)] () mutable {
108         protectedThis->stopLoading();
109     });
110 }
111
112 void WebCoreAVCFResourceLoader::responseReceived(CachedResource* resource, const ResourceResponse& response)
113 {
114     ASSERT(resource == m_resource);
115     UNUSED_PARAM(resource);
116
117     int status = response.httpStatusCode();
118     if (status && (status < 200 || status > 299)) {
119         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, status, nullptr));
120         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
121         return;
122     }
123
124     notImplemented();
125 }
126
127 void WebCoreAVCFResourceLoader::dataReceived(CachedResource* resource, const char*, int)
128 {
129     fulfillRequestWithResource(resource);
130 }
131
132 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource* resource)
133 {
134     if (resource->loadFailedOrCanceled()) {
135         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
136         // string to trigger AVAsset's playable value to complete loading.
137         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
138         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
139         notImplemented();
140
141         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
142         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
143     } else {
144         fulfillRequestWithResource(resource);
145         // FIXME: [m_avRequest.get() finishLoading];
146         notImplemented();
147     }
148     stopLoading();
149 }
150
151 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource* resource)
152 {
153     ASSERT(resource == m_resource);
154     notImplemented();
155 }
156
157 }
158
159 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)