xbmc
AddonManager.h
1 /*
2  * Copyright (C) 2005-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 "threads/CriticalSection.h"
12 #include "utils/EventStream.h"
13 
14 #include <map>
15 #include <memory>
16 #include <mutex>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 namespace ADDON
22 {
23 enum class AddonDisabledReason;
24 enum class AddonOriginType;
25 enum class AddonType;
26 enum class AddonUpdateRule;
27 enum class AllowCheckForUpdates : bool;
28 
29 class CAddonDatabase;
30 class CAddonUpdateRules;
31 class CAddonVersion;
32 class IAddonMgrCallback;
33 
34 class CAddonInfo;
35 using AddonInfoPtr = std::shared_ptr<CAddonInfo>;
36 using ADDON_INFO_LIST = std::map<std::string, AddonInfoPtr>;
37 
38 class IAddon;
39 using AddonPtr = std::shared_ptr<IAddon>;
40 using VECADDONS = std::vector<AddonPtr>;
41 
42 struct AddonEvent;
43 struct AddonWithUpdate;
44 struct DependencyInfo;
45 struct RepositoryDirInfo;
46 
47 using AddonInstanceId = uint32_t;
48 
49 enum class AddonCheckType : bool
50 {
51  OUTDATED_ADDONS,
52  AVAILABLE_UPDATES,
53 };
54 
55 enum class OnlyEnabled : bool
56 {
57  CHOICE_YES = true,
58  CHOICE_NO = false,
59 };
60 
61 enum class OnlyEnabledRootAddon : bool
62 {
63  CHOICE_YES = true,
64  CHOICE_NO = false,
65 };
66 
67 enum class CheckIncompatible : bool
68 {
69  CHOICE_YES = true,
70  CHOICE_NO = false,
71 };
72 
79 class CAddonMgr
80 {
81 public:
82  bool ReInit()
83  {
84  DeInit();
85  return Init();
86  }
87  bool Init();
88  void DeInit();
89 
90  CAddonMgr();
91  CAddonMgr(const CAddonMgr&) = delete;
92  virtual ~CAddonMgr();
93 
94  CEventStream<AddonEvent>& Events() { return m_events; }
95  CEventStream<AddonEvent>& UnloadEvents() { return m_unloadEvents; }
96 
97  IAddonMgrCallback* GetCallbackForType(AddonType type);
98  bool RegisterAddonMgrCallback(AddonType type, IAddonMgrCallback* cb);
99  void UnregisterAddonMgrCallback(AddonType type);
100 
108  bool GetAddon(const std::string& id,
109  AddonPtr& addon,
110  AddonType type,
111  OnlyEnabled onlyEnabled) const;
112 
119  bool GetAddon(const std::string& id, AddonPtr& addon, OnlyEnabled onlyEnabled) const;
120 
121  bool HasType(const std::string& id, AddonType type);
122 
123  bool HasAddons(AddonType type);
124 
125  bool HasInstalledAddons(AddonType type);
126 
128  bool GetAddonsForUpdate(VECADDONS& addons) const;
129 
131  bool GetAddons(VECADDONS& addons) const;
132 
134  bool GetAddons(VECADDONS& addons, AddonType type);
135 
137  bool GetInstalledAddons(VECADDONS& addons);
138 
140  bool GetInstalledAddons(VECADDONS& addons, AddonType type);
141 
142  bool GetDisabledAddons(VECADDONS& addons);
143 
144  bool GetDisabledAddons(VECADDONS& addons, AddonType type);
145 
147  bool GetInstallableAddons(VECADDONS& addons);
148 
149  bool GetInstallableAddons(VECADDONS& addons, AddonType type);
150 
163  bool FindInstallableById(const std::string& addonId, AddonPtr& addon);
164 
165  void AddToUpdateableAddons(AddonPtr& pAddon);
166  void RemoveFromUpdateableAddons(AddonPtr& pAddon);
167  bool ReloadSettings(const std::string& addonId, AddonInstanceId instanceId);
168 
170  std::vector<std::shared_ptr<IAddon>> GetAvailableUpdates() const;
171 
173  std::vector<std::shared_ptr<IAddon>> GetOutdatedAddons() const;
174 
176  bool HasAvailableUpdates();
177 
184  bool IsOrphaned(const std::shared_ptr<IAddon>& addon,
185  const std::vector<std::shared_ptr<IAddon>>& allAddons) const;
186 
190  bool FindAddons();
191 
198  bool FindAddon(const std::string& addonId,
199  const std::string& origin,
200  const CAddonVersion& addonVersion);
201 
209  bool GetIncompatibleEnabledAddonInfos(std::vector<AddonInfoPtr>& incompatible) const;
210 
217  std::vector<AddonInfoPtr> MigrateAddons();
218 
225  std::vector<AddonInfoPtr> DisableIncompatibleAddons(
226  const std::vector<AddonInfoPtr>& incompatible);
227 
232  void CheckAndInstallAddonUpdates(bool wait) const;
233 
239  bool UnloadAddon(const std::string& addonId);
240 
246  bool LoadAddon(const std::string& addonId,
247  const std::string& origin,
248  const CAddonVersion& addonVersion);
249 
254  void OnPostUnInstall(const std::string& id);
255 
257  bool DisableAddon(const std::string& ID, AddonDisabledReason disabledReason);
258 
260  bool UpdateDisabledReason(const std::string& id, AddonDisabledReason newDisabledReason);
261 
263  bool EnableAddon(const std::string& ID);
264 
265  /* \brief Check whether an addon has been disabled via DisableAddon.
266  In case the disabled cache does not know about the current state the database routine will be used.
267  \param ID id of the addon
268  \sa DisableAddon
269  */
270  bool IsAddonDisabled(const std::string& ID) const;
271 
281  bool IsAddonDisabledExcept(const std::string& ID, AddonDisabledReason disabledReason) const;
282 
283  /* \brief Checks whether an addon can be disabled via DisableAddon.
284  \param ID id of the addon
285  \sa DisableAddon
286  */
287  bool CanAddonBeDisabled(const std::string& ID);
288 
289  bool CanAddonBeEnabled(const std::string& id);
290 
291  /* \brief Checks whether an addon is installed.
292  \param ID id of the addon
293  */
294  bool IsAddonInstalled(const std::string& ID);
295 
296  /* \brief Checks whether an addon is installed from a
297  * particular origin repo
298  * \note if checked for an origin defined as official (i.e. repository.xbmc.org)
299  * this function will return true even if the addon is a shipped system add-on
300  * \param ID id of the addon
301  * \param origin origin repository id
302  */
303  bool IsAddonInstalled(const std::string& ID, const std::string& origin) const;
304 
305  /* \brief Checks whether an addon is installed from a
306  * particular origin repo and version
307  * \note if checked for an origin defined as official (i.e. repository.xbmc.org)
308  * this function will return true even if the addon is a shipped system add-on
309  * \param ID id of the addon
310  * \param origin origin repository id
311  * \param version the version of the addon
312  */
313  bool IsAddonInstalled(const std::string& ID,
314  const std::string& origin,
315  const CAddonVersion& version);
316 
317  /* \brief Checks whether an addon can be installed. Broken addons can't be installed.
318  \param addon addon to be checked
319  */
320  bool CanAddonBeInstalled(const AddonPtr& addon);
321 
322  bool CanUninstall(const AddonPtr& addon);
323 
330  bool IsBundledAddon(const std::string& id);
331 
338  bool IsSystemAddon(const std::string& id);
339 
346  bool IsRequiredSystemAddon(const std::string& id);
347 
354  bool IsOptionalSystemAddon(const std::string& id);
355 
365 
367  /* \brief Add a single update rule to the list for an addon
368  * \sa CAddonUpdateRules::AddUpdateRuleToList()
369  */
370  bool AddUpdateRuleToList(const std::string& id, AddonUpdateRule updateRule);
371 
372  /* \brief Remove all rules from update rules list for an addon
373  * \sa CAddonUpdateRules::RemoveAllUpdateRulesFromList()
374  */
375  bool RemoveAllUpdateRulesFromList(const std::string& id);
376 
377  /* \brief Remove a specific rule from update rules list for an addon
378  * \sa CAddonUpdateRules::RemoveUpdateRuleFromList()
379  */
380  bool RemoveUpdateRuleFromList(const std::string& id, AddonUpdateRule updateRule);
381 
382  /* \brief Check if an addon version is auto-updateable
383  * \param id addon id to be checked
384  * \return true is addon is auto-updateable, false otherwise
385  * \sa CAddonUpdateRules::IsAutoUpdateable()
386  */
387  bool IsAutoUpdateable(const std::string& id) const;
388 
391  /* \brief Launches event AddonEvent::AutoUpdateStateChanged
392  * \param id addon id to pass through
393  * \sa CGUIDialogAddonInfo::OnToggleAutoUpdates()
394  */
395  void PublishEventAutoUpdateStateChanged(const std::string& id);
396  void UpdateLastUsed(const std::string& id);
397 
406  void PublishInstanceAdded(const std::string& addonId, AddonInstanceId instanceId);
407 
416  void PublishInstanceRemoved(const std::string& addonId, AddonInstanceId instanceId);
417 
424  bool LoadAddonDescription(const std::string& path, AddonPtr& addon);
425 
426  bool ServicesHasStarted() const;
427 
434  bool IsCompatible(const std::shared_ptr<const IAddon>& addon) const;
435 
442  bool IsCompatible(const AddonInfoPtr& addonInfo) const;
443 
448  std::vector<DependencyInfo> GetDepsRecursive(const std::string& id,
449  OnlyEnabledRootAddon onlyEnabledRootAddon);
450 
464  bool GetAddonInfos(std::vector<AddonInfoPtr>& addonInfos, bool onlyEnabled, AddonType type) const;
465 
477  std::vector<AddonInfoPtr> GetAddonInfos(bool onlyEnabled,
478  const std::vector<AddonType>& types) const;
479 
491  bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos, AddonType type) const;
492 
509  bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos,
510  AddonType type,
511  AddonDisabledReason disabledReason) const;
512 
513  const AddonInfoPtr GetAddonInfo(const std::string& id, AddonType type) const;
514 
522  const std::string& GetTempAddonBasePath() { return m_tempAddonBasePath; }
523 
524  AddonOriginType GetAddonOriginType(const AddonPtr& addon) const;
525 
532  bool IsAddonDisabledWithReason(const std::string& ID, AddonDisabledReason disabledReason) const;
533 
542 
562  bool SetAddonOrigin(const std::string& addonId, const std::string& repoAddonId, bool isUpdate);
563 
577  bool AddonsFromRepoXML(const RepositoryDirInfo& repo,
578  const std::string& xml,
579  std::vector<AddonInfoPtr>& addons);
580 
588  std::map<std::string, AddonWithUpdate> GetAddonsWithAvailableUpdate() const;
589 
595  std::vector<std::shared_ptr<IAddon>> GetCompatibleVersions(const std::string& addonId) const;
596 
603  const std::string& GetLastAvailableUpdatesCountAsString() const;
604 
609  std::vector<std::shared_ptr<IAddon>> GetOrphanedDependencies() const;
610 
611 private:
612  CAddonMgr& operator=(CAddonMgr const&) = delete;
613 
614  VECADDONS m_updateableAddons;
615 
623  std::vector<std::shared_ptr<IAddon>> GetAvailableUpdatesOrOutdatedAddons(
624  AddonCheckType addonCheckType) const;
625 
626  bool GetAddonsInternal(AddonType type,
627  VECADDONS& addons,
628  OnlyEnabled onlyEnabled,
629  CheckIncompatible checkIncompatible) const;
630 
631  bool EnableSingle(const std::string& id);
632 
633  void FindAddons(ADDON_INFO_LIST& addonmap, const std::string& path);
634 
643  bool GetIncompatibleAddonInfos(std::vector<AddonInfoPtr>& incompatible,
644  bool includeDisabled) const;
645 
651  bool GetAddonUpdateCandidates(VECADDONS& updates) const;
652 
657  void SortByDependencies(VECADDONS& updates) const;
658 
666  void InstallAddonUpdates(VECADDONS& updates,
667  bool wait,
668  AllowCheckForUpdates allowCheckForUpdates) const;
669 
670  // This guards the addon installation process to make sure
671  // addon updates are not installed concurrently
672  // while the migration is running. Addon updates can be triggered
673  // as a result of a repository update event.
674  // (migration will install any available update anyway)
675  mutable std::mutex m_installAddonsMutex;
676 
677  std::map<std::string, AddonDisabledReason> m_disabled;
678  static std::map<AddonType, IAddonMgrCallback*> m_managers;
679  mutable CCriticalSection m_critSection;
680  std::unique_ptr<CAddonDatabase> m_database;
681  std::unique_ptr<CAddonUpdateRules> m_updateRules;
682  CEventSource<AddonEvent> m_events;
683  CBlockingEventSource<AddonEvent> m_unloadEvents;
684  std::set<std::string> m_systemAddons;
685  std::set<std::string> m_optionalSystemAddons;
686  ADDON_INFO_LIST m_installedAddons;
687 
688  // Temporary path given to add-ons, whose content is deleted when Kodi is stopped
689  const std::string m_tempAddonBasePath = "special://temp/addons";
690 
694  mutable std::string m_lastAvailableUpdatesCountAsString;
695  mutable std::mutex m_lastAvailableUpdatesCountMutex;
696 };
697 
698 }; /* namespace ADDON */
Class - CAddonMgr Holds references to all addons, enabled or otherwise.
Definition: AddonManager.h:79
const std::string & GetTempAddonBasePath()
Get the path where temporary add-on files are stored.
Definition: AddonManager.h:522
Definition: AddonVersion.h:27
Definition: EventStream.h:65
Class - IAddonMgrCallback This callback should be inherited by any class which manages specific addon...
Definition: IAddonManagerCallback.h:23
Definition: EventStream.h:88
Definition: Addon.cpp:39
Definition: Repository.h:23
Definition: EventStream.h:22