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