Implement HasPermission() method in PermissionService.
[chromium-blink-merge.git] / content / browser / permissions / permission_service_impl.cc
blobbb5b027742907b8ebf25ae8d1feff364460c35ea
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/public/browser/content_browser_client.h"
9 namespace content {
11 namespace {
13 PermissionType PermissionNameToPermissionType(PermissionName name) {
14 switch(name) {
15 case PERMISSION_NAME_GEOLOCATION:
16 return PERMISSION_GEOLOCATION;
17 case PERMISSION_NAME_MIDI_SYSEX:
18 return PERMISSION_MIDI_SYSEX;
19 case PERMISSION_NAME_NOTIFICATIONS:
20 return PERMISSION_NOTIFICATIONS;
23 NOTREACHED();
24 return PERMISSION_NUM;
27 } // anonymous namespace
29 PermissionServiceImpl::PendingRequest::PendingRequest(PermissionType permission,
30 const GURL& origin)
31 : permission(permission),
32 origin(origin) {
35 PermissionServiceImpl::PermissionServiceImpl(PermissionServiceContext* context)
36 : context_(context),
37 weak_factory_(this) {
40 PermissionServiceImpl::~PermissionServiceImpl() {
43 void PermissionServiceImpl::OnConnectionError() {
44 context_->ServiceHadConnectionError(this);
45 // After that call, |this| will be deleted.
48 void PermissionServiceImpl::RequestPermission(
49 PermissionName permission,
50 const mojo::String& origin,
51 const mojo::Callback<void(PermissionStatus)>& callback) {
52 // This condition is valid if the call is coming from a ChildThread instead of
53 // a RenderFrame. Some consumers of the service run in Workers and some in
54 // Frames. In the context of a Worker, it is not possible to show a
55 // permission prompt because there is no tab. In the context of a Frame, we
56 // can. Even if the call comes from a context where it is not possible to show
57 // any UI, we want to still return something relevant so the current
58 // permission status is returned.
59 if (!context_->web_contents()) {
60 // There is no way to show a UI so the call will simply return the current
61 // permission.
62 HasPermission(permission, origin, callback);
63 return;
66 PermissionType permission_type = PermissionNameToPermissionType(permission);
67 int request_id = pending_requests_.Add(
68 new PendingRequest(permission_type, GURL(origin)));
70 GetContentClient()->browser()->RequestPermission(
71 permission_type,
72 context_->web_contents(),
73 request_id,
74 GURL(origin),
75 true, // TODO(mlamouri): should be removed, see http://crbug.com/423770
76 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse,
77 weak_factory_.GetWeakPtr(),
78 callback,
79 request_id));
82 void PermissionServiceImpl::OnRequestPermissionResponse(
83 const mojo::Callback<void(PermissionStatus)>& callback,
84 int request_id,
85 bool allowed) {
86 pending_requests_.Remove(request_id);
88 // TODO(mlamouri): for now, we only get a boolean back, but we would ideally
89 // need a ContentSetting, see http://crbug.com/432978
90 callback.Run(allowed ? PERMISSION_STATUS_GRANTED : PERMISSION_STATUS_ASK);
93 void PermissionServiceImpl::CancelPendingRequests() {
94 DCHECK(context_->web_contents());
96 for (RequestsMap::Iterator<PendingRequest> it(&pending_requests_);
97 !it.IsAtEnd(); it.Advance()) {
98 GetContentClient()->browser()->CancelPermissionRequest(
99 it.GetCurrentValue()->permission,
100 context_->web_contents(),
101 it.GetCurrentKey(),
102 it.GetCurrentValue()->origin);
105 pending_requests_.Clear();
108 void PermissionServiceImpl::HasPermission(
109 PermissionName permission,
110 const mojo::String& origin,
111 const mojo::Callback<void(PermissionStatus)>& callback) {
112 DCHECK(context_->GetBrowserContext());
114 // If the embedding_origin is empty we'll use |origin| instead.
115 GURL embedding_origin = context_->GetEmbeddingOrigin();
117 callback.Run(GetContentClient()->browser()->GetPermissionStatus(
118 PermissionNameToPermissionType(permission),
119 context_->GetBrowserContext(),
120 GURL(origin),
121 embedding_origin.is_empty() ? GURL(origin) : embedding_origin));
124 } // namespace content