1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86

content / browser / media / media_devices_permission_checker.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_
#define CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_

#include "base/functional/callback.h"
#include "content/browser/renderer_host/media/media_devices_manager.h"
#include "content/common/content_export.h"

using blink::mojom::MediaDeviceType;

namespace content {

// This class provides various utility functions to check if a render frame
// has permission to access media devices. Note that none of the methods
// prompts the user to request permission.
class CONTENT_EXPORT MediaDevicesPermissionChecker {
 public:
  MediaDevicesPermissionChecker();

  // This constructor creates a MediaDevicesPermissionChecker that replies
  // |override_value| to all permission requests. Use only for testing.
  explicit MediaDevicesPermissionChecker(bool override_value);

  MediaDevicesPermissionChecker(const MediaDevicesPermissionChecker&) = delete;
  MediaDevicesPermissionChecker& operator=(
      const MediaDevicesPermissionChecker&) = delete;

  // Checks if the origin associated to a render frame identified by
  // |render_process_id| and |render_frame_id| is allowed to access the media
  // device type |device_type|.
  // This method must be called on the UI thread.
  bool CheckPermissionOnUIThread(MediaDeviceType device_type,
                                 int render_process_id,
                                 int render_frame_id) const;

  // This function checks if speaker selection is denied for the
  // SelectAudioOutput API. This API requires a different permission check
  // compared to other MediaDevices APIs.
  void IsSpeakerSelectionDenied(
      int render_process_id,
      int render_frame_id,
      base::OnceCallback<void(MediaDevicesManager::PermissionDeniedState)>
          callback) const;

  // Checks if the origin associated to a render frame identified by
  // |render_process_id| and |render_frame_id| is allowed to access the media
  // device type |device_type|. The result is passed to |callback|.
  // This method can be called on any thread. |callback| is fired on the same
  // thread this method is called on.
  void CheckPermission(MediaDeviceType device_type,
                       int render_process_id,
                       int render_frame_id,
                       base::OnceCallback<void(bool)> callback) const;

  // Checks if the origin associated to a render frame identified by
  // |render_process_id| and |render_frame_id| is allowed to access the media
  // device types marked with a value of true in |requested_device_types|. The
  // result is passed to |callback|. The result is indexed by
  // blink::mojom::MediaDeviceType. Entries in the result with a value of true
  // for requested device types indicate that the frame has permission to access
  // devices of the corresponding types. This method can be called on any
  // thread. |callback| is fired on the same thread this method is called on.
  void CheckPermissions(
      MediaDevicesManager::BoolDeviceTypes requested_device_types,
      int render_process_id,
      int render_frame_id,
      base::OnceCallback<void(const MediaDevicesManager::BoolDeviceTypes&)>
          callback) const;

  // Returns true if the origin associated to a render frame identified by
  // |render_process_id| and |render_frame_id| is allowed to control camera
  // movement (pan, tilt, and zoom). Otherwise, returns false.
  static bool HasPanTiltZoomPermissionGrantedOnUIThread(int render_process_id,
                                                        int render_frame_id);

 private:
  const bool use_override_;
  const bool override_value_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_MEDIA_MEDIA_DEVICES_PERMISSION_CHECKER_H_