OLD | NEW |
---|---|
(Empty) | |
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_impl.h" | |
6 | |
7 #include "content/browser/geolocation/geolocation_provider_impl.h" | |
8 #include "content/public/browser/content_browser_client.h" | |
9 #include "content/public/browser/render_process_host.h" | |
10 | |
11 namespace content { | |
12 | |
13 namespace { | |
14 | |
15 PermissionType PermissionNameToPermissionType(PermissionName name) { | |
16 switch(name) { | |
17 case PERMISSION_NAME_GEOLOCATION: | |
18 return PERMISSION_GEOLOCATION; | |
19 } | |
20 | |
21 NOTREACHED(); | |
22 return PERMISSION_NUM; | |
23 } | |
24 | |
25 } // anonymous namespace | |
26 | |
27 PermissionServiceImpl::PendingRequest::PendingRequest(PermissionType permission, | |
28 const GURL& origin) | |
29 : permission(permission), | |
30 origin(origin) { | |
31 } | |
32 | |
33 PermissionServiceImpl::PermissionServiceImpl(PermissionServiceContext* context) | |
34 : context_(context), | |
35 weak_factory_(this) { | |
36 } | |
37 | |
38 PermissionServiceImpl::~PermissionServiceImpl() { | |
39 } | |
40 | |
41 void PermissionServiceImpl::OnConnectionError() { | |
42 context_->ServiceHadConnectionError(this); | |
43 // After that call, |this| will be deleted. | |
44 } | |
45 | |
46 void PermissionServiceImpl::RequestPermission( | |
47 PermissionName permission, | |
48 const mojo::String& origin, | |
49 const mojo::Callback<void(PermissionStatus)>& callback) { | |
50 // This condition is valid if the call is coming from a ChildThread instead of | |
51 // a RenderFrame. Some consumers of the service run in Workers and some in | |
52 // Frames. In the context of a Worker, it is not possible to show a | |
53 // permission prompt because there is no tab. In the context of a Frame, we | |
54 // can. Even if the call comes from a context where it is not possible to show | |
55 // any UI, we want to still return something relevant so the current | |
56 // permission status is returned. | |
57 if (!context_->web_contents()) { | |
58 // There is no way to show a UI so the call will simply return the current | |
59 // permission. | |
60 QueryPermission(permission, origin, callback); | |
61 return; | |
62 } | |
63 | |
64 PermissionType permission_type = PermissionNameToPermissionType(permission); | |
65 int request_id = pending_requests_.Add( | |
blundell
2014/11/14 14:41:59
Not for this CL, but when would a PermissionServic
mlamouri (slow - plz ping)
2014/11/14 16:14:45
This is definitely possible. Have you ever seen th
blundell
2014/11/17 13:14:01
More than 1 incoming request per RenderFrame at a
| |
66 new PendingRequest(permission_type, GURL(origin))); | |
67 | |
68 GetContentClient()->browser()->RequestPermission( | |
69 permission_type, | |
70 context_->web_contents(), | |
71 request_id, | |
72 GURL(origin), | |
73 true, // TODO(mlamouri): should be removed, see http://crbug.com/423770 | |
74 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse, | |
75 weak_factory_.GetWeakPtr(), | |
76 callback, | |
77 request_id)); | |
78 } | |
79 | |
80 void PermissionServiceImpl::OnRequestPermissionResponse( | |
81 const mojo::Callback<void(PermissionStatus)>& callback, | |
82 int request_id, | |
83 bool allowed) { | |
84 // TODO(mlamouri): we might want a generic way to handle those things. | |
85 if (allowed && | |
86 pending_requests_.Lookup(request_id)->permission == | |
87 PERMISSION_GEOLOCATION) { | |
88 GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices(); | |
89 } | |
90 | |
91 pending_requests_.Remove(request_id); | |
92 | |
93 // TODO(mlamouri): for now, we only get a boolean back, but we would ideally | |
94 // need a ContentSetting, see http://crbug.com/432978 | |
95 callback.Run(allowed ? PERMISSION_STATUS_GRANTED : PERMISSION_STATUS_ASK); | |
96 } | |
97 | |
98 void PermissionServiceImpl::CancelPendingRequests() { | |
99 DCHECK(context_->web_contents()); | |
100 | |
101 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_); | |
102 !it.IsAtEnd(); it.Advance()) { | |
103 GetContentClient()->browser()->CancelPermissionRequest( | |
104 it.GetCurrentValue()->permission, | |
105 context_->web_contents(), | |
106 it.GetCurrentKey(), | |
107 it.GetCurrentValue()->origin); | |
108 } | |
109 | |
110 pending_requests_.Clear(); | |
111 } | |
112 | |
113 void PermissionServiceImpl::QueryPermission( | |
114 PermissionName permission, | |
115 const mojo::String& origin, | |
116 const mojo::Callback<void(PermissionStatus)>& callback) { | |
117 NOTIMPLEMENTED(); | |
qsr
2014/11/14 14:28:18
I still do not understand that. If you reach that
mlamouri (slow - plz ping)
2014/11/14 16:14:45
The difference between having NOTIMPLEMENTED() in
| |
118 } | |
119 | |
120 } // namespace content | |
OLD | NEW |