file_system_access_permission_context.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) 2024 Microsoft, GmbH
  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_FILE_SYSTEM_ACCESS_ELECTRON_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_
  5. #define ELECTRON_SHELL_BROWSER_FILE_SYSTEM_ACCESS_ELECTRON_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_
  6. #include "shell/browser/file_system_access/file_system_access_permission_context.h"
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/functional/callback.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/keyed_service/core/keyed_service.h"
  13. #include "content/public/browser/file_system_access_permission_context.h"
  14. class GURL;
  15. namespace base {
  16. class FilePath;
  17. } // namespace base
  18. namespace storage {
  19. class FileSystemURL;
  20. } // namespace storage
  21. namespace electron {
  22. class FileSystemAccessPermissionContext
  23. : public KeyedService,
  24. public content::FileSystemAccessPermissionContext {
  25. public:
  26. enum class GrantType { kRead, kWrite };
  27. explicit FileSystemAccessPermissionContext(
  28. content::BrowserContext* browser_context);
  29. FileSystemAccessPermissionContext(const FileSystemAccessPermissionContext&) =
  30. delete;
  31. FileSystemAccessPermissionContext& operator=(
  32. const FileSystemAccessPermissionContext&) = delete;
  33. ~FileSystemAccessPermissionContext() override;
  34. // content::FileSystemAccessPermissionContext:
  35. scoped_refptr<content::FileSystemAccessPermissionGrant>
  36. GetReadPermissionGrant(const url::Origin& origin,
  37. const base::FilePath& path,
  38. HandleType handle_type,
  39. UserAction user_action) override;
  40. scoped_refptr<content::FileSystemAccessPermissionGrant>
  41. GetWritePermissionGrant(const url::Origin& origin,
  42. const base::FilePath& path,
  43. HandleType handle_type,
  44. UserAction user_action) override;
  45. void ConfirmSensitiveEntryAccess(
  46. const url::Origin& origin,
  47. PathType path_type,
  48. const base::FilePath& path,
  49. HandleType handle_type,
  50. UserAction user_action,
  51. content::GlobalRenderFrameHostId frame_id,
  52. base::OnceCallback<void(SensitiveEntryResult)> callback) override;
  53. void PerformAfterWriteChecks(
  54. std::unique_ptr<content::FileSystemAccessWriteItem> item,
  55. content::GlobalRenderFrameHostId frame_id,
  56. base::OnceCallback<void(AfterWriteCheckResult)> callback) override;
  57. bool CanObtainReadPermission(const url::Origin& origin) override;
  58. bool CanObtainWritePermission(const url::Origin& origin) override;
  59. void SetLastPickedDirectory(const url::Origin& origin,
  60. const std::string& id,
  61. const base::FilePath& path,
  62. const PathType type) override;
  63. PathInfo GetLastPickedDirectory(const url::Origin& origin,
  64. const std::string& id) override;
  65. base::FilePath GetWellKnownDirectoryPath(
  66. blink::mojom::WellKnownDirectory directory,
  67. const url::Origin& origin) override;
  68. std::u16string GetPickerTitle(
  69. const blink::mojom::FilePickerOptionsPtr& options) override;
  70. void NotifyEntryMoved(const url::Origin& origin,
  71. const base::FilePath& old_path,
  72. const base::FilePath& new_path) override;
  73. void OnFileCreatedFromShowSaveFilePicker(
  74. const GURL& file_picker_binding_context,
  75. const storage::FileSystemURL& url) override;
  76. void CheckPathsAgainstEnterprisePolicy(
  77. std::vector<PathInfo> entries,
  78. content::GlobalRenderFrameHostId frame_id,
  79. EntriesAllowedByEnterprisePolicyCallback callback) override;
  80. enum class Access { kRead, kWrite, kReadWrite };
  81. enum class RequestType { kNewPermission, kRestorePermissions };
  82. void RevokeGrant(const url::Origin& origin,
  83. const base::FilePath& file_path = base::FilePath());
  84. bool OriginHasReadAccess(const url::Origin& origin);
  85. bool OriginHasWriteAccess(const url::Origin& origin);
  86. content::BrowserContext* browser_context() const { return browser_context_; }
  87. protected:
  88. SEQUENCE_CHECKER(sequence_checker_);
  89. private:
  90. class PermissionGrantImpl;
  91. void PermissionGrantDestroyed(PermissionGrantImpl* grant);
  92. void CheckPathAgainstBlocklist(PathType path_type,
  93. const base::FilePath& path,
  94. HandleType handle_type,
  95. base::OnceCallback<void(bool)> callback);
  96. void DidCheckPathAgainstBlocklist(
  97. const url::Origin& origin,
  98. const base::FilePath& path,
  99. HandleType handle_type,
  100. UserAction user_action,
  101. content::GlobalRenderFrameHostId frame_id,
  102. base::OnceCallback<void(SensitiveEntryResult)> callback,
  103. bool should_block);
  104. void CleanupPermissions(const url::Origin& origin);
  105. bool AncestorHasActivePermission(const url::Origin& origin,
  106. const base::FilePath& path,
  107. GrantType grant_type) const;
  108. base::WeakPtr<FileSystemAccessPermissionContext> GetWeakPtr();
  109. const raw_ptr<content::BrowserContext, DanglingUntriaged> browser_context_;
  110. struct OriginState;
  111. std::map<url::Origin, OriginState> active_permissions_map_;
  112. base::WeakPtrFactory<FileSystemAccessPermissionContext> weak_factory_{this};
  113. };
  114. } // namespace electron
  115. #endif // ELECTRON_SHELL_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_