OLD | NEW |
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 }; | 128 }; |
129 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, | 129 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, |
130 download_danger_type_enum_changed); | 130 download_danger_type_enum_changed); |
131 | 131 |
132 // Note: Any change to the state strings, should be accompanied by a | 132 // Note: Any change to the state strings, should be accompanied by a |
133 // corresponding change to downloads.json. | 133 // corresponding change to downloads.json. |
134 const char* kStateStrings[] = { | 134 const char* kStateStrings[] = { |
135 kStateInProgress, | 135 kStateInProgress, |
136 kStateComplete, | 136 kStateComplete, |
137 kStateInterrupted, | 137 kStateInterrupted, |
138 NULL, | |
139 kStateInterrupted, | 138 kStateInterrupted, |
140 }; | 139 }; |
141 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, | 140 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, |
142 download_item_state_enum_changed); | 141 download_item_state_enum_changed); |
143 | 142 |
144 const char* DangerString(content::DownloadDangerType danger) { | 143 const char* DangerString(content::DownloadDangerType danger) { |
145 DCHECK(danger >= 0); | 144 DCHECK(danger >= 0); |
146 DCHECK(danger < static_cast<content::DownloadDangerType>( | 145 DCHECK(danger < static_cast<content::DownloadDangerType>( |
147 arraysize(kDangerStrings))); | 146 arraysize(kDangerStrings))); |
148 return kDangerStrings[danger]; | 147 return kDangerStrings[danger]; |
149 } | 148 } |
150 | 149 |
151 content::DownloadDangerType DangerEnumFromString(const std::string& danger) { | 150 content::DownloadDangerType DangerEnumFromString(const std::string& danger) { |
152 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) { | 151 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) { |
153 if (danger == kDangerStrings[i]) | 152 if (danger == kDangerStrings[i]) |
154 return static_cast<content::DownloadDangerType>(i); | 153 return static_cast<content::DownloadDangerType>(i); |
155 } | 154 } |
156 return content::DOWNLOAD_DANGER_TYPE_MAX; | 155 return content::DOWNLOAD_DANGER_TYPE_MAX; |
157 } | 156 } |
158 | 157 |
159 const char* StateString(DownloadItem::DownloadState state) { | 158 const char* StateString(DownloadItem::DownloadState state) { |
160 DCHECK(state >= 0); | 159 DCHECK(state >= 0); |
161 DCHECK(state < static_cast<DownloadItem::DownloadState>( | 160 DCHECK(state < static_cast<DownloadItem::DownloadState>( |
162 arraysize(kStateStrings))); | 161 arraysize(kStateStrings))); |
163 DCHECK(state != DownloadItem::REMOVING); | |
164 return kStateStrings[state]; | 162 return kStateStrings[state]; |
165 } | 163 } |
166 | 164 |
167 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { | 165 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { |
168 for (size_t i = 0; i < arraysize(kStateStrings); ++i) { | 166 for (size_t i = 0; i < arraysize(kStateStrings); ++i) { |
169 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i])) | 167 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i])) |
170 return static_cast<DownloadItem::DownloadState>(i); | 168 return static_cast<DownloadItem::DownloadState>(i); |
171 } | 169 } |
172 return DownloadItem::MAX_DOWNLOAD_STATE; | 170 return DownloadItem::MAX_DOWNLOAD_STATE; |
173 } | 171 } |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() | 948 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() |
951 : fires(0), | 949 : fires(0), |
952 total(0) { | 950 total(0) { |
953 } | 951 } |
954 | 952 |
955 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { | 953 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { |
956 if (total > 0) | 954 if (total > 0) |
957 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); | 955 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); |
958 } | 956 } |
959 | 957 |
| 958 void ExtensionDownloadsEventRouter::OnDownloadDestructed(DownloadItem* item) { |
| 959 if (!profile_) |
| 960 return; |
| 961 int download_id = item->GetId(); |
| 962 downloads_.erase(download_id); |
| 963 item->RemoveObserver(this); |
| 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 } |
| 969 |
| 970 void ExtensionDownloadsEventRouter::OnDownloadRemoved(DownloadItem* item) { |
| 971 if (!profile_) |
| 972 return; |
| 973 int download_id = item->GetId(); |
| 974 DispatchEvent(extension_event_names::kOnDownloadErased, |
| 975 base::Value::CreateIntegerValue(download_id)); |
| 976 } |
| 977 |
960 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | 978 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { |
961 if (!profile_) | 979 if (!profile_) |
962 return; | 980 return; |
963 int download_id = item->GetId(); | 981 int download_id = item->GetId(); |
964 if (item->GetState() == DownloadItem::REMOVING) { | |
965 // The REMOVING state indicates that this item is being erased. | |
966 // Let's unregister as an observer so that we don't see any more updates | |
967 // from it, dispatch the onErased event, and remove its json and is | |
968 // OnChangedStat from our maps. | |
969 downloads_.erase(download_id); | |
970 item->RemoveObserver(this); | |
971 DispatchEvent(extension_event_names::kOnDownloadErased, | |
972 base::Value::CreateIntegerValue(download_id)); | |
973 delete item_jsons_[download_id]; | |
974 item_jsons_.erase(download_id); | |
975 delete on_changed_stats_[download_id]; | |
976 on_changed_stats_.erase(download_id); | |
977 return; | |
978 } | |
979 | 982 |
980 base::DictionaryValue* old_json = item_jsons_[download_id]; | 983 base::DictionaryValue* old_json = item_jsons_[download_id]; |
981 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); | 984 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); |
982 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); | 985 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); |
983 delta->SetInteger(kIdKey, download_id); | 986 delta->SetInteger(kIdKey, download_id); |
984 std::set<std::string> new_fields; | 987 std::set<std::string> new_fields; |
985 bool changed = false; | 988 bool changed = false; |
986 | 989 |
987 // For each field in the new json representation of the item except the | 990 // For each field in the new json representation of the item except the |
988 // bytesReceived field, if the field has changed from the previous old json, | 991 // bytesReceived field, if the field has changed from the previous old json, |
(...skipping 28 matching lines...) Expand all Loading... |
1017 // Update the OnChangedStat and dispatch the event if something significant | 1020 // Update the OnChangedStat and dispatch the event if something significant |
1018 // changed. Replace the stored json with the new json. | 1021 // changed. Replace the stored json with the new json. |
1019 ++(on_changed_stats_[download_id]->total); | 1022 ++(on_changed_stats_[download_id]->total); |
1020 if (changed) { | 1023 if (changed) { |
1021 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | 1024 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
1022 ++(on_changed_stats_[download_id]->fires); | 1025 ++(on_changed_stats_[download_id]->fires); |
1023 } | 1026 } |
1024 item_jsons_[download_id]->Swap(new_json.get()); | 1027 item_jsons_[download_id]->Swap(new_json.get()); |
1025 } | 1028 } |
1026 | 1029 |
1027 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | |
1028 } | |
1029 | |
1030 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { | 1030 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { |
1031 if (!profile_) | 1031 if (!profile_) |
1032 return; | 1032 return; |
1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1034 DCHECK(manager_ == manager); | 1034 DCHECK(manager_ == manager); |
1035 typedef std::set<int> DownloadIdSet; | 1035 typedef std::set<int> DownloadIdSet; |
1036 | 1036 |
1037 // Get all the download items. | 1037 // Get all the download items. |
1038 DownloadManager::DownloadVector current_vec; | 1038 DownloadManager::DownloadVector current_vec; |
1039 manager_->SearchDownloads(string16(), ¤t_vec); | 1039 manager_->SearchDownloads(string16(), ¤t_vec); |
(...skipping 30 matching lines...) Expand all Loading... |
1070 iter != new_set.end(); ++iter) { | 1070 iter != new_set.end(); ++iter) { |
1071 scoped_ptr<base::DictionaryValue> item( | 1071 scoped_ptr<base::DictionaryValue> item( |
1072 DownloadItemToJSON(current_map[*iter])); | 1072 DownloadItemToJSON(current_map[*iter])); |
1073 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy()); | 1073 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy()); |
1074 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); | 1074 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); |
1075 on_changed_stats_[*iter] = new OnChangedStat(); | 1075 on_changed_stats_[*iter] = new OnChangedStat(); |
1076 current_map[*iter]->AddObserver(this); | 1076 current_map[*iter]->AddObserver(this); |
1077 item_jsons_[*iter] = item.release(); | 1077 item_jsons_[*iter] = item.release(); |
1078 } | 1078 } |
1079 downloads_.swap(current_map); | 1079 downloads_.swap(current_map); |
1080 | |
1081 // Dispatching onErased is handled in OnDownloadUpdated when an item | |
1082 // transitions to the REMOVING state. | |
1083 } | 1080 } |
1084 | 1081 |
1085 void ExtensionDownloadsEventRouter::ManagerGoingDown( | 1082 void ExtensionDownloadsEventRouter::ManagerGoingDown( |
1086 DownloadManager* manager) { | 1083 DownloadManager* manager) { |
1087 manager_->RemoveObserver(this); | 1084 manager_->RemoveObserver(this); |
1088 manager_ = NULL; | 1085 manager_ = NULL; |
1089 profile_ = NULL; | 1086 profile_ = NULL; |
1090 } | 1087 } |
1091 | 1088 |
1092 void ExtensionDownloadsEventRouter::DispatchEvent( | 1089 void ExtensionDownloadsEventRouter::DispatchEvent( |
1093 const char* event_name, base::Value* arg) { | 1090 const char* event_name, base::Value* arg) { |
1094 if (!profile_ || !profile_->GetExtensionEventRouter()) | 1091 if (!profile_ || !profile_->GetExtensionEventRouter()) |
1095 return; | 1092 return; |
1096 base::ListValue args; | 1093 base::ListValue args; |
1097 args.Append(arg); | 1094 args.Append(arg); |
1098 std::string json_args; | 1095 std::string json_args; |
1099 base::JSONWriter::Write(&args, &json_args); | 1096 base::JSONWriter::Write(&args, &json_args); |
1100 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 1097 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
1101 event_name, | 1098 event_name, |
1102 json_args, | 1099 json_args, |
1103 profile_, | 1100 profile_, |
1104 GURL()); | 1101 GURL()); |
1105 } | 1102 } |
OLD | NEW |