Commit ed617592 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Delete all in-memory fetch data

Inform upstream systems when a fetch is completely finished (event
dispatch is complete) to know that it's safe to remove in-memory
fetch data.

Bug: 904818
Change-Id: Ieef3ff87ce66e97f177ef8422994adb22568e155
Reviewed-on: https://chromium-review.googlesource.com/c/1333815Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarMugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608033}
parent e7eed439
...@@ -256,12 +256,11 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest { ...@@ -256,12 +256,11 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest {
SetUpBrowser(browser()); SetUpBrowser(browser());
BackgroundFetchDelegateImpl* delegate = delegate_ = static_cast<BackgroundFetchDelegateImpl*>(
static_cast<BackgroundFetchDelegateImpl*>( active_browser_->profile()->GetBackgroundFetchDelegate());
active_browser_->profile()->GetBackgroundFetchDelegate()); DCHECK(delegate_);
DCHECK(delegate);
offline_content_provider_observer_->set_delegate(delegate); offline_content_provider_observer_->set_delegate(delegate_);
} }
void SetUpBrowser(Browser* browser) { void SetUpBrowser(Browser* browser) {
...@@ -323,11 +322,8 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest { ...@@ -323,11 +322,8 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest {
const ContentId& offline_item_id, const ContentId& offline_item_id,
std::unique_ptr<OfflineItemVisuals>* out_visuals) { std::unique_ptr<OfflineItemVisuals>* out_visuals) {
base::RunLoop run_loop; base::RunLoop run_loop;
BackgroundFetchDelegateImpl* delegate =
static_cast<BackgroundFetchDelegateImpl*>( delegate_->GetVisualsForItem(
active_browser_->profile()->GetBackgroundFetchDelegate());
DCHECK(delegate);
delegate->GetVisualsForItem(
offline_item_id, base::Bind(&BackgroundFetchBrowserTest::DidGetVisuals, offline_item_id, base::Bind(&BackgroundFetchBrowserTest::DidGetVisuals,
base::Unretained(this), base::Unretained(this),
run_loop.QuitClosure(), out_visuals)); run_loop.QuitClosure(), out_visuals));
...@@ -427,7 +423,8 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest { ...@@ -427,7 +423,8 @@ class BackgroundFetchBrowserTest : public InProcessBrowserTest {
} }
protected: protected:
download::DownloadService* download_service_{nullptr}; BackgroundFetchDelegateImpl* delegate_ = nullptr;
download::DownloadService* download_service_ = nullptr;
std::unique_ptr<WaitableDownloadLoggerObserver> download_observer_; std::unique_ptr<WaitableDownloadLoggerObserver> download_observer_;
std::unique_ptr<OfflineContentProviderObserver> std::unique_ptr<OfflineContentProviderObserver>
...@@ -685,6 +682,9 @@ IN_PROC_BROWSER_TEST_F(BackgroundFetchBrowserTest, ...@@ -685,6 +682,9 @@ IN_PROC_BROWSER_TEST_F(BackgroundFetchBrowserTest,
EXPECT_TRUE( EXPECT_TRUE(
base::StartsWith(offline_content_provider_observer_->latest_item().title, base::StartsWith(offline_content_provider_observer_->latest_item().title,
"New Fetched Title!", base::CompareCase::SENSITIVE)); "New Fetched Title!", base::CompareCase::SENSITIVE));
// Make sure the delegate cleans up after the fetch is complete.
EXPECT_TRUE(delegate_->job_details_map_.empty());
} }
IN_PROC_BROWSER_TEST_F(BackgroundFetchBrowserTest, IN_PROC_BROWSER_TEST_F(BackgroundFetchBrowserTest,
......
...@@ -314,7 +314,11 @@ void BackgroundFetchDelegateImpl::Abort(const std::string& job_unique_id) { ...@@ -314,7 +314,11 @@ void BackgroundFetchDelegateImpl::Abort(const std::string& job_unique_id) {
download_job_unique_id_map_.erase(download_guid); download_job_unique_id_map_.erase(download_guid);
} }
UpdateOfflineItemAndUpdateObservers(&job_details); UpdateOfflineItemAndUpdateObservers(&job_details);
job_details_map_.erase(job_details_iter); }
void BackgroundFetchDelegateImpl::MarkJobComplete(
const std::string& job_unique_id) {
job_details_map_.erase(job_unique_id);
} }
void BackgroundFetchDelegateImpl::UpdateUI( void BackgroundFetchDelegateImpl::UpdateUI(
......
...@@ -65,6 +65,7 @@ class BackgroundFetchDelegateImpl ...@@ -65,6 +65,7 @@ class BackgroundFetchDelegateImpl
const net::NetworkTrafficAnnotationTag& traffic_annotation, const net::NetworkTrafficAnnotationTag& traffic_annotation,
const net::HttpRequestHeaders& headers) override; const net::HttpRequestHeaders& headers) override;
void Abort(const std::string& job_unique_id) override; void Abort(const std::string& job_unique_id) override;
void MarkJobComplete(const std::string& job_unique_id) override;
void UpdateUI(const std::string& job_unique_id, void UpdateUI(const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
const base::Optional<SkBitmap>& icon) override; const base::Optional<SkBitmap>& icon) override;
...@@ -122,6 +123,9 @@ class BackgroundFetchDelegateImpl ...@@ -122,6 +123,9 @@ class BackgroundFetchDelegateImpl
} }
private: private:
FRIEND_TEST_ALL_PREFIXES(BackgroundFetchBrowserTest,
FetchesRunToCompletionAndUpdateTitle_Fetched);
struct JobDetails { struct JobDetails {
// If a job is part of the |job_details_map_|, it will have one of these // If a job is part of the |job_details_map_|, it will have one of these
// states. // states.
......
...@@ -168,6 +168,13 @@ class BackgroundFetchDelegateProxy::Core ...@@ -168,6 +168,13 @@ class BackgroundFetchDelegateProxy::Core
delegate_->Abort(job_unique_id); delegate_->Abort(job_unique_id);
} }
void MarkJobComplete(const std::string& job_unique_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (delegate_)
delegate_->MarkJobComplete(job_unique_id);
}
void UpdateUI(const std::string& job_unique_id, void UpdateUI(const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
const base::Optional<SkBitmap>& icon) { const base::Optional<SkBitmap>& icon) {
...@@ -381,7 +388,13 @@ void BackgroundFetchDelegateProxy::Abort(const std::string& job_unique_id) { ...@@ -381,7 +388,13 @@ void BackgroundFetchDelegateProxy::Abort(const std::string& job_unique_id) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::Abort, ui_core_ptr_, job_unique_id)); base::BindOnce(&Core::Abort, ui_core_ptr_, job_unique_id));
}
void BackgroundFetchDelegateProxy::MarkJobComplete(
const std::string& job_unique_id) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::MarkJobComplete, ui_core_ptr_, job_unique_id));
job_details_map_.erase(job_unique_id); job_details_map_.erase(job_unique_id);
} }
......
...@@ -111,6 +111,9 @@ class CONTENT_EXPORT BackgroundFetchDelegateProxy { ...@@ -111,6 +111,9 @@ class CONTENT_EXPORT BackgroundFetchDelegateProxy {
// requests already called OnDownloadComplete. // requests already called OnDownloadComplete.
void Abort(const std::string& job_unique_id); void Abort(const std::string& job_unique_id);
// Called when the fetch associated |job_unique_id| is completed.
void MarkJobComplete(const std::string& job_unique_id);
private: private:
class Core; class Core;
......
...@@ -68,10 +68,13 @@ class FakeBackgroundFetchDelegate : public BackgroundFetchDelegate { ...@@ -68,10 +68,13 @@ class FakeBackgroundFetchDelegate : public BackgroundFetchDelegate {
base::Unretained(this), job_unique_id, guid)); base::Unretained(this), job_unique_id, guid));
} }
} }
void Abort(const std::string& job_unique_id) override { void Abort(const std::string& job_unique_id) override {
aborted_jobs_.insert(job_unique_id); aborted_jobs_.insert(job_unique_id);
} }
void MarkJobComplete(const std::string& job_unique_id) override {}
void UpdateUI(const std::string& job_unique_id, void UpdateUI(const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
const base::Optional<SkBitmap>& icon) override { const base::Optional<SkBitmap>& icon) override {
...@@ -257,7 +260,6 @@ TEST_F(BackgroundFetchDelegateProxyTest, Abort) { ...@@ -257,7 +260,6 @@ TEST_F(BackgroundFetchDelegateProxyTest, Abort) {
delegate_proxy_.Abort(kExampleUniqueId); delegate_proxy_.Abort(kExampleUniqueId);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_FALSE(controller.request_started_) << "Aborted job started";
EXPECT_FALSE(controller.request_completed_) << "Aborted job completed"; EXPECT_FALSE(controller.request_completed_) << "Aborted job completed";
EXPECT_TRUE(controller2.request_started_) << "Normal job did not start"; EXPECT_TRUE(controller2.request_started_) << "Normal job did not start";
EXPECT_TRUE(controller2.request_completed_) << "Normal job did not complete"; EXPECT_TRUE(controller2.request_completed_) << "Normal job did not complete";
......
...@@ -164,6 +164,9 @@ void BackgroundFetchScheduler::CleanupRegistration( ...@@ -164,6 +164,9 @@ void BackgroundFetchScheduler::CleanupRegistration(
// downloaded data around so long as there are references to it, and delete // downloaded data around so long as there are references to it, and delete
// it once there is none. We don't need to do that accounting. // it once there is none. We don't need to do that accounting.
data_manager_->DeleteRegistration(registration_id, base::DoNothing()); data_manager_->DeleteRegistration(registration_id, base::DoNothing());
// Notify other systems that this registration is complete.
delegate_proxy_->MarkJobComplete(registration_id.unique_id());
} }
void BackgroundFetchScheduler::DispatchClickEvent( void BackgroundFetchScheduler::DispatchClickEvent(
......
...@@ -641,6 +641,10 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) { ...@@ -641,6 +641,10 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
ASSERT_FALSE(fetches[i].response->blob->uuid.empty()); ASSERT_FALSE(fetches[i].response->blob->uuid.empty());
ASSERT_GT(fetches[i].response->blob->size, 0u); ASSERT_GT(fetches[i].response->blob->size, 0u);
} }
auto* delegate = static_cast<MockBackgroundFetchDelegate*>(
browser_context()->GetBackgroundFetchDelegate());
EXPECT_TRUE(delegate->completed_jobs().count(registration.unique_id));
} }
TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) { TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) {
...@@ -732,6 +736,10 @@ TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) { ...@@ -732,6 +736,10 @@ TEST_F(BackgroundFetchServiceTest, FetchFailEventDispatch) {
blink::mojom::ServiceWorkerResponseError::kUnknown); blink::mojom::ServiceWorkerResponseError::kUnknown);
EXPECT_TRUE(fetches[i].response->cors_exposed_header_names.empty()); EXPECT_TRUE(fetches[i].response->cors_exposed_header_names.empty());
} }
auto* delegate = static_cast<MockBackgroundFetchDelegate*>(
browser_context()->GetBackgroundFetchDelegate());
EXPECT_TRUE(delegate->completed_jobs().count(registration.unique_id));
} }
TEST_F(BackgroundFetchServiceTest, UpdateUI) { TEST_F(BackgroundFetchServiceTest, UpdateUI) {
...@@ -815,6 +823,10 @@ TEST_F(BackgroundFetchServiceTest, Abort) { ...@@ -815,6 +823,10 @@ TEST_F(BackgroundFetchServiceTest, Abort) {
GetRegistration(service_worker_registration_id, kExampleDeveloperId, GetRegistration(service_worker_registration_id, kExampleDeveloperId,
&second_error, &second_registration); &second_error, &second_registration);
ASSERT_EQ(second_error, blink::mojom::BackgroundFetchError::INVALID_ID); ASSERT_EQ(second_error, blink::mojom::BackgroundFetchError::INVALID_ID);
auto* delegate = static_cast<MockBackgroundFetchDelegate*>(
browser_context()->GetBackgroundFetchDelegate());
EXPECT_TRUE(delegate->completed_jobs().count(registration_id.unique_id()));
} }
TEST_F(BackgroundFetchServiceTest, AbortInvalidDeveloperIdArgument) { TEST_F(BackgroundFetchServiceTest, AbortInvalidDeveloperIdArgument) {
......
...@@ -171,6 +171,11 @@ void MockBackgroundFetchDelegate::Abort(const std::string& job_unique_id) { ...@@ -171,6 +171,11 @@ void MockBackgroundFetchDelegate::Abort(const std::string& job_unique_id) {
aborted_jobs_.insert(job_unique_id); aborted_jobs_.insert(job_unique_id);
} }
void MockBackgroundFetchDelegate::MarkJobComplete(
const std::string& job_unique_id) {
completed_jobs_.insert(job_unique_id);
}
void MockBackgroundFetchDelegate::UpdateUI( void MockBackgroundFetchDelegate::UpdateUI(
const std::string& job_unique_id, const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
......
...@@ -83,6 +83,7 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate { ...@@ -83,6 +83,7 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate {
const net::NetworkTrafficAnnotationTag& traffic_annotation, const net::NetworkTrafficAnnotationTag& traffic_annotation,
const net::HttpRequestHeaders& headers) override; const net::HttpRequestHeaders& headers) override;
void Abort(const std::string& job_unique_id) override; void Abort(const std::string& job_unique_id) override;
void MarkJobComplete(const std::string& job_unique_id) override;
void UpdateUI(const std::string& job_unique_id, void UpdateUI(const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
const base::Optional<SkBitmap>& icon) override; const base::Optional<SkBitmap>& icon) override;
...@@ -90,6 +91,10 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate { ...@@ -90,6 +91,10 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate {
void RegisterResponse(const GURL& url, void RegisterResponse(const GURL& url,
std::unique_ptr<TestResponse> response); std::unique_ptr<TestResponse> response);
const std::set<std::string>& completed_jobs() const {
return completed_jobs_;
}
private: private:
// Posts (to the default task runner) a callback that is only run if the job // Posts (to the default task runner) a callback that is only run if the job
// indicated by |job_unique_id| has not been aborted. // indicated by |job_unique_id| has not been aborted.
...@@ -113,6 +118,9 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate { ...@@ -113,6 +118,9 @@ class MockBackgroundFetchDelegate : public BackgroundFetchDelegate {
// Set of unique job ids that have been aborted. // Set of unique job ids that have been aborted.
std::set<std::string> aborted_jobs_; std::set<std::string> aborted_jobs_;
// Set of unique job ids that have been completed.
std::set<std::string> completed_jobs_;
// Map from download GUIDs to unique job ids. // Map from download GUIDs to unique job ids.
std::map<std::string, std::string> download_guid_to_job_id_map_; std::map<std::string, std::string> download_guid_to_job_id_map_;
......
...@@ -137,6 +137,9 @@ class CONTENT_EXPORT BackgroundFetchDelegate { ...@@ -137,6 +137,9 @@ class CONTENT_EXPORT BackgroundFetchDelegate {
// Aborts any downloads associated with |job_unique_id|. // Aborts any downloads associated with |job_unique_id|.
virtual void Abort(const std::string& job_unique_id) = 0; virtual void Abort(const std::string& job_unique_id) = 0;
// Called after the fetch has completed so that the delegate can clean up.
virtual void MarkJobComplete(const std::string& job_unique_id) = 0;
// Updates the UI shown for the fetch job associated with |job_unique_id| to // Updates the UI shown for the fetch job associated with |job_unique_id| to
// display a new |title| or |icon|. // display a new |title| or |icon|.
virtual void UpdateUI(const std::string& job_unique_id, virtual void UpdateUI(const std::string& job_unique_id,
......
...@@ -252,6 +252,9 @@ void LayoutTestBackgroundFetchDelegate::Abort( ...@@ -252,6 +252,9 @@ void LayoutTestBackgroundFetchDelegate::Abort(
// TODO(peter): Implement the ability to abort the |job_unique_id|. // TODO(peter): Implement the ability to abort the |job_unique_id|.
} }
void LayoutTestBackgroundFetchDelegate::MarkJobComplete(
const std::string& job_unique_id) {}
void LayoutTestBackgroundFetchDelegate::UpdateUI( void LayoutTestBackgroundFetchDelegate::UpdateUI(
const std::string& job_unique_id, const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
......
...@@ -40,6 +40,7 @@ class LayoutTestBackgroundFetchDelegate : public BackgroundFetchDelegate { ...@@ -40,6 +40,7 @@ class LayoutTestBackgroundFetchDelegate : public BackgroundFetchDelegate {
const net::NetworkTrafficAnnotationTag& traffic_annotation, const net::NetworkTrafficAnnotationTag& traffic_annotation,
const net::HttpRequestHeaders& headers) override; const net::HttpRequestHeaders& headers) override;
void Abort(const std::string& job_unique_id) override; void Abort(const std::string& job_unique_id) override;
void MarkJobComplete(const std::string& job_unique_id) override;
void UpdateUI(const std::string& job_unique_id, void UpdateUI(const std::string& job_unique_id,
const base::Optional<std::string>& title, const base::Optional<std::string>& title,
const base::Optional<SkBitmap>& icon) override; const base::Optional<SkBitmap>& icon) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment