file_system_access_permission_context.h 6.6 KB

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