1 // Copyright 2015 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_CHILD_PERMISSIONS_PERMISSION_DISPATCHER_H_
6 #define CONTENT_CHILD_PERMISSIONS_PERMISSION_DISPATCHER_H_
10 #include "base/callback_forward.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "content/child/permissions/permission_observers_registry.h"
15 #include "content/common/permission_service.mojom.h"
16 #include "third_party/WebKit/public/platform/modules/permissions/WebPermissionClient.h"
20 class ServiceRegistry
;
22 // The PermissionDispatcher is a layer between Blink and the Mojo
23 // PermissionService. It implements blink::WebPermissionClient. It is being used
24 // from workers and frames independently. When called outside of the main
25 // thread, QueryPermissionForWorker is meant to be called. It will handle the
27 class PermissionDispatcher
: public blink::WebPermissionClient
,
28 public PermissionObserversRegistry
{
30 // Returns whether the given WebPermissionType is observable. Some types have
31 // static values that never changes.
32 static bool IsObservable(blink::WebPermissionType type
);
34 // The caller must guarantee that |service_registry| will have a lifetime
35 // larger than this instance of PermissionDispatcher.
36 explicit PermissionDispatcher(ServiceRegistry
* service_registry
);
37 virtual ~PermissionDispatcher();
39 // blink::WebPermissionClient implementation.
40 virtual void queryPermission(blink::WebPermissionType type
,
41 const blink::WebURL
& origin
,
42 blink::WebPermissionCallback
* callback
);
43 virtual void requestPermission(blink::WebPermissionType
,
44 const blink::WebURL
& origin
,
45 blink::WebPermissionCallback
* callback
);
46 virtual void requestPermissions(
47 const blink::WebVector
<blink::WebPermissionType
>& types
,
48 const blink::WebURL
& origin
,
49 blink::WebPermissionsCallback
* callback
);
50 virtual void revokePermission(blink::WebPermissionType
,
51 const blink::WebURL
& origin
,
52 blink::WebPermissionCallback
* callback
);
53 virtual void startListening(blink::WebPermissionType type
,
54 const blink::WebURL
& origin
,
55 blink::WebPermissionObserver
* observer
);
56 virtual void stopListening(blink::WebPermissionObserver
* observer
);
58 // The following methods must be called by workers on the main thread.
59 void QueryPermissionForWorker(blink::WebPermissionType type
,
60 const std::string
& origin
,
61 blink::WebPermissionCallback
* callback
,
62 int worker_thread_id
);
63 void RequestPermissionForWorker(blink::WebPermissionType type
,
64 const std::string
& origin
,
65 blink::WebPermissionCallback
* callback
,
66 int worker_thread_id
);
67 void RequestPermissionsForWorker(
68 const blink::WebVector
<blink::WebPermissionType
>& types
,
69 const std::string
& origin
,
70 blink::WebPermissionsCallback
* callback
,
71 int worker_thread_id
);
72 void RevokePermissionForWorker(blink::WebPermissionType type
,
73 const std::string
& origin
,
74 blink::WebPermissionCallback
* callback
,
75 int worker_thread_id
);
76 void StartListeningForWorker(
77 blink::WebPermissionType type
,
78 const std::string
& origin
,
80 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
);
81 void GetNextPermissionChangeForWorker(
82 blink::WebPermissionType type
,
83 const std::string
& origin
,
84 blink::WebPermissionStatus status
,
86 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
);
89 using PermissionCallbackMap
= base::ScopedPtrHashMap
<uintptr_t,
90 scoped_ptr
<blink::WebPermissionCallback
>>;
91 using PermissionsCallbackMap
= base::ScopedPtrHashMap
<uintptr_t,
92 scoped_ptr
<blink::WebPermissionsCallback
>>;
94 // Runs the given |callback| with |status| as a parameter. It has to be run
95 // on a worker thread.
96 static void RunPermissionCallbackOnWorkerThread(
97 scoped_ptr
<blink::WebPermissionCallback
> callback
,
98 blink::WebPermissionStatus status
);
99 static void RunPermissionsCallbackOnWorkerThread(
100 scoped_ptr
<blink::WebPermissionsCallback
> callback
,
101 scoped_ptr
<blink::WebVector
<blink::WebPermissionStatus
>> statuses
);
103 // Helper method that returns an initialized PermissionServicePtr.
104 PermissionServicePtr
& GetPermissionServicePtr();
106 void QueryPermissionInternal(blink::WebPermissionType type
,
107 const std::string
& origin
,
108 blink::WebPermissionCallback
* callback
,
109 int worker_thread_id
);
110 void RequestPermissionInternal(blink::WebPermissionType type
,
111 const std::string
& origin
,
112 blink::WebPermissionCallback
* callback
,
113 int worker_thread_id
);
114 void RequestPermissionsInternal(
115 const blink::WebVector
<blink::WebPermissionType
>& types
,
116 const std::string
& origin
,
117 blink::WebPermissionsCallback
* callback
,
118 int worker_thread_id
);
119 void RevokePermissionInternal(blink::WebPermissionType type
,
120 const std::string
& origin
,
121 blink::WebPermissionCallback
* callback
,
122 int worker_thread_id
);
124 // This is the callback function used for query, request and revoke.
125 void OnPermissionResponse(int worker_thread_id
,
126 uintptr_t callback_key
,
127 PermissionStatus status
);
128 void OnRequestPermissionsResponse(
129 int worker_thread_id
,
130 uintptr_t callback_key
,
131 const mojo::Array
<PermissionStatus
>& status
);
132 void OnPermissionChanged(blink::WebPermissionType type
,
133 const std::string
& origin
,
134 blink::WebPermissionObserver
* observer
,
135 PermissionStatus status
);
136 void OnPermissionChangedForWorker(
137 int worker_thread_id
,
138 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
,
139 PermissionStatus status
);
141 void GetNextPermissionChange(blink::WebPermissionType type
,
142 const std::string
& origin
,
143 blink::WebPermissionObserver
* observer
,
144 PermissionStatus current_status
);
146 // Pending callbacks for query(), revoke() and request() single permission.
147 PermissionCallbackMap permission_callbacks_
;
149 // Pending callbacks for request() multiple permissions.
150 PermissionsCallbackMap permissions_callbacks_
;
152 ServiceRegistry
* service_registry_
;
153 PermissionServicePtr permission_service_
;
155 DISALLOW_COPY_AND_ASSIGN(PermissionDispatcher
);
158 } // namespace content
160 #endif // CONTENT_CHILD_PERMISSIONS_PERMISSION_DISPATCHER_H_