BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_item_mac.mm
blobb39137cb104872a77293cdc344c8b1d1910de3ed
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"
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"
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!
35     return;
36   }
38   if ([item_controller_ isDangerousMode] && !download_model_.IsDangerous()) {
39     // We have been approved.
40     [item_controller_ clearDangerousMode];
41   }
43   if (download->GetTargetFilePath() != lastFilePath_) {
44     LoadIcon();
45     lastFilePath_ = download->GetTargetFilePath();
47     [item_controller_ updateToolTip];
48   }
50   switch (download->GetState()) {
51     case DownloadItem::COMPLETE:
52       if (download_model_.ShouldRemoveFromShelfWhenComplete()) {
53         [item_controller_ remove];  // We're deleted now!
54         return;
55       }
56       // fall through
57     case DownloadItem::IN_PROGRESS:
58     case DownloadItem::CANCELLED:
59       [item_controller_ setStateFromDownload:&download_model_];
60       break;
61     case DownloadItem::INTERRUPTED:
62       [item_controller_ updateToolTip];
63       [item_controller_ setStateFromDownload:&download_model_];
64       break;
65     default:
66       NOTREACHED();
67   }
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();
81   if (!icon_manager)
82     return;
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);
88   if (icon) {
89     [item_controller_ setIcon:icon->ToNSImage()];
90     return;
91   }
93   // The icon isn't cached, load it asynchronously.
94   icon_manager->LoadIcon(file,
95                          IconLoader::ALL,
96                          base::Bind(&DownloadItemMac::OnExtractIconComplete,
97                                     base::Unretained(this)),
98                          &cancelable_task_tracker_);
101 void DownloadItemMac::OnExtractIconComplete(gfx::Image* icon) {
102   if (!icon)
103     return;
104   [item_controller_ setIcon:icon->ToNSImage()];