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