Eliminate ResourceBuffer and use SharedBuffer directly instead
[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     URL requestURL = CFURLRequestGetURL(urlRequest.get());
73
74     CachedResourceRequest request(ResourceRequest(requestURL), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo));
75
76     request.mutableResourceRequest().setPriority(ResourceLoadPriorityLow);
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", requestURL.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     m_parent = nullptr;
103     stopLoading();
104 }
105
106 void WebCoreAVCFResourceLoader::responseReceived(CachedResource* resource, const ResourceResponse& response)
107 {
108     ASSERT(resource == m_resource);
109     UNUSED_PARAM(resource);
110
111     int status = response.httpStatusCode();
112     if (status && (status < 200 || status > 299)) {
113         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, status, nullptr));
114         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
115         return;
116     }
117
118     notImplemented();
119 }
120
121 void WebCoreAVCFResourceLoader::dataReceived(CachedResource* resource, const char*, int)
122 {
123     fulfillRequestWithResource(resource);
124 }
125
126 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource* resource)
127 {
128     if (resource->loadFailedOrCanceled()) {
129         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
130         // string to trigger AVAsset's playable value to complete loading.
131         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
132         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
133         notImplemented();
134
135         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
136         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
137     } else {
138         fulfillRequestWithResource(resource);
139         // FIXME: [m_avRequest.get() finishLoading];
140         notImplemented();
141     }
142     stopLoading();
143 }
144
145 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource* resource)
146 {
147     ASSERT(resource == m_resource);
148     notImplemented();
149 }
150
151 }
152
153 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)