summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/cocoa/download_item_cell.mm10
-rw-r--r--chrome/browser/cocoa/download_item_controller.h3
-rw-r--r--chrome/browser/cocoa/download_item_controller.mm7
-rw-r--r--chrome/browser/cocoa/download_item_mac.h18
-rw-r--r--chrome/browser/cocoa/download_item_mac.mm34
5 files changed, 61 insertions, 11 deletions
diff --git a/chrome/browser/cocoa/download_item_cell.mm b/chrome/browser/cocoa/download_item_cell.mm
index 1d1793e..11179fd 100644
--- a/chrome/browser/cocoa/download_item_cell.mm
+++ b/chrome/browser/cocoa/download_item_cell.mm
@@ -102,7 +102,7 @@ NSTimeInterval kHideStatusDuration = 0.3;
return self;
}
-// For programmatic instantiations
+// For programmatic instantiations.
- (id)initTextCell:(NSString *)string {
if ((self = [super initTextCell:string])) {
[self setInitialState];
@@ -118,15 +118,9 @@ NSTimeInterval kHideStatusDuration = 0.3;
}
- (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel {
- // Set name and icon of download.
+ // Set the name of the download.
downloadPath_ = downloadModel->download()->GetFileName();
- // TODO(paulg): Use IconManager for loading icons on the file thread
- // (crbug.com/16226).
- NSString* extension = base::SysUTF8ToNSString(downloadPath_.Extension());
- NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFileType:extension];
- [self setImage:icon];
-
std::wstring statusText = downloadModel->GetStatusText();
if (statusText.empty()) {
// Remove the status text label.
diff --git a/chrome/browser/cocoa/download_item_controller.h b/chrome/browser/cocoa/download_item_controller.h
index 30bc28e..8fa3831 100644
--- a/chrome/browser/cocoa/download_item_controller.h
+++ b/chrome/browser/cocoa/download_item_controller.h
@@ -42,6 +42,9 @@ class DownloadShelfContextMenuMac;
// Remove ourself from the download UI.
- (void)remove;
+// Asynchronous icon loading callback.
+- (void)setIcon:(NSImage*)icon;
+
// Download item button clicked
- (IBAction)handleButtonClick:(id)sender;
diff --git a/chrome/browser/cocoa/download_item_controller.mm b/chrome/browser/cocoa/download_item_controller.mm
index f5d2836..d00fd58 100644
--- a/chrome/browser/cocoa/download_item_controller.mm
+++ b/chrome/browser/cocoa/download_item_controller.mm
@@ -51,6 +51,7 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu {
- (void)awakeFromNib {
[self setStateFromDownload:bridge_->download_model()];
+ bridge_->LoadIcon();
}
- (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel {
@@ -58,7 +59,7 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu {
// (gratituous use of animation, special handling of dangerous downloads)
// that we don't currently do.
- // Set correct popup menu.
+ // Set the correct popup menu.
if (downloadModel->download()->state() == DownloadItem::COMPLETE)
currentMenu_ = completeDownloadMenu_;
else
@@ -68,6 +69,10 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu {
[cell_ setStateFromDownload:downloadModel];
}
+- (void)setIcon:(NSImage*)icon {
+ [cell_ setImage:icon];
+}
+
- (void)remove {
// We are deleted after this!
[shelf_ remove:self];
diff --git a/chrome/browser/cocoa/download_item_mac.h b/chrome/browser/cocoa/download_item_mac.h
index 7f31c2a..f992e21 100644
--- a/chrome/browser/cocoa/download_item_mac.h
+++ b/chrome/browser/cocoa/download_item_mac.h
@@ -9,13 +9,16 @@
#include "base/scoped_nsobject.h"
#include "base/scoped_ptr.h"
+#include "chrome/browser/cancelable_request.h"
#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/icon_manager.h"
class BaseDownloadItemModel;
@class DownloadItemController;
-// A class that bridges the visible mac download items to chromium's
-// download model.
+// A class that bridges the visible mac download items to chromium's download
+// model. The owning object (DownloadItemController) must explicitly call
+// |LoadIcon| if it wants to display the icon associated with this download.
class DownloadItemMac : DownloadItem::Observer {
public:
@@ -32,12 +35,23 @@ class DownloadItemMac : DownloadItem::Observer {
BaseDownloadItemModel* download_model() { return download_model_.get(); }
+ // Asynchronous icon loading support.
+ void LoadIcon();
+
private:
+ // Callback for asynchronous icon loading.
+ void OnExtractIconComplete(IconManager::Handle handle, SkBitmap* icon_bitmap);
+
// The download item model we represent.
scoped_ptr<BaseDownloadItemModel> download_model_;
// The objective-c controller object.
DownloadItemController* item_controller_; // weak, owns us.
+
+ // For canceling an in progress icon request.
+ CancelableRequestConsumerT<int, 0> icon_consumer_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadItemMac);
};
#endif // CHROME_BROWSER_COCOA_DOWNLOAD_ITEM_MAC_H_
diff --git a/chrome/browser/cocoa/download_item_mac.mm b/chrome/browser/cocoa/download_item_mac.mm
index d3e12a9..357c902 100644
--- a/chrome/browser/cocoa/download_item_mac.mm
+++ b/chrome/browser/cocoa/download_item_mac.mm
@@ -4,8 +4,10 @@
#include "chrome/browser/cocoa/download_item_mac.h"
+#include "chrome/browser/browser_process.h"
#import "chrome/browser/cocoa/download_item_controller.h"
#include "chrome/browser/download/download_item_model.h"
+#include "skia/ext/skia_utils_mac.h"
// DownloadItemMac -------------------------------------------------------------
@@ -17,6 +19,7 @@ DownloadItemMac::DownloadItemMac(BaseDownloadItemModel* download_model,
DownloadItemMac::~DownloadItemMac() {
download_model_->download()->RemoveObserver(this);
+ icon_consumer_.CancelAllRequests();
}
void DownloadItemMac::OnDownloadUpdated(DownloadItem* download) {
@@ -35,3 +38,34 @@ void DownloadItemMac::OnDownloadUpdated(DownloadItem* download) {
NOTREACHED();
}
}
+
+void DownloadItemMac::LoadIcon() {
+ IconManager* icon_manager = g_browser_process->icon_manager();
+ if (!icon_manager) {
+ NOTREACHED();
+ return;
+ }
+
+ // We may already have this particular image cached.
+ FilePath file = download_model_->download()->full_path();
+ SkBitmap* icon_bitmap = icon_manager->LookupIcon(file, IconLoader::SMALL);
+ if (icon_bitmap) {
+ NSImage* icon = gfx::SkBitmapToNSImage(*icon_bitmap);
+ [item_controller_ setIcon:icon];
+ return;
+ }
+
+ // The icon isn't cached, load it asynchronously.
+ icon_manager->LoadIcon(file, IconLoader::NORMAL, &icon_consumer_,
+ NewCallback(this,
+ &DownloadItemMac::OnExtractIconComplete));
+}
+
+void DownloadItemMac::OnExtractIconComplete(IconManager::Handle handle,
+ SkBitmap* icon_bitmap) {
+ if (!icon_bitmap)
+ return;
+
+ NSImage* icon = gfx::SkBitmapToNSImage(*icon_bitmap);
+ [item_controller_ setIcon:icon];
+}