summaryrefslogtreecommitdiff
path: root/python/interaction.py
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2022-09-26 23:12:20 -0400
committerGlenn Smith <glenn@vector35.com>2022-09-29 21:02:20 -0400
commit207925956bb8ca0174d40523fda70bfb5bc7bd38 (patch)
tree4d5c8cba9ee2ad2b061f1e420c681a793ad397b8 /python/interaction.py
parent5c8aa2ad6c2c03b37cacad4021f81310be12f375 (diff)
Interaction::RunProgressDialog
Diffstat (limited to 'python/interaction.py')
-rw-r--r--python/interaction.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/python/interaction.py b/python/interaction.py
index 2331b586..9fb5d9c7 100644
--- a/python/interaction.py
+++ b/python/interaction.py
@@ -21,7 +21,7 @@
import ctypes
import traceback
import webbrowser
-from typing import Optional
+from typing import Optional, Callable
# Binary Ninja components
from . import _binaryninjacore as core
@@ -29,6 +29,7 @@ from .enums import FormInputFieldType, MessageBoxIcon, MessageBoxButtonSet, Mess
from . import binaryview
from .log import log_error
from . import flowgraph
+from . import mainthread
class LabelField:
@@ -493,6 +494,7 @@ class InteractionHandler:
self._cb.getFormInput = self._cb.getFormInput.__class__(self._get_form_input)
self._cb.showMessageBox = self._cb.showMessageBox.__class__(self._show_message_box)
self._cb.openUrl = self._cb.openUrl.__class__(self._open_url)
+ self._cb.runProgressDialog = self._cb.runProgressDialog.__class__(self._run_progress_dialog)
def register(self):
self.__class__._interaction_handler = self
@@ -708,6 +710,17 @@ class InteractionHandler:
log_error(traceback.format_exc())
return False
+ def _run_progress_dialog(self, title, can_cancel, task, task_ctxt):
+ try:
+ def py_task(progress: Callable[[int, int], bool]):
+ progress_c = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, lambda ctxt, cur, max: progress(cur, max))
+ task(task_ctxt, progress_c, None)
+
+ return self.run_progress_dialog(title, can_cancel, py_task)
+ except:
+ log_error(traceback.format_exc())
+ return False
+
def show_plain_text_report(self, view, title, contents):
pass
@@ -762,6 +775,10 @@ class InteractionHandler:
webbrowser.open(url)
return True
+ def run_progress_dialog(self, task: Callable[[Callable[[int, int], bool]], None]) -> bool:
+ mainthread.execute_on_main_thread_and_wait(lambda: task(lambda cur, max: True))
+ return True
+
class PlainTextReport:
def __init__(self, title, contents, view=None):
@@ -1359,3 +1376,27 @@ def open_url(url):
:rtype: bool
"""
return core.BNOpenUrl(url)
+
+
+def run_progress_dialog(title: str, can_cancel: bool, task: Callable[[Callable[[int, int], bool]], None]) -> bool:
+ """
+ ``run_progress_dialog`` runs a given task in a background thread, showing an updating
+ progress bar which the user can cancel.
+
+ :param title: Dialog title
+ :param can_cancel: If the task can be cancelled
+ :param task: Function to perform the task, taking as a parameter a function which should
+ be called to report progress updates and check for cancellation. If the progress function
+ returns false, the user has requested to cancel, and the task should handle this appropriately.
+ :return: True if not cancelled
+ """
+
+ progress_type = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t)
+ task_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p, progress_type, ctypes.c_void_p)
+
+ def do_task(ctxt: ctypes.c_void_p, progress: progress_type, progress_ctxt: ctypes.c_void_p):
+ def py_progress(cur: int, max: int) -> bool:
+ return progress(progress_ctxt, cur, max)
+ task(py_progress)
+
+ return core.BNRunProgressDialog(title, can_cancel, task_type(do_task), None)