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 (!download_model_.ShouldShowInShelf()) {
34 [item_controller_ remove]; // We're deleted now!
38 if ([item_controller_ isDangerousMode] && !download_model_.IsDangerous()) {
39 // We have been approved.
40 [item_controller_ clearDangerousMode];
43 if (download->GetTargetFilePath() != lastFilePath_) {
45 lastFilePath_ = download->GetTargetFilePath();
47 [item_controller_ updateToolTip];
50 switch (download->GetState()) {
51 case DownloadItem::COMPLETE:
52 if (download_model_.ShouldRemoveFromShelfWhenComplete()) {
53 [item_controller_ remove]; // We're deleted now!
57 case DownloadItem::IN_PROGRESS:
58 case DownloadItem::CANCELLED:
59 [item_controller_ setStateFromDownload:&download_model_];
61 case DownloadItem::INTERRUPTED:
62 [item_controller_ updateToolTip];
63 [item_controller_ setStateFromDownload:&download_model_];
70 void DownloadItemMac::OnDownloadDestroyed(content::DownloadItem* download) {
71 [item_controller_ remove]; // We're deleted now!
74 void DownloadItemMac::OnDownloadOpened(content::DownloadItem* download) {
75 DCHECK_EQ(download, download_model_.download());
76 [item_controller_ downloadWasOpened];
79 void DownloadItemMac::LoadIcon() {
80 IconManager* icon_manager = g_browser_process->icon_manager();
84 // We may already have this particular image cached.
85 base::FilePath file = download_model_.download()->GetTargetFilePath();
86 gfx::Image* icon = icon_manager->LookupIconFromFilepath(
87 file, IconLoader::ALL);
89 [item_controller_ setIcon:icon->ToNSImage()];
93 // The icon isn't cached, load it asynchronously.
94 icon_manager->LoadIcon(file,
96 base::Bind(&DownloadItemMac::OnExtractIconComplete,
97 base::Unretained(this)),
98 &cancelable_task_tracker_);
101 void DownloadItemMac::OnExtractIconComplete(gfx::Image* icon) {
104 [item_controller_ setIcon:icon->ToNSImage()];