#ifndef BW_CEF_BW_HANDLE_MAP
#define BW_CEF_BW_HANDLE_MAP
#include "../browser_window.h"
#include <include/cef_browser.h>
#include <optional>
#include <map>
#include <mutex>
namespace bw {
struct OnCreateCallback {
bw_BrowserWindowCreationCallbackFn callback;
void* data;
};
struct BrowserInfo {
bw_BrowserWindow* handle;
std::optional<OnCreateCallback> callback;
};
class HandleMap {
std::map<int, BrowserInfo> map;
std::mutex mutex;
public:
HandleMap() {}
void drop( CefRefPtr<CefBrowser> cef_handle ) {
this->mutex.lock();
this->map.erase(cef_handle->GetIdentifier());
this->mutex.unlock();
}
void store(CefRefPtr<CefBrowser> cef_handle, bw_BrowserWindow* our_handle, bw_BrowserWindowCreationCallbackFn callback, void* callback_data) {
BrowserInfo& bw_info = this->map[cef_handle->GetIdentifier()];
bw_info.handle = our_handle;
OnCreateCallback occ = {
callback,
callback_data
};
bw_info.callback = std::optional(occ);
this->mutex.unlock();
}
std::optional<BrowserInfo*> fetch( CefRefPtr<CefBrowser> cef_handle ) {
this->mutex.lock();
auto it = this->map.find( cef_handle->GetIdentifier() );
if ( it == this->map.end() )
return std::optional<BrowserInfo*>();
std::optional<BrowserInfo*> result(&(*it).second);
this->mutex.unlock();
return result;
}
};
extern HandleMap bw_handle_map;
}
#endif