Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Unified Diff: chrome/browser/extensions/extension_downloads_api.cc

Issue 7825035: Implement chrome.experimental.downloads.search() (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: " Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 2182b000fdcf62ef0ff1d153824ef259c545a0f2..5d56c73871bbb9e4b459c0b6231a7dedcf63591e 100644
--- a/chrome/browser/extensions/extension_downloads_api.cc
+++ b/chrome/browser/extensions/extension_downloads_api.cc
@@ -40,6 +40,35 @@
namespace constants = extension_downloads_api_constants;
+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_error()));
+ // TODO(benjhayden): Implement endTime and fileSize.
+ // json->SetInteger(constants::kEndTimeKey, -1);
+ json->SetInteger(constants::kFileSizeKey, item->total_bytes());
+ return json;
+}
+} // anonymous namespace
+
bool DownloadsFunctionInterface::RunImplImpl(
DownloadsFunctionInterface* pimpl) {
CHECK(pimpl);
@@ -221,7 +250,8 @@ void DownloadsDownloadFunction::RespondOnUIThread(int dl_id, net::Error error) {
}
DownloadsSearchFunction::DownloadsSearchFunction()
- : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) {
+ : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH),
+ get_id_(-1) {
}
DownloadsSearchFunction::~DownloadsSearchFunction() {}
@@ -229,12 +259,41 @@ DownloadsSearchFunction::~DownloadsSearchFunction() {}
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::kIdKey)) {
+ EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger(
+ constants::kIdKey, &get_id_));
Randy Smith (Not in Mondays) 2011/10/10 18:09:42 What is the behavior if we have both an id and oth
benjhayden 2011/10/13 14:07:34 search() will only return items that match the que
+ } else {
+#define CppInteger int
+#define CppString std::string
+#define CppBoolean bool
+#define FIELD(field_name, field_type) \
+ if (query_json->HasKey(#field_name)) { \
+ Cpp ## field_type value; \
+ EXTENSION_FUNCTION_VALIDATE(query_json->Get ## field_type( \
+ #field_name, &value)); \
+ query_.field_name(value); \
+ }
+#include "content/browser/download/download_query_fields.h"
Aaron Boodman 2011/10/12 08:26:08 whoa.
cbentzel 2011/10/12 09:26:37 Ben is reimplementing the code in a way that won't
benjhayden 2011/10/13 14:07:34 Done.
benjhayden 2011/10/13 14:07:34 Done.
+#undef FIELD
+#undef CppInteger
+#undef CppString
+#undef CppBoolean
+ }
+ return true;
}
void DownloadsSearchFunction::RunInternal() {
- NOTIMPLEMENTED();
+ base::ListValue* results = new base::ListValue();
+ result_.reset(results);
+ if (get_id_ < 0) {
+ profile()->GetDownloadManager()->Search(
+ query_, &error_, NULL/*c++ results*/, results);
Aaron Boodman 2011/10/12 08:26:08 We avoid /* ... */ style comments in Chrome. If yo
benjhayden 2011/10/13 14:07:34 Done.
+ } else {
+ DownloadItem* item = profile()->GetDownloadManager()->GetDownloadItem(
+ get_id_);
+ if (item != NULL)
+ results->Append(DownloadItemToJSON(item));
+ }
}
DownloadsPauseFunction::DownloadsPauseFunction()
@@ -382,35 +441,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_error()));
- // 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),

Powered by Google App Engine
This is Rietveld 408576698