Commit efbea763 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Introduce AllDownloadEventNotifier to listen to all download events

AllDownloadEventNotifier will listen to SimpleDownloadManagerCoordinate.
And report all the download events to its observers.

BUG=942770

Change-Id: I29b1e573d9c1f0e8b05002499410f296b6873765
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1560446Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Min Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#649719}
parent 74a7a39a
...@@ -18,6 +18,7 @@ source_set("internal") { ...@@ -18,6 +18,7 @@ source_set("internal") {
] ]
sources = [ sources = [
"all_download_event_notifier.cc",
"base_file.cc", "base_file.cc",
"base_file_win.cc", "base_file_win.cc",
"download_create_info.cc", "download_create_info.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/download/public/common/all_download_event_notifier.h"
namespace download {
AllDownloadEventNotifier::AllDownloadEventNotifier(
SimpleDownloadManagerCoordinator* simple_download_manager_coordinator)
: simple_download_manager_coordinator_(simple_download_manager_coordinator),
download_initialized_(false) {
simple_download_manager_coordinator_->AddObserver(this);
std::vector<DownloadItem*> downloads;
simple_download_manager_coordinator_->GetAllDownloads(&downloads);
for (auto* download : downloads) {
download->AddObserver(this);
observing_.insert(download);
}
}
AllDownloadEventNotifier::~AllDownloadEventNotifier() {
if (simple_download_manager_coordinator_)
simple_download_manager_coordinator_->RemoveObserver(this);
for (auto it = observing_.begin(); it != observing_.end(); ++it) {
(*it)->RemoveObserver(this);
}
observing_.clear();
}
void AllDownloadEventNotifier::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
if (download_initialized_) {
observer->OnDownloadsInitialized(
simple_download_manager_coordinator_,
!simple_download_manager_coordinator_->has_all_history_downloads());
}
}
void AllDownloadEventNotifier::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void AllDownloadEventNotifier::OnDownloadsInitialized(
bool active_downloads_only) {
download_initialized_ = true;
for (auto& observer : observers_)
observer.OnDownloadsInitialized(simple_download_manager_coordinator_,
active_downloads_only);
}
void AllDownloadEventNotifier::OnManagerGoingDown() {
for (auto& observer : observers_)
observer.OnManagerGoingDown(simple_download_manager_coordinator_);
simple_download_manager_coordinator_->RemoveObserver(this);
simple_download_manager_coordinator_ = nullptr;
}
void AllDownloadEventNotifier::OnDownloadCreated(DownloadItem* item) {
if (observing_.find(item) != observing_.end())
return;
item->AddObserver(this);
observing_.insert(item);
for (auto& observer : observers_)
observer.OnDownloadCreated(simple_download_manager_coordinator_, item);
}
void AllDownloadEventNotifier::OnDownloadUpdated(DownloadItem* item) {
for (auto& observer : observers_)
observer.OnDownloadUpdated(simple_download_manager_coordinator_, item);
}
void AllDownloadEventNotifier::OnDownloadOpened(DownloadItem* item) {
for (auto& observer : observers_)
observer.OnDownloadOpened(simple_download_manager_coordinator_, item);
}
void AllDownloadEventNotifier::OnDownloadRemoved(DownloadItem* item) {
for (auto& observer : observers_)
observer.OnDownloadRemoved(simple_download_manager_coordinator_, item);
}
void AllDownloadEventNotifier::OnDownloadDestroyed(DownloadItem* item) {
item->RemoveObserver(this);
observing_.erase(item);
}
} // namespace download
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "components/download/public/common/all_download_event_notifier.h"
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "components/download/public/common/simple_download_manager.h" #include "components/download/public/common/simple_download_manager.h"
...@@ -82,4 +83,10 @@ void SimpleDownloadManagerCoordinator::OnDownloadCreated(DownloadItem* item) { ...@@ -82,4 +83,10 @@ void SimpleDownloadManagerCoordinator::OnDownloadCreated(DownloadItem* item) {
observer.OnDownloadCreated(item); observer.OnDownloadCreated(item);
} }
AllDownloadEventNotifier* SimpleDownloadManagerCoordinator::GetNotifier() {
if (!notifier_)
notifier_ = std::make_unique<AllDownloadEventNotifier>(this);
return notifier_.get();
}
} // namespace download } // namespace download
...@@ -15,6 +15,7 @@ config("components_download_implementation") { ...@@ -15,6 +15,7 @@ config("components_download_implementation") {
component("public") { component("public") {
sources = [ sources = [
"all_download_event_notifier.h",
"auto_resumption_handler.cc", "auto_resumption_handler.cc",
"auto_resumption_handler.h", "auto_resumption_handler.h",
"base_file.h", "base_file.h",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_COMMON_ALL_DOWNLOAD_EVENT_NOTIFIER_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_ALL_DOWNLOAD_EVENT_NOTIFIER_H_
#include <set>
#include "base/macros.h"
#include "base/observer_list.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/simple_download_manager_coordinator.h"
namespace download {
// Observes all the download events from a single
// SimpleDownloadManagerCoordinator.
class AllDownloadEventNotifier
: public SimpleDownloadManagerCoordinator::Observer,
public DownloadItem::Observer {
public:
// All of the methods take the SimpleDownloadManagerCoordinator so that
// subclasses can observe multiple managers at once and easily distinguish
// which manager a given item belongs to.
class Observer {
public:
Observer() = default;
virtual ~Observer() = default;
virtual void OnDownloadsInitialized(
SimpleDownloadManagerCoordinator* coordinator,
bool active_downloads_only) {}
virtual void OnManagerGoingDown(
SimpleDownloadManagerCoordinator* coordinator) {}
virtual void OnDownloadCreated(SimpleDownloadManagerCoordinator* manager,
DownloadItem* item) {}
virtual void OnDownloadUpdated(SimpleDownloadManagerCoordinator* manager,
DownloadItem* item) {}
virtual void OnDownloadOpened(SimpleDownloadManagerCoordinator* manager,
DownloadItem* item) {}
virtual void OnDownloadRemoved(SimpleDownloadManagerCoordinator* manager,
DownloadItem* item) {}
private:
DISALLOW_COPY_AND_ASSIGN(Observer);
};
explicit AllDownloadEventNotifier(SimpleDownloadManagerCoordinator* manager);
~AllDownloadEventNotifier() override;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
private:
// SimpleDownloadManagerCoordinator::Observer
void OnDownloadsInitialized(bool active_downloads_only) override;
void OnManagerGoingDown() override;
void OnDownloadCreated(DownloadItem* item) override;
// DownloadItem::Observer
void OnDownloadUpdated(DownloadItem* item) override;
void OnDownloadOpened(DownloadItem* item) override;
void OnDownloadRemoved(DownloadItem* item) override;
void OnDownloadDestroyed(DownloadItem* item) override;
SimpleDownloadManagerCoordinator* simple_download_manager_coordinator_;
std::set<DownloadItem*> observing_;
bool download_initialized_;
// Observers that want to be notified of download events.
base::ObserverList<Observer>::Unchecked observers_;
DISALLOW_COPY_AND_ASSIGN(AllDownloadEventNotifier);
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_ALL_DOWNLOAD_EVENT_NOTIFIER_H_
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
namespace download { namespace download {
class AllDownloadEventNotifier;
class DownloadItem; class DownloadItem;
// This object allows swapping between different SimppleDownloadManager // This object allows swapping between different SimppleDownloadManager
...@@ -65,6 +66,9 @@ class COMPONENTS_DOWNLOAD_EXPORT SimpleDownloadManagerCoordinator ...@@ -65,6 +66,9 @@ class COMPONENTS_DOWNLOAD_EXPORT SimpleDownloadManagerCoordinator
// Return whether this object has download manager set. // Return whether this object has download manager set.
bool HasSetDownloadManager(); bool HasSetDownloadManager();
// Returns a non-empty notifier to be used for observing download events.
AllDownloadEventNotifier* GetNotifier();
bool has_all_history_downloads() const { return has_all_history_downloads_; } bool has_all_history_downloads() const { return has_all_history_downloads_; }
private: private:
...@@ -77,6 +81,9 @@ class COMPONENTS_DOWNLOAD_EXPORT SimpleDownloadManagerCoordinator ...@@ -77,6 +81,9 @@ class COMPONENTS_DOWNLOAD_EXPORT SimpleDownloadManagerCoordinator
SimpleDownloadManager* simple_download_manager_; SimpleDownloadManager* simple_download_manager_;
// Object for notifying others about various download events.
std::unique_ptr<AllDownloadEventNotifier> notifier_;
// Whether all the history downloads are ready. // Whether all the history downloads are ready.
bool has_all_history_downloads_; bool has_all_history_downloads_;
......
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