Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api.cc

Issue 10704026: Reland DownloadItem::Observer::OnDownloadDestroyed() replaces DownloadItem::REMOVING (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 5 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cctype> 8 #include <cctype>
9 #include <iterator> 9 #include <iterator>
10 #include <set> 10 #include <set>
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 }; 130 };
131 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, 131 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX,
132 download_danger_type_enum_changed); 132 download_danger_type_enum_changed);
133 133
134 // Note: Any change to the state strings, should be accompanied by a 134 // Note: Any change to the state strings, should be accompanied by a
135 // corresponding change to downloads.json. 135 // corresponding change to downloads.json.
136 const char* kStateStrings[] = { 136 const char* kStateStrings[] = {
137 kStateInProgress, 137 kStateInProgress,
138 kStateComplete, 138 kStateComplete,
139 kStateInterrupted, 139 kStateInterrupted,
140 NULL,
141 kStateInterrupted, 140 kStateInterrupted,
142 }; 141 };
143 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, 142 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE,
144 download_item_state_enum_changed); 143 download_item_state_enum_changed);
145 144
146 const char* DangerString(content::DownloadDangerType danger) { 145 const char* DangerString(content::DownloadDangerType danger) {
147 DCHECK(danger >= 0); 146 DCHECK(danger >= 0);
148 DCHECK(danger < static_cast<content::DownloadDangerType>( 147 DCHECK(danger < static_cast<content::DownloadDangerType>(
149 arraysize(kDangerStrings))); 148 arraysize(kDangerStrings)));
150 return kDangerStrings[danger]; 149 return kDangerStrings[danger];
151 } 150 }
152 151
153 content::DownloadDangerType DangerEnumFromString(const std::string& danger) { 152 content::DownloadDangerType DangerEnumFromString(const std::string& danger) {
154 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) { 153 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) {
155 if (danger == kDangerStrings[i]) 154 if (danger == kDangerStrings[i])
156 return static_cast<content::DownloadDangerType>(i); 155 return static_cast<content::DownloadDangerType>(i);
157 } 156 }
158 return content::DOWNLOAD_DANGER_TYPE_MAX; 157 return content::DOWNLOAD_DANGER_TYPE_MAX;
159 } 158 }
160 159
161 const char* StateString(DownloadItem::DownloadState state) { 160 const char* StateString(DownloadItem::DownloadState state) {
162 DCHECK(state >= 0); 161 DCHECK(state >= 0);
163 DCHECK(state < static_cast<DownloadItem::DownloadState>( 162 DCHECK(state < static_cast<DownloadItem::DownloadState>(
164 arraysize(kStateStrings))); 163 arraysize(kStateStrings)));
165 DCHECK(state != DownloadItem::REMOVING);
166 return kStateStrings[state]; 164 return kStateStrings[state];
167 } 165 }
168 166
169 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { 167 DownloadItem::DownloadState StateEnumFromString(const std::string& state) {
170 for (size_t i = 0; i < arraysize(kStateStrings); ++i) { 168 for (size_t i = 0; i < arraysize(kStateStrings); ++i) {
171 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i])) 169 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i]))
172 return static_cast<DownloadItem::DownloadState>(i); 170 return static_cast<DownloadItem::DownloadState>(i);
173 } 171 }
174 return DownloadItem::MAX_DOWNLOAD_STATE; 172 return DownloadItem::MAX_DOWNLOAD_STATE;
175 } 173 }
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() 940 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat()
943 : fires(0), 941 : fires(0),
944 total(0) { 942 total(0) {
945 } 943 }
946 944
947 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { 945 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() {
948 if (total > 0) 946 if (total > 0)
949 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); 947 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total));
950 } 948 }
951 949
950 void ExtensionDownloadsEventRouter::OnDownloadDestroyed(DownloadItem* item) {
951 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
952 if (!profile_)
953 return;
Randy Smith (Not in Mondays) 2012/07/11 17:55:37 If we're getting notification and the download is
benjhayden 2012/07/13 20:03:17 Done.
954 int download_id = item->GetId();
955 downloads_.erase(download_id);
956 item->RemoveObserver(this);
957 delete item_jsons_[download_id];
958 item_jsons_.erase(download_id);
959 delete on_changed_stats_[download_id];
960 on_changed_stats_.erase(download_id);
961 }
962
963 void ExtensionDownloadsEventRouter::OnDownloadRemoved(DownloadItem* item) {
964 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
965 if (!profile_)
966 return;
967 int download_id = item->GetId();
968 DispatchEvent(extension_event_names::kOnDownloadErased,
969 base::Value::CreateIntegerValue(download_id));
970 }
971
952 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { 972 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) {
953 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 973 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
974 if (!profile_)
975 return;
954 int download_id = item->GetId(); 976 int download_id = item->GetId();
955 if (item->GetState() == DownloadItem::REMOVING) {
956 // The REMOVING state indicates that this item is being erased.
957 // Let's unregister as an observer so that we don't see any more updates
958 // from it, dispatch the onErased event, and remove its json and is
959 // OnChangedStat from our maps.
960 downloads_.erase(download_id);
961 item->RemoveObserver(this);
962 DispatchEvent(extension_event_names::kOnDownloadErased,
963 base::Value::CreateIntegerValue(download_id));
964 delete item_jsons_[download_id];
965 item_jsons_.erase(download_id);
966 delete on_changed_stats_[download_id];
967 on_changed_stats_.erase(download_id);
968 return;
969 }
970 977
971 base::DictionaryValue* old_json = item_jsons_[download_id]; 978 base::DictionaryValue* old_json = item_jsons_[download_id];
972 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); 979 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item));
973 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); 980 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue());
974 delta->SetInteger(kIdKey, download_id); 981 delta->SetInteger(kIdKey, download_id);
975 std::set<std::string> new_fields; 982 std::set<std::string> new_fields;
976 bool changed = false; 983 bool changed = false;
977 984
978 // For each field in the new json representation of the item except the 985 // For each field in the new json representation of the item except the
979 // bytesReceived field, if the field has changed from the previous old json, 986 // bytesReceived field, if the field has changed from the previous old json,
(...skipping 28 matching lines...) Expand all
1008 // Update the OnChangedStat and dispatch the event if something significant 1015 // Update the OnChangedStat and dispatch the event if something significant
1009 // changed. Replace the stored json with the new json. 1016 // changed. Replace the stored json with the new json.
1010 ++(on_changed_stats_[download_id]->total); 1017 ++(on_changed_stats_[download_id]->total);
1011 if (changed) { 1018 if (changed) {
1012 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); 1019 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release());
1013 ++(on_changed_stats_[download_id]->fires); 1020 ++(on_changed_stats_[download_id]->fires);
1014 } 1021 }
1015 item_jsons_[download_id]->Swap(new_json.get()); 1022 item_jsons_[download_id]->Swap(new_json.get());
1016 } 1023 }
1017 1024
1018 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) {
1019 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1020 }
1021
1022 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { 1025 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) {
1023 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1026 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1024 DCHECK(manager_ == manager); 1027 DCHECK(manager_ == manager);
1025 typedef std::set<int> DownloadIdSet; 1028 typedef std::set<int> DownloadIdSet;
1026 1029
1027 // Get all the download items. 1030 // Get all the download items.
1028 DownloadManager::DownloadVector current_vec; 1031 DownloadManager::DownloadVector current_vec;
1029 manager_->SearchDownloads(string16(), &current_vec); 1032 manager_->SearchDownloads(string16(), &current_vec);
1030 1033
1031 // Populate set<>s of download item identifiers so that we can find 1034 // Populate set<>s of download item identifiers so that we can find
(...skipping 28 matching lines...) Expand all
1060 iter != new_set.end(); ++iter) { 1063 iter != new_set.end(); ++iter) {
1061 scoped_ptr<base::DictionaryValue> item( 1064 scoped_ptr<base::DictionaryValue> item(
1062 DownloadItemToJSON(current_map[*iter])); 1065 DownloadItemToJSON(current_map[*iter]));
1063 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy()); 1066 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy());
1064 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); 1067 DCHECK(item_jsons_.find(*iter) == item_jsons_.end());
1065 on_changed_stats_[*iter] = new OnChangedStat(); 1068 on_changed_stats_[*iter] = new OnChangedStat();
1066 current_map[*iter]->AddObserver(this); 1069 current_map[*iter]->AddObserver(this);
1067 item_jsons_[*iter] = item.release(); 1070 item_jsons_[*iter] = item.release();
1068 } 1071 }
1069 downloads_.swap(current_map); 1072 downloads_.swap(current_map);
1070
1071 // Dispatching onErased is handled in OnDownloadUpdated when an item
1072 // transitions to the REMOVING state.
1073 } 1073 }
1074 1074
1075 void ExtensionDownloadsEventRouter::ManagerGoingDown( 1075 void ExtensionDownloadsEventRouter::ManagerGoingDown(
1076 DownloadManager* manager) { 1076 DownloadManager* manager) {
1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1078 manager_->RemoveObserver(this); 1078 manager_->RemoveObserver(this);
1079 manager_ = NULL; 1079 manager_ = NULL;
1080 } 1080 }
1081 1081
1082 void ExtensionDownloadsEventRouter::DispatchEvent( 1082 void ExtensionDownloadsEventRouter::DispatchEvent(
(...skipping 11 matching lines...) Expand all
1094 extensions::EventFilteringInfo()); 1094 extensions::EventFilteringInfo());
1095 1095
1096 DownloadsNotificationSource notification_source; 1096 DownloadsNotificationSource notification_source;
1097 notification_source.event_name = event_name; 1097 notification_source.event_name = event_name;
1098 notification_source.profile = profile_; 1098 notification_source.profile = profile_;
1099 content::NotificationService::current()->Notify( 1099 content::NotificationService::current()->Notify(
1100 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, 1100 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT,
1101 content::Source<DownloadsNotificationSource>(&notification_source), 1101 content::Source<DownloadsNotificationSource>(&notification_source),
1102 content::Details<std::string>(&json_args)); 1102 content::Details<std::string>(&json_args));
1103 } 1103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698