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_DRIVE_FILE_SYSTEM_UTIL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_SYSTEM_UTIL_H_ |
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_SYSTEM_UTIL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_SYSTEM_UTIL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
15 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" | 15 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" |
16 #include "chrome/browser/google_apis/gdata_errorcode.h" | 16 #include "chrome/browser/google_apis/gdata_errorcode.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 | 18 |
| 19 class Profile; |
| 20 |
| 21 namespace base { |
19 class FilePath; | 22 class FilePath; |
20 class Profile; | 23 } |
21 | 24 |
22 namespace drive { | 25 namespace drive { |
23 | 26 |
24 class PlatformFileInfoProto; | 27 class PlatformFileInfoProto; |
25 | 28 |
26 namespace util { | 29 namespace util { |
27 | 30 |
28 // Path constants. | 31 // Path constants. |
29 | 32 |
30 // The extension for dirty files. The file names look like | 33 // The extension for dirty files. The file names look like |
31 // "<resource-id>.local". | 34 // "<resource-id>.local". |
32 const char kLocallyModifiedFileExtension[] = "local"; | 35 const char kLocallyModifiedFileExtension[] = "local"; |
33 // The extension for mounted files. The file names look like | 36 // The extension for mounted files. The file names look like |
34 // "<resource-id>.<md5>.mounted". | 37 // "<resource-id>.<md5>.mounted". |
35 const char kMountedArchiveFileExtension[] = "mounted"; | 38 const char kMountedArchiveFileExtension[] = "mounted"; |
36 const char kWildCard[] = "*"; | 39 const char kWildCard[] = "*"; |
37 // The path is used for creating a symlink in "pinned" directory for a file | 40 // The path is used for creating a symlink in "pinned" directory for a file |
38 // which is not yet fetched. | 41 // which is not yet fetched. |
39 const char kSymLinkToDevNull[] = "/dev/null"; | 42 const char kSymLinkToDevNull[] = "/dev/null"; |
40 | 43 |
41 // Returns the Drive mount point path, which looks like "/special/drive". | 44 // Returns the Drive mount point path, which looks like "/special/drive". |
42 const FilePath& GetDriveMountPointPath(); | 45 const base::FilePath& GetDriveMountPointPath(); |
43 | 46 |
44 // Returns the Drive mount path as string. | 47 // Returns the Drive mount path as string. |
45 const std::string& GetDriveMountPointPathAsString(); | 48 const std::string& GetDriveMountPointPathAsString(); |
46 | 49 |
47 // Returns the 'local' root of remote file system as "/special". | 50 // Returns the 'local' root of remote file system as "/special". |
48 const FilePath& GetSpecialRemoteRootPath(); | 51 const base::FilePath& GetSpecialRemoteRootPath(); |
49 | 52 |
50 // Returns the gdata file resource url formatted as | 53 // Returns the gdata file resource url formatted as |
51 // chrome://drive/<resource_id>/<file_name>. | 54 // chrome://drive/<resource_id>/<file_name>. |
52 GURL GetFileResourceUrl(const std::string& resource_id, | 55 GURL GetFileResourceUrl(const std::string& resource_id, |
53 const std::string& file_name); | 56 const std::string& file_name); |
54 | 57 |
55 // Given a profile and a drive_cache_path, return the file resource url. | 58 // Given a profile and a drive_cache_path, return the file resource url. |
56 void ModifyDriveFileResourceUrl(Profile* profile, | 59 void ModifyDriveFileResourceUrl(Profile* profile, |
57 const FilePath& drive_cache_path, | 60 const base::FilePath& drive_cache_path, |
58 GURL* url); | 61 GURL* url); |
59 | 62 |
60 // Returns true if the given path is under the Drive mount point. | 63 // Returns true if the given path is under the Drive mount point. |
61 bool IsUnderDriveMountPoint(const FilePath& path); | 64 bool IsUnderDriveMountPoint(const base::FilePath& path); |
62 | 65 |
63 // Extracts the Drive path from the given path located under the Drive mount | 66 // Extracts the Drive path from the given path located under the Drive mount |
64 // point. Returns an empty path if |path| is not under the Drive mount point. | 67 // point. Returns an empty path if |path| is not under the Drive mount point. |
65 // Examples: ExtractGDatPath("/special/drive/foo.txt") => "drive/foo.txt" | 68 // Examples: ExtractGDatPath("/special/drive/foo.txt") => "drive/foo.txt" |
66 FilePath ExtractDrivePath(const FilePath& path); | 69 base::FilePath ExtractDrivePath(const base::FilePath& path); |
67 | 70 |
68 // Escapes a file name in Drive cache. | 71 // Escapes a file name in Drive cache. |
69 // Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex) | 72 // Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex) |
70 std::string EscapeCacheFileName(const std::string& filename); | 73 std::string EscapeCacheFileName(const std::string& filename); |
71 | 74 |
72 // Unescapes a file path in Drive cache. | 75 // Unescapes a file path in Drive cache. |
73 // This is the inverse of EscapeCacheFileName. | 76 // This is the inverse of EscapeCacheFileName. |
74 std::string UnescapeCacheFileName(const std::string& filename); | 77 std::string UnescapeCacheFileName(const std::string& filename); |
75 | 78 |
76 // Escapes forward slashes from file names with magic unicode character | 79 // Escapes forward slashes from file names with magic unicode character |
77 // \u2215 pretty much looks the same in UI. | 80 // \u2215 pretty much looks the same in UI. |
78 std::string EscapeUtf8FileName(const std::string& input); | 81 std::string EscapeUtf8FileName(const std::string& input); |
79 | 82 |
80 // Extracts resource_id out of edit url. | 83 // Extracts resource_id out of edit url. |
81 std::string ExtractResourceIdFromUrl(const GURL& url); | 84 std::string ExtractResourceIdFromUrl(const GURL& url); |
82 | 85 |
83 // Extracts resource_id, md5, and extra_extension from cache path. | 86 // Extracts resource_id, md5, and extra_extension from cache path. |
84 // Case 1: Pinned and outgoing symlinks only have resource_id. | 87 // Case 1: Pinned and outgoing symlinks only have resource_id. |
85 // Example: path="/user/GCache/v1/pinned/pdf:a1b2" => | 88 // Example: path="/user/GCache/v1/pinned/pdf:a1b2" => |
86 // resource_id="pdf:a1b2", md5="", extra_extension=""; | 89 // resource_id="pdf:a1b2", md5="", extra_extension=""; |
87 // Case 2: Normal files have both resource_id and md5. | 90 // Case 2: Normal files have both resource_id and md5. |
88 // Example: path="/user/GCache/v1/tmp/pdf:a1b2.01234567" => | 91 // Example: path="/user/GCache/v1/tmp/pdf:a1b2.01234567" => |
89 // resource_id="pdf:a1b2", md5="01234567", extra_extension=""; | 92 // resource_id="pdf:a1b2", md5="01234567", extra_extension=""; |
90 // Case 3: Mounted files have all three parts. | 93 // Case 3: Mounted files have all three parts. |
91 // Example: path="/user/GCache/v1/persistent/pdf:a1b2.01234567.mounted" => | 94 // Example: path="/user/GCache/v1/persistent/pdf:a1b2.01234567.mounted" => |
92 // resource_id="pdf:a1b2", md5="01234567", extra_extension="mounted". | 95 // resource_id="pdf:a1b2", md5="01234567", extra_extension="mounted". |
93 void ParseCacheFilePath(const FilePath& path, | 96 void ParseCacheFilePath(const base::FilePath& path, |
94 std::string* resource_id, | 97 std::string* resource_id, |
95 std::string* md5, | 98 std::string* md5, |
96 std::string* extra_extension); | 99 std::string* extra_extension); |
97 | 100 |
98 // Callback type for PrepareWritableFilePathAndRun. | 101 // Callback type for PrepareWritablebase::FilePathAndRun. |
99 typedef base::Callback<void (DriveFileError, const FilePath& path)> | 102 typedef base::Callback<void (DriveFileError, const base::FilePath& path)> |
100 OpenFileCallback; | 103 OpenFileCallback; |
101 | 104 |
102 // Invokes |callback| on blocking thread pool, after converting virtual |path| | 105 // Invokes |callback| on blocking thread pool, after converting virtual |path| |
103 // string like "/special/drive/foo.txt" to the concrete local cache file path. | 106 // string like "/special/drive/foo.txt" to the concrete local cache file path. |
104 // After |callback| returns, the written content is synchronized to the server. | 107 // After |callback| returns, the written content is synchronized to the server. |
105 // | 108 // |
106 // If |path| is not a Drive path, it is regarded as a local path and no path | 109 // If |path| is not a Drive path, it is regarded as a local path and no path |
107 // conversion takes place. | 110 // conversion takes place. |
108 // | 111 // |
109 // Must be called from UI thread. | 112 // Must be called from UI thread. |
110 void PrepareWritableFileAndRun(Profile* profile, | 113 void PrepareWritableFileAndRun(Profile* profile, |
111 const FilePath& path, | 114 const base::FilePath& path, |
112 const OpenFileCallback& callback); | 115 const OpenFileCallback& callback); |
113 | 116 |
114 // Ensures the existence of |directory| of '/special/drive/foo'. This will | 117 // Ensures the existence of |directory| of '/special/drive/foo'. This will |
115 // create |directory| and its ancestors if they don't exist. |callback| is | 118 // create |directory| and its ancestors if they don't exist. |callback| is |
116 // invoked after making sure that |directory| exists. |callback| should | 119 // invoked after making sure that |directory| exists. |callback| should |
117 // interpret error codes of either DRIVE_FILE_OK or DRIVE_FILE_ERROR_EXISTS as | 120 // interpret error codes of either DRIVE_FILE_OK or DRIVE_FILE_ERROR_EXISTS as |
118 // indicating that |directory| now exists. | 121 // indicating that |directory| now exists. |
119 // | 122 // |
120 // If |directory| is not a Drive path, it won't check the existence and just | 123 // If |directory| is not a Drive path, it won't check the existence and just |
121 // runs |callback|. | 124 // runs |callback|. |
122 // | 125 // |
123 // Must be called from UI/IO thread. | 126 // Must be called from UI/IO thread. |
124 void EnsureDirectoryExists(Profile* profile, | 127 void EnsureDirectoryExists(Profile* profile, |
125 const FilePath& directory, | 128 const base::FilePath& directory, |
126 const FileOperationCallback& callback); | 129 const FileOperationCallback& callback); |
127 | 130 |
128 // Converts GData error code into file platform error code. | 131 // Converts GData error code into file platform error code. |
129 DriveFileError GDataToDriveFileError(google_apis::GDataErrorCode status); | 132 DriveFileError GDataToDriveFileError(google_apis::GDataErrorCode status); |
130 | 133 |
131 // Converts the proto representation to the platform file. | 134 // Converts the proto representation to the platform file. |
132 void ConvertProtoToPlatformFileInfo(const PlatformFileInfoProto& proto, | 135 void ConvertProtoToPlatformFileInfo(const PlatformFileInfoProto& proto, |
133 base::PlatformFileInfo* file_info); | 136 base::PlatformFileInfo* file_info); |
134 | 137 |
135 // Converts the platform file info to the proto representation. | 138 // Converts the platform file info to the proto representation. |
136 void ConvertPlatformFileInfoToProto(const base::PlatformFileInfo& file_info, | 139 void ConvertPlatformFileInfoToProto(const base::PlatformFileInfo& file_info, |
137 PlatformFileInfoProto* proto); | 140 PlatformFileInfoProto* proto); |
138 } // namespace util | 141 } // namespace util |
139 } // namespace drive | 142 } // namespace drive |
140 | 143 |
141 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_SYSTEM_UTIL_H_ | 144 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_SYSTEM_UTIL_H_ |
OLD | NEW |