1 // Copyright (c) 2011 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/callback.h" 8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/download/download_item.h" 10 #include "chrome/browser/download/download_item_model.h" 11 #import "chrome/browser/ui/cocoa/download/download_item_controller.h" 12 #include "chrome/browser/ui/cocoa/download/download_util_mac.h" 13 #include "ui/gfx/image.h" 14 15 // DownloadItemMac ------------------------------------------------------------- 16 17 DownloadItemMac::DownloadItemMac(BaseDownloadItemModel* download_model, 18 DownloadItemController* controller) 19 : download_model_(download_model), item_controller_(controller) { 20 download_model_->download()->AddObserver(this); 21 } 22 23 DownloadItemMac::~DownloadItemMac() { 24 download_model_->download()->RemoveObserver(this); 25 icon_consumer_.CancelAllRequests(); 26 } 27 28 void DownloadItemMac::OnDownloadUpdated(DownloadItem* download) { 29 DCHECK_EQ(download, download_model_->download()); 30 31 if ([item_controller_ isDangerousMode] && 32 download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) { 33 // We have been approved. 34 [item_controller_ clearDangerousMode]; 35 } 36 37 if (download->GetUserVerifiedFilePath() != lastFilePath_) { 38 // Turns out the file path is "Unconfirmed %d.crdownload" for dangerous 39 // downloads. When the download is confirmed, the file is renamed on 40 // another thread, so reload the icon if the download filename changes. 41 LoadIcon(); 42 lastFilePath_ = download->GetUserVerifiedFilePath(); 43 44 [item_controller_ updateToolTip]; 45 } 46 47 switch (download->state()) { 48 case DownloadItem::REMOVING: 49 [item_controller_ remove]; // We're deleted now! 50 break; 51 case DownloadItem::COMPLETE: 52 if (download->auto_opened()) { 53 [item_controller_ remove]; // We're deleted now! 54 return; 55 } 56 download_util::NotifySystemOfDownloadComplete(download->full_path()); 57 // fall through 58 case DownloadItem::IN_PROGRESS: 59 case DownloadItem::INTERRUPTED: 60 case DownloadItem::CANCELLED: 61 [item_controller_ setStateFromDownload:download_model_.get()]; 62 break; 63 default: 64 NOTREACHED(); 65 } 66 } 67 68 void DownloadItemMac::OnDownloadOpened(DownloadItem* download) { 69 DCHECK_EQ(download, download_model_->download()); 70 [item_controller_ downloadWasOpened]; 71 } 72 73 void DownloadItemMac::LoadIcon() { 74 IconManager* icon_manager = g_browser_process->icon_manager(); 75 if (!icon_manager) { 76 NOTREACHED(); 77 return; 78 } 79 80 // We may already have this particular image cached. 81 FilePath file = download_model_->download()->GetUserVerifiedFilePath(); 82 gfx::Image* icon = icon_manager->LookupIcon(file, IconLoader::SMALL); 83 if (icon) { 84 [item_controller_ setIcon:*icon]; 85 return; 86 } 87 88 // The icon isn't cached, load it asynchronously. 89 icon_manager->LoadIcon(file, IconLoader::SMALL, &icon_consumer_, 90 NewCallback(this, 91 &DownloadItemMac::OnExtractIconComplete)); 92 } 93 94 void DownloadItemMac::OnExtractIconComplete(IconManager::Handle handle, 95 gfx::Image* icon) { 96 if (!icon) 97 return; 98 [item_controller_ setIcon:*icon]; 99 } 100