Skip to content

Commit

Permalink
fix: geolocation crashes electron on macOS (#29343) (#29914)
Browse files Browse the repository at this point in the history
  • Loading branch information
omarkilani committed Jun 29, 2021
1 parent 8486a73 commit f539275
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
16 changes: 15 additions & 1 deletion shell/browser/electron_browser_client.cc
Expand Up @@ -867,7 +867,13 @@ ElectronBrowserClient::GetSystemNetworkContext() {
std::unique_ptr<content::BrowserMainParts>
ElectronBrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& params) {
return std::make_unique<ElectronBrowserMainParts>(params);
auto browser_main_parts = std::make_unique<ElectronBrowserMainParts>(params);

#if defined(OS_MAC)
browser_main_parts_ = browser_main_parts.get();
#endif

return browser_main_parts;
}

void ElectronBrowserClient::WebNotificationAllowed(
Expand Down Expand Up @@ -1603,4 +1609,12 @@ void ElectronBrowserClient::RegisterBrowserInterfaceBindersForServiceWorker(
base::BindRepeating(&BindBadgeServiceForServiceWorker));
}

device::GeolocationManager* ElectronBrowserClient::GetGeolocationManager() {
#if defined(OS_MAC)
return browser_main_parts_->GetGeolocationManager();
#else
return nullptr;
#endif
}

} // namespace electron
7 changes: 7 additions & 0 deletions shell/browser/electron_browser_client.h
Expand Up @@ -34,6 +34,7 @@ class SSLCertRequestInfo;

namespace electron {

class ElectronBrowserMainParts;
class NotificationPresenter;
class PlatformNotificationService;

Expand Down Expand Up @@ -88,6 +89,8 @@ class ElectronBrowserClient : public content::ContentBrowserClient,

content::BluetoothDelegate* GetBluetoothDelegate() override;

device::GeolocationManager* GetGeolocationManager() override;

protected:
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
content::SpeechRecognitionManagerDelegate*
Expand Down Expand Up @@ -298,6 +301,10 @@ class ElectronBrowserClient : public content::ContentBrowserClient,
std::unique_ptr<ElectronSerialDelegate> serial_delegate_;
std::unique_ptr<ElectronBluetoothDelegate> bluetooth_delegate_;

#if defined(OS_MAC)
ElectronBrowserMainParts* browser_main_parts_ = nullptr;
#endif

DISALLOW_COPY_AND_ASSIGN(ElectronBrowserClient);
};

Expand Down
7 changes: 7 additions & 0 deletions shell/browser/electron_browser_main_parts.cc
Expand Up @@ -92,6 +92,7 @@
#endif

#if defined(OS_MAC)
#include "services/device/public/cpp/geolocation/geolocation_manager.h"
#include "shell/browser/ui/cocoa/views_delegate_mac.h"
#else
#include "shell/browser/ui/views/electron_views_delegate.h"
Expand Down Expand Up @@ -543,6 +544,12 @@ ElectronBrowserMainParts::GetGeolocationControl() {
return geolocation_control_.get();
}

#if defined(OS_MAC)
device::GeolocationManager* ElectronBrowserMainParts::GetGeolocationManager() {
return geolocation_manager_.get();
}
#endif

IconManager* ElectronBrowserMainParts::GetIconManager() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!icon_manager_.get())
Expand Down
12 changes: 12 additions & 0 deletions shell/browser/electron_browser_main_parts.h
Expand Up @@ -40,6 +40,10 @@ class GtkUiPlatform;
}
#endif

namespace device {
class GeolocationManager;
} // namespace device

namespace electron {

class ElectronBrowserContext;
Expand Down Expand Up @@ -84,6 +88,10 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {
// used to enable the location services once per client.
device::mojom::GeolocationControl* GetGeolocationControl();

#if defined(OS_MAC)
device::GeolocationManager* GetGeolocationManager();
#endif

// Returns handle to the class responsible for extracting file icons.
IconManager* GetIconManager();

Expand Down Expand Up @@ -162,6 +170,10 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {

mojo::Remote<device::mojom::GeolocationControl> geolocation_control_;

#if defined(OS_MAC)
std::unique_ptr<device::GeolocationManager> geolocation_manager_;
#endif

static ElectronBrowserMainParts* self_;

DISALLOW_COPY_AND_ASSIGN(ElectronBrowserMainParts);
Expand Down
3 changes: 3 additions & 0 deletions shell/browser/electron_browser_main_parts_mac.mm
Expand Up @@ -7,6 +7,7 @@
#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
#include "base/path_service.h"
#include "services/device/public/cpp/geolocation/geolocation_manager_impl_mac.h"
#import "shell/browser/mac/electron_application.h"
#include "shell/browser/mac/electron_application_delegate.h"
#include "shell/common/electron_paths.h"
Expand All @@ -27,6 +28,8 @@
[[NSUserDefaults standardUserDefaults]
setObject:@"NO"
forKey:@"NSTreatUnknownArgumentsAsOpen"];

geolocation_manager_ = device::GeolocationManagerImpl::Create();
}

void ElectronBrowserMainParts::FreeAppDelegate() {
Expand Down

0 comments on commit f539275

Please sign in to comment.