| 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" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 render_frame_host_(nullptr), | 67 render_frame_host_(nullptr), |
| 68 render_process_host_(render_process_host) { | 68 render_process_host_(render_process_host) { |
| 69 } | 69 } |
| 70 | 70 |
| 71 PermissionServiceContext::~PermissionServiceContext() { | 71 PermissionServiceContext::~PermissionServiceContext() { |
| 72 } | 72 } |
| 73 | 73 |
| 74 void PermissionServiceContext::CreateService( | 74 void PermissionServiceContext::CreateService( |
| 75 const service_manager::BindSourceInfo& source_info, | 75 const service_manager::BindSourceInfo& source_info, |
| 76 blink::mojom::PermissionServiceRequest request) { | 76 blink::mojom::PermissionServiceRequest request) { |
| 77 services_.push_back( | 77 services_.AddBinding(base::MakeUnique<PermissionServiceImpl>(this), |
| 78 base::MakeUnique<PermissionServiceImpl>(this, std::move(request))); | 78 std::move(request)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void PermissionServiceContext::CreateSubscription( | 81 void PermissionServiceContext::CreateSubscription( |
| 82 PermissionType permission_type, | 82 PermissionType permission_type, |
| 83 const url::Origin& origin, | 83 const url::Origin& origin, |
| 84 PermissionObserverPtr observer) { | 84 PermissionObserverPtr observer) { |
| 85 BrowserContext* browser_context = GetBrowserContext(); | 85 BrowserContext* browser_context = GetBrowserContext(); |
| 86 DCHECK(browser_context); | 86 DCHECK(browser_context); |
| 87 if (!browser_context->GetPermissionManager()) | 87 if (!browser_context->GetPermissionManager()) |
| 88 return; | 88 return; |
| 89 | 89 |
| 90 auto subscription = | 90 auto subscription = |
| 91 base::MakeUnique<PermissionSubscription>(this, std::move(observer)); | 91 base::MakeUnique<PermissionSubscription>(this, std::move(observer)); |
| 92 GURL requesting_origin(origin.Serialize()); | 92 GURL requesting_origin(origin.Serialize()); |
| 93 GURL embedding_origin = GetEmbeddingOrigin(); | 93 GURL embedding_origin = GetEmbeddingOrigin(); |
| 94 int subscription_id = | 94 int subscription_id = |
| 95 browser_context->GetPermissionManager()->SubscribePermissionStatusChange( | 95 browser_context->GetPermissionManager()->SubscribePermissionStatusChange( |
| 96 permission_type, requesting_origin, | 96 permission_type, requesting_origin, |
| 97 // If the embedding_origin is empty, we'll use the |origin| instead. | 97 // If the embedding_origin is empty, we'll use the |origin| instead. |
| 98 embedding_origin.is_empty() ? requesting_origin : embedding_origin, | 98 embedding_origin.is_empty() ? requesting_origin : embedding_origin, |
| 99 base::Bind(&PermissionSubscription::OnPermissionStatusChanged, | 99 base::Bind(&PermissionSubscription::OnPermissionStatusChanged, |
| 100 base::Unretained(subscription.get()))); | 100 base::Unretained(subscription.get()))); |
| 101 subscription->set_id(subscription_id); | 101 subscription->set_id(subscription_id); |
| 102 subscriptions_[subscription_id] = std::move(subscription); | 102 subscriptions_[subscription_id] = std::move(subscription); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void PermissionServiceContext::ServiceHadConnectionError( | |
| 106 PermissionServiceImpl* service) { | |
| 107 auto it = std::find_if( | |
| 108 services_.begin(), services_.end(), | |
| 109 [service](const std::unique_ptr<PermissionServiceImpl>& this_service) { | |
| 110 return service == this_service.get(); | |
| 111 }); | |
| 112 DCHECK(it != services_.end()); | |
| 113 services_.erase(it); | |
| 114 } | |
| 115 | |
| 116 void PermissionServiceContext::ObserverHadConnectionError(int subscription_id) { | 105 void PermissionServiceContext::ObserverHadConnectionError(int subscription_id) { |
| 117 auto it = subscriptions_.find(subscription_id); | 106 auto it = subscriptions_.find(subscription_id); |
| 118 DCHECK(it != subscriptions_.end()); | 107 DCHECK(it != subscriptions_.end()); |
| 119 subscriptions_.erase(it); | 108 subscriptions_.erase(it); |
| 120 } | 109 } |
| 121 | 110 |
| 122 void PermissionServiceContext::RenderFrameHostChanged( | 111 void PermissionServiceContext::RenderFrameHostChanged( |
| 123 RenderFrameHost* old_host, | 112 RenderFrameHost* old_host, |
| 124 RenderFrameHost* new_host) { | 113 RenderFrameHost* new_host) { |
| 125 CancelPendingOperations(old_host); | 114 CloseBindings(old_host); |
| 126 } | 115 } |
| 127 | 116 |
| 128 void PermissionServiceContext::FrameDeleted( | 117 void PermissionServiceContext::FrameDeleted( |
| 129 RenderFrameHost* render_frame_host) { | 118 RenderFrameHost* render_frame_host) { |
| 130 CancelPendingOperations(render_frame_host); | 119 CloseBindings(render_frame_host); |
| 131 } | 120 } |
| 132 | 121 |
| 133 void PermissionServiceContext::DidFinishNavigation( | 122 void PermissionServiceContext::DidFinishNavigation( |
| 134 NavigationHandle* navigation_handle) { | 123 NavigationHandle* navigation_handle) { |
| 135 if (!navigation_handle->HasCommitted() || navigation_handle->IsSameDocument()) | 124 if (!navigation_handle->HasCommitted() || navigation_handle->IsSameDocument()) |
| 136 return; | 125 return; |
| 137 | 126 |
| 138 CancelPendingOperations(navigation_handle->GetRenderFrameHost()); | 127 CloseBindings(navigation_handle->GetRenderFrameHost()); |
| 139 } | 128 } |
| 140 | 129 |
| 141 void PermissionServiceContext::CancelPendingOperations( | 130 void PermissionServiceContext::CloseBindings( |
| 142 RenderFrameHost* render_frame_host) { | 131 RenderFrameHost* render_frame_host) { |
| 143 DCHECK(render_frame_host_); | 132 DCHECK(render_frame_host_); |
| 144 if (render_frame_host != render_frame_host_) | 133 if (render_frame_host != render_frame_host_) |
| 145 return; | 134 return; |
| 146 | 135 |
| 147 for (const auto& service : services_) | 136 services_.CloseAllBindings(); |
| 148 service->CancelPendingOperations(); | |
| 149 | |
| 150 subscriptions_.clear(); | 137 subscriptions_.clear(); |
| 151 } | 138 } |
| 152 | 139 |
| 153 BrowserContext* PermissionServiceContext::GetBrowserContext() const { | 140 BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
| 154 if (!web_contents()) { | 141 if (!web_contents()) { |
| 155 DCHECK(render_process_host_); | 142 DCHECK(render_process_host_); |
| 156 return render_process_host_->GetBrowserContext(); | 143 return render_process_host_->GetBrowserContext(); |
| 157 } | 144 } |
| 158 return web_contents()->GetBrowserContext(); | 145 return web_contents()->GetBrowserContext(); |
| 159 } | 146 } |
| 160 | 147 |
| 161 GURL PermissionServiceContext::GetEmbeddingOrigin() const { | 148 GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
| 162 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() | 149 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() |
| 163 : GURL(); | 150 : GURL(); |
| 164 } | 151 } |
| 165 | 152 |
| 166 RenderFrameHost* PermissionServiceContext::render_frame_host() const { | 153 RenderFrameHost* PermissionServiceContext::render_frame_host() const { |
| 167 return render_frame_host_; | 154 return render_frame_host_; |
| 168 } | 155 } |
| 169 | 156 |
| 170 } // namespace content | 157 } // namespace content |
| OLD | NEW |