Shaka Player Embedded
js_manager_impl.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 
16 
17 #include <utility>
18 
19 #include "src/mapping/convert_js.h"
20 #include "src/mapping/js_engine.h"
21 #include "src/util/clock.h"
22 #include "src/util/file_system.h"
23 
24 namespace shaka {
25 
26 using std::placeholders::_1;
27 
29  : tracker_(&heap_tracer_),
30  startup_options_(options),
31  event_loop_(std::bind(&JsManagerImpl::EventThreadWrapper, this, _1),
32  &util::Clock::Instance, /* is_worker */ false) {}
33 
35  Stop();
36 }
37 
38 std::string JsManagerImpl::GetPathForStaticFile(const std::string& file) const {
40  startup_options_.static_data_dir,
41  startup_options_.is_static_relative_to_bundle, file);
42 }
43 
45  const std::string& file) const {
47  startup_options_.dynamic_data_dir, file);
48 }
49 
51  if (event_loop_.is_running() && event_loop_.HasPendingWork()) {
52  event_loop_.WaitUntilFinished();
53  }
54 }
55 
56 std::shared_ptr<ThreadEvent<bool>> JsManagerImpl::RunScript(
57  const std::string& path) {
58  // Script must be executed on the event thread.
59  CHECK(event_loop_.is_running());
60  auto callback = std::bind(
61  static_cast<bool (*)(const std::string&)>(&shaka::RunScript), path);
62  return event_loop_.AddInternalTask(TaskPriority::Immediate, "RunScript",
63  std::move(callback));
64 }
65 
66 std::shared_ptr<ThreadEvent<bool>> JsManagerImpl::RunScript(
67  const std::string& path, const uint8_t* data, size_t data_size) {
68  // Script must be executed on the event thread.
69  CHECK(event_loop_.is_running());
70  auto* run_script =
71  static_cast<bool (*)(const std::string&, const uint8_t*, size_t)>(
73  auto callback = std::bind(run_script, path, data, data_size);
74  return event_loop_.AddInternalTask(TaskPriority::Immediate, "RunScript",
75  std::move(callback));
76 }
77 
78 void JsManagerImpl::EventThreadWrapper(TaskRunner::RunLoop run_loop) {
79  JsEngine engine;
80 
81  {
83 #ifdef USING_V8
84  engine.isolate()->SetEmbedderHeapTracer(&heap_tracer_);
85 #endif
86 
87  Environment env;
88  env.Install();
89 
90  run_loop();
91 
92  network_thread_.Stop();
93  tracker_.Dispose();
94  }
95 }
96 
97 } // namespace shaka
static std::string GetPathForStaticFile(const std::string &static_data_dir, bool is_bundle_relative, const std::string &file)
Definition: file_system.cc:46
std::string GetPathForDynamicFile(const std::string &file) const
bool is_running() const
Definition: task_runner.h:163
static std::string GetPathForDynamicFile(const std::string &dynamic_data_dir, const std::string &file)
Definition: file_system.cc:57
std::function< void()> RunLoop
Definition: task_runner.h:156
void WaitUntilFinished()
Definition: task_runner.cc:87
bool RunScript(const std::string &path)
Definition: js_wrappers.cc:239
std::shared_ptr< ThreadEvent< bool > > RunScript(const std::string &path)
JsManagerImpl(const JsManager::StartupOptions &options)
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask(TaskPriority priority, const std::string &name, Func &&callback)
Definition: task_runner.h:219
bool HasPendingWork() const
Definition: task_runner.cc:59
std::string GetPathForStaticFile(const std::string &file) const