Chromium Code Reviews
|
| 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 ClearDownloadItems(); | |
|
asanka
2012/08/07 23:29:59
If you are going to delete |this| in ManagerGoingD
ananta
2012/08/07 23:44:25
Done.
| |
| 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); | |
|
asanka
2012/08/07 23:29:59
Did you intend to delete |this| here?
ananta
2012/08/07 23:44:25
Yeah. Thanks for catching this
| |
| 43 } | |
| 44 | |
| 45 void DownloadCompletionObserver::OnDownloadUpdated(DownloadItem* download) { | |
| 46 if (!base::win::IsMetroProcess()) | |
| 47 return; | |
| 48 | |
| 49 switch (download->GetState()) { | |
| 50 case DownloadItem::COMPLETE: { | |
| 51 if (!download->GetOpenWhenComplete() && | |
| 52 !download->ShouldOpenFileBasedOnExtension() && | |
| 53 !download->IsTemporary() && | |
| 54 !download->GetAutoOpened()) { | |
| 55 // In Windows 8 metro mode display a metro style notification which | |
| 56 // informs the user that the download is complete. | |
| 57 HMODULE metro = base::win::GetMetroModule(); | |
| 58 base::win::MetroNotification display_notification = | |
| 59 reinterpret_cast< base::win::MetroNotification>( | |
| 60 ::GetProcAddress(metro, "DisplayNotification")); | |
| 61 DCHECK(display_notification); | |
| 62 if (display_notification) { | |
| 63 string16 title = l10n_util::GetStringUTF16( | |
| 64 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION_TITLE); | |
| 65 string16 body = l10n_util::GetStringUTF16( | |
| 66 IDS_METRO_DOWNLOAD_COMPLETE_NOTIFICATION); | |
| 67 | |
| 68 // Dummy notification id. Every metro style notification needs a | |
| 69 // unique notification id. | |
| 70 std::string notification_id = kDownloadNotificationPrefix; | |
| 71 notification_id += base::IntToString(g_next_notification_id++); | |
| 72 | |
| 73 display_notification(download->GetURL().spec().c_str(), | |
| 74 "", | |
| 75 title.c_str(), | |
| 76 body.c_str(), | |
| 77 L"", | |
| 78 notification_id.c_str()); | |
| 79 } | |
| 80 } | |
| 81 if (ContainsKey(download_items_, download)) { | |
|
asanka
2012/08/07 23:29:59
Nit: This should be a DCHECK() and the .erase() an
ananta
2012/08/07 23:44:25
Done. Yeah. We should not be receiving status upda
| |
| 82 download_items_.erase(download); | |
| 83 download->RemoveObserver(this); | |
| 84 } | |
| 85 break; | |
| 86 } | |
| 87 | |
| 88 case DownloadItem::REMOVING: | |
| 89 case DownloadItem::INTERRUPTED: | |
| 90 case DownloadItem::CANCELLED: { | |
| 91 if (ContainsKey(download_items_, download)) { | |
|
asanka
2012/08/07 23:29:59
Ditto.
ananta
2012/08/07 23:44:25
Done.
| |
| 92 download_items_.erase(download); | |
| 93 download->RemoveObserver(this); | |
| 94 } | |
| 95 break; | |
| 96 } | |
| 97 | |
| 98 default: | |
| 99 break; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void DownloadCompletionObserver::ClearDownloadItems() { | |
| 104 for (std::set<DownloadItem*>::iterator it = download_items_.begin(); | |
| 105 it != download_items_.end(); ++it) { | |
| 106 (*it)->RemoveObserver(this); | |
| 107 } | |
| 108 download_items_.clear(); | |
| 109 } | |
| OLD | NEW |