#pragma once #include "bookmarks.h" #include "file_server.h" #include "image_renderer.h" #include "include/cef_client.h" #include "include/cef_dialog_handler.h" #include "include/cef_display_handler.h" #include "include/cef_download_handler.h" #include "include/cef_find_handler.h" #include "include/cef_jsdialog_handler.h" #include "include/cef_render_handler.h" #include "include/cef_request_handler.h" #include "include/cef_resource_request_handler.h" #include "passwords.h" #include "status_bar.h" #include "user_scripts.h" #include #include #include #include #include #include class InputHandler; // Forward declaration class TabManager; // Forward declaration class BrowserClient : public CefClient, public CefRenderHandler, public CefLifeSpanHandler, public CefLoadHandler, public CefDisplayHandler, public CefJSDialogHandler, public CefDownloadHandler, public CefDialogHandler, public CefRequestHandler, public CefResourceRequestHandler, public CefFindHandler { public: struct DownloadEntry { int32_t id; std::string filename; std::string url; std::string full_path; int64_t total_bytes; int64_t received_bytes; int percent_complete; int64_t speed; bool is_complete; bool is_canceled; bool is_in_progress; }; BrowserClient(int width, int height, int cell_width, int cell_height, GraphicsBackend backend, int fb_offset_x, int fb_offset_y); CefRefPtr GetRenderHandler() override; CefRefPtr GetLifeSpanHandler() override; CefRefPtr GetLoadHandler() override; CefRefPtr GetDisplayHandler() override; CefRefPtr GetJSDialogHandler() override; CefRefPtr GetDownloadHandler() override; CefRefPtr GetDialogHandler() override; CefRefPtr GetRequestHandler() override; CefRefPtr GetResourceRequestHandler( CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool is_navigation, bool is_download, const CefString &request_initiator, bool &disable_default_handling) override; cef_return_value_t OnBeforeResourceLoad( CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr callback) override; CefRefPtr GetFindHandler() override { return this; } virtual void GetViewRect(CefRefPtr browser, CefRect &rect) override; virtual void OnPaint(CefRefPtr browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height) override; virtual void OnAfterCreated(CefRefPtr browser) override; virtual void OnBeforeClose(CefRefPtr browser) override; virtual bool OnBeforePopup( CefRefPtr browser, CefRefPtr frame, int popup_id, const CefString &target_url, const CefString &target_frame_name, CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture, const CefPopupFeatures &popupFeatures, CefWindowInfo &windowInfo, CefRefPtr &client, CefBrowserSettings &settings, CefRefPtr &extra_info, bool *no_javascript_access) override; virtual void OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) override; virtual void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString &errorText, const CefString &failedUrl) override; virtual bool OnConsoleMessage(CefRefPtr browser, cef_log_severity_t level, const CefString &message, const CefString &source, int line) override; virtual void OnTitleChange(CefRefPtr browser, const CefString &title) override; // CefJSDialogHandler methods virtual bool OnJSDialog(CefRefPtr browser, const CefString &origin_url, CefJSDialogHandler::JSDialogType dialog_type, const CefString &message_text, const CefString &default_prompt_text, CefRefPtr callback, bool &suppress_message) override; // CefDialogHandler methods virtual bool OnFileDialog(CefRefPtr browser, CefDialogHandler::FileDialogMode mode, const CefString &title, const CefString &default_file_path, const std::vector &accept_filters, const std::vector &accept_extensions, const std::vector &accept_descriptions, CefRefPtr callback) override; // CefRequestHandler method virtual bool GetAuthCredentials(CefRefPtr browser, const CefString &origin_url, bool isProxy, const CefString &host, int port, const CefString &realm, const CefString &scheme, CefRefPtr callback) override; // CefFindHandler methods void OnFindResult(CefRefPtr browser, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate) override; CefRefPtr GetBrowser() { return browser_; } bool IsClosing() const { return is_closing_; } // Tab support: only the active tab renders to /dev/fb0 and receives input. void setActive(bool a) { active_ = a; } bool isActive() const { return active_; } void setTabManager(TabManager *t) { tab_manager_ = t; } StatusBar *GetStatusBar() { return status_bar_.get(); } void Resize(int width, int height, int cell_width, int cell_height, int fb_offset_x, int fb_offset_y); void SetTiledRenderingEnabled(bool enabled) { if (renderer_) renderer_->setTiledRenderingEnabled(enabled); } void ForceFullRedraw() { if (renderer_) { renderer_->forceFullRender(); } } // Re-blit the last cached CEF frame onto /dev/fb0. Used after a tmux pane // switch: tmux repaints the window from its own cell buffer, which wipes the // pixels we blit directly onto the framebuffer and never calls OnPaint again // for a static page. void RedrawLastFrame() { std::lock_guard lock(render_mutex_); if (active_ && renderer_ && !last_frame_.empty() && last_width_ > 0 && last_height_ > 0) { renderer_->forceFullRender(); renderer_->render(last_frame_.data(), last_width_, last_height_, false); } } // Milliseconds since the last OnPaint frame, -1 if never painted. int msSinceLastPaint() const { if (last_paint_time_.time_since_epoch().count() == 0) { return -1; } return std::chrono::duration_cast( std::chrono::steady_clock::now() - last_paint_time_) .count(); } bool HandleSelectNavigation(int direction); // Returns true if handled bool HandleSelectConfirm(); // Returns true if handled bool IsSelectOptionsActive() const { return !current_options_.empty(); } void SetUrlInputActive(bool active) { url_input_active_ = active; } void SetConsoleActive(bool active) { console_active_ = active; } void SetSearchActive(bool active) { search_active_ = active; } void setInputHandler(InputHandler* handler) { input_handler_ = handler; } bool IsConsoleActive() const { return console_active_; } const std::vector &GetConsoleLogs() const { return console_logs_; } void ClearConsoleLogs() { console_logs_.clear(); } void SetShowInternalConsoleLogs(bool show) { show_internal_console_logs_ = show; } void ExecuteJavaScript(const std::string &code); void SetPopupConfirmActive(bool active, const std::string &url = ""); bool IsPopupConfirmActive() const { return popup_confirm_active_; } const std::string &GetPopupUrl() const { return popup_url_; } void HandlePopupResponse(bool accept); void HandleJSDialogResponse(bool success, const std::string &input = ""); bool IsJSDialogActive() const { return js_dialog_active_; } CefJSDialogHandler::JSDialogType GetJSDialogType() const { return js_dialog_type_; } const std::string &GetJSDialogMessage() const { return js_dialog_message_; } const std::string &GetJSDialogPromptDefault() const { return js_dialog_prompt_default_; } // HTTP Basic Auth handling void SetAuthDialogActive(bool active) { auth_dialog_active_ = active; } bool IsAuthDialogActive() const { return auth_dialog_active_; } const std::string &GetAuthRealm() const { return auth_realm_; } void HandleAuthResponse(bool accept, const std::string &username = "", const std::string &password = ""); // File dialog handling (CEF OnFileDialog callback) void HandleFileDialogResponse(const std::string &file_path); // File chooser popup (Firefox-style dialog). Shared by the JS-interceptor // upload flow (for_upload=true) and native CEF file dialogs (for_upload= // false). State lives here; file_chooser.js only renders. void OpenFileChooser(bool for_upload); bool IsFileChooserActive() const { return file_chooser_active_; } void FileChooserNavigate(int delta); void FileChooserTop(); void FileChooserBottom(); void FileChooserEnter(); void FileChooserParent(); void CancelFileChooser(); // File upload handling (JS interceptor) void HandleFileUpload(const std::string &file_path); bool IsFileUploadPending() const { return file_upload_pending_; } void CancelFileUpload(); // CefDownloadHandler methods virtual bool OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item, const CefString &suggested_name, CefRefPtr callback) override; virtual void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) override; bool IsDownloadConfirmActive() const { return download_confirm_active_; } const std::string &GetDownloadFilename() const { return download_filename_; } const std::string &GetDownloadUrl() const { return download_url_; } void HandleDownloadResponse(bool accept, const std::string &path = ""); // Download Manager void ToggleDownloadManager(); bool IsDownloadManagerActive() const { return download_manager_active_; } void HandleDownloadManagerNavigation(int direction); void HandleDownloadManagerAction(char action); // Bookmarks void AddCurrentPageToBookmarks(); void SetBookmarksActive(bool active); bool IsBookmarksActive() const { return bookmarks_active_; } bool HandleBookmarkNavigation(int direction); bool HandleBookmarkConfirm(); bool HandleBookmarkDelete(); BookmarksManager *GetBookmarksManager() { return &bookmarks_manager_; } // Password autofill void SetPasswordsActive(bool active); bool IsPasswordsActive() const { return passwords_active_; } bool HandlePasswordNavigation(int direction); bool HandlePasswordConfirm(); bool HandlePasswordDelete(); // User Scripts void SetUserScriptsActive(bool active); bool IsUserScriptsActive() const { return user_scripts_active_; } bool HandleUserScriptNavigation(int direction); bool HandleUserScriptConfirm(); void ToggleAutoInjectUserScripts(); void InjectUserScriptsForCurrentPage(); UserScriptsManager *GetUserScriptsManager() { return &user_scripts_manager_; } // Coordinate Click Mode (click via x,y coordinates) void ActivateCoordinateMode(); void SetCoordinateModeActive(bool active); bool IsCoordinateModeActive() const { return coordinate_mode_active_; } void HandleCoordinateClick(int x, int y); void PlaceCoordinateCursor(int col, int row); void SetCoordinateHighlight(int col, int row); void HandleHintClick(int x, int y); void HandleFileInputClick(int x, int y); int GetViewportWidth() const { return width_; } int GetViewportHeight() const { return height_; } // Visual Mode (text selection) void ActivateVisualMode(); void SetVisualModeActive(bool active); bool IsVisualModeActive() const { return visual_mode_active_; } void HandleVisualModeKey(const std::string &key); // Clipboard operations void CopyCurrentURL(); // Mode switch request (from mouse click detection) bool GetModeSwitchRequest() const { return mode_switch_requested_; } bool GetSwitchToInsertMode() const { return switch_to_insert_mode_; } void ClearModeSwitchRequest() { mode_switch_requested_ = false; switch_to_insert_mode_ = false; } // Input mode display void SetInputMode(const char *mode) { input_mode_ = mode; // Update status bar with current title and new mode if (status_bar_ && !current_page_title_.empty()) { status_bar_->showTitle(current_page_title_, input_mode_); } } private: int width_; int height_; int cell_width_; int cell_height_; GraphicsBackend backend_; int fb_offset_x_; int fb_offset_y_; CefRefPtr browser_; std::unique_ptr renderer_; std::unique_ptr status_bar_; InputHandler* input_handler_ = nullptr; TabManager* tab_manager_ = nullptr; bool active_ = false; std::atomic is_closing_; std::mutex render_mutex_; // Synchronize rendering and status updates // Cached copy of the last OnPaint frame so it can be re-blitted after a // tmux pane switch wipes the framebuffer. std::vector last_frame_; int last_width_ = 0; int last_height_ = 0; std::chrono::steady_clock::time_point last_paint_time_; // Track current select element state std::vector current_options_; int current_selected_index_; bool url_input_active_ = false; bool console_active_ = false; bool search_active_ = false; int search_match_count_ = 0; int search_active_match_ = 0; std::vector console_logs_; std::mutex console_mutex_; bool show_internal_console_logs_ = false; // Popup window confirmation bool popup_confirm_active_ = false; std::string popup_url_; // JS dialog handling bool js_dialog_active_ = false; CefJSDialogHandler::JSDialogType js_dialog_type_; std::string js_dialog_message_; std::string js_dialog_prompt_default_; CefRefPtr js_dialog_callback_; std::mutex js_dialog_mutex_; // File dialog handling (CEF OnFileDialog callback) bool file_input_active_ = false; CefRefPtr file_dialog_callback_; std::mutex file_dialog_mutex_; // File chooser popup state struct FileChooserEntry { std::string name; bool is_dir; }; bool file_chooser_active_ = false; std::string fc_dir_; std::vector fc_entries_; int fc_selected_ = 0; int fc_scroll_ = 0; static constexpr int fc_view_rows_ = 15; // must match VIEW_ROWS in file_chooser.js bool fc_for_upload_ = true; std::vector fc_accept_ext_; void FcScanDirectory(); void FcSendToJs(); // File upload handling (JS interceptor bypasses CEF file dialog) bool file_upload_active_ = false; bool file_upload_pending_ = false; // true while waiting for user to type path std::mutex file_upload_mutex_; FileServer file_server_; // serves file via HTTP for large uploads // HTTP Basic Auth handling bool auth_dialog_active_ = false; std::string auth_realm_; CefRefPtr auth_callback_; std::mutex auth_mutex_; // Download handling bool download_confirm_active_ = false; std::string download_filename_; std::string download_url_; CefRefPtr download_callback_; std::mutex download_mutex_; // Download manager std::vector downloads_list_; bool download_manager_active_ = false; int download_manager_selected_index_ = 0; // Bookmarks handling bool bookmarks_active_ = false; int bookmarks_selected_index_ = 0; BookmarksManager bookmarks_manager_; std::string current_page_title_; // Password autofill handling bool passwords_active_ = false; int passwords_selected_index_ = 0; PasswordManager passwords_manager_; std::vector passwords_sorted_; // entries sorted for current page void ShowPasswordsList(); void FillCredentials(const PasswordEntry &entry); void RunOneTimeImports(); // User Scripts handling bool user_scripts_active_ = false; int user_scripts_selected_index_ = 0; UserScriptsManager user_scripts_manager_; // Coordinate click mode handling bool coordinate_mode_active_ = false; int coordinate_count_ = 0; // Input mode switch handling (auto-insert when an input field is focused) bool mode_switch_requested_ = false; bool switch_to_insert_mode_ = false; // Visual mode handling bool visual_mode_active_ = false; bool first_load_complete_ = false; bool force_next_paint_ = false; int paint_count_ = 0; // Input mode display const char *input_mode_ = "S"; // Parsed Chromium major version (e.g. "143") used to spoof UA client hints std::string chrome_major_; void injectSelectDetector(); void injectFileUploadInterceptor(); void parseSelectMessage(const std::string &message); IMPLEMENT_REFCOUNTING(BrowserClient); };