Example #1
0
        private void TitleChangeCallback(IntPtr webview, IntPtr userdata)
        {
            string?title = GLibString.FromPointer(WebKit.GetTitle(webview));

            TitleChanged?.Invoke(this, title ?? string.Empty);
        }
Example #2
0
        private async void UriSchemeCallback(IntPtr request, IntPtr userdata)
        {
            try
            {
                var uri  = new Uri(GLibString.FromPointer(WebKit.UriScheme.GetRequestUri(request)) !);
                var host = new Uri(uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));
                if (host == customHost)
                {
                    using var contentStream = await Application.ContentProvider.GetStreamAsync(uri);

                    if (contentStream != null)
                    {
                        IntPtr stream = IntPtr.Zero;
                        try
                        {
                            if (contentStream is UnmanagedMemoryStream unmanagedMemoryStream)
                            {
                                unsafe
                                {
                                    long length = unmanagedMemoryStream.Length - unmanagedMemoryStream.Position;
                                    stream = GLib.CreateStreamFromData((IntPtr)unmanagedMemoryStream.PositionPointer, length, IntPtr.Zero);
                                    FinishUriSchemeCallback(request, stream, length, uri);
                                    return;
                                }
                            }
                            else
                            {
                                byte[] data;
                                long   length;
                                if (contentStream is MemoryStream memoryStream)
                                {
                                    data   = memoryStream.GetBuffer();
                                    length = memoryStream.Length;
                                }
                                else
                                {
                                    using var copyStream = new MemoryStream();
                                    await contentStream.CopyToAsync(copyStream);

                                    data   = copyStream.GetBuffer();
                                    length = copyStream.Length;
                                }

                                unsafe
                                {
                                    fixed(byte *dataPtr = data)
                                    {
                                        stream = GLib.CreateStreamFromData((IntPtr)dataPtr, length, IntPtr.Zero);
                                        FinishUriSchemeCallback(request, stream, length, uri);
                                        return;
                                    }
                                }
                            }
                        }
                        finally { if (stream != IntPtr.Zero)
                                  {
                                      GLib.UnrefObject(stream);
                                  }
                        }
                    }
                }

                FinishUriSchemeCallbackWithError(request, 4); // 4 = file not found
            }
            catch { FinishUriSchemeCallbackWithError(request, 24); } // 24 = failed for unspecified reason
        }