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/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/permission_manager.h" |
11 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
12 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
14 | 16 |
| 17 using blink::mojom::PermissionObserverPtr; |
| 18 |
15 namespace content { | 19 namespace content { |
16 | 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 }; |
| 48 |
17 PermissionServiceContext::PermissionServiceContext( | 49 PermissionServiceContext::PermissionServiceContext( |
18 RenderFrameHost* render_frame_host) | 50 RenderFrameHost* render_frame_host) |
19 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), | 51 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), |
20 render_frame_host_(render_frame_host), | 52 render_frame_host_(render_frame_host), |
21 render_process_host_(nullptr) { | 53 render_process_host_(nullptr) { |
22 } | 54 } |
23 | 55 |
24 PermissionServiceContext::PermissionServiceContext( | 56 PermissionServiceContext::PermissionServiceContext( |
25 RenderProcessHost* render_process_host) | 57 RenderProcessHost* render_process_host) |
26 : WebContentsObserver(nullptr), | 58 : WebContentsObserver(nullptr), |
27 render_frame_host_(nullptr), | 59 render_frame_host_(nullptr), |
28 render_process_host_(render_process_host) { | 60 render_process_host_(render_process_host) { |
29 } | 61 } |
30 | 62 |
31 PermissionServiceContext::~PermissionServiceContext() { | 63 PermissionServiceContext::~PermissionServiceContext() { |
32 } | 64 } |
33 | 65 |
34 void PermissionServiceContext::CreateService( | 66 void PermissionServiceContext::CreateService( |
35 mojo::InterfaceRequest<blink::mojom::PermissionService> request) { | 67 mojo::InterfaceRequest<blink::mojom::PermissionService> request) { |
36 services_.push_back(new PermissionServiceImpl(this, std::move(request))); | 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); |
37 } | 94 } |
38 | 95 |
39 void PermissionServiceContext::ServiceHadConnectionError( | 96 void PermissionServiceContext::ServiceHadConnectionError( |
40 PermissionServiceImpl* service) { | 97 PermissionServiceImpl* service) { |
41 auto it = std::find(services_.begin(), services_.end(), service); | 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 }); |
42 DCHECK(it != services_.end()); | 103 DCHECK(it != services_.end()); |
43 services_.erase(it); | 104 services_.erase(it); |
44 } | 105 } |
45 | 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); |
| 118 } |
| 119 |
46 void PermissionServiceContext::RenderFrameHostChanged( | 120 void PermissionServiceContext::RenderFrameHostChanged( |
47 RenderFrameHost* old_host, | 121 RenderFrameHost* old_host, |
48 RenderFrameHost* new_host) { | 122 RenderFrameHost* new_host) { |
49 CancelPendingOperations(old_host); | 123 CancelPendingOperations(old_host); |
50 } | 124 } |
51 | 125 |
52 void PermissionServiceContext::FrameDeleted( | 126 void PermissionServiceContext::FrameDeleted( |
53 RenderFrameHost* render_frame_host) { | 127 RenderFrameHost* render_frame_host) { |
54 CancelPendingOperations(render_frame_host); | 128 CancelPendingOperations(render_frame_host); |
55 } | 129 } |
56 | 130 |
57 void PermissionServiceContext::DidNavigateAnyFrame( | 131 void PermissionServiceContext::DidNavigateAnyFrame( |
58 RenderFrameHost* render_frame_host, | 132 RenderFrameHost* render_frame_host, |
59 const LoadCommittedDetails& details, | 133 const LoadCommittedDetails& details, |
60 const FrameNavigateParams& params) { | 134 const FrameNavigateParams& params) { |
61 if (details.is_in_page) | 135 if (details.is_in_page) |
62 return; | 136 return; |
63 | 137 |
64 CancelPendingOperations(render_frame_host); | 138 CancelPendingOperations(render_frame_host); |
65 } | 139 } |
66 | 140 |
67 void PermissionServiceContext::CancelPendingOperations( | 141 void PermissionServiceContext::CancelPendingOperations( |
68 RenderFrameHost* render_frame_host) const { | 142 RenderFrameHost* render_frame_host) const { |
69 DCHECK(render_frame_host_); | 143 DCHECK(render_frame_host_); |
70 if (render_frame_host != render_frame_host_) | 144 if (render_frame_host != render_frame_host_) |
71 return; | 145 return; |
72 | 146 |
73 for (auto* service : services_) | 147 for (const auto& service : services_) |
74 service->CancelPendingOperations(); | 148 service->CancelPendingOperations(); |
75 } | 149 } |
76 | 150 |
77 BrowserContext* PermissionServiceContext::GetBrowserContext() const { | 151 BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
78 if (!web_contents()) { | 152 if (!web_contents()) { |
79 DCHECK(render_process_host_); | 153 DCHECK(render_process_host_); |
80 return render_process_host_->GetBrowserContext(); | 154 return render_process_host_->GetBrowserContext(); |
81 } | 155 } |
82 return web_contents()->GetBrowserContext(); | 156 return web_contents()->GetBrowserContext(); |
83 } | 157 } |
84 | 158 |
85 GURL PermissionServiceContext::GetEmbeddingOrigin() const { | 159 GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
86 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() | 160 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() |
87 : GURL(); | 161 : GURL(); |
88 } | 162 } |
89 | 163 |
90 RenderFrameHost* PermissionServiceContext::render_frame_host() const { | 164 RenderFrameHost* PermissionServiceContext::render_frame_host() const { |
91 return render_frame_host_; | 165 return render_frame_host_; |
92 } | 166 } |
93 | 167 |
94 } // namespace content | 168 } // namespace content |
OLD | NEW |