15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/file_path.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/storage_browser_export.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_mount_option.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_types.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A class representing a filesystem URL which consists of origin URL,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type and an internal path used inside the filesystem.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// When a FileSystemURL instance is created for a GURL (for filesystem: scheme),
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// each accessor method would return following values:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example: For a URL 'filesystem:http://foo.com/temporary/foo/bar':
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   origin() returns 'http://foo.com',
27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   mount_type() returns kFileSystemTypeTemporary,
28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   virtual_path() returns 'foo/bar',
29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   type() returns the same value as mount_type(),
30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   path() returns the same value as virtual_path(),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// All other accessors return empty or invalid value.
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FileSystemURL can also be created to represent a 'cracked' filesystem URL if
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the original URL's type/path is pointing to a mount point which can be
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// further resolved to a lower filesystem type/path.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example: Assume a path '/media/removable' is mounted at mount name
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 'mount_name' with type kFileSystemTypeFoo as an external file system.
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The original URL would look like:
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     'filesystem:http://bar.com/external/mount_name/foo/bar':
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FileSystemURL('http://bar.com',
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//               kFileSystemTypeExternal,
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//               'mount_name/foo/bar'
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//               'mount_name',
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//               kFileSystemTypeFoo,
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//               '/media/removable/foo/bar');
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// would create a FileSystemURL whose accessors return:
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   origin() returns 'http://bar.com',
53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   mount_type() returns kFileSystemTypeExternal,
54b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//   virtual_path() returns 'mount_name/foo/bar',
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   type() returns the kFileSystemTypeFoo,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   path() returns '/media/removable/foo/bar',
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Note that in either case virtual_path() always returns the path part after
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 'type' part in the original URL, and mount_type() always returns the 'type'
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// part in the original URL.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Additionally, following accessors would return valid values:
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   filesystem_id() returns 'mount_name'.
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// It is impossible to directly create a valid FileSystemURL instance (except by
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// using CreatedForTest methods, which should not be used in production code).
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// To get a valid FileSystemURL, one of the following methods can be used:
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// <Friend>::CrackURL, <Friend>::CreateCrackedFileSystemURL, where <Friend> is
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// one of the friended classes.
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
71b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// TODO(ericu): Look into making virtual_path() [and all FileSystem API virtual
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// paths] just an std::string, to prevent platform-specific base::FilePath
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// behavior from getting invoked by accident. Currently the base::FilePath
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// returned here needs special treatment, as it may contain paths that are
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// illegal on the current platform.
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// To avoid problems, use VirtualPath::BaseName and
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// VirtualPath::GetComponents instead of the base::FilePath methods.
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass STORAGE_EXPORT FileSystemURL {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileSystemURL();
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~FileSystemURL();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Methods for creating FileSystemURL without attempting to crack them.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Should be used only in tests.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static FileSystemURL CreateForTest(const GURL& url);
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static FileSystemURL CreateForTest(const GURL& origin,
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     FileSystemType mount_type,
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const base::FilePath& virtual_path);
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static FileSystemURL CreateForTest(const GURL& origin,
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     FileSystemType mount_type,
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     const base::FilePath& virtual_path,
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     const std::string& mount_filesystem_id,
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     FileSystemType cracked_type,
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     const base::FilePath& cracked_path,
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     const std::string& filesystem_id,
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     const FileSystemMountOption& mount_option);
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this instance represents a valid FileSystem URL.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_valid() const { return is_valid_; }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the origin part of this URL. See the class comment for details.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& origin() const { return origin_; }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type part of this URL. See the class comment for details.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileSystemType type() const { return type_; }
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Returns the cracked path of this URL. See the class comment for details.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath& path() const { return path_; }
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the original path part of this URL.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See the class comment for details.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(kinuko): this must return std::string.
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath& virtual_path() const { return virtual_path_; }
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the filesystem ID/mount name for isolated/external filesystem URLs.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See the class comment for details.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& filesystem_id() const { return filesystem_id_; }
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string& mount_filesystem_id() const {
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return mount_filesystem_id_;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileSystemType mount_type() const { return mount_type_; }
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
124a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const FileSystemMountOption& mount_option() const { return mount_option_; }
125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Returns the formatted URL of this instance.
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GURL ToGURL() const;
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string DebugString() const;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this URL is a strict parent of the |child|.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsParent(const FileSystemURL& child) const;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool IsInSameFileSystem(const FileSystemURL& other) const;
13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(const FileSystemURL& that) const;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
138d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool operator!=(const FileSystemURL& that) const {
139d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return !(*this == that);
140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  struct STORAGE_EXPORT Comparator {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool operator() (const FileSystemURL& lhs, const FileSystemURL& rhs) const;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class FileSystemContext;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class ExternalMountPoints;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class IsolatedContext;
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit FileSystemURL(const GURL& filesystem_url);
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileSystemURL(const GURL& origin,
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                FileSystemType mount_type,
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                const base::FilePath& virtual_path);
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a cracked FileSystemURL.
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileSystemURL(const GURL& origin,
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                FileSystemType mount_type,
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                const base::FilePath& virtual_path,
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                const std::string& mount_filesystem_id,
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                FileSystemType cracked_type,
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                const base::FilePath& cracked_path,
162a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                const std::string& filesystem_id,
163a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                const FileSystemMountOption& mount_option);
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool is_valid_;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Values parsed from the original URL.
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL origin_;
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileSystemType mount_type_;
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath virtual_path_;
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Values obtained by cracking URLs.
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |mount_filesystem_id_| is retrieved from the first round of cracking,
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and the rest of the fields are from recursive cracking. Permission
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // checking on the top-level mount information should be done with the former,
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and the low-level file operation should be implemented with the latter.
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string mount_filesystem_id_;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileSystemType type_;
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path_;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string filesystem_id_;
181a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  FileSystemMountOption mount_option_;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::set<FileSystemURL, FileSystemURL::Comparator> FileSystemURLSet;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
189