1 // Copyright 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.
5 // This file provides File API related utilities.
7 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
8 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
12 #include "base/callback_forward.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "storage/browser/fileapi/file_system_operation_runner.h"
21 struct FileChooserFileInfo
;
26 class FileSystemContext
;
30 struct SelectedFileInfo
;
33 namespace file_manager
{
36 // Structure information necessary to create a EntryDefinition, and therefore
37 // an Entry object on the JavaScript side.
38 struct FileDefinition
{
39 base::FilePath virtual_path
;
40 base::FilePath absolute_path
;
44 // Contains all information needed to create an Entry object in custom bindings.
45 struct EntryDefinition
{
49 std::string file_system_root_url
; // Used to create DOMFileSystem.
50 std::string file_system_name
; // Value of DOMFileSystem.name.
51 base::FilePath full_path
; // Value of Entry.fullPath.
52 // Whether to create FileEntry or DirectoryEntry when the corresponding entry
55 base::File::Error error
;
58 typedef std::vector
<FileDefinition
> FileDefinitionList
;
59 typedef std::vector
<EntryDefinition
> EntryDefinitionList
;
60 typedef std::vector
<ui::SelectedFileInfo
> SelectedFileInfoList
;
61 typedef std::vector
<content::FileChooserFileInfo
> FileChooserFileInfoList
;
63 // The callback used by ConvertFileDefinitionToEntryDefinition. Returns the
64 // result of the conversion.
65 typedef base::Callback
<void(const EntryDefinition
& entry_definition
)>
66 EntryDefinitionCallback
;
68 // The callback used by ConvertFileDefinitionListToEntryDefinitionList. Returns
69 // the result of the conversion as a list.
70 typedef base::Callback
<void(scoped_ptr
<
71 EntryDefinitionList
> entry_definition_list
)> EntryDefinitionListCallback
;
73 // The callback used by
74 // ConvertFileSelectedInfoListToFileChooserFileInfoList. Returns the result of
75 // the conversion as a list.
76 typedef base::Callback
<void(const FileChooserFileInfoList
&)>
77 FileChooserFileInfoListCallback
;
79 // Returns a file system context associated with the given profile and the
81 storage::FileSystemContext
* GetFileSystemContextForExtensionId(
83 const std::string
& extension_id
);
85 // Returns a file system context associated with the given profile and the
87 storage::FileSystemContext
* GetFileSystemContextForRenderViewHost(
89 content::RenderViewHost
* render_view_host
);
91 // Converts DrivePath (e.g., "drive/root", which always starts with the fixed
92 // "drive" directory) to a RelativeFileSystemPathrelative (e.g.,
93 // "drive-xxx/root/foo". which starts from the "mount point" in the FileSystem
94 // API that may be distinguished for each profile by the appended "xxx" part.)
95 base::FilePath
ConvertDrivePathToRelativeFileSystemPath(
97 const std::string
& extension_id
,
98 const base::FilePath
& drive_path
);
100 // Converts DrivePath to FileSystem URL.
101 // E.g., "drive/root" to filesystem://id/external/drive-xxx/root.
102 GURL
ConvertDrivePathToFileSystemUrl(Profile
* profile
,
103 const base::FilePath
& drive_path
,
104 const std::string
& extension_id
);
106 // Converts AbsolutePath (e.g., "/special/drive-xxx/root" or
107 // "/home/chronos/u-xxx/Downloads") into filesystem URL. Returns false
108 // if |absolute_path| is not managed by the external filesystem provider.
109 bool ConvertAbsoluteFilePathToFileSystemUrl(Profile
* profile
,
110 const base::FilePath
& absolute_path
,
111 const std::string
& extension_id
,
114 // Converts AbsolutePath into RelativeFileSystemPath (e.g.,
115 // "/special/drive-xxx/root/foo" => "drive-xxx/root/foo".) Returns false if
116 // |absolute_path| is not managed by the external filesystem provider.
117 bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
119 const std::string
& extension_id
,
120 const base::FilePath
& absolute_path
,
121 base::FilePath
* relative_path
);
123 // Converts a file definition to a entry definition and returns the result
124 // via a callback. |profile| cannot be null. Must be called on UI thread.
125 void ConvertFileDefinitionToEntryDefinition(
127 const std::string
& extension_id
,
128 const FileDefinition
& file_definition
,
129 const EntryDefinitionCallback
& callback
);
131 // Converts a list of file definitions into a list of entry definitions and
132 // returns it via |callback|. The method is safe, |file_definition_list| is
133 // copied internally. The output list has the same order of items and size as
134 // the input vector. |profile| cannot be null. Must be called on UI thread.
135 void ConvertFileDefinitionListToEntryDefinitionList(
137 const std::string
& extension_id
,
138 const FileDefinitionList
& file_definition_list
,
139 const EntryDefinitionListCallback
& callback
);
141 // Converts SelectedFileInfoList into FileChooserFileInfoList.
142 void ConvertSelectedFileInfoListToFileChooserFileInfoList(
143 storage::FileSystemContext
* context
,
145 const SelectedFileInfoList
& selected_info_list
,
146 const FileChooserFileInfoListCallback
& callback
);
148 // Checks if a directory exists at |directory_path| absolute path.
149 void CheckIfDirectoryExists(
150 scoped_refptr
<storage::FileSystemContext
> file_system_context
,
151 const base::FilePath
& directory_path
,
152 const storage::FileSystemOperationRunner::StatusCallback
& callback
);
154 // Get metadata for an entry at |entry_path| absolute path.
155 void GetMetadataForPath(
156 scoped_refptr
<storage::FileSystemContext
> file_system_context
,
157 const base::FilePath
& entry_path
,
158 const storage::FileSystemOperationRunner::GetMetadataCallback
& callback
);
160 // Obtains isolated file system URL from |virtual_path| pointing a file in the
161 // external file system.
162 storage::FileSystemURL
CreateIsolatedURLFromVirtualPath(
163 const storage::FileSystemContext
& context
,
165 const base::FilePath
& virtual_path
);
168 } // namespace file_manager
170 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_