javaandroid-studiofirefox-addon-sdkgeckogeckoview

How to get current page URL with gecko?


I am trying to make my android web browser open only specific urls. Because of that, I want to check if loaded url meets the requirements, and according to that to do something. I saw many answers about WebView, but since I have to use open source browser (Mozilla Firefox) I am using gecko. Here is my code, I tried to do something with onLoadRequest but I do not know how to make it work. Thanks.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GeckoView view = findViewById(R.id.geckoView);
    GeckoSession session = new GeckoSession();
    GeckoRuntime runtime = GeckoRuntime.create(this);

    session.open(runtime);
    view.setSession(session);
    session.loadUri("https://www.google.com");

    GeckoSession.NavigationDelegate.LoadRequest loadRequest=new GeckoSession.NavigationDelegate.LoadRequest();

   session.getNavigationDelegate().onLoadRequest(session,loadRequest);


}

@Override
public void onLoadRequest(GeckoSession session, GeckoSession.NavigationDelegate.LoadRequest request)
{
    if(request.uri.contains("mail"))
        GeckoResult.fromValue(AllowOrDeny.ALLOW);
    else
        GeckoResult.fromValue(AllowOrDeny.DENY);
}

Solution

  • GeckoView heavily relies on its delegates to allow for app-specific handling of most relevant mechanics and events.

    In short, there are runtime and session delegates, set on GeckoRuntime and GeckoSession respectively. The general pattern is that for each delegate there is a set{DelegateName}Delegate() method to attach delegates to the runtime or session with one exception being RuntimeTelemetry.Delegate which is set in GeckoRuntimeSettings instead.

    Delegate methods are called by GeckoView and should not be called by the app.

    In your case, you want to implement the NavigationDelegate and set your implementation on the GeckoSession to override the default top-level page load behavior.

    class MyNavigationDelegate implements GeckoSession.NavigationDelegate {
        @Override
        public GeckoResult<AllowOrDeny> onLoadRequest(
                final GeckoSession session,
                final LoadRequest request) {
            // TODO: deny/allow based on your constrains.
        }
    
        // TODO: You should implement the rest of the delegate to handle page load
        // errors and new session requests triggered by new-tab/window requests.
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        GeckoView view = findViewById(R.id.geckoView);
        GeckoSession session = new GeckoSession();
        GeckoRuntime runtime = GeckoRuntime.create(this);
    
        session.setNavigationDelegate(new MyNavigationDelegate());
    
        session.open(runtime);
        view.setSession(session);
        session.loadUri("https://www.google.com");
    }
    

    For more details, please consult the API reference and the GeckoView Example implementation.