gdata: Add GDataCache::OnCacheCommitted event.

The event will be used to upload dirty files.

BUG=127080
TEST=add expectations to gdata_file_system_unittest.cc

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142784 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/gdata/gdata_cache.cc b/chrome/browser/chromeos/gdata/gdata_cache.cc
index 32539f3..9c07325 100644
--- a/chrome/browser/chromeos/gdata/gdata_cache.cc
+++ b/chrome/browser/chromeos/gdata/gdata_cache.cc
@@ -593,11 +593,12 @@
                  md5,
                  GDataCache::FILE_OPERATION_MOVE,
                  error),
-      base::Bind(&RunCacheOperationCallback,
-                 callback,
+      base::Bind(&GDataCache::OnCommitDirty,
+                 ui_weak_ptr_,
                  base::Owned(error),
                  resource_id,
-                 md5));
+                 md5,
+                 callback));
 }
 
 void GDataCache::ClearDirtyOnUIThread(const std::string& resource_id,
@@ -1397,6 +1398,20 @@
                  base::Owned(has_enough_space)));
 }
 
+void GDataCache::OnCommitDirty(base::PlatformFileError* error,
+                               const std::string& resource_id,
+                               const std::string& md5,
+                               const CacheOperationCallback& callback) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK(error);
+
+  if (!callback.is_null())
+    callback.Run(*error, resource_id, md5);
+
+  if (*error == base::PLATFORM_FILE_OK)
+    FOR_EACH_OBSERVER(Observer, observers_, OnCacheCommitted(resource_id));
+}
+
 // static
 FilePath GDataCache::GetCacheRootPath(Profile* profile) {
   FilePath cache_base_path;
diff --git a/chrome/browser/chromeos/gdata/gdata_cache.h b/chrome/browser/chromeos/gdata/gdata_cache.h
index 20328552..90d59cd2 100644
--- a/chrome/browser/chromeos/gdata/gdata_cache.h
+++ b/chrome/browser/chromeos/gdata/gdata_cache.h
@@ -103,6 +103,9 @@
     virtual void OnCacheUnpinned(const std::string& resource_id,
                                  const std::string& md5) {}
 
+    // Triggered when a dirty file has been committed (saved) successfully.
+    virtual void OnCacheCommitted(const std::string& resource_id) {}
+
    protected:
     virtual ~Observer() {}
   };
@@ -396,6 +399,12 @@
                   const std::string& md5,
                   const CacheOperationCallback& callback);
 
+  // Runs callback and notifies the observers when file is committed.
+  void OnCommitDirty(base::PlatformFileError* error,
+                     const std::string& resource_id,
+                     const std::string& md5,
+                     const CacheOperationCallback& callback);
+
   // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1).
   const FilePath cache_root_path_;
   // Paths for all subdirectories of GCache, one for each
diff --git a/chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc b/chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc
index 0b6974f..db214ba3 100644
--- a/chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc
+++ b/chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc
@@ -2666,6 +2666,7 @@
 TEST_F(GDataFileSystemTest, DirtyCacheSimple) {
   std::string resource_id("pdf:1a2b");
   std::string md5("abcdef0123456789");
+  EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1);
 
   // First store a file to cache.
   TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"),
@@ -2699,6 +2700,7 @@
   std::string resource_id("pdf:1a2b");
   std::string md5("abcdef0123456789");
   EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1);
+  EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1);
 
   // First store a file to cache and pin it.
   TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"),
@@ -2781,6 +2783,7 @@
 TEST_F(GDataFileSystemTest, DirtyCacheRepetitive) {
   std::string resource_id("pdf:1a2b");
   std::string md5("abcdef0123456789");
+  EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(3);
 
   // First store a file to cache.
   TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"),
@@ -2912,6 +2915,7 @@
   std::string resource_id("pdf:1a2b");
   std::string md5("abcdef0123456789");
   EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1);
+  EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1);
 
   // Store a file to cache, pin it, mark it dirty and commit it.
   TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"),
@@ -3852,6 +3856,9 @@
   const std::string file_resource_id = entry->resource_id();
   const std::string file_md5 = file->file_md5();
 
+  // A dirty file is created on close.
+  EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(file_resource_id)).Times(1);
+
   // Pretend we have enough space.
   EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace())
       .Times(2).WillRepeatedly(Return(file_size + kMinFreeSpace));
diff --git a/chrome/browser/chromeos/gdata/gdata_sync_client.cc b/chrome/browser/chromeos/gdata/gdata_sync_client.cc
index f21b784..2536f6c 100644
--- a/chrome/browser/chromeos/gdata/gdata_sync_client.cc
+++ b/chrome/browser/chromeos/gdata/gdata_sync_client.cc
@@ -220,6 +220,13 @@
     queue_.erase(iter);
 }
 
+void GDataSyncClient::OnCacheCommitted(const std::string& resource_id) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+  // TODO(satorux): Initiate uploading of the committed file.
+  // crbug.com/127080
+}
+
 void GDataSyncClient::OnInitialScanComplete(
     const base::Closure& closure,
     std::vector<std::string>* resource_ids) {
diff --git a/chrome/browser/chromeos/gdata/gdata_sync_client.h b/chrome/browser/chromeos/gdata/gdata_sync_client.h
index 7858f84..33ff5c849 100644
--- a/chrome/browser/chromeos/gdata/gdata_sync_client.h
+++ b/chrome/browser/chromeos/gdata/gdata_sync_client.h
@@ -76,6 +76,7 @@
                              const std::string& md5) OVERRIDE;
   virtual void OnCacheUnpinned(const std::string& resource_id,
                                const std::string& md5) OVERRIDE;
+  virtual void OnCacheCommitted(const std::string& resource_id) OVERRIDE;
 
   // Starts scanning the pinned directory in the cache to collect
   // pinned-but-not-fetched files. |closure| is run on the calling thread
diff --git a/chrome/browser/chromeos/gdata/mock_gdata_sync_client.h b/chrome/browser/chromeos/gdata/mock_gdata_sync_client.h
index 2ea030c..ac67674 100644
--- a/chrome/browser/chromeos/gdata/mock_gdata_sync_client.h
+++ b/chrome/browser/chromeos/gdata/mock_gdata_sync_client.h
@@ -32,7 +32,7 @@
                                    const std::string& md5));
   MOCK_METHOD2(OnCacheUnpinned, void(const std::string& resource_id,
                                      const std::string& md5));
-  // GDataSyncClient is not interested in OnDirectoryChanged.
+  MOCK_METHOD1(OnCacheCommitted, void(const std::string& resource_id));
 };
 
 }  // namespace gdata