///////////////////////////////////////////////////////////////////////////// // Name: src/gtk/webview_webkit2.cpp // Purpose: GTK WebKit2 backend for web view component // Author: Scott Talbert // Copyright: (c) 2017 Scott Talbert // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT2 #include "wx/stockitem.h" #include "wx/gtk/webview_webkit.h" #include "wx/gtk/control.h" #include "wx/gtk/private.h" #include "wx/filesys.h" #include "wx/base64.h" #include "wx/log.h" #include "wx/gtk/private/webview_webkit2_extension.h" #include // ---------------------------------------------------------------------------- // GTK callbacks // ---------------------------------------------------------------------------- extern "C" { static void wxgtk_webview_webkit_load_changed(GtkWidget *, WebKitLoadEvent load_event, wxWebViewWebKit *webKitCtrl) { wxString url = webKitCtrl->GetCurrentURL(); wxString target; // TODO: get target (if possible) if (load_event == WEBKIT_LOAD_FINISHED) { webKitCtrl->m_busy = false; wxWebViewEvent event(wxEVT_WEBVIEW_LOADED, webKitCtrl->GetId(), url, target); webKitCtrl->HandleWindowEvent(event); } else if (load_event == WEBKIT_LOAD_COMMITTED) { webKitCtrl->m_busy = true; wxWebViewEvent event(wxEVT_WEBVIEW_NAVIGATED, webKitCtrl->GetId(), url, target); webKitCtrl->HandleWindowEvent(event); } } static gboolean wxgtk_webview_webkit_navigation(WebKitWebView *web_view, WebKitPolicyDecision *decision, wxWebViewWebKit *webKitCtrl) { WebKitNavigationPolicyDecision* navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION(decision); WebKitNavigationAction* action = webkit_navigation_policy_decision_get_navigation_action(navigation_decision); WebKitURIRequest* request = webkit_navigation_action_get_request(action); const gchar* uri = webkit_uri_request_get_uri(request); wxString target = webkit_navigation_policy_decision_get_frame_name(navigation_decision); //If m_creating is true then we are the result of a new window //and so we need to send the event and veto the load if(webKitCtrl->m_creating) { webKitCtrl->m_creating = false; wxWebViewEvent event(wxEVT_WEBVIEW_NEWWINDOW, webKitCtrl->GetId(), wxString(uri, wxConvUTF8), target); webKitCtrl->HandleWindowEvent(event); webkit_policy_decision_ignore(decision); return TRUE; } if(webKitCtrl->m_guard) { webKitCtrl->m_guard = false; //We set this to make sure that we don't try to load the page again from //the resource request callback webKitCtrl->m_vfsurl = webkit_web_view_get_uri(web_view); webkit_policy_decision_use(decision); return FALSE; } webKitCtrl->m_busy = true; wxWebViewEvent event(wxEVT_WEBVIEW_NAVIGATING, webKitCtrl->GetId(), wxString( uri, wxConvUTF8 ), target); webKitCtrl->HandleWindowEvent(event); if (!event.IsAllowed()) { webKitCtrl->m_busy = false; webkit_policy_decision_ignore(decision); return TRUE; } else { wxString wxuri = uri; wxSharedPtr handler; wxVector > handlers = webKitCtrl->GetHandlers(); //We are not vetoed so see if we match one of the additional handlers for(wxVector >::iterator it = handlers.begin(); it != handlers.end(); ++it) { if(wxuri.substr(0, (*it)->GetName().length()) == (*it)->GetName()) { handler = (*it); } } //If we found a handler we can then use it to load the file directly //ourselves if(handler) { webKitCtrl->m_guard = true; wxFSFile* file = handler->GetFile(wxuri); if(file) { webKitCtrl->SetPage(*file->GetStream(), wxuri); } //We need to throw some sort of error here if file is NULL webkit_policy_decision_ignore(decision); return TRUE; } return FALSE; } } static gboolean wxgtk_webview_webkit_load_failed(WebKitWebView *, WebKitLoadEvent, gchar *uri, GError *error, wxWebViewWebKit* webKitWindow) { webKitWindow->m_busy = false; wxWebViewNavigationError type = wxWEBVIEW_NAV_ERR_OTHER; wxString description(error->message, wxConvUTF8); if (strcmp(g_quark_to_string(error->domain), "soup_http_error_quark") == 0) { switch (error->code) { case SOUP_STATUS_CANCELLED: type = wxWEBVIEW_NAV_ERR_USER_CANCELLED; break; case SOUP_STATUS_CANT_RESOLVE: case SOUP_STATUS_NOT_FOUND: type = wxWEBVIEW_NAV_ERR_NOT_FOUND; break; case SOUP_STATUS_CANT_RESOLVE_PROXY: case SOUP_STATUS_CANT_CONNECT: case SOUP_STATUS_CANT_CONNECT_PROXY: case SOUP_STATUS_SSL_FAILED: case SOUP_STATUS_IO_ERROR: type = wxWEBVIEW_NAV_ERR_CONNECTION; break; case SOUP_STATUS_MALFORMED: type = wxWEBVIEW_NAV_ERR_REQUEST; break; case SOUP_STATUS_BAD_REQUEST: type = wxWEBVIEW_NAV_ERR_REQUEST; break; case SOUP_STATUS_UNAUTHORIZED: case SOUP_STATUS_FORBIDDEN: type = wxWEBVIEW_NAV_ERR_AUTH; break; case SOUP_STATUS_METHOD_NOT_ALLOWED: case SOUP_STATUS_NOT_ACCEPTABLE: type = wxWEBVIEW_NAV_ERR_SECURITY; break; case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED: type = wxWEBVIEW_NAV_ERR_AUTH; break; case SOUP_STATUS_REQUEST_TIMEOUT: type = wxWEBVIEW_NAV_ERR_CONNECTION; break; case SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE: case SOUP_STATUS_REQUEST_URI_TOO_LONG: case SOUP_STATUS_UNSUPPORTED_MEDIA_TYPE: type = wxWEBVIEW_NAV_ERR_REQUEST; break; case SOUP_STATUS_BAD_GATEWAY: case SOUP_STATUS_SERVICE_UNAVAILABLE: case SOUP_STATUS_GATEWAY_TIMEOUT: type = wxWEBVIEW_NAV_ERR_CONNECTION; break; case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED: type = wxWEBVIEW_NAV_ERR_REQUEST; break; } } else if (strcmp(g_quark_to_string(error->domain), "webkit-network-error-quark") == 0) { switch (error->code) { case WEBKIT_NETWORK_ERROR_UNKNOWN_PROTOCOL: type = wxWEBVIEW_NAV_ERR_REQUEST; break; case WEBKIT_NETWORK_ERROR_CANCELLED: type = wxWEBVIEW_NAV_ERR_USER_CANCELLED; break; case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST: type = wxWEBVIEW_NAV_ERR_NOT_FOUND; break; } } else if (strcmp(g_quark_to_string(error->domain), "webkit-policy-error-quark") == 0) { switch (error->code) { case WEBKIT_POLICY_ERROR_CANNOT_USE_RESTRICTED_PORT: type = wxWEBVIEW_NAV_ERR_SECURITY; break; } } wxWebViewEvent event(wxEVT_WEBVIEW_ERROR, webKitWindow->GetId(), uri, ""); event.SetString(description); event.SetInt(type); webKitWindow->HandleWindowEvent(event); return FALSE; } static gboolean wxgtk_webview_webkit_new_window(WebKitPolicyDecision *decision, wxWebViewWebKit *webKitCtrl) { WebKitNavigationPolicyDecision* navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION(decision); WebKitNavigationAction* action = webkit_navigation_policy_decision_get_navigation_action(navigation_decision); WebKitURIRequest* request = webkit_navigation_action_get_request(action); const gchar* uri = webkit_uri_request_get_uri(request); wxString target = webkit_navigation_policy_decision_get_frame_name(navigation_decision); wxWebViewEvent event(wxEVT_WEBVIEW_NEWWINDOW, webKitCtrl->GetId(), wxString( uri, wxConvUTF8 ), target); webKitCtrl->HandleWindowEvent(event); //We always want the user to handle this themselves webkit_policy_decision_ignore(decision); return TRUE; } static gboolean wxgtk_webview_webkit_decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision, WebKitPolicyDecisionType type, wxWebViewWebKit *webKitCtrl) { switch (type) { case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION: return wxgtk_webview_webkit_navigation(web_view, decision, webKitCtrl); case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION: return wxgtk_webview_webkit_new_window(decision, webKitCtrl); default: return FALSE; } } static void wxgtk_webview_webkit_title_changed(GtkWidget* widget, GParamSpec *, wxWebViewWebKit *webKitCtrl) { gchar *title; g_object_get(G_OBJECT(widget), "title", &title, NULL); wxWebViewEvent event(wxEVT_WEBVIEW_TITLE_CHANGED, webKitCtrl->GetId(), webKitCtrl->GetCurrentURL(), ""); event.SetString(wxString(title, wxConvUTF8)); webKitCtrl->HandleWindowEvent(event); g_free(title); } static void wxgtk_webview_webkit_resource_req(WebKitWebView *, WebKitWebResource *, WebKitURIRequest *request, wxWebViewWebKit *webKitCtrl) { wxString uri = webkit_uri_request_get_uri(request); wxSharedPtr handler; wxVector > handlers = webKitCtrl->GetHandlers(); //We are not vetoed so see if we match one of the additional handlers for(wxVector >::iterator it = handlers.begin(); it != handlers.end(); ++it) { if(uri.substr(0, (*it)->GetName().length()) == (*it)->GetName()) { handler = (*it); } } //If we found a handler we can then use it to load the file directly //ourselves if(handler) { //If it is requsting the page itself then return as we have already //loaded it from the archive if(webKitCtrl->m_vfsurl == uri) return; wxFSFile* file = handler->GetFile(uri); if(file) { //We load the data into a data url to save it being written out again size_t size = file->GetStream()->GetLength(); char *buffer = new char[size]; file->GetStream()->Read(buffer, size); wxString data = wxBase64Encode(buffer, size); delete[] buffer; wxString mime = file->GetMimeType(); wxString path = "data:" + mime + ";base64," + data; //Then we can redirect the call webkit_uri_request_set_uri(request, path.utf8_str()); } } } static gboolean wxgtk_webview_webkit_context_menu(WebKitWebView *, WebKitContextMenu *, GdkEvent *, WebKitHitTestResult *, wxWebViewWebKit *webKitCtrl) { if(webKitCtrl->IsContextMenuEnabled()) return FALSE; else return TRUE; } static WebKitWebView* wxgtk_webview_webkit_create_webview(WebKitWebView *web_view, WebKitNavigationAction *, wxWebViewWebKit *webKitCtrl) { //As we do not know the uri being loaded at this point allow the load to //continue and catch it in navigation-policy-decision-requested webKitCtrl->m_creating = true; return web_view; } static void wxgtk_webview_webkit_counted_matches(WebKitFindController *, guint match_count, int *findCount) { *findCount = match_count; } static void wxgtk_initialize_web_extensions(WebKitWebContext *context, GDBusServer *dbusServer) { const char *address = g_dbus_server_get_client_address(dbusServer); GVariant *user_data = g_variant_new("(s)", address); webkit_web_context_set_web_extensions_directory(context, WX_WEB_EXTENSIONS_DIRECTORY); webkit_web_context_set_web_extensions_initialization_user_data(context, user_data); } static gboolean wxgtk_new_connection_cb(GDBusServer *, GDBusConnection *connection, GDBusProxy **proxy) { GError *error = NULL; GDBusProxyFlags flags = static_cast(static_cast(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) | static_cast(G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS)); *proxy = g_dbus_proxy_new_sync(connection, flags, NULL, NULL, WXGTK_WEB_EXTENSION_OBJECT_PATH, WXGTK_WEB_EXTENSION_INTERFACE, NULL, &error); if (error) { g_warning("Failed to create dbus proxy: %s", error->message); g_error_free(error); } return TRUE; } gboolean wxgtk_dbus_peer_is_authorized(GCredentials *peer_credentials) { static GCredentials *own_credentials = g_credentials_new(); GError *error = NULL; if (peer_credentials && g_credentials_is_same_user(peer_credentials, own_credentials, &error)) { return TRUE; } if (error) { g_warning("Failed to authorize web extension connection: %s", error->message); g_error_free(error); } return FALSE; } static gboolean wxgtk_authorize_authenticated_peer_cb(GDBusAuthObserver *, GIOStream *, GCredentials *credentials, wxWebViewWebKit *) { return wxgtk_dbus_peer_is_authorized(credentials); } } // extern "C" //----------------------------------------------------------------------------- // wxWebViewWebKit //----------------------------------------------------------------------------- wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView); wxWebViewWebKit::wxWebViewWebKit() { m_web_view = NULL; m_dbusServer = NULL; m_extension = NULL; } bool wxWebViewWebKit::Create(wxWindow *parent, wxWindowID id, const wxString &url, const wxPoint& pos, const wxSize& size, long style, const wxString& name) { m_web_view = NULL; m_dbusServer = NULL; m_extension = NULL; m_busy = false; m_guard = false; m_creating = false; FindClear(); // We currently unconditionally impose scrolling in both directions as it's // necessary to show arbitrary pages. style |= wxHSCROLL | wxVSCROLL; if (!PreCreation( parent, pos, size ) || !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) { wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") ); return false; } SetupWebExtensionServer(); g_signal_connect_after(webkit_web_context_get_default(), "initialize-web-extensions", G_CALLBACK(wxgtk_initialize_web_extensions), m_dbusServer); m_web_view = WEBKIT_WEB_VIEW(webkit_web_view_new()); GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view)); g_object_ref(m_widget); g_signal_connect_after(m_web_view, "decide-policy", G_CALLBACK(wxgtk_webview_webkit_decide_policy), this); g_signal_connect_after(m_web_view, "load-failed", G_CALLBACK(wxgtk_webview_webkit_load_failed), this); g_signal_connect_after(m_web_view, "notify::title", G_CALLBACK(wxgtk_webview_webkit_title_changed), this); g_signal_connect_after(m_web_view, "resource-load-started", G_CALLBACK(wxgtk_webview_webkit_resource_req), this); g_signal_connect_after(m_web_view, "context-menu", G_CALLBACK(wxgtk_webview_webkit_context_menu), this); g_signal_connect_after(m_web_view, "create", G_CALLBACK(wxgtk_webview_webkit_create_webview), this); WebKitFindController* findctrl = webkit_web_view_get_find_controller(m_web_view); g_signal_connect_after(findctrl, "counted-matches", G_CALLBACK(wxgtk_webview_webkit_counted_matches), &m_findCount); m_parent->DoAddChild( this ); PostCreation(size); /* Open a webpage */ webkit_web_view_load_uri(m_web_view, url.utf8_str()); // last to avoid getting signal too early g_signal_connect_after(m_web_view, "load-changed", G_CALLBACK(wxgtk_webview_webkit_load_changed), this); return true; } wxWebViewWebKit::~wxWebViewWebKit() { if (m_web_view) GTKDisconnect(m_web_view); if (m_dbusServer) g_dbus_server_stop(m_dbusServer); g_clear_object(&m_dbusServer); g_clear_object(&m_extension); } bool wxWebViewWebKit::Enable( bool enable ) { if (!wxControl::Enable(enable)) return false; gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable); return true; } GdkWindow* wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const { GdkWindow* window = gtk_widget_get_parent_window(m_widget); return window; } void wxWebViewWebKit::ZoomIn() { SetWebkitZoom(GetWebkitZoom() + 0.1); } void wxWebViewWebKit::ZoomOut() { SetWebkitZoom(GetWebkitZoom() - 0.1); } void wxWebViewWebKit::SetWebkitZoom(float level) { webkit_web_view_set_zoom_level(m_web_view, level); } float wxWebViewWebKit::GetWebkitZoom() const { return webkit_web_view_get_zoom_level(m_web_view); } void wxWebViewWebKit::Stop() { webkit_web_view_stop_loading(m_web_view); } void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags) { if (flags & wxWEBVIEW_RELOAD_NO_CACHE) { webkit_web_view_reload_bypass_cache(m_web_view); } else { webkit_web_view_reload(m_web_view); } } void wxWebViewWebKit::LoadURL(const wxString& url) { webkit_web_view_load_uri(m_web_view, wxGTK_CONV(url)); } void wxWebViewWebKit::GoBack() { webkit_web_view_go_back(m_web_view); } void wxWebViewWebKit::GoForward() { webkit_web_view_go_forward(m_web_view); } bool wxWebViewWebKit::CanGoBack() const { return webkit_web_view_can_go_back(m_web_view); } bool wxWebViewWebKit::CanGoForward() const { return webkit_web_view_can_go_forward(m_web_view); } void wxWebViewWebKit::ClearHistory() { // In WebKit2GTK+, the BackForwardList can't be cleared so do nothing. } void wxWebViewWebKit::EnableHistory(bool) { // In WebKit2GTK+, history can't be disabled so do nothing here. } wxVector > wxWebViewWebKit::GetBackwardHistory() { wxVector > backhist; WebKitBackForwardList* history = webkit_web_view_get_back_forward_list(m_web_view); GList* list = webkit_back_forward_list_get_back_list(history); //We need to iterate in reverse to get the order we desire for(int i = g_list_length(list) - 1; i >= 0 ; i--) { WebKitBackForwardListItem* gtkitem = (WebKitBackForwardListItem*)g_list_nth_data(list, i); wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem( webkit_back_forward_list_item_get_uri(gtkitem), webkit_back_forward_list_item_get_title(gtkitem)); wxitem->m_histItem = gtkitem; wxSharedPtr item(wxitem); backhist.push_back(item); } return backhist; } wxVector > wxWebViewWebKit::GetForwardHistory() { wxVector > forwardhist; WebKitBackForwardList* history = webkit_web_view_get_back_forward_list(m_web_view); GList* list = webkit_back_forward_list_get_forward_list(history); for(guint i = 0; i < g_list_length(list); i++) { WebKitBackForwardListItem* gtkitem = (WebKitBackForwardListItem*)g_list_nth_data(list, i); wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem( webkit_back_forward_list_item_get_uri(gtkitem), webkit_back_forward_list_item_get_title(gtkitem)); wxitem->m_histItem = gtkitem; wxSharedPtr item(wxitem); forwardhist.push_back(item); } return forwardhist; } void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr item) { WebKitBackForwardListItem* gtkitem = (WebKitBackForwardListItem*)item->m_histItem; if(gtkitem) { webkit_web_view_go_to_back_forward_list_item(m_web_view, WEBKIT_BACK_FORWARD_LIST_ITEM(gtkitem)); } } static void wxgtk_can_execute_editing_command_cb(WebKitWebView *, GAsyncResult *res, GAsyncResult **res_out) { *res_out = (GAsyncResult*)g_object_ref(res); } bool wxWebViewWebKit::CanExecuteEditingCommand(const gchar* command) const { GAsyncResult *result = NULL; webkit_web_view_can_execute_editing_command(m_web_view, command, NULL, (GAsyncReadyCallback)wxgtk_can_execute_editing_command_cb, &result); GMainContext *main_context = g_main_context_get_thread_default(); while (!result) { g_main_context_iteration(main_context, TRUE); } gboolean can_execute = webkit_web_view_can_execute_editing_command_finish(m_web_view, result, NULL); g_object_unref(result); return can_execute; } bool wxWebViewWebKit::CanCut() const { return CanExecuteEditingCommand(WEBKIT_EDITING_COMMAND_CUT); } bool wxWebViewWebKit::CanCopy() const { return CanExecuteEditingCommand(WEBKIT_EDITING_COMMAND_COPY); } bool wxWebViewWebKit::CanPaste() const { return CanExecuteEditingCommand(WEBKIT_EDITING_COMMAND_PASTE); } void wxWebViewWebKit::Cut() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_CUT); } void wxWebViewWebKit::Copy() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_COPY); } void wxWebViewWebKit::Paste() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_PASTE); } bool wxWebViewWebKit::CanUndo() const { return CanExecuteEditingCommand(WEBKIT_EDITING_COMMAND_UNDO); } bool wxWebViewWebKit::CanRedo() const { return CanExecuteEditingCommand(WEBKIT_EDITING_COMMAND_REDO); } void wxWebViewWebKit::Undo() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_UNDO); } void wxWebViewWebKit::Redo() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_REDO); } wxString wxWebViewWebKit::GetCurrentURL() const { // FIXME: check which encoding the web kit control uses instead of // assuming UTF8 (here and elsewhere too) return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view)); } wxString wxWebViewWebKit::GetCurrentTitle() const { return wxString::FromUTF8(webkit_web_view_get_title(m_web_view)); } static void wxgtk_web_resource_get_data_cb(WebKitWebResource *, GAsyncResult *res, GAsyncResult **res_out) { *res_out = (GAsyncResult*)g_object_ref(res); } wxString wxWebViewWebKit::GetPageSource() const { WebKitWebResource *resource = webkit_web_view_get_main_resource(m_web_view); if (!resource) { return wxString(); } GAsyncResult *result = NULL; webkit_web_resource_get_data(resource, NULL, (GAsyncReadyCallback)wxgtk_web_resource_get_data_cb, &result); GMainContext *main_context = g_main_context_get_thread_default(); while (!result) { g_main_context_iteration(main_context, TRUE); } guchar *source = webkit_web_resource_get_data_finish(resource, result, NULL, NULL); if (result) { g_object_unref(result); } if (source) { wxString wxs = wxString(source, wxConvUTF8); free(source); return wxs; } return wxString(); } wxWebViewZoom wxWebViewWebKit::GetZoom() const { float zoom = GetWebkitZoom(); // arbitrary way to map float zoom to our common zoom enum if (zoom <= 0.65) { return wxWEBVIEW_ZOOM_TINY; } else if (zoom > 0.65 && zoom <= 0.90) { return wxWEBVIEW_ZOOM_SMALL; } else if (zoom > 0.90 && zoom <= 1.15) { return wxWEBVIEW_ZOOM_MEDIUM; } else if (zoom > 1.15 && zoom <= 1.45) { return wxWEBVIEW_ZOOM_LARGE; } else if (zoom > 1.45) { return wxWEBVIEW_ZOOM_LARGEST; } // to shut up compilers, this can never be reached logically wxFAIL; return wxWEBVIEW_ZOOM_MEDIUM; } void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom) { // arbitrary way to map our common zoom enum to float zoom switch (zoom) { case wxWEBVIEW_ZOOM_TINY: SetWebkitZoom(0.6f); break; case wxWEBVIEW_ZOOM_SMALL: SetWebkitZoom(0.8f); break; case wxWEBVIEW_ZOOM_MEDIUM: SetWebkitZoom(1.0f); break; case wxWEBVIEW_ZOOM_LARGE: SetWebkitZoom(1.3); break; case wxWEBVIEW_ZOOM_LARGEST: SetWebkitZoom(1.6); break; default: wxFAIL; } } void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type) { WebKitSettings* settings = webkit_web_view_get_settings(m_web_view); webkit_settings_set_zoom_text_only(settings, (type == wxWEBVIEW_ZOOM_TYPE_TEXT ? TRUE : FALSE)); } wxWebViewZoomType wxWebViewWebKit::GetZoomType() const { WebKitSettings* settings = webkit_web_view_get_settings(m_web_view); gboolean tozoom = webkit_settings_get_zoom_text_only(settings); if (tozoom) return wxWEBVIEW_ZOOM_TYPE_TEXT; else return wxWEBVIEW_ZOOM_TYPE_LAYOUT; } bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const { // this port supports all zoom types return true; } void wxWebViewWebKit::DoSetPage(const wxString& html, const wxString& baseUri) { webkit_web_view_load_html(m_web_view, html.mb_str(wxConvUTF8), baseUri.mb_str(wxConvUTF8)); } void wxWebViewWebKit::Print() { WebKitPrintOperation* printop = webkit_print_operation_new(m_web_view); webkit_print_operation_run_dialog(printop, NULL); g_object_unref(printop); } bool wxWebViewWebKit::IsBusy() const { return m_busy; } void wxWebViewWebKit::SetEditable(bool enable) { webkit_web_view_set_editable(m_web_view, enable); } bool wxWebViewWebKit::IsEditable() const { gboolean editable; g_object_get(m_web_view, "editable", &editable, NULL); return editable; } void wxWebViewWebKit::DeleteSelection() { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "DeleteSelection", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { g_variant_unref(retval); } } } bool wxWebViewWebKit::HasSelection() const { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "HasSelection", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { gboolean has_selection = FALSE; g_variant_get(retval, "(b)", &has_selection); g_variant_unref(retval); return has_selection; } } return false; } void wxWebViewWebKit::SelectAll() { webkit_web_view_execute_editing_command(m_web_view, WEBKIT_EDITING_COMMAND_SELECT_ALL); } wxString wxWebViewWebKit::GetSelectedText() const { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "GetSelectedText", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { char *text; g_variant_get(retval, "(s)", &text); g_variant_unref(retval); return wxString(text, wxConvUTF8); } } return wxString(); } wxString wxWebViewWebKit::GetSelectedSource() const { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "GetSelectedSource", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { char *source; g_variant_get(retval, "(s)", &source); g_variant_unref(retval); return wxString(source, wxConvUTF8); } } return wxString(); } void wxWebViewWebKit::ClearSelection() { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "ClearSelection", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { g_variant_unref(retval); } } } wxString wxWebViewWebKit::GetPageText() const { if (m_extension) { guint64 page_id = webkit_web_view_get_page_id(m_web_view); GVariant *retval = g_dbus_proxy_call_sync(m_extension, "GetPageText", g_variant_new("(t)", page_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); if (retval) { char *text; g_variant_get(retval, "(s)", &text); g_variant_unref(retval); return wxString(text, wxConvUTF8); } } return wxString(); } void wxWebViewWebKit::RunScript(const wxString& javascript) { webkit_web_view_run_javascript(m_web_view, javascript.mb_str(wxConvUTF8), NULL, NULL, NULL); } void wxWebViewWebKit::RegisterHandler(wxSharedPtr handler) { m_handlerList.push_back(handler); } void wxWebViewWebKit::EnableContextMenu(bool enable) { wxWebView::EnableContextMenu(enable); } long wxWebViewWebKit::Find(const wxString& text, int flags) { WebKitFindController* findctrl = webkit_web_view_get_find_controller(m_web_view); bool newSearch = false; if(text != m_findText || (flags & wxWEBVIEW_FIND_MATCH_CASE) != (m_findFlags & wxWEBVIEW_FIND_MATCH_CASE)) { newSearch = true; //If it is a new search we need to clear existing highlights webkit_find_controller_search_finish(findctrl); } m_findFlags = flags; m_findText = text; //If the search string is empty then we clear any selection and highlight if(text.empty()) { webkit_find_controller_search_finish(findctrl); ClearSelection(); return wxNOT_FOUND; } bool wrap = false, forward = true; guint32 options = WEBKIT_FIND_OPTIONS_NONE; if(flags & wxWEBVIEW_FIND_WRAP) { wrap = true; options |= WEBKIT_FIND_OPTIONS_WRAP_AROUND; } if(!(flags & wxWEBVIEW_FIND_MATCH_CASE)) { options |= WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE; } if(flags & wxWEBVIEW_FIND_BACKWARDS) { forward = false; options |= WEBKIT_FIND_OPTIONS_BACKWARDS; } if(newSearch) { //Initially we count the matches to know how many we have m_findCount = -1; webkit_find_controller_count_matches(findctrl, wxGTK_CONV(text), options, G_MAXUINT); GMainContext *main_context = g_main_context_get_thread_default(); while (m_findCount == -1) { g_main_context_iteration(main_context, TRUE); } //Highlight them if needed if(flags & wxWEBVIEW_FIND_HIGHLIGHT_RESULT) { webkit_find_controller_search(findctrl, wxGTK_CONV(text), options, G_MAXUINT); } //In this case we return early to match IE behaviour m_findPosition = -1; return m_findCount; } else { if(forward) m_findPosition++; else m_findPosition--; if(m_findPosition < 0) m_findPosition += m_findCount; if(m_findPosition > m_findCount) m_findPosition -= m_findCount; } if(forward) { webkit_find_controller_search_next(findctrl); if(m_findPosition == m_findCount && !wrap) { return wxNOT_FOUND; } } else { webkit_find_controller_search_previous(findctrl); if(m_findPosition == -1 && !wrap) { return wxNOT_FOUND; } } return newSearch ? m_findCount : m_findPosition; } void wxWebViewWebKit::FindClear() { m_findCount = 0; m_findFlags = 0; m_findText.clear(); m_findPosition = -1; } // static wxVisualAttributes wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) { return GetDefaultAttributesFromGTKWidget(webkit_web_view_new()); } void wxWebViewWebKit::SetupWebExtensionServer() { char *address = g_strdup_printf("unix:tmpdir=%s", g_get_tmp_dir()); char *guid = g_dbus_generate_guid(); GDBusAuthObserver *observer = g_dbus_auth_observer_new(); GError *error = NULL; g_signal_connect(observer, "authorize-authenticated-peer", G_CALLBACK(wxgtk_authorize_authenticated_peer_cb), this); m_dbusServer = g_dbus_server_new_sync(address, G_DBUS_SERVER_FLAGS_NONE, guid, observer, NULL, &error); if (error) { g_warning("Failed to start web extension server on %s: %s", address, error->message); g_error_free(error); } else { g_signal_connect(m_dbusServer, "new-connection", G_CALLBACK(wxgtk_new_connection_cb), &m_extension); g_dbus_server_start(m_dbusServer); } g_free(address); g_free(guid); g_object_unref(observer); } #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT2