mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 1 | // Copyright 2014 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 | |
| 5 | #include "content/browser/permissions/permission_service_context.h" |
| 6 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 9 | #include "base/bind.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 10 | #include "base/memory/raw_ptr.h" |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 11 | #include "content/browser/permissions/permission_controller_impl.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 12 | #include "content/browser/permissions/permission_service_impl.h" |
Illia Klimov | 7709f0c | 2021-07-05 09:42:39 | [diff] [blame] | 13 | #include "content/browser/permissions/permission_util.h" |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 14 | #include "content/public/browser/browser_context.h" |
jam | 5c2473dc | 2017-02-06 15:51:21 | [diff] [blame] | 15 | #include "content/public/browser/navigation_handle.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 16 | #include "content/public/browser/render_frame_host.h" |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 17 | #include "content/public/browser/render_process_host.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 18 | #include "content/public/browser/web_contents.h" |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 19 | #include "mojo/public/cpp/bindings/remote.h" |
Andy Paicu | bdb19c7 | 2022-04-22 09:24:31 | [diff] [blame^] | 20 | #include "url/origin.h" |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 21 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 22 | namespace content { |
| 23 | |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 24 | // A holder owning document-associated PermissionServiceContext. The holder is |
| 25 | // used as PermissionServiceContext itself can't inherit from |
Sreeja Kamishetty | 0a0961f | 2021-10-11 16:23:53 | [diff] [blame] | 26 | // DocumentUserData, as PermissionServiceContext (unlike |
| 27 | // DocumentUserData) can be created when RenderFrameHost doesn't exist |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 28 | // (e.g. for service workers). |
| 29 | struct PermissionServiceContext::DocumentPermissionServiceContextHolder |
Sreeja Kamishetty | 0a0961f | 2021-10-11 16:23:53 | [diff] [blame] | 30 | : public DocumentUserData<DocumentPermissionServiceContextHolder> { |
Lukasz Anforowicz | cfeb95c | 2021-10-01 19:33:35 | [diff] [blame] | 31 | explicit DocumentPermissionServiceContextHolder(RenderFrameHost* rfh) |
Sreeja Kamishetty | 0a0961f | 2021-10-11 16:23:53 | [diff] [blame] | 32 | : DocumentUserData<DocumentPermissionServiceContextHolder>(rfh), |
Lukasz Anforowicz | cfeb95c | 2021-10-01 19:33:35 | [diff] [blame] | 33 | permission_service_context(rfh) {} |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 34 | |
| 35 | PermissionServiceContext permission_service_context; |
| 36 | |
Sreeja Kamishetty | 0a0961f | 2021-10-11 16:23:53 | [diff] [blame] | 37 | DOCUMENT_USER_DATA_KEY_DECL(); |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 38 | }; |
| 39 | |
Sreeja Kamishetty | 0a0961f | 2021-10-11 16:23:53 | [diff] [blame] | 40 | DOCUMENT_USER_DATA_KEY_IMPL( |
Daniel Cheng | 383df85 | 2021-10-02 03:28:01 | [diff] [blame] | 41 | PermissionServiceContext::DocumentPermissionServiceContextHolder); |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 42 | |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 43 | class PermissionServiceContext::PermissionSubscription { |
| 44 | public: |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 45 | PermissionSubscription( |
| 46 | PermissionServiceContext* context, |
| 47 | mojo::PendingRemote<blink::mojom::PermissionObserver> observer) |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 48 | : context_(context), observer_(std::move(observer)) { |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 49 | observer_.set_disconnect_handler(base::BindOnce( |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 50 | &PermissionSubscription::OnConnectionError, base::Unretained(this))); |
| 51 | } |
Lei Zhang | 67b7bf3 | 2019-10-08 18:46:01 | [diff] [blame] | 52 | PermissionSubscription(const PermissionSubscription&) = delete; |
| 53 | PermissionSubscription& operator=(const PermissionSubscription&) = delete; |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 54 | |
reillyg | 9c28b5c | 2017-01-06 20:51:31 | [diff] [blame] | 55 | ~PermissionSubscription() { |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 56 | DCHECK(id_); |
reillyg | 9c28b5c | 2017-01-06 20:51:31 | [diff] [blame] | 57 | BrowserContext* browser_context = context_->GetBrowserContext(); |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 58 | if (browser_context) { |
| 59 | PermissionControllerImpl::FromBrowserContext(browser_context) |
reillyg | 9c28b5c | 2017-01-06 20:51:31 | [diff] [blame] | 60 | ->UnsubscribePermissionStatusChange(id_); |
| 61 | } |
| 62 | } |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 63 | |
| 64 | void OnConnectionError() { |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 65 | DCHECK(id_); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 66 | context_->ObserverHadConnectionError(id_); |
| 67 | } |
| 68 | |
| 69 | void OnPermissionStatusChanged(blink::mojom::PermissionStatus status) { |
Ravjit Singh Uppal | e306d3fa | 2021-07-05 13:47:45 | [diff] [blame] | 70 | if (observer_.is_connected()) |
| 71 | observer_->OnPermissionStatusChange(status); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 72 | } |
| 73 | |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 74 | void set_id(PermissionController::SubscriptionId id) { id_ = id; } |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 75 | |
| 76 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 77 | const raw_ptr<PermissionServiceContext> context_; |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 78 | mojo::Remote<blink::mojom::PermissionObserver> observer_; |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 79 | PermissionController::SubscriptionId id_; |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 80 | }; |
| 81 | |
Alexander Timin | 72a2f0c | 2021-10-01 01:56:07 | [diff] [blame] | 82 | // static |
| 83 | PermissionServiceContext* PermissionServiceContext::GetForCurrentDocument( |
| 84 | RenderFrameHost* render_frame_host) { |
| 85 | return &DocumentPermissionServiceContextHolder::GetOrCreateForCurrentDocument( |
| 86 | render_frame_host) |
| 87 | ->permission_service_context; |
| 88 | } |
Carlos Caballero | a0955348 | 2021-03-17 15:51:06 | [diff] [blame] | 89 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 90 | PermissionServiceContext::PermissionServiceContext( |
| 91 | RenderFrameHost* render_frame_host) |
Ravjit | 715c2b4 | 2021-07-15 15:25:01 | [diff] [blame] | 92 | : render_frame_host_(render_frame_host), render_process_host_(nullptr) { |
| 93 | render_frame_host->GetProcess()->AddObserver(this); |
| 94 | } |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 95 | |
| 96 | PermissionServiceContext::PermissionServiceContext( |
| 97 | RenderProcessHost* render_process_host) |
Carlos Caballero | a0955348 | 2021-03-17 15:51:06 | [diff] [blame] | 98 | : render_frame_host_(nullptr), render_process_host_(render_process_host) {} |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 99 | |
| 100 | PermissionServiceContext::~PermissionServiceContext() { |
Ravjit | 715c2b4 | 2021-07-15 15:25:01 | [diff] [blame] | 101 | if (render_frame_host_) |
| 102 | render_frame_host_->GetProcess()->RemoveObserver(this); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void PermissionServiceContext::CreateService( |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 106 | mojo::PendingReceiver<blink::mojom::PermissionService> receiver) { |
Sam McNally | 2e5c71f | 2017-12-11 03:24:27 | [diff] [blame] | 107 | DCHECK(render_frame_host_); |
Andy Paicu | bdb19c7 | 2022-04-22 09:24:31 | [diff] [blame^] | 108 | services_.Add( |
| 109 | std::make_unique<PermissionServiceImpl>( |
| 110 | this, url::Origin::Create(PermissionUtil::GetLastCommittedOriginAsURL( |
| 111 | render_frame_host_))), |
| 112 | std::move(receiver)); |
Sam McNally | 2e5c71f | 2017-12-11 03:24:27 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void PermissionServiceContext::CreateServiceForWorker( |
Miyoung Shin | e676be1f | 2019-09-25 00:47:02 | [diff] [blame] | 116 | const url::Origin& origin, |
| 117 | mojo::PendingReceiver<blink::mojom::PermissionService> receiver) { |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 118 | services_.Add(std::make_unique<PermissionServiceImpl>(this, origin), |
| 119 | std::move(receiver)); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void PermissionServiceContext::CreateSubscription( |
| 123 | PermissionType permission_type, |
| 124 | const url::Origin& origin, |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 125 | blink::mojom::PermissionStatus current_status, |
| 126 | blink::mojom::PermissionStatus last_known_status, |
| 127 | mojo::PendingRemote<blink::mojom::PermissionObserver> observer) { |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 128 | BrowserContext* browser_context = GetBrowserContext(); |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 129 | if (!browser_context) |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 130 | return; |
| 131 | |
| 132 | auto subscription = |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 133 | std::make_unique<PermissionSubscription>(this, std::move(observer)); |
Mario Sanchez Prada | 97f6616 | 2019-08-06 21:56:48 | [diff] [blame] | 134 | |
| 135 | if (current_status != last_known_status) { |
| 136 | subscription->OnPermissionStatusChanged(current_status); |
| 137 | last_known_status = current_status; |
| 138 | } |
| 139 | |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 140 | GURL requesting_origin(origin.Serialize()); |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 141 | auto subscription_id = |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 142 | PermissionControllerImpl::FromBrowserContext(browser_context) |
| 143 | ->SubscribePermissionStatusChange( |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 144 | permission_type, render_process_host_, render_frame_host_, |
| 145 | requesting_origin, |
Lei Zhang | 67b7bf3 | 2019-10-08 18:46:01 | [diff] [blame] | 146 | base::BindRepeating( |
| 147 | &PermissionSubscription::OnPermissionStatusChanged, |
| 148 | base::Unretained(subscription.get()))); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 149 | subscription->set_id(subscription_id); |
| 150 | subscriptions_[subscription_id] = std::move(subscription); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 151 | } |
| 152 | |
Balazs Engedy | ad1489b | 2021-03-31 07:47:19 | [diff] [blame] | 153 | void PermissionServiceContext::ObserverHadConnectionError( |
| 154 | PermissionController::SubscriptionId subscription_id) { |
Lei Zhang | 67b7bf3 | 2019-10-08 18:46:01 | [diff] [blame] | 155 | size_t erased = subscriptions_.erase(subscription_id); |
| 156 | DCHECK_EQ(1u, erased); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame] | 157 | } |
| 158 | |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 159 | BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
Carlos Caballero | a0955348 | 2021-03-17 15:51:06 | [diff] [blame] | 160 | if (render_frame_host_) |
| 161 | return render_frame_host_->GetBrowserContext(); |
Dominic Mazzoni | 94e567bf | 2017-08-29 18:07:02 | [diff] [blame] | 162 | |
| 163 | if (render_process_host_) |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 164 | return render_process_host_->GetBrowserContext(); |
Dominic Mazzoni | 94e567bf | 2017-08-29 18:07:02 | [diff] [blame] | 165 | |
| 166 | return nullptr; |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
Henrique Ferreiro | f89870f | 2022-03-30 17:08:13 | [diff] [blame] | 170 | return render_frame_host_ ? PermissionUtil::GetLastCommittedOriginAsURL( |
| 171 | render_frame_host_->GetMainFrame()) |
| 172 | : GURL(); |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 173 | } |
| 174 | |
Ravjit | 715c2b4 | 2021-07-15 15:25:01 | [diff] [blame] | 175 | void PermissionServiceContext::RenderProcessHostDestroyed( |
| 176 | RenderProcessHost* host) { |
| 177 | DCHECK(host == render_frame_host_->GetProcess()); |
| 178 | subscriptions_.clear(); |
| 179 | // RenderProcessHostImpl will always outlive 'this', but it gets cleaned up |
| 180 | // earlier so we need to listen to this event so we can do our clean up as |
| 181 | // well. |
| 182 | host->RemoveObserver(this); |
| 183 | } |
| 184 | |
Lei Zhang | 67b7bf3 | 2019-10-08 18:46:01 | [diff] [blame] | 185 | } // namespace content |