This source file includes following definitions.
- NotifyDownloadStarting
- NotifyDownloadStarting
- delegate_
- OnDownloadCreated
- OnDownloadUpdated
#include "chrome/browser/download/download_ui_controller.h"
#include "base/stl_util.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#if defined(OS_ANDROID)
#include "content/public/browser/android/download_controller_android.h"
#else
#include "chrome/browser/profiles/profile.h"
#endif
namespace {
#if defined(OS_ANDROID)
class DefaultUIControllerDelegateAndroid
: public DownloadUIController::Delegate {
public:
DefaultUIControllerDelegateAndroid() {}
virtual ~DefaultUIControllerDelegateAndroid() {}
private:
virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE;
};
void DefaultUIControllerDelegateAndroid::NotifyDownloadStarting(
content::DownloadItem* item) {
content::DownloadControllerAndroid::Get()->OnDownloadStarted(item);
}
#else
class DefaultUIControllerDelegate : public DownloadUIController::Delegate {
public:
explicit DefaultUIControllerDelegate(Profile* profile)
: profile_(profile) {}
virtual ~DefaultUIControllerDelegate() {}
private:
virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE;
Profile* profile_;
};
void DefaultUIControllerDelegate::NotifyDownloadStarting(
content::DownloadItem* item) {
content::WebContents* web_contents = item->GetWebContents();
Browser* browser =
web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL;
if (browser == NULL) {
browser = chrome::FindLastActiveWithProfile(profile_,
chrome::GetActiveDesktop());
}
if (browser)
browser->ShowDownload(item);
}
#endif
}
DownloadUIController::Delegate::~Delegate() {
}
DownloadUIController::DownloadUIController(content::DownloadManager* manager,
scoped_ptr<Delegate> delegate)
: download_notifier_(manager, this),
delegate_(delegate.Pass()) {
if (!delegate_) {
#if defined(OS_ANDROID)
delegate_.reset(new DefaultUIControllerDelegateAndroid());
#else
delegate_.reset(new DefaultUIControllerDelegate(
Profile::FromBrowserContext(manager->GetBrowserContext())));
#endif
}
}
DownloadUIController::~DownloadUIController() {
}
void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager,
content::DownloadItem* item) {
if (item->GetState() != content::DownloadItem::IN_PROGRESS)
return;
DownloadItemModel(item).SetShouldNotifyUI(true);
OnDownloadUpdated(manager, item);
}
void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager,
content::DownloadItem* item) {
if (!DownloadItemModel(item).ShouldNotifyUI())
return;
if (item->GetTargetFilePath().empty())
return;
DCHECK_NE(content::DownloadItem::COMPLETE, item->GetState());
DownloadItemModel(item).SetShouldNotifyUI(false);
delegate_->NotifyDownloadStarting(item);
}