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> |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 #include "content/browser/download/download_item.h" | 33 #include "content/browser/download/download_item.h" |
| 34 #include "content/browser/download/download_types.h" | 34 #include "content/browser/download/download_types.h" |
| 35 #include "content/browser/renderer_host/render_process_host.h" | 35 #include "content/browser/renderer_host/render_process_host.h" |
| 36 #include "content/browser/renderer_host/render_view_host.h" | 36 #include "content/browser/renderer_host/render_view_host.h" |
| 37 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 37 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 38 #include "net/http/http_util.h" | 38 #include "net/http/http_util.h" |
| 39 #include "net/url_request/url_request.h" | 39 #include "net/url_request/url_request.h" |
| 40 | 40 |
| 41 namespace constants = extension_downloads_api_constants; | 41 namespace constants = extension_downloads_api_constants; |
| 42 | 42 |
| 43 namespace { | |
| 44 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { | |
| 45 base::DictionaryValue* json = new base::DictionaryValue(); | |
| 46 json->SetInteger(constants::kIdKey, item->id()); | |
| 47 json->SetString(constants::kUrlKey, item->original_url().spec()); | |
| 48 json->SetString(constants::kFilenameKey, | |
| 49 item->full_path().LossyDisplayName()); | |
| 50 json->SetString(constants::kDangerKey, | |
| 51 constants::DangerString(item->GetDangerType())); | |
| 52 json->SetBoolean(constants::kDangerAcceptedKey, | |
| 53 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); | |
| 54 json->SetString(constants::kStateKey, | |
| 55 constants::StateString(item->state())); | |
| 56 json->SetBoolean(constants::kPausedKey, item->is_paused()); | |
| 57 json->SetString(constants::kMimeKey, item->mime_type()); | |
| 58 json->SetInteger(constants::kStartTimeKey, | |
| 59 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); | |
| 60 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); | |
| 61 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); | |
| 62 if (item->state() == DownloadItem::INTERRUPTED) | |
| 63 json->SetInteger(constants::kErrorKey, | |
| 64 static_cast<int>(item->last_error())); | |
| 65 // TODO(benjhayden): Implement endTime and fileSize. | |
| 66 // json->SetInteger(constants::kEndTimeKey, -1); | |
| 67 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | |
| 68 return json; | |
| 69 } | |
| 70 } // anonymous namespace | |
| 71 | |
| 43 bool DownloadsFunctionInterface::RunImplImpl( | 72 bool DownloadsFunctionInterface::RunImplImpl( |
| 44 DownloadsFunctionInterface* pimpl) { | 73 DownloadsFunctionInterface* pimpl) { |
| 45 CHECK(pimpl); | 74 CHECK(pimpl); |
| 46 if (!pimpl->ParseArgs()) return false; | 75 if (!pimpl->ParseArgs()) return false; |
| 47 UMA_HISTOGRAM_ENUMERATION( | 76 UMA_HISTOGRAM_ENUMERATION( |
| 48 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); | 77 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); |
| 49 pimpl->RunInternal(); | 78 pimpl->RunInternal(); |
| 50 return true; | 79 return true; |
| 51 } | 80 } |
| 52 | 81 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 VLOG(1) << __FUNCTION__; | 243 VLOG(1) << __FUNCTION__; |
| 215 if (dl_id >= 0) { | 244 if (dl_id >= 0) { |
| 216 result_.reset(base::Value::CreateIntegerValue(dl_id)); | 245 result_.reset(base::Value::CreateIntegerValue(dl_id)); |
| 217 } else { | 246 } else { |
| 218 error_ = net::ErrorToString(error); | 247 error_ = net::ErrorToString(error); |
| 219 } | 248 } |
| 220 SendResponse(error_.empty()); | 249 SendResponse(error_.empty()); |
| 221 } | 250 } |
| 222 | 251 |
| 223 DownloadsSearchFunction::DownloadsSearchFunction() | 252 DownloadsSearchFunction::DownloadsSearchFunction() |
| 224 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH) { | 253 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), |
| 254 get_id_(-1) { | |
| 225 } | 255 } |
| 226 | 256 |
| 227 DownloadsSearchFunction::~DownloadsSearchFunction() {} | 257 DownloadsSearchFunction::~DownloadsSearchFunction() {} |
| 228 | 258 |
| 229 bool DownloadsSearchFunction::ParseArgs() { | 259 bool DownloadsSearchFunction::ParseArgs() { |
| 230 base::DictionaryValue* query_json = NULL; | 260 base::DictionaryValue* query_json = NULL; |
| 231 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); | 261 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); |
| 232 error_ = constants::kNotImplemented; | 262 if (query_json->HasKey(constants::kIdKey)) { |
| 233 return false; | 263 EXTENSION_FUNCTION_VALIDATE(query_json->GetInteger( |
| 264 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
| |
| 265 } else { | |
| 266 #define CppInteger int | |
| 267 #define CppString std::string | |
| 268 #define CppBoolean bool | |
| 269 #define FIELD(field_name, field_type) \ | |
| 270 if (query_json->HasKey(#field_name)) { \ | |
| 271 Cpp ## field_type value; \ | |
| 272 EXTENSION_FUNCTION_VALIDATE(query_json->Get ## field_type( \ | |
| 273 #field_name, &value)); \ | |
| 274 query_.field_name(value); \ | |
| 275 } | |
| 276 #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.
| |
| 277 #undef FIELD | |
| 278 #undef CppInteger | |
| 279 #undef CppString | |
| 280 #undef CppBoolean | |
| 281 } | |
| 282 return true; | |
| 234 } | 283 } |
| 235 | 284 |
| 236 void DownloadsSearchFunction::RunInternal() { | 285 void DownloadsSearchFunction::RunInternal() { |
| 237 NOTIMPLEMENTED(); | 286 base::ListValue* results = new base::ListValue(); |
| 287 result_.reset(results); | |
| 288 if (get_id_ < 0) { | |
| 289 profile()->GetDownloadManager()->Search( | |
| 290 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.
| |
| 291 } else { | |
| 292 DownloadItem* item = profile()->GetDownloadManager()->GetDownloadItem( | |
| 293 get_id_); | |
| 294 if (item != NULL) | |
| 295 results->Append(DownloadItemToJSON(item)); | |
| 296 } | |
| 238 } | 297 } |
| 239 | 298 |
| 240 DownloadsPauseFunction::DownloadsPauseFunction() | 299 DownloadsPauseFunction::DownloadsPauseFunction() |
| 241 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) { | 300 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE) { |
| 242 } | 301 } |
| 243 | 302 |
| 244 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 303 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
| 245 | 304 |
| 246 bool DownloadsPauseFunction::ParseArgs() { | 305 bool DownloadsPauseFunction::ParseArgs() { |
| 247 int dl_id = 0; | 306 int dl_id = 0; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 434 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 376 VLOG(1) << __FUNCTION__ << " " << dl_id; | 435 VLOG(1) << __FUNCTION__ << " " << dl_id; |
| 377 error_ = constants::kNotImplemented; | 436 error_ = constants::kNotImplemented; |
| 378 return false; | 437 return false; |
| 379 } | 438 } |
| 380 | 439 |
| 381 void DownloadsDragFunction::RunInternal() { | 440 void DownloadsDragFunction::RunInternal() { |
| 382 NOTIMPLEMENTED(); | 441 NOTIMPLEMENTED(); |
| 383 } | 442 } |
| 384 | 443 |
| 385 namespace { | |
| 386 base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) { | |
| 387 base::DictionaryValue* json = new base::DictionaryValue(); | |
| 388 json->SetInteger(constants::kIdKey, item->id()); | |
| 389 json->SetString(constants::kUrlKey, item->original_url().spec()); | |
| 390 json->SetString(constants::kFilenameKey, | |
| 391 item->full_path().LossyDisplayName()); | |
| 392 json->SetString(constants::kDangerKey, | |
| 393 constants::DangerString(item->GetDangerType())); | |
| 394 json->SetBoolean(constants::kDangerAcceptedKey, | |
| 395 item->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED); | |
| 396 json->SetString(constants::kStateKey, | |
| 397 constants::StateString(item->state())); | |
| 398 json->SetBoolean(constants::kPausedKey, item->is_paused()); | |
| 399 json->SetString(constants::kMimeKey, item->mime_type()); | |
| 400 json->SetInteger(constants::kStartTimeKey, | |
| 401 (item->start_time() - base::Time::UnixEpoch()).InMilliseconds()); | |
| 402 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); | |
| 403 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); | |
| 404 if (item->state() == DownloadItem::INTERRUPTED) | |
| 405 json->SetInteger(constants::kErrorKey, | |
| 406 static_cast<int>(item->last_error())); | |
| 407 // TODO(benjhayden): Implement endTime and fileSize. | |
| 408 // json->SetInteger(constants::kEndTimeKey, -1); | |
| 409 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | |
| 410 return json; | |
| 411 } | |
| 412 } // anonymous namespace | |
| 413 | |
| 414 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 444 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( |
| 415 Profile* profile) | 445 Profile* profile) |
| 416 : profile_(profile), | 446 : profile_(profile), |
| 417 manager_(profile ? profile->GetDownloadManager() : NULL) { | 447 manager_(profile ? profile->GetDownloadManager() : NULL) { |
| 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 419 DCHECK(profile_); | 449 DCHECK(profile_); |
| 420 DCHECK(manager_); | 450 DCHECK(manager_); |
| 421 manager_->AddObserver(this); | 451 manager_->AddObserver(this); |
| 422 } | 452 } |
| 423 | 453 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 480 ListValue args; | 510 ListValue args; |
| 481 args.Append(arg); | 511 args.Append(arg); |
| 482 std::string json_args; | 512 std::string json_args; |
| 483 base::JSONWriter::Write(&args, false, &json_args); | 513 base::JSONWriter::Write(&args, false, &json_args); |
| 484 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 514 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 485 event_name, | 515 event_name, |
| 486 json_args, | 516 json_args, |
| 487 profile_, | 517 profile_, |
| 488 GURL()); | 518 GURL()); |
| 489 } | 519 } |
| OLD | NEW |