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/download/download_extension_api.h" | 5 #include "chrome/browser/download/download_extension_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 842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 853 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
854 if (url.empty()) | 854 if (url.empty()) |
855 error_ = download_extension_errors::kIconNotFoundError; | 855 error_ = download_extension_errors::kIconNotFoundError; |
856 else | 856 else |
857 result_.reset(base::Value::CreateStringValue(url)); | 857 result_.reset(base::Value::CreateStringValue(url)); |
858 SendResponse(error_.empty()); | 858 SendResponse(error_.empty()); |
859 } | 859 } |
860 | 860 |
861 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) | 861 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) |
862 : profile_(profile), | 862 : profile_(profile), |
863 manager_(NULL) { | 863 manager_(NULL), |
| 864 delete_item_jsons_(&item_jsons_), |
| 865 delete_on_changed_stats_(&on_changed_stats_) { |
864 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 866 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
865 DCHECK(profile_); | 867 DCHECK(profile_); |
866 // Register a callback with the DownloadService for this profile to be called | 868 // Register a callback with the DownloadService for this profile to be called |
867 // when it creates the DownloadManager, or now if the manager already exists. | 869 // when it creates the DownloadManager, or now if the manager already exists. |
868 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( | 870 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( |
869 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); | 871 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); |
870 } | 872 } |
871 | 873 |
872 // The only public methods on this class are ModelChanged() and | 874 // The only public methods on this class are ModelChanged() and |
873 // ManagerGoingDown(), and they are only called by DownloadManager, so | 875 // ManagerGoingDown(), and they are only called by DownloadManager, so |
874 // there's no way for any methods on this class to be called before | 876 // there's no way for any methods on this class to be called before |
875 // DownloadService calls Init() via the OnManagerCreated Callback above. | 877 // DownloadService calls Init() via the OnManagerCreated Callback above. |
876 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { | 878 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { |
877 DCHECK(manager_ == NULL); | 879 DCHECK(manager_ == NULL); |
878 manager_ = manager; | 880 manager_ = manager; |
| 881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
879 manager_->AddObserver(this); | 882 manager_->AddObserver(this); |
880 } | 883 } |
881 | 884 |
882 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 885 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
883 if (manager_ != NULL) | 886 if (manager_ != NULL) |
884 manager_->RemoveObserver(this); | 887 manager_->RemoveObserver(this); |
| 888 for (ItemMap::const_iterator iter = downloads_.begin(); |
| 889 iter != downloads_.end(); ++iter) { |
| 890 if (iter->second != NULL) |
| 891 iter->second->RemoveObserver(this); |
| 892 } |
| 893 } |
| 894 |
| 895 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() |
| 896 : fires(0), |
| 897 total(0) { |
| 898 } |
| 899 |
| 900 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { |
| 901 if (total > 0) |
| 902 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); |
| 903 } |
| 904 |
| 905 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { |
| 906 int download_id = item->GetId(); |
| 907 if (item->GetState() == DownloadItem::REMOVING) { |
| 908 // The REMOVING state indicates that this item is being erased. |
| 909 // Let's unregister as an observer so that we don't see any more updates |
| 910 // from it, dispatch the onErased event, and remove its json and is |
| 911 // OnChangedStat from our maps. |
| 912 downloads_.erase(download_id); |
| 913 item->RemoveObserver(this); |
| 914 DispatchEvent(extension_event_names::kOnDownloadErased, |
| 915 base::Value::CreateIntegerValue(download_id)); |
| 916 delete item_jsons_[download_id]; |
| 917 item_jsons_.erase(download_id); |
| 918 delete on_changed_stats_[download_id]; |
| 919 on_changed_stats_.erase(download_id); |
| 920 return; |
| 921 } |
| 922 |
| 923 base::DictionaryValue* old_json = item_jsons_[download_id]; |
| 924 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); |
| 925 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); |
| 926 delta->SetInteger(kIdKey, download_id); |
| 927 std::set<std::string> new_fields; |
| 928 bool changed = false; |
| 929 |
| 930 // For each field in the new json representation of the item except the |
| 931 // bytesReceived field, if the field has changed from the previous old json, |
| 932 // set the differences in the |delta| object and remember that something |
| 933 // significant changed. |
| 934 for (base::DictionaryValue::Iterator iter(*new_json.get()); |
| 935 iter.HasNext(); iter.Advance()) { |
| 936 new_fields.insert(iter.key()); |
| 937 if (iter.key() != kBytesReceivedKey) { |
| 938 base::Value* old_value = NULL; |
| 939 if (!old_json->HasKey(iter.key()) || |
| 940 (old_json->Get(iter.key(), &old_value) && |
| 941 !iter.value().Equals(old_value))) { |
| 942 delta->Set(iter.key() + ".new", iter.value().DeepCopy()); |
| 943 if (old_value) |
| 944 delta->Set(iter.key() + ".old", old_value->DeepCopy()); |
| 945 changed = true; |
| 946 } |
| 947 } |
| 948 } |
| 949 |
| 950 // If a field was in the previous json but is not in the new json, set the |
| 951 // difference in |delta|. |
| 952 for (base::DictionaryValue::Iterator iter(*old_json); |
| 953 iter.HasNext(); iter.Advance()) { |
| 954 if (new_fields.find(iter.key()) == new_fields.end()) { |
| 955 delta->Set(iter.key() + ".old", iter.value().DeepCopy()); |
| 956 changed = true; |
| 957 } |
| 958 } |
| 959 |
| 960 // Update the OnChangedStat and dispatch the event if something significant |
| 961 // changed. Replace the stored json with the new json. |
| 962 ++(on_changed_stats_[download_id]->total); |
| 963 if (changed) { |
| 964 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
| 965 ++(on_changed_stats_[download_id]->fires); |
| 966 } |
| 967 item_jsons_[download_id]->Swap(new_json.get()); |
| 968 } |
| 969 |
| 970 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { |
885 } | 971 } |
886 | 972 |
887 void ExtensionDownloadsEventRouter::ModelChanged() { | 973 void ExtensionDownloadsEventRouter::ModelChanged() { |
| 974 typedef std::set<int> DownloadIdSet; |
| 975 |
888 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 976 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
889 if (manager_ == NULL) | 977 if (manager_ == NULL) |
890 return; | 978 return; |
| 979 |
| 980 // Get all the download items. |
891 DownloadManager::DownloadVector current_vec; | 981 DownloadManager::DownloadVector current_vec; |
892 manager_->SearchDownloads(string16(), ¤t_vec); | 982 manager_->SearchDownloads(string16(), ¤t_vec); |
893 DownloadIdSet current_set; | 983 |
| 984 // Populate set<>s of download item identifiers so that we can find |
| 985 // differences between the old and the new set of download items. |
| 986 DownloadIdSet current_set, prev_set; |
| 987 for (ItemMap::const_iterator iter = downloads_.begin(); |
| 988 iter != downloads_.end(); ++iter) { |
| 989 prev_set.insert(iter->first); |
| 990 } |
894 ItemMap current_map; | 991 ItemMap current_map; |
895 for (DownloadManager::DownloadVector::const_iterator iter = | 992 for (DownloadManager::DownloadVector::const_iterator iter = |
896 current_vec.begin(); | 993 current_vec.begin(); |
897 iter != current_vec.end(); ++iter) { | 994 iter != current_vec.end(); ++iter) { |
898 DownloadItem* item = *iter; | 995 DownloadItem* item = *iter; |
899 int item_id = item->GetId(); | 996 int item_id = item->GetId(); |
900 // TODO(benjhayden): Remove the following line when every item's id >= 0, | 997 CHECK(item_id >= 0); |
901 // which will allow firing onErased events for items from the history. | |
902 if (item_id < 0) continue; | |
903 DCHECK(current_map.find(item_id) == current_map.end()); | 998 DCHECK(current_map.find(item_id) == current_map.end()); |
904 current_set.insert(item_id); | 999 current_set.insert(item_id); |
905 current_map[item_id] = item; | 1000 current_map[item_id] = item; |
906 } | 1001 } |
907 DownloadIdSet new_set; // current_set - downloads_; | 1002 DownloadIdSet new_set; // current_set - prev_set; |
908 DownloadIdSet erased_set; // downloads_ - current_set; | |
909 std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin()); | |
910 std::insert_iterator<DownloadIdSet> erased_insertor( | |
911 erased_set, erased_set.begin()); | |
912 std::set_difference(current_set.begin(), current_set.end(), | 1003 std::set_difference(current_set.begin(), current_set.end(), |
913 downloads_.begin(), downloads_.end(), | 1004 prev_set.begin(), prev_set.end(), |
914 new_insertor); | 1005 std::insert_iterator<DownloadIdSet>( |
915 std::set_difference(downloads_.begin(), downloads_.end(), | 1006 new_set, new_set.begin())); |
916 current_set.begin(), current_set.end(), | 1007 |
917 erased_insertor); | 1008 // For each download that was not in the old set of downloads but is in the |
| 1009 // new set of downloads, fire an onCreated event, register as an Observer of |
| 1010 // the item, store a json representation of the item so that we can easily |
| 1011 // find changes in that json representation, and make an OnChangedStat. |
918 for (DownloadIdSet::const_iterator iter = new_set.begin(); | 1012 for (DownloadIdSet::const_iterator iter = new_set.begin(); |
919 iter != new_set.end(); ++iter) { | 1013 iter != new_set.end(); ++iter) { |
920 scoped_ptr<base::DictionaryValue> item( | 1014 scoped_ptr<base::DictionaryValue> item( |
921 DownloadItemToJSON(current_map[*iter])); | 1015 DownloadItemToJSON(current_map[*iter])); |
922 DispatchEvent(extension_event_names::kOnDownloadCreated, item.release()); | 1016 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy()); |
| 1017 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); |
| 1018 on_changed_stats_[*iter] = new OnChangedStat(); |
| 1019 current_map[*iter]->AddObserver(this); |
| 1020 item_jsons_[*iter] = item.release(); |
923 } | 1021 } |
924 for (DownloadIdSet::const_iterator iter = erased_set.begin(); | 1022 downloads_.swap(current_map); |
925 iter != erased_set.end(); ++iter) { | 1023 |
926 DispatchEvent(extension_event_names::kOnDownloadErased, | 1024 // Dispatching onErased is handled in OnDownloadUpdated when an item |
927 base::Value::CreateIntegerValue(*iter)); | 1025 // transitions to the REMOVING state. |
928 } | |
929 downloads_.swap(current_set); | |
930 } | 1026 } |
931 | 1027 |
932 void ExtensionDownloadsEventRouter::ManagerGoingDown() { | 1028 void ExtensionDownloadsEventRouter::ManagerGoingDown() { |
933 manager_->RemoveObserver(this); | 1029 manager_->RemoveObserver(this); |
934 manager_ = NULL; | 1030 manager_ = NULL; |
935 } | 1031 } |
936 | 1032 |
937 void ExtensionDownloadsEventRouter::DispatchEvent( | 1033 void ExtensionDownloadsEventRouter::DispatchEvent( |
938 const char* event_name, base::Value* arg) { | 1034 const char* event_name, base::Value* arg) { |
939 ListValue args; | 1035 ListValue args; |
940 args.Append(arg); | 1036 args.Append(arg); |
941 std::string json_args; | 1037 std::string json_args; |
942 base::JSONWriter::Write(&args, false, &json_args); | 1038 base::JSONWriter::Write(&args, false, &json_args); |
943 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 1039 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
944 event_name, | 1040 event_name, |
945 json_args, | 1041 json_args, |
946 profile_, | 1042 profile_, |
947 GURL()); | 1043 GURL()); |
948 } | 1044 } |
OLD | NEW |