file_system_access_permission_context.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. bool CanObtainReadPermission(const url::Origin& origin) override;
  66. bool CanObtainWritePermission(const url::Origin& origin) override;
  67. void SetLastPickedDirectory(const url::Origin& origin,
  68. const std::string& id,
  69. const content::PathInfo& path) override;
  70. content::PathInfo GetLastPickedDirectory(const url::Origin& origin,
  71. const std::string& id) override;
  72. base::FilePath GetWellKnownDirectoryPath(
  73. blink::mojom::WellKnownDirectory directory,
  74. const url::Origin& origin) override;
  75. std::u16string GetPickerTitle(
  76. const blink::mojom::FilePickerOptionsPtr& options) override;
  77. void NotifyEntryMoved(const url::Origin& origin,
  78. const content::PathInfo& old_path,
  79. const content::PathInfo& new_path) override;
  80. void OnFileCreatedFromShowSaveFilePicker(
  81. const GURL& file_picker_binding_context,
  82. const storage::FileSystemURL& url) override;
  83. void CheckPathsAgainstEnterprisePolicy(
  84. std::vector<content::PathInfo> entries,
  85. content::GlobalRenderFrameHostId frame_id,
  86. EntriesAllowedByEnterprisePolicyCallback callback) override;
  87. enum class Access { kRead, kWrite, kReadWrite };
  88. enum class RequestType { kNewPermission, kRestorePermissions };
  89. void RevokeActiveGrants(const url::Origin& origin,
  90. const base::FilePath& file_path = base::FilePath());
  91. bool OriginHasReadAccess(const url::Origin& origin);
  92. bool OriginHasWriteAccess(const url::Origin& origin);
  93. // Called by FileSystemAccessWebContentsHelper when a top-level frame was
  94. // navigated away from `origin` to some other origin.
  95. void NavigatedAwayFromOrigin(const url::Origin& origin);
  96. content::BrowserContext* browser_context() const { return browser_context_; }
  97. protected:
  98. SEQUENCE_CHECKER(sequence_checker_);
  99. private:
  100. class PermissionGrantImpl;
  101. void PermissionGrantDestroyed(PermissionGrantImpl* grant);
  102. void CheckPathAgainstBlocklist(const content::PathInfo& path,
  103. HandleType handle_type,
  104. base::OnceCallback<void(bool)> callback);
  105. void DidCheckPathAgainstBlocklist(const url::Origin& origin,
  106. const content::PathInfo& path,
  107. HandleType handle_type,
  108. UserAction user_action,
  109. content::GlobalRenderFrameHostId frame_id,
  110. bool should_block);
  111. void RunRestrictedPathCallback(SensitiveEntryResult result);
  112. void OnRestrictedPathResult(gin::Arguments* args);
  113. void MaybeEvictEntries(base::Value::Dict& dict);
  114. void CleanupPermissions(const url::Origin& origin);
  115. bool AncestorHasActivePermission(const url::Origin& origin,
  116. const base::FilePath& path,
  117. GrantType grant_type) const;
  118. base::WeakPtr<FileSystemAccessPermissionContext> GetWeakPtr();
  119. const raw_ptr<content::BrowserContext, DanglingUntriaged> browser_context_;
  120. struct OriginState;
  121. std::map<url::Origin, OriginState> active_permissions_map_;
  122. // Number of custom IDs an origin can specify.
  123. size_t max_ids_per_origin_ = 32u;
  124. const raw_ptr<const base::Clock> clock_;
  125. std::map<url::Origin, base::Value::Dict> id_pathinfo_map_;
  126. base::OnceCallback<void(SensitiveEntryResult)> callback_;
  127. base::WeakPtrFactory<FileSystemAccessPermissionContext> weak_factory_{this};
  128. };
  129. } // namespace electron
  130. #endif // ELECTRON_SHELL_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_PERMISSION_CONTEXT_H_