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 if (!base::win::IsMetroProcess()) |
| 48 return; |
| 49 |
| 50 switch (download->GetState()) { |
| 51 case DownloadItem::COMPLETE: { |
| 52 if (!download->GetOpenWhenComplete() && |
| 53 !download->ShouldOpenFileBasedOnExtension() && |
| 54 !download->IsTemporary() && |
| 55 !download->GetAutoOpened()) { |
| 56 // In Windows 8 metro mode display a metro style notification which |
| 57 // informs the user that the download is complete. |
| 58 HMODULE metro = base::win::GetMetroModule(); |
| 59 base::win::MetroNotification display_notification = |
| 60 reinterpret_cast< base::win::MetroNotification>( |
| 61 ::GetProcAddress(metro, "DisplayNotification")); |
| 62 DCHECK(display_notification); |
| 63 if (display_notification) { |
| 64 string16 title = l10n_util::GetStringUTF16( |
| 65 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION_TITLE); |
| 66 string16 body = l10n_util::GetStringUTF16( |
| 67 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION); |
| 68 |
| 69 // Dummy notification id. Every metro style notification needs a |
| 70 // unique notification id. |
| 71 std::string notification_id = kDownloadNotificationPrefix; |
| 72 notification_id += base::IntToString(g_next_notification_id++); |
| 73 |
| 74 display_notification(download->GetURL().spec().c_str(), |
| 75 "", |
| 76 title.c_str(), |
| 77 body.c_str(), |
| 78 L"", |
| 79 notification_id.c_str()); |
| 80 } |
| 81 } |
| 82 DCHECK(ContainsKey(download_items_, download)); |
| 83 download_items_.erase(download); |
| 84 download->RemoveObserver(this); |
| 85 break; |
| 86 } |
| 87 |
| 88 case DownloadItem::REMOVING: |
| 89 case DownloadItem::INTERRUPTED: |
| 90 case DownloadItem::CANCELLED: { |
| 91 DCHECK(ContainsKey(download_items_, download)); |
| 92 download_items_.erase(download); |
| 93 download->RemoveObserver(this); |
| 94 break; |
| 95 } |
| 96 |
| 97 default: |
| 98 break; |
| 99 } |
| 100 } |
| 101 |
| 102 void DownloadCompletionObserver::ClearDownloadItems() { |
| 103 for (std::set<DownloadItem*>::iterator it = download_items_.begin(); |
| 104 it != download_items_.end(); ++it) { |
| 105 (*it)->RemoveObserver(this); |
| 106 } |
| 107 download_items_.clear(); |
| 108 } |
OLD | NEW |