Add remote media resource loader for the GPU process
[WebKit.git] / Source / WebKit / GPUProcess / media / RemoteMediaResourceManager.cpp
1 /*
2  * Copyright (C) 2019 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 "RemoteMediaResourceManager.h"
28
29 #if ENABLE(GPU_PROCESS)
30
31 #include "Connection.h"
32 #include "DataReference.h"
33 #include "RemoteMediaPlayerManagerMessages.h"
34 #include "RemoteMediaResource.h"
35 #include "RemoteMediaResourceIdentifier.h"
36 #include "WebCoreArgumentCoders.h"
37 #include <WebCore/ResourceRequest.h>
38
39 namespace WebKit {
40
41 using namespace WebCore;
42
43 RemoteMediaResourceManager::RemoteMediaResourceManager()
44 {
45 }
46
47 RemoteMediaResourceManager::~RemoteMediaResourceManager()
48 {
49 }
50
51 void RemoteMediaResourceManager::addMediaResource(RemoteMediaResourceIdentifier remoteMediaResourceIdentifier, RemoteMediaResource& remoteMediaResource)
52 {
53     ASSERT(!m_remoteMediaResources.contains(remoteMediaResourceIdentifier));
54     m_remoteMediaResources.add(remoteMediaResourceIdentifier, &remoteMediaResource);
55 }
56
57 void RemoteMediaResourceManager::removeMediaResource(RemoteMediaResourceIdentifier remoteMediaResourceIdentifier)
58 {
59     ASSERT(m_remoteMediaResources.contains(remoteMediaResourceIdentifier));
60     m_remoteMediaResources.remove(remoteMediaResourceIdentifier);
61 }
62
63 void RemoteMediaResourceManager::responseReceived(RemoteMediaResourceIdentifier id, const ResourceResponse& response, bool didPassAccessControlCheck, CompletionHandler<void(PolicyChecker::ShouldContinue)>&& completionHandler)
64 {
65     auto* resource = m_remoteMediaResources.get(id);
66     if (!resource || !resource->ready()) {
67         completionHandler(PolicyChecker::ShouldContinue::No);
68         return;
69     }
70
71     m_remoteMediaResources.get(id)->responseReceived(response, didPassAccessControlCheck, WTFMove(completionHandler));
72 }
73
74 void RemoteMediaResourceManager::redirectReceived(RemoteMediaResourceIdentifier id, ResourceRequest&& request, const ResourceResponse& response, CompletionHandler<void(WebCore::ResourceRequest&&)>&& completionHandler)
75 {
76     auto* resource = m_remoteMediaResources.get(id);
77     if (!resource || !resource->ready()) {
78         completionHandler({ });
79         return;
80     }
81
82     m_remoteMediaResources.get(id)->redirectReceived(WTFMove(request), response, WTFMove(completionHandler));
83 }
84
85 void RemoteMediaResourceManager::dataSent(RemoteMediaResourceIdentifier id, uint64_t bytesSent, uint64_t totalBytesToBeSent)
86 {
87     auto* resource = m_remoteMediaResources.get(id);
88     if (!resource || !resource->ready())
89         return;
90
91     m_remoteMediaResources.get(id)->dataSent(bytesSent, totalBytesToBeSent);
92 }
93
94 void RemoteMediaResourceManager::dataReceived(RemoteMediaResourceIdentifier id, const IPC::DataReference& data)
95 {
96     auto* resource = m_remoteMediaResources.get(id);
97     if (!resource || !resource->ready())
98         return;
99
100     m_remoteMediaResources.get(id)->dataReceived(reinterpret_cast<const char*>(data.data()), data.size());
101 }
102
103 void RemoteMediaResourceManager::accessControlCheckFailed(RemoteMediaResourceIdentifier id, const ResourceError& error)
104 {
105     auto* resource = m_remoteMediaResources.get(id);
106     if (!resource || !resource->ready())
107         return;
108
109     m_remoteMediaResources.get(id)->accessControlCheckFailed(error);
110 }
111
112 void RemoteMediaResourceManager::loadFailed(RemoteMediaResourceIdentifier id, const ResourceError& error)
113 {
114     auto* resource = m_remoteMediaResources.get(id);
115     if (!resource || !resource->ready())
116         return;
117
118     m_remoteMediaResources.get(id)->loadFailed(error);
119 }
120
121 void RemoteMediaResourceManager::loadFinished(RemoteMediaResourceIdentifier id)
122 {
123     auto* resource = m_remoteMediaResources.get(id);
124     if (!resource || !resource->ready())
125         return;
126
127     m_remoteMediaResources.get(id)->loadFinished();
128 }
129
130 } // namespace WebKit
131
132 #endif