| 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 11 matching lines...) Expand all Loading... |
| 22 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 23 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 24 #include "base/strings/string16.h" | 24 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_split.h" | 25 #include "base/strings/string_split.h" |
| 26 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 28 #include "base/values.h" | 28 #include "base/values.h" |
| 29 #include "chrome/browser/browser_process.h" | 29 #include "chrome/browser/browser_process.h" |
| 30 #include "chrome/browser/download/download_danger_prompt.h" | 30 #include "chrome/browser/download/download_danger_prompt.h" |
| 31 #include "chrome/browser/download/download_file_icon_extractor.h" | 31 #include "chrome/browser/download/download_file_icon_extractor.h" |
| 32 #include "chrome/browser/download/download_prefs.h" |
| 32 #include "chrome/browser/download/download_query.h" | 33 #include "chrome/browser/download/download_query.h" |
| 33 #include "chrome/browser/download/download_service.h" | 34 #include "chrome/browser/download/download_service.h" |
| 34 #include "chrome/browser/download/download_service_factory.h" | 35 #include "chrome/browser/download/download_service_factory.h" |
| 35 #include "chrome/browser/download/download_util.h" | 36 #include "chrome/browser/download/download_util.h" |
| 36 #include "chrome/browser/extensions/event_names.h" | 37 #include "chrome/browser/extensions/event_names.h" |
| 37 #include "chrome/browser/extensions/event_router.h" | 38 #include "chrome/browser/extensions/event_router.h" |
| 38 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 39 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 39 #include "chrome/browser/extensions/extension_info_map.h" | 40 #include "chrome/browser/extensions/extension_info_map.h" |
| 40 #include "chrome/browser/extensions/extension_prefs.h" | 41 #include "chrome/browser/extensions/extension_prefs.h" |
| 41 #include "chrome/browser/extensions/extension_service.h" | 42 #include "chrome/browser/extensions/extension_service.h" |
| 42 #include "chrome/browser/extensions/extension_system.h" | 43 #include "chrome/browser/extensions/extension_system.h" |
| 43 #include "chrome/browser/icon_loader.h" | 44 #include "chrome/browser/icon_loader.h" |
| 44 #include "chrome/browser/icon_manager.h" | 45 #include "chrome/browser/icon_manager.h" |
| 46 #include "chrome/browser/platform_util.h" |
| 45 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 47 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 46 #include "chrome/browser/ui/browser.h" | 48 #include "chrome/browser/ui/browser.h" |
| 47 #include "chrome/common/cancelable_task_tracker.h" | 49 #include "chrome/common/cancelable_task_tracker.h" |
| 48 #include "chrome/common/chrome_notification_types.h" | 50 #include "chrome/common/chrome_notification_types.h" |
| 49 #include "chrome/common/extensions/api/downloads.h" | 51 #include "chrome/common/extensions/api/downloads.h" |
| 50 #include "chrome/common/extensions/permissions/permissions_data.h" | 52 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 51 #include "content/public/browser/download_interrupt_reasons.h" | 53 #include "content/public/browser/download_interrupt_reasons.h" |
| 52 #include "content/public/browser/download_item.h" | 54 #include "content/public/browser/download_item.h" |
| 53 #include "content/public/browser/download_save_info.h" | 55 #include "content/public/browser/download_save_info.h" |
| 54 #include "content/public/browser/download_url_parameters.h" | 56 #include "content/public/browser/download_url_parameters.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 80 const char kGenericError[] = "I'm afraid I can't do that"; | 82 const char kGenericError[] = "I'm afraid I can't do that"; |
| 81 const char kIconNotFoundError[] = "Icon not found"; | 83 const char kIconNotFoundError[] = "Icon not found"; |
| 82 const char kInvalidDangerTypeError[] = "Invalid danger type"; | 84 const char kInvalidDangerTypeError[] = "Invalid danger type"; |
| 83 const char kInvalidFilenameError[] = "Invalid filename"; | 85 const char kInvalidFilenameError[] = "Invalid filename"; |
| 84 const char kInvalidFilterError[] = "Invalid query filter"; | 86 const char kInvalidFilterError[] = "Invalid query filter"; |
| 85 const char kInvalidOperationError[] = "Invalid operation"; | 87 const char kInvalidOperationError[] = "Invalid operation"; |
| 86 const char kInvalidOrderByError[] = "Invalid orderBy field"; | 88 const char kInvalidOrderByError[] = "Invalid orderBy field"; |
| 87 const char kInvalidQueryLimit[] = "Invalid query limit"; | 89 const char kInvalidQueryLimit[] = "Invalid query limit"; |
| 88 const char kInvalidStateError[] = "Invalid state"; | 90 const char kInvalidStateError[] = "Invalid state"; |
| 89 const char kInvalidURLError[] = "Invalid URL"; | 91 const char kInvalidURLError[] = "Invalid URL"; |
| 92 const char kNotPermittedURLError[] = "In order to access that URL, this " |
| 93 "extension must add the host to \"permissions\" in manifest.json"; |
| 90 const char kNotImplementedError[] = "NotImplemented"; | 94 const char kNotImplementedError[] = "NotImplemented"; |
| 91 const char kTooManyListenersError[] = "Each extension may have at most one " | 95 const char kTooManyListenersError[] = "Each extension may have at most one " |
| 92 "onDeterminingFilename listener between all of its renderer execution " | 96 "onDeterminingFilename listener between all of its renderer execution " |
| 93 "contexts."; | 97 "contexts."; |
| 94 | 98 |
| 95 } // namespace download_extension_errors | 99 } // namespace download_extension_errors |
| 96 | 100 |
| 97 namespace { | 101 namespace { |
| 98 | 102 |
| 99 // Default icon size for getFileIcon() in pixels. | 103 // Default icon size for getFileIcon() in pixels. |
| 100 const int kDefaultIconSize = 32; | 104 const int kDefaultIconSize = 32; |
| 101 | 105 |
| 102 // Parameter keys | 106 // Parameter keys |
| 103 const char kBytesReceivedKey[] = "bytesReceived"; | 107 const char kBytesReceivedKey[] = "bytesReceived"; |
| 104 const char kDangerAcceptedKey[] = "dangerAccepted"; | 108 const char kCanResumeKey[] = "canResume"; |
| 109 const char kDangerAccepted[] = "accepted"; |
| 105 const char kDangerContent[] = "content"; | 110 const char kDangerContent[] = "content"; |
| 106 const char kDangerFile[] = "file"; | 111 const char kDangerFile[] = "file"; |
| 112 const char kDangerHost[] = "host"; |
| 107 const char kDangerKey[] = "danger"; | 113 const char kDangerKey[] = "danger"; |
| 108 const char kDangerSafe[] = "safe"; | 114 const char kDangerSafe[] = "safe"; |
| 109 const char kDangerUncommon[] = "uncommon"; | 115 const char kDangerUncommon[] = "uncommon"; |
| 110 const char kDangerAccepted[] = "accepted"; | |
| 111 const char kDangerHost[] = "host"; | |
| 112 const char kDangerUrl[] = "url"; | 116 const char kDangerUrl[] = "url"; |
| 113 const char kEndTimeKey[] = "endTime"; | 117 const char kEndTimeKey[] = "endTime"; |
| 114 const char kEndedAfterKey[] = "endedAfter"; | 118 const char kEndedAfterKey[] = "endedAfter"; |
| 115 const char kEndedBeforeKey[] = "endedBefore"; | 119 const char kEndedBeforeKey[] = "endedBefore"; |
| 116 const char kErrorKey[] = "error"; | 120 const char kErrorKey[] = "error"; |
| 121 const char kEstimatedEndTimeKey[] = "estimatedEndTime"; |
| 117 const char kExistsKey[] = "exists"; | 122 const char kExistsKey[] = "exists"; |
| 118 const char kFileSizeKey[] = "fileSize"; | 123 const char kFileSizeKey[] = "fileSize"; |
| 119 const char kFilenameKey[] = "filename"; | 124 const char kFilenameKey[] = "filename"; |
| 120 const char kFilenameRegexKey[] = "filenameRegex"; | 125 const char kFilenameRegexKey[] = "filenameRegex"; |
| 121 const char kIdKey[] = "id"; | 126 const char kIdKey[] = "id"; |
| 122 const char kIncognito[] = "incognito"; | 127 const char kIncognito[] = "incognito"; |
| 123 const char kMimeKey[] = "mime"; | 128 const char kMimeKey[] = "mime"; |
| 124 const char kPausedKey[] = "paused"; | 129 const char kPausedKey[] = "paused"; |
| 125 const char kQueryKey[] = "query"; | 130 const char kQueryKey[] = "query"; |
| 131 const char kReferrerUrlKey[] = "referrer"; |
| 126 const char kStartTimeKey[] = "startTime"; | 132 const char kStartTimeKey[] = "startTime"; |
| 127 const char kStartedAfterKey[] = "startedAfter"; | 133 const char kStartedAfterKey[] = "startedAfter"; |
| 128 const char kStartedBeforeKey[] = "startedBefore"; | 134 const char kStartedBeforeKey[] = "startedBefore"; |
| 129 const char kStateComplete[] = "complete"; | 135 const char kStateComplete[] = "complete"; |
| 130 const char kStateInProgress[] = "in_progress"; | 136 const char kStateInProgress[] = "in_progress"; |
| 131 const char kStateInterrupted[] = "interrupted"; | 137 const char kStateInterrupted[] = "interrupted"; |
| 132 const char kStateKey[] = "state"; | 138 const char kStateKey[] = "state"; |
| 133 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; | 139 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; |
| 134 const char kTotalBytesKey[] = "totalBytes"; | 140 const char kTotalBytesKey[] = "totalBytes"; |
| 135 const char kTotalBytesLessKey[] = "totalBytesLess"; | 141 const char kTotalBytesLessKey[] = "totalBytesLess"; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 } | 214 } |
| 209 | 215 |
| 210 scoped_ptr<base::DictionaryValue> DownloadItemToJSON( | 216 scoped_ptr<base::DictionaryValue> DownloadItemToJSON( |
| 211 DownloadItem* download_item, | 217 DownloadItem* download_item, |
| 212 bool incognito) { | 218 bool incognito) { |
| 213 base::DictionaryValue* json = new base::DictionaryValue(); | 219 base::DictionaryValue* json = new base::DictionaryValue(); |
| 214 json->SetBoolean(kExistsKey, !download_item->GetFileExternallyRemoved()); | 220 json->SetBoolean(kExistsKey, !download_item->GetFileExternallyRemoved()); |
| 215 json->SetInteger(kIdKey, download_item->GetId()); | 221 json->SetInteger(kIdKey, download_item->GetId()); |
| 216 const GURL& url = download_item->GetOriginalUrl(); | 222 const GURL& url = download_item->GetOriginalUrl(); |
| 217 json->SetString(kUrlKey, (url.is_valid() ? url.spec() : std::string())); | 223 json->SetString(kUrlKey, (url.is_valid() ? url.spec() : std::string())); |
| 224 const GURL& referrer = download_item->GetReferrerUrl(); |
| 225 json->SetString(kReferrerUrlKey, (referrer.is_valid() ? referrer.spec() |
| 226 : std::string())); |
| 218 json->SetString(kFilenameKey, | 227 json->SetString(kFilenameKey, |
| 219 download_item->GetTargetFilePath().LossyDisplayName()); | 228 download_item->GetTargetFilePath().LossyDisplayName()); |
| 220 json->SetString(kDangerKey, DangerString(download_item->GetDangerType())); | 229 json->SetString(kDangerKey, DangerString(download_item->GetDangerType())); |
| 221 if (download_item->GetDangerType() != | |
| 222 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) | |
| 223 json->SetBoolean(kDangerAcceptedKey, | |
| 224 download_item->GetDangerType() == | |
| 225 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED); | |
| 226 json->SetString(kStateKey, StateString(download_item->GetState())); | 230 json->SetString(kStateKey, StateString(download_item->GetState())); |
| 231 json->SetBoolean(kCanResumeKey, download_item->CanResume()); |
| 227 json->SetBoolean(kPausedKey, download_item->IsPaused()); | 232 json->SetBoolean(kPausedKey, download_item->IsPaused()); |
| 228 json->SetString(kMimeKey, download_item->GetMimeType()); | 233 json->SetString(kMimeKey, download_item->GetMimeType()); |
| 229 json->SetString(kStartTimeKey, TimeToISO8601(download_item->GetStartTime())); | 234 json->SetString(kStartTimeKey, TimeToISO8601(download_item->GetStartTime())); |
| 230 json->SetInteger(kBytesReceivedKey, download_item->GetReceivedBytes()); | 235 json->SetInteger(kBytesReceivedKey, download_item->GetReceivedBytes()); |
| 231 json->SetInteger(kTotalBytesKey, download_item->GetTotalBytes()); | 236 json->SetInteger(kTotalBytesKey, download_item->GetTotalBytes()); |
| 232 json->SetBoolean(kIncognito, incognito); | 237 json->SetBoolean(kIncognito, incognito); |
| 233 if (download_item->GetState() == DownloadItem::INTERRUPTED) { | 238 if (download_item->GetState() == DownloadItem::INTERRUPTED) { |
| 234 json->SetInteger(kErrorKey, static_cast<int>( | 239 json->SetInteger(kErrorKey, static_cast<int>( |
| 235 download_item->GetLastReason())); | 240 download_item->GetLastReason())); |
| 236 } else if (download_item->GetState() == DownloadItem::CANCELLED) { | 241 } else if (download_item->GetState() == DownloadItem::CANCELLED) { |
| 237 json->SetInteger(kErrorKey, static_cast<int>( | 242 json->SetInteger(kErrorKey, static_cast<int>( |
| 238 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED)); | 243 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED)); |
| 239 } | 244 } |
| 240 if (!download_item->GetEndTime().is_null()) | 245 if (!download_item->GetEndTime().is_null()) |
| 241 json->SetString(kEndTimeKey, TimeToISO8601(download_item->GetEndTime())); | 246 json->SetString(kEndTimeKey, TimeToISO8601(download_item->GetEndTime())); |
| 247 base::TimeDelta time_remaining; |
| 248 if (download_item->TimeRemaining(&time_remaining)) { |
| 249 base::Time now = base::Time::Now(); |
| 250 json->SetString(kEstimatedEndTimeKey, TimeToISO8601(now + time_remaining)); |
| 251 } |
| 242 // TODO(benjhayden): Implement fileSize. | 252 // TODO(benjhayden): Implement fileSize. |
| 243 json->SetInteger(kFileSizeKey, download_item->GetTotalBytes()); | 253 json->SetInteger(kFileSizeKey, download_item->GetTotalBytes()); |
| 244 return scoped_ptr<base::DictionaryValue>(json); | 254 return scoped_ptr<base::DictionaryValue>(json); |
| 245 } | 255 } |
| 246 | 256 |
| 247 class DownloadFileIconExtractorImpl : public DownloadFileIconExtractor { | 257 class DownloadFileIconExtractorImpl : public DownloadFileIconExtractor { |
| 248 public: | 258 public: |
| 249 DownloadFileIconExtractorImpl() {} | 259 DownloadFileIconExtractorImpl() {} |
| 250 | 260 |
| 251 virtual ~DownloadFileIconExtractorImpl() {} | 261 virtual ~DownloadFileIconExtractorImpl() {} |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 default: | 302 default: |
| 293 NOTREACHED(); | 303 NOTREACHED(); |
| 294 return IconLoader::NORMAL; | 304 return IconLoader::NORMAL; |
| 295 } | 305 } |
| 296 } | 306 } |
| 297 | 307 |
| 298 typedef base::hash_map<std::string, DownloadQuery::FilterType> FilterTypeMap; | 308 typedef base::hash_map<std::string, DownloadQuery::FilterType> FilterTypeMap; |
| 299 | 309 |
| 300 void InitFilterTypeMap(FilterTypeMap& filter_types) { | 310 void InitFilterTypeMap(FilterTypeMap& filter_types) { |
| 301 filter_types[kBytesReceivedKey] = DownloadQuery::FILTER_BYTES_RECEIVED; | 311 filter_types[kBytesReceivedKey] = DownloadQuery::FILTER_BYTES_RECEIVED; |
| 302 filter_types[kDangerAcceptedKey] = DownloadQuery::FILTER_DANGER_ACCEPTED; | |
| 303 filter_types[kExistsKey] = DownloadQuery::FILTER_EXISTS; | 312 filter_types[kExistsKey] = DownloadQuery::FILTER_EXISTS; |
| 304 filter_types[kFilenameKey] = DownloadQuery::FILTER_FILENAME; | 313 filter_types[kFilenameKey] = DownloadQuery::FILTER_FILENAME; |
| 305 filter_types[kFilenameRegexKey] = DownloadQuery::FILTER_FILENAME_REGEX; | 314 filter_types[kFilenameRegexKey] = DownloadQuery::FILTER_FILENAME_REGEX; |
| 306 filter_types[kMimeKey] = DownloadQuery::FILTER_MIME; | 315 filter_types[kMimeKey] = DownloadQuery::FILTER_MIME; |
| 307 filter_types[kPausedKey] = DownloadQuery::FILTER_PAUSED; | 316 filter_types[kPausedKey] = DownloadQuery::FILTER_PAUSED; |
| 308 filter_types[kQueryKey] = DownloadQuery::FILTER_QUERY; | 317 filter_types[kQueryKey] = DownloadQuery::FILTER_QUERY; |
| 309 filter_types[kEndedAfterKey] = DownloadQuery::FILTER_ENDED_AFTER; | 318 filter_types[kEndedAfterKey] = DownloadQuery::FILTER_ENDED_AFTER; |
| 310 filter_types[kEndedBeforeKey] = DownloadQuery::FILTER_ENDED_BEFORE; | 319 filter_types[kEndedBeforeKey] = DownloadQuery::FILTER_ENDED_BEFORE; |
| 311 filter_types[kEndTimeKey] = DownloadQuery::FILTER_END_TIME; | 320 filter_types[kEndTimeKey] = DownloadQuery::FILTER_END_TIME; |
| 312 filter_types[kStartedAfterKey] = DownloadQuery::FILTER_STARTED_AFTER; | 321 filter_types[kStartedAfterKey] = DownloadQuery::FILTER_STARTED_AFTER; |
| 313 filter_types[kStartedBeforeKey] = DownloadQuery::FILTER_STARTED_BEFORE; | 322 filter_types[kStartedBeforeKey] = DownloadQuery::FILTER_STARTED_BEFORE; |
| 314 filter_types[kStartTimeKey] = DownloadQuery::FILTER_START_TIME; | 323 filter_types[kStartTimeKey] = DownloadQuery::FILTER_START_TIME; |
| 315 filter_types[kTotalBytesKey] = DownloadQuery::FILTER_TOTAL_BYTES; | 324 filter_types[kTotalBytesKey] = DownloadQuery::FILTER_TOTAL_BYTES; |
| 316 filter_types[kTotalBytesGreaterKey] = | 325 filter_types[kTotalBytesGreaterKey] = |
| 317 DownloadQuery::FILTER_TOTAL_BYTES_GREATER; | 326 DownloadQuery::FILTER_TOTAL_BYTES_GREATER; |
| 318 filter_types[kTotalBytesLessKey] = DownloadQuery::FILTER_TOTAL_BYTES_LESS; | 327 filter_types[kTotalBytesLessKey] = DownloadQuery::FILTER_TOTAL_BYTES_LESS; |
| 319 filter_types[kUrlKey] = DownloadQuery::FILTER_URL; | 328 filter_types[kUrlKey] = DownloadQuery::FILTER_URL; |
| 320 filter_types[kUrlRegexKey] = DownloadQuery::FILTER_URL_REGEX; | 329 filter_types[kUrlRegexKey] = DownloadQuery::FILTER_URL_REGEX; |
| 321 } | 330 } |
| 322 | 331 |
| 323 typedef base::hash_map<std::string, DownloadQuery::SortType> SortTypeMap; | 332 typedef base::hash_map<std::string, DownloadQuery::SortType> SortTypeMap; |
| 324 | 333 |
| 325 void InitSortTypeMap(SortTypeMap& sorter_types) { | 334 void InitSortTypeMap(SortTypeMap& sorter_types) { |
| 326 sorter_types[kBytesReceivedKey] = DownloadQuery::SORT_BYTES_RECEIVED; | 335 sorter_types[kBytesReceivedKey] = DownloadQuery::SORT_BYTES_RECEIVED; |
| 327 sorter_types[kDangerKey] = DownloadQuery::SORT_DANGER; | 336 sorter_types[kDangerKey] = DownloadQuery::SORT_DANGER; |
| 328 sorter_types[kDangerAcceptedKey] = DownloadQuery::SORT_DANGER_ACCEPTED; | |
| 329 sorter_types[kEndTimeKey] = DownloadQuery::SORT_END_TIME; | 337 sorter_types[kEndTimeKey] = DownloadQuery::SORT_END_TIME; |
| 330 sorter_types[kExistsKey] = DownloadQuery::SORT_EXISTS; | 338 sorter_types[kExistsKey] = DownloadQuery::SORT_EXISTS; |
| 331 sorter_types[kFilenameKey] = DownloadQuery::SORT_FILENAME; | 339 sorter_types[kFilenameKey] = DownloadQuery::SORT_FILENAME; |
| 332 sorter_types[kMimeKey] = DownloadQuery::SORT_MIME; | 340 sorter_types[kMimeKey] = DownloadQuery::SORT_MIME; |
| 333 sorter_types[kPausedKey] = DownloadQuery::SORT_PAUSED; | 341 sorter_types[kPausedKey] = DownloadQuery::SORT_PAUSED; |
| 334 sorter_types[kStartTimeKey] = DownloadQuery::SORT_START_TIME; | 342 sorter_types[kStartTimeKey] = DownloadQuery::SORT_START_TIME; |
| 335 sorter_types[kStateKey] = DownloadQuery::SORT_STATE; | 343 sorter_types[kStateKey] = DownloadQuery::SORT_STATE; |
| 336 sorter_types[kTotalBytesKey] = DownloadQuery::SORT_TOTAL_BYTES; | 344 sorter_types[kTotalBytesKey] = DownloadQuery::SORT_TOTAL_BYTES; |
| 337 sorter_types[kUrlKey] = DownloadQuery::SORT_URL; | 345 sorter_types[kUrlKey] = DownloadQuery::SORT_URL; |
| 338 } | 346 } |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 DownloadsDownloadFunction::DownloadsDownloadFunction() {} | 808 DownloadsDownloadFunction::DownloadsDownloadFunction() {} |
| 801 | 809 |
| 802 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} | 810 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} |
| 803 | 811 |
| 804 bool DownloadsDownloadFunction::RunImpl() { | 812 bool DownloadsDownloadFunction::RunImpl() { |
| 805 scoped_ptr<extensions::api::downloads::Download::Params> params( | 813 scoped_ptr<extensions::api::downloads::Download::Params> params( |
| 806 extensions::api::downloads::Download::Params::Create(*args_)); | 814 extensions::api::downloads::Download::Params::Create(*args_)); |
| 807 EXTENSION_FUNCTION_VALIDATE(params.get()); | 815 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 808 const extensions::api::downloads::DownloadOptions& options = params->options; | 816 const extensions::api::downloads::DownloadOptions& options = params->options; |
| 809 GURL download_url(options.url); | 817 GURL download_url(options.url); |
| 810 if (!download_url.is_valid() || | 818 if (!download_url.is_valid()) { |
| 811 (!download_url.SchemeIs("data") && | |
| 812 download_url.GetOrigin() != GetExtension()->url().GetOrigin() && | |
| 813 !extensions::PermissionsData::HasHostPermission(GetExtension(), | |
| 814 download_url))) { | |
| 815 error_ = download_extension_errors::kInvalidURLError; | 819 error_ = download_extension_errors::kInvalidURLError; |
| 816 return false; | 820 return false; |
| 821 } else if (!download_url.SchemeIs("data") && |
| 822 download_url.GetOrigin() != GetExtension()->url().GetOrigin() && |
| 823 !extensions::PermissionsData::HasHostPermission(GetExtension(), |
| 824 download_url)) { |
| 825 error_ = download_extension_errors::kNotPermittedURLError; |
| 826 return false; |
| 817 } | 827 } |
| 818 | 828 |
| 819 Profile* current_profile = profile(); | 829 Profile* current_profile = profile(); |
| 820 if (include_incognito() && profile()->HasOffTheRecordProfile()) | 830 if (include_incognito() && profile()->HasOffTheRecordProfile()) |
| 821 current_profile = profile()->GetOffTheRecordProfile(); | 831 current_profile = profile()->GetOffTheRecordProfile(); |
| 822 | 832 |
| 823 scoped_ptr<content::DownloadUrlParameters> download_params( | 833 scoped_ptr<content::DownloadUrlParameters> download_params( |
| 824 new content::DownloadUrlParameters( | 834 new content::DownloadUrlParameters( |
| 825 download_url, | 835 download_url, |
| 826 render_view_host()->GetProcess()->GetID(), | 836 render_view_host()->GetProcess()->GetID(), |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1056 RecordApiFunctions(DOWNLOADS_FUNCTION_ACCEPT_DANGER); | 1066 RecordApiFunctions(DOWNLOADS_FUNCTION_ACCEPT_DANGER); |
| 1057 // DownloadDangerPrompt displays a modal dialog using native widgets that the | 1067 // DownloadDangerPrompt displays a modal dialog using native widgets that the |
| 1058 // user must either accept or cancel. It cannot be scripted. | 1068 // user must either accept or cancel. It cannot be scripted. |
| 1059 DownloadDangerPrompt::Create( | 1069 DownloadDangerPrompt::Create( |
| 1060 download_item, | 1070 download_item, |
| 1061 web_contents, | 1071 web_contents, |
| 1062 true, | 1072 true, |
| 1063 base::Bind(&DownloadsAcceptDangerFunction::DangerPromptCallback, | 1073 base::Bind(&DownloadsAcceptDangerFunction::DangerPromptCallback, |
| 1064 this, true, params->download_id), | 1074 this, true, params->download_id), |
| 1065 base::Bind(&DownloadsAcceptDangerFunction::DangerPromptCallback, | 1075 base::Bind(&DownloadsAcceptDangerFunction::DangerPromptCallback, |
| 1066 this, false, params->download_id)); | 1076 this, false, params->download_id), |
| 1077 base::Bind(&DownloadsAcceptDangerFunction::DangerPromptDestroyed, |
| 1078 this)); |
| 1067 // DownloadDangerPrompt deletes itself | 1079 // DownloadDangerPrompt deletes itself |
| 1068 return true; | 1080 return true; |
| 1069 } | 1081 } |
| 1070 | 1082 |
| 1071 void DownloadsAcceptDangerFunction::DangerPromptCallback( | 1083 void DownloadsAcceptDangerFunction::DangerPromptCallback( |
| 1072 bool accept, int download_id) { | 1084 bool accept, int download_id) { |
| 1085 DownloadItem* download_item = GetDownloadIfInProgress( |
| 1086 profile(), include_incognito(), download_id); |
| 1087 if (!download_item) { |
| 1088 error_ = download_extension_errors::kInvalidOperationError; |
| 1089 return; |
| 1090 } |
| 1073 if (accept) { | 1091 if (accept) { |
| 1074 DownloadItem* download_item = GetDownloadIfInProgress( | 1092 download_item->ValidateDangerousDownload(); |
| 1075 profile(), include_incognito(), download_id); | 1093 } else { |
| 1076 if (download_item) | 1094 download_item->Remove(); |
| 1077 download_item->ValidateDangerousDownload(); | |
| 1078 } | 1095 } |
| 1096 } |
| 1097 |
| 1098 void DownloadsAcceptDangerFunction::DangerPromptDestroyed() { |
| 1079 SendResponse(error_.empty()); | 1099 SendResponse(error_.empty()); |
| 1080 } | 1100 } |
| 1081 | 1101 |
| 1082 DownloadsShowFunction::DownloadsShowFunction() {} | 1102 DownloadsShowFunction::DownloadsShowFunction() {} |
| 1083 | 1103 |
| 1084 DownloadsShowFunction::~DownloadsShowFunction() {} | 1104 DownloadsShowFunction::~DownloadsShowFunction() {} |
| 1085 | 1105 |
| 1086 bool DownloadsShowFunction::RunImpl() { | 1106 bool DownloadsShowFunction::RunImpl() { |
| 1087 scoped_ptr<extensions::api::downloads::Show::Params> params( | 1107 scoped_ptr<extensions::api::downloads::Show::Params> params( |
| 1088 extensions::api::downloads::Show::Params::Create(*args_)); | 1108 extensions::api::downloads::Show::Params::Create(*args_)); |
| 1089 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1109 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 1090 DownloadItem* download_item = GetDownload( | 1110 if (params->download_id) { |
| 1091 profile(), include_incognito(), params->download_id); | 1111 DownloadItem* download_item = GetDownload( |
| 1092 if (!download_item) { | 1112 profile(), include_incognito(), *params->download_id); |
| 1093 error_ = download_extension_errors::kInvalidOperationError; | 1113 if (!download_item) { |
| 1094 return false; | 1114 error_ = download_extension_errors::kInvalidOperationError; |
| 1115 return false; |
| 1116 } |
| 1117 download_item->ShowDownloadInShell(); |
| 1118 } else { |
| 1119 DownloadManager* manager = NULL; |
| 1120 DownloadManager* incognito_manager = NULL; |
| 1121 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); |
| 1122 platform_util::OpenItem(DownloadPrefs::FromDownloadManager( |
| 1123 manager)->DownloadPath()); |
| 1095 } | 1124 } |
| 1096 download_item->ShowDownloadInShell(); | |
| 1097 RecordApiFunctions(DOWNLOADS_FUNCTION_SHOW); | 1125 RecordApiFunctions(DOWNLOADS_FUNCTION_SHOW); |
| 1098 return true; | 1126 return true; |
| 1099 } | 1127 } |
| 1100 | 1128 |
| 1101 DownloadsOpenFunction::DownloadsOpenFunction() {} | 1129 DownloadsOpenFunction::DownloadsOpenFunction() {} |
| 1102 | 1130 |
| 1103 DownloadsOpenFunction::~DownloadsOpenFunction() {} | 1131 DownloadsOpenFunction::~DownloadsOpenFunction() {} |
| 1104 | 1132 |
| 1105 bool DownloadsOpenFunction::RunImpl() { | 1133 bool DownloadsOpenFunction::RunImpl() { |
| 1106 scoped_ptr<extensions::api::downloads::Open::Params> params( | 1134 scoped_ptr<extensions::api::downloads::Open::Params> params( |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1421 std::set<std::string> new_fields; | 1449 std::set<std::string> new_fields; |
| 1422 bool changed = false; | 1450 bool changed = false; |
| 1423 | 1451 |
| 1424 // For each field in the new json representation of the download_item except | 1452 // For each field in the new json representation of the download_item except |
| 1425 // the bytesReceived field, if the field has changed from the previous old | 1453 // the bytesReceived field, if the field has changed from the previous old |
| 1426 // json, set the differences in the |delta| object and remember that something | 1454 // json, set the differences in the |delta| object and remember that something |
| 1427 // significant changed. | 1455 // significant changed. |
| 1428 for (base::DictionaryValue::Iterator iter(*new_json.get()); | 1456 for (base::DictionaryValue::Iterator iter(*new_json.get()); |
| 1429 !iter.IsAtEnd(); iter.Advance()) { | 1457 !iter.IsAtEnd(); iter.Advance()) { |
| 1430 new_fields.insert(iter.key()); | 1458 new_fields.insert(iter.key()); |
| 1431 if (iter.key() != kBytesReceivedKey) { | 1459 if ((iter.key() != kBytesReceivedKey) && |
| 1460 (iter.key() != kEstimatedEndTimeKey)) { |
| 1432 const base::Value* old_value = NULL; | 1461 const base::Value* old_value = NULL; |
| 1433 if (!data->json().HasKey(iter.key()) || | 1462 if (!data->json().HasKey(iter.key()) || |
| 1434 (data->json().Get(iter.key(), &old_value) && | 1463 (data->json().Get(iter.key(), &old_value) && |
| 1435 !iter.value().Equals(old_value))) { | 1464 !iter.value().Equals(old_value))) { |
| 1436 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); | 1465 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); |
| 1437 if (old_value) | 1466 if (old_value) |
| 1438 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); | 1467 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); |
| 1439 changed = true; | 1468 changed = true; |
| 1440 } | 1469 } |
| 1441 } | 1470 } |
| 1442 } | 1471 } |
| 1443 | 1472 |
| 1444 // If a field was in the previous json but is not in the new json, set the | 1473 // If a field was in the previous json but is not in the new json, set the |
| 1445 // difference in |delta|. | 1474 // difference in |delta|. |
| 1446 for (base::DictionaryValue::Iterator iter(data->json()); | 1475 for (base::DictionaryValue::Iterator iter(data->json()); |
| 1447 !iter.IsAtEnd(); iter.Advance()) { | 1476 !iter.IsAtEnd(); iter.Advance()) { |
| 1448 if (new_fields.find(iter.key()) == new_fields.end()) { | 1477 if ((new_fields.find(iter.key()) == new_fields.end()) && |
| 1478 (iter.key() != kEstimatedEndTimeKey)) { |
| 1479 // estimatedEndTime disappears after completion, but bytesReceived stays. |
| 1449 delta->Set(iter.key() + ".previous", iter.value().DeepCopy()); | 1480 delta->Set(iter.key() + ".previous", iter.value().DeepCopy()); |
| 1450 changed = true; | 1481 changed = true; |
| 1451 } | 1482 } |
| 1452 } | 1483 } |
| 1453 | 1484 |
| 1454 // Update the OnChangedStat and dispatch the event if something significant | 1485 // Update the OnChangedStat and dispatch the event if something significant |
| 1455 // changed. Replace the stored json with the new json. | 1486 // changed. Replace the stored json with the new json. |
| 1456 data->OnItemUpdated(); | 1487 data->OnItemUpdated(); |
| 1457 if (changed) { | 1488 if (changed) { |
| 1458 DispatchEvent(events::kOnDownloadChanged, | 1489 DispatchEvent(events::kOnDownloadChanged, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1502 DownloadsNotificationSource notification_source; | 1533 DownloadsNotificationSource notification_source; |
| 1503 notification_source.event_name = event_name; | 1534 notification_source.event_name = event_name; |
| 1504 notification_source.profile = profile_; | 1535 notification_source.profile = profile_; |
| 1505 content::Source<DownloadsNotificationSource> content_source( | 1536 content::Source<DownloadsNotificationSource> content_source( |
| 1506 ¬ification_source); | 1537 ¬ification_source); |
| 1507 content::NotificationService::current()->Notify( | 1538 content::NotificationService::current()->Notify( |
| 1508 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, | 1539 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, |
| 1509 content_source, | 1540 content_source, |
| 1510 content::Details<std::string>(&json_args)); | 1541 content::Details<std::string>(&json_args)); |
| 1511 } | 1542 } |
| OLD | NEW |