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

Custom implementation of Geolocation #3881

Open
ShelleyLake opened this issue Feb 10, 2025 · 3 comments
Open

Custom implementation of Geolocation #3881

ShelleyLake opened this issue Feb 10, 2025 · 3 comments
Labels
enhancement Enhancement request

Comments

@ShelleyLake
Copy link

ShelleyLake commented Feb 10, 2025

This request is not a duplicate of https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=19767

Dear Team,
I would like to handle the Geolocation request via the “navigator.geolocation-getCurrentPosition()/watchPosition()” functions by providing the answer myself, i.e., an arbitrary position that is therefore not necessarily based on the Google Geolocation.
I am not sure of the correct implementation, but I succeeded by redefining in the Frame Context the two js functions navigator.geolocation.getCurrentPosition and navigator.geolocation.watchPosition

CefApp::OnContextCreated(CefRefPtr<CefBrowser> b, CefRefPtr<CefFrame> f, CefRefPtr<CefV8Context> c)
{
  if (const CefRefPtr<CefV8Value> windowObject = c->GetGlobal())
    if (const CefRefPtr<CefV8Value> navigator = windowObject->GetValue("navigator"))
    {
            _geolocation = navigator->GetValue("geolocation");
            _geolocation->SetValue("getCurrentPosition", CefV8Value::CreateFunction("getCurrentPosition", new CppGeolocationHandler(this, frame)), V8_PROPERTY_ATTRIBUTE_READONLY);
            _geolocation->SetValue("watchPosition", CefV8Value::CreateFunction("watchPosition", new CppGeolocationHandler(this, frame)), V8_PROPERTY_ATTRIBUTE_READONLY);
    }
}

"CppGeolocationHandler" will then provide somehow a minimum js object with latitude and longitude.

So far so good. My only problem is that this method does not really fit with the Permission Request of the Geolocation (CefPermissionHandler::OnShowPermissionPrompt).

When I redefine those functions, the method OnShowPermissionPrompt is not triggered at all.
Testing it with the page "https://browserleaks.com/geo" I get my own value immediately in "getCurrentPosition" but the permission API shows that it is stuck in "prompt" - basically it (should be) waiting for the user to confirm the request (but as I already wrote, the corresponding method is not triggered)

On the other hand, if I do not (re)define the two functions, OnShowPermissionPrompt is triggered, but then I do not know how to provide my own location.

Any idea how to comply with OnShowPermissionPrompt and then provide an own answer if accepted would be very welcome.

Thanks in advance.

J.

@ShelleyLake ShelleyLake added the bug Bug report label Feb 10, 2025
@amaitland
Copy link
Contributor

an arbitrary position that is therefore not necessarily based on the Google Geolocation.

Isn't that the purpose of Emulation.setGeolocationOverride?

@ShelleyLake
Copy link
Author

ShelleyLake commented Feb 11, 2025

@amaitland Does this even work in CEF? Not sure if we are talking about the same thing, but in the Chromium-based browser it is true that it is possible to set a custom/mock/fake geolocation in the DevTools page.
This does not seem to be the case in CEF (I see no way to select or add a custom location in the corresponding DevTools page, even manually).

Still, I would like to do a clean c++/js implementation without extra stuff (DevTools protocol or whatever).

@magreenblatt magreenblatt added enhancement Enhancement request and removed bug Bug report labels Feb 11, 2025
@magreenblatt magreenblatt changed the title CEF *CUSTOM* implementation of Geolocation Custom implementation of Geolocation Feb 11, 2025
@amaitland
Copy link
Contributor

Does this even work in CEF?

Yes. If you use Chrome styled browser

Not sure if we are talking about the same thing, but in the Chromium-based browser it is true that it is possible to set a custom/mock/fake geolocation in the DevTools page.

You can programmatically send CDP commands directly to a browser.

Still, I would like to do a clean c++/js implementation without extra stuff (DevTools protocol or whatever).

Chromium already provides such functionality via CDP protocol.

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

No branches or pull requests

3 participants