xbmc
ScriptInvocationManager.h
1 /*
2  * Copyright (C) 2013-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "addons/IAddon.h"
12 #include "interfaces/generic/ILanguageInvoker.h"
13 #include "threads/CriticalSection.h"
14 
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <vector>
19 
21 typedef std::shared_ptr<CLanguageInvokerThread> CLanguageInvokerThreadPtr;
22 
24 {
25 public:
26  static CScriptInvocationManager& GetInstance();
27 
28  void Process();
29  void Uninitialize();
30 
31  void RegisterLanguageInvocationHandler(ILanguageInvocationHandler *invocationHandler, const std::string &extension);
32  void RegisterLanguageInvocationHandler(ILanguageInvocationHandler *invocationHandler, const std::set<std::string> &extensions);
33  void UnregisterLanguageInvocationHandler(ILanguageInvocationHandler *invocationHandler);
34  bool HasLanguageInvoker(const std::string &script) const;
35  LanguageInvokerPtr GetLanguageInvoker(const std::string& script);
36 
40  int GetReusablePluginHandle(const std::string& script);
41 
50  int ExecuteAsync(const std::string& script,
51  const ADDON::AddonPtr& addon = ADDON::AddonPtr(),
52  const std::vector<std::string>& arguments = std::vector<std::string>(),
53  bool reuseable = false,
54  int pluginHandle = -1);
64  int ExecuteAsync(const std::string& script,
65  const LanguageInvokerPtr& languageInvoker,
66  const ADDON::AddonPtr& addon = ADDON::AddonPtr(),
67  const std::vector<std::string>& arguments = std::vector<std::string>(),
68  bool reuseable = false,
69  int pluginHandle = -1);
70 
87  int ExecuteSync(const std::string& script,
88  const ADDON::AddonPtr& addon = ADDON::AddonPtr(),
89  const std::vector<std::string>& arguments = std::vector<std::string>(),
90  uint32_t timeoutMs = 0,
91  bool waitShutdown = false);
109  int ExecuteSync(const std::string& script,
110  const LanguageInvokerPtr& languageInvoker,
111  const ADDON::AddonPtr& addon = ADDON::AddonPtr(),
112  const std::vector<std::string>& arguments = std::vector<std::string>(),
113  uint32_t timeoutMs = 0,
114  bool waitShutdown = false);
115  bool Stop(int scriptId, bool wait = false);
116  bool Stop(const std::string &scriptPath, bool wait = false);
117 
122  void StopRunningScripts(bool wait = false);
123 
124  bool IsRunning(int scriptId) const;
125  bool IsRunning(const std::string& scriptPath) const;
126 
127 protected:
128  friend class CLanguageInvokerThread;
129 
130  void OnExecutionDone(int scriptId);
131 
132 private:
133  CScriptInvocationManager() = default;
135  CScriptInvocationManager const& operator=(CScriptInvocationManager const&) = delete;
136  virtual ~CScriptInvocationManager();
137 
138  typedef struct {
139  CLanguageInvokerThreadPtr thread;
140  std::string script;
141  bool done;
142  } LanguageInvokerThread;
143  typedef std::map<int, LanguageInvokerThread> LanguageInvokerThreadMap;
144  typedef std::map<std::string, ILanguageInvocationHandler*> LanguageInvocationHandlerMap;
145 
146  LanguageInvokerThread getInvokerThread(int scriptId) const;
147 
148  LanguageInvocationHandlerMap m_invocationHandlers;
149  LanguageInvokerThreadMap m_scripts;
150  CLanguageInvokerThreadPtr m_lastInvokerThread;
151  int m_lastPluginHandle = -1;
152 
153  std::map<std::string, int> m_scriptPaths;
154  int m_nextId = 0;
155  mutable CCriticalSection m_critSection;
156 };
Definition: LanguageInvokerThread.h:19
int GetReusablePluginHandle(const std::string &script)
Returns addon_handle if last reusable invoker is ready to use.
Definition: ScriptInvocationManager.cpp:178
Definition: ILanguageInvocationHandler.h:13
Definition: TestHelpers.h:60
int ExecuteAsync(const std::string &script, const ADDON::AddonPtr &addon=ADDON::AddonPtr(), const std::vector< std::string > &arguments=std::vector< std::string >(), bool reuseable=false, int pluginHandle=-1)
Executes the given script asynchronously in a separate thread.
Definition: ScriptInvocationManager.cpp:219
void StopRunningScripts(bool wait=false)
Stop all running scripts.
Definition: ScriptInvocationManager.cpp:360
Definition: ScriptInvocationManager.h:23
int ExecuteSync(const std::string &script, const ADDON::AddonPtr &addon=ADDON::AddonPtr(), const std::vector< std::string > &arguments=std::vector< std::string >(), uint32_t timeoutMs=0, bool waitShutdown=false)
Executes the given script synchronously.
Definition: ScriptInvocationManager.cpp:293