| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/permissions/permission_service_context.h" | 5 #include "content/browser/permissions/permission_service_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "content/browser/permissions/permission_service_impl.h" | 9 #include "content/browser/permissions/permission_service_impl.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/permission_manager.h" | 12 #include "content/public/browser/permission_manager.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 | 16 |
| 17 using blink::mojom::PermissionObserverPtr; | 17 using blink::mojom::PermissionObserverPtr; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 class PermissionServiceContext::PermissionSubscription { | 21 class PermissionServiceContext::PermissionSubscription { |
| 22 public: | 22 public: |
| 23 PermissionSubscription(PermissionServiceContext* context, | 23 PermissionSubscription(PermissionServiceContext* context, |
| 24 PermissionObserverPtr observer) | 24 PermissionObserverPtr observer) |
| 25 : context_(context), observer_(std::move(observer)) { | 25 : context_(context), observer_(std::move(observer)) { |
| 26 observer_.set_connection_error_handler(base::Bind( | 26 observer_.set_connection_error_handler(base::Bind( |
| 27 &PermissionSubscription::OnConnectionError, base::Unretained(this))); | 27 &PermissionSubscription::OnConnectionError, base::Unretained(this))); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ~PermissionSubscription() = default; | 30 ~PermissionSubscription() { |
| 31 DCHECK_NE(id_, 0); |
| 32 BrowserContext* browser_context = context_->GetBrowserContext(); |
| 33 DCHECK(browser_context); |
| 34 if (browser_context->GetPermissionManager()) { |
| 35 browser_context->GetPermissionManager() |
| 36 ->UnsubscribePermissionStatusChange(id_); |
| 37 } |
| 38 } |
| 31 | 39 |
| 32 void OnConnectionError() { | 40 void OnConnectionError() { |
| 33 DCHECK_NE(id_, 0); | 41 DCHECK_NE(id_, 0); |
| 34 context_->ObserverHadConnectionError(id_); | 42 context_->ObserverHadConnectionError(id_); |
| 35 } | 43 } |
| 36 | 44 |
| 37 void OnPermissionStatusChanged(blink::mojom::PermissionStatus status) { | 45 void OnPermissionStatusChanged(blink::mojom::PermissionStatus status) { |
| 38 observer_->OnPermissionStatusChange(status); | 46 observer_->OnPermissionStatusChange(status); |
| 39 } | 47 } |
| 40 | 48 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 auto it = std::find_if( | 106 auto it = std::find_if( |
| 99 services_.begin(), services_.end(), | 107 services_.begin(), services_.end(), |
| 100 [service](const std::unique_ptr<PermissionServiceImpl>& this_service) { | 108 [service](const std::unique_ptr<PermissionServiceImpl>& this_service) { |
| 101 return service == this_service.get(); | 109 return service == this_service.get(); |
| 102 }); | 110 }); |
| 103 DCHECK(it != services_.end()); | 111 DCHECK(it != services_.end()); |
| 104 services_.erase(it); | 112 services_.erase(it); |
| 105 } | 113 } |
| 106 | 114 |
| 107 void PermissionServiceContext::ObserverHadConnectionError(int subscription_id) { | 115 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 auto it = subscriptions_.find(subscription_id); |
| 116 DCHECK(it != subscriptions_.end()); | 117 DCHECK(it != subscriptions_.end()); |
| 117 subscriptions_.erase(it); | 118 subscriptions_.erase(it); |
| 118 } | 119 } |
| 119 | 120 |
| 120 void PermissionServiceContext::RenderFrameHostChanged( | 121 void PermissionServiceContext::RenderFrameHostChanged( |
| 121 RenderFrameHost* old_host, | 122 RenderFrameHost* old_host, |
| 122 RenderFrameHost* new_host) { | 123 RenderFrameHost* new_host) { |
| 123 CancelPendingOperations(old_host); | 124 CancelPendingOperations(old_host); |
| 124 } | 125 } |
| 125 | 126 |
| 126 void PermissionServiceContext::FrameDeleted( | 127 void PermissionServiceContext::FrameDeleted( |
| 127 RenderFrameHost* render_frame_host) { | 128 RenderFrameHost* render_frame_host) { |
| 128 CancelPendingOperations(render_frame_host); | 129 CancelPendingOperations(render_frame_host); |
| 129 } | 130 } |
| 130 | 131 |
| 131 void PermissionServiceContext::DidNavigateAnyFrame( | 132 void PermissionServiceContext::DidNavigateAnyFrame( |
| 132 RenderFrameHost* render_frame_host, | 133 RenderFrameHost* render_frame_host, |
| 133 const LoadCommittedDetails& details, | 134 const LoadCommittedDetails& details, |
| 134 const FrameNavigateParams& params) { | 135 const FrameNavigateParams& params) { |
| 135 if (details.is_in_page) | 136 if (details.is_in_page) |
| 136 return; | 137 return; |
| 137 | 138 |
| 138 CancelPendingOperations(render_frame_host); | 139 CancelPendingOperations(render_frame_host); |
| 139 } | 140 } |
| 140 | 141 |
| 141 void PermissionServiceContext::CancelPendingOperations( | 142 void PermissionServiceContext::CancelPendingOperations( |
| 142 RenderFrameHost* render_frame_host) const { | 143 RenderFrameHost* render_frame_host) { |
| 143 DCHECK(render_frame_host_); | 144 DCHECK(render_frame_host_); |
| 144 if (render_frame_host != render_frame_host_) | 145 if (render_frame_host != render_frame_host_) |
| 145 return; | 146 return; |
| 146 | 147 |
| 147 for (const auto& service : services_) | 148 for (const auto& service : services_) |
| 148 service->CancelPendingOperations(); | 149 service->CancelPendingOperations(); |
| 150 |
| 151 subscriptions_.clear(); |
| 149 } | 152 } |
| 150 | 153 |
| 151 BrowserContext* PermissionServiceContext::GetBrowserContext() const { | 154 BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
| 152 if (!web_contents()) { | 155 if (!web_contents()) { |
| 153 DCHECK(render_process_host_); | 156 DCHECK(render_process_host_); |
| 154 return render_process_host_->GetBrowserContext(); | 157 return render_process_host_->GetBrowserContext(); |
| 155 } | 158 } |
| 156 return web_contents()->GetBrowserContext(); | 159 return web_contents()->GetBrowserContext(); |
| 157 } | 160 } |
| 158 | 161 |
| 159 GURL PermissionServiceContext::GetEmbeddingOrigin() const { | 162 GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
| 160 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() | 163 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() |
| 161 : GURL(); | 164 : GURL(); |
| 162 } | 165 } |
| 163 | 166 |
| 164 RenderFrameHost* PermissionServiceContext::render_frame_host() const { | 167 RenderFrameHost* PermissionServiceContext::render_frame_host() const { |
| 165 return render_frame_host_; | 168 return render_frame_host_; |
| 166 } | 169 } |
| 167 | 170 |
| 168 } // namespace content | 171 } // namespace content |
| OLD | NEW |