Chromium Code Reviews| Index: webkit/fileapi/external_mount_points.h |
| diff --git a/webkit/fileapi/external_mount_points.h b/webkit/fileapi/external_mount_points.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7c8055a9caf465520fc119143dea102f4c499e13 |
| --- /dev/null |
| +++ b/webkit/fileapi/external_mount_points.h |
| @@ -0,0 +1,159 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |
| +#define WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/synchronization/lock.h" |
| +#include "webkit/fileapi/file_system_types.h" |
| +#include "webkit/fileapi/mount_points.h" |
| +#include "webkit/storage/webkit_storage_export.h" |
| + |
| +class FilePath; |
| + |
| +namespace fileapi { |
| +class RemoteFileSystemProxyInterface; |
| +} |
| + |
| +namespace fileapi { |
| + |
| +// Manages external filesystem namespaces that are identified by 'mount name' |
| +// and are persisted until RevokeFileSystem is called. |
| +// Files in an external filesystem are identified by a filesystem URL like: |
| +// |
| +// filesystem:<origin>/external/<mount_name>/relative/path |
| +// |
| +class WEBKIT_STORAGE_EXPORT ExternalMountPoints |
| + : public base::RefCountedThreadSafe<ExternalMountPoints>, |
| + public MountPoints { |
| + public: |
| + static ExternalMountPoints* GetSystemInstance(); |
| + static scoped_refptr<ExternalMountPoints> CreateRefCounted(); |
| + |
| + // Registers a new named external filesystem. |
| + // The |path| is registered as the root path of the mount point which |
| + // is identified by a URL "filesystem:.../external/mount_name". |
| + // |
| + // For example, if the path "/media/removable" is registered with |
| + // the mount_name "removable", a filesystem URL like |
| + // "filesystem:.../external/removable/a/b" will be resolved as |
| + // "/media/removable/a/b". |
| + // |
| + // The |mount_name| should NOT contain a path separator '/'. |
| + // Returns false if the given name is already registered. |
| + // |
| + // Overlapping mount points are not allowed. Adding mount point whose path |
|
kinuko
2013/01/08 12:22:43
nit: "Overlapping mount points in a single MountPo
tbarzic
2013/01/09 01:26:34
Done.
|
| + // overlaps with an existing mount point will fail. |
| + // |
| + // If not empty, |path| must be absolute. It is allowed for the path to be |
| + // empty, but |GetVirtualPath| will not work for those mount points. |
| + // |
| + // An external file system registered by this method can be revoked |
| + // by calling RevokeFileSystem with |mount_name|. |
| + bool RegisterFileSystem(const std::string& mount_name, |
| + FileSystemType type, |
| + const FilePath& path); |
| + |
| + // Same as |RegisterExternalFileSystem|, but registers a remote file system |
| + // proxy for the file system. |
| + bool RegisterRemoteFileSystem(const std::string& mount_name, |
| + FileSystemType type, |
| + RemoteFileSystemProxyInterface* remote_proxy, |
| + const FilePath& path); |
| + |
| + // Retrieves the remote file system proxy for the registered file system. |
| + // Returns NULL if there is no file system with given name, or if the file |
| + // system does not have a remote file system proxy. |
| + RemoteFileSystemProxyInterface* GetRemoteFileSystemProxy( |
| + const std::string& mount_name) const; |
| + |
| + // Returns a list of registered MountPointInfos (of <mount_name, path>). |
| + void AppendMountPoints(std::vector<MountPointInfo>* mount_points) const; |
| + |
| + // MountPoints overrides. |
| + virtual bool CanCrackMountType(FileSystemType type) const OVERRIDE; |
| + virtual bool GetRegisteredPath(const std::string& mount_name, |
| + FilePath* path) const OVERRIDE; |
| + virtual bool RevokeFileSystem(const std::string& mount_name) OVERRIDE; |
| + |
| + // Converts a path on a registered file system to virtual path relative to the |
| + // file system root. E.g. if 'Downloads' file system is mapped to |
| + // '/usr/local/home/Downloads', and |absolute| path is set to |
| + // 'usr/local/home/Downloads/foo', the method will set |virtual_path| to |
| + // 'Downloads/foo'. |
| + bool GetVirtualPath(const FilePath& absolute_path, FilePath* virtual_path); |
| + |
| + // Cracks the given |virtual_path| (which is the path part of a filesystem URL |
| + // without /'/external' prefix part) and populates the |
| + // |mount_name|, |type|, and |path| if the <mount_name> part embedded in |
| + // the |virtual_path| (i.e. the first component of the |virtual_path|) is a |
| + // valid registered filesystem ID or mount name for an external filesystem. |
| + // |
| + // Returns false if the given virtual_path or the cracked mount_name |
| + // is not valid. |
| + bool CrackExternalPath(const FilePath& virtual_path, |
| + std::string* mount_name, |
| + FileSystemType* type, |
| + FilePath* path) const; |
| + |
| + // Returns the virtual root path that looks like /<mount_name>. |
| + FilePath CreateVirtualRootPath(const std::string& mount_name) const; |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<ExternalMountPoints>; |
| + |
| + // Represents each file system instance (defined in the .cc). |
| + class Instance; |
| + |
| + typedef std::map<std::string, Instance*> NameToInstance; |
| + |
| + // Reverse map from registered path to its corresponding mount name. |
| + typedef std::map<FilePath, std::string> PathToName; |
| + |
| + // Use |GetSystemInstance| of |CreateRefCounted| to get an instance. |
| + explicit ExternalMountPoints(bool is_system_instance); |
| + virtual ~ExternalMountPoints(); |
| + |
| + // Performs sanity checks on the new mount point. |
| + // Checks following: |
| + // - there is no registered mount point with mount_name |
| + // - path does not reference parent |
| + // - path is absolute |
| + // - path does not overlap with an existing mount point path. |
| + // |
| + // |lock_| should be taken before calling this method. |
| + bool ValidateNewMountPoint(const std::string& mount_name, |
| + const FilePath& path); |
| + |
| + // This lock needs to be obtained when accessing the instance_map_. |
| + mutable base::Lock lock_; |
| + |
| + NameToInstance instance_map_; |
| + PathToName path_to_name_map_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints); |
| +}; |
| + |
| +// Registers a scoped external filesystem which gets revoked when it scopes out. |
| +class WEBKIT_STORAGE_EXPORT ScopedExternalFileSystem { |
| + public: |
| + ScopedExternalFileSystem(const std::string& mount_name, |
| + FileSystemType type, |
| + const FilePath& path); |
| + ~ScopedExternalFileSystem(); |
| + |
| + FilePath GetVirtualRootPath() const; |
| + |
| + private: |
| + const std::string mount_name_; |
| +}; |
| + |
| +} // namespace fileapi |
| + |
| +#endif // WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |