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 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 9 | #include "content/browser/permissions/permission_service_impl.h" |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 10 | #include "content/public/browser/browser_context.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 11 | #include "content/public/browser/navigation_details.h" |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 12 | #include "content/public/browser/permission_manager.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 13 | #include "content/public/browser/render_frame_host.h" |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 14 | #include "content/public/browser/render_process_host.h" |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 15 | #include "content/public/browser/web_contents.h" |
| 16 | |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 17 | using blink::mojom::PermissionObserverPtr; |
| 18 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 19 | namespace content { |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 20 | |
| 21 | class PermissionServiceContext::PermissionSubscription { |
| 22 | public: |
| 23 | PermissionSubscription(PermissionServiceContext* context, |
| 24 | PermissionObserverPtr observer) |
| 25 | : context_(context), observer_(std::move(observer)) { |
| 26 | observer_.set_connection_error_handler(base::Bind( |
| 27 | &PermissionSubscription::OnConnectionError, base::Unretained(this))); |
| 28 | } |
| 29 | |
| 30 | ~PermissionSubscription() = default; |
| 31 | |
| 32 | void OnConnectionError() { |
| 33 | DCHECK_NE(id_, 0); |
| 34 | context_->ObserverHadConnectionError(id_); |
| 35 | } |
| 36 | |
| 37 | void OnPermissionStatusChanged(blink::mojom::PermissionStatus status) { |
| 38 | observer_->OnPermissionStatusChange(status); |
| 39 | } |
| 40 | |
| 41 | void set_id(int id) { id_ = id; } |
| 42 | |
| 43 | private: |
| 44 | PermissionServiceContext* context_; |
| 45 | PermissionObserverPtr observer_; |
| 46 | int id_ = 0; |
| 47 | }; |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 48 | |
| 49 | PermissionServiceContext::PermissionServiceContext( |
| 50 | RenderFrameHost* render_frame_host) |
| 51 | : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 52 | render_frame_host_(render_frame_host), |
| 53 | render_process_host_(nullptr) { |
| 54 | } |
| 55 | |
| 56 | PermissionServiceContext::PermissionServiceContext( |
| 57 | RenderProcessHost* render_process_host) |
| 58 | : WebContentsObserver(nullptr), |
| 59 | render_frame_host_(nullptr), |
| 60 | render_process_host_(render_process_host) { |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | PermissionServiceContext::~PermissionServiceContext() { |
| 64 | } |
| 65 | |
| 66 | void PermissionServiceContext::CreateService( |
mathp | cc29ae5 | 2016-05-04 15:22:17 | [diff] [blame] | 67 | mojo::InterfaceRequest<blink::mojom::PermissionService> request) { |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 68 | services_.push_back( |
| 69 | base::MakeUnique<PermissionServiceImpl>(this, std::move(request))); |
| 70 | } |
| 71 | |
| 72 | void PermissionServiceContext::CreateSubscription( |
| 73 | PermissionType permission_type, |
| 74 | const url::Origin& origin, |
| 75 | PermissionObserverPtr observer) { |
| 76 | BrowserContext* browser_context = GetBrowserContext(); |
| 77 | DCHECK(browser_context); |
| 78 | if (!browser_context->GetPermissionManager()) |
| 79 | return; |
| 80 | |
| 81 | auto subscription = |
| 82 | base::MakeUnique<PermissionSubscription>(this, std::move(observer)); |
| 83 | GURL requesting_origin(origin.Serialize()); |
| 84 | GURL embedding_origin = GetEmbeddingOrigin(); |
| 85 | int subscription_id = |
| 86 | browser_context->GetPermissionManager()->SubscribePermissionStatusChange( |
| 87 | permission_type, requesting_origin, |
| 88 | // If the embedding_origin is empty, we'll use the |origin| instead. |
| 89 | embedding_origin.is_empty() ? requesting_origin : embedding_origin, |
| 90 | base::Bind(&PermissionSubscription::OnPermissionStatusChanged, |
| 91 | base::Unretained(subscription.get()))); |
| 92 | subscription->set_id(subscription_id); |
| 93 | subscriptions_[subscription_id] = std::move(subscription); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void PermissionServiceContext::ServiceHadConnectionError( |
| 97 | PermissionServiceImpl* service) { |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 98 | auto it = std::find_if( |
| 99 | services_.begin(), services_.end(), |
| 100 | [service](const std::unique_ptr<PermissionServiceImpl>& this_service) { |
| 101 | return service == this_service.get(); |
| 102 | }); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 103 | DCHECK(it != services_.end()); |
| 104 | services_.erase(it); |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 105 | } |
| 106 | |
| 107 | void PermissionServiceContext::ObserverHadConnectionError(int subscription_id) { |
| 108 | BrowserContext* browser_context = GetBrowserContext(); |
| 109 | DCHECK(browser_context); |
| 110 | if (browser_context->GetPermissionManager()) { |
| 111 | browser_context->GetPermissionManager()->UnsubscribePermissionStatusChange( |
| 112 | subscription_id); |
| 113 | } |
| 114 | |
| 115 | auto it = subscriptions_.find(subscription_id); |
| 116 | DCHECK(it != subscriptions_.end()); |
| 117 | subscriptions_.erase(it); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 118 | } |
| 119 | |
mlamouri | 4ba6e52 | 2015-06-04 17:29:36 | [diff] [blame] | 120 | void PermissionServiceContext::RenderFrameHostChanged( |
| 121 | RenderFrameHost* old_host, |
| 122 | RenderFrameHost* new_host) { |
| 123 | CancelPendingOperations(old_host); |
| 124 | } |
| 125 | |
lalitm | 5bd7eb04 | 2015-10-02 16:00:29 | [diff] [blame] | 126 | void PermissionServiceContext::FrameDeleted( |
| 127 | RenderFrameHost* render_frame_host) { |
| 128 | CancelPendingOperations(render_frame_host); |
| 129 | } |
| 130 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 131 | void PermissionServiceContext::DidNavigateAnyFrame( |
| 132 | RenderFrameHost* render_frame_host, |
| 133 | const LoadCommittedDetails& details, |
| 134 | const FrameNavigateParams& params) { |
| 135 | if (details.is_in_page) |
| 136 | return; |
| 137 | |
mlamouri | a500a0f | 2015-04-01 23:30:57 | [diff] [blame] | 138 | CancelPendingOperations(render_frame_host); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 139 | } |
| 140 | |
mlamouri | a500a0f | 2015-04-01 23:30:57 | [diff] [blame] | 141 | void PermissionServiceContext::CancelPendingOperations( |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 142 | RenderFrameHost* render_frame_host) const { |
mlamouri | 4ba6e52 | 2015-06-04 17:29:36 | [diff] [blame] | 143 | DCHECK(render_frame_host_); |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 144 | if (render_frame_host != render_frame_host_) |
| 145 | return; |
| 146 | |
reillyg | 86dfcea | 2017-01-04 22:27:57 | [diff] [blame^] | 147 | for (const auto& service : services_) |
mlamouri | a500a0f | 2015-04-01 23:30:57 | [diff] [blame] | 148 | service->CancelPendingOperations(); |
timvolodine | 14570267 | 2014-12-09 18:18:44 | [diff] [blame] | 149 | } |
| 150 | |
timvolodine | 089c07c | 2014-12-11 19:32:26 | [diff] [blame] | 151 | BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
| 152 | if (!web_contents()) { |
| 153 | DCHECK(render_process_host_); |
| 154 | return render_process_host_->GetBrowserContext(); |
| 155 | } |
| 156 | return web_contents()->GetBrowserContext(); |
| 157 | } |
| 158 | |
| 159 | GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
| 160 | return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() |
| 161 | : GURL(); |
| 162 | } |
| 163 | |
mlamouri | 5fc460f | 2015-06-03 17:30:28 | [diff] [blame] | 164 | RenderFrameHost* PermissionServiceContext::render_frame_host() const { |
| 165 | return render_frame_host_; |
| 166 | } |
| 167 | |
mlamouri | 4ebe69f | 2014-11-14 17:25:21 | [diff] [blame] | 168 | } // namespace content |