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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 }; | 132 }; |
133 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, | 133 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, |
134 download_danger_type_enum_changed); | 134 download_danger_type_enum_changed); |
135 | 135 |
136 // Note: Any change to the state strings, should be accompanied by a | 136 // Note: Any change to the state strings, should be accompanied by a |
137 // corresponding change to downloads.json. | 137 // corresponding change to downloads.json. |
138 const char* kStateStrings[] = { | 138 const char* kStateStrings[] = { |
139 kStateInProgress, | 139 kStateInProgress, |
140 kStateComplete, | 140 kStateComplete, |
141 kStateInterrupted, | 141 kStateInterrupted, |
| 142 NULL, |
142 kStateInterrupted, | 143 kStateInterrupted, |
143 }; | 144 }; |
144 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, | 145 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, |
145 download_item_state_enum_changed); | 146 download_item_state_enum_changed); |
146 | 147 |
147 const char* DangerString(content::DownloadDangerType danger) { | 148 const char* DangerString(content::DownloadDangerType danger) { |
148 DCHECK(danger >= 0); | 149 DCHECK(danger >= 0); |
149 DCHECK(danger < static_cast<content::DownloadDangerType>( | 150 DCHECK(danger < static_cast<content::DownloadDangerType>( |
150 arraysize(kDangerStrings))); | 151 arraysize(kDangerStrings))); |
151 return kDangerStrings[danger]; | 152 return kDangerStrings[danger]; |
152 } | 153 } |
153 | 154 |
154 content::DownloadDangerType DangerEnumFromString(const std::string& danger) { | 155 content::DownloadDangerType DangerEnumFromString(const std::string& danger) { |
155 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) { | 156 for (size_t i = 0; i < arraysize(kDangerStrings); ++i) { |
156 if (danger == kDangerStrings[i]) | 157 if (danger == kDangerStrings[i]) |
157 return static_cast<content::DownloadDangerType>(i); | 158 return static_cast<content::DownloadDangerType>(i); |
158 } | 159 } |
159 return content::DOWNLOAD_DANGER_TYPE_MAX; | 160 return content::DOWNLOAD_DANGER_TYPE_MAX; |
160 } | 161 } |
161 | 162 |
162 const char* StateString(DownloadItem::DownloadState state) { | 163 const char* StateString(DownloadItem::DownloadState state) { |
163 DCHECK(state >= 0); | 164 DCHECK(state >= 0); |
164 DCHECK(state < static_cast<DownloadItem::DownloadState>( | 165 DCHECK(state < static_cast<DownloadItem::DownloadState>( |
165 arraysize(kStateStrings))); | 166 arraysize(kStateStrings))); |
| 167 DCHECK(state != DownloadItem::REMOVING); |
166 return kStateStrings[state]; | 168 return kStateStrings[state]; |
167 } | 169 } |
168 | 170 |
169 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { | 171 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { |
170 for (size_t i = 0; i < arraysize(kStateStrings); ++i) { | 172 for (size_t i = 0; i < arraysize(kStateStrings); ++i) { |
171 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i])) | 173 if ((kStateStrings[i] != NULL) && (state == kStateStrings[i])) |
172 return static_cast<DownloadItem::DownloadState>(i); | 174 return static_cast<DownloadItem::DownloadState>(i); |
173 } | 175 } |
174 return DownloadItem::MAX_DOWNLOAD_STATE; | 176 return DownloadItem::MAX_DOWNLOAD_STATE; |
175 } | 177 } |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() | 813 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() |
812 : fires(0), | 814 : fires(0), |
813 total(0) { | 815 total(0) { |
814 } | 816 } |
815 | 817 |
816 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { | 818 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { |
817 if (total > 0) | 819 if (total > 0) |
818 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); | 820 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); |
819 } | 821 } |
820 | 822 |
821 void ExtensionDownloadsEventRouter::OnDownloadDestroyed(DownloadItem* item) { | |
822 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
823 int download_id = item->GetId(); | |
824 downloads_.erase(download_id); | |
825 item->RemoveObserver(this); | |
826 delete item_jsons_[download_id]; | |
827 item_jsons_.erase(download_id); | |
828 delete on_changed_stats_[download_id]; | |
829 on_changed_stats_.erase(download_id); | |
830 } | |
831 | |
832 void ExtensionDownloadsEventRouter::OnDownloadRemoved(DownloadItem* item) { | |
833 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
834 if (!profile_) | |
835 return; | |
836 int download_id = item->GetId(); | |
837 DispatchEvent(extensions::event_names::kOnDownloadErased, | |
838 base::Value::CreateIntegerValue(download_id)); | |
839 } | |
840 | |
841 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | 823 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { |
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 824 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
843 if (!profile_) | 825 int download_id = item->GetId(); |
| 826 if (item->GetState() == DownloadItem::REMOVING) { |
| 827 // The REMOVING state indicates that this item is being erased. |
| 828 // Let's unregister as an observer so that we don't see any more updates |
| 829 // from it, dispatch the onErased event, and remove its json and is |
| 830 // OnChangedStat from our maps. |
| 831 downloads_.erase(download_id); |
| 832 item->RemoveObserver(this); |
| 833 DispatchEvent(extensions::event_names::kOnDownloadErased, |
| 834 base::Value::CreateIntegerValue(download_id)); |
| 835 delete item_jsons_[download_id]; |
| 836 item_jsons_.erase(download_id); |
| 837 delete on_changed_stats_[download_id]; |
| 838 on_changed_stats_.erase(download_id); |
844 return; | 839 return; |
845 int download_id = item->GetId(); | 840 } |
846 | 841 |
847 base::DictionaryValue* old_json = item_jsons_[download_id]; | 842 base::DictionaryValue* old_json = item_jsons_[download_id]; |
848 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); | 843 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); |
849 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); | 844 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); |
850 delta->SetInteger(kIdKey, download_id); | 845 delta->SetInteger(kIdKey, download_id); |
851 std::set<std::string> new_fields; | 846 std::set<std::string> new_fields; |
852 bool changed = false; | 847 bool changed = false; |
853 | 848 |
854 // For each field in the new json representation of the item except the | 849 // For each field in the new json representation of the item except the |
855 // bytesReceived field, if the field has changed from the previous old json, | 850 // bytesReceived field, if the field has changed from the previous old json, |
(...skipping 28 matching lines...) Expand all Loading... |
884 // Update the OnChangedStat and dispatch the event if something significant | 879 // Update the OnChangedStat and dispatch the event if something significant |
885 // changed. Replace the stored json with the new json. | 880 // changed. Replace the stored json with the new json. |
886 ++(on_changed_stats_[download_id]->total); | 881 ++(on_changed_stats_[download_id]->total); |
887 if (changed) { | 882 if (changed) { |
888 DispatchEvent(extensions::event_names::kOnDownloadChanged, delta.release()); | 883 DispatchEvent(extensions::event_names::kOnDownloadChanged, delta.release()); |
889 ++(on_changed_stats_[download_id]->fires); | 884 ++(on_changed_stats_[download_id]->fires); |
890 } | 885 } |
891 item_jsons_[download_id]->Swap(new_json.get()); | 886 item_jsons_[download_id]->Swap(new_json.get()); |
892 } | 887 } |
893 | 888 |
| 889 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { |
| 890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 891 } |
| 892 |
894 void ExtensionDownloadsEventRouter::OnDownloadCreated( | 893 void ExtensionDownloadsEventRouter::OnDownloadCreated( |
895 DownloadManager* manager, DownloadItem* download_item) { | 894 DownloadManager* manager, DownloadItem* download_item) { |
896 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
897 DCHECK(manager_ == manager); | 896 DCHECK(manager_ == manager); |
898 if (download_item->IsTemporary()) return; | 897 if (download_item->IsTemporary()) return; |
899 | 898 |
900 download_item->AddObserver(this); | 899 download_item->AddObserver(this); |
901 scoped_ptr<base::DictionaryValue> json_item( | 900 scoped_ptr<base::DictionaryValue> json_item( |
902 DownloadItemToJSON(download_item)); | 901 DownloadItemToJSON(download_item)); |
903 DispatchEvent(extensions::event_names::kOnDownloadCreated, | 902 DispatchEvent(extensions::event_names::kOnDownloadCreated, |
904 json_item->DeepCopy()); | 903 json_item->DeepCopy()); |
905 int32 download_id = download_item->GetId(); | 904 int32 download_id = download_item->GetId(); |
906 DCHECK(item_jsons_.find(download_id) == item_jsons_.end()); | 905 DCHECK(item_jsons_.find(download_id) == item_jsons_.end()); |
907 on_changed_stats_[download_id] = new OnChangedStat(); | 906 on_changed_stats_[download_id] = new OnChangedStat(); |
908 item_jsons_[download_id] = json_item.release(); | 907 item_jsons_[download_id] = json_item.release(); |
909 downloads_[download_id] = download_item; | 908 downloads_[download_id] = download_item; |
910 } | 909 } |
911 | 910 |
912 void ExtensionDownloadsEventRouter::ManagerGoingDown( | 911 void ExtensionDownloadsEventRouter::ManagerGoingDown( |
913 DownloadManager* manager) { | 912 DownloadManager* manager) { |
914 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 913 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
915 manager_->RemoveObserver(this); | 914 manager_->RemoveObserver(this); |
916 manager_ = NULL; | 915 manager_ = NULL; |
917 profile_ = NULL; | |
918 } | 916 } |
919 | 917 |
920 void ExtensionDownloadsEventRouter::DispatchEvent( | 918 void ExtensionDownloadsEventRouter::DispatchEvent( |
921 const char* event_name, base::Value* arg) { | 919 const char* event_name, base::Value* arg) { |
922 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
923 base::ListValue args; | 921 base::ListValue args; |
924 args.Append(arg); | 922 args.Append(arg); |
925 std::string json_args; | 923 std::string json_args; |
926 base::JSONWriter::Write(&args, &json_args); | 924 base::JSONWriter::Write(&args, &json_args); |
927 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 925 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
928 event_name, | 926 event_name, |
929 json_args, | 927 json_args, |
930 profile_, | 928 profile_, |
931 GURL(), | 929 GURL(), |
932 extensions::EventFilteringInfo()); | 930 extensions::EventFilteringInfo()); |
933 | 931 |
934 DownloadsNotificationSource notification_source; | 932 DownloadsNotificationSource notification_source; |
935 notification_source.event_name = event_name; | 933 notification_source.event_name = event_name; |
936 notification_source.profile = profile_; | 934 notification_source.profile = profile_; |
937 content::NotificationService::current()->Notify( | 935 content::NotificationService::current()->Notify( |
938 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, | 936 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, |
939 content::Source<DownloadsNotificationSource>(¬ification_source), | 937 content::Source<DownloadsNotificationSource>(¬ification_source), |
940 content::Details<std::string>(&json_args)); | 938 content::Details<std::string>(&json_args)); |
941 } | 939 } |
OLD | NEW |