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

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

Issue 10204013: Move FindEntryDelegate and friends to separate file. (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 #include <sys/stat.h> 8 #include <sys/stat.h>
9 9
10 #include <set> 10 #include <set>
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 if (!net::GetMimeTypeFromExtension(local_file.Extension(), 681 if (!net::GetMimeTypeFromExtension(local_file.Extension(),
682 &upload_file_info->content_type)) { 682 &upload_file_info->content_type)) {
683 upload_file_info->content_type= kMimeTypeOctetStream; 683 upload_file_info->content_type= kMimeTypeOctetStream;
684 } 684 }
685 685
686 *error = base::PLATFORM_FILE_OK; 686 *error = base::PLATFORM_FILE_OK;
687 } 687 }
688 688
689 } // namespace 689 } // namespace
690 690
691 // FindEntryDelegate class implementation.
692
693 FindEntryDelegate::~FindEntryDelegate() {
694 }
695
696 // FindEntryCallbackRelayDelegate class implementation.
697 // This class is used to relay calls between sync and async versions
698 // of FindFileByPath(Sync|Async) calls.
699 class FindEntryCallbackRelayDelegate : public FindEntryDelegate {
700 public:
701 explicit FindEntryCallbackRelayDelegate(const FindEntryCallback& callback);
702 virtual ~FindEntryCallbackRelayDelegate();
703
704 private:
705 // FindEntryDelegate overrides.
706 virtual void OnDone(base::PlatformFileError error,
707 const FilePath& directory_path,
708 GDataEntry* entry) OVERRIDE;
709
710 const FindEntryCallback callback_;
711 };
712
713 FindEntryCallbackRelayDelegate::FindEntryCallbackRelayDelegate(
714 const FindEntryCallback& callback) : callback_(callback) {
715 }
716
717 FindEntryCallbackRelayDelegate::~FindEntryCallbackRelayDelegate() {
718 }
719
720 void FindEntryCallbackRelayDelegate::OnDone(base::PlatformFileError error,
721 const FilePath& directory_path,
722 GDataEntry* entry) {
723 if (!callback_.is_null()) {
724 callback_.Run(error, directory_path, entry);
725 }
726 }
727
728 // ReadOnlyFindEntryDelegate class implementation.
729
730 ReadOnlyFindEntryDelegate::ReadOnlyFindEntryDelegate() : entry_(NULL) {
731 }
732
733 void ReadOnlyFindEntryDelegate::OnDone(base::PlatformFileError error,
734 const FilePath& directory_path,
735 GDataEntry* entry) {
736 DCHECK(!entry_);
737 if (error == base::PLATFORM_FILE_OK)
738 entry_ = entry;
739 else
740 entry_ = NULL;
741 }
742
743 // GDataFileProperties struct implementation. 691 // GDataFileProperties struct implementation.
744 692
745 GDataFileProperties::GDataFileProperties() : is_hosted_document(false) { 693 GDataFileProperties::GDataFileProperties() : is_hosted_document(false) {
746 } 694 }
747 695
748 GDataFileProperties::~GDataFileProperties() { 696 GDataFileProperties::~GDataFileProperties() {
749 } 697 }
750 698
751 // GDataFileSystem::GetDocumentsParams struct implementation. 699 // GDataFileSystem::GetDocumentsParams struct implementation.
752 700
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 // TokenFetcher, used in DocumentsService, must be run on UI thread. 885 // TokenFetcher, used in DocumentsService, must be run on UI thread.
938 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
939 887
940 documents_service_->Authenticate(callback); 888 documents_service_->Authenticate(callback);
941 } 889 }
942 890
943 void GDataFileSystem::FindEntryByPathSync( 891 void GDataFileSystem::FindEntryByPathSync(
944 const FilePath& search_file_path, 892 const FilePath& search_file_path,
945 FindEntryDelegate* delegate) { 893 FindEntryDelegate* delegate) {
946 base::AutoLock lock(lock_); 894 base::AutoLock lock(lock_);
947 UnsafeFindEntryByPath(search_file_path, delegate); 895 root_->FindEntryByPath(search_file_path, delegate);
948 } 896 }
949 897
950 void GDataFileSystem::FindEntryByResourceIdSync( 898 void GDataFileSystem::FindEntryByResourceIdSync(
951 const std::string& resource_id, 899 const std::string& resource_id,
952 FindEntryDelegate* delegate) { 900 FindEntryDelegate* delegate) {
953 base::AutoLock lock(lock_); // To access the cache map. 901 base::AutoLock lock(lock_); // To access the cache map.
954 902
955 GDataFile* file = NULL; 903 GDataFile* file = NULL;
956 GDataEntry* entry = root_->GetEntryByResourceId(resource_id); 904 GDataEntry* entry = root_->GetEntryByResourceId(resource_id);
957 if (entry) 905 if (entry)
(...skipping 985 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, 1891 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
1944 const ResumeFileUploadCallback& callback, 1892 const ResumeFileUploadCallback& callback,
1945 const ResumeUploadResponse& response, 1893 const ResumeUploadResponse& response,
1946 scoped_ptr<DocumentEntry> new_entry) { 1894 scoped_ptr<DocumentEntry> new_entry) {
1947 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1948 if (!callback.is_null()) 1896 if (!callback.is_null())
1949 message_loop_proxy->PostTask(FROM_HERE, 1897 message_loop_proxy->PostTask(FROM_HERE,
1950 base::Bind(callback, response, base::Passed(&new_entry))); 1898 base::Bind(callback, response, base::Passed(&new_entry)));
1951 } 1899 }
1952 1900
1953 void GDataFileSystem::UnsafeFindEntryByPath(
1954 const FilePath& file_path,
1955 FindEntryDelegate* delegate) {
1956 DCHECK(delegate);
1957 lock_.AssertAcquired();
1958
1959 std::vector<FilePath::StringType> components;
1960 file_path.GetComponents(&components);
1961
1962 GDataDirectory* current_dir = root_.get();
1963 FilePath directory_path;
1964 for (size_t i = 0; i < components.size() && current_dir; i++) {
1965 directory_path = directory_path.Append(current_dir->file_name());
1966
1967 // Last element must match, if not last then it must be a directory.
1968 if (i == components.size() - 1) {
1969 if (current_dir->file_name() == components[i])
1970 delegate->OnDone(base::PLATFORM_FILE_OK, directory_path, current_dir);
1971 else
1972 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
1973
1974 return;
1975 }
1976
1977 // Not the last part of the path, search for the next segment.
1978 GDataFileCollection::const_iterator file_iter =
1979 current_dir->children().find(components[i + 1]);
1980 if (file_iter == current_dir->children().end()) {
1981 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
1982 return;
1983 }
1984
1985 // Found file, must be the last segment.
1986 if (file_iter->second->file_info().is_directory) {
1987 // Found directory, continue traversal.
1988 current_dir = file_iter->second->AsGDataDirectory();
1989 } else {
1990 if ((i + 1) == (components.size() - 1)) {
1991 delegate->OnDone(base::PLATFORM_FILE_OK,
1992 directory_path,
1993 file_iter->second);
1994 } else {
1995 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
1996 }
1997
1998 return;
1999 }
2000 }
2001 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
2002 }
2003
2004 bool GDataFileSystem::GetFileInfoByPath( 1901 bool GDataFileSystem::GetFileInfoByPath(
2005 const FilePath& file_path, GDataFileProperties* properties) { 1902 const FilePath& file_path, GDataFileProperties* properties) {
2006 DCHECK(properties); 1903 DCHECK(properties);
2007 base::AutoLock lock(lock_); 1904 base::AutoLock lock(lock_);
2008 GDataEntry* entry = GetGDataEntryByPath(file_path); 1905 GDataEntry* entry = GetGDataEntryByPath(file_path);
2009 if (!entry) 1906 if (!entry)
2010 return false; 1907 return false;
2011 1908
2012 properties->file_info = entry->file_info(); 1909 properties->file_info = entry->file_info();
2013 properties->resource_id = entry->resource_id(); 1910 properties->resource_id = entry->resource_id();
(...skipping 23 matching lines...) Expand all
2037 NOTREACHED() << "Called on an unexpected thread: " 1934 NOTREACHED() << "Called on an unexpected thread: "
2038 << base::PlatformThread::CurrentId(); 1935 << base::PlatformThread::CurrentId();
2039 return ui_weak_ptr_factory_->GetWeakPtr(); 1936 return ui_weak_ptr_factory_->GetWeakPtr();
2040 } 1937 }
2041 1938
2042 GDataEntry* GDataFileSystem::GetGDataEntryByPath( 1939 GDataEntry* GDataFileSystem::GetGDataEntryByPath(
2043 const FilePath& file_path) { 1940 const FilePath& file_path) {
2044 lock_.AssertAcquired(); 1941 lock_.AssertAcquired();
2045 // Find directory element within the cached file system snapshot. 1942 // Find directory element within the cached file system snapshot.
2046 ReadOnlyFindEntryDelegate find_delegate; 1943 ReadOnlyFindEntryDelegate find_delegate;
2047 UnsafeFindEntryByPath(file_path, &find_delegate); 1944 root_->FindEntryByPath(file_path, &find_delegate);
2048 return find_delegate.entry(); 1945 return find_delegate.entry();
2049 } 1946 }
2050 1947
2051 void GDataFileSystem::GetCacheState(const std::string& resource_id, 1948 void GDataFileSystem::GetCacheState(const std::string& resource_id,
2052 const std::string& md5, 1949 const std::string& md5,
2053 const GetCacheStateCallback& callback) { 1950 const GetCacheStateCallback& callback) {
2054 // This method originates from GDataFile::GetCacheState, which already locks, 1951 // This method originates from GDataFile::GetCacheState, which already locks,
2055 // so we shouldn't lock here. 1952 // so we shouldn't lock here.
2056 UnsafeInitializeCacheIfNecessary(); 1953 UnsafeInitializeCacheIfNecessary();
2057 1954
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
2758 RemoveFromCache(resource_id, CacheOperationCallback()); 2655 RemoveFromCache(resource_id, CacheOperationCallback());
2759 2656
2760 return base::PLATFORM_FILE_OK; 2657 return base::PLATFORM_FILE_OK;
2761 } 2658 }
2762 2659
2763 base::PlatformFileError GDataFileSystem::UpdateFromFeed( 2660 base::PlatformFileError GDataFileSystem::UpdateFromFeed(
2764 const std::vector<DocumentFeed*>& feed_list, 2661 const std::vector<DocumentFeed*>& feed_list,
2765 ContentOrigin origin, 2662 ContentOrigin origin,
2766 int start_changestamp, 2663 int start_changestamp,
2767 int root_feed_changestamp) { 2664 int root_feed_changestamp) {
2768 DVLOG(1) << "Updating directory with a feed"; 2665 DVLOG(1) << "Updating directory with a feed";
2769 2666
2770 bool is_delta_feed = start_changestamp != 0; 2667 bool is_delta_feed = start_changestamp != 0;
2771 // We need to lock here as well (despite FindEntryByPath lock) since directory 2668 // We need to lock here as well (despite FindEntryByPath lock) since directory
2772 // instance below is a 'live' object. 2669 // instance below is a 'live' object.
2773 base::AutoLock lock(lock_); 2670 base::AutoLock lock(lock_);
2774 bool should_notify_initial_load = root_->origin() == INITIALIZING; 2671 bool should_notify_initial_load = root_->origin() == INITIALIZING;
2775 2672
2776 root_->set_origin(origin); 2673 root_->set_origin(origin);
2777 root_->set_refresh_time(base::Time::Now()); 2674 root_->set_refresh_time(base::Time::Now());
2778 2675
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
2948 const DocumentFeed* feed = feed_list[i]; 2845 const DocumentFeed* feed = feed_list[i];
2949 2846
2950 // Get upload url from the root feed. Links for all other collections will 2847 // Get upload url from the root feed. Links for all other collections will
2951 // be handled in GDatadirectory::FromDocumentEntry(); 2848 // be handled in GDatadirectory::FromDocumentEntry();
2952 if (i == 0) { 2849 if (i == 0) {
2953 const Link* root_feed_upload_link = 2850 const Link* root_feed_upload_link =
2954 feed->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA); 2851 feed->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA);
2955 if (root_feed_upload_link) 2852 if (root_feed_upload_link)
2956 root_->set_upload_url(root_feed_upload_link->href()); 2853 root_->set_upload_url(root_feed_upload_link->href());
2957 *feed_changestamp = feed->largest_changestamp(); 2854 *feed_changestamp = feed->largest_changestamp();
2958 DCHECK(*feed_changestamp >= 0); 2855 DCHECK_GE(*feed_changestamp, 0);
2959 } 2856 }
2960 2857
2961 for (ScopedVector<DocumentEntry>::const_iterator iter = 2858 for (ScopedVector<DocumentEntry>::const_iterator iter =
2962 feed->entries().begin(); 2859 feed->entries().begin();
2963 iter != feed->entries().end(); ++iter) { 2860 iter != feed->entries().end(); ++iter) {
2964 DocumentEntry* doc = *iter; 2861 DocumentEntry* doc = *iter;
2965 GDataEntry* entry = GDataEntry::FromDocumentEntry(NULL, doc, 2862 GDataEntry* entry = GDataEntry::FromDocumentEntry(NULL, doc,
2966 root_.get()); 2863 root_.get());
2967 // Some document entries don't map into files (i.e. sites). 2864 // Some document entries don't map into files (i.e. sites).
2968 if (!entry) 2865 if (!entry)
(...skipping 1428 matching lines...) Expand 10 before | Expand all | Expand 10 after
4397 pref_registrar_->Init(profile_->GetPrefs()); 4294 pref_registrar_->Init(profile_->GetPrefs());
4398 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); 4295 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this);
4399 } 4296 }
4400 4297
4401 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { 4298 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) {
4402 delete global_free_disk_getter_for_testing; // Safe to delete NULL; 4299 delete global_free_disk_getter_for_testing; // Safe to delete NULL;
4403 global_free_disk_getter_for_testing = getter; 4300 global_free_disk_getter_for_testing = getter;
4404 } 4301 }
4405 4302
4406 } // namespace gdata 4303 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698