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
content / browser / web_contents / file_chooser_impl_browsertest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/web_contents/file_chooser_impl.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/content_paths.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/test/content_browser_test_utils_internal.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace content {
using FileChooserImplBrowserTest = ContentBrowserTest;
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest, FileChooserAfterRfhDeath) {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
auto* rfh = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
mojo::Remote<blink::mojom::FileChooser> chooser =
FileChooserImpl::CreateBoundForTesting(rfh);
// Kill the renderer process.
RenderProcessHostWatcher crash_observer(
rfh->GetProcess(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
rfh->GetProcess()->Shutdown(0);
crash_observer.Wait();
auto quit_run_loop = [](base::RunLoop* run_loop,
blink::mojom::FileChooserResultPtr result) {
run_loop->Quit();
};
// Call FileChooser methods. The browser process should not crash.
base::RunLoop run_loop1;
chooser->OpenFileChooser(blink::mojom::FileChooserParams::New(),
base::BindOnce(quit_run_loop, &run_loop1));
run_loop1.Run();
base::RunLoop run_loop2;
chooser->EnumerateChosenDirectory(base::FilePath(),
base::BindOnce(quit_run_loop, &run_loop2));
run_loop2.Run();
// Pass if this didn't crash.
}
class FileChooserImplBrowserTestWebContentsDelegate
: public WebContentsDelegate {
public:
void RunFileChooser(RenderFrameHost* render_frame_host,
scoped_refptr<FileSelectListener> listener,
const blink::mojom::FileChooserParams& params) override {
listener_ = listener;
}
FileSelectListener* listener() { return listener_.get(); }
void ClearListener() { listener_ = nullptr; }
private:
scoped_refptr<FileSelectListener> listener_;
};
// This test makes sure that if we kill the renderer after opening the file
// picker, then the cancelation callback is still called in order to prevent
// the parent renderer from thinking that the file picker is still open in the
// case of an iframe.
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest,
FileChooserCallbackAfterRfhDeathCancel) {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
auto delegate =
std::make_unique<FileChooserImplBrowserTestWebContentsDelegate>();
shell()->web_contents()->SetDelegate(delegate.get());
auto* rfh = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
auto chooser_and_remote = FileChooserImpl::CreateForTesting(rfh);
auto* chooser = chooser_and_remote.first;
auto quit_run_loop = [](base::RunLoop* run_loop,
blink::mojom::FileChooserResultPtr result) {
run_loop->Quit();
};
base::RunLoop run_loop;
chooser->OpenFileChooser(blink::mojom::FileChooserParams::New(),
base::BindOnce(quit_run_loop, &run_loop));
// Kill the renderer process
RenderProcessHostWatcher crash_observer(
rfh->GetProcess(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
rfh->GetProcess()->Shutdown(0);
crash_observer.Wait();
static_cast<FileChooserImpl::FileSelectListenerImpl*>(delegate->listener())
->SetListenerFunctionCalledTrueForTesting();
chooser->FileSelectionCanceled();
// Test passes if this run_loop.Run() returns instead of timing out.
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest, ListenerOutlivingChooser) {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
auto delegate =
std::make_unique<FileChooserImplBrowserTestWebContentsDelegate>();
shell()->web_contents()->SetDelegate(delegate.get());
auto* rfh = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
auto chooser_and_remote = FileChooserImpl::CreateForTesting(rfh);
auto* chooser = chooser_and_remote.first;
chooser->OpenFileChooser(blink::mojom::FileChooserParams::New(),
base::DoNothing());
static_cast<FileChooserImpl::FileSelectListenerImpl*>(delegate->listener())
->SetListenerFunctionCalledTrueForTesting();
// After this, `chooser` will no longer have a reference to the listener.
chooser->FileSelectionCanceled();
// Destroy `chooser`.
chooser_and_remote.second.reset();
// The destruction happens asynchronously after the disconnection.
base::RunLoop await_chooser_destruction_run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, await_chooser_destruction_run_loop.QuitClosure());
await_chooser_destruction_run_loop.Run();
// Clear the last reference to the listener, which will destroy it. It
// should gracefully handle being destroyed after the chooser.
delegate->ClearListener();
}
// Same as FileChooserCallbackAfterRfhDeathCancel but with a file selected from
// the file picker.
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest,
FileChooserCallbackAfterRfhDeathSelected) {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
auto delegate =
std::make_unique<FileChooserImplBrowserTestWebContentsDelegate>();
shell()->web_contents()->SetDelegate(delegate.get());
auto* rfh = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
auto chooser_and_remote = FileChooserImpl::CreateForTesting(rfh);
auto* chooser = chooser_and_remote.first;
auto quit_run_loop = [](base::RunLoop* run_loop,
blink::mojom::FileChooserResultPtr result) {
run_loop->Quit();
};
base::RunLoop run_loop;
chooser->OpenFileChooser(blink::mojom::FileChooserParams::New(),
base::BindOnce(quit_run_loop, &run_loop));
// Kill the renderer process
RenderProcessHostWatcher crash_observer(
rfh->GetProcess(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
rfh->GetProcess()->Shutdown(0);
crash_observer.Wait();
static_cast<FileChooserImpl::FileSelectListenerImpl*>(delegate->listener())
->SetListenerFunctionCalledTrueForTesting();
std::vector<blink::mojom::FileChooserFileInfoPtr> files;
files.emplace_back(blink::mojom::FileChooserFileInfoPtr(nullptr));
chooser->FileSelected(base::FilePath(),
blink::mojom::FileChooserParams::Mode::kOpen,
std::move(files));
// Test passes if this run_loop.Run() returns instead of timing out.
run_loop.Run();
}
// https://crbug.com/1345275
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest, UploadFolderWithSymlink) {
EXPECT_TRUE(NavigateToURL(
shell(), GetTestUrl(".", "file_input_webkitdirectory.html")));
// The folder contains a regular file and a symbolic link.
// When uploading the folder, the symbolic link should be excluded.
base::FilePath dir_test_data;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &dir_test_data));
base::FilePath folder_to_upload =
dir_test_data.AppendASCII("file_chooser").AppendASCII("dir_with_symlink");
base::FilePath text_file = folder_to_upload.AppendASCII("text_file.txt");
base::FilePath symlink_file = folder_to_upload.AppendASCII("symlink");
// Skip the test if symbolic links are not supported.
{
base::ScopedAllowBlockingForTesting allow_blocking;
if (!base::IsLink(symlink_file))
return;
}
std::unique_ptr<FileChooserDelegate> delegate(new FileChooserDelegate(
{text_file, symlink_file}, folder_to_upload, base::OnceClosure()));
shell()->web_contents()->SetDelegate(delegate.get());
EXPECT_TRUE(ExecJs(shell(),
"(async () => {"
" let listener = new Promise("
" resolve => fileinput.onchange = resolve);"
" fileinput.click();"
" await listener;"
"})()"));
EXPECT_EQ(
1, EvalJs(shell(), "document.getElementById('fileinput').files.length;"));
EXPECT_EQ(
"text_file.txt",
EvalJs(shell(), "document.getElementById('fileinput').files[0].name;"));
}
// https://crbug.com/1378997
IN_PROC_BROWSER_TEST_F(FileChooserImplBrowserTest, UploadFolderWithDirSymlink) {
EXPECT_TRUE(NavigateToURL(
shell(), GetTestUrl(".", "file_input_webkitdirectory.html")));
// The folder contains a regular file and a directory symbolic link.
// When uploading the folder, the symbolic link should not be followed.
base::FilePath dir_test_data;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &dir_test_data));
base::FilePath folder_to_upload = dir_test_data.AppendASCII("file_chooser")
.AppendASCII("dir_with_dir_symlink");
base::FilePath foo_file = folder_to_upload.AppendASCII("foo.txt");
base::FilePath dir_symlink = folder_to_upload.AppendASCII("symlink");
base::FilePath bar_file = dir_symlink.AppendASCII("bar.txt");
// Skip the test if symbolic links are not supported.
{
base::ScopedAllowBlockingForTesting allow_blocking;
if (!base::IsLink(dir_symlink))
return;
}
std::unique_ptr<FileChooserDelegate> delegate(new FileChooserDelegate(
{foo_file, bar_file}, folder_to_upload, base::OnceClosure()));
shell()->web_contents()->SetDelegate(delegate.get());
EXPECT_TRUE(ExecJs(shell(),
"(async () => {"
" let listener = new Promise("
" resolve => fileinput.onchange = resolve);"
" fileinput.click();"
" await listener;"
"})()"));
EXPECT_EQ(
1, EvalJs(shell(), "document.getElementById('fileinput').files.length;"));
EXPECT_EQ(
"foo.txt",
EvalJs(shell(), "document.getElementById('fileinput').files[0].name;"));
}
} // namespace content