summaryrefslogtreecommitdiff
path: root/plugins/warp/ui/shared/fetcher.h
blob: ad38ed60198e4fd2ad2f49d7cd0d92db95e44b74 (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
#pragma once

#include <atomic>
#include <mutex>
#include <unordered_set>
#include <vector>
#include <functional>

#include "warp.h"
#include "binaryninjaapi.h"
#include "uitypes.h"

enum WarpFetchCompletionStatus
{
	KeepCallback,
	RemoveCallback,
};

// Responsible for fetching data from the containers, to later be queried from the container interface.
class WarpFetcher
{
	LoggerRef m_logger;

	std::mutex m_requestMutex;
	std::vector<FunctionRef> m_pendingRequests;
	std::unordered_set<Warp::FunctionGUID> m_processedGuids;

public:
	using CallbackId = uint64_t;
	using CompletionCallback = std::function<WarpFetchCompletionStatus()>;

	explicit WarpFetcher();

	// The global fetcher instance, this is used for the fetch dialog and the sidebar.
	static std::shared_ptr<WarpFetcher> Global();

	std::atomic<bool> m_requestInProgress = false;

	[[nodiscard]] CallbackId AddCompletionCallback(CompletionCallback cb)
	{
		std::lock_guard<std::mutex> lock(m_requestMutex);
		const CallbackId id = m_nextCallbackId++;
		m_completionCallbacks.emplace(id, std::move(cb));
		return id;
	}

	void RemoveCompletionCallback(CallbackId id)
	{
		std::lock_guard<std::mutex> lock(m_requestMutex);
		m_completionCallbacks.erase(id);
	}

	void AddPendingFunction(const FunctionRef& func);

	void FetchPendingFunctions(const std::vector<Warp::SourceTag>& allowedTags);

	void ClearProcessed();

private:
	// List of callbacks to call when done fetching data, assume that others are using this as well.
	std::atomic<CallbackId> m_nextCallbackId = 1;
	std::unordered_map<CallbackId, CompletionCallback> m_completionCallbacks;
	void ExecuteCompletionCallback();

	std::vector<FunctionRef> FlushPendingFunctions();
};