[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / permissions / permission_service_impl.h
blobb83a6973f06692f8f8df961a9041465194f47abf
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"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
15 namespace content {
17 enum class PermissionType;
19 // Implements the PermissionService Mojo interface.
20 // This service can be created from a RenderFrameHost or a RenderProcessHost.
21 // It is owned by a PermissionServiceContext.
22 // It receives at PermissionServiceContext instance when created which allows it
23 // to have some information about the current context. That enables the service
24 // to know whether it can show UI and have knowledge of the associated
25 // WebContents for example.
26 class PermissionServiceImpl : public PermissionService {
27 public:
28 ~PermissionServiceImpl() override;
30 // Clear pending operations currently run by the service. This will be called
31 // by PermissionServiceContext when it will need the service to clear its
32 // state for example, if the frame changes.
33 void CancelPendingOperations();
35 protected:
36 friend PermissionServiceContext;
38 PermissionServiceImpl(PermissionServiceContext* context,
39 mojo::InterfaceRequest<PermissionService> request);
41 private:
42 using PermissionStatusCallback = mojo::Callback<void(PermissionStatus)>;
43 using PermissionsStatusCallback =
44 mojo::Callback<void(mojo::Array<PermissionStatus>)>;
46 struct PendingRequest {
47 PendingRequest(PermissionType permission, const GURL& origin,
48 const PermissionStatusCallback& callback);
49 ~PendingRequest();
51 PermissionType permission;
52 GURL origin;
53 PermissionStatusCallback callback;
55 using RequestsMap = IDMap<PendingRequest, IDMapOwnPointer>;
57 struct PendingSubscription {
58 PendingSubscription(PermissionType permission, const GURL& origin,
59 const PermissionStatusCallback& callback);
60 ~PendingSubscription();
62 // Subscription ID received from the PermissionManager.
63 int id;
64 PermissionType permission;
65 GURL origin;
66 PermissionStatusCallback callback;
68 using SubscriptionsMap = IDMap<PendingSubscription, IDMapOwnPointer>;
70 // PermissionService.
71 void HasPermission(PermissionName permission,
72 const mojo::String& origin,
73 const PermissionStatusCallback& callback) override;
74 void RequestPermission(PermissionName permission,
75 const mojo::String& origin,
76 bool user_gesture,
77 const PermissionStatusCallback& callback) override;
78 void RequestPermissions(mojo::Array<PermissionName> permissions,
79 const mojo::String& origin,
80 bool user_gesture,
81 const PermissionsStatusCallback& callback) override;
82 void RevokePermission(PermissionName permission,
83 const mojo::String& origin,
84 const PermissionStatusCallback& callback) override;
85 void GetNextPermissionChange(
86 PermissionName permission,
87 const mojo::String& origin,
88 PermissionStatus last_known_status,
89 const PermissionStatusCallback& callback) override;
91 void OnConnectionError();
93 void OnRequestPermissionResponse(int request_id, PermissionStatus status);
95 PermissionStatus GetPermissionStatusFromName(PermissionName permission,
96 const GURL& origin);
97 PermissionStatus GetPermissionStatusFromType(PermissionType type,
98 const GURL& origin);
99 void ResetPermissionStatus(PermissionType type, const GURL& origin);
101 void OnPermissionStatusChanged(int pending_subscription_id,
102 PermissionStatus status);
104 RequestsMap pending_requests_;
105 SubscriptionsMap pending_subscriptions_;
106 // context_ owns |this|.
107 PermissionServiceContext* context_;
108 mojo::Binding<PermissionService> binding_;
109 base::WeakPtrFactory<PermissionServiceImpl> weak_factory_;
111 DISALLOW_COPY_AND_ASSIGN(PermissionServiceImpl);
114 } // namespace content
116 #endif // CONTENT_BROWSER_PERMISSIONS_PERMISSION_SERVICE_IMPL_H_