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 | |
27 ~PermissionSubscription() { | |
28 BrowserContext* browser_context = context_->GetBrowserContext(); | |
29 DCHECK(browser_context); | |
30 if (browser_context->GetPermissionManager()) { | |
31 browser_context->GetPermissionManager() | |
32 ->UnsubscribePermissionStatusChange(id_); | |
33 } | |
34 } | |
35 | |
36 void OnConnectionError() { context_->ObserverHadConnectionError(id_); } | |
37 | |
38 void OnPermissionStatusChanged(blink::mojom::PermissionStatus status) { | |
39 observer_->OnPermissionStatusChange(status); | |
40 } | |
41 | |
42 void set_id(int id) { id_ = id; } | |
43 | |
44 private: | |
45 PermissionServiceContext* context_; | |
46 PermissionObserverPtr observer_; | |
47 int id_; | |
mlamouri (slow - plz ping)
2016/12/19 13:37:45
What's `id_` initial value? Please set a value her
Reilly Grant (use Gerrit)
2016/12/19 22:23:59
Done.
| |
48 }; | |
49 | |
17 PermissionServiceContext::PermissionServiceContext( | 50 PermissionServiceContext::PermissionServiceContext( |
18 RenderFrameHost* render_frame_host) | 51 RenderFrameHost* render_frame_host) |
19 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), | 52 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), |
20 render_frame_host_(render_frame_host), | 53 render_frame_host_(render_frame_host), |
21 render_process_host_(nullptr) { | 54 render_process_host_(nullptr) { |
22 } | 55 } |
23 | 56 |
24 PermissionServiceContext::PermissionServiceContext( | 57 PermissionServiceContext::PermissionServiceContext( |
25 RenderProcessHost* render_process_host) | 58 RenderProcessHost* render_process_host) |
26 : WebContentsObserver(nullptr), | 59 : WebContentsObserver(nullptr), |
27 render_frame_host_(nullptr), | 60 render_frame_host_(nullptr), |
28 render_process_host_(render_process_host) { | 61 render_process_host_(render_process_host) { |
29 } | 62 } |
30 | 63 |
31 PermissionServiceContext::~PermissionServiceContext() { | 64 PermissionServiceContext::~PermissionServiceContext() { |
32 } | 65 } |
33 | 66 |
34 void PermissionServiceContext::CreateService( | 67 void PermissionServiceContext::CreateService( |
35 mojo::InterfaceRequest<blink::mojom::PermissionService> request) { | 68 mojo::InterfaceRequest<blink::mojom::PermissionService> request) { |
36 services_.push_back(new PermissionServiceImpl(this, std::move(request))); | 69 services_.push_back( |
70 base::MakeUnique<PermissionServiceImpl>(this, std::move(request))); | |
71 } | |
72 | |
73 void PermissionServiceContext::CreateSubscription( | |
74 PermissionType permission_type, | |
75 const url::Origin& origin, | |
76 PermissionObserverPtr observer) { | |
77 BrowserContext* browser_context = GetBrowserContext(); | |
78 DCHECK(browser_context); | |
79 if (!browser_context->GetPermissionManager()) | |
80 return; | |
81 | |
82 auto subscription = | |
83 base::MakeUnique<PermissionSubscription>(this, std::move(observer)); | |
84 GURL requesting_origin(origin.Serialize()); | |
85 GURL embedding_origin = GetEmbeddingOrigin(); | |
86 int subscription_id = | |
87 browser_context->GetPermissionManager()->SubscribePermissionStatusChange( | |
88 permission_type, requesting_origin, | |
89 // If the embedding_origin is empty, we,ll use the |origin| instead. | |
90 embedding_origin.is_empty() ? requesting_origin : embedding_origin, | |
91 base::Bind(&PermissionSubscription::OnPermissionStatusChanged, | |
92 base::Unretained(subscription.get()))); | |
93 subscription->set_id(subscription_id); | |
94 subscriptions_[subscription_id] = std::move(subscription); | |
37 } | 95 } |
38 | 96 |
39 void PermissionServiceContext::ServiceHadConnectionError( | 97 void PermissionServiceContext::ServiceHadConnectionError( |
40 PermissionServiceImpl* service) { | 98 PermissionServiceImpl* service) { |
41 auto it = std::find(services_.begin(), services_.end(), service); | 99 auto it = std::find_if( |
100 services_.begin(), services_.end(), | |
101 [service](const std::unique_ptr<PermissionServiceImpl>& this_service) { | |
102 return service == this_service.get(); | |
103 }); | |
42 DCHECK(it != services_.end()); | 104 DCHECK(it != services_.end()); |
43 services_.erase(it); | 105 services_.erase(it); |
44 } | 106 } |
45 | 107 |
108 void PermissionServiceContext::ObserverHadConnectionError(int subscription_id) { | |
109 auto it = subscriptions_.find(subscription_id); | |
110 DCHECK(it != subscriptions_.end()); | |
111 subscriptions_.erase(it); | |
112 } | |
113 | |
46 void PermissionServiceContext::RenderFrameHostChanged( | 114 void PermissionServiceContext::RenderFrameHostChanged( |
47 RenderFrameHost* old_host, | 115 RenderFrameHost* old_host, |
48 RenderFrameHost* new_host) { | 116 RenderFrameHost* new_host) { |
49 CancelPendingOperations(old_host); | 117 CancelPendingOperations(old_host); |
50 } | 118 } |
51 | 119 |
52 void PermissionServiceContext::FrameDeleted( | 120 void PermissionServiceContext::FrameDeleted( |
53 RenderFrameHost* render_frame_host) { | 121 RenderFrameHost* render_frame_host) { |
54 CancelPendingOperations(render_frame_host); | 122 CancelPendingOperations(render_frame_host); |
55 } | 123 } |
56 | 124 |
57 void PermissionServiceContext::DidNavigateAnyFrame( | 125 void PermissionServiceContext::DidNavigateAnyFrame( |
58 RenderFrameHost* render_frame_host, | 126 RenderFrameHost* render_frame_host, |
59 const LoadCommittedDetails& details, | 127 const LoadCommittedDetails& details, |
60 const FrameNavigateParams& params) { | 128 const FrameNavigateParams& params) { |
61 if (details.is_in_page) | 129 if (details.is_in_page) |
62 return; | 130 return; |
63 | 131 |
64 CancelPendingOperations(render_frame_host); | 132 CancelPendingOperations(render_frame_host); |
65 } | 133 } |
66 | 134 |
67 void PermissionServiceContext::CancelPendingOperations( | 135 void PermissionServiceContext::CancelPendingOperations( |
68 RenderFrameHost* render_frame_host) const { | 136 RenderFrameHost* render_frame_host) const { |
69 DCHECK(render_frame_host_); | 137 DCHECK(render_frame_host_); |
70 if (render_frame_host != render_frame_host_) | 138 if (render_frame_host != render_frame_host_) |
71 return; | 139 return; |
72 | 140 |
73 for (auto* service : services_) | 141 for (const auto& service : services_) |
mlamouri (slow - plz ping)
2016/12/19 13:37:45
why?
Reilly Grant (use Gerrit)
2016/12/19 22:23:59
services_ changed from a ScopedVector to a std::ve
| |
74 service->CancelPendingOperations(); | 142 service->CancelPendingOperations(); |
75 } | 143 } |
76 | 144 |
77 BrowserContext* PermissionServiceContext::GetBrowserContext() const { | 145 BrowserContext* PermissionServiceContext::GetBrowserContext() const { |
78 if (!web_contents()) { | 146 if (!web_contents()) { |
79 DCHECK(render_process_host_); | 147 DCHECK(render_process_host_); |
80 return render_process_host_->GetBrowserContext(); | 148 return render_process_host_->GetBrowserContext(); |
81 } | 149 } |
82 return web_contents()->GetBrowserContext(); | 150 return web_contents()->GetBrowserContext(); |
83 } | 151 } |
84 | 152 |
85 GURL PermissionServiceContext::GetEmbeddingOrigin() const { | 153 GURL PermissionServiceContext::GetEmbeddingOrigin() const { |
86 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() | 154 return web_contents() ? web_contents()->GetLastCommittedURL().GetOrigin() |
87 : GURL(); | 155 : GURL(); |
88 } | 156 } |
89 | 157 |
90 RenderFrameHost* PermissionServiceContext::render_frame_host() const { | 158 RenderFrameHost* PermissionServiceContext::render_frame_host() const { |
91 return render_frame_host_; | 159 return render_frame_host_; |
92 } | 160 } |
93 | 161 |
94 } // namespace content | 162 } // namespace content |
OLD | NEW |