Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / sync / entry_update_performer.cc
index 7fa3c9e..3cbbb89 100644 (file)
@@ -9,13 +9,15 @@
 #include "chrome/browser/chromeos/drive/change_list_loader.h"
 #include "chrome/browser/chromeos/drive/drive.pb.h"
 #include "chrome/browser/chromeos/drive/file_cache.h"
-#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
+#include "chrome/browser/chromeos/drive/file_change.h"
+#include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
 #include "chrome/browser/chromeos/drive/file_system_util.h"
 #include "chrome/browser/chromeos/drive/job_scheduler.h"
 #include "chrome/browser/chromeos/drive/resource_metadata.h"
 #include "chrome/browser/chromeos/drive/sync/entry_revert_performer.h"
 #include "chrome/browser/chromeos/drive/sync/remove_performer.h"
 #include "content/public/browser/browser_thread.h"
+#include "google_apis/drive/drive_api_parser.h"
 
 using content::BrowserThread;
 
@@ -50,14 +52,13 @@ FileError PrepareUpdate(ResourceMetadata* metadata,
   if (error != FILE_ERROR_OK)
     return error;
 
-  local_state->drive_file_path = metadata->GetFilePath(local_id);
-  if (local_state->drive_file_path.empty())
-    return FILE_ERROR_NOT_FOUND;
+  error = metadata->GetFilePath(local_id, &local_state->drive_file_path);
+  if (error != FILE_ERROR_OK)
+    return error;
 
-  FileCacheEntry cache_entry;
-  cache->GetCacheEntry(local_id, &cache_entry);
   if (!local_state->entry.file_info().is_directory() &&
-      !cache_entry.is_present() && local_state->entry.resource_id().empty()) {
+      !local_state->entry.file_specific_info().cache_state().is_present() &&
+      local_state->entry.resource_id().empty()) {
     // Locally created file with no cache file, store an empty file.
     base::FilePath empty_file;
     if (!base::CreateTemporaryFile(&empty_file))
@@ -66,22 +67,26 @@ FileError PrepareUpdate(ResourceMetadata* metadata,
                          FileCache::FILE_OPERATION_MOVE);
     if (error != FILE_ERROR_OK)
       return error;
-    if (!cache->GetCacheEntry(local_id, &cache_entry))
-      return FILE_ERROR_NOT_FOUND;
+    error = metadata->GetResourceEntryById(local_id, &local_state->entry);
+    if (error != FILE_ERROR_OK)
+      return error;
   }
 
   // Check if content update is needed or not.
-  if (cache_entry.is_dirty() && !cache->IsOpenedForWrite(local_id)) {
+  if (local_state->entry.file_specific_info().cache_state().is_dirty() &&
+      !cache->IsOpenedForWrite(local_id)) {
     // Update cache entry's MD5 if needed.
-    if (cache_entry.md5().empty()) {
+    if (local_state->entry.file_specific_info().cache_state().md5().empty()) {
       error = cache->UpdateMd5(local_id);
       if (error != FILE_ERROR_OK)
         return error;
-      if (!cache->GetCacheEntry(local_id, &cache_entry))
-        return FILE_ERROR_NOT_FOUND;
+      error = metadata->GetResourceEntryById(local_id, &local_state->entry);
+      if (error != FILE_ERROR_OK)
+        return error;
     }
 
-    if (cache_entry.md5() == local_state->entry.file_specific_info().md5()) {
+    if (local_state->entry.file_specific_info().cache_state().md5() ==
+        local_state->entry.file_specific_info().md5()) {
       error = cache->ClearDirty(local_id);
       if (error != FILE_ERROR_OK)
         return error;
@@ -113,22 +118,30 @@ FileError PrepareUpdate(ResourceMetadata* metadata,
 FileError FinishUpdate(ResourceMetadata* metadata,
                        FileCache* cache,
                        const std::string& local_id,
-                       scoped_ptr<google_apis::ResourceEntry> resource_entry,
-                       base::FilePath* changed_directory) {
+                       scoped_ptr<google_apis::FileResource> file_resource,
+                       FileChange* changed_files) {
+  ResourceEntry entry;
+  FileError error = metadata->GetResourceEntryById(local_id, &entry);
+  if (error != FILE_ERROR_OK)
+    return error;
+
   // When creating new entries, update check may add a new entry with the same
   // resource ID before us. If such an entry exists, remove it.
   std::string existing_local_id;
-  FileError error = metadata->GetIdByResourceId(
-      resource_entry->resource_id(), &existing_local_id);
+  error =
+      metadata->GetIdByResourceId(file_resource->file_id(), &existing_local_id);
+
   switch (error) {
     case FILE_ERROR_OK:
       if (existing_local_id != local_id) {
-        base::FilePath existing_entry_path =
-            metadata->GetFilePath(existing_local_id);
+        base::FilePath existing_entry_path;
+        error = metadata->GetFilePath(existing_local_id, &existing_entry_path);
+        if (error != FILE_ERROR_OK)
+          return error;
         error = metadata->RemoveEntry(existing_local_id);
         if (error != FILE_ERROR_OK)
           return error;
-        *changed_directory = existing_entry_path.DirName();
+        changed_files->Update(existing_entry_path, entry, FileChange::DELETE);
       }
       break;
     case FILE_ERROR_NOT_FOUND:
@@ -137,11 +150,6 @@ FileError FinishUpdate(ResourceMetadata* metadata,
       return error;
   }
 
-  ResourceEntry entry;
-  error = metadata->GetResourceEntryById(local_id, &entry);
-  if (error != FILE_ERROR_OK)
-    return error;
-
   // Update metadata_edit_state and MD5.
   switch (entry.metadata_edit_state()) {
     case ResourceEntry::CLEAN:  // Nothing to do.
@@ -153,16 +161,21 @@ FileError FinishUpdate(ResourceMetadata* metadata,
       break;
   }
   if (!entry.file_info().is_directory())
-    entry.mutable_file_specific_info()->set_md5(resource_entry->file_md5());
-  entry.set_resource_id(resource_entry->resource_id());
+    entry.mutable_file_specific_info()->set_md5(file_resource->md5_checksum());
+  entry.set_resource_id(file_resource->file_id());
   error = metadata->RefreshEntry(entry);
   if (error != FILE_ERROR_OK)
     return error;
+  base::FilePath entry_path;
+  error = metadata->GetFilePath(local_id, &entry_path);
+  if (error != FILE_ERROR_OK)
+    return error;
+  changed_files->Update(entry_path, entry, FileChange::ADD_OR_UPDATE);
 
   // Clear dirty bit unless the file has been edited during update.
-  FileCacheEntry cache_entry;
-  if (cache->GetCacheEntry(local_id, &cache_entry) &&
-      cache_entry.md5() == entry.file_specific_info().md5()) {
+  if (entry.file_specific_info().cache_state().is_dirty() &&
+      entry.file_specific_info().cache_state().md5() ==
+      entry.file_specific_info().md5()) {
     error = cache->ClearDirty(local_id);
     if (error != FILE_ERROR_OK)
       return error;
@@ -174,23 +187,23 @@ FileError FinishUpdate(ResourceMetadata* metadata,
 
 EntryUpdatePerformer::EntryUpdatePerformer(
     base::SequencedTaskRunner* blocking_task_runner,
-    file_system::OperationObserver* observer,
+    file_system::OperationDelegate* delegate,
     JobScheduler* scheduler,
     ResourceMetadata* metadata,
     FileCache* cache,
     LoaderController* loader_controller)
     : blocking_task_runner_(blocking_task_runner),
-      observer_(observer),
+      delegate_(delegate),
       scheduler_(scheduler),
       metadata_(metadata),
       cache_(cache),
       loader_controller_(loader_controller),
       remove_performer_(new RemovePerformer(blocking_task_runner,
-                                            observer,
+                                            delegate,
                                             scheduler,
                                             metadata)),
       entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner,
-                                                       observer,
+                                                       delegate,
                                                        scheduler,
                                                        metadata)),
       weak_ptr_factory_(this) {
@@ -346,7 +359,7 @@ void EntryUpdatePerformer::UpdateEntryAfterUpdateResource(
     const std::string& local_id,
     scoped_ptr<base::ScopedClosureRunner> loader_lock,
     google_apis::GDataErrorCode status,
-    scoped_ptr<google_apis::ResourceEntry> resource_entry) {
+    scoped_ptr<google_apis::FileResource> entry) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   DCHECK(!callback.is_null());
 
@@ -362,27 +375,30 @@ void EntryUpdatePerformer::UpdateEntryAfterUpdateResource(
     return;
   }
 
-  base::FilePath* changed_directory = new base::FilePath;
+  FileChange* changed_files = new FileChange;
   base::PostTaskAndReplyWithResult(
       blocking_task_runner_.get(),
       FROM_HERE,
       base::Bind(&FinishUpdate,
-                 metadata_, cache_, local_id, base::Passed(&resource_entry),
-                 changed_directory),
+                 metadata_,
+                 cache_,
+                 local_id,
+                 base::Passed(&entry),
+                 changed_files),
       base::Bind(&EntryUpdatePerformer::UpdateEntryAfterFinish,
-                 weak_ptr_factory_.GetWeakPtr(), callback,
-                 base::Owned(changed_directory)));
+                 weak_ptr_factory_.GetWeakPtr(),
+                 callback,
+                 base::Owned(changed_files)));
 }
 
 void EntryUpdatePerformer::UpdateEntryAfterFinish(
     const FileOperationCallback& callback,
-    const base::FilePath* changed_directory,
+    const FileChange* changed_files,
     FileError error) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   DCHECK(!callback.is_null());
 
-  if (!changed_directory->empty())
-    observer_->OnDirectoryChangedByOperation(*changed_directory);
+  delegate_->OnFileChangedByOperation(*changed_files);
   callback.Run(error);
 }