file_system_util.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
7
8#include <string>
9
10#include "base/callback_forward.h"
11#include "base/files/file_path.h"
12#include "chrome/browser/chromeos/drive/file_errors.h"
13#include "url/gurl.h"
14
15class Profile;
16
17namespace storage {
18class FileSystemURL;
19}
20
21namespace drive {
22
23class DriveAppRegistry;
24class DriveServiceInterface;
25class FileSystemInterface;
26
27
28namespace util {
29
30// "drive" diretory's local ID is fixed to this value.
31const char kDriveGrandRootLocalId[] = "<drive>";
32
33// "drive/other" diretory's local ID is fixed to this value.
34const char kDriveOtherDirLocalId[] = "<other>";
35
36// "drive/trash" diretory's local ID is fixed to this value.
37const char kDriveTrashDirLocalId[] = "<trash>";
38
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.
41const base::FilePath::CharType kDriveGrandRootDirName[] =
42    FILE_PATH_LITERAL("drive");
43
44const base::FilePath::CharType kDriveMyDriveRootDirName[] =
45    FILE_PATH_LITERAL("root");
46
47const base::FilePath::CharType kDriveOtherDirName[] =
48    FILE_PATH_LITERAL("other");
49
50const base::FilePath::CharType kDriveTrashDirName[] =
51    FILE_PATH_LITERAL("trash");
52
53// Returns the path of the top root of the pseudo tree.
54const base::FilePath& GetDriveGrandRootPath();
55
56// Returns the path of the directory representing "My Drive".
57const base::FilePath& GetDriveMyDriveRootPath();
58
59// Returns the Drive mount point path, which looks like "/special/drive-<hash>".
60base::FilePath GetDriveMountPointPath(Profile* profile);
61
62// Returns the Drive mount point path, which looks like
63// "/special/drive-<username_hash>", when provided with the |user_id_hash|.
64base::FilePath GetDriveMountPointPathForUserIdHash(std::string user_id_hash);
65
66// Returns the FileSystem for the |profile|. If not available (not mounted
67// or disabled), returns NULL.
68FileSystemInterface* GetFileSystemByProfile(Profile* profile);
69
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.
78FileSystemInterface* GetFileSystemByProfileId(void* profile_id);
79
80// Returns the DriveAppRegistry for the |profile|. If not available (not
81// mounted or disabled), returns NULL.
82DriveAppRegistry* GetDriveAppRegistryByProfile(Profile* profile);
83
84// Returns the DriveService for the |profile|. If not available (not mounted
85// or disabled), returns NULL.
86DriveServiceInterface* GetDriveServiceByProfile(Profile* profile);
87
88// Returns the gdata file resource url formatted as "drive:<path>"
89GURL FilePathToDriveURL(const base::FilePath& path);
90
91// Converts a drive: URL back to a path that can be passed to FileSystem.
92base::FilePath DriveURLToFilePath(const GURL& url);
93
94// Overwrites |url| with a Drive URL when appropriate.
95void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url);
96
97// Returns true if the given path is under the Drive mount point.
98bool IsUnderDriveMountPoint(const base::FilePath& path);
99
100// Extracts the Drive path from the given path located under the Drive mount
101// point. Returns an empty path if |path| is not under the Drive mount point.
102// Examples: ExtractDrivePath("/special/drive-xxx/foo.txt") => "drive/foo.txt"
103base::FilePath ExtractDrivePath(const base::FilePath& path);
104
105// Extracts |profile| from the given paths located under
106// GetDriveMountPointPath(profile). Returns NULL if it does not correspond to
107// a valid mount point path. Must be called from UI thread.
108Profile* ExtractProfileFromPath(const base::FilePath& path);
109
110// Extracts the Drive path (e.g., "drive/foo.txt") from the filesystem URL.
111// Returns an empty path if |url| does not point under Drive mount point.
112base::FilePath ExtractDrivePathFromFileSystemUrl(
113    const storage::FileSystemURL& url);
114
115// Escapes a file name in Drive cache.
116// Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex)
117std::string EscapeCacheFileName(const std::string& filename);
118
119// Unescapes a file path in Drive cache.
120// This is the inverse of EscapeCacheFileName.
121std::string UnescapeCacheFileName(const std::string& filename);
122
123// Converts the given string to a form suitable as a file name. Specifically,
124// - Normalizes in Unicode Normalization Form C.
125// - Replaces slashes '/' with '_'.
126// - Replaces the whole input with "_" if the all input characters are '.'.
127// |input| must be a valid UTF-8 encoded string.
128std::string NormalizeFileName(const std::string& input);
129
130// Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
131// profile.
132base::FilePath GetCacheRootPath(Profile* profile);
133
134// Callback type for PrepareWritableFileAndRun.
135typedef base::Callback<void (FileError, const base::FilePath& path)>
136    PrepareWritableFileCallback;
137
138// Invokes |callback| on blocking thread pool, after converting virtual |path|
139// string like "/special/drive/foo.txt" to the concrete local cache file path.
140// After |callback| returns, the written content is synchronized to the server.
141//
142// The |path| must be a path under Drive. Must be called from UI thread.
143void PrepareWritableFileAndRun(Profile* profile,
144                               const base::FilePath& path,
145                               const PrepareWritableFileCallback& callback);
146
147// Ensures the existence of |directory| of '/special/drive/foo'.  This will
148// create |directory| and its ancestors if they don't exist.  |callback| is
149// invoked after making sure that |directory| exists.  |callback| should
150// interpret error codes of either FILE_ERROR_OK or FILE_ERROR_EXISTS as
151// indicating that |directory| now exists.
152//
153// If |directory| is not a Drive path, it won't check the existence and just
154// runs |callback|.
155//
156// Must be called from UI thread.
157void EnsureDirectoryExists(Profile* profile,
158                           const base::FilePath& directory,
159                           const FileOperationCallback& callback);
160
161// Does nothing with |error|. Used with functions taking FileOperationCallback.
162void EmptyFileOperationCallback(FileError error);
163
164// Helper to destroy objects which needs Destroy() to be called on destruction.
165struct DestroyHelper {
166  template<typename T>
167  void operator()(T* object) const {
168    if (object)
169      object->Destroy();
170  }
171};
172
173// Creates a GDoc file with given values.
174//
175// GDoc files are used to represent hosted documents on local filesystems.
176// A GDoc file contains a JSON whose content is a URL to view the document and
177// a resource ID of the entry.
178bool CreateGDocFile(const base::FilePath& file_path,
179                    const GURL& url,
180                    const std::string& resource_id);
181
182// Reads URL from a GDoc file.
183GURL ReadUrlFromGDocFile(const base::FilePath& file_path);
184
185// Reads resource ID from a GDoc file.
186std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path);
187
188// Returns true if Drive is enabled for the given Profile.
189bool IsDriveEnabledForProfile(Profile* profile);
190
191// Enum type for describing the current connection status to Drive.
192enum ConnectionStatusType {
193  // Disconnected because Drive service is unavailable for this account (either
194  // disabled by a flag or the account has no Google account (e.g., guests)).
195  DRIVE_DISCONNECTED_NOSERVICE,
196  // Disconnected because no network is available.
197  DRIVE_DISCONNECTED_NONETWORK,
198  // Disconnected because authentication is not ready.
199  DRIVE_DISCONNECTED_NOTREADY,
200  // Connected by cellular network. Background sync is disabled.
201  DRIVE_CONNECTED_METERED,
202  // Connected without condition (WiFi, Ethernet, or cellular with the
203  // disable-sync preference turned off.)
204  DRIVE_CONNECTED,
205};
206
207// Returns the Drive connection status for the |profile|.
208ConnectionStatusType GetDriveConnectionStatus(Profile* profile);
209
210}  // namespace util
211}  // namespace drive
212
213#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
214