OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Base class for MediaPermission implementations in content. This class allows | |
6 // multiple pending PermissionService calls by managing callbacks for its | |
7 // subclasses. This class is not thread safe and should only be used on one | |
8 // thread. | |
9 | |
10 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ |
11 #define CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ |
12 | 7 |
13 #include <stdint.h> | 8 #include <stdint.h> |
14 | 9 |
15 #include <map> | 10 #include <map> |
16 | 11 |
17 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
18 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
19 #include "base/threading/thread_checker.h" | |
20 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "content/common/permission_service.mojom.h" |
| 17 #include "content/public/renderer/render_frame_observer.h" |
21 #include "media/base/media_permission.h" | 18 #include "media/base/media_permission.h" |
22 | 19 |
| 20 namespace base { |
| 21 class SingleThreadTaskRunner; |
| 22 } |
| 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
25 class CONTENT_EXPORT MediaPermissionDispatcher : public media::MediaPermission { | 26 // MediaPermission implementation. |
| 27 class CONTENT_EXPORT MediaPermissionDispatcher : public media::MediaPermission, |
| 28 public RenderFrameObserver { |
26 public: | 29 public: |
27 MediaPermissionDispatcher(); | 30 explicit MediaPermissionDispatcher(RenderFrame* render_frame); |
28 ~MediaPermissionDispatcher() override; | 31 ~MediaPermissionDispatcher() override; |
29 | 32 |
30 protected: | 33 // media::MediaPermission implementation. |
31 // Register callbacks for PermissionService calls. | 34 // Note: Can be called on any thread. The |permission_status_cb| will always |
32 uint32_t RegisterCallback(const PermissionStatusCB& permission_status_cb); | 35 // be fired on the thread where these methods are called. |
33 | 36 void HasPermission(Type type, |
34 // Deliver the permission status |granted| to the callback identified by | 37 const GURL& security_origin, |
35 // |request_id|. | 38 const PermissionStatusCB& permission_status_cb) override; |
36 void DeliverResult(uint32_t request_id, bool granted); | 39 void RequestPermission( |
37 | 40 Type type, |
38 base::ThreadChecker& thread_checker() { return thread_checker_; } | 41 const GURL& security_origin, |
| 42 const PermissionStatusCB& permission_status_cb) override; |
39 | 43 |
40 private: | 44 private: |
41 // Map of request IDs and pending PermissionStatusCBs. | 45 // Map of request IDs and pending PermissionStatusCBs. |
42 typedef std::map<uint32_t, PermissionStatusCB> RequestMap; | 46 typedef std::map<uint32_t, PermissionStatusCB> RequestMap; |
43 | 47 |
44 base::ThreadChecker thread_checker_; | 48 // Register PermissionStatusCBs. Returns |request_id| that can be used to make |
| 49 // PermissionService calls. |
| 50 uint32_t RegisterCallback(const PermissionStatusCB& permission_status_cb); |
45 | 51 |
| 52 // Callback for |permission_service_| calls. |
| 53 void OnPermissionStatus(uint32_t request_id, PermissionStatus status); |
| 54 |
| 55 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
46 uint32_t next_request_id_; | 56 uint32_t next_request_id_; |
47 RequestMap requests_; | 57 RequestMap requests_; |
| 58 PermissionServicePtr permission_service_; |
| 59 |
| 60 // Used to safely post MediaPermission calls for execution on |task_runner_|. |
| 61 base::WeakPtr<MediaPermissionDispatcher> weak_ptr_; |
| 62 |
| 63 base::WeakPtrFactory<MediaPermissionDispatcher> weak_factory_; |
48 | 64 |
49 DISALLOW_COPY_AND_ASSIGN(MediaPermissionDispatcher); | 65 DISALLOW_COPY_AND_ASSIGN(MediaPermissionDispatcher); |
50 }; | 66 }; |
51 | 67 |
52 } // namespace content | 68 } // namespace content |
53 | 69 |
54 #endif // CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ | 70 #endif // CONTENT_RENDERER_MEDIA_MEDIA_PERMISSION_DISPATCHER_H_ |
OLD | NEW |