Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(860)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_file_system.cc

Issue 9844006: GData downloads cleanup (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/gdata/gdata_file_system.h" 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 18 matching lines...) Expand all
29 #include "chrome/common/chrome_constants.h" 29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths_internal.h" 30 #include "chrome/common/chrome_paths_internal.h"
31 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
32 #include "net/base/mime_util.h" 32 #include "net/base/mime_util.h"
33 #include "webkit/fileapi/file_system_file_util_proxy.h" 33 #include "webkit/fileapi/file_system_file_util_proxy.h"
34 #include "webkit/fileapi/file_system_types.h" 34 #include "webkit/fileapi/file_system_types.h"
35 #include "webkit/fileapi/file_system_util.h" 35 #include "webkit/fileapi/file_system_util.h"
36 36
37 using content::BrowserThread; 37 using content::BrowserThread;
38 38
39 namespace gdata {
39 namespace { 40 namespace {
40 41
41 const char kMimeTypeJson[] = "application/json"; 42 const char kMimeTypeJson[] = "application/json";
42 const char kMimeTypeOctetStream[] = "application/octet-stream"; 43 const char kMimeTypeOctetStream[] = "application/octet-stream";
43 44
44 const FilePath::CharType kGDataRootDirectory[] = FILE_PATH_LITERAL("gdata"); 45 const FilePath::CharType kGDataRootDirectory[] = FILE_PATH_LITERAL("gdata");
45 const char kFeedField[] = "feed"; 46 const char kFeedField[] = "feed";
46 const char kWildCard[] = "*"; 47 const char kWildCard[] = "*";
47 const char kLocallyModifiedFileExtension[] = "local"; 48 const char kLocallyModifiedFileExtension[] = "local";
48 49
49 const FilePath::CharType kGDataCacheVersionDir[] = FILE_PATH_LITERAL("v1"); 50 const FilePath::CharType kGDataCacheVersionDir[] = FILE_PATH_LITERAL("v1");
50 const FilePath::CharType kGDataCacheMetaDir[] = FILE_PATH_LITERAL("meta"); 51 const FilePath::CharType kGDataCacheMetaDir[] = FILE_PATH_LITERAL("meta");
51 const FilePath::CharType kGDataCachePinnedDir[] = FILE_PATH_LITERAL("pinned"); 52 const FilePath::CharType kGDataCachePinnedDir[] = FILE_PATH_LITERAL("pinned");
52 const FilePath::CharType kGDataCacheOutgoingDir[] = 53 const FilePath::CharType kGDataCacheOutgoingDir[] =
53 FILE_PATH_LITERAL("outgoing"); 54 FILE_PATH_LITERAL("outgoing");
54 const FilePath::CharType kGDataCachePersistentDir[] = 55 const FilePath::CharType kGDataCachePersistentDir[] =
55 FILE_PATH_LITERAL("persistent"); 56 FILE_PATH_LITERAL("persistent");
56 const FilePath::CharType kGDataCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); 57 const FilePath::CharType kGDataCacheTmpDir[] = FILE_PATH_LITERAL("tmp");
57 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = 58 const FilePath::CharType kGDataCacheTmpDownloadsDir[] =
58 FILE_PATH_LITERAL("tmp/downloads"); 59 FILE_PATH_LITERAL("tmp/downloads");
59 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = 60 const FilePath::CharType kGDataCacheTmpDocumentsDir[] =
60 FILE_PATH_LITERAL("tmp/documents"); 61 FILE_PATH_LITERAL("tmp/documents");
61 const FilePath::CharType kLastFeedFile[] = FILE_PATH_LITERAL("last_feed.json"); 62 const FilePath::CharType kLastFeedFile[] = FILE_PATH_LITERAL("last_feed.json");
62 const char kGDataFileSystemToken[] = "GDataFileSystemToken"; 63 const char kGDataFileSystemToken[] = "GDataFileSystemToken";
63 const FilePath::CharType kAccountMetadataFile[] = 64 const FilePath::CharType kAccountMetadataFile[] =
64 FILE_PATH_LITERAL("account_metadata.json"); 65 FILE_PATH_LITERAL("account_metadata.json");
65 const FilePath::CharType kSymLinkToDevNull[] = FILE_PATH_LITERAL("/dev/null"); 66 const FilePath::CharType kSymLinkToDevNull[] = FILE_PATH_LITERAL("/dev/null");
66 67
67 // Converts gdata error code into file platform error code. 68 // Converts gdata error code into file platform error code.
68 base::PlatformFileError GDataToPlatformError(gdata::GDataErrorCode status) { 69 base::PlatformFileError GDataToPlatformError(GDataErrorCode status) {
69 switch (status) { 70 switch (status) {
70 case gdata::HTTP_SUCCESS: 71 case HTTP_SUCCESS:
71 case gdata::HTTP_CREATED: 72 case HTTP_CREATED:
72 return base::PLATFORM_FILE_OK; 73 return base::PLATFORM_FILE_OK;
73 case gdata::HTTP_UNAUTHORIZED: 74 case HTTP_UNAUTHORIZED:
74 case gdata::HTTP_FORBIDDEN: 75 case HTTP_FORBIDDEN:
75 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; 76 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
76 case gdata::HTTP_NOT_FOUND: 77 case HTTP_NOT_FOUND:
77 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 78 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
78 case gdata::GDATA_PARSE_ERROR: 79 case GDATA_PARSE_ERROR:
79 case gdata::GDATA_FILE_ERROR: 80 case GDATA_FILE_ERROR:
80 return base::PLATFORM_FILE_ERROR_ABORT; 81 return base::PLATFORM_FILE_ERROR_ABORT;
81 default: 82 default:
82 return base::PLATFORM_FILE_ERROR_FAILED; 83 return base::PLATFORM_FILE_ERROR_FAILED;
83 } 84 }
84 } 85 }
85 86
86 // Converts system error to file platform error code. 87 // Converts system error to file platform error code.
87 // This is copied and modified from base/platform_file_posix.cc. 88 // This is copied and modified from base/platform_file_posix.cc.
88 // TODO(kuan): base/platform.h should probably export this. 89 // TODO(kuan): base/platform.h should probably export this.
89 base::PlatformFileError SystemToPlatformError(int error) { 90 base::PlatformFileError SystemToPlatformError(int error) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 147
147 // Modifies cache state of file on IO thread pool, which involves: 148 // Modifies cache state of file on IO thread pool, which involves:
148 // - moving or copying file (per |file_operation_type|) from |source_path| to 149 // - moving or copying file (per |file_operation_type|) from |source_path| to
149 // |dest_path| if they're different 150 // |dest_path| if they're different
150 // - deleting symlink if |symlink_path| is not empty 151 // - deleting symlink if |symlink_path| is not empty
151 // - creating symlink if |symlink_path| is not empty and |create_symlink| is 152 // - creating symlink if |symlink_path| is not empty and |create_symlink| is
152 // true. 153 // true.
153 base::PlatformFileError ModifyCacheState( 154 base::PlatformFileError ModifyCacheState(
154 const FilePath& source_path, 155 const FilePath& source_path,
155 const FilePath& dest_path, 156 const FilePath& dest_path,
156 gdata::GDataFileSystem::FileOperationType file_operation_type, 157 GDataFileSystem::FileOperationType file_operation_type,
157 const FilePath& symlink_path, 158 const FilePath& symlink_path,
158 bool create_symlink) { 159 bool create_symlink) {
159 // Move or copy |source_path| to |dest_path| if they are different. 160 // Move or copy |source_path| to |dest_path| if they are different.
160 if (source_path != dest_path) { 161 if (source_path != dest_path) {
161 bool success = false; 162 bool success = false;
162 if (file_operation_type == gdata::GDataFileSystem::FILE_OPERATION_MOVE) 163 if (file_operation_type == GDataFileSystem::FILE_OPERATION_MOVE)
163 success = file_util::Move(source_path, dest_path); 164 success = file_util::Move(source_path, dest_path);
164 else if (file_operation_type == gdata::GDataFileSystem::FILE_OPERATION_COPY) 165 else if (file_operation_type ==
166 GDataFileSystem::FILE_OPERATION_COPY)
165 success = file_util::CopyFile(source_path, dest_path); 167 success = file_util::CopyFile(source_path, dest_path);
166 if (!success) { 168 if (!success) {
167 base::PlatformFileError error = SystemToPlatformError(errno); 169 base::PlatformFileError error = SystemToPlatformError(errno);
168 LOG(ERROR) << "Error " 170 LOG(ERROR) << "Error "
169 << (file_operation_type == 171 << (file_operation_type ==
170 gdata::GDataFileSystem::FILE_OPERATION_MOVE ? 172 GDataFileSystem::FILE_OPERATION_MOVE ?
171 "moving " : "copying ") 173 "moving " : "copying ")
172 << source_path.value() 174 << source_path.value()
173 << " to " << dest_path.value() 175 << " to " << dest_path.value()
174 << ": " << strerror(errno); 176 << ": " << strerror(errno);
175 return error; 177 return error;
176 } else { 178 } else {
177 DVLOG(1) << (file_operation_type == 179 DVLOG(1) << (file_operation_type ==
178 gdata::GDataFileSystem::FILE_OPERATION_MOVE ? 180 GDataFileSystem::FILE_OPERATION_MOVE ?
179 "Moved " : "Copied ") 181 "Moved " : "Copied ")
180 << source_path.value() 182 << source_path.value()
181 << " to " << dest_path.value(); 183 << " to " << dest_path.value();
182 } 184 }
183 } else { 185 } else {
184 DVLOG(1) << "No need to move file: source = destination"; 186 DVLOG(1) << "No need to move file: source = destination";
185 } 187 }
186 188
187 if (symlink_path.empty()) 189 if (symlink_path.empty())
188 return base::PLATFORM_FILE_OK; 190 return base::PLATFORM_FILE_OK;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 DVLOG(1) << "Error deleting " << current.value(); 272 DVLOG(1) << "Error deleting " << current.value();
271 else 273 else
272 DVLOG(1) << "Deleted " << current.value(); 274 DVLOG(1) << "Deleted " << current.value();
273 } 275 }
274 } 276 }
275 277
276 // Invoked upon completion of TransferRegularFile initiated by Copy. 278 // Invoked upon completion of TransferRegularFile initiated by Copy.
277 // 279 //
278 // |callback| is run on the thread represented by |relay_proxy|. 280 // |callback| is run on the thread represented by |relay_proxy|.
279 void OnTransferRegularFileCompleteForCopy( 281 void OnTransferRegularFileCompleteForCopy(
280 const gdata::FileOperationCallback& callback, 282 const FileOperationCallback& callback,
281 scoped_refptr<base::MessageLoopProxy> relay_proxy, 283 scoped_refptr<base::MessageLoopProxy> relay_proxy,
282 base::PlatformFileError error) { 284 base::PlatformFileError error) {
283 if (!callback.is_null()) 285 if (!callback.is_null())
284 relay_proxy->PostTask(FROM_HERE, base::Bind(callback, error)); 286 relay_proxy->PostTask(FROM_HERE, base::Bind(callback, error));
285 } 287 }
286 288
287 // Runs GetFileCallback with pointers dereferenced. 289 // Runs GetFileCallback with pointers dereferenced.
288 // Used for PostTaskAndReply(). 290 // Used for PostTaskAndReply().
289 void RunGetFileCallbackHelper(const gdata::GetFileCallback& callback, 291 void RunGetFileCallbackHelper(const GetFileCallback& callback,
290 base::PlatformFileError* error, 292 base::PlatformFileError* error,
291 FilePath* file_path, 293 FilePath* file_path,
292 std::string* mime_type, 294 std::string* mime_type,
293 gdata::GDataFileType* file_type) { 295 GDataFileType* file_type) {
294 DCHECK(error); 296 DCHECK(error);
295 DCHECK(file_path); 297 DCHECK(file_path);
296 DCHECK(mime_type); 298 DCHECK(mime_type);
297 DCHECK(file_type); 299 DCHECK(file_type);
298 300
299 if (!callback.is_null()) 301 if (!callback.is_null())
300 callback.Run(*error, *file_path, *mime_type, *file_type); 302 callback.Run(*error, *file_path, *mime_type, *file_type);
301 } 303 }
302 304
303 // Ditto for CacheOperationCallback. 305 // Ditto for CacheOperationCallback.
304 void RunCacheOperationCallbackHelper( 306 void RunCacheOperationCallbackHelper(
305 const gdata::CacheOperationCallback& callback, 307 const CacheOperationCallback& callback,
306 base::PlatformFileError* error, 308 base::PlatformFileError* error,
307 const std::string& resource_id, 309 const std::string& resource_id,
308 const std::string& md5) { 310 const std::string& md5) {
309 DCHECK(error); 311 DCHECK(error);
310 312
311 if (!callback.is_null()) 313 if (!callback.is_null())
312 callback.Run(*error, resource_id, md5); 314 callback.Run(*error, resource_id, md5);
313 } 315 }
314 316
315 // Ditto for GetFromCacheCallback. 317 // Ditto for GetFromCacheCallback.
316 void RunGetFromCacheCallbackHelper( 318 void RunGetFromCacheCallbackHelper(
317 const gdata::GetFromCacheCallback& callback, 319 const GetFromCacheCallback& callback,
318 base::PlatformFileError* error, 320 base::PlatformFileError* error,
319 const std::string& resource_id, 321 const std::string& resource_id,
320 const std::string& md5, 322 const std::string& md5,
321 const FilePath& gdata_file_path, 323 const FilePath& gdata_file_path,
322 FilePath* cache_file_path) { 324 FilePath* cache_file_path) {
323 DCHECK(error); 325 DCHECK(error);
324 DCHECK(cache_file_path); 326 DCHECK(cache_file_path);
325 327
326 if (!callback.is_null()) 328 if (!callback.is_null())
327 callback.Run(*error, resource_id, md5, gdata_file_path, *cache_file_path); 329 callback.Run(*error, resource_id, md5, gdata_file_path, *cache_file_path);
328 } 330 }
329 331
330 void RunGetCacheStateCallbackHelper( 332 void RunGetCacheStateCallbackHelper(
331 const gdata::GetCacheStateCallback& callback, 333 const GetCacheStateCallback& callback,
332 base::PlatformFileError* error, 334 base::PlatformFileError* error,
333 int* cache_state) { 335 int* cache_state) {
334 DCHECK(error); 336 DCHECK(error);
335 DCHECK(cache_state); 337 DCHECK(cache_state);
336 338
337 if (!callback.is_null()) 339 if (!callback.is_null())
338 callback.Run(*error, *cache_state); 340 callback.Run(*error, *cache_state);
339 } 341 }
340 342
341 } // namespace 343 } // namespace
342 344
343 namespace gdata {
344
345 // FindFileDelegate class implementation. 345 // FindFileDelegate class implementation.
346 346
347 FindFileDelegate::~FindFileDelegate() { 347 FindFileDelegate::~FindFileDelegate() {
348 } 348 }
349 349
350 // FindFileCallbackRelayDelegate class implementation. 350 // FindFileCallbackRelayDelegate class implementation.
351 // This class is used to relay calls between sync and async versions 351 // This class is used to relay calls between sync and async versions
352 // of FindFileByPath(Sync|Async) calls. 352 // of FindFileByPath(Sync|Async) calls.
353 class FindFileCallbackRelayDelegate : public FindFileDelegate { 353 class FindFileCallbackRelayDelegate : public FindFileDelegate {
354 public: 354 public:
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 if (*error != base::PLATFORM_FILE_OK) { 736 if (*error != base::PLATFORM_FILE_OK) {
737 if (!callback.is_null()) 737 if (!callback.is_null())
738 callback.Run(*error); 738 callback.Run(*error);
739 739
740 return; 740 return;
741 } 741 }
742 742
743 upload_file_info->completion_callback = 743 upload_file_info->completion_callback =
744 base::Bind(&GDataFileSystem::OnTransferCompleted, 744 base::Bind(&GDataFileSystem::OnTransferCompleted,
745 GetWeakPtrForCurrentThread(), 745 GetWeakPtrForCurrentThread(),
746 upload_file_info->file_path,
747 upload_file_info->gdata_path,
748 callback); 746 callback);
749 747
750 service->uploader()->UploadFile(upload_file_info); 748 service->uploader()->UploadFile(scoped_ptr<UploadFileInfo>(upload_file_info));
751 } 749 }
752 750
753 void GDataFileSystem::OnTransferCompleted( 751 void GDataFileSystem::OnTransferCompleted(
754 const FilePath& local_file_path,
755 const FilePath& remote_dest_file_path,
756 const FileOperationCallback& callback, 752 const FileOperationCallback& callback,
757 base::PlatformFileError error, 753 base::PlatformFileError error,
758 DocumentEntry* entry) { 754 UploadFileInfo* upload_file_info) {
Ben Chan 2012/03/28 05:29:36 nit: DCHECK(upload_file_info)
achuithb 2012/03/28 09:14:04 Done.
759 if (error == base::PLATFORM_FILE_OK && entry) { 755 if (error == base::PLATFORM_FILE_OK && upload_file_info->entry.get()) {
760 AddUploadedFile(remote_dest_file_path.DirName(), 756 AddUploadedFile(upload_file_info->gdata_path.DirName(),
761 entry, 757 upload_file_info->entry.get(),
762 local_file_path, 758 upload_file_info->file_path,
763 FILE_OPERATION_COPY); 759 FILE_OPERATION_COPY);
764 } 760 }
765 if (!callback.is_null()) 761 if (!callback.is_null())
766 callback.Run(error); 762 callback.Run(error);
763
764 GDataSystemService* service =
765 GDataSystemServiceFactory::GetForProfile(profile_);
766 if (service)
767 service->uploader()->DeleteUpload(upload_file_info);
767 } 768 }
768 769
769 // static. 770 // static.
770 void GDataFileSystem::CreateUploadFileInfoOnIOThreadPool( 771 void GDataFileSystem::CreateUploadFileInfoOnIOThreadPool(
771 const FilePath& local_file, 772 const FilePath& local_file,
772 const FilePath& remote_dest_file, 773 const FilePath& remote_dest_file,
773 base::PlatformFileError* error, 774 base::PlatformFileError* error,
774 UploadFileInfo* upload_file_info) { 775 UploadFileInfo* upload_file_info) {
775 DCHECK(error); 776 DCHECK(error);
776 DCHECK(upload_file_info); 777 DCHECK(upload_file_info);
(...skipping 1725 matching lines...) Expand 10 before | Expand all | Expand 10 after
2502 2503
2503 GDataDirectory* parent_dir = file->parent(); 2504 GDataDirectory* parent_dir = file->parent();
2504 if (!parent_dir->RemoveFile(file)) 2505 if (!parent_dir->RemoveFile(file))
2505 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 2506 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
2506 2507
2507 NotifyDirectoryChanged(parent_dir->GetFilePath()); 2508 NotifyDirectoryChanged(parent_dir->GetFilePath());
2508 return base::PLATFORM_FILE_OK; 2509 return base::PLATFORM_FILE_OK;
2509 } 2510 }
2510 2511
2511 void GDataFileSystem::AddUploadedFile(const FilePath& virtual_dir_path, 2512 void GDataFileSystem::AddUploadedFile(const FilePath& virtual_dir_path,
2512 gdata::DocumentEntry* entry, 2513 DocumentEntry* entry,
2513 const FilePath& file_content_path, 2514 const FilePath& file_content_path,
2514 FileOperationType cache_operation) { 2515 FileOperationType cache_operation) {
2515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2516 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2516 2517
2517 if (!entry) { 2518 if (!entry) {
2518 NOTREACHED(); 2519 NOTREACHED();
2519 return; 2520 return;
2520 } 2521 }
2521 2522
2522 std::string resource_id; 2523 std::string resource_id;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
2671 FILE_OPERATION_MOVE, 2672 FILE_OPERATION_MOVE,
2672 error, 2673 error,
2673 cache_file_path), 2674 cache_file_path),
2674 base::Bind(&RunGetFromCacheCallbackHelper, 2675 base::Bind(&RunGetFromCacheCallbackHelper,
2675 callback, 2676 callback,
2676 base::Owned(error), 2677 base::Owned(error),
2677 resource_id, 2678 resource_id,
2678 md5, 2679 md5,
2679 FilePath() /* gdata_file_path */, 2680 FilePath() /* gdata_file_path */,
2680 base::Owned(cache_file_path))); 2681 base::Owned(cache_file_path)));
2681
2682 } 2682 }
2683 2683
2684 void GDataFileSystem::CommitDirtyInCache( 2684 void GDataFileSystem::CommitDirtyInCache(
2685 const std::string& resource_id, 2685 const std::string& resource_id,
2686 const std::string& md5, 2686 const std::string& md5,
2687 const CacheOperationCallback& callback) { 2687 const CacheOperationCallback& callback) {
2688 InitializeCacheIfNecessary(); 2688 InitializeCacheIfNecessary();
2689 2689
2690 base::PlatformFileError* error = 2690 base::PlatformFileError* error =
2691 new base::PlatformFileError(base::PLATFORM_FILE_OK); 2691 new base::PlatformFileError(base::PLATFORM_FILE_OK);
(...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after
3596 BrowserThread::FILE, 3596 BrowserThread::FILE,
3597 from_here, 3597 from_here,
3598 base::Bind(&GDataFileSystem::RunTaskOnIOThreadPool, 3598 base::Bind(&GDataFileSystem::RunTaskOnIOThreadPool,
3599 base::Unretained(this), 3599 base::Unretained(this),
3600 request_task), 3600 request_task),
3601 reply_task); 3601 reply_task);
3602 DCHECK(posted); 3602 DCHECK(posted);
3603 } 3603 }
3604 3604
3605 } // namespace gdata 3605 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698