Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / permissions / permission_service_impl.h
bloba6d120cd8be515c6dad0afbab3c6510ee53d2a51
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)>;
44 struct PendingRequest {
45 PendingRequest(PermissionType permission, const GURL& origin,
46 const PermissionStatusCallback& callback);
47 ~PendingRequest();
49 PermissionType permission;
50 GURL origin;
51 PermissionStatusCallback callback;
53 using RequestsMap = IDMap<PendingRequest, IDMapOwnPointer>;
55 struct PendingSubscription {
56 PendingSubscription(PermissionType permission, const GURL& origin,
57 const PermissionStatusCallback& callback);
58 ~PendingSubscription();
60 // Subscription ID received from the PermissionManager.
61 int id;
62 PermissionType permission;
63 GURL origin;
64 PermissionStatusCallback callback;
66 using SubscriptionsMap = IDMap<PendingSubscription, IDMapOwnPointer>;
68 // PermissionService.
69 void HasPermission(PermissionName permission,
70 const mojo::String& origin,
71 const PermissionStatusCallback& callback) override;
72 void RequestPermission(PermissionName permission,
73 const mojo::String& origin,
74 bool user_gesture,
75 const PermissionStatusCallback& callback) override;
76 void RevokePermission(PermissionName permission,
77 const mojo::String& origin,
78 const PermissionStatusCallback& callback) override;
79 void GetNextPermissionChange(
80 PermissionName permission,
81 const mojo::String& origin,
82 PermissionStatus last_known_status,
83 const PermissionStatusCallback& callback) override;
85 void OnConnectionError();
87 void OnRequestPermissionResponse(int request_id, PermissionStatus status);
89 PermissionStatus GetPermissionStatusFromName(PermissionName permission,
90 const GURL& origin);
91 PermissionStatus GetPermissionStatusFromType(PermissionType type,
92 const GURL& origin);
93 void ResetPermissionStatus(PermissionType type, const GURL& origin);
95 void OnPermissionStatusChanged(int pending_subscription_id,
96 PermissionStatus status);
98 RequestsMap pending_requests_;
99 SubscriptionsMap pending_subscriptions_;
100 // context_ owns |this|.
101 PermissionServiceContext* context_;
102 mojo::Binding<PermissionService> binding_;
103 base::WeakPtrFactory<PermissionServiceImpl> weak_factory_;
105 DISALLOW_COPY_AND_ASSIGN(PermissionServiceImpl);
108 } // namespace content
110 #endif // CONTENT_BROWSER_PERMISSIONS_PERMISSION_SERVICE_IMPL_H_