Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting extension path for Windows and Linux #1403

Merged
merged 17 commits into from
Oct 28, 2024

Conversation

SpikeHD
Copy link
Contributor

@SpikeHD SpikeHD commented Oct 28, 2024

Introduces with_extension_path, which is now used by Windows and Linux to load extensions from.

Linux doesn't include browser_extensions_enabled because there is no setting to turn them on or off in WebkitGTK, only to set their directory. I assume if no directory is set, then no extensions are loaded from anywhere, so the option would be redundant.

@SpikeHD SpikeHD requested a review from a team as a code owner October 28, 2024 01:06
Copy link
Contributor

github-actions bot commented Oct 28, 2024

Package Changes Through 3b4905b

There are 1 changes which include wry with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
wry 0.46.3 0.46.4

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

amrbashir
amrbashir previously approved these changes Oct 28, 2024
@amrbashir
Copy link
Member

Thank you

@amrbashir
Copy link
Member

Oh looks like you didn't sign your commits and I can't merge the PR unless they are signed. You need to setup commit signing, then you can sign past commit like this. git rebase --exec 'git commit --amend --no-edit -n -S' -i dev

@SpikeHD SpikeHD force-pushed the feat/extension_path branch from b3b62de to 6d23c17 Compare October 28, 2024 16:56
@SpikeHD
Copy link
Contributor Author

SpikeHD commented Oct 28, 2024

One day I will remember to add -S when committing. Today was not that day

@amrbashir
Copy link
Member

One day I will remember to add -S when committing. Today was not that day

🤣 , I have my global .gitconfig to always sign so I don't forget

[commit]
    gpgsign = true

@zhangsan2lisi
Copy link

zhangsan2lisi commented Nov 19, 2024

use std::path::PathBuf;
use tao::event::{Event, WindowEvent};
use tao::event_loop::{ControlFlow, EventLoop};
use tao::window::WindowBuilder;
use wry::{WebContext, WebViewBuilder, WebViewBuilderExtWindows};
fn main() {
    let event_loop = EventLoop::new();
    let window = WindowBuilder::new()
        .build(&event_loop)
        .expect("failed to create window");
    let path = PathBuf::from("test");
    let mut context = WebContext::new(Some(path));
    let webview = WebViewBuilder::with_web_context(&mut context)
        .with_url("https://google.com/")
        // .with_browser_extensions_enabled(true)
        .with_extension_path(PathBuf::from("Extensions/6.6.3_0"))
        .build(&window)
        .expect("could not create webview");
    webview.open_devtools();

    event_loop.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Poll;

        match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                window_id,
                ..
            } => {
                *control_flow = ControlFlow::Exit;
            }
            _ => {}
        }
    });
}

@SpikeHD @amrbashir
First of all, thank you for implementing this function

I attempted to launch a window to load the following extension: https://microsoftedge.microsoft.com/addons/detail/vuejs-devtools/olofadcdnkkjdfgjcmjaadnlehnnihnl
I have already unpacked and repackaged the extension. When tested using Chromium in Electron with the following arguments, it works perfectly fine:

args: [ 
  `--disable-extensions-except=${extensionPath}`,
  `--load-extension=${extensionPath}`,
  "--no-sandbox",
  "--disable-sync",
]

However, when I use Wry on Windows and call .with_extension_path() to load this extension, it fails. Additionally, the extension folder (olofadcdnkkjdfgjcmjaadnlehnnihnl) does not appear in the test\EBWebView\Default\Extensions directory.

When I attempt to enable both .with_browser_extensions_enabled(true) and .with_extension_path(PathBuf::from("Extensions/6.6.3_0")) simultaneously, it results in the following error:
WebView2Error(WindowsError(Error { code: HRESULT(0x80004003), message: "Invalid pointer" })).

Even when I only enable .with_browser_extensions_enabled(true), the extension still does not work.

Of course, I’ve also tried using the with_additional_browser_args function, but the results were equally disappointing.

By the way, I noticed that using the--proxy-serverargument in the browser arguments works the same as the with_proxy_config function. Was this intentionally designed to allow --proxy-server to work, or was it simply overlooked and not filtered out?

How can I resolve this issue? Thank you again for helping me.

@SpikeHD
Copy link
Contributor Author

SpikeHD commented Nov 19, 2024

I've done some digging, and it seems ICoreWebView2Profile7::AddBrowserExtension doesn't like being passed None as the completion handler, which is what's causing Invalid pointer.

Changing that to have an empty handler function fixes your example:

src/webview2/mod.rs L1195

    // Iterate over all folders in the extension path
    for entry in fs::read_dir(extension_path)? {
      let path = entry?.path();
      let path_hs = HSTRING::from(path.as_path());
      let handler = ProfileAddBrowserExtensionCompletedHandler::create(Box::new(|_, _| Ok(())));

      profile.AddBrowserExtension(&path_hs, &handler)?;
    }

Keep in mind @zhangsan2lisi that the path you are providing should be a folder where all extensions reside, even if it is just one.

For example, this would fail:

extension/
├─ manifest.json
let webview = WebViewBuilder::with_web_context(&mut context)
    .with_extension_path(PathBuf::from("extension"))

but this would succeed:

extensions/
├─ my_extension/
│  ├─ manifest.json
let webview = WebViewBuilder::with_web_context(&mut context)
    .with_extension_path(PathBuf::from("extensions"))

because the function reads the files/folders within the provided directory as extensions, not the folder itself.

I will make a PR soon to address the Invalid Pointer issue, as well as rename the function to with_extensions_path so it is less misleading/confusing.

@zhangsan2lisi
Copy link

@SpikeHD Thank you for your help. I have successfully used it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants