Shaka Player Embedded
eme_promise.cc
Go to the documentation of this file.
1 // Copyright 2018 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/eme/eme_promise.h"
16 
17 #include <atomic>
18 
20 #include "src/js/js_error.h"
21 #include "src/mapping/js_utils.h"
22 #include "src/mapping/promise.h"
23 #include "src/memory/heap_tracer.h"
25 
26 namespace shaka {
27 namespace eme {
28 
29 EmePromise::Impl::Impl(const Promise& promise, bool has_value)
30  : promise_(MakeJsRef<Promise>(promise)),
31  is_pending_(false),
32  has_value_(has_value) {}
33 
34 EmePromise::Impl::Impl() : is_pending_(false), has_value_(false) {}
35 
37 
39  bool expected = false;
40  if (is_pending_.compare_exchange_strong(expected, true)) {
41  if (has_value_) {
42  LOG(WARNING) << "Resolve called on Promise that should have a "
43  "value, resolving with false.";
44  }
45 
46  RefPtr<Promise> promise = promise_;
47  bool has_value = has_value_;
49  TaskPriority::Internal, "DoResolvePromise", [=]() {
50  LocalVar<JsValue> value;
51  if (has_value)
52  value = ToJsValue(false);
53  else
54  value = JsUndefined();
55  promise->ResolveWith(value);
56  });
57  }
58 }
59 
61  bool expected = false;
62  if (is_pending_.compare_exchange_strong(expected, true)) {
63  if (!has_value_) {
64  LOG(WARNING) << "ResolveWith called on Promise that shouldn't have a "
65  "value, ignoring value.";
66  }
67 
68  RefPtr<Promise> promise = promise_;
69  bool has_value = has_value_;
71  TaskPriority::Internal, "DoResolvePromise", [=]() {
72  LocalVar<JsValue> js_value;
73  if (has_value)
74  js_value = ToJsValue(value);
75  else
76  js_value = JsUndefined();
77  promise->ResolveWith(js_value);
78  });
79  }
80 }
81 
83  const std::string& message) {
84  bool expected = false;
85  if (is_pending_.compare_exchange_strong(expected, true)) {
86  RefPtr<Promise> promise = promise_;
88  TaskPriority::Internal, "DoRejectPromise", [=]() {
89  switch (except_type) {
91  promise->RejectWith(js::JsError::TypeError(message));
92  break;
94  promise->RejectWith(js::JsError::RangeError(message));
95  break;
97  promise->RejectWith(
99  break;
101  promise->RejectWith(
103  break;
105  promise->RejectWith(
107  break;
108 
109  default:
110  promise->RejectWith(
112  break;
113  }
114  });
115  }
116 }
117 
118 
119 EmePromise::EmePromise() : impl_(nullptr) {}
120 EmePromise::EmePromise(const Promise& promise, bool has_value)
121  : impl_(new Impl(promise, has_value)) {}
122 EmePromise::EmePromise(std::shared_ptr<Impl> impl) : impl_(impl) {}
123 EmePromise::EmePromise(const EmePromise& other) = default;
124 EmePromise::EmePromise(EmePromise&& other) = default;
126 
127 EmePromise& EmePromise::operator=(const EmePromise& other) = default;
128 EmePromise& EmePromise::operator=(EmePromise&& other) = default;
129 
130 bool EmePromise::valid() const {
131  return impl_.get();
132 }
133 
135  impl_->Resolve();
136 }
137 
138 void EmePromise::ResolveWith(bool value) {
139  impl_->ResolveWith(value);
140 }
141 
142 void EmePromise::Reject(ExceptionType except_type, const std::string& message) {
143  impl_->Reject(except_type, message);
144 }
145 
146 } // namespace eme
147 } // namespace shaka
virtual void ResolveWith(bool value)
Definition: eme_promise.cc:60
ReturnVal< JsValue > JsUndefined()
Definition: js_wrappers.cc:284
EmePromise & operator=(const EmePromise &other)
ReturnVal< JsValue > ToJsValue(T &&source)
Definition: convert_js.h:381
void ResolveWith(bool value)
Definition: eme_promise.cc:138
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask(TaskPriority priority, const std::string &name, Func &&callback)
Definition: task_runner.h:219
RefPtr< T > MakeJsRef(Args &&... args)
Definition: js_utils.h:52
static JsError TypeError(const std::string &message)
Definition: js_error.cc:81
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115
const char * message
static JsError RangeError(const std::string &message)
Definition: js_error.cc:65
virtual void Reject(ExceptionType except_type, const std::string &message)
Definition: eme_promise.cc:82
void Reject(ExceptionType except_type, const std::string &message)
Definition: eme_promise.cc:142
TaskRunner * MainThread()