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 true if the given path is under the Drive mount point. 89bool IsUnderDriveMountPoint(const base::FilePath& path); 90 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" 94base::FilePath ExtractDrivePath(const base::FilePath& path); 95 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. 99Profile* ExtractProfileFromPath(const base::FilePath& path); 100 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. 103base::FilePath ExtractDrivePathFromFileSystemUrl( 104 const storage::FileSystemURL& url); 105 106// Escapes a file name in Drive cache. 107// Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex) 108std::string EscapeCacheFileName(const std::string& filename); 109 110// Unescapes a file path in Drive cache. 111// This is the inverse of EscapeCacheFileName. 112std::string UnescapeCacheFileName(const std::string& filename); 113 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. 119std::string NormalizeFileName(const std::string& input); 120 121// Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the 122// profile. 123base::FilePath GetCacheRootPath(Profile* profile); 124 125// Callback type for PrepareWritableFileAndRun. 126typedef base::Callback<void (FileError, const base::FilePath& path)> 127 PrepareWritableFileCallback; 128 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. 132// 133// The |path| must be a path under Drive. Must be called from UI thread. 134void PrepareWritableFileAndRun(Profile* profile, 135 const base::FilePath& path, 136 const PrepareWritableFileCallback& callback); 137 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. 143// 144// If |directory| is not a Drive path, it won't check the existence and just 145// runs |callback|. 146// 147// Must be called from UI thread. 148void EnsureDirectoryExists(Profile* profile, 149 const base::FilePath& directory, 150 const FileOperationCallback& callback); 151 152// Does nothing with |error|. Used with functions taking FileOperationCallback. 153void EmptyFileOperationCallback(FileError error); 154 155// Helper to destroy objects which needs Destroy() to be called on destruction. 156struct DestroyHelper { 157 template<typename T> 158 void operator()(T* object) const { 159 if (object) 160 object->Destroy(); 161 } 162}; 163 164// Creates a GDoc file with given values. 165// 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. 169bool CreateGDocFile(const base::FilePath& file_path, 170 const GURL& url, 171 const std::string& resource_id); 172 173// Reads URL from a GDoc file. 174GURL ReadUrlFromGDocFile(const base::FilePath& file_path); 175 176// Reads resource ID from a GDoc file. 177std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path); 178 179// Returns true if Drive is enabled for the given Profile. 180bool IsDriveEnabledForProfile(Profile* profile); 181 182// Enum type for describing the current connection status to Drive. 183enum 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.) 195 DRIVE_CONNECTED, 196}; 197 198// Returns the Drive connection status for the |profile|. 199ConnectionStatusType GetDriveConnectionStatus(Profile* profile); 200 201} // namespace util 202} // namespace drive 203 204#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_ 205