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 
432  bool IsCompatible(const IAddon& addon) const;
433 
440  bool IsCompatible(const AddonInfoPtr& addonInfo) const;
441 
446  std::vector<DependencyInfo> GetDepsRecursive(const std::string& id,
447  OnlyEnabledRootAddon onlyEnabledRootAddon);
448 
462  bool GetAddonInfos(std::vector<AddonInfoPtr>& addonInfos, bool onlyEnabled, AddonType type) const;
463 
475  std::vector<AddonInfoPtr> GetAddonInfos(bool onlyEnabled,
476  const std::vector<AddonType>& types) const;
477 
489  bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos, AddonType type) const;
490 
507  bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos,
508  AddonType type,
509  AddonDisabledReason disabledReason) const;
510 
511  const AddonInfoPtr GetAddonInfo(const std::string& id, AddonType type) const;
512 
520  const std::string& GetTempAddonBasePath() { return m_tempAddonBasePath; }
521 
522  AddonOriginType GetAddonOriginType(const AddonPtr& addon) const;
523 
530  bool IsAddonDisabledWithReason(const std::string& ID, AddonDisabledReason disabledReason) const;
531 
540 
560  bool SetAddonOrigin(const std::string& addonId, const std::string& repoAddonId, bool isUpdate);
561 
575  bool AddonsFromRepoXML(const RepositoryDirInfo& repo,
576  const std::string& xml,
577  std::vector<AddonInfoPtr>& addons);
578 
586  std::map<std::string, AddonWithUpdate> GetAddonsWithAvailableUpdate() const;
587 
593  std::vector<std::shared_ptr<IAddon>> GetCompatibleVersions(const std::string& addonId) const;
594 
601  const std::string& GetLastAvailableUpdatesCountAsString() const;
602 
607  std::vector<std::shared_ptr<IAddon>> GetOrphanedDependencies() const;
608 
609 private:
610  CAddonMgr& operator=(CAddonMgr const&) = delete;
611 
612  VECADDONS m_updateableAddons;
613 
621  std::vector<std::shared_ptr<IAddon>> GetAvailableUpdatesOrOutdatedAddons(
622  AddonCheckType addonCheckType) const;
623 
624  bool GetAddonsInternal(AddonType type,
625  VECADDONS& addons,
626  OnlyEnabled onlyEnabled,
627  CheckIncompatible checkIncompatible) const;
628 
629  bool EnableSingle(const std::string& id);
630 
631  void FindAddons(ADDON_INFO_LIST& addonmap, const std::string& path);
632 
641  bool GetIncompatibleAddonInfos(std::vector<AddonInfoPtr>& incompatible,
642  bool includeDisabled) const;
643 
649  bool GetAddonUpdateCandidates(VECADDONS& updates) const;
650 
655  void SortByDependencies(VECADDONS& updates) const;
656 
664  void InstallAddonUpdates(VECADDONS& updates,
665  bool wait,
666  AllowCheckForUpdates allowCheckForUpdates) const;
667 
668  // This guards the addon installation process to make sure
669  // addon updates are not installed concurrently
670  // while the migration is running. Addon updates can be triggered
671  // as a result of a repository update event.
672  // (migration will install any available update anyway)
673  mutable std::mutex m_installAddonsMutex;
674 
675  std::map<std::string, AddonDisabledReason> m_disabled;
676  static std::map<AddonType, IAddonMgrCallback*> m_managers;
677  mutable CCriticalSection m_critSection;
678  std::unique_ptr<CAddonDatabase> m_database;
679  std::unique_ptr<CAddonUpdateRules> m_updateRules;
680  CEventSource<AddonEvent> m_events;
681  CBlockingEventSource<AddonEvent> m_unloadEvents;
682  std::set<std::string> m_systemAddons;
683  std::set<std::string> m_optionalSystemAddons;
684  ADDON_INFO_LIST m_installedAddons;
685 
686  // Temporary path given to add-ons, whose content is deleted when Kodi is stopped
687  const std::string m_tempAddonBasePath = "special://temp/addons";
688 
692  mutable std::string m_lastAvailableUpdatesCountAsString;
693  mutable std::mutex m_lastAvailableUpdatesCountMutex;
694 };
695 
696 }; /* 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:520
Definition: AddonVersion.h:27
Definition: EventStream.h:65
Definition: IAddon.h:72
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