1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2020 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/device_api/device_service_impl.h"
#include <optional>
#include "base/check_deref.h"
#include "base/check_is_test.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/device_api/device_attribute_api.h"
#include "chrome/browser/policy/policy_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/isolated_web_apps/policy/isolated_web_app_policy_constants.h"
#include "chrome/browser/web_applications/policy/web_app_policy_constants.h"
#include "chrome/browser/web_applications/proto/proto_helpers.h"
#include "chrome/browser/web_applications/proto/web_app.pb.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/web_app_filter.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/common/pref_names.h"
#include "components/permissions/features.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "third_party/blink/public/common/features_generated.h"
#include "url/gurl.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#include "chrome/browser/ash/app_mode/isolated_web_app/kiosk_iwa_data.h"
#include "chrome/browser/ash/app_mode/isolated_web_app/kiosk_iwa_manager.h"
#include "chrome/browser/ash/app_mode/web_app/web_kiosk_app_data.h"
#include "chrome/browser/ash/app_mode/web_app/web_kiosk_app_manager.h"
#include "chrome/common/url_constants.h"
#include "components/user_manager/user_manager.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace {
#if BUILDFLAG(IS_CHROMEOS)
// Checks that current user is a web kiosk.
bool IsWebKiosk() {
return user_manager::UserManager::IsInitialized() &&
user_manager::UserManager::Get()->IsLoggedInAsKioskWebApp();
}
// Checks that current user is an IWA kiosk.
bool IsIwaKiosk() {
return ash::features::IsIsolatedWebAppKioskEnabled() &&
user_manager::UserManager::IsInitialized() &&
user_manager::UserManager::Get()->IsLoggedInAsKioskIWA();
}
// Returns an origin of the current kiosk web app.
// Should only be called when the current user is a web kiosk.
url::Origin GetWebKioskOrigin() {
const AccountId& account_id =
user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId();
CHECK(ash::WebKioskAppManager::IsInitialized());
const ash::WebKioskAppData* app_data =
ash::WebKioskAppManager::Get()->GetAppByAccountId(account_id);
return url::Origin::Create(CHECK_DEREF(app_data).install_url());
}
// Returns an origin of the current kiosk isolated web app.
// Should only be called when the current user is an IWA kiosk.
url::Origin GetIwaKioskOrigin() {
const AccountId& account_id =
user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId();
const ash::KioskIwaData* iwa_data =
CHECK_DEREF(ash::KioskIwaManager::Get()).GetApp(account_id);
return CHECK_DEREF(iwa_data).origin();
}
std::optional<url::Origin> MaybeGetCurrentKioskOrigin() {
if (IsWebKiosk()) {
return GetWebKioskOrigin();
}
if (IsIwaKiosk()) {
return GetIwaKioskOrigin();
}
return std::nullopt;
}
// Check whether the target origin is the same as the main application running
// in the Kiosk session.
bool IsEqualToKioskOrigin(const url::Origin& origin) {
std::optional<url::Origin> current_kiosk_origin =
MaybeGetCurrentKioskOrigin();
return current_kiosk_origin.has_value() &&
(current_kiosk_origin.value() == origin);
}
#endif // BUILDFLAG(IS_CHROMEOS)
Profile* GetProfile(content::RenderFrameHost& host) {
return Profile::FromBrowserContext(host.GetBrowserContext());
}
// Check whether an app with the target origin is in the WebAppRegistrar.
bool IsForceInstalledOrigin(content::RenderFrameHost& host,
const url::Origin& origin) {
web_app::WebAppProvider* web_app_provider =
web_app::WebAppProvider::GetForWebApps(GetProfile(host));
if (!web_app_provider) {
return false;
}
// In this case we will not modify any data so it is safe to access
// registrar without lock
const web_app::WebAppRegistrar& registrar =
web_app_provider->registrar_unsafe();
const auto app_id = registrar.FindBestAppWithUrlInScope(
origin.GetURL(), web_app::WebAppFilter::InstalledInChrome());
if (!app_id.has_value()) {
return false;
}
return registrar.IsInstalledByPolicy(app_id.value());
}
const PrefService* GetPrefs(content::RenderFrameHost& host) {
return GetProfile(host)->GetPrefs();
}
bool IsAffiliatedUser() {
#if BUILDFLAG(IS_CHROMEOS)
const user_manager::User* user =
user_manager::UserManager::Get()->GetPrimaryUser();
return (user != nullptr) && user->IsAffiliated();
#else
return false;
#endif
}
bool IsTrustedContext(content::RenderFrameHost& host,
const url::Origin& origin) {
// Do not create the service for the incognito mode.
if (GetProfile(host)->IsIncognitoProfile()) {
return false;
}
#if BUILDFLAG(IS_CHROMEOS)
if (IsRunningInAppMode()) {
if (base::FeatureList::IsEnabled(
permissions::features::
kAllowMultipleOriginsForWebKioskPermissions)) {
return IsEqualToKioskOrigin(origin) ||
IsWebKioskOriginAllowed(GetPrefs(host), origin.GetURL());
}
return IsEqualToKioskOrigin(origin);
}
#endif // BUILDFLAG(IS_CHROMEOS)
return IsForceInstalledOrigin(host, origin);
}
bool IsIwa(content::RenderFrameHost& host) {
if (auto* web_app_id = web_app::WebAppTabHelper::GetAppId(
content::WebContents::FromRenderFrameHost(&host))) {
return web_app::WebAppProvider::GetForWebApps(GetProfile(host))
->registrar_unsafe()
.IsIsolated(*web_app_id);
}
return false;
}
bool IsPermissionsPolicyFeatureEnabled() {
return base::FeatureList::IsEnabled(
blink::features::kDeviceAttributesPermissionPolicy);
}
bool IsAllowedByPermissionsPolicy(content::RenderFrameHost& host) {
if (!IsPermissionsPolicyFeatureEnabled()) {
return true;
}
return host.IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kDeviceAttributes);
}
bool IsBlockedByAdminPolicy(content::RenderFrameHost& host,
const url::Origin& origin) {
if (IsPermissionsPolicyFeatureEnabled() && IsIwa(host)) {
return false;
}
return !policy::IsOriginInAllowlist(
origin.GetURL(), GetPrefs(host),
prefs::kDeviceAttributesAllowedForOrigins);
}
} // namespace
DeviceServiceImpl::DeviceServiceImpl(
content::RenderFrameHost& host,
mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver,
std::unique_ptr<DeviceAttributeApi> device_attribute_api)
: DocumentService(host, std::move(receiver)),
device_attribute_api_(std::move(device_attribute_api)) {
pref_change_registrar_.Init(
Profile::FromBrowserContext(host.GetBrowserContext())->GetPrefs());
pref_change_registrar_.Add(
prefs::kDeviceAttributesAllowedForOrigins,
base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kWebAppInstallForceList,
base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kIsolatedWebAppInstallForceList,
base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
base::Unretained(this)));
#if BUILDFLAG(IS_CHROMEOS)
pref_change_registrar_.Add(
prefs::kKioskBrowserPermissionsAllowedForOrigins,
base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
base::Unretained(this)));
#endif // BUILDFLAG(IS_CHROMEOS)
auto& provider =
CHECK_DEREF(web_app::WebAppProvider::GetForWebApps(GetProfile(host)));
install_manager_observation_.Observe(&provider.install_manager());
}
DeviceServiceImpl::~DeviceServiceImpl() = default;
// static
void DeviceServiceImpl::Create(
content::RenderFrameHost* host,
mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver,
std::unique_ptr<DeviceAttributeApi> device_attribute_api) {
CHECK(host);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (host->GetParentOrOuterDocument()) {
mojo::ReportBadMessage(
"Device Attributes are allowed only in top level frames.");
return;
}
if (!IsTrustedContext(*host, host->GetLastCommittedOrigin())) {
// Not sending bad message here since the API is always exposed to the end
// user.
return;
}
if (!IsAllowedByPermissionsPolicy(*host)) {
mojo::ReportBadMessage(
"Permissions policy blocks access to Device Attributes.");
return;
}
// The object is bound to the lifetime of |host| and the mojo
// connection. See DocumentService for details.
new DeviceServiceImpl(*host, std::move(receiver),
std::move(device_attribute_api));
}
// static
void DeviceServiceImpl::Create(
content::RenderFrameHost* host,
mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver) {
Create(host, std::move(receiver), std::make_unique<DeviceAttributeApiImpl>());
}
// static
void DeviceServiceImpl::CreateForTest(
content::RenderFrameHost* host,
mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver,
std::unique_ptr<DeviceAttributeApi> device_attribute_api) {
CHECK_IS_TEST();
Create(host, std::move(receiver), std::move(device_attribute_api));
}
// static
void DeviceServiceImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kDeviceAttributesAllowedForOrigins);
}
void DeviceServiceImpl::OnWebAppSourceRemoved(const webapps::AppId& app_id) {
OnDisposingIfNeeded();
}
void DeviceServiceImpl::OnWebAppUninstalled(
const webapps::AppId& app_id,
webapps::WebappUninstallSource uninstall_source) {
OnDisposingIfNeeded();
}
void DeviceServiceImpl::OnWebAppInstallManagerDestroyed() {
install_manager_observation_.Reset();
}
void DeviceServiceImpl::OnDisposingIfNeeded() {
// DeviceServiceImpl is allocated on the heap, thus it is safe to remove it
// like this.
if (!IsTrustedContext(render_frame_host(), origin())) {
ResetAndDeleteThis();
}
}
void DeviceServiceImpl::GetDirectoryId(GetDirectoryIdCallback callback) {
GetDeviceAttribute(&DeviceAttributeApi::GetDirectoryId, std::move(callback));
}
void DeviceServiceImpl::GetHostname(GetHostnameCallback callback) {
GetDeviceAttribute(&DeviceAttributeApi::GetHostname, std::move(callback));
}
void DeviceServiceImpl::GetSerialNumber(GetSerialNumberCallback callback) {
GetDeviceAttribute(&DeviceAttributeApi::GetSerialNumber, std::move(callback));
}
void DeviceServiceImpl::GetAnnotatedAssetId(
GetAnnotatedAssetIdCallback callback) {
GetDeviceAttribute(&DeviceAttributeApi::GetAnnotatedAssetId,
std::move(callback));
}
void DeviceServiceImpl::GetAnnotatedLocation(
GetAnnotatedLocationCallback callback) {
GetDeviceAttribute(&DeviceAttributeApi::GetAnnotatedLocation,
std::move(callback));
}
void DeviceServiceImpl::GetDeviceAttribute(
void (DeviceAttributeApi::*method)(DeviceAttributeCallback callback),
DeviceAttributeCallback callback) {
if (!IsAffiliatedUser()) {
device_attribute_api_->ReportNotAffiliatedError(std::move(callback));
return;
}
if (IsBlockedByAdminPolicy(render_frame_host(), origin())) {
device_attribute_api_->ReportNotAllowedError(std::move(callback));
return;
}
(device_attribute_api_.get()->*method)(std::move(callback));
}
|