Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: content/browser/permissions/permission_service_impl.cc

Issue 722153003: Implement basic mojo Permission service and use it for Geolocation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rename HasPermission() to QueryPermission() Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 void PermissionServiceImpl::Create(
34 scoped_ptr<PermissionServiceContext> context,
35 mojo::InterfaceRequest<PermissionService> request) {
36 mojo::BindToRequest(new PermissionServiceImpl(context.Pass()), &request);
37 }
38
39 PermissionServiceImpl::PermissionServiceImpl(
40 scoped_ptr<PermissionServiceContext> context)
41 : context_(context.Pass()),
42 weak_factory_(this) {
43 context_->set_service(this);
44 }
45
46 PermissionServiceImpl::~PermissionServiceImpl() {
47 }
48
49 void PermissionServiceImpl::RequestPermission(
50 PermissionName permission,
51 const mojo::String& origin,
52 const mojo::Callback<void(PermissionStatus)>& callback) {
53 // This condition is valid if the call is coming from a ChildThread instead of
54 // a RenderFrame. Some consumers of the service run in Workers and some in
55 // Frames. In the context of a Worker, it is not possible to show a
56 // permission prompt because there is no tab. In the context of a Frame, we
57 // can. Even if the call comes from a context where it is not possible to show
58 // any UI, we want to still return something relevant so the current
59 // permission status is returned.
60 if (!context_->web_contents()) {
61 // There is no way to show a UI so the call will simply return the current
62 // permission.
63 QueryPermission(permission, origin, callback);
64 return;
65 }
66
67 PermissionType permission_type = PermissionNameToPermissionType(permission);
68 int request_id = pending_requests_.Add(
69 new PendingRequest(permission_type, GURL(origin)));
70
71 GetContentClient()->browser()->RequestPermission(
72 permission_type,
73 context_->web_contents(),
74 request_id,
75 GURL(origin),
76 true, // TODO(mlamouri): should be removed, see http://crbug.com/423770
77 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse,
78 weak_factory_.GetWeakPtr(),
79 callback,
80 request_id));
81 }
82
83 void PermissionServiceImpl::OnRequestPermissionResponse(
84 const mojo::Callback<void(PermissionStatus)>& callback,
85 int request_id,
86 bool allowed) {
87 // TODO(mlamouri): we might want a generic way to handle those things.
88 if (allowed &&
89 pending_requests_.Lookup(request_id)->permission ==
90 PERMISSION_GEOLOCATION) {
91 GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices();
92 }
93
94 pending_requests_.Remove(request_id);
95
96 // TODO(mlamouri): for now, we only get a boolean back, but we would ideally
97 // need a ContentSetting, see http://crbug.com/432978
98 callback.Run(allowed ? PERMISSION_STATUS_GRANTED : PERMISSION_STATUS_ASK);
99 }
100
101 void PermissionServiceImpl::CancelPendingRequests() {
102 DCHECK(context_->web_contents());
103
104 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_);
105 !it.IsAtEnd(); it.Advance()) {
106 GetContentClient()->browser()->CancelPermissionRequest(
107 it.GetCurrentValue()->permission,
108 context_->web_contents(),
109 it.GetCurrentKey(),
110 it.GetCurrentValue()->origin);
111 }
112
113 pending_requests_.Clear();
114 }
115
116 void PermissionServiceImpl::QueryPermission(
117 PermissionName permission,
118 const mojo::String& origin,
119 const mojo::Callback<void(PermissionStatus)>& callback) {
120 NOTIMPLEMENTED();
121 }
122
123 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698