1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/storage/storage_controller.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/thread_pool.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/modules/storage/storage_namespace.h"
#include "third_party/blink/renderer/modules/storage/testing/fake_area_source.h"
#include "third_party/blink/renderer/modules/storage/testing/mock_storage_area.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/storage/blink_storage_key.h"
#include "third_party/blink/renderer/platform/testing/scoped_mocked_url.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_mojo.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_std.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/uuid.h"
namespace blink {
namespace {
const size_t kTestCacheLimit = 100;
class MockDomStorage : public mojom::blink::DomStorage {
public:
// mojom::blink::DomStorage implementation:
void OpenLocalStorage(
const blink::BlinkStorageKey& storage_key,
const blink::LocalFrameToken& local_frame_token,
mojo::PendingReceiver<mojom::blink::StorageArea> receiver) override {}
void BindSessionStorageNamespace(
const String& namespace_id,
mojo::PendingReceiver<mojom::blink::SessionStorageNamespace> receiver)
override {}
void BindSessionStorageArea(
const blink::BlinkStorageKey& storage_key,
const blink::LocalFrameToken& local_frame_token,
const String& namespace_id,
mojo::PendingReceiver<mojom::blink::StorageArea> receiver) override {
session_storage_opens++;
}
void GetSessionStorageUsage(int32_t* out) const {
*out = session_storage_opens;
}
int32_t session_storage_opens = 0;
};
} // namespace
TEST(StorageControllerTest, CacheLimit) {
const String kKey("key");
const String kValue("value");
const std::string kRootString = "http://dom_storage/page";
const KURL kRootUrl = KURL(kRootString.c_str());
const std::string kPageString = "http://dom_storage1/";
const KURL kPageUrl = KURL(kPageString.c_str());
const std::string kPageString2 = "http://dom_storage2/";
const KURL kPageUrl2 = KURL(kPageString2.c_str());
const std::string kPageString3 = "http://dom_storage3/";
const KURL kPageUrl3 = KURL(kPageString3.c_str());
test::TaskEnvironment task_environment;
test::ScopedMockedURLLoad scoped_mocked_url_load_root(
kRootUrl, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper_root;
LocalDOMWindow* local_dom_window_root =
To<LocalDOMWindow>(web_view_helper_root.InitializeAndLoad(kRootString)
->GetPage()
->MainFrame()
->DomWindow());
Persistent<FakeAreaSource> source_area =
MakeGarbageCollected<FakeAreaSource>(kRootUrl, local_dom_window_root);
StorageController::DomStorageConnection connection;
PostCrossThreadTask(
*base::ThreadPool::CreateSequencedTaskRunner({}), FROM_HERE,
CrossThreadBindOnce(
[](mojo::PendingReceiver<mojom::blink::DomStorage> receiver) {
mojo::MakeSelfOwnedReceiver(std::make_unique<MockDomStorage>(),
std::move(receiver));
},
connection.dom_storage_remote.BindNewPipeAndPassReceiver()));
StorageController controller(std::move(connection), kTestCacheLimit);
test::ScopedMockedURLLoad scoped_mocked_url_load(
kPageUrl, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper;
LocalDOMWindow* local_dom_window =
To<LocalDOMWindow>(web_view_helper.InitializeAndLoad(kPageString)
->GetPage()
->MainFrame()
->DomWindow());
auto cached_area1 = controller.GetLocalStorageArea(local_dom_window);
cached_area1->RegisterSource(source_area);
cached_area1->SetItem(kKey, kValue, source_area);
const auto* area1_ptr = cached_area1.get();
size_t expected_total = (kKey.length() + kValue.length()) * 2;
EXPECT_EQ(expected_total, cached_area1->quota_used());
EXPECT_EQ(expected_total, controller.TotalCacheSize());
cached_area1 = nullptr;
test::ScopedMockedURLLoad scoped_mocked_url_load2(
kPageUrl2, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper2;
LocalDOMWindow* local_dom_window2 =
To<LocalDOMWindow>(web_view_helper2.InitializeAndLoad(kPageString2)
->GetPage()
->MainFrame()
->DomWindow());
auto cached_area2 = controller.GetLocalStorageArea(local_dom_window2);
cached_area2->RegisterSource(source_area);
cached_area2->SetItem(kKey, kValue, source_area);
// Area for local_dom_window should still be alive.
EXPECT_EQ(2 * cached_area2->quota_used(), controller.TotalCacheSize());
EXPECT_EQ(area1_ptr, controller.GetLocalStorageArea(local_dom_window));
test::ScopedMockedURLLoad scoped_mocked_url_load3(
kPageUrl3, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper3;
LocalDOMWindow* local_dom_window3 =
To<LocalDOMWindow>(web_view_helper3.InitializeAndLoad(kPageString3)
->GetPage()
->MainFrame()
->DomWindow());
String long_value(Vector<UChar>(kTestCacheLimit, 'a'));
cached_area2->SetItem(kKey, long_value, source_area);
// Cache is cleared when a new area is opened.
auto cached_area3 = controller.GetLocalStorageArea(local_dom_window3);
EXPECT_EQ(cached_area2->quota_used(), controller.TotalCacheSize());
}
TEST(StorageControllerTest, CacheLimitSessionStorage) {
const String kNamespace1 = WTF::CreateCanonicalUUIDString();
const String kNamespace2 = WTF::CreateCanonicalUUIDString();
const String kKey("key");
const String kValue("value");
const std::string kRootString = "http://dom_storage/page";
const KURL kRootUrl = KURL(kRootString.c_str());
const std::string kPageString = "http://dom_storage1/";
const KURL kPageUrl = KURL(kPageString.c_str());
const std::string kPageString2 = "http://dom_storage2/";
const KURL kPageUrl2 = KURL(kPageString2.c_str());
const std::string kPageString3 = "http://dom_storage3/";
const KURL kPageUrl3 = KURL(kPageString3.c_str());
test::TaskEnvironment task_environment;
test::ScopedMockedURLLoad scoped_mocked_url_load_root(
kRootUrl, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper_root;
LocalDOMWindow* local_dom_window_root =
To<LocalDOMWindow>(web_view_helper_root.InitializeAndLoad(kRootString)
->GetPage()
->MainFrame()
->DomWindow());
Persistent<FakeAreaSource> source_area =
MakeGarbageCollected<FakeAreaSource>(kRootUrl, local_dom_window_root);
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner({});
auto mock_dom_storage = std::make_unique<MockDomStorage>();
MockDomStorage* dom_storage_ptr = mock_dom_storage.get();
StorageController::DomStorageConnection connection;
PostCrossThreadTask(
*task_runner, FROM_HERE,
CrossThreadBindOnce(
[](std::unique_ptr<MockDomStorage> dom_storage_ptr,
mojo::PendingReceiver<mojom::blink::DomStorage> receiver) {
mojo::MakeSelfOwnedReceiver(std::move(dom_storage_ptr),
std::move(receiver));
},
std::move(mock_dom_storage),
connection.dom_storage_remote.BindNewPipeAndPassReceiver()));
StorageController controller(std::move(connection), kTestCacheLimit);
StorageNamespace* ns1 = controller.CreateSessionStorageNamespace(
*local_dom_window_root->GetFrame()->GetPage(), kNamespace1);
StorageNamespace* ns2 = controller.CreateSessionStorageNamespace(
*local_dom_window_root->GetFrame()->GetPage(), kNamespace2);
test::ScopedMockedURLLoad scoped_mocked_url_load(
kPageUrl, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper;
LocalDOMWindow* local_dom_window =
To<LocalDOMWindow>(web_view_helper.InitializeAndLoad(kPageString)
->GetPage()
->MainFrame()
->DomWindow());
auto cached_area1 = ns1->GetCachedArea(local_dom_window);
cached_area1->RegisterSource(source_area);
cached_area1->SetItem(kKey, kValue, source_area);
const auto* area1_ptr = cached_area1.get();
size_t expected_total = (kKey.length() + kValue.length()) * 2;
EXPECT_EQ(expected_total, cached_area1->quota_used());
EXPECT_EQ(expected_total, controller.TotalCacheSize());
cached_area1 = nullptr;
test::ScopedMockedURLLoad scoped_mocked_url_load2(
kPageUrl2, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper2;
LocalDOMWindow* local_dom_window2 =
To<LocalDOMWindow>(web_view_helper2.InitializeAndLoad(kPageString2)
->GetPage()
->MainFrame()
->DomWindow());
auto cached_area2 = ns2->GetCachedArea(local_dom_window2);
cached_area2->RegisterSource(source_area);
cached_area2->SetItem(kKey, kValue, source_area);
// Area for local_dom_window should still be alive.
EXPECT_EQ(2 * cached_area2->quota_used(), controller.TotalCacheSize());
EXPECT_EQ(area1_ptr, ns1->GetCachedArea(local_dom_window));
test::ScopedMockedURLLoad scoped_mocked_url_load3(
kPageUrl3, test::CoreTestDataPath("foo.html"));
frame_test_helpers::WebViewHelper web_view_helper3;
LocalDOMWindow* local_dom_window3 =
To<LocalDOMWindow>(web_view_helper3.InitializeAndLoad(kPageString3)
->GetPage()
->MainFrame()
->DomWindow());
String long_value(Vector<UChar>(kTestCacheLimit, 'a'));
cached_area2->SetItem(kKey, long_value, source_area);
// Cache is cleared when a new area is opened.
auto cached_area3 = ns1->GetCachedArea(local_dom_window3);
EXPECT_EQ(cached_area2->quota_used(), controller.TotalCacheSize());
int32_t opens = 0;
{
base::RunLoop loop;
task_runner->PostTaskAndReply(
FROM_HERE,
base::BindOnce(&MockDomStorage::GetSessionStorageUsage,
base::Unretained(dom_storage_ptr), &opens),
loop.QuitClosure());
loop.Run();
}
EXPECT_EQ(opens, 3);
}
} // namespace blink
|