Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extension_downloads_api.h" | 5 #include "chrome/browser/extensions/extension_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/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/string16.h" | 19 #include "base/string16.h" |
| 20 #include "base/string_split.h" | |
| 20 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 21 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 #include "chrome/browser/browser_process.h" | 24 #include "chrome/browser/browser_process.h" |
| 24 #include "chrome/browser/download/download_service.h" | 25 #include "chrome/browser/download/download_service.h" |
| 25 #include "chrome/browser/download/download_service_factory.h" | 26 #include "chrome/browser/download/download_service_factory.h" |
| 26 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
| 27 #include "chrome/browser/extensions/extension_downloads_api_constants.h" | |
| 28 #include "chrome/browser/extensions/extension_event_names.h" | 28 #include "chrome/browser/extensions/extension_event_names.h" |
| 29 #include "chrome/browser/extensions/extension_event_router.h" | 29 #include "chrome/browser/extensions/extension_event_router.h" |
| 30 #include "chrome/browser/icon_loader.h" | 30 #include "chrome/browser/icon_loader.h" |
| 31 #include "chrome/browser/icon_manager.h" | 31 #include "chrome/browser/icon_manager.h" |
| 32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 33 #include "chrome/browser/ui/browser_list.h" | 33 #include "chrome/browser/ui/browser_list.h" |
| 34 #include "content/browser/download/download_file_manager.h" | 34 #include "content/browser/download/download_file_manager.h" |
| 35 #include "content/browser/download/download_item.h" | 35 #include "content/browser/download/download_item.h" |
| 36 #include "content/browser/download/download_types.h" | 36 #include "content/browser/download/download_types.h" |
| 37 #include "content/browser/download/interrupt_reasons.h" | 37 #include "content/browser/download/interrupt_reasons.h" |
| 38 #include "content/browser/renderer_host/render_process_host.h" | 38 #include "content/browser/renderer_host/render_process_host.h" |
| 39 #include "content/browser/renderer_host/render_view_host.h" | 39 #include "content/browser/renderer_host/render_view_host.h" |
| 40 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 40 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 41 #include "net/http/http_util.h" | 41 #include "net/http/http_util.h" |
| 42 #include "net/url_request/url_request.h" | 42 #include "net/url_request/url_request.h" |
| 43 | 43 |
| 44 namespace constants = extension_downloads_api_constants; | 44 namespace { |
| 45 | |
| 46 using download_util::DownloadQuery; | |
| 47 | |
| 48 namespace constants { | |
| 49 | |
| 50 // Error messages | |
| 51 const char kNotImplemented[] = "NotImplemented"; | |
| 52 const char kGenericError[] = "I'm afraid I can't do that."; | |
| 53 const char kInvalidURL[] = "Invalid URL"; | |
| 54 | |
| 55 // Parameter keys | |
| 56 const char kBodyKey[] = "body"; | |
| 57 const char kBytesReceivedKey[] = "bytesReceived"; | |
| 58 const char kDangerAcceptedKey[] = "dangerAccepted"; | |
| 59 const char kDangerFile[] = "file"; | |
| 60 const char kDangerKey[] = "danger"; | |
| 61 const char kDangerSafe[] = "safe"; | |
| 62 const char kDangerUrl[] = "url"; | |
| 63 const char kEndTimeKey[] = "endTime"; | |
| 64 const char kEndedAfterKey[] = "endedAfter"; | |
| 65 const char kEndedBeforeKey[] = "endedBefore"; | |
| 66 const char kErrorKey[] = "error"; | |
| 67 const char kFileSizeGreaterKey[] = "fileSizeGreater"; | |
| 68 const char kFileSizeKey[] = "fileSize"; | |
| 69 const char kFileSizeLessKey[] = "fileSizeLess"; | |
| 70 const char kFilenameKey[] = "filename"; | |
| 71 const char kFilenameRegexKey[] = "filenameRegex"; | |
| 72 const char kHeaderNameKey[] = "name"; | |
| 73 const char kHeaderValueKey[] = "value"; | |
| 74 const char kHeadersKey[] = "headers"; | |
| 75 const char kIdKey[] = "id"; | |
| 76 const char kLimitKey[] = "limit"; | |
| 77 const char kMethodKey[] = "method"; | |
| 78 const char kMimeKey[] = "mime"; | |
| 79 const char kOrderByKey[] = "orderBy"; | |
| 80 const char kPausedKey[] = "paused"; | |
| 81 const char kQueryKey[] = "query"; | |
| 82 const char kSaveAsKey[] = "saveAs"; | |
| 83 const char kStartTimeKey[] = "startTime"; | |
| 84 const char kStartedAfterKey[] = "startedAfter"; | |
| 85 const char kStartedBeforeKey[] = "startedBefore"; | |
| 86 const char kStateComplete[] = "complete"; | |
| 87 const char kStateInProgress[] = "in_progress"; | |
| 88 const char kStateInterrupted[] = "interrupted"; | |
| 89 const char kStateKey[] = "state"; | |
| 90 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; | |
| 91 const char kTotalBytesKey[] = "totalBytes"; | |
| 92 const char kTotalBytesLessKey[] = "totalBytesLess"; | |
| 93 const char kUrlKey[] = "url"; | |
| 94 const char kUrlRegexKey[] = "urlRegex"; | |
| 95 | |
| 96 const char* DangerString(DownloadItem::DangerType danger) { | |
| 97 switch (danger) { | |
| 98 case DownloadItem::NOT_DANGEROUS: return kDangerSafe; | |
| 99 case DownloadItem::DANGEROUS_FILE: return kDangerFile; | |
| 100 case DownloadItem::DANGEROUS_URL: return kDangerUrl; | |
| 101 default: | |
| 102 NOTREACHED(); | |
| 103 return ""; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 const char* StateString(DownloadItem::DownloadState state) { | |
| 108 switch (state) { | |
| 109 case DownloadItem::IN_PROGRESS: return kStateInProgress; | |
| 110 case DownloadItem::COMPLETE: return kStateComplete; | |
| 111 case DownloadItem::INTERRUPTED: // fall through | |
| 112 case DownloadItem::CANCELLED: return kStateInterrupted; | |
| 113 case DownloadItem::REMOVING: // fall through | |
|
cbentzel
2011/10/20 18:18:53
Is this an intentional fall through here? It may g
| |
| 114 default: | |
| 115 NOTREACHED(); | |
| 116 return ""; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 DownloadItem::DangerType DangerEnumFromString(const std::string& danger) { | |
| 121 if (danger == kDangerSafe) return DownloadItem::NOT_DANGEROUS; | |
| 122 if (danger == kDangerFile) return DownloadItem::DANGEROUS_FILE; | |
| 123 if (danger == kDangerUrl) return DownloadItem::DANGEROUS_URL; | |
| 124 return DownloadItem::DANGEROUS_TYPE_MAX; | |
| 125 } | |
| 126 | |
| 127 DownloadItem::DownloadState StateEnumFromString(const std::string& state) { | |
| 128 if (state == kStateInProgress) return DownloadItem::IN_PROGRESS; | |
| 129 if (state == kStateComplete) return DownloadItem::COMPLETE; | |
| 130 if (state == kStateInterrupted) return DownloadItem::INTERRUPTED; | |
| 131 return DownloadItem::MAX_DOWNLOAD_STATE; | |
| 132 } | |
| 133 | |
| 134 } // namespace constants | |
| 135 | |
| 136 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { | |
| 137 base::DictionaryValue* json = new base::DictionaryValue(); | |
| 138 json->SetInteger(constants::kIdKey, item->id()); | |
| 139 json->SetString(constants::kUrlKey, item->original_url().spec()); | |
| 140 json->SetString(constants::kFilenameKey, | |
| 141 item->full_path().LossyDisplayName()); | |
| 142 json->SetString(constants::kDangerKey, | |
| 143 constants::DangerString(item->GetDangerType())); | |
| 144 if (item->safety_state() != DownloadItem::SAFE) | |
| 145 json->SetBoolean(constants::kDangerAcceptedKey, | |
| 146 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); | |
| 147 json->SetString(constants::kStateKey, | |
| 148 constants::StateString(item->state())); | |
| 149 json->SetBoolean(constants::kPausedKey, item->is_paused()); | |
| 150 json->SetString(constants::kMimeKey, item->mime_type()); | |
| 151 // TODO(benjhayden): Change startTime to a double! | |
| 152 json->SetInteger(constants::kStartTimeKey, | |
| 153 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); | |
| 154 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); | |
| 155 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); | |
| 156 if (item->state() == DownloadItem::INTERRUPTED) | |
| 157 json->SetInteger(constants::kErrorKey, | |
| 158 static_cast<int>(item->last_reason())); | |
| 159 // TODO(benjhayden): Implement endTime and fileSize. | |
| 160 // json->SetInteger(constants::kEndTimeKey, -1); | |
| 161 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | |
| 162 return json; | |
| 163 } | |
| 164 | |
| 165 template <typename T> | |
| 166 bool GetJsonValue(base::DictionaryValue* json, const char* name, T* value); | |
| 167 | |
| 168 template<> | |
| 169 bool GetJsonValue(base::DictionaryValue* json, const char* name, int* value) { | |
|
cbentzel
2011/10/20 18:18:53
This seems like a good use of template specializat
| |
| 170 return json->GetInteger(name, value); | |
| 171 } | |
| 172 | |
| 173 template<> | |
| 174 bool GetJsonValue(base::DictionaryValue* json, const char* name, | |
| 175 string16* value) { | |
| 176 return json->GetString(name, value); | |
| 177 } | |
| 178 | |
| 179 template<> | |
| 180 bool GetJsonValue(base::DictionaryValue* json, const char* name, | |
| 181 std::string* value) { | |
| 182 return json->GetString(name, value); | |
| 183 } | |
| 184 | |
| 185 template<> | |
| 186 bool GetJsonValue(base::DictionaryValue* json, const char* name, bool* value) { | |
| 187 return json->GetBoolean(name, value); | |
| 188 } | |
| 189 | |
| 190 struct SortFieldParsePair { | |
| 191 const char* json_name; | |
| 192 const DownloadQuery::SortFieldName field_name; | |
| 193 }; | |
| 194 | |
| 195 const SortFieldParsePair sort_field_parse_pairs[] = { | |
| 196 {constants::kBytesReceivedKey, DownloadQuery::SORT_FIELD_BYTES_RECEIVED}, | |
| 197 {constants::kDangerKey, DownloadQuery::SORT_FIELD_DANGER}, | |
| 198 {constants::kDangerAcceptedKey, DownloadQuery::SORT_FIELD_DANGER_ACCEPTED}, | |
| 199 {constants::kEndTimeKey, DownloadQuery::SORT_FIELD_END_TIME}, | |
| 200 {constants::kErrorKey, DownloadQuery::SORT_FIELD_ERROR}, | |
| 201 {constants::kFilenameKey, DownloadQuery::SORT_FIELD_FILENAME}, | |
| 202 {constants::kFileSizeKey, DownloadQuery::SORT_FIELD_FILE_SIZE}, | |
| 203 {constants::kMimeKey, DownloadQuery::SORT_FIELD_MIME}, | |
| 204 {constants::kPausedKey, DownloadQuery::SORT_FIELD_PAUSED}, | |
| 205 {constants::kStartTimeKey, DownloadQuery::SORT_FIELD_START_TIME}, | |
| 206 {constants::kStateKey, DownloadQuery::SORT_FIELD_STATE}, | |
| 207 {constants::kTotalBytesKey, DownloadQuery::SORT_FIELD_TOTAL_BYTES}, | |
| 208 {constants::kUrlKey, DownloadQuery::SORT_FIELD_URL}, | |
| 209 }; | |
| 210 | |
| 211 bool DownloadSearchOrderByStringToEnum( | |
| 212 const std::string& json_name, DownloadQuery::SortFieldName* field_name) { | |
| 213 for (uint32 i = 0; i < arraysize(sort_field_parse_pairs); ++i) { | |
| 214 if (json_name == sort_field_parse_pairs[i].json_name) { | |
| 215 *field_name = sort_field_parse_pairs[i].field_name; | |
| 216 return true; | |
| 217 } | |
| 218 } | |
| 219 return false; | |
| 220 } | |
| 221 | |
| 222 } // anonymous namespace | |
| 45 | 223 |
| 46 bool DownloadsFunctionInterface::RunImplImpl( | 224 bool DownloadsFunctionInterface::RunImplImpl( |
| 47 DownloadsFunctionInterface* pimpl) { | 225 DownloadsFunctionInterface* pimpl) { |
| 48 CHECK(pimpl); | 226 CHECK(pimpl); |
| 49 if (!pimpl->ParseArgs()) return false; | 227 if (!pimpl->ParseArgs()) return false; |
| 50 UMA_HISTOGRAM_ENUMERATION( | 228 UMA_HISTOGRAM_ENUMERATION( |
| 51 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); | 229 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); |
| 52 pimpl->RunInternal(); | 230 pimpl->RunInternal(); |
| 53 return true; | 231 return true; |
| 54 } | 232 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 VLOG(1) << __FUNCTION__; | 395 VLOG(1) << __FUNCTION__; |
| 218 if (dl_id >= 0) { | 396 if (dl_id >= 0) { |
| 219 result_.reset(base::Value::CreateIntegerValue(dl_id)); | 397 result_.reset(base::Value::CreateIntegerValue(dl_id)); |
| 220 } else { | 398 } else { |
| 221 error_ = net::ErrorToString(error); | 399 error_ = net::ErrorToString(error); |
| 222 } | 400 } |
| 223 SendResponse(error_.empty()); | 401 SendResponse(error_.empty()); |
| 224 } | 402 } |
| 225 | 403 |
| 226 DownloadsSearchFunction::DownloadsSearchFunction() | 404 DownloadsSearchFunction::DownloadsSearchFunction() |
| 227 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) { | 405 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), |
| 406 get_id_(0), | |
| 407 has_get_id_(false) { | |
| 228 } | 408 } |
| 229 | 409 |
| 230 DownloadsSearchFunction::~DownloadsSearchFunction() {} | 410 DownloadsSearchFunction::~DownloadsSearchFunction() {} |
| 231 | 411 |
| 412 template <typename ValueType> | |
| 413 bool DownloadsSearchFunction::Parse( | |
|
cbentzel
2011/10/20 18:18:53
I think this could be a non-member function if it
| |
| 414 base::DictionaryValue* json, | |
| 415 const char* json_name, | |
| 416 DownloadQuery::FilterFieldName filter_name) { | |
| 417 if (json->HasKey(json_name)) { | |
| 418 ValueType value; | |
| 419 EXTENSION_FUNCTION_VALIDATE(GetJsonValue(json, json_name, &value)); | |
| 420 if (!query_.Set(filter_name, value)) { | |
| 421 error_ = constants::kGenericError; // TODO(benjhayden) | |
| 422 return false; | |
| 423 } | |
| 424 } | |
| 425 return true; | |
| 426 } | |
| 427 | |
| 232 bool DownloadsSearchFunction::ParseArgs() { | 428 bool DownloadsSearchFunction::ParseArgs() { |
| 233 base::DictionaryValue* query_json = NULL; | 429 base::DictionaryValue* query_json = NULL; |
| 234 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); | 430 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); |
| 235 error_ = constants::kNotImplemented; | 431 if (query_json->HasKey(constants::kDangerKey)) { |
| 236 return false; | 432 std::string danger; |
| 433 EXTENSION_FUNCTION_VALIDATE(query_json->GetString( | |
| 434 constants::kDangerKey, &danger)); | |
| 435 if (!query_.Set(DownloadQuery::FILTER_FIELD_DANGER, | |
| 436 constants::DangerEnumFromString(danger))) { | |
| 437 error_ = constants::kGenericError; // TODO | |
| 438 return false; | |
| 439 } | |
| 440 } | |
| 441 if (query_json->HasKey(constants::kStateKey)) { | |
| 442 std::string state; | |
| 443 EXTENSION_FUNCTION_VALIDATE(query_json->GetString( | |
| 444 constants::kStateKey, &state)); | |
| 445 if (!query_.Set(DownloadQuery::FILTER_FIELD_STATE, | |
| 446 constants::StateEnumFromString(state))) { | |
| 447 error_ = constants::kGenericError; // TODO | |
| 448 return false; | |
| 449 } | |
| 450 } | |
| 451 if (query_json->HasKey(constants::kIdKey)) { | |
| 452 EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger( | |
| 453 constants::kIdKey, &get_id_)); | |
| 454 has_get_id_ = true; | |
| 455 } | |
| 456 if (query_json->HasKey(constants::kOrderByKey)) { | |
|
asanka
2011/10/20 19:29:16
The extensions API doc should be updated to mentio
| |
| 457 std::string order_by_str; | |
| 458 EXTENSION_FUNCTION_VALIDATE(query_json->GetString( | |
| 459 constants::kOrderByKey, &order_by_str)); | |
| 460 std::vector<std::string> order_by_strs; | |
| 461 base::SplitString(order_by_str, ' ', &order_by_strs); | |
| 462 DownloadQuery::OrderTerms order_terms; | |
| 463 for (std::vector<std::string>::const_iterator iter = order_by_strs.begin(); | |
| 464 iter != order_by_strs.end(); ++iter) { | |
| 465 std::string term_str = *iter; | |
| 466 bool ascending = true; | |
| 467 if (term_str[0] == '-') { | |
| 468 ascending = false; | |
| 469 term_str = term_str.substr(1); | |
| 470 } | |
| 471 DownloadQuery::SortFieldName field_enum; | |
| 472 if (!DownloadSearchOrderByStringToEnum(term_str, &field_enum)) { | |
| 473 error_ = constants::kGenericError; // TODO | |
| 474 return false; | |
| 475 } | |
| 476 order_terms.push_back(DownloadQuery::OrderTerm(field_enum, ascending)); | |
| 477 } | |
| 478 query_.OrderBy(order_terms); | |
| 479 } | |
| 480 if (query_json->HasKey(constants::kLimitKey)) { | |
| 481 int limit = 0; | |
| 482 EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger( | |
| 483 constants::kLimitKey, &limit)); | |
| 484 query_.Limit(limit); | |
| 485 } | |
| 486 return Parse<int>(query_json, constants::kStartTimeKey, | |
| 487 DownloadQuery::FILTER_FIELD_START_TIME) && | |
| 488 Parse<int>(query_json, constants::kEndTimeKey, | |
| 489 DownloadQuery::FILTER_FIELD_END_TIME) && | |
| 490 Parse<int>(query_json, constants::kStartedBeforeKey, | |
| 491 DownloadQuery::FILTER_FIELD_STARTED_BEFORE) && | |
| 492 Parse<int>(query_json, constants::kStartedAfterKey, | |
| 493 DownloadQuery::FILTER_FIELD_STARTED_AFTER) && | |
| 494 Parse<int>(query_json, constants::kEndedBeforeKey, | |
| 495 DownloadQuery::FILTER_FIELD_ENDED_BEFORE) && | |
| 496 Parse<int>(query_json, constants::kEndedAfterKey, | |
| 497 DownloadQuery::FILTER_FIELD_ENDED_AFTER) && | |
| 498 Parse<int>(query_json, constants::kBytesReceivedKey, | |
| 499 DownloadQuery::FILTER_FIELD_BYTES_RECEIVED) && | |
| 500 Parse<int>(query_json, constants::kTotalBytesKey, | |
| 501 DownloadQuery::FILTER_FIELD_TOTAL_BYTES) && | |
| 502 Parse<int>(query_json, constants::kTotalBytesGreaterKey, | |
| 503 DownloadQuery::FILTER_FIELD_TOTAL_BYTES_GREATER) && | |
| 504 Parse<int>(query_json, constants::kTotalBytesLessKey, | |
| 505 DownloadQuery::FILTER_FIELD_TOTAL_BYTES_LESS) && | |
| 506 Parse<int>(query_json, constants::kFileSizeKey, | |
| 507 DownloadQuery::FILTER_FIELD_FILE_SIZE) && | |
| 508 Parse<int>(query_json, constants::kFileSizeGreaterKey, | |
| 509 DownloadQuery::FILTER_FIELD_FILE_SIZE_GREATER) && | |
| 510 Parse<int>(query_json, constants::kFileSizeLessKey, | |
| 511 DownloadQuery::FILTER_FIELD_FILE_SIZE_LESS) && | |
| 512 Parse<int>(query_json, constants::kErrorKey, | |
| 513 DownloadQuery::FILTER_FIELD_ERROR) && | |
| 514 Parse<bool>(query_json, constants::kDangerAcceptedKey, | |
| 515 DownloadQuery::FILTER_FIELD_DANGER_ACCEPTED) && | |
| 516 Parse<bool>(query_json, constants::kPausedKey, | |
| 517 DownloadQuery::FILTER_FIELD_PAUSED) && | |
| 518 Parse<string16>(query_json, constants::kFilenameKey, | |
| 519 DownloadQuery::FILTER_FIELD_FILENAME) && | |
| 520 Parse<std::string>(query_json, constants::kMimeKey, | |
| 521 DownloadQuery::FILTER_FIELD_MIME) && | |
| 522 Parse<std::string>(query_json, constants::kQueryKey, | |
| 523 DownloadQuery::FILTER_FIELD_QUERY) && | |
| 524 Parse<std::string>(query_json, constants::kFilenameRegexKey, | |
| 525 DownloadQuery::FILTER_FIELD_FILENAME_REGEX) && | |
| 526 Parse<std::string>(query_json, constants::kUrlRegexKey, | |
| 527 DownloadQuery::FILTER_FIELD_URL_REGEX) && | |
| 528 Parse<std::string>(query_json, constants::kUrlKey, | |
| 529 DownloadQuery::FILTER_FIELD_URL); | |
| 237 } | 530 } |
| 238 | 531 |
| 239 void DownloadsSearchFunction::RunInternal() { | 532 void DownloadsSearchFunction::RunInternal() { |
| 240 NOTIMPLEMENTED(); | 533 DownloadManager::DownloadVector cpp_results; |
| 534 if (has_get_id_) { | |
| 535 DownloadItem* item = profile()->GetDownloadManager()->GetDownloadItem( | |
| 536 get_id_); | |
| 537 if (item && query_.Matches(*item)) | |
| 538 cpp_results.push_back(item); | |
| 539 } else { | |
| 540 profile()->GetDownloadManager()->Search(query_, &cpp_results); | |
| 541 } | |
| 542 base::ListValue* json_results = new base::ListValue(); | |
| 543 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); | |
| 544 it != cpp_results.end(); ++it) { | |
| 545 json_results->Append(DownloadItemToJSON(*it)); | |
| 546 } | |
| 547 result_.reset(json_results); | |
| 241 } | 548 } |
| 242 | 549 |
| 243 DownloadsPauseFunction::DownloadsPauseFunction() | 550 DownloadsPauseFunction::DownloadsPauseFunction() |
| 244 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) { | 551 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) { |
| 245 } | 552 } |
| 246 | 553 |
| 247 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 554 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
| 248 | 555 |
| 249 bool DownloadsPauseFunction::ParseArgs() { | 556 bool DownloadsPauseFunction::ParseArgs() { |
| 250 int dl_id = 0; | 557 int dl_id = 0; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 685 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 379 VLOG(1) << __FUNCTION__ << " " << dl_id; | 686 VLOG(1) << __FUNCTION__ << " " << dl_id; |
| 380 error_ = constants::kNotImplemented; | 687 error_ = constants::kNotImplemented; |
| 381 return false; | 688 return false; |
| 382 } | 689 } |
| 383 | 690 |
| 384 void DownloadsDragFunction::RunInternal() { | 691 void DownloadsDragFunction::RunInternal() { |
| 385 NOTIMPLEMENTED(); | 692 NOTIMPLEMENTED(); |
| 386 } | 693 } |
| 387 | 694 |
| 388 namespace { | |
| 389 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { | |
| 390 base::DictionaryValue* json = new base::DictionaryValue(); | |
| 391 json->SetInteger(constants::kIdKey, item->id()); | |
| 392 json->SetString(constants::kUrlKey, item->original_url().spec()); | |
| 393 json->SetString(constants::kFilenameKey, | |
| 394 item->full_path().LossyDisplayName()); | |
| 395 json->SetString(constants::kDangerKey, | |
| 396 constants::DangerString(item->GetDangerType())); | |
| 397 json->SetBoolean(constants::kDangerAcceptedKey, | |
| 398 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); | |
| 399 json->SetString(constants::kStateKey, | |
| 400 constants::StateString(item->state())); | |
| 401 json->SetBoolean(constants::kPausedKey, item->is_paused()); | |
| 402 json->SetString(constants::kMimeKey, item->mime_type()); | |
| 403 json->SetInteger(constants::kStartTimeKey, | |
| 404 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); | |
| 405 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); | |
| 406 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); | |
| 407 if (item->state() == DownloadItem::INTERRUPTED) | |
| 408 json->SetInteger(constants::kErrorKey, | |
| 409 static_cast<int>(item->last_reason())); | |
| 410 // TODO(benjhayden): Implement endTime and fileSize. | |
| 411 // json->SetInteger(constants::kEndTimeKey, -1); | |
| 412 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | |
| 413 return json; | |
| 414 } | |
| 415 } // anonymous namespace | |
| 416 | |
| 417 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 695 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( |
| 418 Profile* profile) | 696 Profile* profile) |
| 419 : profile_(profile), | 697 : profile_(profile), |
| 420 manager_( | 698 manager_( |
| 421 profile ? | 699 profile ? |
| 422 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : | 700 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : |
| 423 NULL) { | 701 NULL) { |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 702 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 425 DCHECK(profile_); | 703 DCHECK(profile_); |
| 426 DCHECK(manager_); | 704 DCHECK(manager_); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 ListValue args; | 764 ListValue args; |
| 487 args.Append(arg); | 765 args.Append(arg); |
| 488 std::string json_args; | 766 std::string json_args; |
| 489 base::JSONWriter::Write(&args, false, &json_args); | 767 base::JSONWriter::Write(&args, false, &json_args); |
| 490 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 768 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 491 event_name, | 769 event_name, |
| 492 json_args, | 770 json_args, |
| 493 profile_, | 771 profile_, |
| 494 GURL()); | 772 GURL()); |
| 495 } | 773 } |
| OLD | NEW |