Commit ff9ee2b6 authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Send updates for FAILED downloads only once

When download offline content provider is enabled, we send redundant
FAILED updates. However the notification layer removes it from the shared
preferences the first time it sees a FAILED download. Hence it will
result in two different notifications. The solution is to treat COMPLETED
and FAILED as terminal downloads and filter out duplicate updates for them.

Bug: 1003550
Change-Id: Ief7c4450d644259352d3626e4f9dbd7b15362091
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1801228
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#696989}
parent 1de5d871
...@@ -80,8 +80,7 @@ DownloadCoreServiceImpl::GetDownloadManagerDelegate() { ...@@ -80,8 +80,7 @@ DownloadCoreServiceImpl::GetDownloadManagerDelegate() {
// Pass an empty delegate when constructing the DownloadUIController. The // Pass an empty delegate when constructing the DownloadUIController. The
// default delegate does all the notifications we need. // default delegate does all the notifications we need.
download_ui_.reset(new DownloadUIController( download_ui_.reset(new DownloadUIController(
manager, std::unique_ptr<DownloadUIController::Delegate>(), manager, std::unique_ptr<DownloadUIController::Delegate>()));
download_provider));
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
download_shelf_controller_.reset(new DownloadShelfController(profile_)); download_shelf_controller_.reset(new DownloadShelfController(profile_));
......
...@@ -456,23 +456,19 @@ void DownloadOfflineContentProvider::OnDownloadUpdated(DownloadItem* item) { ...@@ -456,23 +456,19 @@ void DownloadOfflineContentProvider::OnDownloadUpdated(DownloadItem* item) {
return; return;
UpdateDelta update_delta; UpdateDelta update_delta;
if (item->GetState() == DownloadItem::COMPLETE) { auto offline_item = OfflineItemUtils::CreateOfflineItem(name_space_, item);
update_delta.state_changed = completed_downloads_.find(item->GetGuid()) == if (offline_item.state == OfflineItemState::COMPLETE ||
completed_downloads_.end(); offline_item.state == OfflineItemState::FAILED ||
offline_item.state == OfflineItemState::CANCELLED) {
// TODO(crbug.com/938152): May be move this to DownloadItem. // TODO(crbug.com/938152): May be move this to DownloadItem.
// Never call this for completed downloads from history. // Never call this for completed downloads from history.
if (completed_downloads_.find(item->GetGuid()) != item->RemoveObserver(this);
completed_downloads_.end()) {
return;
}
completed_downloads_.insert(item->GetGuid());
AddCompletedDownload(item); update_delta.state_changed = true;
if (item->GetState() == DownloadItem::COMPLETE)
AddCompletedDownload(item);
} }
auto offline_item = OfflineItemUtils::CreateOfflineItem(name_space_, item);
UpdateObservers(offline_item, update_delta); UpdateObservers(offline_item, update_delta);
} }
...@@ -489,10 +485,6 @@ void DownloadOfflineContentProvider::OnDownloadRemoved(DownloadItem* item) { ...@@ -489,10 +485,6 @@ void DownloadOfflineContentProvider::OnDownloadRemoved(DownloadItem* item) {
observer.OnItemRemoved(contentId); observer.OnItemRemoved(contentId);
} }
void DownloadOfflineContentProvider::OnDownloadDestroyed(DownloadItem* item) {
completed_downloads_.erase(item->GetGuid());
}
void DownloadOfflineContentProvider::AddCompletedDownload(DownloadItem* item) { void DownloadOfflineContentProvider::AddCompletedDownload(DownloadItem* item) {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
DownloadManagerBridge::AddCompletedDownload( DownloadManagerBridge::AddCompletedDownload(
......
...@@ -90,7 +90,6 @@ class DownloadOfflineContentProvider ...@@ -90,7 +90,6 @@ class DownloadOfflineContentProvider
// DownloadItem::Observer overrides // DownloadItem::Observer overrides
void OnDownloadUpdated(DownloadItem* item) override; void OnDownloadUpdated(DownloadItem* item) override;
void OnDownloadRemoved(DownloadItem* item) override; void OnDownloadRemoved(DownloadItem* item) override;
void OnDownloadDestroyed(DownloadItem* download) override;
private: private:
enum class State { enum class State {
...@@ -129,8 +128,6 @@ class DownloadOfflineContentProvider ...@@ -129,8 +128,6 @@ class DownloadOfflineContentProvider
std::string name_space_; std::string name_space_;
SimpleDownloadManagerCoordinator* manager_; SimpleDownloadManagerCoordinator* manager_;
// Tracks the completed downloads in the current session.
std::set<std::string> completed_downloads_;
std::unique_ptr<download::AllDownloadEventNotifier::Observer> std::unique_ptr<download::AllDownloadEventNotifier::Observer>
all_download_observer_; all_download_observer_;
bool checked_for_externally_removed_downloads_; bool checked_for_externally_removed_downloads_;
......
...@@ -111,13 +111,9 @@ void DownloadShelfUIControllerDelegate::OnNewDownloadReady( ...@@ -111,13 +111,9 @@ void DownloadShelfUIControllerDelegate::OnNewDownloadReady(
DownloadUIController::Delegate::~Delegate() { DownloadUIController::Delegate::~Delegate() {
} }
DownloadUIController::DownloadUIController( DownloadUIController::DownloadUIController(content::DownloadManager* manager,
content::DownloadManager* manager, std::unique_ptr<Delegate> delegate)
std::unique_ptr<Delegate> delegate, : download_notifier_(manager, this), delegate_(std::move(delegate)) {
DownloadOfflineContentProvider* provider)
: download_notifier_(manager, this),
delegate_(std::move(delegate)),
download_provider_(provider) {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
if (!delegate_) if (!delegate_)
delegate_.reset(new AndroidUIControllerDelegate()); delegate_.reset(new AndroidUIControllerDelegate());
...@@ -207,6 +203,4 @@ void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, ...@@ -207,6 +203,4 @@ void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager,
DownloadItemModel(item).SetWasUINotified(true); DownloadItemModel(item).SetWasUINotified(true);
delegate_->OnNewDownloadReady(item); delegate_->OnNewDownloadReady(item);
if (download_provider_)
download_provider_->OnDownloadStarted(item);
} }
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <set> #include <set>
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/download/download_offline_content_provider.h"
#include "components/download/content/public/all_download_item_notifier.h" #include "components/download/content/public/all_download_item_notifier.h"
// This class handles the task of observing a single DownloadManager for // This class handles the task of observing a single DownloadManager for
...@@ -37,8 +36,7 @@ class DownloadUIController ...@@ -37,8 +36,7 @@ class DownloadUIController
// //
// Currently explicit delegates are only used for testing. // Currently explicit delegates are only used for testing.
DownloadUIController(content::DownloadManager* manager, DownloadUIController(content::DownloadManager* manager,
std::unique_ptr<Delegate> delegate, std::unique_ptr<Delegate> delegate);
DownloadOfflineContentProvider* provider);
~DownloadUIController() override; ~DownloadUIController() override;
...@@ -51,7 +49,6 @@ class DownloadUIController ...@@ -51,7 +49,6 @@ class DownloadUIController
download::AllDownloadItemNotifier download_notifier_; download::AllDownloadItemNotifier download_notifier_;
std::unique_ptr<Delegate> delegate_; std::unique_ptr<Delegate> delegate_;
DownloadOfflineContentProvider* download_provider_;
DISALLOW_COPY_AND_ASSIGN(DownloadUIController); DISALLOW_COPY_AND_ASSIGN(DownloadUIController);
}; };
......
...@@ -97,8 +97,6 @@ class DownloadUIControllerTest : public ChromeRenderViewHostTestHarness { ...@@ -97,8 +97,6 @@ class DownloadUIControllerTest : public ChromeRenderViewHostTestHarness {
// delegate results in the DownloadItem* being stored in |notified_item_|. // delegate results in the DownloadItem* being stored in |notified_item_|.
std::unique_ptr<DownloadUIController::Delegate> GetTestDelegate(); std::unique_ptr<DownloadUIController::Delegate> GetTestDelegate();
DownloadOfflineContentProvider* GetDownloadProvider() { return nullptr; }
MockDownloadManager* manager() { return manager_.get(); } MockDownloadManager* manager() { return manager_.get(); }
// Returns the DownloadManager::Observer registered by a test case. This is // Returns the DownloadManager::Observer registered by a test case. This is
...@@ -274,8 +272,7 @@ DownloadUIControllerTest::GetTestDelegate() { ...@@ -274,8 +272,7 @@ DownloadUIControllerTest::GetTestDelegate() {
// a non-empty path. I.e. once the download target has been determined. // a non-empty path. I.e. once the download target has been determined.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
std::unique_ptr<MockDownloadItem> item(CreateMockInProgressDownload()); std::unique_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
DownloadUIController controller(manager(), GetTestDelegate(), DownloadUIController controller(manager(), GetTestDelegate());
GetDownloadProvider());
EXPECT_CALL(*item, GetTargetFilePath()) EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath())); .WillOnce(ReturnRefOfCopy(base::FilePath()));
...@@ -297,8 +294,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { ...@@ -297,8 +294,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
// A download that's created in an interrupted state should also be displayed. // A download that's created in an interrupted state should also be displayed.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) { TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) {
std::unique_ptr<MockDownloadItem> item = CreateMockInProgressDownload(); std::unique_ptr<MockDownloadItem> item = CreateMockInProgressDownload();
DownloadUIController controller(manager(), GetTestDelegate(), DownloadUIController controller(manager(), GetTestDelegate());
GetDownloadProvider());
EXPECT_CALL(*item, GetState()) EXPECT_CALL(*item, GetState())
.WillRepeatedly(Return(download::DownloadItem::INTERRUPTED)); .WillRepeatedly(Return(download::DownloadItem::INTERRUPTED));
...@@ -312,8 +308,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) { ...@@ -312,8 +308,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) {
// additional OnDownloadUpdated() notification. // additional OnDownloadUpdated() notification.
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
std::unique_ptr<MockDownloadItem> item(CreateMockInProgressDownload()); std::unique_ptr<MockDownloadItem> item(CreateMockInProgressDownload());
DownloadUIController controller(manager(), GetTestDelegate(), DownloadUIController controller(manager(), GetTestDelegate());
GetDownloadProvider());
ASSERT_TRUE(manager_observer()); ASSERT_TRUE(manager_observer());
manager_observer()->OnDownloadCreated(manager(), item.get()); manager_observer()->OnDownloadCreated(manager(), item.get());
...@@ -322,8 +317,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { ...@@ -322,8 +317,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
// The UI shouldn't be notified of downloads that were restored from history. // The UI shouldn't be notified of downloads that were restored from history.
TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) { TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) {
DownloadUIController controller(manager(), GetTestDelegate(), DownloadUIController controller(manager(), GetTestDelegate());
GetDownloadProvider());
// DownloadHistory should already have been created. It performs a query of // DownloadHistory should already have been created. It performs a query of
// existing downloads upon creation. We'll use the callback to inject a // existing downloads upon creation. We'll use the callback to inject a
// history download. // history download.
......
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