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. 4 5 #include "chrome/browser/ui/cocoa/download/download_item_mac.h" 6 7 #include "base/bind.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" 14 15 using content::DownloadItem; 16 17 // DownloadItemMac ------------------------------------------------------------- 18 19 DownloadItemMac::DownloadItemMac(DownloadItem* download, 20 DownloadItemController* controller) 21 : download_model_(download), 22 item_controller_(controller) { 23 download_model_.download()->AddObserver(this); 24 } 25 26 DownloadItemMac::~DownloadItemMac() { 27 download_model_.download()->RemoveObserver(this); 28 } 29 30 void DownloadItemMac::OnDownloadUpdated(content::DownloadItem* download) { 31 DCHECK_EQ(download, download_model_.download()); 32 33 if ([item_controller_ isDangerousMode] && !download_model_.IsDangerous()) { 34 // We have been approved. 35 [item_controller_ clearDangerousMode]; 36 } 37 38 if (download->GetTargetFilePath() != lastFilePath_) { 39 LoadIcon(); 40 lastFilePath_ = download->GetTargetFilePath(); 41 42 [item_controller_ updateToolTip]; 43 } 44 45 switch (download->GetState()) { 46 case DownloadItem::COMPLETE: 47 if (download_model_.ShouldRemoveFromShelfWhenComplete()) { 48 [item_controller_ remove]; // We're deleted now! 49 return; 50 } 51 // fall through 52 case DownloadItem::IN_PROGRESS: 53 case DownloadItem::CANCELLED: 54 [item_controller_ setStateFromDownload:&download_model_]; 55 break; 56 case DownloadItem::INTERRUPTED: 57 [item_controller_ updateToolTip]; 58 [item_controller_ setStateFromDownload:&download_model_]; 59 break; 60 default: 61 NOTREACHED(); 62 } 63 } 64 65 void DownloadItemMac::OnDownloadDestroyed(content::DownloadItem* download) { 66 [item_controller_ remove]; // We're deleted now! 67 } 68 69 void DownloadItemMac::OnDownloadOpened(content::DownloadItem* download) { 70 DCHECK_EQ(download, download_model_.download()); 71 [item_controller_ downloadWasOpened]; 72 } 73 74 void DownloadItemMac::LoadIcon() { 75 IconManager* icon_manager = g_browser_process->icon_manager(); 76 if (!icon_manager) 77 return; 78 79 // We may already have this particular image cached. 80 base::FilePath file = download_model_.download()->GetTargetFilePath(); 81 gfx::Image* icon = icon_manager->LookupIconFromFilepath( 82 file, IconLoader::ALL); 83 if (icon) { 84 [item_controller_ setIcon:icon->ToNSImage()]; 85 return; 86 } 87 88 // The icon isn't cached, load it asynchronously. 89 icon_manager->LoadIcon(file, 90 IconLoader::ALL, 91 base::Bind(&DownloadItemMac::OnExtractIconComplete, 92 base::Unretained(this)), 93 &cancelable_task_tracker_); 94 } 95 96 void DownloadItemMac::OnExtractIconComplete(gfx::Image* icon) { 97 if (!icon) 98 return; 99 [item_controller_ setIcon:icon->ToNSImage()]; 100 } 101