-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Privacy Hub] Geolocation manager for CrOS
Extending the geolocation manager permission functionality to Chrome OS. Bug: b/257906959 Low-Coverage-Reason: Low coverage is due to 1 uncovered line which is actually covered by a lacros browser test. Change-Id: Ia6ad5e2efb3b05802d5adbcac236357bfa699cce Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4014144 Reviewed-by: Xiyuan Xia <xiyuan@chromium.org> Reviewed-by: Hidehiko Abe <hidehiko@chromium.org> Commit-Queue: Jan Láník <janlanik@google.com> Reviewed-by: Reilly Grant <reillyg@chromium.org> Cr-Commit-Position: refs/heads/main@{#1103304}
- Loading branch information
Jan Lanik
authored and
Chromium LUCI CQ
committed
Feb 9, 2023
1 parent
1964b2d
commit 7653a0b
Showing
16 changed files
with
511 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
chrome/browser/ash/geolocation/system_geolocation_source.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 "chrome/browser/ash/geolocation/system_geolocation_source.h" | ||
|
||
#include <utility> | ||
|
||
#include "ash/constants/ash_pref_names.h" | ||
#include "ash/session/session_controller_impl.h" | ||
#include "ash/shell.h" | ||
#include "base/check.h" | ||
#include "base/functional/callback_helpers.h" | ||
#include "components/prefs/pref_change_registrar.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "services/device/public/cpp/geolocation/geolocation_manager.h" | ||
|
||
namespace ash { | ||
|
||
SystemGeolocationSource::SystemGeolocationSource() | ||
: permission_update_callback_(base::DoNothing()) { | ||
DCHECK(Shell::Get()); | ||
DCHECK(Shell::Get()->session_controller()); | ||
observer_.Observe(Shell::Get()->session_controller()); | ||
PrefService* last_active_user_pref_service = | ||
Shell::Get()->session_controller()->GetLastActiveUserPrefService(); | ||
if (last_active_user_pref_service) { | ||
OnActiveUserPrefServiceChanged(last_active_user_pref_service); | ||
} | ||
} | ||
|
||
SystemGeolocationSource::~SystemGeolocationSource() = default; | ||
|
||
// static | ||
std::unique_ptr<device::GeolocationManager> | ||
SystemGeolocationSource::CreateGeolocationManagerOnAsh() { | ||
return std::make_unique<device::GeolocationManager>( | ||
std::make_unique<SystemGeolocationSource>()); | ||
} | ||
|
||
void SystemGeolocationSource::RegisterPermissionUpdateCallback( | ||
PermissionUpdateCallback callback) { | ||
permission_update_callback_ = std::move(callback); | ||
if (pref_change_registrar_) { | ||
OnPrefChanged(prefs::kUserGeolocationAllowed); | ||
} | ||
} | ||
|
||
void SystemGeolocationSource::OnActiveUserPrefServiceChanged( | ||
PrefService* pref_service) { | ||
// Subscribing to pref changes. | ||
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); | ||
pref_change_registrar_->Init(pref_service); | ||
// value might have changed, hence we trigger the update function | ||
OnPrefChanged(prefs::kUserGeolocationAllowed); | ||
pref_change_registrar_->Add( | ||
prefs::kUserGeolocationAllowed, | ||
base::BindRepeating(&SystemGeolocationSource::OnPrefChanged, | ||
base::Unretained(this))); | ||
} | ||
|
||
void SystemGeolocationSource::OnPrefChanged(const std::string& pref_name) { | ||
DCHECK_EQ(pref_name, prefs::kUserGeolocationAllowed); | ||
DCHECK(pref_change_registrar_); | ||
// Get the actual permission status from CrOS by directly accessing pref | ||
// service. | ||
device::LocationSystemPermissionStatus status = | ||
device::LocationSystemPermissionStatus::kNotDetermined; | ||
|
||
PrefService* pref_service = pref_change_registrar_->prefs(); | ||
if (pref_service) { | ||
status = pref_service->GetBoolean(prefs::kUserGeolocationAllowed) | ||
? device::LocationSystemPermissionStatus::kAllowed | ||
: device::LocationSystemPermissionStatus::kDenied; | ||
} | ||
permission_update_callback_.Run(status); | ||
} | ||
} // namespace ash |
54 changes: 54 additions & 0 deletions
54
chrome/browser/ash/geolocation/system_geolocation_source.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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. | ||
|
||
#ifndef CHROME_BROWSER_ASH_GEOLOCATION_SYSTEM_GEOLOCATION_SOURCE_H_ | ||
#define CHROME_BROWSER_ASH_GEOLOCATION_SYSTEM_GEOLOCATION_SOURCE_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "ash/public/cpp/session/session_controller.h" | ||
#include "ash/public/cpp/session/session_observer.h" | ||
#include "base/scoped_observation.h" | ||
#include "services/device/public/cpp/geolocation/system_geolocation_source.h" | ||
|
||
namespace device { | ||
class GeolocationManager; | ||
} | ||
|
||
class PrefService; | ||
class PrefChangeRegistrar; | ||
|
||
namespace ash { | ||
|
||
// The SystemGeolocationSource is responsible for listening to geolocation | ||
// permissions from the operation system and allows the | ||
// device::GeolocationManager to access it in a platform agnostic manner. This | ||
// concrete implementation is to be used within the Ash browser. | ||
class SystemGeolocationSource : public device::SystemGeolocationSource, | ||
public SessionObserver { | ||
public: | ||
SystemGeolocationSource(); | ||
~SystemGeolocationSource() override; | ||
|
||
static std::unique_ptr<device::GeolocationManager> | ||
CreateGeolocationManagerOnAsh(); | ||
|
||
// device::SystemGeolocationSource: | ||
void RegisterPermissionUpdateCallback( | ||
PermissionUpdateCallback callback) override; | ||
|
||
private: | ||
// SessionObserver: | ||
void OnActiveUserPrefServiceChanged(PrefService* pref_service) override; | ||
|
||
void OnPrefChanged(const std::string& pref_name); | ||
|
||
PermissionUpdateCallback permission_update_callback_; | ||
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | ||
base::ScopedObservation<SessionController, SessionObserver> observer_{this}; | ||
}; | ||
|
||
} // namespace ash | ||
#endif // CHROME_BROWSER_ASH_GEOLOCATION_SYSTEM_GEOLOCATION_SOURCE_H_ |
59 changes: 59 additions & 0 deletions
59
chrome/browser/ash/geolocation/system_geolocation_source_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 "ash/system/privacy_hub/camera_privacy_switch_controller.h" | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
#include "ash/constants/ash_pref_names.h" | ||
#include "ash/session/session_controller_impl.h" | ||
#include "ash/shell.h" | ||
#include "ash/test/ash_test_base.h" | ||
#include "base/test/repeating_test_future.h" | ||
#include "base/test/scoped_feature_list.h" | ||
#include "chrome/browser/ash/geolocation/system_geolocation_source.h" | ||
#include "components/prefs/pref_service.h" | ||
|
||
namespace ash { | ||
|
||
class SystemGeolocationSourceTests : public AshTestBase { | ||
protected: | ||
SystemGeolocationSourceTests() | ||
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) { | ||
scoped_feature_list_.InitAndEnableFeature(ash::features::kCrosPrivacyHub); | ||
} | ||
|
||
// AshTestBase: | ||
void SetUp() override { AshTestBase::SetUp(); } | ||
|
||
void SetUserPref(bool allowed) { | ||
Shell::Get()->session_controller()->GetActivePrefService()->SetBoolean( | ||
prefs::kUserGeolocationAllowed, allowed); | ||
} | ||
|
||
base::test::ScopedFeatureList scoped_feature_list_; | ||
}; | ||
|
||
TEST_F(SystemGeolocationSourceTests, PermissionUpdate) { | ||
SystemGeolocationSource source; | ||
base::test::RepeatingTestFuture<device::LocationSystemPermissionStatus> | ||
status; | ||
|
||
source.RegisterPermissionUpdateCallback(status.GetCallback()); | ||
|
||
// Initial value should be to allow. | ||
EXPECT_EQ(device::LocationSystemPermissionStatus::kAllowed, status.Take()); | ||
|
||
// Change user settings to deny and check that the callback is called. | ||
SetUserPref(false); | ||
EXPECT_EQ(device::LocationSystemPermissionStatus::kDenied, status.Take()); | ||
|
||
// Change user settings back to allowedy and check that the callback is | ||
// called. | ||
SetUserPref(true); | ||
EXPECT_EQ(device::LocationSystemPermissionStatus::kAllowed, status.Take()); | ||
} | ||
|
||
} // namespace ash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.