summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/binary-addons/BinaryAddonBase.cpp
blob: cea1586d99b047f2d13736ba64e26f3548f91544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "BinaryAddonBase.h"

#include "addons/addoninfo/AddonInfo.h"
#include "addons/binary-addons/AddonDll.h"
#include "addons/binary-addons/AddonInstanceHandler.h"
#include "utils/log.h"

#include <mutex>

using namespace ADDON;

const std::string& CBinaryAddonBase::ID() const
{
  return m_addonInfo->ID();
}

AddonDllPtr CBinaryAddonBase::GetAddon(IAddonInstanceHandler* handler)
{
  if (handler == nullptr)
  {
    CLog::Log(LOGERROR, "CBinaryAddonBase::{}: for Id '{}' called with empty instance handler",
              __FUNCTION__, ID());
    return nullptr;
  }

  std::unique_lock<CCriticalSection> lock(m_critSection);

  // If no 'm_activeAddon' is defined create it new.
  if (m_activeAddon == nullptr)
    m_activeAddon = std::make_shared<CAddonDll>(m_addonInfo, shared_from_this());

  // add the instance handler to the info to know used amount on addon
  m_activeAddonHandlers.insert(handler);

  return m_activeAddon;
}

void CBinaryAddonBase::ReleaseAddon(IAddonInstanceHandler* handler)
{
  if (handler == nullptr)
  {
    CLog::Log(LOGERROR, "CBinaryAddonBase::{}: for Id '{}' called with empty instance handler",
              __FUNCTION__, ID());
    return;
  }

  std::unique_lock<CCriticalSection> lock(m_critSection);

  auto presentHandler = m_activeAddonHandlers.find(handler);
  if (presentHandler == m_activeAddonHandlers.end())
    return;

  m_activeAddonHandlers.erase(presentHandler);

  // if no handler is present anymore reset and delete the add-on class on information
  if (m_activeAddonHandlers.empty())
  {
    m_activeAddon.reset();
  }
}

size_t CBinaryAddonBase::UsedInstanceCount() const
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  return m_activeAddonHandlers.size();
}

AddonDllPtr CBinaryAddonBase::GetActiveAddon()
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  return m_activeAddon;
}

void CBinaryAddonBase::OnPreInstall()
{
  const std::unordered_set<IAddonInstanceHandler*> activeAddonHandlers = m_activeAddonHandlers;
  for (const auto& instance : activeAddonHandlers)
    instance->OnPreInstall();
}

void CBinaryAddonBase::OnPostInstall(bool update, bool modal)
{
  const std::unordered_set<IAddonInstanceHandler*> activeAddonHandlers = m_activeAddonHandlers;
  for (const auto& instance : activeAddonHandlers)
    instance->OnPostInstall(update, modal);
}

void CBinaryAddonBase::OnPreUnInstall()
{
  const std::unordered_set<IAddonInstanceHandler*> activeAddonHandlers = m_activeAddonHandlers;
  for (const auto& instance : activeAddonHandlers)
    instance->OnPreUnInstall();
}

void CBinaryAddonBase::OnPostUnInstall()
{
  const std::unordered_set<IAddonInstanceHandler*> activeAddonHandlers = m_activeAddonHandlers;
  for (const auto& instance : activeAddonHandlers)
    instance->OnPostUnInstall();
}