Shaka Player Embedded
js_manager_impl.h
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 #ifndef SHAKA_EMBEDDED_CORE_JS_MANAGER_IMPL_H_
16 #define SHAKA_EMBEDDED_CORE_JS_MANAGER_IMPL_H_
17 
18 #include <glog/logging.h>
19 
20 #include <functional>
21 #include <memory>
22 #include <string>
23 
24 #include "shaka/js_manager.h"
25 #include "src/core/environment.h"
27 #include "src/core/task_runner.h"
28 #include "src/debug/thread_event.h"
29 #include "src/memory/heap_tracer.h"
31 #ifdef USING_V8
33 #endif
35 
36 namespace shaka {
37 
38 class JsManagerImpl : public PseudoSingleton<JsManagerImpl> {
39  public:
40  explicit JsManagerImpl(const JsManager::StartupOptions& options);
42 
44  return &event_loop_;
45  }
47  return &network_thread_;
48  }
50  return &heap_tracer_;
51  }
52 
53  std::string GetPathForStaticFile(const std::string& file) const;
54  std::string GetPathForDynamicFile(const std::string& file) const;
55 
56  void Stop() {
57  event_loop_.Stop();
58  }
59 
60  void WaitUntilFinished();
61 
62  std::shared_ptr<ThreadEvent<bool>> RunScript(const std::string& path);
63  std::shared_ptr<ThreadEvent<bool>> RunScript(const std::string& path,
64  const uint8_t* data,
65  size_t data_size);
66 
67  private:
68  void EventThreadWrapper(TaskRunner::RunLoop run_loop);
69 
70 #ifdef USING_V8
71  memory::V8HeapTracer heap_tracer_;
72 #else
73  memory::HeapTracer heap_tracer_;
74 #endif
75  memory::ObjectTracker tracker_;
76  JsManager::StartupOptions startup_options_;
77 
78  TaskRunner event_loop_;
79  class NetworkThread network_thread_;
80 };
81 
86 template <typename... Args>
87 std::function<void(Args...)> MainThreadCallback(
88  std::function<void(Args...)> cb) {
89  return [=](Args... args) {
91  TaskPriority::Internal, "", std::bind(cb, args...));
92  };
93 }
94 
95 } // namespace shaka
96 
97 #endif // SHAKA_EMBEDDED_CORE_JS_MANAGER_IMPL_H_
std::string GetPathForDynamicFile(const std::string &file) const
std::function< void()> RunLoop
Definition: task_runner.h:156
memory::HeapTracer * HeapTracer()
std::function< void(Args...)> MainThreadCallback(std::function< void(Args...)> cb)
NetworkThread * NetworkThread()
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
std::string GetPathForStaticFile(const std::string &file) const
TaskRunner * MainThread()