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