Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 1 | // 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 Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 8 | #include "chrome/browser/device_api/device_attribute_api.h" |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 9 | #include "chrome/browser/profiles/profile.h" |
| 10 | #include "chrome/browser/web_applications/components/policy/web_app_policy_constants.h" |
Anqing Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 11 | #include "chrome/common/chrome_features.h" |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 12 | #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 | |
| 18 | namespace { |
| 19 | |
| 20 | bool IsTrustedContext(content::RenderFrameHost* host, |
| 21 | const url::Origin& origin) { |
Anqing Zhao | 1814497 | 2021-06-19 09:20:35 | [diff] [blame] | 22 | // 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 Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 24 | if (!base::FeatureList::IsEnabled(features::kEnableRestrictedWebApis)) |
| 25 | return false; |
| 26 | |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 27 | PrefService* prefs = |
| 28 | Profile::FromBrowserContext(host->GetBrowserContext())->GetPrefs(); |
| 29 | |
Anqing Zhao | 1814497 | 2021-06-19 09:20:35 | [diff] [blame] | 30 | if (!prefs->GetBoolean( |
| 31 | prefs::kManagedWebAppsAccessToDeviceAttributesAllowed)) { |
| 32 | return false; |
| 33 | } |
| 34 | |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 35 | // 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 | |
| 48 | DeviceServiceImpl::DeviceServiceImpl( |
| 49 | content::RenderFrameHost* host, |
| 50 | mojo::PendingReceiver<blink::mojom::DeviceAPIService> receiver) |
Alexander Timin | a085dd4 | 2021-06-04 16:55:41 | [diff] [blame] | 51 | : DocumentServiceBase(host, std::move(receiver)), host_(host) { |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 52 | pref_change_registrar_.Init( |
| 53 | Profile::FromBrowserContext(host->GetBrowserContext())->GetPrefs()); |
| 54 | pref_change_registrar_.Add( |
Anqing Zhao | 1814497 | 2021-06-19 09:20:35 | [diff] [blame] | 55 | prefs::kManagedWebAppsAccessToDeviceAttributesAllowed, |
| 56 | base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded, |
| 57 | base::Unretained(this))); |
| 58 | pref_change_registrar_.Add( |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 59 | prefs::kWebAppInstallForceList, |
Anqing Zhao | 1814497 | 2021-06-19 09:20:35 | [diff] [blame] | 60 | base::BindRepeating(&DeviceServiceImpl::OnDisposingIfNeeded, |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 61 | base::Unretained(this))); |
| 62 | } |
Anqing Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 63 | |
Anatoliy Potapchuk | dbe8904 | 2021-03-30 23:04:02 | [diff] [blame] | 64 | DeviceServiceImpl::~DeviceServiceImpl() = default; |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 65 | |
| 66 | // static |
| 67 | void 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 Timin | a085dd4 | 2021-06-04 16:55:41 | [diff] [blame] | 79 | // connection. See DocumentServiceBase for details. |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 80 | new DeviceServiceImpl(host, std::move(receiver)); |
| 81 | } |
| 82 | |
Anqing Zhao | 9595e48 | 2021-06-10 11:33:55 | [diff] [blame] | 83 | // static |
| 84 | void DeviceServiceImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 85 | registry->RegisterBooleanPref( |
| 86 | prefs::kManagedWebAppsAccessToDeviceAttributesAllowed, true); |
| 87 | } |
| 88 | |
Anqing Zhao | 1814497 | 2021-06-19 09:20:35 | [diff] [blame] | 89 | void DeviceServiceImpl::OnDisposingIfNeeded() { |
Anatoliy Potapchuk | 096e4d0a | 2020-12-01 16:49:21 | [diff] [blame] | 90 | // 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 Potapchuk | 04b379f3 | 2020-12-16 23:03:23 | [diff] [blame] | 96 | |
Anqing Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 97 | void DeviceServiceImpl::GetDirectoryId(GetDirectoryIdCallback callback) { |
| 98 | device_attribute_api::GetDirectoryId(std::move(callback)); |
| 99 | } |
| 100 | |
Anqing Zhao | c284ebb | 2021-03-20 21:28:43 | [diff] [blame] | 101 | void DeviceServiceImpl::GetHostname(GetHostnameCallback callback) { |
| 102 | device_attribute_api::GetHostname(std::move(callback)); |
| 103 | } |
| 104 | |
Anqing Zhao | c7136af | 2021-01-20 05:52:39 | [diff] [blame] | 105 | void DeviceServiceImpl::GetSerialNumber(GetSerialNumberCallback callback) { |
| 106 | device_attribute_api::GetSerialNumber(std::move(callback)); |
| 107 | } |
| 108 | |
| 109 | void DeviceServiceImpl::GetAnnotatedAssetId( |
| 110 | GetAnnotatedAssetIdCallback callback) { |
| 111 | device_attribute_api::GetAnnotatedAssetId(std::move(callback)); |
| 112 | } |
| 113 | |
| 114 | void DeviceServiceImpl::GetAnnotatedLocation( |
| 115 | GetAnnotatedLocationCallback callback) { |
| 116 | device_attribute_api::GetAnnotatedLocation(std::move(callback)); |
| 117 | } |