web_contents_permission_helper.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_
  5. #define ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/values.h"
  8. #include "content/public/browser/media_stream_request.h"
  9. #include "content/public/browser/web_contents_user_data.h"
  10. #include "third_party/blink/public/common/mediastream/media_stream_request.h"
  11. #include "third_party/blink/public/common/permissions/permission_utils.h"
  12. namespace electron {
  13. // Applies the permission requested for WebContents.
  14. class WebContentsPermissionHelper
  15. : public content::WebContentsUserData<WebContentsPermissionHelper> {
  16. public:
  17. ~WebContentsPermissionHelper() override;
  18. // disable copy
  19. WebContentsPermissionHelper(const WebContentsPermissionHelper&) = delete;
  20. WebContentsPermissionHelper& operator=(const WebContentsPermissionHelper&) =
  21. delete;
  22. enum class PermissionType {
  23. POINTER_LOCK = static_cast<int>(blink::PermissionType::NUM) + 1,
  24. FULLSCREEN,
  25. OPEN_EXTERNAL,
  26. SERIAL,
  27. HID,
  28. USB
  29. };
  30. // Asynchronous Requests
  31. void RequestFullscreenPermission(content::RenderFrameHost* requesting_frame,
  32. base::OnceCallback<void(bool)> callback);
  33. void RequestMediaAccessPermission(const content::MediaStreamRequest& request,
  34. content::MediaResponseCallback callback);
  35. void RequestPointerLockPermission(
  36. bool user_gesture,
  37. bool last_unlocked_by_target,
  38. base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
  39. callback);
  40. void RequestWebNotificationPermission(
  41. content::RenderFrameHost* requesting_frame,
  42. base::OnceCallback<void(bool)> callback);
  43. void RequestOpenExternalPermission(content::RenderFrameHost* requesting_frame,
  44. base::OnceCallback<void(bool)> callback,
  45. bool user_gesture,
  46. const GURL& url);
  47. // Synchronous Checks
  48. bool CheckMediaAccessPermission(const GURL& security_origin,
  49. blink::mojom::MediaStreamType type) const;
  50. bool CheckSerialAccessPermission(const url::Origin& embedding_origin) const;
  51. private:
  52. explicit WebContentsPermissionHelper(content::WebContents* web_contents);
  53. friend class content::WebContentsUserData<WebContentsPermissionHelper>;
  54. void RequestPermission(content::RenderFrameHost* requesting_frame,
  55. blink::PermissionType permission,
  56. base::OnceCallback<void(bool)> callback,
  57. bool user_gesture = false,
  58. base::Value::Dict details = {});
  59. bool CheckPermission(blink::PermissionType permission,
  60. base::Value::Dict details) const;
  61. // TODO(clavin): refactor to use the WebContents provided by the
  62. // WebContentsUserData base class instead of storing a duplicate ref
  63. raw_ptr<content::WebContents> web_contents_;
  64. WEB_CONTENTS_USER_DATA_KEY_DECL();
  65. };
  66. } // namespace electron
  67. #endif // ELECTRON_SHELL_BROWSER_WEB_CONTENTS_PERMISSION_HELPER_H_