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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
content / browser / file_system_access / file_system_access_file_modification_host_impl_browsertest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/common/shell_switches.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "storage/browser/quota/quota_manager.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
namespace content {
// This browser test implements end-to-end tests for
// FileSystemAccessFileModificationHostImpl.
class FileSystemAccessFileModificationHostImplBrowserTest
: public ContentBrowserTest {
public:
void SetUpOnMainThread() override {
ASSERT_TRUE(embedded_test_server()->Start());
ContentBrowserTest::SetUpOnMainThread();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
command_line->AppendSwitchPath(switches::kContentShellUserDataDir,
temp_dir_.GetPath());
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
}
void RunOnIOThreadBlocking(base::OnceClosure task) {
base::RunLoop run_loop;
content::GetIOThreadTaskRunner({})->PostTaskAndReply(
FROM_HERE, std::move(task), run_loop.QuitClosure());
run_loop.Run();
}
int64_t GetUsageSync(scoped_refptr<storage::QuotaManager> quota_manager,
blink::StorageKey storage_key) {
int64_t current_usage;
base::RunLoop run_loop;
RunOnIOThreadBlocking(base::BindOnce(
&storage::QuotaManager::GetUsageAndQuota, quota_manager, storage_key,
blink::mojom::StorageType::kTemporary,
base::BindLambdaForTesting([&](blink::mojom::QuotaStatusCode result,
int64_t usage, int64_t quota) {
current_usage = usage;
run_loop.Quit();
})));
run_loop.Run();
return current_usage;
}
private:
base::ScopedTempDir temp_dir_;
};
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileModificationHostImplBrowserTest,
QuotaUsageAfterClosing) {
const GURL& test_url =
embedded_test_server()->GetURL("/run_async_code_on_worker.html");
Shell* browser = CreateBrowser();
scoped_refptr<storage::QuotaManager> quota_manager =
browser->web_contents()
->GetBrowserContext()
->GetDefaultStoragePartition()
->GetQuotaManager();
blink::StorageKey storage_key =
blink::StorageKey::CreateFromStringForTesting(test_url.spec());
NavigateToURLBlockUntilNavigationsComplete(browser, test_url,
/*number_of_navigations=*/1);
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: true});
return true;
`);
)")
.ExtractBool());
int64_t usage_before_operation = GetUsageSync(quota_manager, storage_key);
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: false});
let ah = await fh.createSyncAccessHandle();
ah.truncate(100);
ah.truncate(10);
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_after_operation = GetUsageSync(quota_manager, storage_key);
EXPECT_EQ(usage_after_operation, usage_before_operation + 10);
}
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileModificationHostImplBrowserTest,
QuotaUsageAfterForNonemptyFile) {
const GURL& test_url =
embedded_test_server()->GetURL("/run_async_code_on_worker.html");
Shell* browser = CreateBrowser();
scoped_refptr<storage::QuotaManager> quota_manager =
browser->web_contents()
->GetBrowserContext()
->GetDefaultStoragePartition()
->GetQuotaManager();
blink::StorageKey storage_key =
blink::StorageKey::CreateFromStringForTesting(test_url.spec());
NavigateToURLBlockUntilNavigationsComplete(browser, test_url,
/*number_of_navigations=*/1);
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_existing', {create: true});
let ah = await fh.createSyncAccessHandle();
ah.truncate(100);
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_before_operation = GetUsageSync(quota_manager, storage_key);
ReloadBypassingCacheBlockUntilNavigationsComplete(
browser, /*number_of_navigations=*/1);
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_existing', {create: false});
let ah = await fh.createSyncAccessHandle();
ah.truncate(0);
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_after_operation = GetUsageSync(quota_manager, storage_key);
EXPECT_EQ(usage_before_operation, usage_after_operation + 100);
}
// TODO(crbug.com/40826793): Failing on various builders.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
(BUILDFLAG(IS_CHROMEOS_LACROS) && defined(ADDRESS_SANITIZER) && \
defined(LEAK_SANITIZER))
#define MAYBE_QuotaUsageOverallocation DISABLED_QuotaUsageOverallocation
#else
#define MAYBE_QuotaUsageOverallocation QuotaUsageOverallocation
#endif
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileModificationHostImplBrowserTest,
MAYBE_QuotaUsageOverallocation) {
// TODO(crbug.com/40194113): Implement a more sophisticated test suite
// for this feature.
const GURL& test_url =
embedded_test_server()->GetURL("/run_async_code_on_worker.html");
Shell* browser = CreateBrowser();
NavigateToURLBlockUntilNavigationsComplete(browser, test_url,
/*number_of_navigations=*/1);
EXPECT_EQ(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_file_small', {create: true});
let ah = await fh.createSyncAccessHandle();
let storage_manager = await navigator.storage.estimate();
let usage_before_operation = storage_manager.usageDetails.fileSystem;
ah.truncate(100);
storage_manager = await navigator.storage.estimate();
let usage_after_operation = storage_manager.usageDetails.fileSystem;
ah.close();
return usage_after_operation-usage_before_operation;
`);
)")
.ExtractInt(),
1024 * 1024);
EXPECT_EQ(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_file_medium', {create: true});
let ah = await fh.createSyncAccessHandle();
let storage_manager = await navigator.storage.estimate();
let usage_before_operation = storage_manager.usageDetails.fileSystem;
let new_file_size = 3*1024*1024;
ah.truncate(new_file_size);
storage_manager = await navigator.storage.estimate();
let usage_after_operation = storage_manager.usageDetails.fileSystem;
ah.close();
return usage_after_operation-usage_before_operation;
`);
)")
.ExtractInt(),
4 * 1024 * 1024);
}
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileModificationHostImplBrowserTest,
QuotaUsageShrinks) {
const GURL& test_url =
embedded_test_server()->GetURL("/run_async_code_on_worker.html");
Shell* browser = CreateBrowser();
NavigateToURLBlockUntilNavigationsComplete(browser, test_url,
/*number_of_navigations=*/1);
EXPECT_EQ(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_file_medium', {create: true});
let ah = await fh.createSyncAccessHandle();
let storage_manager = await navigator.storage.estimate();
let usage_before_operation = storage_manager.usageDetails.fileSystem;
let new_file_size = 3*1024*1024;
ah.truncate(new_file_size);
storage_manager = await navigator.storage.estimate();
let usage_after_operation = storage_manager.usageDetails.fileSystem;
ah.close();
return usage_after_operation-usage_before_operation;
`);
)")
.ExtractInt(),
4 * 1024 * 1024);
EXPECT_EQ(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_file_small', {create: true});
let ah = await fh.createSyncAccessHandle();
let storage_manager = await navigator.storage.estimate();
let usage_before_operation = storage_manager.usageDetails.fileSystem;
ah.truncate(100);
storage_manager = await navigator.storage.estimate();
let usage_after_operation = storage_manager.usageDetails.fileSystem;
ah.close();
return usage_after_operation-usage_before_operation;
`);
)")
.ExtractInt(),
1024 * 1024);
}
IN_PROC_BROWSER_TEST_F(FileSystemAccessFileModificationHostImplBrowserTest,
QuotaUsageWrite) {
const GURL& test_url =
embedded_test_server()->GetURL("/run_async_code_on_worker.html");
Shell* browser = CreateBrowser();
scoped_refptr<storage::QuotaManager> quota_manager =
browser->web_contents()
->GetBrowserContext()
->GetDefaultStoragePartition()
->GetQuotaManager();
blink::StorageKey storage_key =
blink::StorageKey::CreateFromStringForTesting(test_url.spec());
NavigateToURLBlockUntilNavigationsComplete(browser, test_url,
/*number_of_navigations=*/1);
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: true});
return true;
`);
)")
.ExtractBool());
int64_t usage_before_operation = GetUsageSync(quota_manager, storage_key);
// write() should update quota correctly.
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: false});
let ah = await fh.createSyncAccessHandle();
const buffer = new DataView(new ArrayBuffer(10));
ah.write(buffer, { at: 0 });
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_after_write1 = GetUsageSync(quota_manager, storage_key);
EXPECT_EQ(usage_after_write1, usage_before_operation + 10);
// Write at an offset past the end of the file.
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: false});
let ah = await fh.createSyncAccessHandle();
const buffer = new DataView(new ArrayBuffer(80));
ah.write(buffer, { at: 20 });
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_after_write2 = GetUsageSync(quota_manager, storage_key);
EXPECT_EQ(usage_after_write2, usage_before_operation + 100);
// Writing in bytes the middle of the file so the file size does not change.
EXPECT_TRUE(EvalJs(browser, R"(
runOnWorkerAndWaitForResult(`
let root = await navigator.storage.getDirectory();
let fh = await root.getFileHandle('test_closing', {create: false});
let ah = await fh.createSyncAccessHandle();
const buffer = new DataView(new ArrayBuffer(20));
ah.write(buffer, { at: 5 });
ah.close();
return true;
`);
)")
.ExtractBool());
int64_t usage_after_write3 = GetUsageSync(quota_manager, storage_key);
EXPECT_EQ(usage_after_write3, usage_after_write2);
}
} // namespace content