Update CachedResourceLoader::requestResource() to return a WTF::Expected
[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 <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(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions::Credentials::Omit, DoSecurityCheck, FetchOptions::Mode::NoCors, DoNotIncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck, DefersLoadingPolicy::AllowDefersLoading, CachingPolicy::DisallowCaching));
78
79     CachedResourceLoader* loader = m_parent->player()->cachedResourceLoader();
80     m_resource = loader ? loader->requestRawResource(WTFMove(request)).valueOr(nullptr) : nullptr;
81     if (m_resource)
82         m_resource->addClient(*this);
83     else {
84         LOG_ERROR("Failed to start load for media at url %s", URL(CFURLRequestGetURL(urlRequest.get())).string().ascii().data());
85         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
86         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
87     }
88 }
89
90 void WebCoreAVCFResourceLoader::stopLoading()
91 {
92     if (!m_resource)
93         return;
94
95     m_resource->removeClient(*this);
96     m_resource = 0;
97
98     if (m_parent)
99         m_parent->didStopLoadingRequest(m_avRequest.get());
100 }
101
102 void WebCoreAVCFResourceLoader::invalidate()
103 {
104     if (!m_parent)
105         return;
106
107     m_parent = nullptr;
108
109     callOnMainThread([protectedThis = makeRef(*this)] () mutable {
110         protectedThis->stopLoading();
111     });
112 }
113
114 void WebCoreAVCFResourceLoader::responseReceived(CachedResource& resource, const ResourceResponse& response)
115 {
116     ASSERT_UNUSED(resource, &resource == m_resource);
117
118     int status = response.httpStatusCode();
119     if (status && (status < 200 || status > 299)) {
120         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, status, nullptr));
121         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
122         return;
123     }
124
125     notImplemented();
126 }
127
128 void WebCoreAVCFResourceLoader::dataReceived(CachedResource& resource, const char*, int)
129 {
130     fulfillRequestWithResource(resource);
131 }
132
133 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource& resource)
134 {
135     if (resource.loadFailedOrCanceled()) {
136         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
137         // string to trigger AVAsset's playable value to complete loading.
138         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
139         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
140         notImplemented();
141
142         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
143         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
144     } else {
145         fulfillRequestWithResource(resource);
146         // FIXME: [m_avRequest.get() finishLoading];
147         notImplemented();
148     }
149     stopLoading();
150 }
151
152 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource& resource)
153 {
154     ASSERT_UNUSED(resource, &resource == m_resource);
155     notImplemented();
156 }
157
158 }
159
160 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)