1ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
2ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// found in the LICENSE file.
4ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch//
5ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// This file provides File API related utilities.
6ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
9ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
10ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include <string>
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/callback_forward.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/file.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/file_path.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation_runner.h"
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "url/gurl.h"
17ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
18ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochclass Profile;
19ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
20ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace content {
21ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochclass RenderViewHost;
22ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
23ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
25ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochclass FileSystemContext;
26ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
27ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
28ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace file_manager {
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace util {
30ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Structure information necessary to create a EntryDefinition, and therefore
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// an Entry object on the JavaScript side.
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)struct FileDefinition {
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::FilePath virtual_path;
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::FilePath absolute_path;
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool is_directory;
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Contains all information needed to create an Entry object in custom bindings.
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)struct EntryDefinition {
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EntryDefinition();
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ~EntryDefinition();
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string file_system_root_url;  // Used to create DOMFileSystem.
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string file_system_name;      // Value of DOMFileSystem.name.
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::FilePath full_path;    // Value of Entry.fullPath.
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Whether to create FileEntry or DirectoryEntry when the corresponding entry
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // is not found.
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool is_directory;
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::File::Error error;
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef std::vector<FileDefinition> FileDefinitionList;
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef std::vector<EntryDefinition> EntryDefinitionList;
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The callback used by ConvertFileDefinitionToEntryDefinition. Returns the
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// result of the conversion.
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef base::Callback<void(const EntryDefinition& entry_definition)>
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EntryDefinitionCallback;
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The callback used by ConvertFileDefinitionListToEntryDefinitionList. Returns
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// the result of the conversion as a list.
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef base::Callback<void(scoped_ptr<
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    EntryDefinitionList> entry_definition_list)> EntryDefinitionListCallback;
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
66ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Returns a file system context associated with the given profile and the
67ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// extension ID.
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)storage::FileSystemContext* GetFileSystemContextForExtensionId(
69ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    Profile* profile,
70ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const std::string& extension_id);
71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
72ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// Returns a file system context associated with the given profile and the
73ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch// render view host.
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)storage::FileSystemContext* GetFileSystemContextForRenderViewHost(
75ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    Profile* profile,
76ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    content::RenderViewHost* render_view_host);
77ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Converts DrivePath (e.g., "drive/root", which always starts with the fixed
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// "drive" directory) to a RelativeFileSystemPathrelative (e.g.,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// "drive-xxx/root/foo". which starts from the "mount point" in the FileSystem
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// API that may be distinguished for each profile by the appended "xxx" part.)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::FilePath ConvertDrivePathToRelativeFileSystemPath(
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    Profile* profile,
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& extension_id,
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::FilePath& drive_path);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Converts DrivePath to FileSystem URL.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// E.g., "drive/root" to filesystem://id/external/drive-xxx/root.
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GURL ConvertDrivePathToFileSystemUrl(Profile* profile,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     const base::FilePath& drive_path,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     const std::string& extension_id);
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Converts AbsolutePath (e.g., "/special/drive-xxx/root" or
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// "/home/chronos/u-xxx/Downloads") into filesystem URL. Returns false
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// if |absolute_path| is not managed by the external filesystem provider.
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ConvertAbsoluteFilePathToFileSystemUrl(Profile* profile,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                            const base::FilePath& absolute_path,
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                            const std::string& extension_id,
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                            GURL* url);
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Converts AbsolutePath into RelativeFileSystemPath (e.g.,
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// "/special/drive-xxx/root/foo" => "drive-xxx/root/foo".) Returns false if
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// |absolute_path| is not managed by the external filesystem provider.
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    Profile* profile,
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& extension_id,
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const base::FilePath& absolute_path,
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::FilePath* relative_path);
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Converts a file definition to a entry definition and returns the result
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// via a callback. |profile| cannot be null. Must be called on UI thread.
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void ConvertFileDefinitionToEntryDefinition(
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Profile* profile,
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& extension_id,
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const FileDefinition& file_definition,
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const EntryDefinitionCallback& callback);
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Converts a list of file definitions into a list of entry definitions and
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// returns it via |callback|. The method is safe, |file_definition_list| is
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// copied internally. The output list has the same order of items and size as
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// the input vector. |profile| cannot be null. Must be called on UI thread.
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void ConvertFileDefinitionListToEntryDefinitionList(
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Profile* profile,
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& extension_id,
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const FileDefinitionList& file_definition_list,
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const EntryDefinitionListCallback& callback);
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
12846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Checks if a directory exists at |url|.
12946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void CheckIfDirectoryExists(
13003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    scoped_refptr<storage::FileSystemContext> file_system_context,
13146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    const GURL& url,
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const storage::FileSystemOperationRunner::StatusCallback& callback);
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace util
135ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}  // namespace file_manager
136ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
138