In DownloadOperation use locally modified file info if dirty Drive cache exists.

BUG=245693

Review URL: https://chromiumcodereview.appspot.com/16278006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203940 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/drive/file_system/download_operation.cc b/chrome/browser/chromeos/drive/file_system/download_operation.cc
index c11457da..e5a1076 100644
--- a/chrome/browser/chromeos/drive/file_system/download_operation.cc
+++ b/chrome/browser/chromeos/drive/file_system/download_operation.cc
@@ -37,31 +37,31 @@
 FileError CheckPreConditionForEnsureFileDownloaded(
     internal::ResourceMetadata* metadata,
     internal::FileCache* cache,
-    const ResourceEntry& entry,
+    ResourceEntry* entry,
     base::FilePath* cache_file_path) {
   DCHECK(metadata);
   DCHECK(cache);
   DCHECK(cache_file_path);
 
-  if (entry.file_info().is_directory())
+  if (entry->file_info().is_directory())
     return FILE_ERROR_NOT_A_FILE;
 
   // The file's entry should have its file specific info.
-  DCHECK(entry.has_file_specific_info());
+  DCHECK(entry->has_file_specific_info());
 
   // For a hosted document, we create a special JSON file to represent the
   // document instead of fetching the document content in one of the exported
   // formats. The JSON file contains the edit URL and resource ID of the
   // document.
-  if (entry.file_specific_info().is_hosted_document()) {
+  if (entry->file_specific_info().is_hosted_document()) {
     base::FilePath gdoc_file_path;
     if (!file_util::CreateTemporaryFileInDir(
             cache->GetCacheDirectoryPath(
                 internal::FileCache::CACHE_TYPE_TMP_DOCUMENTS),
             &gdoc_file_path) ||
         !util::CreateGDocFile(gdoc_file_path,
-                              GURL(entry.file_specific_info().alternate_url()),
-                              entry.resource_id()))
+                              GURL(entry->file_specific_info().alternate_url()),
+                              entry->resource_id()))
       return FILE_ERROR_FAILED;
 
     *cache_file_path = gdoc_file_path;
@@ -69,9 +69,31 @@
   }
 
   // Get the cache file path if available.
-  cache->GetFile(entry.resource_id(),
-                 entry.file_specific_info().file_md5(),
+  cache->GetFile(entry->resource_id(),
+                 entry->file_specific_info().file_md5(),
                  cache_file_path);
+
+  // If the cache file is available and dirty, the modified file info needs to
+  // be stored in |entry|.
+  // TODO(kinaba): crbug.com/246469. The logic below is a duplicate of that in
+  // drive::FileSystem::CheckLocalModificationAndRun. We should merge them once
+  // the drive::FS side is also converted to run fully on blocking pool.
+  if (!cache_file_path->empty()) {
+    FileCacheEntry cache_entry;
+    if (cache->GetCacheEntry(entry->resource_id(),
+                             entry->file_specific_info().file_md5(),
+                             &cache_entry) &&
+        cache_entry.is_dirty()) {
+      base::PlatformFileInfo file_info;
+      if (file_util::GetFileInfo(*cache_file_path, &file_info)) {
+        PlatformFileInfoProto entry_file_info;
+        util::ConvertPlatformFileInfoToResourceEntry(file_info,
+                                                     &entry_file_info);
+        *entry->mutable_file_info() = entry_file_info;
+      }
+    }
+  }
+
   return FILE_ERROR_OK;
 }
 
@@ -87,7 +109,7 @@
   if (error != FILE_ERROR_OK)
     return error;
   return CheckPreConditionForEnsureFileDownloaded(
-      metadata, cache, *entry, cache_file_path);
+      metadata, cache, entry, cache_file_path);
 }
 
 // Calls CheckPreConditionForEnsureFileDownloaded() with the entry specified by
@@ -102,7 +124,7 @@
   if (error != FILE_ERROR_OK)
     return error;
   return CheckPreConditionForEnsureFileDownloaded(
-      metadata, cache, *entry, cache_file_path);
+      metadata, cache, entry, cache_file_path);
 }
 
 // Creates a file with unique name in |dir| and stores the path to |temp_file|.
diff --git a/chrome/browser/chromeos/drive/file_system/download_operation_unittest.cc b/chrome/browser/chromeos/drive/file_system/download_operation_unittest.cc
index b76bf1a..d4bb6bc 100644
--- a/chrome/browser/chromeos/drive/file_system/download_operation_unittest.cc
+++ b/chrome/browser/chromeos/drive/file_system/download_operation_unittest.cc
@@ -389,5 +389,52 @@
   EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
 }
 
+TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_DirtyCache) {
+  base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
+  ResourceEntry src_entry;
+  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
+
+  // Prepare a dirty file to store to cache that has a different size than
+  // stored in resource metadata.
+  base::FilePath dirty_file = temp_dir().AppendASCII("dirty.txt");
+  size_t dirty_size = src_entry.file_info().size() + 10;
+  google_apis::test_util::WriteStringToFile(dirty_file,
+                                            std::string(dirty_size, 'x'));
+
+  // Store the file as a cache, marking it to be dirty.
+  FileError error = FILE_ERROR_FAILED;
+  cache()->StoreLocallyModifiedOnUIThread(
+      src_entry.resource_id(),
+      src_entry.file_specific_info().file_md5(),
+      dirty_file,
+      internal::FileCache::FILE_OPERATION_COPY,
+      google_apis::test_util::CreateCopyResultCallback(&error));
+  google_apis::test_util::RunBlockingPoolTask();
+  EXPECT_EQ(FILE_ERROR_OK, error);
+
+  // Record values passed to GetFileContentInitializedCallback().
+  FileError init_error;
+  base::FilePath init_path;
+  scoped_ptr<ResourceEntry> init_entry;
+  base::Closure cancel_callback;
+
+  base::FilePath file_path;
+  scoped_ptr<ResourceEntry> entry;
+  operation_->EnsureFileDownloadedByPath(
+      file_in_root,
+      ClientContext(USER_INITIATED),
+      google_apis::test_util::CreateCopyResultCallback(
+          &init_error, &init_entry, &init_path, &cancel_callback),
+      google_apis::GetContentCallback(),
+      google_apis::test_util::CreateCopyResultCallback(
+          &error, &file_path, &entry));
+  google_apis::test_util::RunBlockingPoolTask();
+
+  EXPECT_EQ(FILE_ERROR_OK, error);
+  // Check that the result of local modification is propagated.
+  EXPECT_EQ(static_cast<int64>(dirty_size), init_entry->file_info().size());
+  EXPECT_EQ(static_cast<int64>(dirty_size), entry->file_info().size());
+}
+
 }  // namespace file_system
 }  // namespace drive