Implement basic mojo Permission service and use it for Geolocation.
[chromium-blink-merge.git] / content / browser / permissions / permission_service_impl.cc
bloba5f66bf2f54790a6b89d835636d2cffe7a5eb759
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.
5 #include "content/browser/permissions/permission_service_impl.h"
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"
11 namespace content {
13 namespace {
15 PermissionType PermissionNameToPermissionType(PermissionName name) {
16 switch(name) {
17 case PERMISSION_NAME_GEOLOCATION:
18 return PERMISSION_GEOLOCATION;
21 NOTREACHED();
22 return PERMISSION_NUM;
25 } // anonymous namespace
27 PermissionServiceImpl::PendingRequest::PendingRequest(PermissionType permission,
28 const GURL& origin)
29 : permission(permission),
30 origin(origin) {
33 PermissionServiceImpl::PermissionServiceImpl(PermissionServiceContext* context)
34 : context_(context),
35 weak_factory_(this) {
38 PermissionServiceImpl::~PermissionServiceImpl() {
41 void PermissionServiceImpl::OnConnectionError() {
42 context_->ServiceHadConnectionError(this);
43 // After that call, |this| will be deleted.
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 HasPermission(permission, origin, callback);
61 return;
64 PermissionType permission_type = PermissionNameToPermissionType(permission);
65 int request_id = pending_requests_.Add(
66 new PendingRequest(permission_type, GURL(origin)));
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));
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();
91 pending_requests_.Remove(request_id);
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);
98 void PermissionServiceImpl::CancelPendingRequests() {
99 DCHECK(context_->web_contents());
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);
110 pending_requests_.Clear();
113 void PermissionServiceImpl::HasPermission(
114 PermissionName permission,
115 const mojo::String& origin,
116 const mojo::Callback<void(PermissionStatus)>& callback) {
117 NOTIMPLEMENTED();
120 } // namespace content