1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
10 #include "base/callback_forward.h"
11 #include "base/files/file_path.h"
12 #include "chrome/browser/chromeos/drive/file_errors.h"
23 class DriveAppRegistry
;
24 class DriveServiceInterface
;
25 class FileSystemInterface
;
30 // "drive" diretory's local ID is fixed to this value.
31 const char kDriveGrandRootLocalId
[] = "<drive>";
33 // "drive/other" diretory's local ID is fixed to this value.
34 const char kDriveOtherDirLocalId
[] = "<other>";
36 // "drive/trash" diretory's local ID is fixed to this value.
37 const char kDriveTrashDirLocalId
[] = "<trash>";
39 // The directory names used for the Google Drive file system tree. These names
40 // are used in URLs for the file manager, hence user-visible.
41 const base::FilePath::CharType kDriveGrandRootDirName
[] =
42 FILE_PATH_LITERAL("drive");
44 const base::FilePath::CharType kDriveMyDriveRootDirName
[] =
45 FILE_PATH_LITERAL("root");
47 const base::FilePath::CharType kDriveOtherDirName
[] =
48 FILE_PATH_LITERAL("other");
50 const base::FilePath::CharType kDriveTrashDirName
[] =
51 FILE_PATH_LITERAL("trash");
53 // Returns the path of the top root of the pseudo tree.
54 const base::FilePath
& GetDriveGrandRootPath();
56 // Returns the path of the directory representing "My Drive".
57 const base::FilePath
& GetDriveMyDriveRootPath();
59 // Returns the Drive mount point path, which looks like "/special/drive-<hash>".
60 base::FilePath
GetDriveMountPointPath(Profile
* profile
);
62 // Returns the Drive mount point path, which looks like
63 // "/special/drive-<username_hash>", when provided with the |user_id_hash|.
64 base::FilePath
GetDriveMountPointPathForUserIdHash(std::string user_id_hash
);
66 // Returns the FileSystem for the |profile|. If not available (not mounted
67 // or disabled), returns NULL.
68 FileSystemInterface
* GetFileSystemByProfile(Profile
* profile
);
70 // Returns a FileSystemInterface instance for the |profile_id|, or NULL
71 // if the Profile for |profile_id| is destructed or Drive File System is
72 // disabled for the profile.
73 // Note: |profile_id| should be the pointer of the Profile instance if it is
74 // alive. Considering timing issues due to task posting across threads,
75 // this function can accept a dangling pointer as |profile_id| (and will return
76 // NULL for such a case).
77 // This function must be called on UI thread.
78 FileSystemInterface
* GetFileSystemByProfileId(void* profile_id
);
80 // Returns the DriveAppRegistry for the |profile|. If not available (not
81 // mounted or disabled), returns NULL.
82 DriveAppRegistry
* GetDriveAppRegistryByProfile(Profile
* profile
);
84 // Returns the DriveService for the |profile|. If not available (not mounted
85 // or disabled), returns NULL.
86 DriveServiceInterface
* GetDriveServiceByProfile(Profile
* profile
);
88 // Returns true if the given path is under the Drive mount point.
89 bool IsUnderDriveMountPoint(const base::FilePath
& path
);
91 // Extracts the Drive path from the given path located under the Drive mount
92 // point. Returns an empty path if |path| is not under the Drive mount point.
93 // Examples: ExtractDrivePath("/special/drive-xxx/foo.txt") => "drive/foo.txt"
94 base::FilePath
ExtractDrivePath(const base::FilePath
& path
);
96 // Extracts |profile| from the given paths located under
97 // GetDriveMountPointPath(profile). Returns NULL if it does not correspond to
98 // a valid mount point path. Must be called from UI thread.
99 Profile
* ExtractProfileFromPath(const base::FilePath
& path
);
101 // Extracts the Drive path (e.g., "drive/foo.txt") from the filesystem URL.
102 // Returns an empty path if |url| does not point under Drive mount point.
103 base::FilePath
ExtractDrivePathFromFileSystemUrl(
104 const storage::FileSystemURL
& url
);
106 // Escapes a file name in Drive cache.
107 // Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex)
108 std::string
EscapeCacheFileName(const std::string
& filename
);
110 // Unescapes a file path in Drive cache.
111 // This is the inverse of EscapeCacheFileName.
112 std::string
UnescapeCacheFileName(const std::string
& filename
);
114 // Converts the given string to a form suitable as a file name. Specifically,
115 // - Normalizes in Unicode Normalization Form C.
116 // - Replaces slashes '/' with '_'.
117 // - Replaces the whole input with "_" if the all input characters are '.'.
118 // |input| must be a valid UTF-8 encoded string.
119 std::string
NormalizeFileName(const std::string
& input
);
121 // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
123 base::FilePath
GetCacheRootPath(Profile
* profile
);
125 // Callback type for PrepareWritableFileAndRun.
126 typedef base::Callback
<void (FileError
, const base::FilePath
& path
)>
127 PrepareWritableFileCallback
;
129 // Invokes |callback| on blocking thread pool, after converting virtual |path|
130 // string like "/special/drive/foo.txt" to the concrete local cache file path.
131 // After |callback| returns, the written content is synchronized to the server.
133 // The |path| must be a path under Drive. Must be called from UI thread.
134 void PrepareWritableFileAndRun(Profile
* profile
,
135 const base::FilePath
& path
,
136 const PrepareWritableFileCallback
& callback
);
138 // Ensures the existence of |directory| of '/special/drive/foo'. This will
139 // create |directory| and its ancestors if they don't exist. |callback| is
140 // invoked after making sure that |directory| exists. |callback| should
141 // interpret error codes of either FILE_ERROR_OK or FILE_ERROR_EXISTS as
142 // indicating that |directory| now exists.
144 // If |directory| is not a Drive path, it won't check the existence and just
147 // Must be called from UI thread.
148 void EnsureDirectoryExists(Profile
* profile
,
149 const base::FilePath
& directory
,
150 const FileOperationCallback
& callback
);
152 // Does nothing with |error|. Used with functions taking FileOperationCallback.
153 void EmptyFileOperationCallback(FileError error
);
155 // Helper to destroy objects which needs Destroy() to be called on destruction.
156 struct DestroyHelper
{
158 void operator()(T
* object
) const {
164 // Creates a GDoc file with given values.
166 // GDoc files are used to represent hosted documents on local filesystems.
167 // A GDoc file contains a JSON whose content is a URL to view the document and
168 // a resource ID of the entry.
169 bool CreateGDocFile(const base::FilePath
& file_path
,
171 const std::string
& resource_id
);
173 // Reads URL from a GDoc file.
174 GURL
ReadUrlFromGDocFile(const base::FilePath
& file_path
);
176 // Reads resource ID from a GDoc file.
177 std::string
ReadResourceIdFromGDocFile(const base::FilePath
& file_path
);
179 // Returns true if Drive is enabled for the given Profile.
180 bool IsDriveEnabledForProfile(Profile
* profile
);
182 // Enum type for describing the current connection status to Drive.
183 enum ConnectionStatusType
{
184 // Disconnected because Drive service is unavailable for this account (either
185 // disabled by a flag or the account has no Google account (e.g., guests)).
186 DRIVE_DISCONNECTED_NOSERVICE
,
187 // Disconnected because no network is available.
188 DRIVE_DISCONNECTED_NONETWORK
,
189 // Disconnected because authentication is not ready.
190 DRIVE_DISCONNECTED_NOTREADY
,
191 // Connected by cellular network. Background sync is disabled.
192 DRIVE_CONNECTED_METERED
,
193 // Connected without condition (WiFi, Ethernet, or cellular with the
194 // disable-sync preference turned off.)
198 // Returns the Drive connection status for the |profile|.
199 ConnectionStatusType
GetDriveConnectionStatus(Profile
* profile
);
204 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_