diff options
author | sreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-30 00:19:02 +0000 |
---|---|---|
committer | sreeram@chromium.org <sreeram@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-30 00:19:02 +0000 |
commit | bc7e587bc904361362b67f1731c1fa4894b69cc7 (patch) | |
tree | 3ab447d20b868c81d03c51677b92512afa1d2eaf /chrome/browser/chromeos/gdata | |
parent | c226ab44d6ce6d4cbedebc3e64e10c82ea9829e4 (diff) | |
download | chromium_src-bc7e587bc904361362b67f1731c1fa4894b69cc7.zip chromium_src-bc7e587bc904361362b67f1731c1fa4894b69cc7.tar.gz chromium_src-bc7e587bc904361362b67f1731c1fa4894b69cc7.tar.bz2 |
Revert 154002 - RefreshFile cleanup.
BUG=142048
TEST=unit tests.
* RefreshFile now takes a scoped_ptr<DocumentEntry> instead of scoped_ptr<DriveFile>. It also has a GetEntryInfoWithFilePathCallback so the file path and entry_proto of the refreshed file may be provided to the caller.
* Rename PostError to PostFileMoveCallbackError. Introduce PostGetEntryInfoWithFilePathCallbackError in anonymous scope.
* Get rid of RefreshFile unit test. Various DriveFileSystemTests provide adequate coverage. Need to write a more targeted RefreshFile unit test esp for error scenarios, but for now, this test is not salvageable.
* DocumentFeed now has a new method ReleaseEntries, necessary for DriveFileSystem::OnSearch.
* AddEntryToSearchResults is now a private method DriveFileSystem::AddToSearchResults. This is necessary to get rid of entry_skipped_callback, which is unnecessary - CheckForUpdates should be directly called instead. Also, this takes the AddToSearchResultsCallback, to get around the 7 arg limit for base::Bind. should_run_callback check is replaced by the null callback check.
* DriveFileSystem::OnSearch is considerably simplified.
* OnGetDocumentEntry is continued with CheckForSpaceBeforeDownload after RefreshFile (previously errors were ignored).
* Add cache_file_path to GetFileFromCacheParams, so these params can be reused for CheckForSpaceBeforeDownload.
* UpdateEntryDataOnUIThread is continued with UpdateCacheEntryOnUIThread after RefreshFile (previously errors were ignored). UpdateEntryParams is introduced so number of args of UpdateCacheEntryOnUIThread is under 7.
* Remove some unnecessary struct dtors.
Review URL: https://chromiumcodereview.appspot.com/10898013
TBR=achuith@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10887040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@154012 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/gdata')
8 files changed, 278 insertions, 293 deletions
diff --git a/chrome/browser/chromeos/gdata/drive_file_system.cc b/chrome/browser/chromeos/gdata/drive_file_system.cc index 54bc90f..4e71a5e 100644 --- a/chrome/browser/chromeos/gdata/drive_file_system.cc +++ b/chrome/browser/chromeos/gdata/drive_file_system.cc @@ -226,6 +226,47 @@ void CopyLocalFileOnBlockingPool( DRIVE_FILE_OK : DRIVE_FILE_ERROR_FAILED; } +// This is similar to SearchCallback, but the first two parameters (error, +// next_feed) are omitted. These parameters are pre-bound by the client in +// order to reduce parameters of AddEntryToSearchResults(). +typedef base::Callback<void( + scoped_ptr<std::vector<SearchResultInfo> > results)> + ProcessSearchResultsCallback; + +// Callback for GetEntryByResourceIdAsync. +// Adds |entry_proto| to |results|. Runs |callback| with |results| when +// |run_callback| is true. When |entry| is not present in our local file system +// snapshot, it is not added to |results|. Instead, |entry_skipped_callback| is +// called. +void AddToSearchResults( + std::vector<SearchResultInfo>* results, + const ProcessSearchResultsCallback& callback, + const base::Closure& entry_skipped_callback, + bool run_callback, + DriveFileError error, + const FilePath& file_path, + scoped_ptr<DriveEntryProto> entry_proto) { + DCHECK(!callback.is_null()); + DCHECK(!entry_skipped_callback.is_null()); + + // If a result is not present in our local file system snapshot, invoke + // |entry_skipped_callback| and refreshes the snapshot with delta feed. + // For example, this may happen if the entry has recently been added to the + // drive (and we still haven't received its delta feed). + if (error == DRIVE_FILE_OK) { + DCHECK(entry_proto.get()); + const bool is_directory = (entry_proto->file_info().is_directory()); + results->push_back(SearchResultInfo(file_path, is_directory)); + } else { + entry_skipped_callback.Run(); + } + + if (run_callback) { + scoped_ptr<std::vector<SearchResultInfo> > result_vec(results); + callback.Run(result_vec.Pass()); + } +} + // Helper function for binding |path| to GetEntryInfoWithFilePathCallback and // create GetEntryInfoCallback. void RunGetEntryInfoWithFilePathCallback( @@ -260,6 +301,7 @@ DriveFileSystem::CreateDirectoryParams::~CreateDirectoryParams() { struct DriveFileSystem::GetFileCompleteForOpenParams { GetFileCompleteForOpenParams(const std::string& resource_id, const std::string& md5); + ~GetFileCompleteForOpenParams(); std::string resource_id; std::string md5; }; @@ -271,6 +313,9 @@ DriveFileSystem::GetFileCompleteForOpenParams::GetFileCompleteForOpenParams( md5(md5) { } +DriveFileSystem::GetFileCompleteForOpenParams::~GetFileCompleteForOpenParams() { +} + // DriveFileSystem::GetFileFromCacheParams struct implementation. struct DriveFileSystem::GetFileFromCacheParams { GetFileFromCacheParams( @@ -281,28 +326,41 @@ struct DriveFileSystem::GetFileFromCacheParams { const std::string& md5, const std::string& mime_type, const GetFileCallback& get_file_callback, - const GetContentCallback& get_content_callback) - : virtual_file_path(virtual_file_path), - local_tmp_path(local_tmp_path), - content_url(content_url), - resource_id(resource_id), - md5(md5), - mime_type(mime_type), - get_file_callback(get_file_callback), - get_content_callback(get_content_callback) { - } + const GetContentCallback& get_content_callback); + ~GetFileFromCacheParams(); FilePath virtual_file_path; FilePath local_tmp_path; - FilePath cache_file_path; GURL content_url; std::string resource_id; std::string md5; std::string mime_type; - GetFileCallback get_file_callback; - GetContentCallback get_content_callback; + const GetFileCallback get_file_callback; + const GetContentCallback get_content_callback; }; +DriveFileSystem::GetFileFromCacheParams::GetFileFromCacheParams( + const FilePath& virtual_file_path, + const FilePath& local_tmp_path, + const GURL& content_url, + const std::string& resource_id, + const std::string& md5, + const std::string& mime_type, + const GetFileCallback& get_file_callback, + const GetContentCallback& get_content_callback) + : virtual_file_path(virtual_file_path), + local_tmp_path(local_tmp_path), + content_url(content_url), + resource_id(resource_id), + md5(md5), + mime_type(mime_type), + get_file_callback(get_file_callback), + get_content_callback(get_content_callback) { +} + +DriveFileSystem::GetFileFromCacheParams::~GetFileFromCacheParams() { +} + // DriveFileSystem::StartFileUploadParams implementation. struct DriveFileSystem::StartFileUploadParams { StartFileUploadParams(const FilePath& in_local_file_path, @@ -343,24 +401,6 @@ struct DriveFileSystem::AddUploadedFileParams { std::string md5; }; -// DriveFileSystem::UpdateEntryParams implementation. -struct DriveFileSystem::UpdateEntryParams { - UpdateEntryParams(const std::string& resource_id, - const std::string& md5, - const FilePath& file_content_path, - const base::Closure& callback) - : resource_id(resource_id), - md5(md5), - file_content_path(file_content_path), - callback(callback) { - } - - std::string resource_id; - std::string md5; - FilePath file_content_path; - base::Closure callback; -}; - // DriveFileSystem class implementation. @@ -406,8 +446,6 @@ void DriveFileSystem::Initialize() { void DriveFileSystem::CheckForUpdates() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DVLOG(1) << "CheckForUpdates"; - ContentOrigin initial_origin = resource_metadata_->origin(); if (initial_origin == FROM_SERVER) { resource_metadata_->set_origin(REFRESHING); @@ -1562,21 +1600,20 @@ void DriveFileSystem::GetFileByResourceIdAfterGetEntry( entry_proto.Pass()); } -void DriveFileSystem::OnGetFileFromCache( - const GetFileFromCacheParams& in_params, - DriveFileError error, - const std::string& resource_id, - const std::string& md5, - const FilePath& cache_file_path) { +void DriveFileSystem::OnGetFileFromCache(const GetFileFromCacheParams& params, + DriveFileError error, + const std::string& resource_id, + const std::string& md5, + const FilePath& cache_file_path) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!in_params.get_file_callback.is_null()); + DCHECK(!params.get_file_callback.is_null()); // Have we found the file in cache? If so, return it back to the caller. if (error == DRIVE_FILE_OK) { - in_params.get_file_callback.Run(error, - cache_file_path, - in_params.mime_type, - REGULAR_FILE); + params.get_file_callback.Run(error, + cache_file_path, + params.mime_type, + REGULAR_FILE); return; } @@ -1591,63 +1628,57 @@ void DriveFileSystem::OnGetFileFromCache( // - if we don't have enough space, try to free up the disk space // - if we still don't have enough space, return "no space" error // - if we have enough space, start downloading the file from the server - GetFileFromCacheParams params(in_params); - params.cache_file_path = cache_file_path; drive_service_->GetDocumentEntry( resource_id, base::Bind(&DriveFileSystem::OnGetDocumentEntry, ui_weak_ptr_, - params)); -} - -void DriveFileSystem::OnGetDocumentEntry(const GetFileFromCacheParams& params, + cache_file_path, + GetFileFromCacheParams(params.virtual_file_path, + params.local_tmp_path, + params.content_url, + params.resource_id, + params.md5, + params.mime_type, + params.get_file_callback, + params.get_content_callback))); +} + +void DriveFileSystem::OnGetDocumentEntry(const FilePath& cache_file_path, + const GetFileFromCacheParams& params, GDataErrorCode status, scoped_ptr<base::Value> data) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(!params.get_file_callback.is_null()); - const DriveFileError error = util::GDataToDriveFileError(status); - if (error != DRIVE_FILE_OK) { - params.get_file_callback.Run(error, - params.cache_file_path, - params.mime_type, - REGULAR_FILE); - return; - } - - scoped_ptr<DocumentEntry> doc_entry(DocumentEntry::ExtractAndParse(*data)); - GURL content_url = doc_entry->content_url(); - int64 file_size = doc_entry->file_size(); - - DCHECK(!content_url.is_empty()); - DCHECK_EQ(params.resource_id, doc_entry->resource_id()); - resource_metadata_->RefreshFile( - doc_entry.Pass(), - base::Bind(&DriveFileSystem::CheckForSpaceBeforeDownload, - ui_weak_ptr_, - params, - file_size, - content_url)); -} + DriveFileError error = util::GDataToDriveFileError(status); -void DriveFileSystem::CheckForSpaceBeforeDownload( - const GetFileFromCacheParams& params, - int64 file_size, - const GURL& content_url, - DriveFileError error, - const FilePath& /* drive_file_path */, - scoped_ptr<DriveEntryProto> /* entry_proto */) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!params.get_file_callback.is_null()); + scoped_ptr<DriveEntry> fresh_entry; + if (error == DRIVE_FILE_OK) { + scoped_ptr<DocumentEntry> doc_entry(DocumentEntry::ExtractAndParse(*data)); + if (doc_entry.get()) + fresh_entry.reset(resource_metadata_->FromDocumentEntry(*doc_entry)); + if (!fresh_entry.get() || !fresh_entry->AsDriveFile()) { + LOG(ERROR) << "Got invalid entry from server for " << params.resource_id; + error = DRIVE_FILE_ERROR_FAILED; + } + } if (error != DRIVE_FILE_OK) { params.get_file_callback.Run(error, - params.cache_file_path, + cache_file_path, params.mime_type, REGULAR_FILE); return; } + GURL content_url = fresh_entry->content_url(); + int64 file_size = fresh_entry->file_info().size; + + DCHECK_EQ(params.resource_id, fresh_entry->resource_id()); + scoped_ptr<DriveFile> fresh_entry_as_file( + fresh_entry.release()->AsDriveFile()); + resource_metadata_->RefreshFile(fresh_entry_as_file.Pass()); + bool* has_enough_space = new bool(false); util::PostBlockingPoolSequencedTaskAndReply( FROM_HERE, @@ -1660,7 +1691,7 @@ void DriveFileSystem::CheckForSpaceBeforeDownload( ui_weak_ptr_, params, content_url, - params.cache_file_path, + cache_file_path, base::Owned(has_enough_space))); } @@ -2190,16 +2221,14 @@ void DriveFileSystem::ContinueCreateDirectory( } } -void DriveFileSystem::OnSearch(const SearchCallback& search_callback, +void DriveFileSystem::OnSearch(const SearchCallback& callback, GetDocumentsParams* params, DriveFileError error) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!search_callback.is_null()); + DCHECK(!callback.is_null()); if (error != DRIVE_FILE_OK) { - search_callback.Run(error, - GURL(), - scoped_ptr<std::vector<SearchResultInfo> >()); + callback.Run(error, GURL(), scoped_ptr<std::vector<SearchResultInfo> >()); return; } @@ -2207,7 +2236,6 @@ void DriveFileSystem::OnSearch(const SearchCallback& search_callback, // The directory is not really part of the file system, so it has no parent or // root. std::vector<SearchResultInfo>* results(new std::vector<SearchResultInfo>()); - scoped_ptr<std::vector<SearchResultInfo> > result_vec(results); DCHECK_EQ(1u, params->feed_list->size()); DocumentFeed* feed = params->feed_list->at(0); @@ -2216,55 +2244,47 @@ void DriveFileSystem::OnSearch(const SearchCallback& search_callback, GURL next_feed; feed->GetNextFeedURL(&next_feed); - const base::Closure callback = base::Bind( - search_callback, DRIVE_FILE_OK, next_feed, base::Passed(&result_vec)); - - std::vector<DocumentEntry*> entries; - feed->ReleaseEntries(&entries); - if (entries.empty()) { - callback.Run(); + if (feed->entries().empty()) { + scoped_ptr<std::vector<SearchResultInfo> > result_vec(results); + callback.Run(DRIVE_FILE_OK, next_feed, result_vec.Pass()); return; } - // Go through all entries generated by the feed and add them to the search + // Go through all entires generated by the feed and add them to the search // result directory. - for (size_t i = 0; i < entries.size(); ++i) { - // Run the callback if this is the last iteration of the loop. - bool const should_run_callback = (i+1 == entries.size()); - resource_metadata_->RefreshFile( - scoped_ptr<DocumentEntry>(entries[i]), - base::Bind(&DriveFileSystem::AddToSearchResults, - ui_weak_ptr_, - results, - should_run_callback ? callback : base::Closure())); - } -} + for (size_t i = 0; i < feed->entries().size(); ++i) { + DocumentEntry* doc = const_cast<DocumentEntry*>(feed->entries()[i]); + scoped_ptr<DriveEntry> entry(resource_metadata_->FromDocumentEntry(*doc)); -void DriveFileSystem::AddToSearchResults( - std::vector<SearchResultInfo>* results, - const base::Closure& callback, - DriveFileError error, - const FilePath& drive_file_path, - scoped_ptr<DriveEntryProto> entry_proto) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + if (!entry.get()) + continue; - // If a result is not present in our local file system snapshot, call - // CheckForUpdates to refresh the snapshot with a delta feed. This may happen - // if the entry has recently been added to the drive (and we still haven't - // received its delta feed). - if (error == DRIVE_FILE_OK) { - DCHECK(entry_proto.get()); - const bool is_directory = entry_proto->file_info().is_directory(); - results->push_back(SearchResultInfo(drive_file_path, is_directory)); - DVLOG(1) << "AddToSearchResults " << drive_file_path.value(); - } else if (error == DRIVE_FILE_ERROR_NOT_FOUND) { - CheckForUpdates(); - } else { - NOTREACHED(); - } + DCHECK_EQ(doc->resource_id(), entry->resource_id()); + DCHECK(!entry->is_deleted()); - if (!callback.is_null()) - callback.Run(); + std::string entry_resource_id = entry->resource_id(); + + // This will do nothing if the entry is not already present in file system. + if (entry->AsDriveFile()) { + scoped_ptr<DriveFile> entry_as_file(entry.release()->AsDriveFile()); + resource_metadata_->RefreshFile(entry_as_file.Pass()); + // We shouldn't use entry object after this point. + DCHECK(!entry.get()); + } + + // We will need information about result entry to create info for callback. + // We can't use |entry| anymore, so we have to refetch entry from file + // system. Also, |entry| doesn't have file path set before |RefreshFile| + // call, so we can't get file path from there. + const bool should_run_callback = (i+1 == feed->entries().size()); + resource_metadata_->GetEntryInfoByResourceId( + entry_resource_id, + base::Bind(&AddToSearchResults, + results, + base::Bind(callback, DRIVE_FILE_OK, next_feed), + base::Bind(&DriveFileSystem::CheckForUpdates, ui_weak_ptr_), + should_run_callback)); + } } void DriveFileSystem::Search(const std::string& search_query, @@ -2824,43 +2844,36 @@ void DriveFileSystem::UpdateEntryData(const std::string& resource_id, FROM_HERE, base::Bind(&DriveFileSystem::UpdateEntryDataOnUIThread, ui_weak_ptr_, - UpdateEntryParams(resource_id, - md5, - file_content_path, - callback), - base::Passed(&entry))); + resource_id, + md5, + base::Passed(&entry), + file_content_path, + callback)); } void DriveFileSystem::UpdateEntryDataOnUIThread( - const UpdateEntryParams& params, - scoped_ptr<DocumentEntry> entry) { + const std::string& resource_id, + const std::string& md5, + scoped_ptr<DocumentEntry> entry, + const FilePath& file_content_path, + const base::Closure& callback) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - resource_metadata_->RefreshFile( - entry.Pass(), - base::Bind(&DriveFileSystem::UpdateCacheEntryOnUIThread, - ui_weak_ptr_, - params)); -} - -void DriveFileSystem::UpdateCacheEntryOnUIThread( - const UpdateEntryParams& params, - DriveFileError error, - const FilePath& /* drive_file_path */, - scoped_ptr<DriveEntryProto> /* entry_proto */) { - if (error != DRIVE_FILE_OK) { - if (!params.callback.is_null()) - params.callback.Run(); + scoped_ptr<DriveFile> new_entry( + resource_metadata_->FromDocumentEntry(*entry)->AsDriveFile()); + if (!new_entry.get()) { return; } + resource_metadata_->RefreshFile(new_entry.Pass()); + // Add the file to the cache if we have uploaded a new file. - cache_->StoreOnUIThread(params.resource_id, - params.md5, - params.file_content_path, + cache_->StoreOnUIThread(resource_id, + md5, + file_content_path, DriveCache::FILE_OPERATION_MOVE, base::Bind(&OnCacheUpdatedForAddUploadedFile, - params.callback)); + callback)); } void DriveFileSystem::Observe(int type, diff --git a/chrome/browser/chromeos/gdata/drive_file_system.h b/chrome/browser/chromeos/gdata/drive_file_system.h index e6acaf6..9e5dd4e 100644 --- a/chrome/browser/chromeos/gdata/drive_file_system.h +++ b/chrome/browser/chromeos/gdata/drive_file_system.h @@ -194,30 +194,15 @@ class DriveFileSystem : public DriveFileSystemInterface, // Struct used for AddUploadedFile. struct AddUploadedFileParams; - // Struct used by UpdateEntryData. - struct UpdateEntryParams; - // Callback passed to |LoadFeedFromServer| from |Search| method. // |callback| is that should be run with data received from - // |LoadFeedFromServer|. |callback| must not be null. + // |LoadFeedFromServer|. // |params| params used for getting document feed for content search. // |error| error code returned by |LoadFeedFromServer|. void OnSearch(const SearchCallback& callback, GetDocumentsParams* params, DriveFileError error); - // Callback for DriveResourceMetadata::RefreshFile, from OnSearch. - // Adds |drive_file_path| to |results|. When |entry_proto| is not present in - // the local file system snapshot, it is not added to |results|. Instead, - // CheckForUpdates is called. Runs |callback| with |results|. - // |callback| may be null. - void AddToSearchResults( - std::vector<SearchResultInfo>* results, - const base::Closure& callback, - DriveFileError error, - const FilePath& drive_file_path, - scoped_ptr<DriveEntryProto> entry_proto); - // Part of TransferFileFromLocalToRemote(). Called after // GetEntryInfoByPath() is complete. void TransferFileFromLocalToRemoteAfterGetEntryInfo( @@ -622,21 +607,14 @@ class DriveFileSystem : public DriveFileSystemInterface, // Callback for |drive_service_->GetDocumentEntry|. // It is called before file download. If GetDocumentEntry was successful, // file download procedure is started for the file. The file is downloaded - // from the content url extracted from the fetched metadata. - void OnGetDocumentEntry(const GetFileFromCacheParams& params, + // from the content url extracted from the fetched metadata to + // |cache_file_path|. Also, available space checks are done using file size + // from the fetched metadata. + void OnGetDocumentEntry(const FilePath& cache_file_path, + const GetFileFromCacheParams& params, GDataErrorCode status, scoped_ptr<base::Value> data); - // Check available space using file size from the fetched metadata. Called - // from OnGetDocumentEntry after RefreshFile is complete. - void CheckForSpaceBeforeDownload( - const GetFileFromCacheParams& params, - int64 file_size, - const GURL& content_url, - DriveFileError error, - const FilePath& drive_file_path, - scoped_ptr<DriveEntryProto> entry_proto); - // Starts downloading a file if we have enough disk space indicated by // |has_enough_space|. void StartDownloadFileIfEnoughSpace(const GetFileFromCacheParams& params, @@ -698,7 +676,7 @@ class DriveFileSystem : public DriveFileSystemInterface, void UpdateFileByEntryInfo( const FileOperationCallback& callback, DriveFileError error, - const FilePath& drive_file_path, + const FilePath& /* drive_file_path */, scoped_ptr<DriveEntryProto> entry_proto); // Part of UpdateFileByResourceId(). @@ -792,8 +770,11 @@ class DriveFileSystem : public DriveFileSystemInterface, const FilePath& file_content_path, DriveCache::FileOperationType cache_operation, const base::Closure& callback); - void UpdateEntryDataOnUIThread(const UpdateEntryParams& params, - scoped_ptr<DocumentEntry> entry); + void UpdateEntryDataOnUIThread(const std::string& resource_id, + const std::string& md5, + scoped_ptr<DocumentEntry> entry, + const FilePath& file_content_path, + const base::Closure& callback); // Part of GetEntryInfoByResourceId(). Called after // DriveResourceMetadata::GetEntryInfoByResourceId() is complete. @@ -844,13 +825,6 @@ class DriveFileSystem : public DriveFileSystemInterface, DriveFileError error, scoped_ptr<DriveEntryProto> entry_proto); - // Part of UpdateEntryDataOnUIThread(). Called after RefreshFile is complete. - void UpdateCacheEntryOnUIThread( - const UpdateEntryParams& params, - DriveFileError error, - const FilePath& drive_file_path, - scoped_ptr<DriveEntryProto> entry_proto); - // Part of GetEntryByResourceId and GetEntryByPath. Checks whether there is a // local dirty cache for the entry, and if there is, replace the // PlatformFileInfo part of the |entry_proto| with the locally modified info. diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata.cc b/chrome/browser/chromeos/gdata/drive_resource_metadata.cc index 934a5ef..198b933 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata.cc +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata.cc @@ -28,24 +28,12 @@ const char kDBKeyLargestChangestamp[] = "m:largest_changestamp"; const char kDBKeyVersion[] = "m:version"; const char kDBKeyResourceIdPrefix[] = "r:"; -// Posts |error| to |callback| asynchronously. |callback| must not be null. -void PostFileMoveCallbackError(const FileMoveCallback& callback, - DriveFileError error) { - base::MessageLoopProxy::current()->PostTask( - FROM_HERE, +// Posts |error| to |callback| asynchronously. +void PostError(const FileMoveCallback& callback, DriveFileError error) { + base::MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(callback, error, FilePath())); } -// Posts |error| to |callback| asynchronously. |callback| must not be null. -void PostGetEntryInfoWithFilePathCallbackError( - const GetEntryInfoWithFilePathCallback& callback, - DriveFileError error) { - scoped_ptr<DriveEntryProto> proto; - base::MessageLoopProxy::current()->PostTask( - FROM_HERE, - base::Bind(callback, error, FilePath(), base::Passed(&proto))); -} - } // namespace EntryInfoResult::EntryInfoResult() : error(DRIVE_FILE_ERROR_FAILED) { @@ -245,25 +233,25 @@ void DriveResourceMetadata::AddEntryToDirectory( DCHECK(!callback.is_null()); if (!doc_entry.get()) { - PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_FAILED); + PostError(callback, DRIVE_FILE_ERROR_FAILED); return; } DriveEntry* new_entry = FromDocumentEntry(*doc_entry); if (!new_entry) { - PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_FAILED); + PostError(callback, DRIVE_FILE_ERROR_FAILED); return; } DriveEntry* dir_entry = FindEntryByPathSync(directory_path); if (!dir_entry) { - PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); + PostError(callback, DRIVE_FILE_ERROR_NOT_FOUND); return; } DriveDirectory* directory = dir_entry->AsDriveDirectory(); if (!directory) { - PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_A_DIRECTORY); + PostError(callback, DRIVE_FILE_ERROR_NOT_A_DIRECTORY); return; } @@ -486,70 +474,29 @@ void DriveResourceMetadata::GetEntryInfoPairByPaths( callback)); } -void DriveResourceMetadata::RefreshFile( - scoped_ptr<DocumentEntry> doc_entry, - const GetEntryInfoWithFilePathCallback& callback) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!callback.is_null()); - - if (!doc_entry.get()) { - PostGetEntryInfoWithFilePathCallbackError( - callback, DRIVE_FILE_ERROR_FAILED); - return; - } - - DriveEntry* drive_entry = FromDocumentEntry(*doc_entry); - if (!drive_entry) { - PostGetEntryInfoWithFilePathCallbackError( - callback, DRIVE_FILE_ERROR_FAILED); - return; - } - - scoped_ptr<DriveFile> fresh_file(drive_entry->AsDriveFile()); - if (!fresh_file.get()) { - // This is a directory, return the directory info instead. - GetEntryInfoByResourceId(drive_entry->resource_id(), callback); - return; - } +void DriveResourceMetadata::RefreshFile(scoped_ptr<DriveFile> fresh_file) { + DCHECK(fresh_file.get()); // Need to get a reference here because Passed() could get evaluated first. const std::string& resource_id = fresh_file->resource_id(); - DVLOG(1) << "RefreshFile " << resource_id; GetEntryByResourceIdAsync( resource_id, base::Bind(&DriveResourceMetadata::RefreshFileInternal, - base::Passed(&fresh_file), - callback)); + base::Passed(&fresh_file))); } // static void DriveResourceMetadata::RefreshFileInternal( scoped_ptr<DriveFile> fresh_file, - const GetEntryInfoWithFilePathCallback& callback, DriveEntry* old_entry) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!callback.is_null()); - DriveDirectory* entry_parent = old_entry ? old_entry->parent() : NULL; + if (entry_parent) { + DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id()); + DCHECK(old_entry->AsDriveFile()); - if (!entry_parent) { - callback.Run(DRIVE_FILE_ERROR_NOT_FOUND, - FilePath(), - scoped_ptr<DriveEntryProto>()); - return; + entry_parent->RemoveEntry(old_entry); + entry_parent->AddEntry(fresh_file.release()); } - - DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id()); - DCHECK(old_entry->AsDriveFile()); - - entry_parent->RemoveEntry(old_entry); - DriveEntry* new_entry = fresh_file.release(); - entry_parent->AddEntry(new_entry); - - DVLOG(1) << "RefreshFileInternal"; - scoped_ptr<DriveEntryProto> entry_proto(new DriveEntryProto); - new_entry->ToProtoFull(entry_proto.get()); - callback.Run(DRIVE_FILE_OK, new_entry->GetFilePath(), entry_proto.Pass()); } void DriveResourceMetadata::RefreshDirectory( diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata.h b/chrome/browser/chromeos/gdata/drive_resource_metadata.h index 866c155..e883b5c 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata.h +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata.h @@ -162,20 +162,20 @@ class DriveResourceMetadata { // Add |doc entry| to directory with path |directory_path| and invoke the // callback asynchronously. - // |callback| must not be null. + // |callback| may not be null. void AddEntryToDirectory(const FilePath& directory_path, scoped_ptr<DocumentEntry> doc_entry, const FileMoveCallback& callback); // Moves |entry| to |directory_path| asynchronously. Removes entry from // previous parent. Must be called on UI thread. |callback| is called on the - // UI thread. |callback| must not be null. + // UI thread. |callback| may not be null. void MoveEntryToDirectory(const FilePath& directory_path, DriveEntry* entry, const FileMoveCallback& callback); // Removes entry with |resource_id| from its parent. Calls |callback| with the - // path of the parent directory. |callback| must not be null. + // path of the parent directory. |callback| may not be null. void RemoveEntryFromParent(const std::string& resource_id, const FileMoveCallback& callback); @@ -228,16 +228,12 @@ class DriveResourceMetadata { const FilePath& second_path, const GetEntryInfoPairCallback& callback); - // Replaces a file entry with the same resource id as |doc_entry| by deleting - // the existing entry, creating a new DriveFile from |doc_entry|, and adding - // it to the parent of the old entry. For directories, this just returns the - // existing directory proto. |callback| is run with the error, file path and - // proto of the entry. |callback| must not be null. - void RefreshFile(scoped_ptr<DocumentEntry> doc_entry, - const GetEntryInfoWithFilePathCallback& callback); + // Replaces file entry with the same resource id as |fresh_file| with its + // fresh value |fresh_file|. + void RefreshFile(scoped_ptr<DriveFile> fresh_file); // Removes all child files of |directory| and replace with file_map. - // |callback| is called with the directory path. |callback| must not be null. + // |callback| is called with the directory path. |callback| may not be null. void RefreshDirectory(const std::string& directory_resource_id, const ResourceMap& file_map, const FileMoveCallback& callback); @@ -295,21 +291,17 @@ class DriveResourceMetadata { // These internal functions need friend access to private DriveDirectory // methods. // Replaces file entry |old_entry| with its fresh value |fresh_file|. - // |callback| is run with the error, file path and proto of |fresh_file|. - // |callback| must not be null. - static void RefreshFileInternal( - scoped_ptr<DriveFile> fresh_file, - const GetEntryInfoWithFilePathCallback& callback, - DriveEntry* old_entry); + static void RefreshFileInternal(scoped_ptr<DriveFile> fresh_file, + DriveEntry* old_entry); // Removes all child files of |directory| and replace with file_map. - // |callback| must not be null. + // |callback| may not be null. static void RefreshDirectoryInternal(const ResourceMap& file_map, const FileMoveCallback& callback, DriveEntry* directory_entry); // Removes |entry| from its parent and calls |callback|. - // |callback| must not be null. + // |callback| may not be null. static void RemoveEntryFromParentInternal(const FileMoveCallback& callback, DriveEntry* entry); diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc b/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc index 190d4fb..816ef43 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc @@ -237,6 +237,75 @@ TEST(DriveResourceMetadataTest, VersionCheck) { ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); } +TEST(DriveResourceMetadataTest, RefreshFile) { + MessageLoopForUI message_loop; + content::TestBrowserThread ui_thread(content::BrowserThread::UI, + &message_loop); + + DriveResourceMetadata resource_metadata; + // Add a directory to the file system. + DriveDirectory* directory_entry = resource_metadata.CreateDriveDirectory(); + directory_entry->set_resource_id("folder:directory_resource_id"); + directory_entry->set_title("directory"); + directory_entry->SetBaseNameFromTitle(); + DriveFileError error = DRIVE_FILE_ERROR_FAILED; + FilePath moved_file_path; + FilePath root_path(kDriveRootDirectory); + resource_metadata.MoveEntryToDirectory( + root_path, + directory_entry, + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, + &moved_file_path)); + test_util::RunBlockingPoolTask(); + ASSERT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(root_path.AppendASCII(directory_entry->base_name()), + moved_file_path); + + // Add a new file to the directory. + DriveFile* initial_file_entry = resource_metadata.CreateDriveFile(); + initial_file_entry->set_resource_id("file:file_resource_id"); + initial_file_entry->set_title("file"); + initial_file_entry->SetBaseNameFromTitle(); + error = DRIVE_FILE_ERROR_FAILED; + moved_file_path.clear(); + resource_metadata.MoveEntryToDirectory( + directory_entry->GetFilePath(), + initial_file_entry, + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, + &moved_file_path)); + test_util::RunBlockingPoolTask(); + ASSERT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(directory_entry->GetFilePath().AppendASCII( + initial_file_entry->base_name()), moved_file_path); + + ASSERT_EQ(directory_entry, initial_file_entry->parent()); + + // Initial file system state set, let's try refreshing entries. + + // New value for the entry with resource id "file:file_resource_id". + DriveFile* new_file_entry = resource_metadata.CreateDriveFile(); + new_file_entry->set_resource_id("file:file_resource_id"); + resource_metadata.RefreshFile(scoped_ptr<DriveFile>(new_file_entry).Pass()); + // Root should have |new_file_entry|, not |initial_file_entry|. + // If this is not true, |new_file_entry| has probably been destroyed, hence + // ASSERT (we're trying to access |new_file_entry| later on). + ASSERT_EQ(new_file_entry, + resource_metadata.GetEntryByResourceId("file:file_resource_id")); + // We have just verified new_file_entry exists inside root, so accessing + // |new_file_entry->parent()| should be safe. + EXPECT_EQ(directory_entry, new_file_entry->parent()); + + // Let's try refreshing file that didn't prviously exist. + DriveFile* non_existent_entry = resource_metadata.CreateDriveFile(); + non_existent_entry->set_resource_id("file:does_not_exist"); + resource_metadata.RefreshFile( + scoped_ptr<DriveFile>(non_existent_entry).Pass()); + // File with non existent resource id should not be added. + EXPECT_FALSE(resource_metadata.GetEntryByResourceId("file:does_not_exist")); +} + TEST(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { DriveResourceMetadata resource_metadata; // Look up the root directory by its resource ID. diff --git a/chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.cc b/chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.cc index 671082d37..f3d1055 100644 --- a/chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.cc +++ b/chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.cc @@ -512,8 +512,6 @@ void GDataWapiFeedLoader::SearchFromServer( const std::string& search_query, const GURL& next_feed, const LoadDocumentFeedCallback& feed_load_callback) { - DCHECK(!feed_load_callback.is_null()); - LoadFeedParams params(initial_origin, feed_load_callback); params.search_query = search_query; params.feed_to_load = next_feed; diff --git a/chrome/browser/chromeos/gdata/gdata_wapi_parser.cc b/chrome/browser/chromeos/gdata/gdata_wapi_parser.cc index 3310faa..5cf40b4 100644 --- a/chrome/browser/chromeos/gdata/gdata_wapi_parser.cc +++ b/chrome/browser/chromeos/gdata/gdata_wapi_parser.cc @@ -985,10 +985,6 @@ bool DocumentFeed::GetNextFeedURL(GURL* url) { return false; } -void DocumentFeed::ReleaseEntries(std::vector<DocumentEntry*>* entries) { - entries_.release(entries); -} - //////////////////////////////////////////////////////////////////////////////// // InstalledApp implementation diff --git a/chrome/browser/chromeos/gdata/gdata_wapi_parser.h b/chrome/browser/chromeos/gdata/gdata_wapi_parser.h index 4751afe..03599eb 100644 --- a/chrome/browser/chromeos/gdata/gdata_wapi_parser.h +++ b/chrome/browser/chromeos/gdata/gdata_wapi_parser.h @@ -545,10 +545,6 @@ class DocumentFeed : public FeedEntry { // List of document entries. const ScopedVector<DocumentEntry>& entries() const { return entries_; } - // Releases entries_ into |entries|. This is a transfer of ownership, so the - // caller is responsible for deleting the elements of |entries|. - void ReleaseEntries(std::vector<DocumentEntry*>* entries); - // Start index of the document entry list. int start_index() const { return start_index_; } |