Shaka Player Embedded
net_public.cc
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "shaka/net.h"
16 #include "src/core/ref_ptr.h"
17 #include "src/js/net.h"
18 #include "src/mapping/js_utils.h"
20 
21 namespace shaka {
22 
24  public:
26 };
27 
29 
30 const uint8_t* Request::body() const {
31  if (!impl_->request->body.has_value())
32  return nullptr;
33  return impl_->request->body->data();
34 }
35 
36 size_t Request::body_size() const {
37  if (!impl_->request->body.has_value())
38  return 0;
39  return impl_->request->body->size();
40 }
41 
42 void Request::SetBodyCopy(const uint8_t* data, size_t size) {
43  if (data) {
44  if (impl_->request->body.has_value())
45  impl_->request->body->SetFromBuffer(data, size);
46  else
47  impl_->request->body.emplace(data, size);
48  } else {
49  impl_->request->body.reset();
50  }
51 }
52 
54  : uris(request.uris),
55  method(request.method),
56  headers(request.headers),
57  impl_(new Impl{MakeJsRef<js::Request>(std::move(request))}) {}
58 
59 void Request::Finalize() {
60  impl_->request->uris = uris;
61  impl_->request->method = method;
62  impl_->request->headers = headers;
63  // Call ToJsValue to make the Struct update the JS object.
64  (void)impl_->request->ToJsValue();
65 }
66 
67 
69  public:
71 };
72 
74 
75 const uint8_t* Response::data() const {
76  return impl_->response->data.data();
77 }
78 
79 size_t Response::data_size() const {
80  return impl_->response->data.size();
81 }
82 
83 void Response::SetDataCopy(const uint8_t* data, size_t size) {
84  impl_->response->data.SetFromBuffer(data, size);
85 }
86 
87 Response::Response()
88  : timeMs(0), fromCache(false), impl_(new Impl{MakeJsRef<js::Response>()}) {}
89 
90 Response::Response(js::Response&& response)
91  : uri(response.uri),
92  originalUri(response.originalUri),
93  headers(response.headers),
94  timeMs(response.timeMs),
95  fromCache(response.fromCache),
96  impl_(new Impl{MakeJsRef<js::Response>(std::move(response))}) {}
97 
98 void Response::Finalize() {
99  impl_->response->uri = uri;
100  impl_->response->originalUri = originalUri;
101  impl_->response->headers = headers;
102  impl_->response->timeMs = timeMs;
103  impl_->response->fromCache = fromCache;
104  // Call ToJsValue to make the Struct update the JS object.
105  (void)impl_->response->ToJsValue();
106 }
107 
108 js::Response* Response::JsObject() {
109  Finalize();
110  return impl_->response.get();
111 }
112 
113 
114 // \cond Doxygen_Skip
116 SchemePlugin::~SchemePlugin() {}
117 
119 SchemePlugin::Client::~Client() {}
120 
122 NetworkFilters::~NetworkFilters() {}
123 // \endcond Doxygen_Skip
124 
125 
126 std::future<optional<Error>> NetworkFilters::OnRequestFilter(RequestType type,
127  Request* request) {
128  return {};
129 }
130 
131 std::future<optional<Error>> NetworkFilters::OnResponseFilter(
132  RequestType type, Response* response) {
133  return {};
134 }
135 
136 } // namespace shaka
size_t body_size() const
Definition: net_public.cc:36
void SetDataCopy(const uint8_t *data, size_t size)
Definition: net_public.cc:83
std::unordered_map< std::string, std::string > headers
Definition: net.h:71
virtual std::future< optional< Error > > OnRequestFilter(RequestType type, Request *request)
Definition: net_public.cc:126
ExceptionCode type
const uint8_t * body() const
Definition: net_public.cc:30
RefPtr< js::Response > response
Definition: net_public.cc:70
std::vector< std::string > uris
Definition: net.h:65
RefPtr< js::Request > request
Definition: net_public.cc:25
RequestType
Definition: net.h:38
size_t data_size() const
Definition: net_public.cc:79
void SetBodyCopy(const uint8_t *data, size_t size)
Definition: net_public.cc:42
Request(const Request &)=delete
std::string method
Definition: net.h:68
const uint8_t * data() const
Definition: net_public.cc:75
virtual std::future< optional< Error > > OnResponseFilter(RequestType type, Response *response)
Definition: net_public.cc:131