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 #ifndef CONTENT_BROWSER_PERMISSIONS_PERMISSION_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_PERMISSIONS_PERMISSION_SERVICE_IMPL_H_
8 #include "base/id_map.h"
9 #include "base/macros.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/permissions/permission_service_context.h"
12 #include "content/common/permission_service.mojom.h"
16 enum class PermissionType
;
18 // Implements the PermissionService Mojo interface.
19 // This service can be created from a RenderFrameHost or a RenderProcessHost.
20 // It is owned by a PermissionServiceContext.
21 // It receives at PermissionServiceContext instance when created which allows it
22 // to have some information about the current context. That enables the service
23 // to know whether it can show UI and have knowledge of the associated
24 // WebContents for example.
25 class PermissionServiceImpl
: public mojo::InterfaceImpl
<PermissionService
> {
27 ~PermissionServiceImpl() override
;
29 // Clear pending operations currently run by the service. This will be called
30 // by PermissionServiceContext when it will need the service to clear its
31 // state for example, if the frame changes.
32 void CancelPendingOperations();
35 friend PermissionServiceContext
;
37 PermissionServiceImpl(PermissionServiceContext
* context
);
40 using PermissionStatusCallback
= mojo::Callback
<void(PermissionStatus
)>;
42 struct PendingRequest
{
43 PendingRequest(PermissionType permission
, const GURL
& origin
,
44 const PermissionStatusCallback
& callback
);
47 PermissionType permission
;
49 PermissionStatusCallback callback
;
51 using RequestsMap
= IDMap
<PendingRequest
, IDMapOwnPointer
>;
53 struct PendingSubscription
{
54 PendingSubscription(PermissionType permission
, const GURL
& origin
,
55 const PermissionStatusCallback
& callback
);
56 ~PendingSubscription();
58 // Subscription ID received from the PermissionManager.
60 PermissionType permission
;
62 PermissionStatusCallback callback
;
64 using SubscriptionsMap
= IDMap
<PendingSubscription
, IDMapOwnPointer
>;
67 void HasPermission(PermissionName permission
,
68 const mojo::String
& origin
,
69 const PermissionStatusCallback
& callback
) override
;
70 void RequestPermission(PermissionName permission
,
71 const mojo::String
& origin
,
73 const PermissionStatusCallback
& callback
) override
;
74 void RevokePermission(PermissionName permission
,
75 const mojo::String
& origin
,
76 const PermissionStatusCallback
& callback
) override
;
77 void GetNextPermissionChange(
78 PermissionName permission
,
79 const mojo::String
& origin
,
80 PermissionStatus last_known_status
,
81 const PermissionStatusCallback
& callback
) override
;
83 // mojo::InterfaceImpl.
84 void OnConnectionError() override
;
86 void OnRequestPermissionResponse(int request_id
, PermissionStatus status
);
88 PermissionStatus
GetPermissionStatusFromName(PermissionName permission
,
90 PermissionStatus
GetPermissionStatusFromType(PermissionType type
,
92 void ResetPermissionStatus(PermissionType type
, const GURL
& origin
);
94 void OnPermissionStatusChanged(int pending_subscription_id
,
95 PermissionStatus status
);
97 RequestsMap pending_requests_
;
98 SubscriptionsMap pending_subscriptions_
;
99 // context_ owns |this|.
100 PermissionServiceContext
* context_
;
101 base::WeakPtrFactory
<PermissionServiceImpl
> weak_factory_
;
103 DISALLOW_COPY_AND_ASSIGN(PermissionServiceImpl
);
106 } // namespace content
108 #endif // CONTENT_BROWSER_PERMISSIONS_PERMISSION_SERVICE_IMPL_H_