Skip to content

Commit

Permalink
Implement GetOrigins for indexeddb quota
Browse files Browse the repository at this point in the history
BUG=83652
TEST=llvm/Debug/unit_tests --gtest_filter=IndexedDBQuotaClientTest.* && llvm/Debug/browser_tests --gtest_filter=IndexedDBBrowser*:BrowsingDataIndexedDBHelperTest.*

Review URL: http://codereview.chromium.org/7310022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91796 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dgrogan@chromium.org committed Jul 8, 2011
1 parent 354a4d6 commit 98bc449
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
21 changes: 19 additions & 2 deletions content/browser/in_process_webkit/indexed_db_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ IndexedDBContext::IndexedDBContext(
special_storage_policy_(special_storage_policy) {
data_path_ = webkit_context->data_path().Append(kIndexedDBDirectory);
if (quota_manager_proxy) {
// quota_manager_proxy->RegisterClient(
// new IndexedDBQuotaClient(webkit_thread_loop, this));
quota_manager_proxy->RegisterClient(
new IndexedDBQuotaClient(webkit_thread_loop, this));
}
}

Expand Down Expand Up @@ -118,3 +118,20 @@ bool IndexedDBContext::IsUnlimitedStorageGranted(
const GURL& origin) const {
return special_storage_policy_->IsStorageUnlimited(origin);
}

// TODO(dgrogan): Merge this code with the similar loop in
// BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread.
void IndexedDBContext::GetAllOriginIdentifiers(
std::vector<string16>* origin_ids) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT));
file_util::FileEnumerator file_enumerator(data_path_,
false, file_util::FileEnumerator::DIRECTORIES);
for (FilePath file_path = file_enumerator.Next(); !file_path.empty();
file_path = file_enumerator.Next()) {
if (file_path.Extension() == IndexedDBContext::kIndexedDBExtension) {
WebKit::WebString origin_id_webstring =
webkit_glue::FilePathToWebString(file_path.BaseName());
origin_ids->push_back(origin_id_webstring);
}
}
}
2 changes: 2 additions & 0 deletions content/browser/in_process_webkit/indexed_db_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class IndexedDBContext : public base::RefCountedThreadSafe<IndexedDBContext> {
// Does a particular origin get unlimited storage?
bool IsUnlimitedStorageGranted(const GURL& origin) const;

void GetAllOriginIdentifiers(std::vector<string16>* origin_ids);

#ifdef UNIT_TEST
// For unit tests allow to override the |data_path_|.
void set_data_path(const FilePath& data_path) { data_path_ = data_path; }
Expand Down
10 changes: 9 additions & 1 deletion content/browser/in_process_webkit/indexed_db_quota_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ class IndexedDBQuotaClient::GetOriginsTaskBase : public HelperTask {
virtual bool ShouldAddOrigin(const GURL& origin) = 0;

virtual void RunOnTargetThread() OVERRIDE {
// TODO(dgrogan): Implement.
std::vector<string16> origin_identifiers;
indexed_db_context_->GetAllOriginIdentifiers(&origin_identifiers);
for (std::vector<string16>::const_iterator iter =
origin_identifiers.begin();
iter != origin_identifiers.end(); ++iter) {
GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
if (ShouldAddOrigin(origin))
origins_.insert(origin);
}
}

std::set<GURL> origins_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class IndexedDBQuotaClientTest : public testing::Test {
public:
const GURL kOriginA;
const GURL kOriginB;
const GURL kOriginOther;

IndexedDBQuotaClientTest()
: kOriginA("http://host"),
kOriginB("http://host:8000"),
kOriginOther("http://other"),
usage_(0),
callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
message_loop_(MessageLoop::TYPE_IO),
Expand Down Expand Up @@ -68,6 +70,29 @@ class IndexedDBQuotaClientTest : public testing::Test {
return usage_;
}

const std::set<GURL>& GetOriginsForType(
quota::QuotaClient* client,
quota::StorageType type) {
origins_.clear();
client->GetOriginsForType(type,
callback_factory_.NewCallback(
&IndexedDBQuotaClientTest::OnGetOriginsComplete));
MessageLoop::current()->RunAllPending();
return origins_;
}

const std::set<GURL>& GetOriginsForHost(
quota::QuotaClient* client,
quota::StorageType type,
const std::string& host) {
origins_.clear();
client->GetOriginsForHost(type, host,
callback_factory_.NewCallback(
&IndexedDBQuotaClientTest::OnGetOriginsComplete));
MessageLoop::current()->RunAllPending();
return origins_;
}

IndexedDBContext* idb_context() { return idb_context_.get(); }

void SetFileSizeTo(const FilePath& path, int size) {
Expand All @@ -91,8 +116,13 @@ class IndexedDBQuotaClientTest : public testing::Test {
usage_ = usage;
}

void OnGetOriginsComplete(const std::set<GURL>& origins) {
origins_ = origins;
}

ScopedTempDir temp_dir_;
int64 usage_;
std::set<GURL> origins_;
scoped_refptr<IndexedDBContext> idb_context_;
base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_;
MessageLoop message_loop_;
Expand All @@ -118,3 +148,45 @@ TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
}

TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::CreateForCurrentThread(),
idb_context());

EXPECT_EQ(kOriginA.host(), kOriginB.host());
EXPECT_NE(kOriginA.host(), kOriginOther.host());

std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_TRUE(origins.empty());

AddFakeIndexedDB(kOriginA, 1000);
origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_EQ(origins.size(), 1ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());

AddFakeIndexedDB(kOriginB, 1000);
origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_EQ(origins.size(), 2ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());
EXPECT_TRUE(origins.find(kOriginB) != origins.end());

EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty());
EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty());
}

TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::CreateForCurrentThread(),
idb_context());

EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty());
EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());

AddFakeIndexedDB(kOriginA, 1000);
std::set<GURL> origins = GetOriginsForType(&client, kTemp);
EXPECT_EQ(origins.size(), 1ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());

EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
}

0 comments on commit 98bc449

Please sign in to comment.