Skip to content

Commit

Permalink
[iOS] Add GeolocationManager in iOS
Browse files Browse the repository at this point in the history
GeolocationManager is owned by ShellBrowserMainParts on iOS and
shell_browser_main_parts_ios.mm is introduced to implement
GetGeolocationManager() method so that ShellContentBrowserClient can
get GeolocationManager from it.

`NSLocationWhenInUseUsageDescription` permission is added for
ContentShell on IOS to ask Geolocation permission.
If `kMacCoreLocationBackend` is enabled, it would work with
CoreLocation.framework on iOS as system LocationProvider

Bug: 1421221
Change-Id: Id7e8e382c8537238811b6ce05656bfd774258471
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4494918
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Julie Jeongeun Kim <jkim@igalia.com>
Reviewed-by: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1146889}
  • Loading branch information
jkim-julie authored and Chromium LUCI CQ committed May 20, 2023
1 parent 573bfb2 commit bb75cae
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions content/shell/BUILD.gn
Expand Up @@ -225,6 +225,7 @@ static_library("content_shell_lib") {
"browser/bluetooth/ios/shell_bluetooth_device_list_view_controller.mm",
"browser/bluetooth/shell_bluetooth_delegate_impl_client.cc",
"browser/bluetooth/shell_bluetooth_delegate_impl_client.h",
"browser/shell_browser_main_parts_ios.mm",
"browser/shell_file_select_helper.cc",
"browser/shell_file_select_helper.h",
"browser/shell_platform_delegate_ios.mm",
Expand Down
2 changes: 2 additions & 0 deletions content/shell/app/ios-Info.plist
Expand Up @@ -54,5 +54,7 @@
<string>Allow content_shell access to camera</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Allow content_shell access to Bluetooth</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Allow content_shell access to location</string>
</dict>
</plist>
10 changes: 10 additions & 0 deletions content/shell/browser/shell_browser_main_parts.h
Expand Up @@ -12,6 +12,10 @@
#include "content/public/browser/browser_main_parts.h"
#include "content/shell/browser/shell_browser_context.h"

#if BUILDFLAG(IS_IOS)
#include "services/device/public/cpp/geolocation/geolocation_manager.h"
#endif

namespace performance_manager {
class PerformanceManagerLifetime;
} // namespace performance_manager
Expand Down Expand Up @@ -52,6 +56,9 @@ class ShellBrowserMainParts : public BrowserMainParts {
std::unique_ptr<base::RunLoop>& run_loop) override;
void PostMainMessageLoopRun() override;
void PostDestroyThreads() override;
#if BUILDFLAG(IS_IOS)
device::GeolocationManager* GetGeolocationManager();
#endif

ShellBrowserContext* browser_context() { return browser_context_.get(); }
ShellBrowserContext* off_the_record_browser_context() {
Expand Down Expand Up @@ -84,6 +91,9 @@ class ShellBrowserMainParts : public BrowserMainParts {
#if BUILDFLAG(IS_FUCHSIA)
std::unique_ptr<FuchsiaViewPresenter> fuchsia_view_presenter_;
#endif
#if BUILDFLAG(IS_IOS)
std::unique_ptr<device::GeolocationManager> geolocation_manager_;
#endif
};

} // namespace content
Expand Down
23 changes: 23 additions & 0 deletions content/shell/browser/shell_browser_main_parts_ios.mm
@@ -0,0 +1,23 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/shell/browser/shell_browser_main_parts.h"

#include "services/device/public/cpp/geolocation/system_geolocation_source_mac.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

namespace content {

device::GeolocationManager* ShellBrowserMainParts::GetGeolocationManager() {
if (!geolocation_manager_) {
geolocation_manager_ =
device::SystemGeolocationSourceMac::CreateGeolocationManagerOnMac();
}
return geolocation_manager_.get();
}

} // namespace content
12 changes: 8 additions & 4 deletions content/shell/browser/shell_content_browser_client.cc
Expand Up @@ -102,7 +102,7 @@
#include "components/crash/content/browser/crash_handler_host_linux.h"
#endif

#if BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_MAC)
#include "services/device/public/cpp/test/fake_geolocation_manager.h"
#endif

Expand Down Expand Up @@ -254,14 +254,14 @@ base::flat_set<url::Origin> GetIsolatedContextOriginSetFromFlag() {
// needed should be added here so that it's shared between the instances.
struct SharedState {
SharedState() {
#if BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_MAC)
location_manager = std::make_unique<device::FakeGeolocationManager>();
location_manager->SetSystemPermission(
device::LocationSystemPermissionStatus::kAllowed);
#endif
}

#if BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_MAC)
std::unique_ptr<device::FakeGeolocationManager> location_manager;
#endif

Expand Down Expand Up @@ -438,8 +438,12 @@ void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
}

device::GeolocationManager* ShellContentBrowserClient::GetGeolocationManager() {
#if BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_MAC)
return GetSharedState().location_manager.get();
#elif BUILDFLAG(IS_IOS)
// TODO(crbug.com/1431447, 1411704): Unify this to FakeGeolocationManager once
// exploring browser features in ContentShell on iOS is done.
return GetSharedState().shell_browser_main_parts->GetGeolocationManager();
#else
return nullptr;
#endif
Expand Down
2 changes: 1 addition & 1 deletion services/device/public/cpp/device_features.cc
Expand Up @@ -17,7 +17,7 @@ BASE_FEATURE(kWinrtGeolocationImplementation,
"WinrtGeolocationImplementation",
base::FEATURE_DISABLED_BY_DEFAULT);
// Enables usage of the CoreLocation API for LocationProvider instead of
// NetworkLocationProvider for macOS.
// NetworkLocationProvider for macOS or iOS.
BASE_FEATURE(kMacCoreLocationBackend,
"MacCoreLocationBackend",
base::FEATURE_DISABLED_BY_DEFAULT);
Expand Down
Expand Up @@ -40,6 +40,9 @@ class COMPONENT_EXPORT(GEOLOCATION) SystemGeolocationSourceMac
void StartWatchingPosition(bool high_accuracy) override;
void StopWatchingPosition() override;

// Calls requestWhenInUseAuthorization from CLLocationManager.
void AppAttemptsToUseGeolocation() override;

private:
LocationSystemPermissionStatus GetSystemPermission() const;

Expand Down
Expand Up @@ -10,6 +10,7 @@

#include "base/functional/callback_helpers.h"
#include "base/sequence_checker.h"
#include "build/build_config.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
Expand Down Expand Up @@ -109,6 +110,14 @@ - (BOOL)permissionInitialized;
return LocationSystemPermissionStatus::kDenied;
}

void SystemGeolocationSourceMac::AppAttemptsToUseGeolocation() {
#if BUILDFLAG(IS_IOS)
if (@available(ios 8.0, macOS 10.15, *)) {
[location_manager_ requestWhenInUseAuthorization];
}
#endif
}

} // namespace device

@implementation GeolocationManagerDelegate
Expand All @@ -131,6 +140,15 @@ - (void)locationManager:(CLLocationManager*)manager
} else {
_hasPermission = NO;
}

#if BUILDFLAG(IS_IOS)
if (@available(iOS 8.0, *)) {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
_hasPermission = YES;
}
}
#endif

_manager->PermissionUpdated();
}

Expand Down

0 comments on commit bb75cae

Please sign in to comment.