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