external_mount_points.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef STORAGE_BROWSER_FILE_SYSTEM_EXTERNAL_MOUNT_POINTS_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_EXTERNAL_MOUNT_POINTS_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/synchronization/lock.h"
  13. #include "storage/browser/file_system/mount_points.h"
  14. #include "storage/common/file_system/file_system_mount_option.h"
  15. #include "storage/common/file_system/file_system_types.h"
  16. class GURL;
  17. namespace base {
  18. class FilePath;
  19. } // namespace base
  20. namespace blink {
  21. class StorageKey;
  22. } // namespace blink
  23. namespace storage {
  24. class FileSystemURL;
  25. // Manages external filesystem namespaces that are identified by 'mount name'
  26. // and are persisted until RevokeFileSystem is called.
  27. // Files in an external filesystem are identified by a filesystem URL like:
  28. //
  29. // filesystem:<origin>/external/<mount_name>/relative/path
  30. //
  31. class COMPONENT_EXPORT(STORAGE_BROWSER) ExternalMountPoints
  32. : public base::RefCountedThreadSafe<ExternalMountPoints>,
  33. public MountPoints {
  34. public:
  35. static ExternalMountPoints* GetSystemInstance();
  36. static scoped_refptr<ExternalMountPoints> CreateRefCounted();
  37. ExternalMountPoints(const ExternalMountPoints&) = delete;
  38. ExternalMountPoints& operator=(const ExternalMountPoints&) = delete;
  39. // Registers a new named external filesystem.
  40. // The |path| is registered as the root path of the mount point which
  41. // is identified by a URL "filesystem:.../external/mount_name".
  42. //
  43. // For example, if the path "/media/removable" is registered with
  44. // the mount_name "removable", a filesystem URL like
  45. // "filesystem:.../external/removable/a/b" will be resolved as
  46. // "/media/removable/a/b".
  47. //
  48. // The |mount_name| should NOT contain a path separator '/'.
  49. // Returns false if the given name is already registered.
  50. //
  51. // Overlapping mount points in a single MountPoints instance are not allowed.
  52. // Adding mount point whose path overlaps with an existing mount point will
  53. // fail except for media galleries, which do not count toward registered
  54. // paths for overlap calculation.
  55. //
  56. // If not empty, |path| must be absolute. It is allowed for the path to be
  57. // empty, but |GetVirtualPath| will not work for those mount points.
  58. //
  59. // An external file system registered by this method can be revoked
  60. // by calling RevokeFileSystem with |mount_name|.
  61. bool RegisterFileSystem(const std::string& mount_name,
  62. FileSystemType type,
  63. const FileSystemMountOption& mount_option,
  64. const base::FilePath& path);
  65. // MountPoints overrides.
  66. bool HandlesFileSystemMountType(FileSystemType type) const override;
  67. bool RevokeFileSystem(const std::string& mount_name) override;
  68. bool GetRegisteredPath(const std::string& mount_name,
  69. base::FilePath* path) const override;
  70. bool CrackVirtualPath(const base::FilePath& virtual_path,
  71. std::string* mount_name,
  72. FileSystemType* type,
  73. std::string* cracked_id,
  74. base::FilePath* path,
  75. FileSystemMountOption* mount_option) const override;
  76. FileSystemURL CrackURL(const GURL& url,
  77. const blink::StorageKey& storage_key) const override;
  78. FileSystemURL CreateCrackedFileSystemURL(
  79. const blink::StorageKey& storage_key,
  80. FileSystemType type,
  81. const base::FilePath& virtual_path) const override;
  82. // Returns a list of registered MountPointInfos (of <mount_name, path>).
  83. void AddMountPointInfosTo(std::vector<MountPointInfo>* mount_points) const;
  84. // Converts a path on a registered file system to virtual path relative to the
  85. // file system root. E.g. if 'Downloads' file system is mapped to
  86. // '/usr/local/home/Downloads', and |absolute| path is set to
  87. // '/usr/local/home/Downloads/foo', the method will set |virtual_path| to
  88. // 'Downloads/foo'.
  89. // Returns false if the path cannot be resolved (e.g. if the path is not
  90. // part of any registered filesystem).
  91. //
  92. // Media gallery type file systems do not count for this calculation. i.e.
  93. // if only a media gallery is registered for the path, false will be returned.
  94. // If a media gallery and another file system are registered for related
  95. // paths, only the other registration is taken into account.
  96. //
  97. // Returned virtual_path will have normalized path separators.
  98. bool GetVirtualPath(const base::FilePath& absolute_path,
  99. base::FilePath* virtual_path) const;
  100. // Returns the virtual root path that looks like /<mount_name>.
  101. base::FilePath CreateVirtualRootPath(const std::string& mount_name) const;
  102. FileSystemURL CreateExternalFileSystemURL(
  103. const blink::StorageKey& storage_key,
  104. const std::string& mount_name,
  105. const base::FilePath& path) const;
  106. // Revoke all registered filesystems. Used only by testing (for clean-ups).
  107. void RevokeAllFileSystems();
  108. private:
  109. friend class base::RefCountedThreadSafe<ExternalMountPoints>;
  110. // Represents each file system instance (defined in the .cc).
  111. class Instance;
  112. using NameToInstance = std::map<std::string, std::unique_ptr<Instance>>;
  113. // Use |GetSystemInstance| of |CreateRefCounted| to get an instance.
  114. ExternalMountPoints();
  115. ~ExternalMountPoints() override;
  116. // MountPoint overrides.
  117. FileSystemURL CrackFileSystemURL(const FileSystemURL& url) const override;
  118. // Performs sanity checks on the new mount point.
  119. // Checks the following:
  120. // - there is no registered mount point with mount_name
  121. // - path does not contain a reference to a parent
  122. // - path is absolute
  123. // - path does not overlap with an existing mount point path unless it is a
  124. // media gallery type.
  125. //
  126. // |lock_| should be taken before calling this method.
  127. bool ValidateNewMountPoint(const std::string& mount_name,
  128. FileSystemType type,
  129. const base::FilePath& path);
  130. // This lock needs to be obtained when accessing the instance_map_.
  131. mutable base::Lock lock_;
  132. NameToInstance instance_map_;
  133. // Reverse map from registered path to its corresponding mount name.
  134. std::map<base::FilePath, std::string> path_to_name_map_;
  135. };
  136. } // namespace storage
  137. #endif // STORAGE_BROWSER_FILE_SYSTEM_EXTERNAL_MOUNT_POINTS_H_