Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <map> | 5 #include <map> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/guid.h" | 11 #include "base/guid.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "content/browser/background_fetch/background_fetch_context.h" | 17 #include "content/browser/background_fetch/background_fetch_context.h" |
| 16 #include "content/browser/background_fetch/background_fetch_embedded_worker_test _helper.h" | 18 #include "content/browser/background_fetch/background_fetch_embedded_worker_test _helper.h" |
| 17 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 19 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 18 #include "content/browser/background_fetch/background_fetch_service_impl.h" | 20 #include "content/browser/background_fetch/background_fetch_service_impl.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 48 // for which is contained in the |params|. | 50 // for which is contained in the |params|. |
| 49 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { | 51 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { |
| 50 auto iter = registered_responses_.find(params->url()); | 52 auto iter = registered_responses_.find(params->url()); |
| 51 if (iter == registered_responses_.end()) | 53 if (iter == registered_responses_.end()) |
| 52 return; | 54 return; |
| 53 | 55 |
| 54 std::unique_ptr<FakeDownloadItem> download_item = | 56 std::unique_ptr<FakeDownloadItem> download_item = |
| 55 base::MakeUnique<FakeDownloadItem>(); | 57 base::MakeUnique<FakeDownloadItem>(); |
| 56 | 58 |
| 57 download_item->SetURL(params->url()); | 59 download_item->SetURL(params->url()); |
| 60 download_item->SetUrlChain({params->url()}); | |
| 58 download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS); | 61 download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS); |
| 59 download_item->SetGuid(base::GenerateGUID()); | 62 download_item->SetGuid(base::GenerateGUID()); |
| 60 download_item->SetStartTime(base::Time()); | 63 download_item->SetStartTime(base::Time::Now()); |
| 61 | 64 |
| 62 // Asynchronously invoke the callback set on the |params|, and then continue | 65 // Asynchronously invoke the callback set on the |params|, and then continue |
| 63 // dealing with the response in this class. | 66 // dealing with the response in this class. |
| 64 BrowserThread::PostTaskAndReply( | 67 BrowserThread::PostTaskAndReply( |
| 65 BrowserThread::UI, FROM_HERE, | 68 BrowserThread::UI, FROM_HERE, |
| 66 base::Bind(params->callback(), download_item.get(), | 69 base::Bind(params->callback(), download_item.get(), |
| 67 DOWNLOAD_INTERRUPT_REASON_NONE), | 70 DOWNLOAD_INTERRUPT_REASON_NONE), |
| 68 base::Bind(&RespondingDownloadManager::DidStartDownload, | 71 base::Bind(&RespondingDownloadManager::DidStartDownload, |
| 69 weak_ptr_factory_.GetWeakPtr(), download_item.get())); | 72 weak_ptr_factory_.GetWeakPtr(), download_item.get())); |
| 70 | 73 |
| 71 download_items_.push_back(std::move(download_item)); | 74 download_items_.push_back(std::move(download_item)); |
| 72 } | 75 } |
| 73 | 76 |
| 74 private: | 77 private: |
| 75 // Called when the download has been "started" by the download manager. This | 78 // Called when the download has been "started" by the download manager. This |
| 76 // is where we finish the download by sending a single update. | 79 // is where we finish the download by sending a single update. |
| 77 void DidStartDownload(FakeDownloadItem* download_item) { | 80 void DidStartDownload(FakeDownloadItem* download_item) { |
| 78 auto iter = registered_responses_.find(download_item->GetURL()); | 81 auto iter = registered_responses_.find(download_item->GetURL()); |
| 79 DCHECK(iter != registered_responses_.end()); | 82 DCHECK(iter != registered_responses_.end()); |
| 80 | 83 |
| 81 const ResponseInfo& response_info = iter->second; | 84 const ResponseInfo& response_info = iter->second; |
| 82 | 85 |
| 83 download_item->SetState(DownloadItem::DownloadState::COMPLETE); | 86 download_item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 84 download_item->SetEndTime(base::Time()); | 87 download_item->SetEndTime(base::Time::Now()); |
| 85 | 88 |
| 86 // TODO(peter): Set response body, status code and so on. | 89 base::FilePath response_path; |
| 90 if (!temp_directory_.IsValid()) | |
| 91 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); | |
| 92 | |
| 93 // Write the |response_info|'s response_text to a temporary file. | |
| 94 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory_.GetPath(), | |
| 95 &response_path)); | |
| 96 | |
| 97 ASSERT_NE(-1 /* error */, | |
| 98 base::WriteFile(response_path, response_info.second.c_str(), | |
| 99 response_info.second.size())); | |
| 100 | |
| 101 download_item->SetTargetFilePath(response_path); | |
| 87 download_item->SetReceivedBytes(response_info.second.size()); | 102 download_item->SetReceivedBytes(response_info.second.size()); |
| 88 | 103 |
| 89 // Notify the Job Controller about the download having been updated. | 104 // Notify the Job Controller about the download having been updated. |
| 90 download_item->NotifyDownloadUpdated(); | 105 download_item->NotifyDownloadUpdated(); |
| 91 } | 106 } |
| 92 | 107 |
| 93 using ResponseInfo = | 108 using ResponseInfo = |
| 94 std::pair<int /* status_code */, std::string /* response_text */>; | 109 std::pair<int /* status_code */, std::string /* response_text */>; |
| 95 | 110 |
| 96 // Map of URL to the response information associated with that URL. | 111 // Map of URL to the response information associated with that URL. |
| 97 std::map<GURL, ResponseInfo> registered_responses_; | 112 std::map<GURL, ResponseInfo> registered_responses_; |
| 98 | 113 |
| 99 // Only used to guarantee the lifetime of the created FakeDownloadItems. | 114 // Only used to guarantee the lifetime of the created FakeDownloadItems. |
| 100 std::vector<std::unique_ptr<FakeDownloadItem>> download_items_; | 115 std::vector<std::unique_ptr<FakeDownloadItem>> download_items_; |
| 101 | 116 |
| 117 // Temporary directory in which successfully downloaded files will be stored. | |
| 118 base::ScopedTempDir temp_directory_; | |
|
harkness
2017/04/05 09:26:00
Is there a reason we can't rely on the download ma
Peter Beverloo
2017/04/05 09:39:27
This is a test.
| |
| 119 | |
| 102 base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_; | 120 base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_; |
| 103 | 121 |
| 104 DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager); | 122 DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager); |
| 105 }; | 123 }; |
| 106 | 124 |
| 107 IconDefinition CreateIcon(std::string src, | 125 IconDefinition CreateIcon(std::string src, |
| 108 std::string sizes, | 126 std::string sizes, |
| 109 std::string type) { | 127 std::string type) { |
| 110 IconDefinition icon; | 128 IconDefinition icon; |
| 111 icon.src = std::move(src); | 129 icon.src = std::move(src); |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 base::RunLoop event_dispatched_loop; | 452 base::RunLoop event_dispatched_loop; |
| 435 embedded_worker_test_helper()->set_fetched_event_closure( | 453 embedded_worker_test_helper()->set_fetched_event_closure( |
| 436 event_dispatched_loop.QuitClosure()); | 454 event_dispatched_loop.QuitClosure()); |
| 437 | 455 |
| 438 std::vector<ServiceWorkerFetchRequest> requests; | 456 std::vector<ServiceWorkerFetchRequest> requests; |
| 439 requests.push_back(CreateRequestWithProvidedResponse( | 457 requests.push_back(CreateRequestWithProvidedResponse( |
| 440 "GET", "https://example.com/funny_cat.txt", 200 /* status_code */, | 458 "GET", "https://example.com/funny_cat.txt", 200 /* status_code */, |
| 441 "This text describes a scenario involving a funny cat.")); | 459 "This text describes a scenario involving a funny cat.")); |
| 442 requests.push_back(CreateRequestWithProvidedResponse( | 460 requests.push_back(CreateRequestWithProvidedResponse( |
| 443 "GET", "https://example.com/crazy_cat.txt", 200 /* status_code */, | 461 "GET", "https://example.com/crazy_cat.txt", 200 /* status_code */, |
| 444 "This text descrubes a scenario involving a crazy cat.")); | 462 "This text describes another scenario that involves a crazy cat.")); |
| 445 | 463 |
| 446 // Create the registration with the given |requests|. | 464 // Create the registration with the given |requests|. |
| 447 { | 465 { |
| 448 BackgroundFetchOptions options; | 466 BackgroundFetchOptions options; |
| 449 | 467 |
| 450 blink::mojom::BackgroundFetchError error; | 468 blink::mojom::BackgroundFetchError error; |
| 451 BackgroundFetchRegistration registration; | 469 BackgroundFetchRegistration registration; |
| 452 | 470 |
| 453 // Create the first registration. This must succeed. | 471 // Create the first registration. This must succeed. |
| 454 ASSERT_NO_FATAL_FAILURE( | 472 ASSERT_NO_FATAL_FAILURE( |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 475 EXPECT_EQ(fetches[i].request.url, fetches[i].response.url_list[0]); | 493 EXPECT_EQ(fetches[i].request.url, fetches[i].response.url_list[0]); |
| 476 | 494 |
| 477 // TODO(peter): change-detector tests for unsupported properties. | 495 // TODO(peter): change-detector tests for unsupported properties. |
| 478 EXPECT_EQ(fetches[i].response.status_code, 0); | 496 EXPECT_EQ(fetches[i].response.status_code, 0); |
| 479 EXPECT_TRUE(fetches[i].response.status_text.empty()); | 497 EXPECT_TRUE(fetches[i].response.status_text.empty()); |
| 480 EXPECT_EQ(fetches[i].response.response_type, | 498 EXPECT_EQ(fetches[i].response.response_type, |
| 481 blink::WebServiceWorkerResponseTypeOpaque); | 499 blink::WebServiceWorkerResponseTypeOpaque); |
| 482 EXPECT_TRUE(fetches[i].response.headers.empty()); | 500 EXPECT_TRUE(fetches[i].response.headers.empty()); |
| 483 EXPECT_EQ(fetches[i].response.error, | 501 EXPECT_EQ(fetches[i].response.error, |
| 484 blink::WebServiceWorkerResponseErrorUnknown); | 502 blink::WebServiceWorkerResponseErrorUnknown); |
| 485 EXPECT_TRUE(fetches[i].response.response_time.is_null()); | |
| 486 | 503 |
| 487 // TODO(peter): Change-detector tests for when bodies are supported. | 504 // Verify that all properties have a sensible value. |
| 488 EXPECT_TRUE(fetches[i].response.blob_uuid.empty()); | 505 EXPECT_FALSE(fetches[i].response.response_time.is_null()); |
| 489 EXPECT_EQ(fetches[i].response.blob_size, 0u); | 506 |
| 507 // Verify that the response blobs have been populated. We cannot consume | |
| 508 // their data here since the handles have already been released. | |
| 509 ASSERT_FALSE(fetches[i].response.blob_uuid.empty()); | |
| 510 ASSERT_GT(fetches[i].response.blob_size, 0u); | |
| 490 } | 511 } |
| 491 } | 512 } |
| 492 | 513 |
| 493 TEST_F(BackgroundFetchServiceTest, Abort) { | 514 TEST_F(BackgroundFetchServiceTest, Abort) { |
| 494 // This test starts a new Background Fetch, completes the registration, and | 515 // This test starts a new Background Fetch, completes the registration, and |
| 495 // then aborts the Background Fetch mid-process. Tests all of StartFetch(), | 516 // then aborts the Background Fetch mid-process. Tests all of StartFetch(), |
| 496 // GetActiveFetches() and GetActiveTagsForServiceWorkerRegistration(). | 517 // GetActiveFetches() and GetActiveTagsForServiceWorkerRegistration(). |
| 497 | 518 |
| 498 BackgroundFetchRegistrationId registration_id; | 519 BackgroundFetchRegistrationId registration_id; |
| 499 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); | 520 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 const bool has_alternative_tag = | 649 const bool has_alternative_tag = |
| 629 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; | 650 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; |
| 630 | 651 |
| 631 EXPECT_TRUE(has_example_tag); | 652 EXPECT_TRUE(has_example_tag); |
| 632 EXPECT_TRUE(has_alternative_tag); | 653 EXPECT_TRUE(has_alternative_tag); |
| 633 } | 654 } |
| 634 } | 655 } |
| 635 | 656 |
| 636 } // namespace | 657 } // namespace |
| 637 } // namespace content | 658 } // namespace content |
| OLD | NEW |