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/id_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::WebPermissionQueryCallback
* callback
);
43 virtual void startListening(blink::WebPermissionType type
,
44 const blink::WebURL
& origin
,
45 blink::WebPermissionObserver
* observer
);
46 virtual void stopListening(blink::WebPermissionObserver
* observer
);
48 // The following methods must be called by workers on the main thread.
49 void QueryPermissionForWorker(blink::WebPermissionType type
,
50 const std::string
& origin
,
51 blink::WebPermissionQueryCallback
* callback
,
52 int worker_thread_id
);
53 void StartListeningForWorker(
54 blink::WebPermissionType type
,
55 const std::string
& origin
,
57 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
);
58 void GetNextPermissionChangeForWorker(
59 blink::WebPermissionType type
,
60 const std::string
& origin
,
61 blink::WebPermissionStatus status
,
63 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
);
66 // Runs the given |callback| with |status| as a parameter. It has to be run
67 // on a worker thread.
68 static void RunCallbackOnWorkerThread(
69 blink::WebPermissionQueryCallback
* callback
,
70 scoped_ptr
<blink::WebPermissionStatus
> status
);
72 // Helper method that returns an initialized PermissionServicePtr.
73 PermissionServicePtr
& GetPermissionServicePtr();
75 void QueryPermissionInternal(blink::WebPermissionType type
,
76 const std::string
& origin
,
77 blink::WebPermissionQueryCallback
* callback
,
78 int worker_thread_id
);
80 void OnQueryPermission(int request_id
, PermissionStatus status
);
81 void OnPermissionChanged(blink::WebPermissionType type
,
82 const std::string
& origin
,
83 blink::WebPermissionObserver
* observer
,
84 PermissionStatus status
);
85 void OnPermissionChangedForWorker(
87 const base::Callback
<void(blink::WebPermissionStatus
)>& callback
,
88 PermissionStatus status
);
90 void GetNextPermissionChange(blink::WebPermissionType type
,
91 const std::string
& origin
,
92 blink::WebPermissionObserver
* observer
,
93 PermissionStatus current_status
);
95 // Saves some basic information about the callback in order to be able to run
96 // it in the right thread.
97 class CallbackInformation
{
99 CallbackInformation(blink::WebPermissionQueryCallback
* callback
,
100 int worker_thread_id
);
101 ~CallbackInformation();
103 blink::WebPermissionQueryCallback
* callback() const;
104 int worker_thread_id() const;
106 blink::WebPermissionQueryCallback
* ReleaseCallback();
109 scoped_ptr
<blink::WebPermissionQueryCallback
> callback_
;
110 int worker_thread_id_
;
112 DISALLOW_COPY_AND_ASSIGN(CallbackInformation
);
114 using CallbackMap
= IDMap
<CallbackInformation
, IDMapOwnPointer
>;
115 CallbackMap pending_callbacks_
;
117 ServiceRegistry
* service_registry_
;
118 PermissionServicePtr permission_service_
;
120 DISALLOW_COPY_AND_ASSIGN(PermissionDispatcher
);
123 } // namespace content
125 #endif // CONTENT_CHILD_PERMISSIONS_PERMISSION_DISPATCHER_H_