• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 class Profile;
16 
17 namespace fileapi {
18 class FileSystemURL;
19 }
20 
21 namespace drive {
22 
23 class DriveAppRegistry;
24 class DriveServiceInterface;
25 class FileSystemInterface;
26 
27 
28 namespace util {
29 
30 // "drive" diretory's local ID is fixed to this value.
31 const char kDriveGrandRootLocalId[] = "<drive>";
32 
33 // "drive/other" diretory's local ID is fixed to this value.
34 const char kDriveOtherDirLocalId[] = "<other>";
35 
36 // "drive/trash" diretory's local ID is fixed to this value.
37 const 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.
41 const base::FilePath::CharType kDriveGrandRootDirName[] =
42     FILE_PATH_LITERAL("drive");
43 
44 const base::FilePath::CharType kDriveMyDriveRootDirName[] =
45     FILE_PATH_LITERAL("root");
46 
47 const base::FilePath::CharType kDriveOtherDirName[] =
48     FILE_PATH_LITERAL("other");
49 
50 const base::FilePath::CharType kDriveTrashDirName[] =
51     FILE_PATH_LITERAL("trash");
52 
53 // Returns the path of the top root of the pseudo tree.
54 const base::FilePath& GetDriveGrandRootPath();
55 
56 // Returns the path of the directory representing "My Drive".
57 const base::FilePath& GetDriveMyDriveRootPath();
58 
59 // Returns the Drive mount point path, which looks like "/special/drive-<hash>".
60 base::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|.
64 base::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.
68 FileSystemInterface* 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.
78 FileSystemInterface* GetFileSystemByProfileId(void* profile_id);
79 
80 // Returns the DriveAppRegistry for the |profile|. If not available (not
81 // mounted or disabled), returns NULL.
82 DriveAppRegistry* GetDriveAppRegistryByProfile(Profile* profile);
83 
84 // Returns the DriveService for the |profile|. If not available (not mounted
85 // or disabled), returns NULL.
86 DriveServiceInterface* GetDriveServiceByProfile(Profile* profile);
87 
88 // Returns the gdata file resource url formatted as "drive:<path>"
89 GURL FilePathToDriveURL(const base::FilePath& path);
90 
91 // Converts a drive: URL back to a path that can be passed to FileSystem.
92 base::FilePath DriveURLToFilePath(const GURL& url);
93 
94 // Overwrites |url| with a Drive URL when appropriate.
95 void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url);
96 
97 // Returns true if the given path is under the Drive mount point.
98 bool 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"
103 base::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.
108 Profile* 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.
112 base::FilePath ExtractDrivePathFromFileSystemUrl(
113     const fileapi::FileSystemURL& url);
114 
115 // Escapes a file name in Drive cache.
116 // Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex)
117 std::string EscapeCacheFileName(const std::string& filename);
118 
119 // Unescapes a file path in Drive cache.
120 // This is the inverse of EscapeCacheFileName.
121 std::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.
128 std::string NormalizeFileName(const std::string& input);
129 
130 // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
131 // profile.
132 base::FilePath GetCacheRootPath(Profile* profile);
133 
134 // Callback type for PrepareWritableFileAndRun.
135 typedef 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.
143 void 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.
157 void EnsureDirectoryExists(Profile* profile,
158                            const base::FilePath& directory,
159                            const FileOperationCallback& callback);
160 
161 // Does nothing with |error|. Used with functions taking FileOperationCallback.
162 void EmptyFileOperationCallback(FileError error);
163 
164 // Helper to destroy objects which needs Destroy() to be called on destruction.
165 struct DestroyHelper {
166   template<typename T>
operatorDestroyHelper167   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.
178 bool CreateGDocFile(const base::FilePath& file_path,
179                     const GURL& url,
180                     const std::string& resource_id);
181 
182 // Returns true if |file_path| has a GDoc file extension. (e.g. ".gdoc")
183 bool HasGDocFileExtension(const base::FilePath& file_path);
184 
185 // Reads URL from a GDoc file.
186 GURL ReadUrlFromGDocFile(const base::FilePath& file_path);
187 
188 // Reads resource ID from a GDoc file.
189 std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path);
190 
191 // Returns true if Drive is enabled for the given Profile.
192 bool IsDriveEnabledForProfile(Profile* profile);
193 
194 // Enum type for describing the current connection status to Drive.
195 enum ConnectionStatusType {
196   // Disconnected because Drive service is unavailable for this account (either
197   // disabled by a flag or the account has no Google account (e.g., guests)).
198   DRIVE_DISCONNECTED_NOSERVICE,
199   // Disconnected because no network is available.
200   DRIVE_DISCONNECTED_NONETWORK,
201   // Disconnected because authentication is not ready.
202   DRIVE_DISCONNECTED_NOTREADY,
203   // Connected by cellular network. Background sync is disabled.
204   DRIVE_CONNECTED_METERED,
205   // Connected without condition (WiFi, Ethernet, or cellular with the
206   // disable-sync preference turned off.)
207   DRIVE_CONNECTED,
208 };
209 
210 // Returns the Drive connection status for the |profile|.
211 ConnectionStatusType GetDriveConnectionStatus(Profile* profile);
212 
213 }  // namespace util
214 }  // namespace drive
215 
216 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
217