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