file_system_access_permission_context.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_forward.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/clock.h"
  13. #include "base/time/default_clock.h"
  14. #include "base/values.h"
  15. #include "components/keyed_service/core/keyed_service.h"
  16. #include "content/public/browser/file_system_access_permission_context.h"
  17. class GURL;
  18. namespace gin {
  19. class Arguments;
  20. } // namespace gin
  21. namespace base {
  22. class FilePath;
  23. } // namespace base
  24. namespace storage {
  25. class FileSystemURL;
  26. } // namespace storage
  27. namespace electron {
  28. class FileSystemAccessPermissionContext
  29. : public KeyedService,
  30. public content::FileSystemAccessPermissionContext {
  31. public:
  32. enum class GrantType { kRead, kWrite };
  33. explicit FileSystemAccessPermissionContext(
  34. content::BrowserContext* browser_context,
  35. const base::Clock* clock = base::DefaultClock::GetInstance());
  36. FileSystemAccessPermissionContext(const FileSystemAccessPermissionContext&) =
  37. delete;
  38. FileSystemAccessPermissionContext& operator=(
  39. const FileSystemAccessPermissionContext&) = delete;
  40. ~FileSystemAccessPermissionContext() override;
  41. // content::FileSystemAccessPermissionContext:
  42. scoped_refptr<content::FileSystemAccessPermissionGrant>
  43. GetReadPermissionGrant(const url::Origin& origin,
  44. const content::PathInfo& path,
  45. HandleType handle_type,
  46. UserAction user_action) override;
  47. scoped_refptr<content::FileSystemAccessPermissionGrant>
  48. GetWritePermissionGrant(const url::Origin& origin,
  49. const content::PathInfo& path,
  50. HandleType handle_type,
  51. UserAction user_action) override;
  52. void ConfirmSensitiveEntryAccess(
  53. const url::Origin& origin,
  54. const content::PathInfo& path,
  55. HandleType handle_type,
  56. UserAction user_action,
  57. content::GlobalRenderFrameHostId frame_id,
  58. base::OnceCallback<void(SensitiveEntryResult)> callback) override;
  59. void PerformAfterWriteChecks(
  60. std::unique_ptr<content::FileSystemAccessWriteItem> item,
  61. content::GlobalRenderFrameHostId frame_id,
  62. base::OnceCallback<void(AfterWriteCheckResult)> callback) override;
  63. bool IsFileTypeDangerous(const base::FilePath& path,
  64. const url::Origin& origin) override;
  65. base::expected<void, std::string> CanShowFilePicker(
  66. content::RenderFrameHost* rfh) override;
  67. bool CanObtainReadPermission(const url::Origin& origin) override;
  68. bool CanObtainWritePermission(const url::Origin& origin) override;
  69. void SetLastPickedDirectory(const url::Origin& origin,
  70. const std::string& id,
  71. const content::PathInfo& path) override;
  72. content::PathInfo GetLastPickedDirectory(const url::Origin& origin,
  73. const std::string& id) override;
  74. base::FilePath GetWellKnownDirectoryPath(
  75. blink::mojom::WellKnownDirectory directory,
  76. const url::Origin& origin) override;
  77. std::u16string GetPickerTitle(
  78. const blink::mojom::FilePickerOptionsPtr& options) override;
  79. void NotifyEntryMoved(const url::Origin& origin,
  80. const content::PathInfo& old_path,
  81. const content::PathInfo& new_path) override;
  82. void OnFileCreatedFromShowSaveFilePicker(
  83. const GURL& file_picker_binding_context,
  84. const storage::FileSystemURL& url) override;
  85. void CheckPathsAgainstEnterprisePolicy(
  86. std::vector<content::PathInfo> entries,
  87. content::GlobalRenderFrameHostId frame_id,
  88. EntriesAllowedByEnterprisePolicyCallback callback) override;
  89. enum class Access { kRead, kWrite, kReadWrite };
  90. enum class RequestType { kNewPermission, kRestorePermissions };
  91. void RevokeActiveGrants(const url::Origin& origin,
  92. const base::FilePath& file_path = base::FilePath());
  93. bool OriginHasReadAccess(const url::Origin& origin);
  94. bool OriginHasWriteAccess(const url::Origin& origin);
  95. // Called by FileSystemAccessWebContentsHelper when a top-level frame was
  96. // navigated away from `origin` to some other origin.
  97. void NavigatedAwayFromOrigin(const url::Origin& origin);
  98. content::BrowserContext* browser_context() const { return browser_context_; }
  99. protected:
  100. SEQUENCE_CHECKER(sequence_checker_);
  101. private:
  102. class PermissionGrantImpl;
  103. void PermissionGrantDestroyed(PermissionGrantImpl* grant);
  104. void CheckPathAgainstBlocklist(const content::PathInfo& path,
  105. HandleType handle_type,
  106. base::OnceCallback<void(bool)> callback);
  107. void DidCheckPathAgainstBlocklist(const url::Origin& origin,
  108. const content::PathInfo& path,
  109. HandleType handle_type,
  110. UserAction user_action,
  111. content::GlobalRenderFrameHostId frame_id,
  112. bool should_block);
  113. void RunRestrictedPathCallback(SensitiveEntryResult result);
  114. void OnRestrictedPathResult(gin::Arguments* args);
  115. void MaybeEvictEntries(base::Value::Dict& dict);
  116. void CleanupPermissions(const url::Origin& origin);
  117. bool AncestorHasActivePermission(const url::Origin& origin,
  118. const base::FilePath& path,
  119. GrantType grant_type) const;
  120. base::WeakPtr<FileSystemAccessPermissionContext> GetWeakPtr();
  121. const raw_ptr<content::BrowserContext, DanglingUntriaged> browser_context_;
  122. struct OriginState;
  123. std::map<url::Origin, OriginState> active_permissions_map_;
  124. // Number of custom IDs an origin can specify.
  125. size_t max_ids_per_origin_ = 32u;
  126. const raw_ptr<const base::Clock> clock_;
  127. std::map<url::Origin, base::Value::Dict> id_pathinfo_map_;
  128. base::OnceCallback<void(SensitiveEntryResult)> callback_;
  129. base::WeakPtrFactory<FileSystemAccessPermissionContext> weak_factory_{this};
  130. };
  131. } // namespace electron
  132. #endif // ELECTRON_SHELL_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_