Skip to content

Commit

Permalink
Updated IsSystemLocationSettingEnabled()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed May 7, 2024
1 parent e4b32f7 commit 9cb97cd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 39 deletions.
7 changes: 5 additions & 2 deletions browser/ui/geolocation/geolocation_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#ifndef BRAVE_BROWSER_UI_GEOLOCATION_GEOLOCATION_UTILS_H_
#define BRAVE_BROWSER_UI_GEOLOCATION_GEOLOCATION_UTILS_H_

#include "base/functional/callback_forward.h"

namespace geolocation {

// True when system location service is available to applications.
bool IsSystemLocationSettingEnabled();
// Run |callback| with true when system location service is available to
// applications.
void IsSystemLocationSettingEnabled(base::OnceCallback<void(bool)> callback);

bool CanGiveDetailedGeolocationRequestInfo();

Expand Down
4 changes: 1 addition & 3 deletions browser/ui/geolocation/geolocation_utils_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

namespace geolocation {

bool IsSystemLocationSettingEnabled() {
return false;
}
void IsSystemLocationSettingEnabled(base::OnceCallback<void(bool)> callback) {}

bool CanGiveDetailedGeolocationRequestInfo() {
return false;
Expand Down
12 changes: 11 additions & 1 deletion browser/ui/geolocation/geolocation_utils_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

#import <CoreLocation/CoreLocation.h>

#include <utility>

namespace geolocation {

bool IsSystemLocationSettingEnabled() {
namespace {

bool GetSystemLocationSettingEnabled() {
// Service is off globally.
if (![CLLocationManager locationServicesEnabled]) {
return false;
Expand All @@ -26,6 +30,12 @@ bool IsSystemLocationSettingEnabled() {
return false;
}

} // namespace

void IsSystemLocationSettingEnabled(base::OnceCallback<void(bool)> callback) {
std::move(callback).Run(GetSystemLocationSettingEnabled());
}

bool CanGiveDetailedGeolocationRequestInfo() {
if (@available(macOS 11.0, *)) {
return true;
Expand Down
17 changes: 16 additions & 1 deletion browser/ui/geolocation/geolocation_utils_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

#include "brave/browser/ui/geolocation/geolocation_utils.h"

#include <utility>

#include <windows.h>

#include <windows.devices.enumeration.h>
#include <windows.foundation.h>
#include <wrl/event.h>

#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/task/thread_pool.h"
#include "base/win/core_winrt_util.h"

using ABI::Windows::Devices::Enumeration::DeviceAccessStatus;
Expand All @@ -22,8 +26,10 @@ using Microsoft::WRL::ComPtr;

namespace geolocation {

namespace {

// Copied from services/device/geolocation/win/location_provider_winrt.cc
bool IsSystemLocationSettingEnabled() {
bool GetSystemLocationSettingEnabled() {
ComPtr<IDeviceAccessInformationStatics> dev_access_info_statics;
HRESULT hr = base::win::GetActivationFactory<
IDeviceAccessInformationStatics,
Expand All @@ -49,6 +55,15 @@ bool IsSystemLocationSettingEnabled() {
status == DeviceAccessStatus::DeviceAccessStatus_DeniedByUser);
}

} // namespace

void IsSystemLocationSettingEnabled(base::OnceCallback<void(bool)> callback) {
base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()})
->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&GetSystemLocationSettingEnabled),
std::move(callback));
}

bool CanGiveDetailedGeolocationRequestInfo() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/task/thread_pool.h"
#include "brave/browser/ui/geolocation/brave_geolocation_permission_tab_helper.h"
#include "brave/browser/ui/geolocation/geolocation_utils.h"
#include "brave/browser/ui/views/dialog_footnote_utils.h"
Expand Down Expand Up @@ -329,37 +328,26 @@ void AddGeolocationDescriptionIfNeeded(
}
}

#if BUILDFLAG(IS_WIN)
// IsSystemLocationSettingEnabled() uses COM.
base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()})
->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&geolocation::IsSystemLocationSettingEnabled),
base::BindOnce(
[](base::WeakPtr<views::WidgetDelegate> widget_delegate,
base::WeakPtr<Browser> browser, bool enable_high_accuracy,
bool settings_enabled) {
// Browser or Bubble is already gone.
if (!browser || !widget_delegate) {
return;
}
PermissionPromptBubbleBaseView* bubble_base_view =
static_cast<PermissionPromptBubbleBaseView*>(
widget_delegate.get());
AddGeolocationDescription(
bubble_base_view, browser.get(),
/*enable_high_accuracy*/ enable_high_accuracy,
/*use_exact_location_label*/ settings_enabled);

// To update widget layout after adding another child view.
bubble_base_view->UpdateAnchorPosition();
},
bubble_base_view->AsWeakPtr(), browser->AsWeakPtr(),
enable_high_accuracy));
#else
AddGeolocationDescription(bubble_base_view, browser, enable_high_accuracy,
geolocation::IsSystemLocationSettingEnabled());
#endif
geolocation::IsSystemLocationSettingEnabled(base::BindOnce(
[](base::WeakPtr<views::WidgetDelegate> widget_delegate,
base::WeakPtr<Browser> browser, bool enable_high_accuracy,
bool settings_enabled) {
// Browser or Bubble is already gone.
if (!browser || !widget_delegate) {
return;
}
PermissionPromptBubbleBaseView* bubble_base_view =
static_cast<PermissionPromptBubbleBaseView*>(widget_delegate.get());
AddGeolocationDescription(
bubble_base_view, browser.get(),
/*enable_high_accuracy*/ enable_high_accuracy,
/*use_exact_location_label*/ settings_enabled);

// To update widget layout after adding another child view.
bubble_base_view->UpdateAnchorPosition();
},
bubble_base_view->AsWeakPtr(), browser->AsWeakPtr(),
enable_high_accuracy));
}

views::View* AddPermissionLifetimeComboboxIfNeeded(
Expand Down

0 comments on commit 9cb97cd

Please sign in to comment.