guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 1 | // Copyright 2016 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. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_ |
| 6 | #define CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/callback.h" |
guidou | 098908e5 | 2016-11-09 23:06:22 | [diff] [blame] | 11 | #include "base/macros.h" |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 12 | #include "content/browser/renderer_host/media/media_devices_manager.h" |
| 13 | #include "content/common/content_export.h" |
| 14 | |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 15 | namespace content { |
| 16 | |
| 17 | // This class provides various utility functions to check if a render frame |
| 18 | // has permission to access media devices. Note that none of the methods |
| 19 | // prompts the user to request permission. |
| 20 | class CONTENT_EXPORT MediaDevicesPermissionChecker { |
| 21 | public: |
| 22 | MediaDevicesPermissionChecker(); |
guidou | 098908e5 | 2016-11-09 23:06:22 | [diff] [blame] | 23 | // This constructor creates a MediaDevicesPermissionChecker that replies |
| 24 | // |override_value| to all permission requests. Use only for testing. |
| 25 | explicit MediaDevicesPermissionChecker(bool override_value); |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 26 | |
guidou | a72075a | 2017-05-15 08:36:17 | [diff] [blame] | 27 | // Checks if the origin associated to a render frame identified by |
| 28 | // |render_process_id| and |render_frame_id| is allowed to access the media |
| 29 | // device type |device_type|. |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 30 | // This method must be called on the UI thread. |
| 31 | bool CheckPermissionOnUIThread(MediaDeviceType device_type, |
| 32 | int render_process_id, |
guidou | a72075a | 2017-05-15 08:36:17 | [diff] [blame] | 33 | int render_frame_id) const; |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 34 | |
guidou | a72075a | 2017-05-15 08:36:17 | [diff] [blame] | 35 | // Checks if the origin associated to a render frame identified by |
| 36 | // |render_process_id| and |render_frame_id| is allowed to access the media |
| 37 | // device type |device_type|. The result is passed to |callback|. |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 38 | // This method can be called on any thread. |callback| is fired on the same |
| 39 | // thread this method is called on. |
| 40 | void CheckPermission(MediaDeviceType device_type, |
| 41 | int render_process_id, |
| 42 | int render_frame_id, |
maxmorin | fd201228 | 2017-05-16 10:30:55 | [diff] [blame] | 43 | base::OnceCallback<void(bool)> callback) const; |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 44 | |
guidou | a72075a | 2017-05-15 08:36:17 | [diff] [blame] | 45 | // Checks if the origin associated to a render frame identified by |
| 46 | // |render_process_id| and |render_frame_id| is allowed to access the media |
| 47 | // device types marked with a value of true in |requested_device_types|. The |
| 48 | // result is passed to |callback|. The result is indexed by MediaDeviceType. |
| 49 | // Entries in the result with a value of true for requested device types |
| 50 | // indicate that the frame has permission to access devices of the |
| 51 | // corresponding types. |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 52 | // This method can be called on any thread. |callback| is fired on the same |
| 53 | // thread this method is called on. |
| 54 | void CheckPermissions( |
| 55 | MediaDevicesManager::BoolDeviceTypes requested_device_types, |
| 56 | int render_process_id, |
| 57 | int render_frame_id, |
maxmorin | fd201228 | 2017-05-16 10:30:55 | [diff] [blame] | 58 | base::OnceCallback<void(const MediaDevicesManager::BoolDeviceTypes&)> |
guidou | 098908e5 | 2016-11-09 23:06:22 | [diff] [blame] | 59 | callback) const; |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 60 | |
| 61 | private: |
guidou | 098908e5 | 2016-11-09 23:06:22 | [diff] [blame] | 62 | const bool use_override_; |
| 63 | const bool override_value_; |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(MediaDevicesPermissionChecker); |
guidou | df036b3 | 2016-10-31 14:51:14 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace content |
| 69 | |
| 70 | #endif // CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_ |