| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 weak_factory_(this), | 41 weak_factory_(this), |
| 42 message_loop_(base::MessageLoop::TYPE_IO), | 42 message_loop_(base::MessageLoop::TYPE_IO), |
| 43 db_thread_(BrowserThread::DB, &message_loop_), | 43 db_thread_(BrowserThread::DB, &message_loop_), |
| 44 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), | 44 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), |
| 45 file_thread_(BrowserThread::FILE, &message_loop_), | 45 file_thread_(BrowserThread::FILE, &message_loop_), |
| 46 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | 46 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, |
| 47 &message_loop_), | 47 &message_loop_), |
| 48 io_thread_(BrowserThread::IO, &message_loop_) { | 48 io_thread_(BrowserThread::IO, &message_loop_) { |
| 49 browser_context_.reset(new TestBrowserContext()); | 49 browser_context_.reset(new TestBrowserContext()); |
| 50 idb_context_ = static_cast<IndexedDBContextImpl*>( | 50 idb_context_ = static_cast<IndexedDBContextImpl*>( |
| 51 BrowserContext::GetDefaultStoragePartition(browser_context_.get())-> | 51 BrowserContext::GetDefaultStoragePartition(browser_context_.get()) |
| 52 GetIndexedDBContext()); | 52 ->GetIndexedDBContext()); |
| 53 message_loop_.RunUntilIdle(); | 53 message_loop_.RunUntilIdle(); |
| 54 setup_temp_dir(); | 54 setup_temp_dir(); |
| 55 } | 55 } |
| 56 void setup_temp_dir() { | 56 void setup_temp_dir() { |
| 57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 58 base::FilePath indexeddb_dir = temp_dir_.path().Append( | 58 base::FilePath indexeddb_dir = |
| 59 IndexedDBContextImpl::kIndexedDBDirectory); | 59 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); |
| 60 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 60 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
| 61 idb_context()->set_data_path_for_testing(indexeddb_dir); | 61 idb_context()->set_data_path_for_testing(indexeddb_dir); |
| 62 } | 62 } |
| 63 | 63 |
| 64 virtual ~IndexedDBQuotaClientTest() { | 64 virtual ~IndexedDBQuotaClientTest() { |
| 65 // IndexedDBContext needs to be destructed on | 65 // IndexedDBContext needs to be destructed on |
| 66 // BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this | 66 // BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this |
| 67 // class. Cause IndexedDBContext's destruction now to ensure that it | 67 // class. Cause IndexedDBContext's destruction now to ensure that it |
| 68 // doesn't outlive BrowserThread::WEBKIT_DEPRECATED. | 68 // doesn't outlive BrowserThread::WEBKIT_DEPRECATED. |
| 69 idb_context_ = NULL; | 69 idb_context_ = NULL; |
| 70 browser_context_.reset(); | 70 browser_context_.reset(); |
| 71 base::MessageLoop::current()->RunUntilIdle(); | 71 base::MessageLoop::current()->RunUntilIdle(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 int64 GetOriginUsage( | 74 int64 GetOriginUsage(quota::QuotaClient* client, |
| 75 quota::QuotaClient* client, | 75 const GURL& origin, |
| 76 const GURL& origin, | 76 quota::StorageType type) { |
| 77 quota::StorageType type) { | |
| 78 usage_ = -1; | 77 usage_ = -1; |
| 79 client->GetOriginUsage( | 78 client->GetOriginUsage( |
| 80 origin, type, | 79 origin, |
| 80 type, |
| 81 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, | 81 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
| 82 weak_factory_.GetWeakPtr())); | 82 weak_factory_.GetWeakPtr())); |
| 83 base::MessageLoop::current()->RunUntilIdle(); | 83 base::MessageLoop::current()->RunUntilIdle(); |
| 84 EXPECT_GT(usage_, -1); | 84 EXPECT_GT(usage_, -1); |
| 85 return usage_; | 85 return usage_; |
| 86 } | 86 } |
| 87 | 87 |
| 88 const std::set<GURL>& GetOriginsForType( | 88 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, |
| 89 quota::QuotaClient* client, | 89 quota::StorageType type) { |
| 90 quota::StorageType type) { | |
| 91 origins_.clear(); | 90 origins_.clear(); |
| 92 client->GetOriginsForType( | 91 client->GetOriginsForType( |
| 93 type, | 92 type, |
| 94 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 93 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 95 weak_factory_.GetWeakPtr())); | 94 weak_factory_.GetWeakPtr())); |
| 96 base::MessageLoop::current()->RunUntilIdle(); | 95 base::MessageLoop::current()->RunUntilIdle(); |
| 97 return origins_; | 96 return origins_; |
| 98 } | 97 } |
| 99 | 98 |
| 100 const std::set<GURL>& GetOriginsForHost( | 99 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, |
| 101 quota::QuotaClient* client, | 100 quota::StorageType type, |
| 102 quota::StorageType type, | 101 const std::string& host) { |
| 103 const std::string& host) { | |
| 104 origins_.clear(); | 102 origins_.clear(); |
| 105 client->GetOriginsForHost( | 103 client->GetOriginsForHost( |
| 106 type, host, | 104 type, |
| 105 host, |
| 107 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 106 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 108 weak_factory_.GetWeakPtr())); | 107 weak_factory_.GetWeakPtr())); |
| 109 base::MessageLoop::current()->RunUntilIdle(); | 108 base::MessageLoop::current()->RunUntilIdle(); |
| 110 return origins_; | 109 return origins_; |
| 111 } | 110 } |
| 112 | 111 |
| 113 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 112 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 114 const GURL& origin_url) { | 113 const GURL& origin_url) { |
| 115 delete_status_ = quota::kQuotaStatusUnknown; | 114 delete_status_ = quota::kQuotaStatusUnknown; |
| 116 client->DeleteOriginData( | 115 client->DeleteOriginData( |
| 117 origin_url, kTemp, | 116 origin_url, |
| 117 kTemp, |
| 118 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, | 118 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
| 119 weak_factory_.GetWeakPtr())); | 119 weak_factory_.GetWeakPtr())); |
| 120 base::MessageLoop::current()->RunUntilIdle(); | 120 base::MessageLoop::current()->RunUntilIdle(); |
| 121 return delete_status_; | 121 return delete_status_; |
| 122 } | 122 } |
| 123 | 123 |
| 124 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } | 124 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } |
| 125 | 125 |
| 126 void SetFileSizeTo(const base::FilePath& path, int size) { | 126 void SetFileSizeTo(const base::FilePath& path, int size) { |
| 127 std::string junk(size, 'a'); | 127 std::string junk(size, 'a'); |
| 128 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 128 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void AddFakeIndexedDB(const GURL& origin, int size) { | 131 void AddFakeIndexedDB(const GURL& origin, int size) { |
| 132 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( | 132 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( |
| 133 webkit_base::GetOriginIdentifierFromURL(origin)); | 133 webkit_base::GetOriginIdentifierFromURL(origin)); |
| 134 if (!file_util::CreateDirectory(file_path_origin)) { | 134 if (!file_util::CreateDirectory(file_path_origin)) { |
| 135 LOG(ERROR) << "failed to file_util::CreateDirectory " | 135 LOG(ERROR) << "failed to file_util::CreateDirectory " |
| 136 << file_path_origin.value(); | 136 << file_path_origin.value(); |
| 137 } | 137 } |
| 138 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 138 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
| 139 SetFileSizeTo(file_path_origin, size); | 139 SetFileSizeTo(file_path_origin, size); |
| 140 idb_context()->ResetCaches(); | 140 idb_context()->ResetCaches(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 private: | 143 private: |
| 144 void OnGetOriginUsageComplete(int64 usage) { | 144 void OnGetOriginUsageComplete(int64 usage) { usage_ = usage; } |
| 145 usage_ = usage; | |
| 146 } | |
| 147 | 145 |
| 148 void OnGetOriginsComplete(const std::set<GURL>& origins) { | 146 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
| 149 origins_ = origins; | 147 origins_ = origins; |
| 150 } | 148 } |
| 151 | 149 |
| 152 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 150 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 153 delete_status_ = code; | 151 delete_status_ = code; |
| 154 } | 152 } |
| 155 | 153 |
| 156 base::ScopedTempDir temp_dir_; | 154 base::ScopedTempDir temp_dir_; |
| 157 int64 usage_; | 155 int64 usage_; |
| 158 std::set<GURL> origins_; | 156 std::set<GURL> origins_; |
| 159 scoped_refptr<IndexedDBContextImpl> idb_context_; | 157 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 160 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; | 158 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
| 161 base::MessageLoop message_loop_; | 159 base::MessageLoop message_loop_; |
| 162 BrowserThreadImpl db_thread_; | 160 BrowserThreadImpl db_thread_; |
| 163 BrowserThreadImpl webkit_thread_; | 161 BrowserThreadImpl webkit_thread_; |
| 164 BrowserThreadImpl file_thread_; | 162 BrowserThreadImpl file_thread_; |
| 165 BrowserThreadImpl file_user_blocking_thread_; | 163 BrowserThreadImpl file_user_blocking_thread_; |
| 166 BrowserThreadImpl io_thread_; | 164 BrowserThreadImpl io_thread_; |
| 167 scoped_ptr<TestBrowserContext> browser_context_; | 165 scoped_ptr<TestBrowserContext> browser_context_; |
| 168 quota::QuotaStatusCode delete_status_; | 166 quota::QuotaStatusCode delete_status_; |
| 169 }; | 167 }; |
| 170 | 168 |
| 171 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 169 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 172 IndexedDBQuotaClient client( | 170 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); |
| 173 base::MessageLoopProxy::current(), | |
| 174 idb_context()); | |
| 175 | 171 |
| 176 AddFakeIndexedDB(kOriginA, 6); | 172 AddFakeIndexedDB(kOriginA, 6); |
| 177 AddFakeIndexedDB(kOriginB, 3); | 173 AddFakeIndexedDB(kOriginB, 3); |
| 178 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | 174 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
| 179 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 175 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 180 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 176 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 181 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 177 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 182 | 178 |
| 183 AddFakeIndexedDB(kOriginA, 1000); | 179 AddFakeIndexedDB(kOriginA, 1000); |
| 184 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 180 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 185 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 181 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 186 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 182 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 187 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 183 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 188 } | 184 } |
| 189 | 185 |
| 190 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { | 186 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { |
| 191 IndexedDBQuotaClient client( | 187 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); |
| 192 base::MessageLoopProxy::current(), | |
| 193 idb_context()); | |
| 194 | 188 |
| 195 EXPECT_EQ(kOriginA.host(), kOriginB.host()); | 189 EXPECT_EQ(kOriginA.host(), kOriginB.host()); |
| 196 EXPECT_NE(kOriginA.host(), kOriginOther.host()); | 190 EXPECT_NE(kOriginA.host(), kOriginOther.host()); |
| 197 | 191 |
| 198 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 192 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 199 EXPECT_TRUE(origins.empty()); | 193 EXPECT_TRUE(origins.empty()); |
| 200 | 194 |
| 201 AddFakeIndexedDB(kOriginA, 1000); | 195 AddFakeIndexedDB(kOriginA, 1000); |
| 202 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 196 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 203 EXPECT_EQ(origins.size(), 1ul); | 197 EXPECT_EQ(origins.size(), 1ul); |
| 204 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 198 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 205 | 199 |
| 206 AddFakeIndexedDB(kOriginB, 1000); | 200 AddFakeIndexedDB(kOriginB, 1000); |
| 207 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 201 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 208 EXPECT_EQ(origins.size(), 2ul); | 202 EXPECT_EQ(origins.size(), 2ul); |
| 209 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 203 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 210 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); | 204 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); |
| 211 | 205 |
| 212 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); | 206 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); |
| 213 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); | 207 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); |
| 214 } | 208 } |
| 215 | 209 |
| 216 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { | 210 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { |
| 217 IndexedDBQuotaClient client( | 211 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); |
| 218 base::MessageLoopProxy::current(), | |
| 219 idb_context()); | |
| 220 | 212 |
| 221 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 213 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
| 222 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 214 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 223 | 215 |
| 224 AddFakeIndexedDB(kOriginA, 1000); | 216 AddFakeIndexedDB(kOriginA, 1000); |
| 225 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 217 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
| 226 EXPECT_EQ(origins.size(), 1ul); | 218 EXPECT_EQ(origins.size(), 1ul); |
| 227 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 219 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 228 | 220 |
| 229 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 221 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 230 } | 222 } |
| 231 | 223 |
| 232 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { | 224 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 233 IndexedDBQuotaClient client( | 225 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); |
| 234 base::MessageLoopProxy::current(), | |
| 235 idb_context()); | |
| 236 | 226 |
| 237 AddFakeIndexedDB(kOriginA, 1000); | 227 AddFakeIndexedDB(kOriginA, 1000); |
| 238 AddFakeIndexedDB(kOriginB, 50); | 228 AddFakeIndexedDB(kOriginB, 50); |
| 239 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 229 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 240 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 230 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 241 | 231 |
| 242 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 232 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 243 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 233 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 244 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 234 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 245 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 235 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 246 } | 236 } |
| 247 | 237 |
| 248 } // namespace content | 238 } // namespace content |
| OLD | NEW |