Shaka Player Embedded
promise.h
Go to the documentation of this file.
1 // Copyright 2017 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 #ifndef SHAKA_EMBEDDED_MAPPING_PROMISE_H_
16 #define SHAKA_EMBEDDED_MAPPING_PROMISE_H_
17 
18 #include <string>
19 
20 #include "src/js/js_error.h"
21 #include "src/mapping/any.h"
25 
26 namespace shaka {
27 
28 #if defined(USING_V8)
29 using JsPromiseResolver = v8::Promise::Resolver;
30 #endif
31 
32 
37 class Promise : public GenericConverter, public memory::Traceable {
38  public:
39  static std::string name() {
40  return "Promise";
41  }
42 
47  Promise();
48  ~Promise() override;
49 
55  return Promise(false);
56  }
57 
58  // Chromium style complains without these.
59  Promise(const Promise&);
60  Promise(Promise&&);
61  Promise& operator=(const Promise&);
63 
64  static Promise Resolved() {
65  Promise ret = PendingPromise();
66  LocalVar<JsValue> undef = JsUndefined();
67  ret.ResolveWith(undef, /* run_events */ false);
68  return ret;
69  }
70 
71  static Promise Resolved(Handle<JsValue> value) {
72  Promise ret = PendingPromise();
73  ret.ResolveWith(value, /* run_events */ false);
74  return ret;
75  }
76 
77  static Promise Rejected(const js::JsError& error) {
78  Promise ret = PendingPromise();
79  ret.RejectWith(error, /* run_events */ false);
80  return ret;
81  }
82 
83  bool TryConvert(Handle<JsValue> value) override;
84  ReturnVal<JsValue> ToJsValue() const override;
85  void Trace(memory::HeapTracer* tracer) const override;
86 
88  bool CanResolve() const {
89 #ifdef USING_JSC
90  return !resolve_.empty();
91 #else
92  return !resolver_.empty();
93 #endif
94  }
95 
101  void ResolveWith(Handle<JsValue> value, bool run_events = true);
102  void RejectWith(const js::JsError& error, bool run_events = true);
103 
109  void Then(std::function<void(Any)> on_resolve,
110  std::function<void(Any)> on_reject);
111 
112  private:
113  explicit Promise(bool unused);
114 
115 #ifdef USING_JSC
116  WeakJsPtr<JsObject> resolve_;
117  WeakJsPtr<JsObject> reject_;
118 #else
120 #endif
121  WeakJsPtr<JsPromise> promise_;
122 };
123 
124 } // namespace shaka
125 
126 #endif // SHAKA_EMBEDDED_MAPPING_PROMISE_H_
bool CanResolve() const
Definition: promise.h:88
bool empty() const
Definition: weak_js_ptr.h:78
Definition: any.h:31
ReturnVal< JsValue > JsUndefined()
Definition: js_wrappers.cc:284
Promise & operator=(const Promise &)
void Trace(memory::HeapTracer *tracer) const override
Definition: promise.cc:91
void RejectWith(const js::JsError &error, bool run_events=true)
Definition: promise.cc:121
static Promise Resolved(Handle< JsValue > value)
Definition: promise.h:71
ReturnVal< JsValue > ToJsValue() const override
Definition: promise.cc:87
void ResolveWith(Handle< JsValue > value, bool run_events=true)
Definition: promise.cc:101
bool TryConvert(Handle< JsValue > value) override
Definition: promise.cc:73
static Promise Rejected(const js::JsError &error)
Definition: promise.h:77
~Promise() override
Definition: promise.cc:66
void Then(std::function< void(Any)> on_resolve, std::function< void(Any)> on_reject)
Definition: promise.cc:139
static Promise PendingPromise()
Definition: promise.h:54
static Promise Resolved()
Definition: promise.h:64
static std::string name()
Definition: promise.h:39