Shaka Player Embedded
js_manager.cc
Go to the documentation of this file.
1 // Copyright 2016 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/js_manager.h"
16 
17 #include "shaka/error.h"
20 #include "src/js/js_error.h"
21 #include "src/js/net.h"
22 #include "src/mapping/callback.h"
23 #include "src/mapping/convert_js.h"
24 #include "src/mapping/js_utils.h"
26 #include "src/mapping/promise.h"
28 
29 namespace shaka {
30 
31 namespace {
32 
33 class ProgressClient : public SchemePlugin::Client {
34  public:
35  explicit ProgressClient(Callback on_progress)
36  : on_progress_(MakeJsRef<Callback>(std::move(on_progress))) {}
37 
38  void OnProgress(double time, uint64_t bytes, uint64_t remaining) override {
39  RefPtr<Callback> progress = on_progress_;
40  auto cb = [=]() { (*progress)(time, bytes, remaining); };
42  }
43 
44  private:
45  RefPtr<Callback> on_progress_;
46 };
47 
48 } // namespace
49 
52  : impl_(new JsManagerImpl(options)) {}
54 
55 JsManager::JsManager(JsManager&&) = default;
57 
58 
60  impl_->Stop();
61 }
62 
64  impl_->WaitUntilFinished();
65 }
66 
67 AsyncResults<void> JsManager::RunScript(const std::string& path) {
68  auto run_future = impl_->RunScript(path)->future();
69  // This creates a std::future that will invoke the given method when the
70  // wait()/get() method is called. This exists to convert the
71  // std::future<bool> that is returned into a
72  // std::future<variant<monostate, Error>> that AsyncResults expects.
73  // TODO: Find a better way to do this.
74  auto future = std::async(std::launch ::deferred,
75  [run_future]() -> variant<monostate, Error> {
76  if (run_future.get())
77  return monostate();
78 
79  return Error("Unknown script error");
80  });
81 
82  return future.share();
83 }
84 
86  SchemePlugin* plugin) {
87  auto js_scheme_plugin = [=](const std::string& uri, js::Request request,
88  RequestType type, Callback on_progress) {
89  std::shared_ptr<Request> pub_req(new Request(std::move(request)));
90  std::shared_ptr<Response> resp(new Response);
91  std::shared_ptr<SchemePlugin::Client> client(
92  new ProgressClient(std::move(on_progress)));
94  auto future =
95  plugin->OnNetworkRequest(uri, type, *pub_req, client.get(), resp.get());
96  auto on_done = [pub_req, resp, client, ret]() {
97  Promise copy = ret; // By-value captures are const, so make a copy.
98  copy.ResolveWith(resp->JsObject()->ToJsValue(),
99  /* raise_events= */ false);
100  };
101  HandleNetworkFuture(ret, std::move(future), std::move(on_done));
102  return ret;
103  };
104 
105  return JsObjectWrapper::CallGlobalMethod<void>(
106  {"shaka", "net", "NetworkingEngine", "registerScheme"}, scheme,
107  std::move(js_scheme_plugin));
108 }
109 
111  const std::string& scheme) {
112  return JsObjectWrapper::CallGlobalMethod<void>(
113  {"shaka", "net", "NetworkingEngine", "unregisterScheme"}, scheme);
114 }
115 
116 } // namespace shaka
AsyncResults< void > RunScript(const std::string &path)
Definition: js_manager.cc:67
virtual std::future< optional< Error > > OnNetworkRequest(const std::string &uri, RequestType type, const Request &request, Client *client, Response *response)=0
void HandleNetworkFuture(Promise promise, std::future< optional< Error >> future, std::function< void()> on_done)
Definition: js_utils.cc:68
void ResolveWith(Handle< JsValue > value, bool run_events=true)
Definition: promise.cc:101
ExceptionCode type
std::shared_future< impl::RetOf< Func > > InvokeOrSchedule(Func &&callback)
Definition: task_runner.h:192
JsManager & operator=(JsManager &&)
void WaitUntilFinished()
Definition: js_manager.cc:63
AsyncResults< void > RegisterNetworkScheme(const std::string &scheme, SchemePlugin *plugin)
Definition: js_manager.cc:85
static Promise PendingPromise()
Definition: promise.h:54
RequestType
Definition: net.h:38
RefPtr< T > MakeJsRef(Args &&... args)
Definition: js_utils.h:52
AsyncResults< void > UnregisterNetworkScheme(const std::string &scheme)
Definition: js_manager.cc:110
TaskRunner * MainThread()