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.
4//
5// This file provides File API related utilities.
6
7#ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
8#define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
9
10#include <string>
11
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"
16#include "url/gurl.h"
17
18class Profile;
19
20namespace content {
21class RenderViewHost;
22}
23
24namespace storage {
25class FileSystemContext;
26}
27
28namespace file_manager {
29namespace util {
30
31// Structure information necessary to create a EntryDefinition, and therefore
32// an Entry object on the JavaScript side.
33struct FileDefinition {
34  base::FilePath virtual_path;
35  base::FilePath absolute_path;
36  bool is_directory;
37};
38
39// Contains all information needed to create an Entry object in custom bindings.
40struct EntryDefinition {
41  EntryDefinition();
42  ~EntryDefinition();
43
44  std::string file_system_root_url;  // Used to create DOMFileSystem.
45  std::string file_system_name;      // Value of DOMFileSystem.name.
46  base::FilePath full_path;    // Value of Entry.fullPath.
47  // Whether to create FileEntry or DirectoryEntry when the corresponding entry
48  // is not found.
49  bool is_directory;
50  base::File::Error error;
51};
52
53typedef std::vector<FileDefinition> FileDefinitionList;
54typedef std::vector<EntryDefinition> EntryDefinitionList;
55
56// The callback used by ConvertFileDefinitionToEntryDefinition. Returns the
57// result of the conversion.
58typedef base::Callback<void(const EntryDefinition& entry_definition)>
59    EntryDefinitionCallback;
60
61// The callback used by ConvertFileDefinitionListToEntryDefinitionList. Returns
62// the result of the conversion as a list.
63typedef base::Callback<void(scoped_ptr<
64    EntryDefinitionList> entry_definition_list)> EntryDefinitionListCallback;
65
66// Returns a file system context associated with the given profile and the
67// extension ID.
68storage::FileSystemContext* GetFileSystemContextForExtensionId(
69    Profile* profile,
70    const std::string& extension_id);
71
72// Returns a file system context associated with the given profile and the
73// render view host.
74storage::FileSystemContext* GetFileSystemContextForRenderViewHost(
75    Profile* profile,
76    content::RenderViewHost* render_view_host);
77
78// Converts DrivePath (e.g., "drive/root", which always starts with the fixed
79// "drive" directory) to a RelativeFileSystemPathrelative (e.g.,
80// "drive-xxx/root/foo". which starts from the "mount point" in the FileSystem
81// API that may be distinguished for each profile by the appended "xxx" part.)
82base::FilePath ConvertDrivePathToRelativeFileSystemPath(
83    Profile* profile,
84    const std::string& extension_id,
85    const base::FilePath& drive_path);
86
87// Converts DrivePath to FileSystem URL.
88// E.g., "drive/root" to filesystem://id/external/drive-xxx/root.
89GURL ConvertDrivePathToFileSystemUrl(Profile* profile,
90                                     const base::FilePath& drive_path,
91                                     const std::string& extension_id);
92
93// Converts AbsolutePath (e.g., "/special/drive-xxx/root" or
94// "/home/chronos/u-xxx/Downloads") into filesystem URL. Returns false
95// if |absolute_path| is not managed by the external filesystem provider.
96bool ConvertAbsoluteFilePathToFileSystemUrl(Profile* profile,
97                                            const base::FilePath& absolute_path,
98                                            const std::string& extension_id,
99                                            GURL* url);
100
101// Converts AbsolutePath into RelativeFileSystemPath (e.g.,
102// "/special/drive-xxx/root/foo" => "drive-xxx/root/foo".) Returns false if
103// |absolute_path| is not managed by the external filesystem provider.
104bool ConvertAbsoluteFilePathToRelativeFileSystemPath(
105    Profile* profile,
106    const std::string& extension_id,
107    const base::FilePath& absolute_path,
108    base::FilePath* relative_path);
109
110// Converts a file definition to a entry definition and returns the result
111// via a callback. |profile| cannot be null. Must be called on UI thread.
112void ConvertFileDefinitionToEntryDefinition(
113    Profile* profile,
114    const std::string& extension_id,
115    const FileDefinition& file_definition,
116    const EntryDefinitionCallback& callback);
117
118// Converts a list of file definitions into a list of entry definitions and
119// returns it via |callback|. The method is safe, |file_definition_list| is
120// copied internally. The output list has the same order of items and size as
121// the input vector. |profile| cannot be null. Must be called on UI thread.
122void ConvertFileDefinitionListToEntryDefinitionList(
123    Profile* profile,
124    const std::string& extension_id,
125    const FileDefinitionList& file_definition_list,
126    const EntryDefinitionListCallback& callback);
127
128// Checks if a directory exists at |url|.
129void CheckIfDirectoryExists(
130    scoped_refptr<storage::FileSystemContext> file_system_context,
131    const GURL& url,
132    const storage::FileSystemOperationRunner::StatusCallback& callback);
133
134}  // namespace util
135}  // namespace file_manager
136
137#endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILEAPI_UTIL_H_
138