1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/download/download_item_mac.h"
8 #include "base/callback.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/download/download_item_model.h"
11 #import "chrome/browser/ui/cocoa/download/download_item_controller.h"
12 #include "content/public/browser/download_item.h"
13 #include "ui/gfx/image/image.h"
15 using content::DownloadItem;
17 // DownloadItemMac -------------------------------------------------------------
19 DownloadItemMac::DownloadItemMac(DownloadItem* download,
20 DownloadItemController* controller)
21 : download_model_(download),
22 item_controller_(controller) {
23 download_model_.download()->AddObserver(this);
26 DownloadItemMac::~DownloadItemMac() {
27 download_model_.download()->RemoveObserver(this);
30 void DownloadItemMac::OnDownloadUpdated(content::DownloadItem* download) {
31 DCHECK_EQ(download, download_model_.download());
33 if ([item_controller_ isDangerousMode] && !download_model_.IsDangerous()) {
34 // We have been approved.
35 [item_controller_ clearDangerousMode];
38 if (download->GetTargetFilePath() != lastFilePath_) {
40 lastFilePath_ = download->GetTargetFilePath();
42 [item_controller_ updateToolTip];
45 switch (download->GetState()) {
46 case DownloadItem::COMPLETE:
47 if (download_model_.ShouldRemoveFromShelfWhenComplete()) {
48 [item_controller_ remove]; // We're deleted now!
52 case DownloadItem::IN_PROGRESS:
53 case DownloadItem::CANCELLED:
54 [item_controller_ setStateFromDownload:&download_model_];
56 case DownloadItem::INTERRUPTED:
57 [item_controller_ updateToolTip];
58 [item_controller_ setStateFromDownload:&download_model_];
65 void DownloadItemMac::OnDownloadDestroyed(content::DownloadItem* download) {
66 [item_controller_ remove]; // We're deleted now!
69 void DownloadItemMac::OnDownloadOpened(content::DownloadItem* download) {
70 DCHECK_EQ(download, download_model_.download());
71 [item_controller_ downloadWasOpened];
74 void DownloadItemMac::LoadIcon() {
75 IconManager* icon_manager = g_browser_process->icon_manager();
81 // We may already have this particular image cached.
82 base::FilePath file = download_model_.download()->GetTargetFilePath();
83 gfx::Image* icon = icon_manager->LookupIconFromFilepath(
84 file, IconLoader::ALL);
86 [item_controller_ setIcon:icon->ToNSImage()];
90 // The icon isn't cached, load it asynchronously.
91 icon_manager->LoadIcon(file,
93 base::Bind(&DownloadItemMac::OnExtractIconComplete,
94 base::Unretained(this)),
95 &cancelable_task_tracker_);
98 void DownloadItemMac::OnExtractIconComplete(gfx::Image* icon) {
101 [item_controller_ setIcon:icon->ToNSImage()];