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