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 19 matching lines...) Expand all Loading... |
30 #include "chrome/browser/download/download_service.h" | 30 #include "chrome/browser/download/download_service.h" |
31 #include "chrome/browser/download/download_service_factory.h" | 31 #include "chrome/browser/download/download_service_factory.h" |
32 #include "chrome/browser/download/download_util.h" | 32 #include "chrome/browser/download/download_util.h" |
33 #include "chrome/browser/extensions/extension_event_names.h" | 33 #include "chrome/browser/extensions/extension_event_names.h" |
34 #include "chrome/browser/extensions/extension_event_router.h" | 34 #include "chrome/browser/extensions/extension_event_router.h" |
35 #include "chrome/browser/icon_loader.h" | 35 #include "chrome/browser/icon_loader.h" |
36 #include "chrome/browser/icon_manager.h" | 36 #include "chrome/browser/icon_manager.h" |
37 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 37 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
38 #include "chrome/browser/ui/browser.h" | 38 #include "chrome/browser/ui/browser.h" |
39 #include "chrome/browser/ui/webui/web_ui_util.h" | 39 #include "chrome/browser/ui/webui/web_ui_util.h" |
40 #include "chrome/common/chrome_notification_types.h" | |
41 #include "content/public/browser/download_interrupt_reasons.h" | 40 #include "content/public/browser/download_interrupt_reasons.h" |
42 #include "content/public/browser/download_item.h" | 41 #include "content/public/browser/download_item.h" |
43 #include "content/public/browser/download_save_info.h" | 42 #include "content/public/browser/download_save_info.h" |
44 #include "content/public/browser/notification_service.h" | |
45 #include "content/public/browser/render_process_host.h" | 43 #include "content/public/browser/render_process_host.h" |
46 #include "content/public/browser/render_view_host.h" | 44 #include "content/public/browser/render_view_host.h" |
47 #include "content/public/browser/resource_context.h" | 45 #include "content/public/browser/resource_context.h" |
48 #include "content/public/browser/resource_dispatcher_host.h" | 46 #include "content/public/browser/resource_dispatcher_host.h" |
49 #include "net/base/load_flags.h" | 47 #include "net/base/load_flags.h" |
50 #include "net/http/http_util.h" | 48 #include "net/http/http_util.h" |
51 #include "net/url_request/url_request.h" | 49 #include "net/url_request/url_request.h" |
52 | 50 |
53 using content::BrowserContext; | 51 using content::BrowserContext; |
54 using content::BrowserThread; | 52 using content::BrowserThread; |
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 | 906 |
909 void DownloadsGetFileIconFunction::OnIconURLExtracted(const std::string& url) { | 907 void DownloadsGetFileIconFunction::OnIconURLExtracted(const std::string& url) { |
910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 908 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
911 if (url.empty()) | 909 if (url.empty()) |
912 error_ = download_extension_errors::kIconNotFoundError; | 910 error_ = download_extension_errors::kIconNotFoundError; |
913 else | 911 else |
914 result_.reset(base::Value::CreateStringValue(url)); | 912 result_.reset(base::Value::CreateStringValue(url)); |
915 SendResponse(error_.empty()); | 913 SendResponse(error_.empty()); |
916 } | 914 } |
917 | 915 |
918 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 916 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) |
919 Profile* profile, | |
920 DownloadManager* manager) | |
921 : profile_(profile), | 917 : profile_(profile), |
922 manager_(manager) { | 918 manager_(NULL) { |
923 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 919 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
924 DCHECK(profile_); | 920 DCHECK(profile_); |
925 DCHECK(manager_); | 921 // Register a callback with the DownloadService for this profile to be called |
| 922 // when it creates the DownloadManager, or now if the manager already exists. |
| 923 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( |
| 924 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); |
| 925 } |
| 926 |
| 927 // The only public methods on this class are ModelChanged() and |
| 928 // ManagerGoingDown(), and they are only called by DownloadManager, so |
| 929 // there's no way for any methods on this class to be called before |
| 930 // DownloadService calls Init() via the OnManagerCreated Callback above. |
| 931 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { |
| 932 DCHECK(manager_ == NULL); |
| 933 manager_ = manager; |
| 934 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
926 manager_->AddObserver(this); | 935 manager_->AddObserver(this); |
927 } | 936 } |
928 | 937 |
929 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 938 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
930 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
931 if (manager_ != NULL) | 939 if (manager_ != NULL) |
932 manager_->RemoveObserver(this); | 940 manager_->RemoveObserver(this); |
933 for (ItemMap::const_iterator iter = downloads_.begin(); | 941 for (ItemMap::const_iterator iter = downloads_.begin(); |
934 iter != downloads_.end(); ++iter) { | 942 iter != downloads_.end(); ++iter) { |
935 if (iter->second != NULL) | 943 if (iter->second != NULL) |
936 iter->second->RemoveObserver(this); | 944 iter->second->RemoveObserver(this); |
937 } | 945 } |
938 STLDeleteValues(&item_jsons_); | 946 STLDeleteValues(&item_jsons_); |
939 STLDeleteValues(&on_changed_stats_); | 947 STLDeleteValues(&on_changed_stats_); |
940 } | 948 } |
941 | 949 |
942 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() | 950 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() |
943 : fires(0), | 951 : fires(0), |
944 total(0) { | 952 total(0) { |
945 } | 953 } |
946 | 954 |
947 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { | 955 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { |
948 if (total > 0) | 956 if (total > 0) |
949 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); | 957 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); |
950 } | 958 } |
951 | 959 |
952 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | 960 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { |
953 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 961 if (!profile_) |
| 962 return; |
954 int download_id = item->GetId(); | 963 int download_id = item->GetId(); |
955 if (item->GetState() == DownloadItem::REMOVING) { | 964 if (item->GetState() == DownloadItem::REMOVING) { |
956 // The REMOVING state indicates that this item is being erased. | 965 // 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 | 966 // 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 | 967 // from it, dispatch the onErased event, and remove its json and is |
959 // OnChangedStat from our maps. | 968 // OnChangedStat from our maps. |
960 downloads_.erase(download_id); | 969 downloads_.erase(download_id); |
961 item->RemoveObserver(this); | 970 item->RemoveObserver(this); |
962 DispatchEvent(extension_event_names::kOnDownloadErased, | 971 DispatchEvent(extension_event_names::kOnDownloadErased, |
963 base::Value::CreateIntegerValue(download_id)); | 972 base::Value::CreateIntegerValue(download_id)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 // changed. Replace the stored json with the new json. | 1018 // changed. Replace the stored json with the new json. |
1010 ++(on_changed_stats_[download_id]->total); | 1019 ++(on_changed_stats_[download_id]->total); |
1011 if (changed) { | 1020 if (changed) { |
1012 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | 1021 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
1013 ++(on_changed_stats_[download_id]->fires); | 1022 ++(on_changed_stats_[download_id]->fires); |
1014 } | 1023 } |
1015 item_jsons_[download_id]->Swap(new_json.get()); | 1024 item_jsons_[download_id]->Swap(new_json.get()); |
1016 } | 1025 } |
1017 | 1026 |
1018 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | 1027 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { |
1019 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1020 } | 1028 } |
1021 | 1029 |
1022 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { | 1030 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { |
| 1031 if (!profile_) |
| 1032 return; |
1023 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1024 DCHECK(manager_ == manager); | 1034 DCHECK(manager_ == manager); |
1025 typedef std::set<int> DownloadIdSet; | 1035 typedef std::set<int> DownloadIdSet; |
1026 | 1036 |
1027 // Get all the download items. | 1037 // Get all the download items. |
1028 DownloadManager::DownloadVector current_vec; | 1038 DownloadManager::DownloadVector current_vec; |
1029 manager_->SearchDownloads(string16(), ¤t_vec); | 1039 manager_->SearchDownloads(string16(), ¤t_vec); |
1030 | 1040 |
1031 // Populate set<>s of download item identifiers so that we can find | 1041 // Populate set<>s of download item identifiers so that we can find |
1032 // differences between the old and the new set of download items. | 1042 // differences between the old and the new set of download items. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1067 item_jsons_[*iter] = item.release(); | 1077 item_jsons_[*iter] = item.release(); |
1068 } | 1078 } |
1069 downloads_.swap(current_map); | 1079 downloads_.swap(current_map); |
1070 | 1080 |
1071 // Dispatching onErased is handled in OnDownloadUpdated when an item | 1081 // Dispatching onErased is handled in OnDownloadUpdated when an item |
1072 // transitions to the REMOVING state. | 1082 // transitions to the REMOVING state. |
1073 } | 1083 } |
1074 | 1084 |
1075 void ExtensionDownloadsEventRouter::ManagerGoingDown( | 1085 void ExtensionDownloadsEventRouter::ManagerGoingDown( |
1076 DownloadManager* manager) { | 1086 DownloadManager* manager) { |
1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1078 manager_->RemoveObserver(this); | 1087 manager_->RemoveObserver(this); |
1079 manager_ = NULL; | 1088 manager_ = NULL; |
| 1089 profile_ = NULL; |
1080 } | 1090 } |
1081 | 1091 |
1082 void ExtensionDownloadsEventRouter::DispatchEvent( | 1092 void ExtensionDownloadsEventRouter::DispatchEvent( |
1083 const char* event_name, base::Value* arg) { | 1093 const char* event_name, base::Value* arg) { |
1084 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1094 if (!profile_ || !profile_->GetExtensionEventRouter()) |
| 1095 return; |
1085 base::ListValue args; | 1096 base::ListValue args; |
1086 args.Append(arg); | 1097 args.Append(arg); |
1087 std::string json_args; | 1098 std::string json_args; |
1088 base::JSONWriter::Write(&args, &json_args); | 1099 base::JSONWriter::Write(&args, &json_args); |
1089 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 1100 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
1090 event_name, | 1101 event_name, |
1091 json_args, | 1102 json_args, |
1092 profile_, | 1103 profile_, |
1093 GURL()); | 1104 GURL()); |
1094 | |
1095 DownloadsNotificationSource notification_source; | |
1096 notification_source.event_name = event_name; | |
1097 notification_source.profile = profile_; | |
1098 content::NotificationService::current()->Notify( | |
1099 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, | |
1100 content::Source<DownloadsNotificationSource>(¬ification_source), | |
1101 content::Details<std::string>(&json_args)); | |
1102 } | 1105 } |
OLD | NEW |