Vibration API: convert implementation to java mojo-service.
[chromium-blink-merge.git] / content / child / permissions / permission_dispatcher.h
blob87e464763d4aaeb9d1ee1a1db2205a4a3b92f6d9
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_
8 #include <string>
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"
18 namespace content {
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
26 // thread jumping.
27 class PermissionDispatcher : public blink::WebPermissionClient,
28 public PermissionObserversRegistry {
29 public:
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 revokePermission(blink::WebPermissionType,
47 const blink::WebURL& origin,
48 blink::WebPermissionCallback* callback);
49 virtual void startListening(blink::WebPermissionType type,
50 const blink::WebURL& origin,
51 blink::WebPermissionObserver* observer);
52 virtual void stopListening(blink::WebPermissionObserver* observer);
54 // The following methods must be called by workers on the main thread.
55 void QueryPermissionForWorker(blink::WebPermissionType type,
56 const std::string& origin,
57 blink::WebPermissionCallback* callback,
58 int worker_thread_id);
59 void RequestPermissionForWorker(blink::WebPermissionType type,
60 const std::string& origin,
61 blink::WebPermissionCallback* callback,
62 int worker_thread_id);
63 void RevokePermissionForWorker(blink::WebPermissionType type,
64 const std::string& origin,
65 blink::WebPermissionCallback* callback,
66 int worker_thread_id);
67 void StartListeningForWorker(
68 blink::WebPermissionType type,
69 const std::string& origin,
70 int worker_thread_id,
71 const base::Callback<void(blink::WebPermissionStatus)>& callback);
72 void GetNextPermissionChangeForWorker(
73 blink::WebPermissionType type,
74 const std::string& origin,
75 blink::WebPermissionStatus status,
76 int worker_thread_id,
77 const base::Callback<void(blink::WebPermissionStatus)>& callback);
79 private:
80 // Runs the given |callback| with |status| as a parameter. It has to be run
81 // on a worker thread.
82 static void RunCallbackOnWorkerThread(
83 blink::WebPermissionCallback* callback,
84 scoped_ptr<blink::WebPermissionStatus> status);
86 // Helper method that returns an initialized PermissionServicePtr.
87 PermissionServicePtr& GetPermissionServicePtr();
89 void QueryPermissionInternal(blink::WebPermissionType type,
90 const std::string& origin,
91 blink::WebPermissionCallback* callback,
92 int worker_thread_id);
93 void RequestPermissionInternal(blink::WebPermissionType type,
94 const std::string& origin,
95 blink::WebPermissionCallback* callback,
96 int worker_thread_id);
97 void RevokePermissionInternal(blink::WebPermissionType type,
98 const std::string& origin,
99 blink::WebPermissionCallback* callback,
100 int worker_thread_id);
102 // This is the callback function used for query and revoke
103 void OnPermissionResponse(int request_id,
104 PermissionStatus status);
105 void OnPermissionChanged(blink::WebPermissionType type,
106 const std::string& origin,
107 blink::WebPermissionObserver* observer,
108 PermissionStatus status);
109 void OnPermissionChangedForWorker(
110 int worker_thread_id,
111 const base::Callback<void(blink::WebPermissionStatus)>& callback,
112 PermissionStatus status);
114 void GetNextPermissionChange(blink::WebPermissionType type,
115 const std::string& origin,
116 blink::WebPermissionObserver* observer,
117 PermissionStatus current_status);
119 // Saves some basic information about the callback in order to be able to run
120 // it in the right thread.
121 class CallbackInformation {
122 public:
123 CallbackInformation(blink::WebPermissionCallback* callback,
124 int worker_thread_id);
125 ~CallbackInformation();
127 blink::WebPermissionCallback* callback() const;
128 int worker_thread_id() const;
130 blink::WebPermissionCallback* ReleaseCallback();
132 private:
133 scoped_ptr<blink::WebPermissionCallback> callback_;
134 int worker_thread_id_;
136 DISALLOW_COPY_AND_ASSIGN(CallbackInformation);
138 using CallbackMap = IDMap<CallbackInformation, IDMapOwnPointer>;
139 CallbackMap pending_callbacks_;
141 ServiceRegistry* service_registry_;
142 PermissionServicePtr permission_service_;
144 DISALLOW_COPY_AND_ASSIGN(PermissionDispatcher);
147 } // namespace content
149 #endif // CONTENT_CHILD_PERMISSIONS_PERMISSION_DISPATCHER_H_