summaryrefslogtreecommitdiff
path: root/chromium/webkit/browser/fileapi/file_system_url.h
blob: d6443e69189dd1f4f2b238acaca4d2cfe7ef18bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
#define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_

#include <set>
#include <string>

#include "base/platform_file.h"
#include "url/gurl.h"
#include "webkit/browser/webkit_storage_browser_export.h"
#include "webkit/common/fileapi/file_system_types.h"

namespace fileapi {

// A class representing a filesystem URL which consists of origin URL,
// type and an internal path used inside the filesystem.
//
// When a FileSystemURL instance is created for a GURL (for filesystem: scheme),
// each accessor method would return following values:
//
// Example: For a URL 'filesystem:http://foo.com/temporary/foo/bar':
//   origin() returns 'http://foo.com',
//   mount_type() returns kFileSystemTypeTemporary,
//   virtual_path() returns 'foo/bar',
//   type() returns the same value as mount_type(),
//   path() returns the same value as virtual_path(),
//
// All other accessors return empty or invalid value.
//
// FileSystemURL can also be created to represent a 'cracked' filesystem URL if
// the original URL's type/path is pointing to a mount point which can be
// further resolved to a lower filesystem type/path.
//
// Example: Assume a path '/media/removable' is mounted at mount name
// 'mount_name' with type kFileSystemTypeFoo as an external file system.
//
// The original URL would look like:
//     'filesystem:http://bar.com/external/mount_name/foo/bar':
//
// FileSystemURL('http://bar.com',
//               kFileSystemTypeExternal,
//               'mount_name/foo/bar'
//               'mount_name',
//               kFileSystemTypeFoo,
//               '/media/removable/foo/bar');
// would create a FileSystemURL whose accessors return:
//
//   origin() returns 'http://bar.com',
//   mount_type() returns kFileSystemTypeExternal,
//   virtual_path() returns 'mount_name/foo/bar',
//   type() returns the kFileSystemTypeFoo,
//   path() returns '/media/removable/foo/bar',
//
// Note that in either case virtual_path() always returns the path part after
// 'type' part in the original URL, and mount_type() always returns the 'type'
// part in the original URL.
//
// Additionally, following accessors would return valid values:
//   filesystem_id() returns 'mount_name'.
//
// It is impossible to directly create a valid FileSystemURL instance (except by
// using CreatedForTest methods, which should not be used in production code).
// To get a valid FileSystemURL, one of the following methods can be used:
// <Friend>::CrackURL, <Friend>::CreateCrackedFileSystemURL, where <Friend> is
// one of the friended classes.
//
// TODO(ericu): Look into making virtual_path() [and all FileSystem API virtual
// paths] just an std::string, to prevent platform-specific base::FilePath
// behavior from getting invoked by accident. Currently the base::FilePath
// returned here needs special treatment, as it may contain paths that are
// illegal on the current platform.
// To avoid problems, use VirtualPath::BaseName and
// VirtualPath::GetComponents instead of the base::FilePath methods.
class WEBKIT_STORAGE_BROWSER_EXPORT FileSystemURL {
 public:
  FileSystemURL();
  ~FileSystemURL();

  // Methods for creating FileSystemURL without attempting to crack them.
  // Should be used only in tests.
  static FileSystemURL CreateForTest(const GURL& url);
  static FileSystemURL CreateForTest(const GURL& origin,
                                     FileSystemType mount_type,
                                     const base::FilePath& virtual_path);

  // Parses filesystem scheme |url| into uncracked FileSystemURL components.
  static bool ParseFileSystemSchemeURL(const GURL& url,
                                       GURL* origin,
                                       FileSystemType* mount_type,
                                       base::FilePath* virtual_path);

  // Returns true if this instance represents a valid FileSystem URL.
  bool is_valid() const { return is_valid_; }

  // Returns the origin part of this URL. See the class comment for details.
  const GURL& origin() const { return origin_; }

  // Returns the type part of this URL. See the class comment for details.
  FileSystemType type() const { return type_; }

  // Returns the cracked path of this URL. See the class comment for details.
  const base::FilePath& path() const { return path_; }

  // Returns the original path part of this URL.
  // See the class comment for details.
  // TODO(kinuko): this must return std::string.
  const base::FilePath& virtual_path() const { return virtual_path_; }

  // Returns the filesystem ID/mount name for isolated/external filesystem URLs.
  // See the class comment for details.
  const std::string& filesystem_id() const { return filesystem_id_; }
  const std::string& mount_filesystem_id() const {
    return mount_filesystem_id_;
  }

  FileSystemType mount_type() const { return mount_type_; }

  // Returns the formatted URL of this instance.
  GURL ToGURL() const;

  std::string DebugString() const;

  // Returns true if this URL is a strict parent of the |child|.
  bool IsParent(const FileSystemURL& child) const;

  bool IsInSameFileSystem(const FileSystemURL& other) const;

  bool operator==(const FileSystemURL& that) const;

  bool operator!=(const FileSystemURL& that) const {
    return !(*this == that);
  }

  struct WEBKIT_STORAGE_BROWSER_EXPORT Comparator {
    bool operator() (const FileSystemURL& lhs, const FileSystemURL& rhs) const;
  };

 private:
  friend class FileSystemContext;
  friend class ExternalMountPoints;
  friend class IsolatedContext;

  explicit FileSystemURL(const GURL& filesystem_url);
  FileSystemURL(const GURL& origin,
                FileSystemType mount_type,
                const base::FilePath& virtual_path);
  // Creates a cracked FileSystemURL.
  FileSystemURL(const GURL& origin,
                FileSystemType mount_type,
                const base::FilePath& virtual_path,
                const std::string& mount_filesystem_id,
                FileSystemType cracked_type,
                const base::FilePath& cracked_path,
                const std::string& filesystem_id);

  bool is_valid_;

  // Values parsed from the original URL.
  GURL origin_;
  FileSystemType mount_type_;
  base::FilePath virtual_path_;

  // Values obtained by cracking URLs.
  // |mount_filesystem_id_| is retrieved from the first round of cracking,
  // and the rest of the fields are from recursive cracking. Permission
  // checking on the top-level mount information should be done with the former,
  // and the low-level file operation should be implemented with the latter.
  std::string mount_filesystem_id_;
  FileSystemType type_;
  base::FilePath path_;
  std::string filesystem_id_;
};

typedef std::set<FileSystemURL, FileSystemURL::Comparator> FileSystemURLSet;

}  // namespace fileapi

#endif  // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_