[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_item_mac.mm
blob3efffe17d004d0b8b376dcdb01c28f49cf316117
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 ([item_controller_ isDangerousMode] && !download_model_.IsDangerous()) {
34     // We have been approved.
35     [item_controller_ clearDangerousMode];
36   }
38   if (download->GetTargetFilePath() != lastFilePath_) {
39     LoadIcon();
40     lastFilePath_ = download->GetTargetFilePath();
42     [item_controller_ updateToolTip];
43   }
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   }
65 void DownloadItemMac::OnDownloadDestroyed(content::DownloadItem* download) {
66   [item_controller_ remove];  // We're deleted now!
69 void DownloadItemMac::OnDownloadOpened(content::DownloadItem* download) {
70   DCHECK_EQ(download, download_model_.download());
71   [item_controller_ downloadWasOpened];
74 void DownloadItemMac::LoadIcon() {
75   IconManager* icon_manager = g_browser_process->icon_manager();
76   if (!icon_manager) {
77     NOTREACHED();
78     return;
79   }
81   // We may already have this particular image cached.
82   base::FilePath file = download_model_.download()->GetTargetFilePath();
83   gfx::Image* icon = icon_manager->LookupIconFromFilepath(
84       file, IconLoader::ALL);
85   if (icon) {
86     [item_controller_ setIcon:icon->ToNSImage()];
87     return;
88   }
90   // The icon isn't cached, load it asynchronously.
91   icon_manager->LoadIcon(file,
92                          IconLoader::ALL,
93                          base::Bind(&DownloadItemMac::OnExtractIconComplete,
94                                     base::Unretained(this)),
95                          &cancelable_task_tracker_);
98 void DownloadItemMac::OnExtractIconComplete(gfx::Image* icon) {
99   if (!icon)
100     return;
101   [item_controller_ setIcon:icon->ToNSImage()];