Chromium Code Reviews| Index: chrome/browser/extensions/extension_downloads_api.cc |
| diff --git a/chrome/browser/extensions/extension_downloads_api.cc b/chrome/browser/extensions/extension_downloads_api.cc |
| index 0e1470eb1c556a3fa32a6b071d38e7cda9143ced..56388c24b966158948e89070f2573db90a821142 100644 |
| --- a/chrome/browser/extensions/extension_downloads_api.cc |
| +++ b/chrome/browser/extensions/extension_downloads_api.cc |
| @@ -24,7 +24,6 @@ |
| #include "chrome/browser/download/download_service.h" |
| #include "chrome/browser/download/download_service_factory.h" |
| #include "chrome/browser/download/download_util.h" |
| -#include "chrome/browser/extensions/extension_downloads_api_constants.h" |
| #include "chrome/browser/extensions/extension_event_names.h" |
| #include "chrome/browser/extensions/extension_event_router.h" |
| #include "chrome/browser/icon_loader.h" |
| @@ -41,7 +40,150 @@ |
| #include "net/http/http_util.h" |
| #include "net/url_request/url_request.h" |
| -namespace constants = extension_downloads_api_constants; |
| +namespace { |
| + |
| +namespace constants { |
| + |
| +// Error messages |
| +const char kNotImplemented[] = "NotImplemented"; |
| +const char kGenericError[] = "I'm afraid I can't do that."; |
| +const char kInvalidURL[] = "Invalid URL"; |
| + |
| +// Parameter keys |
| +const char kBodyKey[] = "body"; |
| +const char kBytesReceivedKey[] = "bytesReceived"; |
| +const char kDangerAcceptedKey[] = "dangerAccepted"; |
| +const char kDangerFile[] = "file"; |
| +const char kDangerKey[] = "danger"; |
| +const char kDangerSafe[] = "safe"; |
| +const char kDangerUrl[] = "url"; |
| +const char kEndTimeKey[] = "endTime"; |
| +const char kEndedAfterKey[] = "endedAfter"; |
| +const char kEndedBeforeKey[] = "endedBefore"; |
| +const char kErrorKey[] = "error"; |
| +const char kFileSizeGreaterKey[] = "fileSizeGreater"; |
| +const char kFileSizeKey[] = "fileSize"; |
| +const char kFileSizeLessKey[] = "fileSizeLess"; |
| +const char kFilenameKey[] = "filename"; |
| +const char kFilenameRegexKey[] = "filenameRegex"; |
| +const char kHeaderNameKey[] = "name"; |
| +const char kHeaderValueKey[] = "value"; |
| +const char kHeadersKey[] = "headers"; |
| +const char kIdKey[] = "id"; |
| +const char kLimitKey[] = "limit"; |
| +const char kMethodKey[] = "method"; |
| +const char kMimeKey[] = "mime"; |
| +const char kOrderByKey[] = "orderBy"; |
| +const char kPausedKey[] = "paused"; |
| +const char kQueryKey[] = "query"; |
| +const char kSaveAsKey[] = "saveAs"; |
| +const char kStartTimeKey[] = "startTime"; |
| +const char kStartedAfterKey[] = "startedAfter"; |
| +const char kStartedBeforeKey[] = "startedBefore"; |
| +const char kStateComplete[] = "complete"; |
| +const char kStateInProgress[] = "in_progress"; |
| +const char kStateInterrupted[] = "interrupted"; |
| +const char kStateKey[] = "state"; |
| +const char kTotalBytesGreaterKey[] = "totalBytesGreater"; |
| +const char kTotalBytesKey[] = "totalBytes"; |
| +const char kTotalBytesLessKey[] = "totalBytesLess"; |
| +const char kUrlKey[] = "url"; |
| +const char kUrlRegexKey[] = "urlRegex"; |
| + |
| +const char* DangerString(DownloadItem::DangerType danger) { |
| + switch (danger) { |
| + case DownloadItem::NOT_DANGEROUS: return kDangerSafe; |
| + case DownloadItem::DANGEROUS_FILE: return kDangerFile; |
| + case DownloadItem::DANGEROUS_URL: return kDangerUrl; |
| + default: |
| + NOTREACHED(); |
| + return ""; |
| + } |
| +} |
| + |
| +const char* StateString(DownloadItem::DownloadState state) { |
| + switch (state) { |
| + case DownloadItem::IN_PROGRESS: return kStateInProgress; |
| + case DownloadItem::COMPLETE: return kStateComplete; |
| + case DownloadItem::INTERRUPTED: // fall through |
| + case DownloadItem::CANCELLED: return kStateInterrupted; |
| + case DownloadItem::REMOVING: // fall through |
| + default: |
| + NOTREACHED(); |
| + return ""; |
| + } |
| +} |
| + |
| +DownloadItem::DangerType DangerEnumFromString(const std::string& danger) { |
| + if (danger == kDangerSafe) return DownloadItem::NOT_DANGEROUS; |
| + if (danger == kDangerFile) return DownloadItem::DANGEROUS_FILE; |
| + if (danger == kDangerUrl) return DownloadItem::DANGEROUS_URL; |
| + return DownloadItem::DANGEROUS_TYPE_MAX; |
| +} |
| + |
| +DownloadItem::DownloadState StateEnumFromString(const std::string& state) { |
| + if (state == kStateInProgress) return DownloadItem::IN_PROGRESS; |
| + if (state == kStateComplete) return DownloadItem::COMPLETE; |
| + if (state == kStateInterrupted) return DownloadItem::INTERRUPTED; |
| + return DownloadItem::MAX_DOWNLOAD_STATE; |
| +} |
| + |
| +} // namespace constants |
| + |
| +base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { |
| + base::DictionaryValue* json = new base::DictionaryValue(); |
| + json->SetInteger(constants::kIdKey, item->id()); |
| + json->SetString(constants::kUrlKey, item->original_url().spec()); |
| + json->SetString(constants::kFilenameKey, |
| + item->full_path().LossyDisplayName()); |
| + json->SetString(constants::kDangerKey, |
| + constants::DangerString(item->GetDangerType())); |
| + json->SetBoolean(constants::kDangerAcceptedKey, |
| + item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); |
|
Randy Smith (Not in Mondays)
2011/10/13 23:23:11
If it's not dangerous, should this value be set at
benjhayden
2011/10/14 19:31:02
I hadn't thought about it, that makes sense.
|
| + json->SetString(constants::kStateKey, |
| + constants::StateString(item->state())); |
| + json->SetBoolean(constants::kPausedKey, item->is_paused()); |
| + json->SetString(constants::kMimeKey, item->mime_type()); |
| + // TODO(benjhayden): Change startTime to a double! |
| + json->SetInteger(constants::kStartTimeKey, |
| + (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); |
| + json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); |
| + json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); |
| + if (item->state() == DownloadItem::INTERRUPTED) |
| + json->SetInteger(constants::kErrorKey, |
| + static_cast<int>(item->last_reason())); |
| + // TODO(benjhayden): Implement endTime and fileSize. |
| + // json->SetInteger(constants::kEndTimeKey, -1); |
| + json->SetInteger(constants::kFileSizeKey, item->total_bytes()); |
| + return json; |
| +} |
| + |
| +template <typename T> |
| +bool GetJsonValue(base::DictionaryValue* json, const char* name, T* value); |
| + |
| +template<> |
| +bool GetJsonValue(base::DictionaryValue* json, const char* name, int* value) { |
| + return json->GetInteger(name, value); |
| +} |
| + |
| +template<> |
| +bool GetJsonValue(base::DictionaryValue* json, const char* name, |
| + string16* value) { |
| + return json->GetString(name, value); |
| +} |
| + |
| +template<> |
| +bool GetJsonValue(base::DictionaryValue* json, const char* name, |
| + std::string* value) { |
| + return json->GetString(name, value); |
| +} |
| + |
| +template<> |
| +bool GetJsonValue(base::DictionaryValue* json, const char* name, bool* value) { |
| + return json->GetBoolean(name, value); |
| +} |
| + |
| +} // anonymous namespace |
| bool DownloadsFunctionInterface::RunImplImpl( |
| DownloadsFunctionInterface* pimpl) { |
| @@ -224,20 +366,110 @@ void DownloadsDownloadFunction::RespondOnUIThread(int dl_id, net::Error error) { |
| } |
| DownloadsSearchFunction::DownloadsSearchFunction() |
| - : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) { |
| + : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), |
| + get_id_(0), |
| + has_get_id_(false) { |
| } |
| DownloadsSearchFunction::~DownloadsSearchFunction() {} |
| +template <typename ValueType> |
| +bool DownloadsSearchFunction::Parse(base::DictionaryValue* json, |
| + const char* name) { |
| + if (json->HasKey(name)) { |
| + ValueType value; |
| + EXTENSION_FUNCTION_VALIDATE(GetJsonValue(json, name, &value)); |
| + if (!query_.Set(name, value)) { |
| + error_ = constants::kGenericError; // TODO(benjhayden) |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| bool DownloadsSearchFunction::ParseArgs() { |
| base::DictionaryValue* query_json = NULL; |
| EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); |
| - error_ = constants::kNotImplemented; |
| - return false; |
| + if (query_json->HasKey(constants::kDangerKey)) { |
| + std::string danger; |
| + EXTENSION_FUNCTION_VALIDATE(query_json->GetString( |
| + constants::kDangerKey, &danger)); |
| + if (!query_.Set(constants::kDangerKey, |
| + constants::DangerEnumFromString(danger))) { |
| + error_ = constants::kGenericError; // TODO |
| + return false; |
| + } |
| + } |
| + if (query_json->HasKey(constants::kStateKey)) { |
| + std::string state; |
| + EXTENSION_FUNCTION_VALIDATE(query_json->GetString( |
| + constants::kStateKey, &state)); |
| + if (!query_.Set(constants::kStateKey, |
| + constants::StateEnumFromString(state))) { |
| + error_ = constants::kGenericError; // TODO |
| + return false; |
| + } |
| + } |
| + if (query_json->HasKey(constants::kIdKey)) { |
| + EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger( |
| + constants::kIdKey, &get_id_)); |
| + has_get_id_ = true; |
| + } |
| + if (query_json->HasKey(constants::kOrderByKey)) { |
| + std::string order_by; |
| + EXTENSION_FUNCTION_VALIDATE(query_json->GetString( |
| + constants::kOrderByKey, &order_by)); |
| + if (!query_.OrderBy(order_by)) { |
| + error_ = constants::kGenericError; // TODO |
| + return false; |
| + } |
| + } |
| + if (query_json->HasKey(constants::kLimitKey)) { |
| + int limit = 0; |
| + EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger( |
| + constants::kLimitKey, &limit)); |
| + query_.Limit(limit); |
| + } |
| + return Parse<int>(query_json, constants::kStartTimeKey) && |
| + Parse<int>(query_json, constants::kEndTimeKey) && |
| + Parse<int>(query_json, constants::kStartedBeforeKey) && |
| + Parse<int>(query_json, constants::kStartedAfterKey) && |
| + Parse<int>(query_json, constants::kEndedBeforeKey) && |
| + Parse<int>(query_json, constants::kEndedAfterKey) && |
| + Parse<int>(query_json, constants::kBytesReceivedKey) && |
| + Parse<int>(query_json, constants::kTotalBytesKey) && |
| + Parse<int>(query_json, constants::kTotalBytesGreaterKey) && |
| + Parse<int>(query_json, constants::kTotalBytesLessKey) && |
| + Parse<int>(query_json, constants::kFileSizeKey) && |
| + Parse<int>(query_json, constants::kFileSizeGreaterKey) && |
| + Parse<int>(query_json, constants::kFileSizeLessKey) && |
| + Parse<int>(query_json, constants::kErrorKey) && |
| + Parse<bool>(query_json, constants::kDangerAcceptedKey) && |
| + Parse<bool>(query_json, constants::kPausedKey) && |
| + Parse<string16>(query_json, constants::kFilenameKey) && |
| + Parse<std::string>(query_json, constants::kMimeKey) && |
| + Parse<std::string>(query_json, constants::kQueryKey) && |
| + Parse<std::string>(query_json, constants::kFilenameRegexKey) && |
| + Parse<std::string>(query_json, constants::kUrlRegexKey) && |
| + Parse<std::string>(query_json, constants::kUrlKey); |
| } |
| void DownloadsSearchFunction::RunInternal() { |
| - NOTIMPLEMENTED(); |
| + DownloadManager::DownloadVector cpp_results; |
| + if (has_get_id_) { |
| + DownloadItem* item = profile()->GetDownloadManager()->GetDownloadItem( |
| + get_id_); |
| + if (item && query_.Matches(*item)) |
| + cpp_results.push_back(item); |
| + } else { |
| + profile()->GetDownloadManager()->Search(query_, &cpp_results); |
| + } |
| + base::ListValue* json_results = new base::ListValue(); |
| + for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); |
| + it != cpp_results.end(); ++it) { |
| + json_results->Append(DownloadItemToJSON(*it)); |
| + } |
| + result_.reset(json_results); |
| } |
| DownloadsPauseFunction::DownloadsPauseFunction() |
| @@ -385,35 +617,6 @@ void DownloadsDragFunction::RunInternal() { |
| NOTIMPLEMENTED(); |
| } |
| -namespace { |
| -base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { |
| - base::DictionaryValue* json = new base::DictionaryValue(); |
| - json->SetInteger(constants::kIdKey, item->id()); |
| - json->SetString(constants::kUrlKey, item->original_url().spec()); |
| - json->SetString(constants::kFilenameKey, |
| - item->full_path().LossyDisplayName()); |
| - json->SetString(constants::kDangerKey, |
| - constants::DangerString(item->GetDangerType())); |
| - json->SetBoolean(constants::kDangerAcceptedKey, |
| - item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); |
| - json->SetString(constants::kStateKey, |
| - constants::StateString(item->state())); |
| - json->SetBoolean(constants::kPausedKey, item->is_paused()); |
| - json->SetString(constants::kMimeKey, item->mime_type()); |
| - json->SetInteger(constants::kStartTimeKey, |
| - (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); |
| - json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); |
| - json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); |
| - if (item->state() == DownloadItem::INTERRUPTED) |
| - json->SetInteger(constants::kErrorKey, |
| - static_cast<int>(item->last_reason())); |
| - // TODO(benjhayden): Implement endTime and fileSize. |
| - // json->SetInteger(constants::kEndTimeKey, -1); |
| - json->SetInteger(constants::kFileSizeKey, item->total_bytes()); |
| - return json; |
| -} |
| -} // anonymous namespace |
| - |
| ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( |
| Profile* profile) |
| : profile_(profile), |