| 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/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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 28 #include "chrome/browser/download/download_file_icon_extractor.h" | 28 #include "chrome/browser/download/download_file_icon_extractor.h" |
| 29 #include "chrome/browser/download/download_query.h" | 29 #include "chrome/browser/download/download_query.h" |
| 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_list.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 "content/public/browser/download_interrupt_reasons.h" | 40 #include "content/public/browser/download_interrupt_reasons.h" |
| 41 #include "content/public/browser/download_item.h" | 41 #include "content/public/browser/download_item.h" |
| 42 #include "content/public/browser/download_save_info.h" | 42 #include "content/public/browser/download_save_info.h" |
| 43 #include "content/public/browser/render_process_host.h" | 43 #include "content/public/browser/render_process_host.h" |
| 44 #include "content/public/browser/render_view_host.h" | 44 #include "content/public/browser/render_view_host.h" |
| 45 #include "content/public/browser/resource_dispatcher_host.h" | 45 #include "content/public/browser/resource_dispatcher_host.h" |
| 46 #include "net/base/load_flags.h" | 46 #include "net/base/load_flags.h" |
| 47 #include "net/http/http_util.h" | 47 #include "net/http/http_util.h" |
| 48 #include "net/url_request/url_request.h" | 48 #include "net/url_request/url_request.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 const char kDangerSafe[] = "safe"; | 84 const char kDangerSafe[] = "safe"; |
| 85 const char kDangerUncommon[] = "uncommon"; | 85 const char kDangerUncommon[] = "uncommon"; |
| 86 const char kDangerUrl[] = "url"; | 86 const char kDangerUrl[] = "url"; |
| 87 const char kEndTimeKey[] = "endTime"; | 87 const char kEndTimeKey[] = "endTime"; |
| 88 const char kErrorKey[] = "error"; | 88 const char kErrorKey[] = "error"; |
| 89 const char kFileSizeKey[] = "fileSize"; | 89 const char kFileSizeKey[] = "fileSize"; |
| 90 const char kFilenameKey[] = "filename"; | 90 const char kFilenameKey[] = "filename"; |
| 91 const char kFilenameRegexKey[] = "filenameRegex"; | 91 const char kFilenameRegexKey[] = "filenameRegex"; |
| 92 const char kHeaderNameKey[] = "name"; | 92 const char kHeaderNameKey[] = "name"; |
| 93 const char kHeaderValueKey[] = "value"; | 93 const char kHeaderValueKey[] = "value"; |
| 94 const char kHeaderBinaryValueKey[] = "binaryValue"; | |
| 95 const char kHeadersKey[] = "headers"; | 94 const char kHeadersKey[] = "headers"; |
| 96 const char kIdKey[] = "id"; | 95 const char kIdKey[] = "id"; |
| 97 const char kIncognito[] = "incognito"; | 96 const char kIncognito[] = "incognito"; |
| 98 const char kLimitKey[] = "limit"; | 97 const char kLimitKey[] = "limit"; |
| 99 const char kMethodKey[] = "method"; | 98 const char kMethodKey[] = "method"; |
| 100 const char kMimeKey[] = "mime"; | 99 const char kMimeKey[] = "mime"; |
| 101 const char kOrderByKey[] = "orderBy"; | 100 const char kOrderByKey[] = "orderBy"; |
| 102 const char kPausedKey[] = "paused"; | 101 const char kPausedKey[] = "paused"; |
| 103 const char kQueryKey[] = "query"; | 102 const char kQueryKey[] = "query"; |
| 104 const char kSaveAsKey[] = "saveAs"; | 103 const char kSaveAsKey[] = "saveAs"; |
| 105 const char kSizeKey[] = "size"; | 104 const char kSizeKey[] = "size"; |
| 106 const char kStartTimeKey[] = "startTime"; | 105 const char kStartTimeKey[] = "startTime"; |
| 107 const char kStartedAfterKey[] = "startedAfter"; | 106 const char kStartedAfterKey[] = "startedAfter"; |
| 108 const char kStartedBeforeKey[] = "startedBefore"; | 107 const char kStartedBeforeKey[] = "startedBefore"; |
| 109 const char kStateComplete[] = "complete"; | 108 const char kStateComplete[] = "complete"; |
| 110 const char kStateInProgress[] = "in_progress"; | 109 const char kStateInProgress[] = "in_progress"; |
| 111 const char kStateInterrupted[] = "interrupted"; | 110 const char kStateInterrupted[] = "interrupted"; |
| 112 const char kStateKey[] = "state"; | 111 const char kStateKey[] = "state"; |
| 113 const char kTotalBytesKey[] = "totalBytes"; | 112 const char kTotalBytesKey[] = "totalBytes"; |
| 114 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; | 113 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; |
| 115 const char kTotalBytesLessKey[] = "totalBytesLess"; | 114 const char kTotalBytesLessKey[] = "totalBytesLess"; |
| 116 const char kUrlKey[] = "url"; | 115 const char kUrlKey[] = "url"; |
| 117 const char kUrlRegexKey[] = "urlRegex"; | 116 const char kUrlRegexKey[] = "urlRegex"; |
| 118 | 117 |
| 119 // Note: Any change to the danger type strings, should be accompanied by a | 118 // Note: Any change to the danger type strings, should be accompanied by a |
| 120 // corresponding change to {experimental.}downloads.json. | 119 // corresponding change to downloads.json. |
| 121 const char* kDangerStrings[] = { | 120 const char* kDangerStrings[] = { |
| 122 kDangerSafe, | 121 kDangerSafe, |
| 123 kDangerFile, | 122 kDangerFile, |
| 124 kDangerUrl, | 123 kDangerUrl, |
| 125 kDangerContent, | 124 kDangerContent, |
| 126 kDangerSafe, | 125 kDangerSafe, |
| 127 kDangerUncommon, | 126 kDangerUncommon, |
| 128 }; | 127 }; |
| 129 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, | 128 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, |
| 130 download_danger_type_enum_changed); | 129 download_danger_type_enum_changed); |
| 131 | 130 |
| 132 // Note: Any change to the state strings, should be accompanied by a | 131 // Note: Any change to the state strings, should be accompanied by a |
| 133 // corresponding change to {experimental.}downloads.json. | 132 // corresponding change to downloads.json. |
| 134 const char* kStateStrings[] = { | 133 const char* kStateStrings[] = { |
| 135 kStateInProgress, | 134 kStateInProgress, |
| 136 kStateComplete, | 135 kStateComplete, |
| 137 kStateInterrupted, | 136 kStateInterrupted, |
| 138 NULL, | 137 NULL, |
| 139 kStateInterrupted, | 138 kStateInterrupted, |
| 140 }; | 139 }; |
| 141 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, | 140 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, |
| 142 download_item_state_enum_changed); | 141 download_item_state_enum_changed); |
| 143 | 142 |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 } | 455 } |
| 457 | 456 |
| 458 if (iodata_->extra_headers != NULL) { | 457 if (iodata_->extra_headers != NULL) { |
| 459 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { | 458 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { |
| 460 base::DictionaryValue* header = NULL; | 459 base::DictionaryValue* header = NULL; |
| 461 std::string name; | 460 std::string name; |
| 462 EXTENSION_FUNCTION_VALIDATE(iodata_->extra_headers->GetDictionary( | 461 EXTENSION_FUNCTION_VALIDATE(iodata_->extra_headers->GetDictionary( |
| 463 index, &header)); | 462 index, &header)); |
| 464 EXTENSION_FUNCTION_VALIDATE(header->GetString( | 463 EXTENSION_FUNCTION_VALIDATE(header->GetString( |
| 465 kHeaderNameKey, &name)); | 464 kHeaderNameKey, &name)); |
| 466 if (header->HasKey(kHeaderBinaryValueKey)) { | |
| 467 base::ListValue* binary_value = NULL; | |
| 468 EXTENSION_FUNCTION_VALIDATE(header->GetList( | |
| 469 kHeaderBinaryValueKey, &binary_value)); | |
| 470 for (size_t char_i = 0; char_i < binary_value->GetSize(); ++char_i) { | |
| 471 int char_value = 0; | |
| 472 EXTENSION_FUNCTION_VALIDATE(binary_value->GetInteger( | |
| 473 char_i, &char_value)); | |
| 474 } | |
| 475 } else if (header->HasKey(kHeaderValueKey)) { | |
| 476 std::string value; | |
| 477 EXTENSION_FUNCTION_VALIDATE(header->GetString( | |
| 478 kHeaderValueKey, &value)); | |
| 479 } | |
| 480 if (!net::HttpUtil::IsSafeHeader(name)) { | 465 if (!net::HttpUtil::IsSafeHeader(name)) { |
| 481 error_ = download_extension_errors::kGenericError; | 466 error_ = download_extension_errors::kGenericError; |
| 482 return false; | 467 return false; |
| 483 } | 468 } |
| 484 } | 469 } |
| 485 } | 470 } |
| 486 iodata_->rdh = content::ResourceDispatcherHost::Get(); | 471 iodata_->rdh = content::ResourceDispatcherHost::Get(); |
| 487 iodata_->resource_context = profile()->GetResourceContext(); | 472 iodata_->resource_context = profile()->GetResourceContext(); |
| 488 iodata_->render_process_host_id = render_view_host()->GetProcess()->GetID(); | 473 iodata_->render_process_host_id = render_view_host()->GetProcess()->GetID(); |
| 489 iodata_->render_view_host_routing_id = render_view_host()->GetRoutingID(); | 474 iodata_->render_view_host_routing_id = render_view_host()->GetRoutingID(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 510 save_info.prompt_for_save_location = iodata_->save_as; | 495 save_info.prompt_for_save_location = iodata_->save_as; |
| 511 | 496 |
| 512 scoped_ptr<net::URLRequest> request(new net::URLRequest(iodata_->url, NULL)); | 497 scoped_ptr<net::URLRequest> request(new net::URLRequest(iodata_->url, NULL)); |
| 513 request->set_method(iodata_->method); | 498 request->set_method(iodata_->method); |
| 514 if (iodata_->extra_headers != NULL) { | 499 if (iodata_->extra_headers != NULL) { |
| 515 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { | 500 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { |
| 516 base::DictionaryValue* header = NULL; | 501 base::DictionaryValue* header = NULL; |
| 517 std::string name, value; | 502 std::string name, value; |
| 518 CHECK(iodata_->extra_headers->GetDictionary(index, &header)); | 503 CHECK(iodata_->extra_headers->GetDictionary(index, &header)); |
| 519 CHECK(header->GetString(kHeaderNameKey, &name)); | 504 CHECK(header->GetString(kHeaderNameKey, &name)); |
| 520 if (header->HasKey(kHeaderBinaryValueKey)) { | 505 CHECK(header->GetString(kHeaderValueKey, &value)); |
| 521 base::ListValue* binary_value = NULL; | |
| 522 CHECK(header->GetList(kHeaderBinaryValueKey, &binary_value)); | |
| 523 for (size_t char_i = 0; char_i < binary_value->GetSize(); ++char_i) { | |
| 524 int char_value = 0; | |
| 525 CHECK(binary_value->GetInteger(char_i, &char_value)); | |
| 526 if ((0 <= char_value) && | |
| 527 (char_value <= 0xff)) { | |
| 528 value.push_back(char_value); | |
| 529 } | |
| 530 } | |
| 531 } else if (header->HasKey(kHeaderValueKey)) { | |
| 532 CHECK(header->GetString(kHeaderValueKey, &value)); | |
| 533 } | |
| 534 request->SetExtraRequestHeaderByName(name, value, false/*overwrite*/); | 506 request->SetExtraRequestHeaderByName(name, value, false/*overwrite*/); |
| 535 } | 507 } |
| 536 } | 508 } |
| 537 if (!iodata_->post_body.empty()) { | 509 if (!iodata_->post_body.empty()) { |
| 538 request->AppendBytesToUpload(iodata_->post_body.data(), | 510 request->AppendBytesToUpload(iodata_->post_body.data(), |
| 539 iodata_->post_body.size()); | 511 iodata_->post_body.size()); |
| 540 } | 512 } |
| 541 | 513 |
| 542 // Prevent login prompts for 401/407 responses. | 514 // Prevent login prompts for 401/407 responses. |
| 543 request->set_load_flags(request->load_flags() | | 515 request->set_load_flags(request->load_flags() | |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 | 859 |
| 888 bool DownloadsGetFileIconFunction::ParseArgs() { | 860 bool DownloadsGetFileIconFunction::ParseArgs() { |
| 889 int dl_id = 0; | 861 int dl_id = 0; |
| 890 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 862 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 891 | 863 |
| 892 base::DictionaryValue* options = NULL; | 864 base::DictionaryValue* options = NULL; |
| 893 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | 865 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
| 894 if (options->HasKey(kSizeKey)) { | 866 if (options->HasKey(kSizeKey)) { |
| 895 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSizeKey, &icon_size_)); | 867 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSizeKey, &icon_size_)); |
| 896 // We only support 16px and 32px icons. This is enforced in | 868 // We only support 16px and 32px icons. This is enforced in |
| 897 // experimental.downloads.json. | 869 // downloads.json. |
| 898 DCHECK(icon_size_ == 16 || icon_size_ == 32); | 870 DCHECK(icon_size_ == 16 || icon_size_ == 32); |
| 899 } | 871 } |
| 900 | 872 |
| 901 DownloadManager* manager = NULL; | 873 DownloadManager* manager = NULL; |
| 902 DownloadManager* incognito_manager = NULL; | 874 DownloadManager* incognito_manager = NULL; |
| 903 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | 875 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); |
| 904 DownloadItem* download_item = manager->GetDownloadItem(dl_id); | 876 DownloadItem* download_item = manager->GetDownloadItem(dl_id); |
| 905 if (!download_item && incognito_manager) | 877 if (!download_item && incognito_manager) |
| 906 download_item = incognito_manager->GetDownloadItem(dl_id); | 878 download_item = incognito_manager->GetDownloadItem(dl_id); |
| 907 if (!download_item) { | 879 if (!download_item) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 932 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 904 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 933 if (url.empty()) | 905 if (url.empty()) |
| 934 error_ = download_extension_errors::kIconNotFoundError; | 906 error_ = download_extension_errors::kIconNotFoundError; |
| 935 else | 907 else |
| 936 result_.reset(base::Value::CreateStringValue(url)); | 908 result_.reset(base::Value::CreateStringValue(url)); |
| 937 SendResponse(error_.empty()); | 909 SendResponse(error_.empty()); |
| 938 } | 910 } |
| 939 | 911 |
| 940 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) | 912 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(Profile* profile) |
| 941 : profile_(profile), | 913 : profile_(profile), |
| 942 manager_(NULL), | 914 manager_(NULL) { |
| 943 delete_item_jsons_(&item_jsons_), | |
| 944 delete_on_changed_stats_(&on_changed_stats_) { | |
| 945 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 915 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 946 DCHECK(profile_); | 916 DCHECK(profile_); |
| 947 // Register a callback with the DownloadService for this profile to be called | 917 // Register a callback with the DownloadService for this profile to be called |
| 948 // when it creates the DownloadManager, or now if the manager already exists. | 918 // when it creates the DownloadManager, or now if the manager already exists. |
| 949 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( | 919 DownloadServiceFactory::GetForProfile(profile)->OnManagerCreated(base::Bind( |
| 950 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); | 920 &ExtensionDownloadsEventRouter::Init, base::Unretained(this))); |
| 951 } | 921 } |
| 952 | 922 |
| 953 // The only public methods on this class are ModelChanged() and | 923 // The only public methods on this class are ModelChanged() and |
| 954 // ManagerGoingDown(), and they are only called by DownloadManager, so | 924 // ManagerGoingDown(), and they are only called by DownloadManager, so |
| 955 // there's no way for any methods on this class to be called before | 925 // there's no way for any methods on this class to be called before |
| 956 // DownloadService calls Init() via the OnManagerCreated Callback above. | 926 // DownloadService calls Init() via the OnManagerCreated Callback above. |
| 957 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { | 927 void ExtensionDownloadsEventRouter::Init(DownloadManager* manager) { |
| 958 DCHECK(manager_ == NULL); | 928 DCHECK(manager_ == NULL); |
| 959 manager_ = manager; | 929 manager_ = manager; |
| 960 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 930 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 961 manager_->AddObserver(this); | 931 manager_->AddObserver(this); |
| 962 } | 932 } |
| 963 | 933 |
| 964 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 934 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
| 965 if (manager_ != NULL) | 935 if (manager_ != NULL) |
| 966 manager_->RemoveObserver(this); | 936 manager_->RemoveObserver(this); |
| 967 for (ItemMap::const_iterator iter = downloads_.begin(); | 937 for (ItemMap::const_iterator iter = downloads_.begin(); |
| 968 iter != downloads_.end(); ++iter) { | 938 iter != downloads_.end(); ++iter) { |
| 969 if (iter->second != NULL) | 939 if (iter->second != NULL) |
| 970 iter->second->RemoveObserver(this); | 940 iter->second->RemoveObserver(this); |
| 971 } | 941 } |
| 942 STLDeleteValues(&item_jsons_); |
| 943 STLDeleteValues(&on_changed_stats_); |
| 972 } | 944 } |
| 973 | 945 |
| 974 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() | 946 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() |
| 975 : fires(0), | 947 : fires(0), |
| 976 total(0) { | 948 total(0) { |
| 977 } | 949 } |
| 978 | 950 |
| 979 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { | 951 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { |
| 980 if (total > 0) | 952 if (total > 0) |
| 981 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); | 953 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", (fires * 100 / total)); |
| 982 } | 954 } |
| 983 | 955 |
| 984 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | 956 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { |
| 957 if (!profile_) |
| 958 return; |
| 985 int download_id = item->GetId(); | 959 int download_id = item->GetId(); |
| 986 if (item->GetState() == DownloadItem::REMOVING) { | 960 if (item->GetState() == DownloadItem::REMOVING) { |
| 987 // The REMOVING state indicates that this item is being erased. | 961 // The REMOVING state indicates that this item is being erased. |
| 988 // Let's unregister as an observer so that we don't see any more updates | 962 // Let's unregister as an observer so that we don't see any more updates |
| 989 // from it, dispatch the onErased event, and remove its json and is | 963 // from it, dispatch the onErased event, and remove its json and is |
| 990 // OnChangedStat from our maps. | 964 // OnChangedStat from our maps. |
| 991 downloads_.erase(download_id); | 965 downloads_.erase(download_id); |
| 992 item->RemoveObserver(this); | 966 item->RemoveObserver(this); |
| 993 DispatchEvent(extension_event_names::kOnDownloadErased, | 967 DispatchEvent(extension_event_names::kOnDownloadErased, |
| 994 base::Value::CreateIntegerValue(download_id)); | 968 base::Value::CreateIntegerValue(download_id)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1011 // set the differences in the |delta| object and remember that something | 985 // set the differences in the |delta| object and remember that something |
| 1012 // significant changed. | 986 // significant changed. |
| 1013 for (base::DictionaryValue::Iterator iter(*new_json.get()); | 987 for (base::DictionaryValue::Iterator iter(*new_json.get()); |
| 1014 iter.HasNext(); iter.Advance()) { | 988 iter.HasNext(); iter.Advance()) { |
| 1015 new_fields.insert(iter.key()); | 989 new_fields.insert(iter.key()); |
| 1016 if (iter.key() != kBytesReceivedKey) { | 990 if (iter.key() != kBytesReceivedKey) { |
| 1017 base::Value* old_value = NULL; | 991 base::Value* old_value = NULL; |
| 1018 if (!old_json->HasKey(iter.key()) || | 992 if (!old_json->HasKey(iter.key()) || |
| 1019 (old_json->Get(iter.key(), &old_value) && | 993 (old_json->Get(iter.key(), &old_value) && |
| 1020 !iter.value().Equals(old_value))) { | 994 !iter.value().Equals(old_value))) { |
| 1021 delta->Set(iter.key() + ".new", iter.value().DeepCopy()); | 995 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); |
| 1022 if (old_value) | 996 if (old_value) |
| 1023 delta->Set(iter.key() + ".old", old_value->DeepCopy()); | 997 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); |
| 1024 changed = true; | 998 changed = true; |
| 1025 } | 999 } |
| 1026 } | 1000 } |
| 1027 } | 1001 } |
| 1028 | 1002 |
| 1029 // If a field was in the previous json but is not in the new json, set the | 1003 // If a field was in the previous json but is not in the new json, set the |
| 1030 // difference in |delta|. | 1004 // difference in |delta|. |
| 1031 for (base::DictionaryValue::Iterator iter(*old_json); | 1005 for (base::DictionaryValue::Iterator iter(*old_json); |
| 1032 iter.HasNext(); iter.Advance()) { | 1006 iter.HasNext(); iter.Advance()) { |
| 1033 if (new_fields.find(iter.key()) == new_fields.end()) { | 1007 if (new_fields.find(iter.key()) == new_fields.end()) { |
| 1034 delta->Set(iter.key() + ".old", iter.value().DeepCopy()); | 1008 delta->Set(iter.key() + ".previous", iter.value().DeepCopy()); |
| 1035 changed = true; | 1009 changed = true; |
| 1036 } | 1010 } |
| 1037 } | 1011 } |
| 1038 | 1012 |
| 1039 // Update the OnChangedStat and dispatch the event if something significant | 1013 // Update the OnChangedStat and dispatch the event if something significant |
| 1040 // changed. Replace the stored json with the new json. | 1014 // changed. Replace the stored json with the new json. |
| 1041 ++(on_changed_stats_[download_id]->total); | 1015 ++(on_changed_stats_[download_id]->total); |
| 1042 if (changed) { | 1016 if (changed) { |
| 1043 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | 1017 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
| 1044 ++(on_changed_stats_[download_id]->fires); | 1018 ++(on_changed_stats_[download_id]->fires); |
| 1045 } | 1019 } |
| 1046 item_jsons_[download_id]->Swap(new_json.get()); | 1020 item_jsons_[download_id]->Swap(new_json.get()); |
| 1047 } | 1021 } |
| 1048 | 1022 |
| 1049 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | 1023 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { |
| 1050 } | 1024 } |
| 1051 | 1025 |
| 1052 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { | 1026 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { |
| 1027 if (!profile_) |
| 1028 return; |
| 1053 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1029 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1054 DCHECK(manager_ == manager); | 1030 DCHECK(manager_ == manager); |
| 1055 typedef std::set<int> DownloadIdSet; | 1031 typedef std::set<int> DownloadIdSet; |
| 1056 | 1032 |
| 1057 // Get all the download items. | 1033 // Get all the download items. |
| 1058 DownloadManager::DownloadVector current_vec; | 1034 DownloadManager::DownloadVector current_vec; |
| 1059 manager_->SearchDownloads(string16(), ¤t_vec); | 1035 manager_->SearchDownloads(string16(), ¤t_vec); |
| 1060 | 1036 |
| 1061 // Populate set<>s of download item identifiers so that we can find | 1037 // Populate set<>s of download item identifiers so that we can find |
| 1062 // differences between the old and the new set of download items. | 1038 // differences between the old and the new set of download items. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 downloads_.swap(current_map); | 1075 downloads_.swap(current_map); |
| 1100 | 1076 |
| 1101 // Dispatching onErased is handled in OnDownloadUpdated when an item | 1077 // Dispatching onErased is handled in OnDownloadUpdated when an item |
| 1102 // transitions to the REMOVING state. | 1078 // transitions to the REMOVING state. |
| 1103 } | 1079 } |
| 1104 | 1080 |
| 1105 void ExtensionDownloadsEventRouter::ManagerGoingDown( | 1081 void ExtensionDownloadsEventRouter::ManagerGoingDown( |
| 1106 DownloadManager* manager) { | 1082 DownloadManager* manager) { |
| 1107 manager_->RemoveObserver(this); | 1083 manager_->RemoveObserver(this); |
| 1108 manager_ = NULL; | 1084 manager_ = NULL; |
| 1085 profile_ = NULL; |
| 1109 } | 1086 } |
| 1110 | 1087 |
| 1111 void ExtensionDownloadsEventRouter::DispatchEvent( | 1088 void ExtensionDownloadsEventRouter::DispatchEvent( |
| 1112 const char* event_name, base::Value* arg) { | 1089 const char* event_name, base::Value* arg) { |
| 1113 ListValue args; | 1090 if (!profile_ || !profile_->GetExtensionEventRouter()) |
| 1091 return; |
| 1092 base::ListValue args; |
| 1114 args.Append(arg); | 1093 args.Append(arg); |
| 1115 std::string json_args; | 1094 std::string json_args; |
| 1116 base::JSONWriter::Write(&args, &json_args); | 1095 base::JSONWriter::Write(&args, &json_args); |
| 1117 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 1096 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 1118 event_name, | 1097 event_name, |
| 1119 json_args, | 1098 json_args, |
| 1120 profile_, | 1099 profile_, |
| 1121 GURL()); | 1100 GURL()); |
| 1122 } | 1101 } |
| OLD | NEW |