Shrink various loading-related enums to shrink CachedResource
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / cf / WebCoreAVCFResourceLoader.cpp
1 /*
2  * Copyright (C) 2014-2017 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 <AVFoundationCF/AVCFAssetResourceLoader.h>
39 #include <AVFoundationCF/AVFoundationCF.h>
40 #include <wtf/SoftLinking.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 Ref<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     ResourceRequest resourceRequest(urlRequest.get());
74     resourceRequest.setPriority(ResourceLoadPriority::Low);
75
76     // ContentSecurityPolicyImposition::DoPolicyCheck is a placeholder value. It does not affect the request since Content Security Policy does not apply to raw resources.
77     CachedResourceRequest request(WTFMove(resourceRequest), ResourceLoaderOptions(
78         SendCallbackPolicy::SendCallbacks,
79         ContentSniffingPolicy::DoNotSniffContent,
80         DataBufferingPolicy::BufferData,
81         StoredCredentialsPolicy::DoNotUse,
82         ClientCredentialPolicy::CannotAskClientForCredentials,
83         FetchOptions::Credentials::Omit,
84         SecurityCheckPolicy::DoSecurityCheck,
85         FetchOptions::Mode::NoCors,
86         CertificateInfoPolicy::DoNotIncludeCertificateInfo,
87         ContentSecurityPolicyImposition::DoPolicyCheck,
88         DefersLoadingPolicy::AllowDefersLoading,
89         CachingPolicy::DisallowCaching));
90
91     CachedResourceLoader* loader = m_parent->player()->cachedResourceLoader();
92     m_resource = loader ? loader->requestRawResource(WTFMove(request)).value_or(nullptr) : nullptr;
93     if (m_resource)
94         m_resource->addClient(*this);
95     else {
96         LOG_ERROR("Failed to start load for media at url %s", URL(CFURLRequestGetURL(urlRequest.get())).string().ascii().data());
97         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
98         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
99     }
100 }
101
102 void WebCoreAVCFResourceLoader::stopLoading()
103 {
104     if (!m_resource)
105         return;
106
107     m_resource->removeClient(*this);
108     m_resource = 0;
109
110     if (m_parent)
111         m_parent->didStopLoadingRequest(m_avRequest.get());
112 }
113
114 void WebCoreAVCFResourceLoader::invalidate()
115 {
116     if (!m_parent)
117         return;
118
119     m_parent = nullptr;
120
121     callOnMainThread([protectedThis = makeRef(*this)] () mutable {
122         protectedThis->stopLoading();
123     });
124 }
125
126 void WebCoreAVCFResourceLoader::responseReceived(CachedResource& resource, const ResourceResponse& response, CompletionHandler<void()>&& completionHandler)
127 {
128     ASSERT_UNUSED(resource, &resource == m_resource);
129     CompletionHandlerCallingScope completionHandlerCaller(WTFMove(completionHandler));
130
131     int status = response.httpStatusCode();
132     if (status && (status < 200 || status > 299)) {
133         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, status, nullptr));
134         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
135         return;
136     }
137
138     notImplemented();
139 }
140
141 void WebCoreAVCFResourceLoader::dataReceived(CachedResource& resource, const char*, int)
142 {
143     fulfillRequestWithResource(resource);
144 }
145
146 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource& resource)
147 {
148     if (resource.loadFailedOrCanceled()) {
149         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
150         // string to trigger AVAsset's playable value to complete loading.
151         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
152         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
153         notImplemented();
154
155         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
156         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
157     } else {
158         fulfillRequestWithResource(resource);
159         // FIXME: [m_avRequest.get() finishLoading];
160         notImplemented();
161     }
162     stopLoading();
163 }
164
165 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource& resource)
166 {
167     ASSERT_UNUSED(resource, &resource == m_resource);
168     notImplemented();
169 }
170
171 }
172
173 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)