Commit 6e2d03a3 authored by kkimlabs@chromium.org's avatar kkimlabs@chromium.org

Add GetStoreSizeInBytes() to ImageStore to know the actual db size.

We will limit the enhanced bookmark image store database size
to ensure that the db file size is reasonable. So add a function
to query the actual file size.

BUG=378853

Review URL: https://codereview.chromium.org/305963004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274297 0039d316-1c4b-4281-b951-d872f2087c98
parent a2a75f57
......@@ -8,4 +8,7 @@ specific_include_rules = {
"image_store_unittest\.cc": [
"+third_party/skia"
],
"test_image_store\.cc": [
"+third_party/skia"
],
}
......@@ -52,6 +52,10 @@ class ImageStore {
// Moves an image from one url to another.
void ChangeImageURL(const GURL& from, const GURL& to);
// Returns the saved images storage size in bytes. If the storage doesn't
// exist yet or failed to read, returns -1.
virtual int64 GetStoreSizeInBytes() = 0;
protected:
base::SequenceChecker sequence_checker_;
......
......@@ -5,6 +5,7 @@
#include "components/enhanced_bookmarks/image_store.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_number_conversions.h"
#include "components/enhanced_bookmarks/image_store_util.h"
#include "components/enhanced_bookmarks/persistent_image_store.h"
#include "components/enhanced_bookmarks/test_image_store.h"
......@@ -218,4 +219,33 @@ TYPED_TEST(ImageStoreUnitTest, Persistence) {
}
}
TYPED_TEST(ImageStoreUnitTest, GetSize) {
gfx::Image src_image = GenerateBlackImage();
const GURL url("foo://bar");
const GURL image_url("a.jpg");
int64 size = 0;
if (this->use_persistent_store()) {
// File shouldn't exist before we actually start using it since we do lazy
// initialization.
EXPECT_EQ(this->store_->GetStoreSizeInBytes(), -1);
} else {
EXPECT_LE(this->store_->GetStoreSizeInBytes(), 1024);
}
for (int i = 0; i < 100; ++i) {
this->store_->Insert(
GURL(url.spec() + '/' + base::IntToString(i)), image_url, src_image);
EXPECT_GE(this->store_->GetStoreSizeInBytes(), size);
size = this->store_->GetStoreSizeInBytes();
}
if (this->use_persistent_store()) {
EXPECT_GE(this->store_->GetStoreSizeInBytes(), 100 * 1024); // 100kb
EXPECT_LE(this->store_->GetStoreSizeInBytes(), 200 * 1024); // 200kb
} else {
EXPECT_GE(this->store_->GetStoreSizeInBytes(), 400 * 1024); // 400kb
EXPECT_LE(this->store_->GetStoreSizeInBytes(), 500 * 1024); // 500kb
}
}
} // namespace
......@@ -4,6 +4,7 @@
#include "components/enhanced_bookmarks/persistent_image_store.h"
#include "base/files/file.h"
#include "components/enhanced_bookmarks/image_store_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"
......@@ -197,6 +198,11 @@ void PersistentImageStore::ClearAll() {
statement.Run();
}
int64 PersistentImageStore::GetStoreSizeInBytes() {
base::File file(path_, base::File::FLAG_OPEN | base::File::FLAG_READ);
return file.IsValid() ? file.GetLength() : -1;
}
PersistentImageStore::~PersistentImageStore() {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
}
......
......@@ -26,6 +26,7 @@ class PersistentImageStore : public ImageStore {
virtual gfx::Size GetSize(const GURL& page_url) OVERRIDE;
virtual void GetAllPageUrls(std::set<GURL>* urls) OVERRIDE;
virtual void ClearAll() OVERRIDE;
virtual int64 GetStoreSizeInBytes() OVERRIDE;
protected:
virtual ~PersistentImageStore();
......
......@@ -4,6 +4,7 @@
#include "components/enhanced_bookmarks/test_image_store.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
......@@ -68,6 +69,21 @@ void TestImageStore::ClearAll() {
store_.clear();
}
int64 TestImageStore::GetStoreSizeInBytes() {
// Not 100% accurate, but it's for testing so the actual value is not
// important.
int64 size = sizeof(store_);
for (ImageMap::const_iterator it = store_.begin(); it != store_.end(); ++it) {
size += sizeof(it->first);
size += it->first.spec().length();
size += sizeof(it->second);
SkBitmap bitmap = it->second.first.AsBitmap();
size += bitmap.getSize();
size += it->second.second.spec().length();
}
return size;
}
TestImageStore::~TestImageStore() {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
}
......@@ -22,6 +22,7 @@ class TestImageStore : public ImageStore {
virtual gfx::Size GetSize(const GURL& page_url) OVERRIDE;
virtual void GetAllPageUrls(std::set<GURL>* urls) OVERRIDE;
virtual void ClearAll() OVERRIDE;
virtual int64 GetStoreSizeInBytes() OVERRIDE;
protected:
virtual ~TestImageStore();
......
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