Commit cd236439 authored by satorux@chromium.org's avatar satorux@chromium.org

gdata: Add GDataCache::GetResourceIdsOfAllFiles()

As a good citizen, not only write a unit test for the new function,
but also for the exsisting similar functions lacking unit tests.

BUG=135328
TEST=out/Release/unit_tests --gtest_filter=GDataCacheTest.GetResourceIdsOf*

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148774 0039d316-1c4b-4281-b951-d872f2087c98
parent a3b059af
...@@ -228,6 +228,15 @@ void CollectExistingPinnedFile(std::vector<std::string>* resource_ids, ...@@ -228,6 +228,15 @@ void CollectExistingPinnedFile(std::vector<std::string>* resource_ids,
resource_ids->push_back(resource_id); resource_ids->push_back(resource_id);
} }
// Appends |resource_id| ID to |resource_ids| unconditionally.
void CollectAnyFile(std::vector<std::string>* resource_ids,
const std::string& resource_id,
const GDataCacheEntry& /* cache_entry */) {
DCHECK(resource_ids);
resource_ids->push_back(resource_id);
}
// Runs callback with pointers dereferenced. // Runs callback with pointers dereferenced.
// Used to implement SetMountedStateOnUIThread. // Used to implement SetMountedStateOnUIThread.
void RunSetMountedStateCallback(const SetMountedStateCallback& callback, void RunSetMountedStateCallback(const SetMountedStateCallback& callback,
...@@ -433,6 +442,21 @@ void GDataCache::GetResourceIdsOfExistingPinnedFilesOnUIThread( ...@@ -433,6 +442,21 @@ void GDataCache::GetResourceIdsOfExistingPinnedFilesOnUIThread(
base::Owned(resource_ids))); base::Owned(resource_ids)));
} }
void GDataCache::GetResourceIdsOfAllFilesOnUIThread(
const GetResourceIdsCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
std::vector<std::string>* resource_ids = new std::vector<std::string>;
blocking_task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(&GDataCache::GetResourceIdsOfAllFiles,
base::Unretained(this),
resource_ids),
base::Bind(&RunGetResourceIdsCallback,
callback,
base::Owned(resource_ids)));
}
void GDataCache::FreeDiskSpaceIfNeededFor(int64 num_bytes, void GDataCache::FreeDiskSpaceIfNeededFor(int64 num_bytes,
bool* has_enough_space) { bool* has_enough_space) {
AssertOnSequencedWorkerPool(); AssertOnSequencedWorkerPool();
...@@ -741,6 +765,14 @@ void GDataCache::GetResourceIdsOfExistingPinnedFiles( ...@@ -741,6 +765,14 @@ void GDataCache::GetResourceIdsOfExistingPinnedFiles(
metadata_->Iterate(base::Bind(&CollectExistingPinnedFile, resource_ids)); metadata_->Iterate(base::Bind(&CollectExistingPinnedFile, resource_ids));
} }
void GDataCache::GetResourceIdsOfAllFiles(
std::vector<std::string>* resource_ids) {
AssertOnSequencedWorkerPool();
DCHECK(resource_ids);
metadata_->Iterate(base::Bind(&CollectAnyFile, resource_ids));
}
void GDataCache::GetFile(const std::string& resource_id, void GDataCache::GetFile(const std::string& resource_id,
const std::string& md5, const std::string& md5,
GDataFileError* error, GDataFileError* error,
......
...@@ -168,12 +168,19 @@ class GDataCache { ...@@ -168,12 +168,19 @@ class GDataCache {
void GetResourceIdsOfBacklogOnUIThread( void GetResourceIdsOfBacklogOnUIThread(
const GetResourceIdsOfBacklogCallback& callback); const GetResourceIdsOfBacklogCallback& callback);
// Gets the resource IDs of all pinned files, including pinned dirty files. // Gets the resource IDs of all exsiting (i.e. cached locally) pinned
// files, including pinned dirty files.
// //
// Must be called on UI thread. |callback| is run on UI thread. // Must be called on UI thread. |callback| is run on UI thread.
void GetResourceIdsOfExistingPinnedFilesOnUIThread( void GetResourceIdsOfExistingPinnedFilesOnUIThread(
const GetResourceIdsCallback& callback); const GetResourceIdsCallback& callback);
// Gets the resource IDs of all files in the cache.
//
// Must be called on UI thread. |callback| is run on UI thread.
void GetResourceIdsOfAllFilesOnUIThread(
const GetResourceIdsCallback& callback);
// Frees up disk space to store the given number of bytes, while keeping // Frees up disk space to store the given number of bytes, while keeping
// kMinFreSpace bytes on the disk, if needed. |has_enough_space| is // kMinFreSpace bytes on the disk, if needed. |has_enough_space| is
// updated to indicate if we have enough space. // updated to indicate if we have enough space.
...@@ -331,6 +338,10 @@ class GDataCache { ...@@ -331,6 +338,10 @@ class GDataCache {
void GetResourceIdsOfExistingPinnedFiles( void GetResourceIdsOfExistingPinnedFiles(
std::vector<std::string>* resource_ids); std::vector<std::string>* resource_ids);
// Used to implement GetResourceIdsOfAllFilesOnUIThread.
void GetResourceIdsOfAllFiles(
std::vector<std::string>* resource_ids);
// Used to implement GetFileOnUIThread. // Used to implement GetFileOnUIThread.
void GetFile(const std::string& resource_id, void GetFile(const std::string& resource_id,
const std::string& md5, const std::string& md5,
......
...@@ -98,6 +98,21 @@ class MockFreeDiskSpaceGetter : public FreeDiskSpaceGetterInterface { ...@@ -98,6 +98,21 @@ class MockFreeDiskSpaceGetter : public FreeDiskSpaceGetterInterface {
MOCK_CONST_METHOD0(AmountOfFreeDiskSpace, int64()); MOCK_CONST_METHOD0(AmountOfFreeDiskSpace, int64());
}; };
// Copies results from GetResourceIdsOfBacklogCallback.
void OnGetResourceIdsOfBacklog(std::vector<std::string>* out_to_fetch,
std::vector<std::string>* out_to_upload,
const std::vector<std::string>& to_fetch,
const std::vector<std::string>& to_upload) {
*out_to_fetch = to_fetch;
*out_to_upload = to_upload;
}
// Copies results from GetResourceIdsCallback.
void OnGetResourceIds(std::vector<std::string>* out_resource_ids,
const std::vector<std::string>& resource_ids) {
*out_resource_ids = resource_ids;
}
} // namespace } // namespace
class GDataCacheTest : public testing::Test { class GDataCacheTest : public testing::Test {
...@@ -1395,4 +1410,55 @@ TEST_F(GDataCacheTest, MountUnmount) { ...@@ -1395,4 +1410,55 @@ TEST_F(GDataCacheTest, MountUnmount) {
EXPECT_EQ(1, num_callback_invocations_); EXPECT_EQ(1, num_callback_invocations_);
} }
TEST_F(GDataCacheTest, GetResourceIdsOfBacklogOnUIThread) {
PrepareForInitCacheTest();
std::vector<std::string> to_fetch;
std::vector<std::string> to_upload;
cache_->GetResourceIdsOfBacklogOnUIThread(
base::Bind(&OnGetResourceIdsOfBacklog, &to_fetch, &to_upload));
test_util::RunBlockingPoolTask();
sort(to_fetch.begin(), to_fetch.end());
ASSERT_EQ(1U, to_fetch.size());
EXPECT_EQ("pinned:non-existent", to_fetch[0]);
sort(to_upload.begin(), to_upload.end());
ASSERT_EQ(2U, to_upload.size());
EXPECT_EQ("dirty:existing", to_upload[0]);
EXPECT_EQ("dirty_and_pinned:existing", to_upload[1]);
}
TEST_F(GDataCacheTest, GetResourceIdsOfExistingPinnedFilesOnUIThread) {
PrepareForInitCacheTest();
std::vector<std::string> resource_ids;
cache_->GetResourceIdsOfExistingPinnedFilesOnUIThread(
base::Bind(&OnGetResourceIds, &resource_ids));
test_util::RunBlockingPoolTask();
sort(resource_ids.begin(), resource_ids.end());
ASSERT_EQ(2U, resource_ids.size());
EXPECT_EQ("dirty_and_pinned:existing", resource_ids[0]);
EXPECT_EQ("pinned:existing", resource_ids[1]);
}
TEST_F(GDataCacheTest, GetResourceIdsOfAllFilesOnUIThread) {
PrepareForInitCacheTest();
std::vector<std::string> resource_ids;
cache_->GetResourceIdsOfAllFilesOnUIThread(
base::Bind(&OnGetResourceIds, &resource_ids));
test_util::RunBlockingPoolTask();
sort(resource_ids.begin(), resource_ids.end());
ASSERT_EQ(6U, resource_ids.size());
EXPECT_EQ("dirty:existing", resource_ids[0]);
EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]);
EXPECT_EQ("pinned:existing", resource_ids[2]);
EXPECT_EQ("pinned:non-existent", resource_ids[3]);
EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]);
EXPECT_EQ("tmp:resource_id", resource_ids[5]);
}
} // namespace gdata } // namespace gdata
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment