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

Side by Side Diff: content/browser/loader/resource_dispatcher_host_unittest.cc

Issue 2503813002: Fix and refactor downloaded file handling in the loading stack (Closed)
Patch Set: +RunUntilIdle Created 4 years, 1 month 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 10 matching lines...) Expand all
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h" 22 #include "base/strings/string_split.h"
23 #include "base/test/scoped_feature_list.h" 23 #include "base/test/scoped_feature_list.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
25 #include "content/browser/browser_thread_impl.h" 25 #include "content/browser/browser_thread_impl.h"
26 #include "content/browser/child_process_security_policy_impl.h" 26 #include "content/browser/child_process_security_policy_impl.h"
27 #include "content/browser/download/download_manager_impl.h" 27 #include "content/browser/download/download_manager_impl.h"
28 #include "content/browser/download/download_resource_handler.h" 28 #include "content/browser/download/download_resource_handler.h"
29 #include "content/browser/frame_host/navigation_request_info.h" 29 #include "content/browser/frame_host/navigation_request_info.h"
30 #include "content/browser/loader/detachable_resource_handler.h" 30 #include "content/browser/loader/detachable_resource_handler.h"
31 #include "content/browser/loader/downloaded_temp_file_impl.h"
31 #include "content/browser/loader/navigation_resource_throttle.h" 32 #include "content/browser/loader/navigation_resource_throttle.h"
32 #include "content/browser/loader/navigation_url_loader.h" 33 #include "content/browser/loader/navigation_url_loader.h"
33 #include "content/browser/loader/resource_dispatcher_host_impl.h" 34 #include "content/browser/loader/resource_dispatcher_host_impl.h"
34 #include "content/browser/loader/resource_loader.h" 35 #include "content/browser/loader/resource_loader.h"
35 #include "content/browser/loader/resource_message_filter.h" 36 #include "content/browser/loader/resource_message_filter.h"
36 #include "content/browser/loader/resource_request_info_impl.h" 37 #include "content/browser/loader/resource_request_info_impl.h"
37 #include "content/browser/loader_delegate_impl.h" 38 #include "content/browser/loader_delegate_impl.h"
38 #include "content/common/appcache_interfaces.h" 39 #include "content/common/appcache_interfaces.h"
39 #include "content/common/child_process_host_impl.h" 40 #include "content/common/child_process_host_impl.h"
40 #include "content/common/navigation_params.h" 41 #include "content/common/navigation_params.h"
(...skipping 3327 matching lines...) Expand 10 before | Expand all | Expand 10 after
3368 scoped_refptr<ShareableFileReference> deletable_file = 3369 scoped_refptr<ShareableFileReference> deletable_file =
3369 ShareableFileReference::GetOrCreate( 3370 ShareableFileReference::GetOrCreate(
3370 file_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, 3371 file_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE,
3371 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get()); 3372 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get());
3372 3373
3373 // Not readable. 3374 // Not readable.
3374 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 3375 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3375 filter_->child_id(), file_path)); 3376 filter_->child_id(), file_path));
3376 3377
3377 // Register it for a resource request. 3378 // Register it for a resource request.
3378 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); 3379 host_.RegisterDownloadedTempFile(base::MakeUnique<DownloadedTempFileImpl>(
3380 &host_, filter_->child_id(), kRequestID, file_path));
3379 3381
3380 // Should be readable now. 3382 // Should be readable now.
3381 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 3383 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3382 filter_->child_id(), file_path)); 3384 filter_->child_id(), file_path));
3383 3385
3384 // The child releases from the request. 3386 // The child releases from the request.
3385 ResourceHostMsg_ReleaseDownloadedFile release_msg(kRequestID); 3387 ResourceHostMsg_ReleaseDownloadedFile release_msg(kRequestID);
3386 host_.OnMessageReceived(release_msg, filter_.get()); 3388 host_.OnMessageReceived(release_msg, filter_.get());
3387 3389
3388 // Still readable because there is another reference to the file. (The child 3390 // Still readable because there is another reference to the file. (The child
3389 // may take additional blob references.) 3391 // may take additional blob references.)
3390 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 3392 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3391 filter_->child_id(), file_path)); 3393 filter_->child_id(), file_path));
3392 3394
3393 // Release extra references and wait for the file to be deleted. (This relies 3395 // Release extra references and wait for the file to be deleted. (This relies
3394 // on the delete happening on the FILE thread which is mapped to main thread 3396 // on the delete happening on the FILE thread which is mapped to main thread
3395 // in this test.) 3397 // in this test.)
3396 deletable_file = NULL; 3398 deletable_file = NULL;
3397 base::RunLoop().RunUntilIdle(); 3399 base::RunLoop().RunUntilIdle();
3398 3400
3399 // The file is no longer readable to the child and has been deleted. 3401 // The file is no longer readable to the child and has been deleted.
3400 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 3402 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3401 filter_->child_id(), file_path)); 3403 filter_->child_id(), file_path));
3402 EXPECT_FALSE(base::PathExists(file_path)); 3404 EXPECT_FALSE(base::PathExists(file_path));
3403 } 3405 }
3404 3406
3407 // Tests the dispatcher host's temporary file management in the mojo-enabled
3408 // loading.
3409 TEST_P(ResourceDispatcherHostTest, RegisterDownloadedTempFileWithMojo) {
3410 const int kRequestID = 1;
3411
3412 // Create a temporary file.
3413 base::FilePath file_path;
3414 ASSERT_TRUE(base::CreateTemporaryFile(&file_path));
3415 scoped_refptr<ShareableFileReference> deletable_file =
3416 ShareableFileReference::GetOrCreate(
3417 file_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE,
3418 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get());
3419
3420 // Not readable.
3421 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3422 filter_->child_id(), file_path));
3423
3424 // Register it for a resource request.
3425 auto downloaded_file = base::MakeUnique<DownloadedTempFileImpl>(
3426 &host_, filter_->child_id(), kRequestID, file_path);
3427 mojom::DownloadedTempFilePtr downloaded_file_ptr =
3428 downloaded_file->CreateInterfacePtrAndBind();
3429 host_.RegisterDownloadedTempFile(std::move(downloaded_file));
3430
3431 // Should be readable now.
3432 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3433 filter_->child_id(), file_path));
3434
3435 // The child releases from the request.
3436 downloaded_file_ptr = nullptr;
3437 base::RunLoop().RunUntilIdle();
3438
3439 // Still readable because there is another reference to the file. (The child
3440 // may take additional blob references.)
3441 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3442 filter_->child_id(), file_path));
3443
3444 // Release extra references and wait for the file to be deleted. (This relies
3445 // on the delete happening on the FILE thread which is mapped to main thread
3446 // in this test.)
3447 deletable_file = NULL;
dcheng 2016/11/17 11:16:02 Nit: nullptr
tzik 2016/11/22 13:54:45 Done.
3448 base::RunLoop().RunUntilIdle();
3449
3450 // The file is no longer readable to the child and has been deleted.
3451 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3452 filter_->child_id(), file_path));
3453 EXPECT_FALSE(base::PathExists(file_path));
3454 }
3455
3405 // Tests that temporary files held on behalf of child processes are released 3456 // Tests that temporary files held on behalf of child processes are released
3406 // when the child process dies. 3457 // when the child process dies.
3407 TEST_P(ResourceDispatcherHostTest, ReleaseTemporiesOnProcessExit) { 3458 TEST_P(ResourceDispatcherHostTest, ReleaseTemporiesOnProcessExit) {
3408 const int kRequestID = 1; 3459 const int kRequestID = 1;
3409 3460
3410 // Create a temporary file. 3461 // Create a temporary file.
3411 base::FilePath file_path; 3462 base::FilePath file_path;
3412 ASSERT_TRUE(base::CreateTemporaryFile(&file_path)); 3463 ASSERT_TRUE(base::CreateTemporaryFile(&file_path));
3413 scoped_refptr<ShareableFileReference> deletable_file = 3464 scoped_refptr<ShareableFileReference> deletable_file =
3414 ShareableFileReference::GetOrCreate( 3465 ShareableFileReference::GetOrCreate(
3415 file_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, 3466 file_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE,
3416 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get()); 3467 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get());
3417 3468
3418 // Register it for a resource request. 3469 // Register it for a resource request.
3419 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path); 3470 host_.RegisterDownloadedTempFile(base::MakeUnique<DownloadedTempFileImpl>(
3471 &host_, filter_->child_id(), kRequestID, file_path));
3420 deletable_file = NULL; 3472 deletable_file = NULL;
3421 3473
3422 // Should be readable now. 3474 // Should be readable now.
3423 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 3475 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
3424 filter_->child_id(), file_path)); 3476 filter_->child_id(), file_path));
3425 3477
3426 // Let the process die. 3478 // Let the process die.
3427 filter_->OnChannelClosing(); 3479 filter_->OnChannelClosing();
3428 base::RunLoop().RunUntilIdle(); 3480 base::RunLoop().RunUntilIdle();
3429 3481
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
3935 return nullptr; 3987 return nullptr;
3936 } 3988 }
3937 3989
3938 INSTANTIATE_TEST_CASE_P( 3990 INSTANTIATE_TEST_CASE_P(
3939 ResourceDispatcherHostTests, 3991 ResourceDispatcherHostTests,
3940 ResourceDispatcherHostTest, 3992 ResourceDispatcherHostTest,
3941 testing::Values(TestConfig::kDefault, 3993 testing::Values(TestConfig::kDefault,
3942 TestConfig::kOptimizeIPCForSmallResourceEnabled)); 3994 TestConfig::kOptimizeIPCForSmallResourceEnabled));
3943 3995
3944 } // namespace content 3996 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698