<commit_before>#include "ee/vungle/private/VungleBridge.hpp"

#include <ee/ads/internal/GuardedFullScreenAd.hpp>
#include <ee/ads/internal/IAsyncHelper.hpp>
#include <ee/ads/internal/MediationManager.hpp>
#include <ee/core/IMessageBridge.hpp>
#include <ee/core/Logger.hpp>
#include <ee/core/PluginManager.hpp>
#include <ee/core/SwitchToLibraryThread.hpp>
#include <ee/core/Task.hpp>
#include <ee/core/Thread.hpp>
#include <ee/core/Utils.hpp>
#include <ee/nlohmann/json.hpp>

#include "ee/vungle/private/VungleRewardedAd.hpp"

namespace ee {
namespace core {
template <>
std::shared_ptr<IVungle>
PluginManager::createPluginImpl(IMessageBridge& bridge) {
    if (not addPlugin(Plugin::Vungle)) {
        return nullptr;
    }
    return std::make_shared<vungle::Bridge>(bridge);
}
} // namespace core

namespace vungle {
namespace {
// clang-format off
const std::string kPrefix    = "VungleBridge";

const auto kInitialize     = kPrefix + "Initialize";

const auto kHasRewardedAd  = kPrefix + "HasRewardedAd";
const auto kLoadRewardedAd = kPrefix + "LoadRewardedAd";
const auto kShowRewardedAd = kPrefix + "ShowRewardedAd";

const auto kOnLoaded       = kPrefix + "OnLoaded";
const auto kOnFailedToLoad = kPrefix + "OnFailedToLoad";
const auto kOnFailedToShow = kPrefix + "OnFailedToShow";
const auto kOnClicked      = kPrefix + "OnClicked";
const auto kOnClosed       = kPrefix + "OnClosed";
// clang-format on
} // namespace

using Self = Bridge;

Self::Bridge(IMessageBridge& bridge)
    : bridge_(bridge)
    , logger_(Logger::getSystemLogger()) {
    logger_.debug(__PRETTY_FUNCTION__);
    auto&& mediation = ads::MediationManager::getInstance();
    displayer_ = mediation.getAdDisplayer();

    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onLoaded(json["ad_id"]);
            });
            return "";
        },
        kOnLoaded);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onFailedToLoad(json["ad_id"], json["message"]);
            });
            return "";
        },
        kOnFailedToLoad);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onFailedToShow(json["ad_id"], json["message"]);
            });
            return "";
        },
        kOnFailedToShow);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                onClicked(message);
            });
            return "";
        },
        kOnClicked);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onClosed(json["ad_id"], json["rewarded"]);
            });
            return "";
        },
        kOnClosed);
}

Self::~Bridge() = default;

void Self::destroy() {
    logger_.debug(__PRETTY_FUNCTION__);
    bridge_.deregisterHandler(kOnLoaded);
    bridge_.deregisterHandler(kOnFailedToLoad);
    bridge_.deregisterHandler(kOnFailedToShow);
    bridge_.deregisterHandler(kOnClosed);
    PluginManager::removePlugin(Plugin::Vungle);
}

Task<bool> Self::initialize(const std::string& appId) {
    logger_.debug("%s: appId = %s", __PRETTY_FUNCTION__, appId.c_str());<commit_msg>Fix to deregister handler.<commit_after>#include "ee/vungle/private/VungleBridge.hpp"

#include <ee/ads/internal/GuardedFullScreenAd.hpp>
#include <ee/ads/internal/IAsyncHelper.hpp>
#include <ee/ads/internal/MediationManager.hpp>
#include <ee/core/IMessageBridge.hpp>
#include <ee/core/Logger.hpp>
#include <ee/core/PluginManager.hpp>
#include <ee/core/SwitchToLibraryThread.hpp>
#include <ee/core/Task.hpp>
#include <ee/core/Thread.hpp>
#include <ee/core/Utils.hpp>
#include <ee/nlohmann/json.hpp>

#include "ee/vungle/private/VungleRewardedAd.hpp"

namespace ee {
namespace core {
template <>
std::shared_ptr<IVungle>
PluginManager::createPluginImpl(IMessageBridge& bridge) {
    if (not addPlugin(Plugin::Vungle)) {
        return nullptr;
    }
    return std::make_shared<vungle::Bridge>(bridge);
}
} // namespace core

namespace vungle {
namespace {
// clang-format off
const std::string kPrefix    = "VungleBridge";

const auto kInitialize     = kPrefix + "Initialize";

const auto kHasRewardedAd  = kPrefix + "HasRewardedAd";
const auto kLoadRewardedAd = kPrefix + "LoadRewardedAd";
const auto kShowRewardedAd = kPrefix + "ShowRewardedAd";

const auto kOnLoaded       = kPrefix + "OnLoaded";
const auto kOnFailedToLoad = kPrefix + "OnFailedToLoad";
const auto kOnFailedToShow = kPrefix + "OnFailedToShow";
const auto kOnClicked      = kPrefix + "OnClicked";
const auto kOnClosed       = kPrefix + "OnClosed";
// clang-format on
} // namespace

using Self = Bridge;

Self::Bridge(IMessageBridge& bridge)
    : bridge_(bridge)
    , logger_(Logger::getSystemLogger()) {
    logger_.debug(__PRETTY_FUNCTION__);
    auto&& mediation = ads::MediationManager::getInstance();
    displayer_ = mediation.getAdDisplayer();

    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onLoaded(json["ad_id"]);
            });
            return "";
        },
        kOnLoaded);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onFailedToLoad(json["ad_id"], json["message"]);
            });
            return "";
        },
        kOnFailedToLoad);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onFailedToShow(json["ad_id"], json["message"]);
            });
            return "";
        },
        kOnFailedToShow);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                onClicked(message);
            });
            return "";
        },
        kOnClicked);
    bridge_.registerHandler(
        [this](const std::string& message) {
            Thread::runOnLibraryThread([this, message] { //
                auto json = nlohmann::json::parse(message);
                onClosed(json["ad_id"], json["rewarded"]);
            });
            return "";
        },
        kOnClosed);
}

Self::~Bridge() = default;

void Self::destroy() {
    logger_.debug(__PRETTY_FUNCTION__);
    bridge_.deregisterHandler(kOnLoaded);
    bridge_.deregisterHandler(kOnFailedToLoad);
    bridge_.deregisterHandler(kOnFailedToShow);
    bridge_.deregisterHandler(kOnClicked);
    bridge_.deregisterHandler(kOnClosed);
    PluginManager::removePlugin(Plugin::Vungle);
}

Task<bool> Self::initialize(const std::string& appId) {
    logger_.debug("%s: appId = %s", __PRETTY_FUNCTION__, appId.c_str());
{'fraction_non_alphanumeric': 0.1374363100026817, 'fraction_numerical': 0.0, 'mean_word_length': 3.0340724716062737, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4031143', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2221, 'n_words': 387}