OLD | NEW |
| (Empty) |
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/download/download_completion_observer_win.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "base/win/metro.h" | |
13 #include "googleurl/src/gurl.h" | |
14 #include "grit/generated_resources.h" | |
15 #include "ui/base/l10n/l10n_util.h" | |
16 | |
17 using content::DownloadItem; | |
18 using content::DownloadManager; | |
19 | |
20 static const char kDownloadNotificationPrefix[] = "DownloadNotification"; | |
21 int g_next_notification_id = 0; | |
22 | |
23 DownloadCompletionObserver::DownloadCompletionObserver( | |
24 DownloadManager* manager) { | |
25 manager->AddObserver(this); | |
26 } | |
27 | |
28 DownloadCompletionObserver::~DownloadCompletionObserver() { | |
29 DCHECK(download_items_.empty()); | |
30 } | |
31 | |
32 void DownloadCompletionObserver::OnDownloadCreated(DownloadManager* manager, | |
33 DownloadItem* download) { | |
34 if (download->IsInProgress()) { | |
35 download_items_.insert(download); | |
36 download->AddObserver(this); | |
37 } | |
38 } | |
39 | |
40 void DownloadCompletionObserver::ManagerGoingDown(DownloadManager* manager) { | |
41 ClearDownloadItems(); | |
42 manager->RemoveObserver(this); | |
43 delete this; | |
44 } | |
45 | |
46 void DownloadCompletionObserver::OnDownloadUpdated(DownloadItem* download) { | |
47 switch (download->GetState()) { | |
48 case DownloadItem::COMPLETE: { | |
49 if (base::win::IsMetroProcess() && | |
50 !download->GetOpenWhenComplete() && | |
51 !download->ShouldOpenFileBasedOnExtension() && | |
52 !download->IsTemporary() && | |
53 !download->GetAutoOpened()) { | |
54 // In Windows 8 metro mode display a metro style notification which | |
55 // informs the user that the download is complete. | |
56 HMODULE metro = base::win::GetMetroModule(); | |
57 base::win::MetroNotification display_notification = | |
58 reinterpret_cast< base::win::MetroNotification>( | |
59 ::GetProcAddress(metro, "DisplayNotification")); | |
60 DCHECK(display_notification); | |
61 if (display_notification) { | |
62 string16 title = l10n_util::GetStringUTF16( | |
63 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION_TITLE); | |
64 string16 body = l10n_util::GetStringUTF16( | |
65 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION); | |
66 | |
67 // Dummy notification id. Every metro style notification needs a | |
68 // unique notification id. | |
69 std::string notification_id = kDownloadNotificationPrefix; | |
70 notification_id += base::IntToString(g_next_notification_id++); | |
71 | |
72 display_notification(download->GetURL().spec().c_str(), | |
73 "", | |
74 title.c_str(), | |
75 body.c_str(), | |
76 L"", | |
77 notification_id.c_str()); | |
78 } | |
79 } | |
80 DCHECK(ContainsKey(download_items_, download)); | |
81 download_items_.erase(download); | |
82 download->RemoveObserver(this); | |
83 break; | |
84 } | |
85 | |
86 case DownloadItem::REMOVING: | |
87 case DownloadItem::INTERRUPTED: | |
88 case DownloadItem::CANCELLED: { | |
89 DCHECK(ContainsKey(download_items_, download)); | |
90 download_items_.erase(download); | |
91 download->RemoveObserver(this); | |
92 break; | |
93 } | |
94 | |
95 default: | |
96 break; | |
97 } | |
98 } | |
99 | |
100 void DownloadCompletionObserver::ClearDownloadItems() { | |
101 for (std::set<DownloadItem*>::iterator it = download_items_.begin(); | |
102 it != download_items_.end(); ++it) { | |
103 (*it)->RemoveObserver(this); | |
104 } | |
105 download_items_.clear(); | |
106 } | |
OLD | NEW |