media_stream_devices_controller.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2012 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. #ifndef COMPONENTS_WEBRTC_MEDIA_STREAM_DEVICES_CONTROLLER_H_
  5. #define COMPONENTS_WEBRTC_MEDIA_STREAM_DEVICES_CONTROLLER_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #include "components/content_settings/core/common/content_settings.h"
  12. #include "components/webrtc/media_stream_device_enumerator_impl.h"
  13. #include "content/public/browser/media_stream_request.h"
  14. #include "third_party/blink/public/common/mediastream/media_stream_request.h"
  15. #include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
  16. #include "third_party/blink/public/mojom/permissions/permission_status.mojom.h"
  17. namespace blink {
  18. enum class PermissionType;
  19. }
  20. namespace content {
  21. enum class PermissionStatusSource;
  22. class WebContents;
  23. }
  24. namespace webrtc {
  25. class MediaStreamDeviceEnumerator;
  26. // A class that provides logic for microphone/camera requests originating in the
  27. // renderer.
  28. class MediaStreamDevicesController {
  29. public:
  30. typedef base::OnceCallback<void(
  31. const blink::mojom::StreamDevicesSet& stream_devices_set,
  32. blink::mojom::MediaStreamRequestResult result,
  33. bool blocked_by_permissions_policy,
  34. ContentSetting audio_setting,
  35. ContentSetting video_setting)>
  36. ResultCallback;
  37. // Requests the mic/camera permissions described in |request|, using
  38. // |enumerator| to list the system's devices. The result of the request is
  39. // synchronously or asynchronously returned via |callback|.
  40. static void RequestPermissions(const content::MediaStreamRequest& request,
  41. MediaStreamDeviceEnumerator* enumerator,
  42. ResultCallback callback);
  43. ~MediaStreamDevicesController();
  44. private:
  45. MediaStreamDevicesController(content::WebContents* web_contents,
  46. MediaStreamDeviceEnumerator* enumerator,
  47. const content::MediaStreamRequest& request,
  48. ResultCallback callback);
  49. MediaStreamDevicesController(const MediaStreamDevicesController&) = delete;
  50. MediaStreamDevicesController& operator=(MediaStreamDevicesController&) =
  51. delete;
  52. static void RequestAndroidPermissionsIfNeeded(
  53. content::WebContents* web_contents,
  54. std::unique_ptr<MediaStreamDevicesController> controller,
  55. bool did_prompt_for_audio,
  56. bool did_prompt_for_video,
  57. const std::vector<blink::mojom::PermissionStatus>& responses);
  58. // Returns true if audio/video should be requested through the
  59. // PermissionManager. We won't try to request permission if the request is
  60. // already blocked for some other reason, e.g. there are no devices available.
  61. bool ShouldRequestAudio() const;
  62. bool ShouldRequestVideo() const;
  63. // Returns a list of devices available for the request for the given
  64. // audio/video permission settings.
  65. blink::mojom::StreamDevicesSetPtr GetDevices(ContentSetting audio_setting,
  66. ContentSetting video_setting);
  67. // Runs |callback_| with the current audio/video permission settings.
  68. void RunCallback(bool blocked_by_permissions_policy);
  69. // Returns the content settings for the given permission type and request.
  70. ContentSetting GetContentSetting(
  71. blink::PermissionType permission,
  72. const content::MediaStreamRequest& request,
  73. blink::mojom::MediaStreamRequestResult* denial_reason) const;
  74. // Returns true if clicking allow on the dialog should give access to the
  75. // requested devices.
  76. bool IsUserAcceptAllowed(blink::PermissionType permission) const;
  77. bool PermissionIsBlockedForReason(
  78. blink::PermissionType permission,
  79. content::PermissionStatusSource reason) const;
  80. // Called when a permission prompt is answered through the PermissionManager.
  81. void PromptAnsweredGroupedRequest(
  82. const std::vector<blink::mojom::PermissionStatus>& permissions_status);
  83. bool HasAvailableDevices(blink::PermissionType permission,
  84. const std::string& device_id) const;
  85. // The current state of the audio/video content settings which may be updated
  86. // through the lifetime of the request.
  87. ContentSetting audio_setting_;
  88. ContentSetting video_setting_;
  89. blink::mojom::MediaStreamRequestResult denial_reason_;
  90. raw_ptr<content::WebContents> web_contents_;
  91. // The object which lists available devices.
  92. raw_ptr<MediaStreamDeviceEnumerator> enumerator_;
  93. // This enumerator is used as |enumerator_| when the instance passed into the
  94. // constructor is null.
  95. MediaStreamDeviceEnumeratorImpl owned_enumerator_;
  96. // The original request for access to devices.
  97. const content::MediaStreamRequest request_;
  98. // The callback that needs to be run to notify WebRTC of whether access to
  99. // audio/video devices was granted or not.
  100. ResultCallback callback_;
  101. };
  102. } // namespace webrtc
  103. #endif // COMPONENTS_WEBRTC_MEDIA_STREAM_DEVICES_CONTROLLER_H_