blob: a8167a113614d26afd09e33e3c2fb526627089e9 [file] [log] [blame]
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:211// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#include "chrome/browser/device_api/device_service_impl.h"
5
6#include <memory>
7
Anqing Zhaoc7136af2021-01-20 05:52:398#include "chrome/browser/device_api/device_attribute_api.h"
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:219#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/web_applications/components/policy/web_app_policy_constants.h"
Anqing Zhaoc7136af2021-01-20 05:52:3911#include "chrome/common/chrome_features.h"
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2112#include "chrome/common/pref_names.h"
13#include "components/prefs/pref_service.h"
14#include "content/public/browser/browser_thread.h"
15#include "mojo/public/cpp/bindings/pending_receiver.h"
16#include "url/origin.h"
17
18namespace {
19
20bool IsTrustedContext(content::RenderFrameHost* host,
21 const url::Origin& origin) {
Anqing Zhao18144972021-06-19 09:20:3522 // TODO(anqing): This feature flag is turned on by default for origin trial.
23 // The flag will be removed when permission policies are ready.
Anqing Zhaoc7136af2021-01-20 05:52:3924 if (!base::FeatureList::IsEnabled(features::kEnableRestrictedWebApis))
25 return false;
26
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2127 PrefService* prefs =
28 Profile::FromBrowserContext(host->GetBrowserContext())->GetPrefs();
29
Anqing Zhao18144972021-06-19 09:20:3530 if (!prefs->GetBoolean(
31 prefs::kManagedWebAppsAccessToDeviceAttributesAllowed)) {
32 return false;
33 }
34
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2135 // TODO(apotapchuk): Implement a more efficient way of checking the trustness
36 // status of the app.
37 for (const base::Value& entry :
38 prefs->GetList(prefs::kWebAppInstallForceList)->GetList()) {
39 if (origin ==
40 url::Origin::Create(GURL(entry.FindKey(web_app::kUrlKey)->GetString())))
41 return true;
42 }
43 return false;
44}
45
46} // namespace
47
48DeviceServiceImpl::DeviceServiceImpl(
49 content::RenderFrameHost* host,
50 mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver)
Alexander Timina085dd42021-06-04 16:55:4151 : DocumentServiceBase(host, std::move(receiver)), host_(host) {
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2152 pref_change_registrar_.Init(
53 Profile::FromBrowserContext(host->GetBrowserContext())->GetPrefs());
54 pref_change_registrar_.Add(
Anqing Zhao18144972021-06-19 09:20:3555 prefs::kManagedWebAppsAccessToDeviceAttributesAllowed,
56 base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
57 base::Unretained(this)));
58 pref_change_registrar_.Add(
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2159 prefs::kWebAppInstallForceList,
Anqing Zhao18144972021-06-19 09:20:3560 base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded,
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2161 base::Unretained(this)));
62}
Anqing Zhaoc7136af2021-01-20 05:52:3963
Anatoliy Potapchukdbe89042021-03-30 23:04:0264DeviceServiceImpl::~DeviceServiceImpl() = default;
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2165
66// static
67void DeviceServiceImpl::Create(
68 content::RenderFrameHost* host,
69 mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver) {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
71
72 if (!IsTrustedContext(host,
73 url::Origin::Create(host->GetLastCommittedURL()))) {
74 // Not sending bad message here since the API is always exposed to the end
75 // user.
76 return;
77 }
78 // The object is bound to the lifetime of |host| and the mojo
Alexander Timina085dd42021-06-04 16:55:4179 // connection. See DocumentServiceBase for details.
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2180 new DeviceServiceImpl(host, std::move(receiver));
81}
82
Anqing Zhao9595e482021-06-10 11:33:5583// static
84void DeviceServiceImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) {
85 registry->RegisterBooleanPref(
86 prefs::kManagedWebAppsAccessToDeviceAttributesAllowed, true);
87}
88
Anqing Zhao18144972021-06-19 09:20:3589void DeviceServiceImpl::OnDisposingIfNeeded() {
Anatoliy Potapchuk096e4d0a2020-12-01 16:49:2190 // DeviceServiceImpl is allocated on the heap, thus it is safe to remove it
91 // like this.
92 if (!IsTrustedContext(host_, origin())) {
93 delete this;
94 }
95}
Anatoliy Potapchuk04b379f32020-12-16 23:03:2396
Anqing Zhaoc7136af2021-01-20 05:52:3997void DeviceServiceImpl::GetDirectoryId(GetDirectoryIdCallback callback) {
98 device_attribute_api::GetDirectoryId(std::move(callback));
99}
100
Anqing Zhaoc284ebb2021-03-20 21:28:43101void DeviceServiceImpl::GetHostname(GetHostnameCallback callback) {
102 device_attribute_api::GetHostname(std::move(callback));
103}
104
Anqing Zhaoc7136af2021-01-20 05:52:39105void DeviceServiceImpl::GetSerialNumber(GetSerialNumberCallback callback) {
106 device_attribute_api::GetSerialNumber(std::move(callback));
107}
108
109void DeviceServiceImpl::GetAnnotatedAssetId(
110 GetAnnotatedAssetIdCallback callback) {
111 device_attribute_api::GetAnnotatedAssetId(std::move(callback));
112}
113
114void DeviceServiceImpl::GetAnnotatedLocation(
115 GetAnnotatedLocationCallback callback) {
116 device_attribute_api::GetAnnotatedLocation(std::move(callback));
117}