| OLD | NEW |
| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 context_ = new DomStorageContext(temp_dir_.path(), | 44 context_ = new DomStorageContext(temp_dir_.path(), |
| 45 FilePath(), | 45 FilePath(), |
| 46 storage_policy_, | 46 storage_policy_, |
| 47 task_runner_); | 47 task_runner_); |
| 48 } | 48 } |
| 49 | 49 |
| 50 virtual void TearDown() { | 50 virtual void TearDown() { |
| 51 MessageLoop::current()->RunAllPending(); | 51 MessageLoop::current()->RunAllPending(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void RemoveDataSince(const base::Time& cutoff, |
| 55 int origin_set_mask) { |
| 56 // Use a new instance to remove the contexts of temp_dir_. |
| 57 scoped_refptr<DomStorageContext> context = new DomStorageContext( |
| 58 temp_dir_.path(), FilePath(), storage_policy_, NULL); |
| 59 context->DeleteDataModifiedSince(cutoff, origin_set_mask); |
| 60 } |
| 61 |
| 54 void VerifySingleOriginRemains(const GURL& origin) { | 62 void VerifySingleOriginRemains(const GURL& origin) { |
| 55 // Use a new instance to examine the contexts of temp_dir_. | 63 // Use a new instance to examine the contexts of temp_dir_. |
| 56 scoped_refptr<DomStorageContext> context = | 64 scoped_refptr<DomStorageContext> context = |
| 57 new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); | 65 new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); |
| 58 std::vector<DomStorageContext::UsageInfo> infos; | 66 std::vector<DomStorageContext::UsageInfo> infos; |
| 59 context->GetUsageInfo(&infos, kDontIncludeFileInfo); | 67 context->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 60 EXPECT_EQ(1u, infos.size()); | 68 EXPECT_EQ(1u, infos.size()); |
| 61 EXPECT_EQ(origin, infos[0].origin); | 69 if (infos.size() > 0) |
| 70 EXPECT_EQ(origin, infos[0].origin); |
| 62 } | 71 } |
| 63 | 72 |
| 64 protected: | 73 protected: |
| 65 ScopedTempDir temp_dir_; | 74 ScopedTempDir temp_dir_; |
| 66 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_; | 75 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_; |
| 67 scoped_refptr<MockDomStorageTaskRunner> task_runner_; | 76 scoped_refptr<MockDomStorageTaskRunner> task_runner_; |
| 68 scoped_refptr<DomStorageContext> context_; | 77 scoped_refptr<DomStorageContext> context_; |
| 69 DISALLOW_COPY_AND_ASSIGN(DomStorageContextTest); | 78 DISALLOW_COPY_AND_ASSIGN(DomStorageContextTest); |
| 70 }; | 79 }; |
| 71 | 80 |
| 72 TEST_F(DomStorageContextTest, Basics) { | 81 TEST_F(DomStorageContextTest, Basics) { |
| 73 // This test doesn't do much, checks that the constructor | 82 // This test doesn't do much, checks that the constructor |
| 74 // initializes members properly and that invoking methods | 83 // initializes members properly and that invoking methods |
| 75 // on a newly created object w/o any data on disk do no harm. | 84 // on a newly created object w/o any data on disk do no harm. |
| 76 EXPECT_EQ(temp_dir_.path(), context_->localstorage_directory()); | 85 EXPECT_EQ(temp_dir_.path(), context_->localstorage_directory()); |
| 77 EXPECT_EQ(FilePath(), context_->sessionstorage_directory()); | 86 EXPECT_EQ(FilePath(), context_->sessionstorage_directory()); |
| 78 EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); | 87 EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); |
| 79 context_->PurgeMemory(); | 88 context_->PurgeMemory(); |
| 80 context_->DeleteOrigin(GURL("http://chromium.org/")); | 89 context_->DeleteOrigin(GURL("http://chromium.org/")); |
| 81 context_->DeleteDataModifiedSince(base::Time::Now()); | 90 context_->DeleteDataModifiedSince(base::Time::Now(), false); |
| 82 const int kFirstSessionStorageNamespaceId = 1; | 91 const int kFirstSessionStorageNamespaceId = 1; |
| 83 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); | 92 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); |
| 84 EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); | 93 EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); |
| 85 EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); | 94 EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); |
| 86 std::vector<DomStorageContext::UsageInfo> infos; | 95 std::vector<DomStorageContext::UsageInfo> infos; |
| 87 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); | 96 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 88 EXPECT_TRUE(infos.empty()); | 97 EXPECT_TRUE(infos.empty()); |
| 89 context_->Shutdown(); | 98 context_->Shutdown(); |
| 90 } | 99 } |
| 91 | 100 |
| 101 TEST_F(DomStorageContextTest, RemoveUnprotectedOriginData) { |
| 102 const GURL kProtectedOrigin("http://www.protected.com/"); |
| 103 storage_policy_->AddProtected(kProtectedOrigin); |
| 104 |
| 105 // Store data for a normal and a protected origin, then remove data since |
| 106 // the epoch. |
| 107 NullableString16 old_value; |
| 108 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 109 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 110 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 111 OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value)); |
| 112 context_ = NULL; |
| 113 MessageLoop::current()->RunAllPending(); |
| 114 |
| 115 RemoveDataSince(base::Time(), quota::SpecialStoragePolicy::UNPROTECTED_WEB); |
| 116 VerifySingleOriginRemains(kProtectedOrigin); |
| 117 } |
| 118 |
| 119 TEST_F(DomStorageContextTest, RemoveNonextensionOriginData) { |
| 120 const GURL kProtectedOrigin("http://www.protected.com/"); |
| 121 storage_policy_->AddProtected(kProtectedOrigin); |
| 122 const GURL kExtensionOrigin("chrome-extension://abcdefghijklmnopqrstuvwxyz/"); |
| 123 storage_policy_->AddProtected(kExtensionOrigin); |
| 124 |
| 125 // Store data for a normal origin, a protected origin, and an extension, then |
| 126 // remove data since the epoch. |
| 127 NullableString16 old_value; |
| 128 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 129 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 130 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 131 OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value)); |
| 132 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 133 OpenStorageArea(kExtensionOrigin)->SetItem(kKey, kValue, &old_value)); |
| 134 context_ = NULL; |
| 135 MessageLoop::current()->RunAllPending(); |
| 136 |
| 137 RemoveDataSince(base::Time(), |
| 138 quota::SpecialStoragePolicy::UNPROTECTED_WEB | |
| 139 quota::SpecialStoragePolicy::PROTECTED_WEB); |
| 140 VerifySingleOriginRemains(kExtensionOrigin); |
| 141 } |
| 142 |
| 143 TEST_F(DomStorageContextTest, RemoveProtectedOriginData) { |
| 144 const GURL kProtectedOrigin("http://www.protected.com/"); |
| 145 storage_policy_->AddProtected(kProtectedOrigin); |
| 146 |
| 147 // Store data for a normal and a protected origin, then remove data since |
| 148 // the epoch. |
| 149 NullableString16 old_value; |
| 150 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 151 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 152 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 153 OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value)); |
| 154 context_ = NULL; |
| 155 MessageLoop::current()->RunAllPending(); |
| 156 |
| 157 RemoveDataSince(base::Time(), |
| 158 quota::SpecialStoragePolicy::UNPROTECTED_WEB | |
| 159 quota::SpecialStoragePolicy::PROTECTED_WEB); |
| 160 |
| 161 // Create a new context that points to the same directory, see that |
| 162 // both origins have been removed. |
| 163 std::vector<DomStorageContext::UsageInfo> infos; |
| 164 context_ = new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); |
| 165 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 166 EXPECT_TRUE(infos.empty()); |
| 167 } |
| 168 |
| 92 TEST_F(DomStorageContextTest, UsageInfo) { | 169 TEST_F(DomStorageContextTest, UsageInfo) { |
| 93 // Should be empty initially | 170 // Should be empty initially |
| 94 std::vector<DomStorageContext::UsageInfo> infos; | 171 std::vector<DomStorageContext::UsageInfo> infos; |
| 95 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); | 172 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 96 EXPECT_TRUE(infos.empty()); | 173 EXPECT_TRUE(infos.empty()); |
| 97 context_->GetUsageInfo(&infos, kDoIncludeFileInfo); | 174 context_->GetUsageInfo(&infos, kDoIncludeFileInfo); |
| 98 EXPECT_TRUE(infos.empty()); | 175 EXPECT_TRUE(infos.empty()); |
| 99 | 176 |
| 100 // Put some data into local storage and shutdown the context | 177 // Put some data into local storage and shutdown the context |
| 101 // to ensure data is written to disk. | 178 // to ensure data is written to disk. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 context_->SetClearLocalState(true); | 250 context_->SetClearLocalState(true); |
| 174 context_->SaveSessionState(); // Should override clear behavior. | 251 context_->SaveSessionState(); // Should override clear behavior. |
| 175 context_->Shutdown(); | 252 context_->Shutdown(); |
| 176 context_ = NULL; | 253 context_ = NULL; |
| 177 MessageLoop::current()->RunAllPending(); | 254 MessageLoop::current()->RunAllPending(); |
| 178 | 255 |
| 179 VerifySingleOriginRemains(kSessionOnlyOrigin); | 256 VerifySingleOriginRemains(kSessionOnlyOrigin); |
| 180 } | 257 } |
| 181 | 258 |
| 182 } // namespace dom_storage | 259 } // namespace dom_storage |
| OLD | NEW |