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 #include "chrome/browser/chromeos/drive/file_system_util.h" | 5 #include "chrome/browser/chromeos/drive/file_system_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 const char kSlash[] = "/"; | 64 const char kSlash[] = "/"; |
65 const char kEscapedSlash[] = "\xE2\x88\x95"; | 65 const char kEscapedSlash[] = "\xE2\x88\x95"; |
66 | 66 |
67 const base::FilePath& GetDriveMyDriveMountPointPath() { | 67 const base::FilePath& GetDriveMyDriveMountPointPath() { |
68 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mydrive_mount_path, | 68 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mydrive_mount_path, |
69 (kDriveMyDriveMountPointPath)); | 69 (kDriveMyDriveMountPointPath)); |
70 return drive_mydrive_mount_path; | 70 return drive_mydrive_mount_path; |
71 } | 71 } |
72 | 72 |
73 FileSystemInterface* GetFileSystem(Profile* profile) { | |
74 DriveIntegrationService* integration_service = | |
75 DriveIntegrationServiceFactory::GetForProfile(profile); | |
76 return integration_service ? integration_service->file_system() : NULL; | |
77 } | |
78 | |
79 std::string ReadStringFromGDocFile(const base::FilePath& file_path, | 73 std::string ReadStringFromGDocFile(const base::FilePath& file_path, |
80 const std::string& key) { | 74 const std::string& key) { |
81 const int64 kMaxGDocSize = 4096; | 75 const int64 kMaxGDocSize = 4096; |
82 int64 file_size = 0; | 76 int64 file_size = 0; |
83 if (!file_util::GetFileSize(file_path, &file_size) || | 77 if (!file_util::GetFileSize(file_path, &file_size) || |
84 file_size > kMaxGDocSize) { | 78 file_size > kMaxGDocSize) { |
85 DLOG(INFO) << "File too large to be a GDoc file " << file_path.value(); | 79 DLOG(INFO) << "File too large to be a GDoc file " << file_path.value(); |
86 return std::string(); | 80 return std::string(); |
87 } | 81 } |
88 | 82 |
(...skipping 24 matching lines...) Expand all Loading... |
113 base::FileEnumerator enumerator(directory_from, false, // not recursive | 107 base::FileEnumerator enumerator(directory_from, false, // not recursive |
114 base::FileEnumerator::FILES); | 108 base::FileEnumerator::FILES); |
115 for (base::FilePath file_from = enumerator.Next(); !file_from.empty(); | 109 for (base::FilePath file_from = enumerator.Next(); !file_from.empty(); |
116 file_from = enumerator.Next()) { | 110 file_from = enumerator.Next()) { |
117 const base::FilePath file_to = directory_to.Append(file_from.BaseName()); | 111 const base::FilePath file_to = directory_to.Append(file_from.BaseName()); |
118 if (!base::PathExists(file_to)) // Do not overwrite existing files. | 112 if (!base::PathExists(file_to)) // Do not overwrite existing files. |
119 base::Move(file_from, file_to); | 113 base::Move(file_from, file_to); |
120 } | 114 } |
121 } | 115 } |
122 | 116 |
| 117 // Returns DriveIntegrationService instance, if Drive is enabled. |
| 118 // Otherwise, NULL. |
| 119 DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) { |
| 120 // TODO(hidehiko): GetForProfile will return DriveIntegrationService |
| 121 // regardless of mounting state. Needs to check the mounting state |
| 122 // later. crbug.com/284972. |
| 123 return DriveIntegrationServiceFactory::GetForProfile(profile); |
| 124 } |
| 125 |
123 } // namespace | 126 } // namespace |
124 | 127 |
125 const base::FilePath& GetDriveGrandRootPath() { | 128 const base::FilePath& GetDriveGrandRootPath() { |
126 CR_DEFINE_STATIC_LOCAL(base::FilePath, grand_root_path, | 129 CR_DEFINE_STATIC_LOCAL(base::FilePath, grand_root_path, |
127 (util::kDriveGrandRootDirName)); | 130 (util::kDriveGrandRootDirName)); |
128 return grand_root_path; | 131 return grand_root_path; |
129 } | 132 } |
130 | 133 |
131 const base::FilePath& GetDriveMyDriveRootPath() { | 134 const base::FilePath& GetDriveMyDriveRootPath() { |
132 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_root_path, | 135 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_root_path, |
133 (util::kDriveMyDriveRootPath)); | 136 (util::kDriveMyDriveRootPath)); |
134 return drive_root_path; | 137 return drive_root_path; |
135 } | 138 } |
136 | 139 |
137 const base::FilePath& GetDriveMountPointPath() { | 140 const base::FilePath& GetDriveMountPointPath() { |
138 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mount_path, | 141 CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mount_path, |
139 (base::FilePath::FromUTF8Unsafe(kDriveMountPointPath))); | 142 (base::FilePath::FromUTF8Unsafe(kDriveMountPointPath))); |
140 return drive_mount_path; | 143 return drive_mount_path; |
141 } | 144 } |
142 | 145 |
| 146 FileSystemInterface* GetFileSystemByProfile(Profile* profile) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 148 |
| 149 DriveIntegrationService* integration_service = |
| 150 GetIntegrationServiceByProfile(profile); |
| 151 return integration_service ? integration_service->file_system() : NULL; |
| 152 } |
| 153 |
143 FileSystemInterface* GetFileSystemByProfileId(void* profile_id) { | 154 FileSystemInterface* GetFileSystemByProfileId(void* profile_id) { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 | 156 |
146 // |profile_id| needs to be checked with ProfileManager::IsValidProfile | 157 // |profile_id| needs to be checked with ProfileManager::IsValidProfile |
147 // before using it. | 158 // before using it. |
148 Profile* profile = reinterpret_cast<Profile*>(profile_id); | 159 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
149 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) | 160 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
150 return NULL; | 161 return NULL; |
151 return GetFileSystem(profile); | 162 return GetFileSystemByProfile(profile); |
| 163 } |
| 164 |
| 165 DriveAppRegistry* GetDriveAppRegistryByProfile(Profile* profile) { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 167 |
| 168 DriveIntegrationService* integration_service = |
| 169 GetIntegrationServiceByProfile(profile); |
| 170 return integration_service ? |
| 171 integration_service->drive_app_registry() : |
| 172 NULL; |
| 173 } |
| 174 |
| 175 DriveServiceInterface* GetDriveServiceByProfile(Profile* profile) { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 |
| 178 DriveIntegrationService* integration_service = |
| 179 GetIntegrationServiceByProfile(profile); |
| 180 return integration_service ? integration_service->drive_service() : NULL; |
152 } | 181 } |
153 | 182 |
154 bool IsSpecialResourceId(const std::string& resource_id) { | 183 bool IsSpecialResourceId(const std::string& resource_id) { |
155 return resource_id == kDriveGrandRootSpecialResourceId || | 184 return resource_id == kDriveGrandRootSpecialResourceId || |
156 resource_id == kDriveOtherDirSpecialResourceId; | 185 resource_id == kDriveOtherDirSpecialResourceId; |
157 } | 186 } |
158 | 187 |
159 ResourceEntry CreateMyDriveRootEntry(const std::string& root_resource_id) { | 188 ResourceEntry CreateMyDriveRootEntry(const std::string& root_resource_id) { |
160 ResourceEntry mydrive_root; | 189 ResourceEntry mydrive_root; |
161 mydrive_root.mutable_file_info()->set_is_directory(true); | 190 mydrive_root.mutable_file_info()->set_is_directory(true); |
(...skipping 23 matching lines...) Expand all Loading... |
185 url.path(), net::UnescapeRule::NORMAL); | 214 url.path(), net::UnescapeRule::NORMAL); |
186 return base::FilePath::FromUTF8Unsafe(path_string); | 215 return base::FilePath::FromUTF8Unsafe(path_string); |
187 } | 216 } |
188 | 217 |
189 void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url) { | 218 void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url) { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
191 | 220 |
192 if (!IsUnderDriveMountPoint(path)) | 221 if (!IsUnderDriveMountPoint(path)) |
193 return; | 222 return; |
194 | 223 |
195 FileSystemInterface* file_system = GetFileSystem(profile); | 224 FileSystemInterface* file_system = GetFileSystemByProfile(profile); |
196 if (!file_system) | 225 if (!file_system) |
197 return; | 226 return; |
198 | 227 |
199 *url = FilePathToDriveURL(util::ExtractDrivePath(path)); | 228 *url = FilePathToDriveURL(util::ExtractDrivePath(path)); |
200 } | 229 } |
201 | 230 |
202 bool IsUnderDriveMountPoint(const base::FilePath& path) { | 231 bool IsUnderDriveMountPoint(const base::FilePath& path) { |
203 return GetDriveMountPointPath() == path || | 232 return GetDriveMountPointPath() == path || |
204 GetDriveMountPointPath().IsParent(path); | 233 GetDriveMountPointPath().IsParent(path); |
205 } | 234 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 // Move all files inside "tmp" to "files". | 341 // Move all files inside "tmp" to "files". |
313 MoveAllFilesFromDirectory(tmp_directory, cache_file_directory); | 342 MoveAllFilesFromDirectory(tmp_directory, cache_file_directory); |
314 } | 343 } |
315 | 344 |
316 void PrepareWritableFileAndRun(Profile* profile, | 345 void PrepareWritableFileAndRun(Profile* profile, |
317 const base::FilePath& path, | 346 const base::FilePath& path, |
318 const PrepareWritableFileCallback& callback) { | 347 const PrepareWritableFileCallback& callback) { |
319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
320 DCHECK(!callback.is_null()); | 349 DCHECK(!callback.is_null()); |
321 | 350 |
322 FileSystemInterface* file_system = GetFileSystem(profile); | 351 FileSystemInterface* file_system = GetFileSystemByProfile(profile); |
323 if (!file_system || !IsUnderDriveMountPoint(path)) { | 352 if (!file_system || !IsUnderDriveMountPoint(path)) { |
324 content::BrowserThread::GetBlockingPool()->PostTask( | 353 content::BrowserThread::GetBlockingPool()->PostTask( |
325 FROM_HERE, base::Bind(callback, FILE_ERROR_FAILED, base::FilePath())); | 354 FROM_HERE, base::Bind(callback, FILE_ERROR_FAILED, base::FilePath())); |
326 return; | 355 return; |
327 } | 356 } |
328 | 357 |
329 WriteOnCacheFile(file_system, | 358 WriteOnCacheFile(file_system, |
330 ExtractDrivePath(path), | 359 ExtractDrivePath(path), |
331 std::string(), // mime_type | 360 std::string(), // mime_type |
332 callback); | 361 callback); |
333 } | 362 } |
334 | 363 |
335 void EnsureDirectoryExists(Profile* profile, | 364 void EnsureDirectoryExists(Profile* profile, |
336 const base::FilePath& directory, | 365 const base::FilePath& directory, |
337 const FileOperationCallback& callback) { | 366 const FileOperationCallback& callback) { |
338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
339 DCHECK(!callback.is_null()); | 368 DCHECK(!callback.is_null()); |
340 if (IsUnderDriveMountPoint(directory)) { | 369 if (IsUnderDriveMountPoint(directory)) { |
341 FileSystemInterface* file_system = GetFileSystem(profile); | 370 FileSystemInterface* file_system = GetFileSystemByProfile(profile); |
342 DCHECK(file_system); | 371 DCHECK(file_system); |
343 file_system->CreateDirectory( | 372 file_system->CreateDirectory( |
344 ExtractDrivePath(directory), | 373 ExtractDrivePath(directory), |
345 true /* is_exclusive */, | 374 true /* is_exclusive */, |
346 true /* is_recursive */, | 375 true /* is_recursive */, |
347 callback); | 376 callback); |
348 } else { | 377 } else { |
349 base::MessageLoopProxy::current()->PostTask( | 378 base::MessageLoopProxy::current()->PostTask( |
350 FROM_HERE, base::Bind(callback, FILE_ERROR_OK)); | 379 FROM_HERE, base::Bind(callback, FILE_ERROR_OK)); |
351 } | 380 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 // Disable Drive if preference is set. This can happen with commandline flag | 452 // Disable Drive if preference is set. This can happen with commandline flag |
424 // --disable-drive or enterprise policy, or with user settings. | 453 // --disable-drive or enterprise policy, or with user settings. |
425 if (profile->GetPrefs()->GetBoolean(prefs::kDisableDrive)) | 454 if (profile->GetPrefs()->GetBoolean(prefs::kDisableDrive)) |
426 return false; | 455 return false; |
427 | 456 |
428 return true; | 457 return true; |
429 } | 458 } |
430 | 459 |
431 } // namespace util | 460 } // namespace util |
432 } // namespace drive | 461 } // namespace drive |
OLD | NEW |