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