Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(772)

Side by Side Diff: content/browser/indexed_db/indexed_db_unittest.cc

Issue 93873017: IndexedDBFactory now ForceCloses databases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added multimap for more efficient origin->db mapping. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.h"
7 #include "base/test/test_simple_task_runner.h" 7 #include "base/test/test_simple_task_runner.h"
8 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/indexed_db/indexed_db_connection.h" 10 #include "content/browser/indexed_db/indexed_db_connection.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 109 }
110 110
111 // Make sure we wait until the destructor has run. 111 // Make sure we wait until the destructor has run.
112 message_loop_.RunUntilIdle(); 112 message_loop_.RunUntilIdle();
113 113
114 // No data was cleared because of SetForceKeepSessionState. 114 // No data was cleared because of SetForceKeepSessionState.
115 EXPECT_TRUE(base::DirectoryExists(normal_path)); 115 EXPECT_TRUE(base::DirectoryExists(normal_path));
116 EXPECT_TRUE(base::DirectoryExists(session_only_path)); 116 EXPECT_TRUE(base::DirectoryExists(session_only_path));
117 } 117 }
118 118
119 class MockConnection : public IndexedDBConnection { 119 class ForceCloseDBCallbacks : public IndexedDBCallbacks {
120 public: 120 public:
121 explicit MockConnection(bool expect_force_close) 121 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context,
122 : IndexedDBConnection(NULL, NULL), 122 const GURL& origin_url)
123 expect_force_close_(expect_force_close), 123 : IndexedDBCallbacks(NULL, 0, 0),
124 force_close_called_(false) {} 124 idb_context_(idb_context),
125 origin_url_(origin_url),
126 connection_(NULL) {}
125 127
126 virtual ~MockConnection() { 128 virtual void OnSuccess() OVERRIDE {}
127 EXPECT_TRUE(force_close_called_ == expect_force_close_); 129 virtual void OnSuccess(const std::vector<base::string16>&) OVERRIDE {}
130 virtual void OnSuccess(scoped_ptr<IndexedDBConnection> connection,
131 const IndexedDBDatabaseMetadata& metadata) OVERRIDE {
132 connection_ = connection.release();
133 idb_context_->ConnectionOpened(origin_url_, connection_);
128 } 134 }
129 135
130 virtual void ForceClose() OVERRIDE { 136 IndexedDBConnection* connection() { return connection_; }
131 ASSERT_TRUE(expect_force_close_);
132 force_close_called_ = true;
133 }
134 137
135 virtual bool IsConnected() OVERRIDE { 138 protected:
136 return !force_close_called_; 139 virtual ~ForceCloseDBCallbacks() {}
137 }
138 140
139 private: 141 private:
140 bool expect_force_close_; 142 scoped_refptr<IndexedDBContextImpl> idb_context_;
141 bool force_close_called_; 143 GURL origin_url_;
144 IndexedDBConnection* connection_;
145 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks);
142 }; 146 };
143 147
144 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { 148 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
145 base::ScopedTempDir temp_dir; 149 base::ScopedTempDir temp_dir;
146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
147 151
152 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks(
153 new MockIndexedDBDatabaseCallbacks());
154 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks(
155 new MockIndexedDBDatabaseCallbacks());
156
148 base::FilePath test_path; 157 base::FilePath test_path;
149 158
150 // Create the scope which will ensure we run the destructor of the context. 159 // Create the scope which will ensure we run the destructor of the context.
151 { 160 {
152 TestBrowserContext browser_context; 161 TestBrowserContext browser_context;
153 162
154 const GURL kTestOrigin("http://test/"); 163 const GURL kTestOrigin("http://test/");
155 164
156 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 165 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
157 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 166 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
158 167
168 scoped_refptr<ForceCloseDBCallbacks> open_callbacks =
169 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
170
171 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks =
172 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
173
174 IndexedDBFactory* factory = idb_context->GetIDBFactory();
175
159 test_path = idb_context->GetFilePathForTesting( 176 test_path = idb_context->GetFilePathForTesting(
160 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); 177 webkit_database::GetIdentifierFromOrigin(kTestOrigin));
161 ASSERT_TRUE(base::CreateDirectory(test_path));
162 178
163 const bool kExpectForceClose = true; 179 factory->Open(base::ASCIIToUTF16("opendb"),
180 0,
181 0,
182 open_callbacks,
183 open_db_callbacks,
184 kTestOrigin,
185 idb_context->data_path());
186 factory->Open(base::ASCIIToUTF16("closeddb"),
187 0,
188 0,
189 closed_callbacks,
190 closed_db_callbacks,
191 kTestOrigin,
192 idb_context->data_path());
164 193
165 MockConnection connection1(kExpectForceClose); 194 closed_callbacks->connection()->Close();
166 idb_context->TaskRunner()->PostTask(
167 FROM_HERE,
168 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
169 idb_context,
170 kTestOrigin,
171 &connection1));
172
173 MockConnection connection2(!kExpectForceClose);
174 idb_context->TaskRunner()->PostTask(
175 FROM_HERE,
176 base::Bind(&IndexedDBContextImpl::ConnectionOpened,
177 idb_context,
178 kTestOrigin,
179 &connection2));
180 idb_context->TaskRunner()->PostTask(
181 FROM_HERE,
182 base::Bind(&IndexedDBContextImpl::ConnectionClosed,
183 idb_context,
184 kTestOrigin,
185 &connection2));
186 195
187 idb_context->TaskRunner()->PostTask( 196 idb_context->TaskRunner()->PostTask(
188 FROM_HERE, 197 FROM_HERE,
189 base::Bind( 198 base::Bind(
190 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); 199 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin));
191 FlushIndexedDBTaskRunner(); 200 FlushIndexedDBTaskRunner();
192 message_loop_.RunUntilIdle(); 201 message_loop_.RunUntilIdle();
193 } 202 }
194 203
195 // Make sure we wait until the destructor has run. 204 // Make sure we wait until the destructor has run.
196 message_loop_.RunUntilIdle(); 205 message_loop_.RunUntilIdle();
197 206
207 EXPECT_TRUE(open_db_callbacks->forced_close_called());
208 EXPECT_FALSE(closed_db_callbacks->forced_close_called());
198 EXPECT_FALSE(base::DirectoryExists(test_path)); 209 EXPECT_FALSE(base::DirectoryExists(test_path));
199 } 210 }
200 211
201 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { 212 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) {
202 base::ScopedTempDir temp_dir; 213 base::ScopedTempDir temp_dir;
203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 214 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
204 const GURL kTestOrigin("http://test/"); 215 const GURL kTestOrigin("http://test/");
205 216
206 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 217 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
207 temp_dir.path(), special_storage_policy_, NULL, task_runner_); 218 temp_dir.path(), special_storage_policy_, NULL, task_runner_);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); 265 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin));
255 266
256 // Simulate the write failure. 267 // Simulate the write failure.
257 callbacks->connection()->database()->TransactionCommitFailed(); 268 callbacks->connection()->database()->TransactionCommitFailed();
258 269
259 EXPECT_TRUE(db_callbacks->forced_close_called()); 270 EXPECT_TRUE(db_callbacks->forced_close_called());
260 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); 271 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin));
261 } 272 }
262 273
263 } // namespace content 274 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698