| 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/download/download_extension_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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 28 #include "chrome/browser/download/download_file_icon_extractor.h" | 28 #include "chrome/browser/download/download_file_icon_extractor.h" |
| 29 #include "chrome/browser/download/download_query.h" | 29 #include "chrome/browser/download/download_query.h" |
| 30 #include "chrome/browser/download/download_service.h" | 30 #include "chrome/browser/download/download_service.h" |
| 31 #include "chrome/browser/download/download_service_factory.h" | 31 #include "chrome/browser/download/download_service_factory.h" |
| 32 #include "chrome/browser/download/download_util.h" | 32 #include "chrome/browser/download/download_util.h" |
| 33 #include "chrome/browser/extensions/extension_event_names.h" | 33 #include "chrome/browser/extensions/extension_event_names.h" |
| 34 #include "chrome/browser/extensions/extension_event_router.h" | 34 #include "chrome/browser/extensions/extension_event_router.h" |
| 35 #include "chrome/browser/icon_loader.h" | 35 #include "chrome/browser/icon_loader.h" |
| 36 #include "chrome/browser/icon_manager.h" | 36 #include "chrome/browser/icon_manager.h" |
| 37 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 37 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 38 #include "chrome/browser/ui/browser_list.h" | 38 #include "chrome/browser/ui/browser.h" |
| 39 #include "chrome/browser/ui/webui/web_ui_util.h" | 39 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 40 #include "chrome/common/extensions/api/downloads.h" |
| 40 #include "content/public/browser/download_interrupt_reasons.h" | 41 #include "content/public/browser/download_interrupt_reasons.h" |
| 41 #include "content/public/browser/download_item.h" | 42 #include "content/public/browser/download_item.h" |
| 42 #include "content/public/browser/download_save_info.h" | 43 #include "content/public/browser/download_save_info.h" |
| 44 #include "content/public/browser/download_url_parameters.h" |
| 43 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
| 44 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
| 45 #include "content/public/browser/resource_dispatcher_host.h" | 47 #include "content/public/browser/resource_dispatcher_host.h" |
| 46 #include "net/base/load_flags.h" | 48 #include "net/base/load_flags.h" |
| 47 #include "net/http/http_util.h" | 49 #include "net/http/http_util.h" |
| 48 #include "net/url_request/url_request.h" | 50 #include "net/url_request/url_request.h" |
| 49 | 51 |
| 50 using content::BrowserContext; | 52 using content::BrowserContext; |
| 51 using content::BrowserThread; | 53 using content::BrowserThread; |
| 52 using content::DownloadId; | 54 using content::DownloadId; |
| 53 using content::DownloadItem; | 55 using content::DownloadItem; |
| 54 using content::DownloadManager; | 56 using content::DownloadManager; |
| 57 using extensions::api::downloads::HeaderNameValuePair; |
| 55 | 58 |
| 56 namespace download_extension_errors { | 59 namespace download_extension_errors { |
| 57 | 60 |
| 58 // Error messages | 61 // Error messages |
| 59 const char kGenericError[] = "I'm afraid I can't do that."; | 62 const char kGenericError[] = "I'm afraid I can't do that."; |
| 60 const char kIconNotFoundError[] = "Icon not found."; | 63 const char kIconNotFoundError[] = "Icon not found."; |
| 61 const char kInvalidDangerTypeError[] = "Invalid danger type"; | 64 const char kInvalidDangerTypeError[] = "Invalid danger type"; |
| 62 const char kInvalidFilterError[] = "Invalid query filter"; | 65 const char kInvalidFilterError[] = "Invalid query filter"; |
| 63 const char kInvalidOperationError[] = "Invalid operation."; | 66 const char kInvalidOperationError[] = "Invalid operation."; |
| 64 const char kInvalidOrderByError[] = "Invalid orderBy field"; | 67 const char kInvalidOrderByError[] = "Invalid orderBy field"; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 const char kStateInProgress[] = "in_progress"; | 113 const char kStateInProgress[] = "in_progress"; |
| 111 const char kStateInterrupted[] = "interrupted"; | 114 const char kStateInterrupted[] = "interrupted"; |
| 112 const char kStateKey[] = "state"; | 115 const char kStateKey[] = "state"; |
| 113 const char kTotalBytesKey[] = "totalBytes"; | 116 const char kTotalBytesKey[] = "totalBytes"; |
| 114 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; | 117 const char kTotalBytesGreaterKey[] = "totalBytesGreater"; |
| 115 const char kTotalBytesLessKey[] = "totalBytesLess"; | 118 const char kTotalBytesLessKey[] = "totalBytesLess"; |
| 116 const char kUrlKey[] = "url"; | 119 const char kUrlKey[] = "url"; |
| 117 const char kUrlRegexKey[] = "urlRegex"; | 120 const char kUrlRegexKey[] = "urlRegex"; |
| 118 | 121 |
| 119 // Note: Any change to the danger type strings, should be accompanied by a | 122 // Note: Any change to the danger type strings, should be accompanied by a |
| 120 // corresponding change to {experimental.}downloads.json. | 123 // corresponding change to downloads.idl. |
| 121 const char* kDangerStrings[] = { | 124 const char* kDangerStrings[] = { |
| 122 kDangerSafe, | 125 kDangerSafe, |
| 123 kDangerFile, | 126 kDangerFile, |
| 124 kDangerUrl, | 127 kDangerUrl, |
| 125 kDangerContent, | 128 kDangerContent, |
| 126 kDangerSafe, | 129 kDangerSafe, |
| 127 kDangerUncommon, | 130 kDangerUncommon, |
| 128 }; | 131 }; |
| 129 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, | 132 COMPILE_ASSERT(arraysize(kDangerStrings) == content::DOWNLOAD_DANGER_TYPE_MAX, |
| 130 download_danger_type_enum_changed); | 133 download_danger_type_enum_changed); |
| 131 | 134 |
| 132 // Note: Any change to the state strings, should be accompanied by a | 135 // Note: Any change to the state strings, should be accompanied by a |
| 133 // corresponding change to {experimental.}downloads.json. | 136 // corresponding change to downloads.idl. |
| 134 const char* kStateStrings[] = { | 137 const char* kStateStrings[] = { |
| 135 kStateInProgress, | 138 kStateInProgress, |
| 136 kStateComplete, | 139 kStateComplete, |
| 137 kStateInterrupted, | 140 kStateInterrupted, |
| 138 NULL, | 141 NULL, |
| 139 kStateInterrupted, | 142 kStateInterrupted, |
| 140 }; | 143 }; |
| 141 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, | 144 COMPILE_ASSERT(arraysize(kStateStrings) == DownloadItem::MAX_DOWNLOAD_STATE, |
| 142 download_item_state_enum_changed); | 145 download_item_state_enum_changed); |
| 143 | 146 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 bool include_incognito, | 318 bool include_incognito, |
| 316 DownloadManager** manager, DownloadManager** incognito_manager) { | 319 DownloadManager** manager, DownloadManager** incognito_manager) { |
| 317 *manager = BrowserContext::GetDownloadManager(profile); | 320 *manager = BrowserContext::GetDownloadManager(profile); |
| 318 *incognito_manager = NULL; | 321 *incognito_manager = NULL; |
| 319 if (include_incognito && profile->HasOffTheRecordProfile()) { | 322 if (include_incognito && profile->HasOffTheRecordProfile()) { |
| 320 *incognito_manager = BrowserContext::GetDownloadManager( | 323 *incognito_manager = BrowserContext::GetDownloadManager( |
| 321 profile->GetOffTheRecordProfile()); | 324 profile->GetOffTheRecordProfile()); |
| 322 } | 325 } |
| 323 } | 326 } |
| 324 | 327 |
| 325 DownloadItem* GetActiveItemInternal( | 328 DownloadItem* GetActiveItem( |
| 326 Profile* profile, | 329 Profile* profile, |
| 327 bool include_incognito, | 330 bool include_incognito, |
| 328 int id) { | 331 int id) { |
| 329 DownloadManager* manager = NULL; | 332 DownloadManager* manager = NULL; |
| 330 DownloadManager* incognito_manager = NULL; | 333 DownloadManager* incognito_manager = NULL; |
| 331 GetManagers(profile, include_incognito, &manager, &incognito_manager); | 334 GetManagers(profile, include_incognito, &manager, &incognito_manager); |
| 332 DownloadItem* download_item = manager->GetActiveDownloadItem(id); | 335 DownloadItem* download_item = manager->GetActiveDownloadItem(id); |
| 333 if (!download_item && incognito_manager) | 336 if (!download_item && incognito_manager) |
| 334 download_item = incognito_manager->GetActiveDownloadItem(id); | 337 download_item = incognito_manager->GetActiveDownloadItem(id); |
| 335 return download_item; | 338 return download_item; |
| 336 } | 339 } |
| 337 | 340 |
| 341 enum DownloadsFunctionName { |
| 342 DOWNLOADS_FUNCTION_DOWNLOAD = 0, |
| 343 DOWNLOADS_FUNCTION_SEARCH = 1, |
| 344 DOWNLOADS_FUNCTION_PAUSE = 2, |
| 345 DOWNLOADS_FUNCTION_RESUME = 3, |
| 346 DOWNLOADS_FUNCTION_CANCEL = 4, |
| 347 DOWNLOADS_FUNCTION_ERASE = 5, |
| 348 DOWNLOADS_FUNCTION_SET_DESTINATION = 6, |
| 349 DOWNLOADS_FUNCTION_ACCEPT_DANGER = 7, |
| 350 DOWNLOADS_FUNCTION_SHOW = 8, |
| 351 DOWNLOADS_FUNCTION_DRAG = 9, |
| 352 DOWNLOADS_FUNCTION_GET_FILE_ICON = 10, |
| 353 DOWNLOADS_FUNCTION_OPEN = 11, |
| 354 // Insert new values here, not at the beginning. |
| 355 DOWNLOADS_FUNCTION_LAST |
| 356 }; |
| 357 |
| 358 void RecordApiFunctions(DownloadsFunctionName function) { |
| 359 UMA_HISTOGRAM_ENUMERATION("Download.ApiFunctions", |
| 360 function, |
| 361 DOWNLOADS_FUNCTION_LAST); |
| 362 } |
| 363 |
| 364 void CompileDownloadQueryOrderBy( |
| 365 const std::string& order_by_str, std::string* error, DownloadQuery* query) { |
| 366 static base::LazyInstance<SortTypeMap> sorter_types = |
| 367 LAZY_INSTANCE_INITIALIZER; |
| 368 if (sorter_types.Get().size() == 0) |
| 369 InitSortTypeMap(sorter_types.Get()); |
| 370 |
| 371 std::vector<std::string> order_by_strs; |
| 372 base::SplitString(order_by_str, ' ', &order_by_strs); |
| 373 for (std::vector<std::string>::const_iterator iter = order_by_strs.begin(); |
| 374 iter != order_by_strs.end(); ++iter) { |
| 375 std::string term_str = *iter; |
| 376 if (term_str.empty()) |
| 377 continue; |
| 378 DownloadQuery::SortDirection direction = DownloadQuery::ASCENDING; |
| 379 if (term_str[0] == '-') { |
| 380 direction = DownloadQuery::DESCENDING; |
| 381 term_str = term_str.substr(1); |
| 382 } |
| 383 SortTypeMap::const_iterator sorter_type = |
| 384 sorter_types.Get().find(term_str); |
| 385 if (sorter_type == sorter_types.Get().end()) { |
| 386 *error = download_extension_errors::kInvalidOrderByError; |
| 387 return; |
| 388 } |
| 389 query->AddSorter(sorter_type->second, direction); |
| 390 } |
| 391 } |
| 392 |
| 393 void RunDownloadQuery( |
| 394 const extensions::api::downloads::DownloadQuery& query_in, |
| 395 Profile* profile, |
| 396 bool include_incognito, |
| 397 std::string* error, |
| 398 DownloadQuery::DownloadVector* results) { |
| 399 static base::LazyInstance<FilterTypeMap> filter_types = |
| 400 LAZY_INSTANCE_INITIALIZER; |
| 401 if (filter_types.Get().size() == 0) |
| 402 InitFilterTypeMap(filter_types.Get()); |
| 403 |
| 404 DownloadQuery query_out; |
| 405 |
| 406 if (query_in.limit.get()) { |
| 407 if (*query_in.limit.get() < 0) { |
| 408 *error = download_extension_errors::kInvalidQueryLimit; |
| 409 return; |
| 410 } |
| 411 query_out.Limit(*query_in.limit.get()); |
| 412 } |
| 413 if (query_in.state.get()) { |
| 414 DownloadItem::DownloadState state = StateEnumFromString( |
| 415 *query_in.state.get()); |
| 416 if (state == DownloadItem::MAX_DOWNLOAD_STATE) { |
| 417 *error = download_extension_errors::kInvalidStateError; |
| 418 return; |
| 419 } |
| 420 query_out.AddFilter(state); |
| 421 } |
| 422 if (query_in.danger.get()) { |
| 423 content::DownloadDangerType danger_type = |
| 424 DangerEnumFromString(*query_in.danger.get()); |
| 425 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { |
| 426 *error = download_extension_errors::kInvalidDangerTypeError; |
| 427 return; |
| 428 } |
| 429 query_out.AddFilter(danger_type); |
| 430 } |
| 431 if (query_in.order_by.get()) { |
| 432 CompileDownloadQueryOrderBy(*query_in.order_by.get(), error, &query_out); |
| 433 if (!error->empty()) |
| 434 return; |
| 435 } |
| 436 |
| 437 scoped_ptr<base::DictionaryValue> query_in_value(query_in.ToValue().Pass()); |
| 438 for (base::DictionaryValue::Iterator query_json_field(*query_in_value.get()); |
| 439 query_json_field.HasNext(); query_json_field.Advance()) { |
| 440 FilterTypeMap::const_iterator filter_type = |
| 441 filter_types.Get().find(query_json_field.key()); |
| 442 if (filter_type != filter_types.Get().end()) { |
| 443 if (!query_out.AddFilter(filter_type->second, query_json_field.value())) { |
| 444 *error = download_extension_errors::kInvalidFilterError; |
| 445 return; |
| 446 } |
| 447 } |
| 448 } |
| 449 |
| 450 DownloadManager* manager = NULL; |
| 451 DownloadManager* incognito_manager = NULL; |
| 452 GetManagers(profile, include_incognito, &manager, &incognito_manager); |
| 453 DownloadQuery::DownloadVector all_items; |
| 454 if (query_in.id.get()) { |
| 455 DownloadItem* item = manager->GetDownloadItem(*query_in.id.get()); |
| 456 if (!item && incognito_manager) |
| 457 item = incognito_manager->GetDownloadItem(*query_in.id.get()); |
| 458 if (item) |
| 459 all_items.push_back(item); |
| 460 } else { |
| 461 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); |
| 462 if (incognito_manager) |
| 463 incognito_manager->GetAllDownloads( |
| 464 FilePath(FILE_PATH_LITERAL("")), &all_items); |
| 465 } |
| 466 query_out.Search(all_items.begin(), all_items.end(), results); |
| 467 } |
| 468 |
| 338 } // namespace | 469 } // namespace |
| 339 | 470 |
| 340 bool DownloadsFunctionInterface::RunImplImpl( | 471 DownloadsDownloadFunction::DownloadsDownloadFunction() {} |
| 341 DownloadsFunctionInterface* pimpl) { | |
| 342 CHECK(pimpl); | |
| 343 if (!pimpl->ParseArgs()) return false; | |
| 344 UMA_HISTOGRAM_ENUMERATION( | |
| 345 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); | |
| 346 return pimpl->RunInternal(); | |
| 347 } | |
| 348 | |
| 349 SyncDownloadsFunction::SyncDownloadsFunction( | |
| 350 DownloadsFunctionInterface::DownloadsFunctionName function) | |
| 351 : function_(function) { | |
| 352 } | |
| 353 | |
| 354 SyncDownloadsFunction::~SyncDownloadsFunction() {} | |
| 355 | |
| 356 bool SyncDownloadsFunction::RunImpl() { | |
| 357 return DownloadsFunctionInterface::RunImplImpl(this); | |
| 358 } | |
| 359 | |
| 360 DownloadsFunctionInterface::DownloadsFunctionName | |
| 361 SyncDownloadsFunction::function() const { | |
| 362 return function_; | |
| 363 } | |
| 364 | |
| 365 DownloadItem* SyncDownloadsFunction::GetActiveItem(int download_id) { | |
| 366 return GetActiveItemInternal(profile(), include_incognito(), download_id); | |
| 367 } | |
| 368 | |
| 369 AsyncDownloadsFunction::AsyncDownloadsFunction( | |
| 370 DownloadsFunctionInterface::DownloadsFunctionName function) | |
| 371 : function_(function) { | |
| 372 } | |
| 373 | |
| 374 AsyncDownloadsFunction::~AsyncDownloadsFunction() {} | |
| 375 | |
| 376 bool AsyncDownloadsFunction::RunImpl() { | |
| 377 return DownloadsFunctionInterface::RunImplImpl(this); | |
| 378 } | |
| 379 | |
| 380 DownloadsFunctionInterface::DownloadsFunctionName | |
| 381 AsyncDownloadsFunction::function() const { | |
| 382 return function_; | |
| 383 } | |
| 384 | |
| 385 DownloadItem* AsyncDownloadsFunction::GetActiveItem(int download_id) { | |
| 386 return GetActiveItemInternal(profile(), include_incognito(), download_id); | |
| 387 } | |
| 388 | |
| 389 DownloadsDownloadFunction::DownloadsDownloadFunction() | |
| 390 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) { | |
| 391 } | |
| 392 | |
| 393 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} | 472 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} |
| 394 | 473 |
| 395 DownloadsDownloadFunction::IOData::IOData() | 474 bool DownloadsDownloadFunction::RunImpl() { |
| 396 : save_as(false), | 475 scoped_ptr<extensions::api::downloads::Download::Params> params( |
| 397 extra_headers(NULL), | 476 extensions::api::downloads::Download::Params::Create(*args_)); |
| 398 method("GET"), | 477 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 399 rdh(NULL), | 478 RecordApiFunctions(DOWNLOADS_FUNCTION_DOWNLOAD); |
| 400 resource_context(NULL), | 479 const extensions::api::downloads::DownloadOptions& options = params->options; |
| 401 render_process_host_id(0), | 480 |
| 402 render_view_host_routing_id(0) { | 481 GURL url(options.url); |
| 403 } | 482 if (!url.is_valid() || |
| 404 | 483 (!url.SchemeIs("data") && |
| 405 DownloadsDownloadFunction::IOData::~IOData() {} | 484 url.GetOrigin() != GetExtension()->url().GetOrigin() && |
| 406 | 485 !GetExtension()->HasHostPermission(url))) { |
| 407 bool DownloadsDownloadFunction::ParseArgs() { | |
| 408 base::DictionaryValue* options = NULL; | |
| 409 std::string url; | |
| 410 iodata_.reset(new IOData()); | |
| 411 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | |
| 412 EXTENSION_FUNCTION_VALIDATE(options->GetString(kUrlKey, &url)); | |
| 413 iodata_->url = GURL(url); | |
| 414 if (!iodata_->url.is_valid()) { | |
| 415 error_ = download_extension_errors::kInvalidURLError; | 486 error_ = download_extension_errors::kInvalidURLError; |
| 416 return false; | 487 return false; |
| 417 } | 488 } |
| 418 | 489 |
| 419 if (!iodata_->url.SchemeIs("data") && | 490 content::DownloadSaveInfo save_info; |
| 420 iodata_->url.GetOrigin() != GetExtension()->url().GetOrigin() && | 491 if (options.filename.get()) { |
| 421 !GetExtension()->HasHostPermission(iodata_->url)) { | 492 // TODO(benjhayden): Make json_schema_compiler generate string16s instead of |
| 422 error_ = download_extension_errors::kInvalidURLError; | 493 // std::strings. Can't get filename16 from options.ToValue() because that |
| 423 return false; | 494 // converts it from std::string. |
| 424 } | 495 base::DictionaryValue* options_value = NULL; |
| 425 | 496 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options_value)); |
| 426 if (options->HasKey(kFilenameKey)) { | 497 string16 filename16; |
| 427 EXTENSION_FUNCTION_VALIDATE(options->GetString( | 498 EXTENSION_FUNCTION_VALIDATE(options_value->GetString( |
| 428 kFilenameKey, &iodata_->filename)); | 499 kFilenameKey, &filename16)); |
| 429 if (!ValidateFilename(iodata_->filename)) { | 500 if (!ValidateFilename(filename16)) { |
| 430 error_ = download_extension_errors::kGenericError; | 501 error_ = download_extension_errors::kGenericError; |
| 431 return false; | 502 return false; |
| 432 } | 503 } |
| 433 } | 504 // TODO(benjhayden) Ensure that this filename is interpreted as a path |
| 434 | 505 // relative to the default downloads directory without allowing '..'. |
| 435 if (options->HasKey(kSaveAsKey)) { | 506 save_info.suggested_name = filename16; |
| 436 EXTENSION_FUNCTION_VALIDATE(options->GetBoolean( | 507 } |
| 437 kSaveAsKey, &iodata_->save_as)); | 508 |
| 438 } | 509 if (options.save_as.get()) |
| 439 | 510 save_info.prompt_for_save_location = *options.save_as.get(); |
| 440 if (options->HasKey(kMethodKey)) { | 511 |
| 441 EXTENSION_FUNCTION_VALIDATE(options->GetString( | 512 Profile* current_profile = profile(); |
| 442 kMethodKey, &iodata_->method)); | 513 if (include_incognito() && profile()->HasOffTheRecordProfile()) |
| 443 } | 514 current_profile = profile()->GetOffTheRecordProfile(); |
| 444 | 515 // TODO XXX test incognito download() |
| 445 // It's ok to use a pointer to extra_headers without DeepCopy()ing because | 516 |
| 446 // |args_| (which owns *extra_headers) is guaranteed to live as long as | 517 scoped_ptr<content::DownloadUrlParameters> download_params( |
| 447 // |this|. | 518 new content::DownloadUrlParameters( |
| 448 if (options->HasKey(kHeadersKey)) { | 519 url, |
| 449 EXTENSION_FUNCTION_VALIDATE(options->GetList( | 520 render_view_host()->GetProcess()->GetID(), |
| 450 kHeadersKey, &iodata_->extra_headers)); | 521 render_view_host()->GetRoutingID(), |
| 451 } | 522 current_profile->GetResourceContext(), |
| 452 | 523 save_info)); |
| 453 if (options->HasKey(kBodyKey)) { | 524 |
| 454 EXTENSION_FUNCTION_VALIDATE(options->GetString( | 525 if (options.headers.get()) { |
| 455 kBodyKey, &iodata_->post_body)); | 526 for (std::vector<linked_ptr<HeaderNameValuePair> >::const_iterator iter = |
| 456 } | 527 options.headers->begin(); |
| 457 | 528 iter != options.headers->end(); |
| 458 if (iodata_->extra_headers != NULL) { | 529 ++iter) { |
| 459 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { | 530 const HeaderNameValuePair& name_value = **iter; |
| 460 base::DictionaryValue* header = NULL; | 531 if (!net::HttpUtil::IsSafeHeader(name_value.name)) { |
| 461 std::string name; | |
| 462 EXTENSION_FUNCTION_VALIDATE(iodata_->extra_headers->GetDictionary( | |
| 463 index, &header)); | |
| 464 EXTENSION_FUNCTION_VALIDATE(header->GetString( | |
| 465 kHeaderNameKey, &name)); | |
| 466 if (header->HasKey(kHeaderBinaryValueKey)) { | |
| 467 base::ListValue* binary_value = NULL; | |
| 468 EXTENSION_FUNCTION_VALIDATE(header->GetList( | |
| 469 kHeaderBinaryValueKey, &binary_value)); | |
| 470 for (size_t char_i = 0; char_i < binary_value->GetSize(); ++char_i) { | |
| 471 int char_value = 0; | |
| 472 EXTENSION_FUNCTION_VALIDATE(binary_value->GetInteger( | |
| 473 char_i, &char_value)); | |
| 474 } | |
| 475 } else if (header->HasKey(kHeaderValueKey)) { | |
| 476 std::string value; | |
| 477 EXTENSION_FUNCTION_VALIDATE(header->GetString( | |
| 478 kHeaderValueKey, &value)); | |
| 479 } | |
| 480 if (!net::HttpUtil::IsSafeHeader(name)) { | |
| 481 error_ = download_extension_errors::kGenericError; | 532 error_ = download_extension_errors::kGenericError; |
| 482 return false; | 533 return false; |
| 483 } | 534 } |
| 484 } | 535 std::string value; |
| 485 } | 536 if (name_value.binary_value.get()) { |
| 486 iodata_->rdh = content::ResourceDispatcherHost::Get(); | 537 for (size_t char_i = 0; |
| 487 iodata_->resource_context = profile()->GetResourceContext(); | 538 char_i < name_value.binary_value->size(); |
| 488 iodata_->render_process_host_id = render_view_host()->GetProcess()->GetID(); | 539 ++char_i) { |
| 489 iodata_->render_view_host_routing_id = render_view_host()->GetRoutingID(); | 540 value.push_back((*name_value.binary_value.get())[char_i]); |
| 541 } |
| 542 } else if (name_value.value.get()) { |
| 543 value = *name_value.value.get(); |
| 544 } |
| 545 download_params->add_request_header(name_value.name, value); |
| 546 } |
| 547 } |
| 548 |
| 549 if (options.method.get()) |
| 550 download_params->set_method(*options.method.get()); |
| 551 if (options.body.get()) |
| 552 download_params->set_post_body(*options.body.get()); |
| 553 download_params->set_callback(base::Bind( |
| 554 &DownloadsDownloadFunction::OnStarted, this)); |
| 555 // Prevent login prompts for 401/407 responses. |
| 556 download_params->set_load_flags(net::LOAD_DO_NOT_PROMPT_FOR_LOGIN); |
| 557 |
| 558 DownloadManager* manager = BrowserContext::GetDownloadManager(current_profile)
; |
| 559 manager->DownloadUrl(download_params.Pass()); |
| 490 return true; | 560 return true; |
| 491 } | 561 } |
| 492 | 562 |
| 493 bool DownloadsDownloadFunction::RunInternal() { | |
| 494 VLOG(1) << __FUNCTION__ << " " << iodata_->url.spec(); | |
| 495 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | |
| 496 &DownloadsDownloadFunction::BeginDownloadOnIOThread, this))) { | |
| 497 error_ = download_extension_errors::kGenericError; | |
| 498 return false; | |
| 499 } | |
| 500 return true; | |
| 501 } | |
| 502 | |
| 503 void DownloadsDownloadFunction::BeginDownloadOnIOThread() { | |
| 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 505 DVLOG(1) << __FUNCTION__ << " " << iodata_->url.spec(); | |
| 506 content::DownloadSaveInfo save_info; | |
| 507 // TODO(benjhayden) Ensure that this filename is interpreted as a path | |
| 508 // relative to the default downloads directory without allowing '..'. | |
| 509 save_info.suggested_name = iodata_->filename; | |
| 510 save_info.prompt_for_save_location = iodata_->save_as; | |
| 511 | |
| 512 scoped_ptr<net::URLRequest> request(new net::URLRequest(iodata_->url, NULL)); | |
| 513 request->set_method(iodata_->method); | |
| 514 if (iodata_->extra_headers != NULL) { | |
| 515 for (size_t index = 0; index < iodata_->extra_headers->GetSize(); ++index) { | |
| 516 base::DictionaryValue* header = NULL; | |
| 517 std::string name, value; | |
| 518 CHECK(iodata_->extra_headers->GetDictionary(index, &header)); | |
| 519 CHECK(header->GetString(kHeaderNameKey, &name)); | |
| 520 if (header->HasKey(kHeaderBinaryValueKey)) { | |
| 521 base::ListValue* binary_value = NULL; | |
| 522 CHECK(header->GetList(kHeaderBinaryValueKey, &binary_value)); | |
| 523 for (size_t char_i = 0; char_i < binary_value->GetSize(); ++char_i) { | |
| 524 int char_value = 0; | |
| 525 CHECK(binary_value->GetInteger(char_i, &char_value)); | |
| 526 if ((0 <= char_value) && | |
| 527 (char_value <= 0xff)) { | |
| 528 value.push_back(char_value); | |
| 529 } | |
| 530 } | |
| 531 } else if (header->HasKey(kHeaderValueKey)) { | |
| 532 CHECK(header->GetString(kHeaderValueKey, &value)); | |
| 533 } | |
| 534 request->SetExtraRequestHeaderByName(name, value, false/*overwrite*/); | |
| 535 } | |
| 536 } | |
| 537 if (!iodata_->post_body.empty()) { | |
| 538 request->AppendBytesToUpload(iodata_->post_body.data(), | |
| 539 iodata_->post_body.size()); | |
| 540 } | |
| 541 | |
| 542 // Prevent login prompts for 401/407 responses. | |
| 543 request->set_load_flags(request->load_flags() | | |
| 544 net::LOAD_DO_NOT_PROMPT_FOR_LOGIN); | |
| 545 | |
| 546 net::Error error = iodata_->rdh->BeginDownload( | |
| 547 request.Pass(), | |
| 548 false, // is_content_initiated | |
| 549 iodata_->resource_context, | |
| 550 iodata_->render_process_host_id, | |
| 551 iodata_->render_view_host_routing_id, | |
| 552 false, // prefer_cache | |
| 553 save_info, | |
| 554 base::Bind(&DownloadsDownloadFunction::OnStarted, this)); | |
| 555 iodata_.reset(); | |
| 556 | |
| 557 if (error != net::OK) { | |
| 558 BrowserThread::PostTask( | |
| 559 BrowserThread::UI, FROM_HERE, | |
| 560 base::Bind(&DownloadsDownloadFunction::OnStarted, this, | |
| 561 DownloadId::Invalid(), error)); | |
| 562 } | |
| 563 } | |
| 564 | |
| 565 void DownloadsDownloadFunction::OnStarted(DownloadId dl_id, net::Error error) { | 563 void DownloadsDownloadFunction::OnStarted(DownloadId dl_id, net::Error error) { |
| 566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 567 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << error; | 565 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << error; |
| 568 if (dl_id.local() >= 0) { | 566 if (dl_id.local() >= 0) { |
| 569 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); | 567 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); |
| 570 } else { | 568 } else { |
| 571 error_ = net::ErrorToString(error); | 569 error_ = net::ErrorToString(error); |
| 572 } | 570 } |
| 573 SendResponse(error_.empty()); | 571 SendResponse(error_.empty()); |
| 574 } | 572 } |
| 575 | 573 |
| 576 DownloadsSearchFunction::DownloadsSearchFunction() | 574 DownloadsSearchFunction::DownloadsSearchFunction() {} |
| 577 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), | |
| 578 query_(new DownloadQuery()), | |
| 579 get_id_(0), | |
| 580 has_get_id_(false) { | |
| 581 } | |
| 582 | |
| 583 DownloadsSearchFunction::~DownloadsSearchFunction() {} | 575 DownloadsSearchFunction::~DownloadsSearchFunction() {} |
| 584 | 576 |
| 585 bool DownloadsSearchFunction::ParseArgs() { | 577 bool DownloadsSearchFunction::RunImpl() { |
| 586 static base::LazyInstance<FilterTypeMap> filter_types = | 578 scoped_ptr<extensions::api::downloads::Search::Params> params( |
| 587 LAZY_INSTANCE_INITIALIZER; | 579 extensions::api::downloads::Search::Params::Create(*args_)); |
| 588 if (filter_types.Get().size() == 0) | 580 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 589 InitFilterTypeMap(filter_types.Get()); | 581 RecordApiFunctions(DOWNLOADS_FUNCTION_SEARCH); |
| 590 | 582 DownloadQuery::DownloadVector results; |
| 591 base::DictionaryValue* query_json = NULL; | 583 RunDownloadQuery(params->query, profile(), include_incognito(), |
| 592 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); | 584 &error_, &results); |
| 593 for (base::DictionaryValue::Iterator query_json_field(*query_json); | 585 if (!error_.empty()) |
| 594 query_json_field.HasNext(); query_json_field.Advance()) { | 586 return false; |
| 595 FilterTypeMap::const_iterator filter_type = | |
| 596 filter_types.Get().find(query_json_field.key()); | |
| 597 | |
| 598 if (filter_type != filter_types.Get().end()) { | |
| 599 if (!query_->AddFilter(filter_type->second, query_json_field.value())) { | |
| 600 error_ = download_extension_errors::kInvalidFilterError; | |
| 601 return false; | |
| 602 } | |
| 603 } else if (query_json_field.key() == kIdKey) { | |
| 604 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( | |
| 605 &get_id_)); | |
| 606 has_get_id_ = true; | |
| 607 } else if (query_json_field.key() == kOrderByKey) { | |
| 608 if (!ParseOrderBy(query_json_field.value())) | |
| 609 return false; | |
| 610 } else if (query_json_field.key() == kDangerKey) { | |
| 611 std::string danger_str; | |
| 612 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( | |
| 613 &danger_str)); | |
| 614 content::DownloadDangerType danger_type = | |
| 615 DangerEnumFromString(danger_str); | |
| 616 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { | |
| 617 error_ = download_extension_errors::kInvalidDangerTypeError; | |
| 618 return false; | |
| 619 } | |
| 620 query_->AddFilter(danger_type); | |
| 621 } else if (query_json_field.key() == kStateKey) { | |
| 622 std::string state_str; | |
| 623 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( | |
| 624 &state_str)); | |
| 625 DownloadItem::DownloadState state = StateEnumFromString(state_str); | |
| 626 if (state == DownloadItem::MAX_DOWNLOAD_STATE) { | |
| 627 error_ = download_extension_errors::kInvalidStateError; | |
| 628 return false; | |
| 629 } | |
| 630 query_->AddFilter(state); | |
| 631 } else if (query_json_field.key() == kLimitKey) { | |
| 632 int limit = 0; | |
| 633 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( | |
| 634 &limit)); | |
| 635 if (limit < 0) { | |
| 636 error_ = download_extension_errors::kInvalidQueryLimit; | |
| 637 return false; | |
| 638 } | |
| 639 query_->Limit(limit); | |
| 640 } else { | |
| 641 EXTENSION_FUNCTION_VALIDATE(false); | |
| 642 } | |
| 643 } | |
| 644 return true; | |
| 645 } | |
| 646 | |
| 647 bool DownloadsSearchFunction::ParseOrderBy(const base::Value& order_by_value) { | |
| 648 static base::LazyInstance<SortTypeMap> sorter_types = | |
| 649 LAZY_INSTANCE_INITIALIZER; | |
| 650 if (sorter_types.Get().size() == 0) | |
| 651 InitSortTypeMap(sorter_types.Get()); | |
| 652 | |
| 653 std::string order_by_str; | |
| 654 EXTENSION_FUNCTION_VALIDATE(order_by_value.GetAsString(&order_by_str)); | |
| 655 std::vector<std::string> order_by_strs; | |
| 656 base::SplitString(order_by_str, ' ', &order_by_strs); | |
| 657 for (std::vector<std::string>::const_iterator iter = order_by_strs.begin(); | |
| 658 iter != order_by_strs.end(); ++iter) { | |
| 659 std::string term_str = *iter; | |
| 660 if (term_str.empty()) | |
| 661 continue; | |
| 662 DownloadQuery::SortDirection direction = DownloadQuery::ASCENDING; | |
| 663 if (term_str[0] == '-') { | |
| 664 direction = DownloadQuery::DESCENDING; | |
| 665 term_str = term_str.substr(1); | |
| 666 } | |
| 667 SortTypeMap::const_iterator sorter_type = | |
| 668 sorter_types.Get().find(term_str); | |
| 669 if (sorter_type == sorter_types.Get().end()) { | |
| 670 error_ = download_extension_errors::kInvalidOrderByError; | |
| 671 return false; | |
| 672 } | |
| 673 query_->AddSorter(sorter_type->second, direction); | |
| 674 } | |
| 675 return true; | |
| 676 } | |
| 677 | |
| 678 bool DownloadsSearchFunction::RunInternal() { | |
| 679 DownloadManager* manager = NULL; | |
| 680 DownloadManager* incognito_manager = NULL; | |
| 681 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | |
| 682 DownloadQuery::DownloadVector all_items, cpp_results; | |
| 683 if (has_get_id_) { | |
| 684 DownloadItem* item = manager->GetDownloadItem(get_id_); | |
| 685 if (!item && incognito_manager) | |
| 686 item = incognito_manager->GetDownloadItem(get_id_); | |
| 687 if (item) | |
| 688 all_items.push_back(item); | |
| 689 } else { | |
| 690 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); | |
| 691 if (incognito_manager) | |
| 692 incognito_manager->GetAllDownloads( | |
| 693 FilePath(FILE_PATH_LITERAL("")), &all_items); | |
| 694 } | |
| 695 query_->Search(all_items.begin(), all_items.end(), &cpp_results); | |
| 696 base::ListValue* json_results = new base::ListValue(); | 587 base::ListValue* json_results = new base::ListValue(); |
| 697 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); | 588 for (DownloadManager::DownloadVector::const_iterator it = results.begin(); |
| 698 it != cpp_results.end(); ++it) { | 589 it != results.end(); ++it) { |
| 699 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); | 590 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); |
| 700 json_results->Append(item.release()); | 591 json_results->Append(item.release()); |
| 701 } | 592 } |
| 702 result_.reset(json_results); | 593 result_.reset(json_results); |
| 703 return true; | 594 return true; |
| 704 } | 595 } |
| 705 | 596 |
| 706 DownloadsPauseFunction::DownloadsPauseFunction() | 597 DownloadsPauseFunction::DownloadsPauseFunction() {} |
| 707 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE), | |
| 708 download_id_(DownloadId::Invalid().local()) { | |
| 709 } | |
| 710 | |
| 711 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 598 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
| 712 | 599 |
| 713 bool DownloadsPauseFunction::ParseArgs() { | 600 bool DownloadsPauseFunction::RunImpl() { |
| 714 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 601 scoped_ptr<extensions::api::downloads::Pause::Params> params( |
| 715 return true; | 602 extensions::api::downloads::Pause::Params::Create(*args_)); |
| 716 } | 603 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 717 | 604 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); |
| 718 bool DownloadsPauseFunction::RunInternal() { | 605 DownloadItem* download_item = GetActiveItem( |
| 719 DownloadItem* download_item = GetActiveItem(download_id_); | 606 profile(), include_incognito(), params->download_id); |
| 720 if ((download_item == NULL) || !download_item->IsInProgress()) { | 607 if ((download_item == NULL) || !download_item->IsInProgress()) { |
| 721 // This could be due to an invalid download ID, or it could be due to the | 608 // This could be due to an invalid download ID, or it could be due to the |
| 722 // download not being currently active. | 609 // download not being currently active. |
| 723 error_ = download_extension_errors::kInvalidOperationError; | 610 error_ = download_extension_errors::kInvalidOperationError; |
| 724 } else if (!download_item->IsPaused()) { | 611 } else if (!download_item->IsPaused()) { |
| 725 // If download_item->IsPaused() already then we treat it as a success. | 612 // If download_item->IsPaused() already then we treat it as a success. |
| 726 download_item->TogglePause(); | 613 download_item->TogglePause(); |
| 727 } | 614 } |
| 728 return error_.empty(); | 615 return error_.empty(); |
| 729 } | 616 } |
| 730 | 617 |
| 731 DownloadsResumeFunction::DownloadsResumeFunction() | 618 DownloadsResumeFunction::DownloadsResumeFunction() {} |
| 732 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME), | |
| 733 download_id_(DownloadId::Invalid().local()) { | |
| 734 } | |
| 735 | |
| 736 DownloadsResumeFunction::~DownloadsResumeFunction() {} | 619 DownloadsResumeFunction::~DownloadsResumeFunction() {} |
| 737 | 620 |
| 738 bool DownloadsResumeFunction::ParseArgs() { | 621 bool DownloadsResumeFunction::RunImpl() { |
| 739 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 622 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
| 740 return true; | 623 extensions::api::downloads::Resume::Params::Create(*args_)); |
| 741 } | 624 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 742 | 625 RecordApiFunctions(DOWNLOADS_FUNCTION_RESUME); |
| 743 bool DownloadsResumeFunction::RunInternal() { | 626 DownloadItem* download_item = GetActiveItem( |
| 744 DownloadItem* download_item = GetActiveItem(download_id_); | 627 profile(), include_incognito(), params->download_id); |
| 745 if (download_item == NULL) { | 628 if (download_item == NULL) { |
| 746 // This could be due to an invalid download ID, or it could be due to the | 629 // This could be due to an invalid download ID, or it could be due to the |
| 747 // download not being currently active. | 630 // download not being currently active. |
| 748 error_ = download_extension_errors::kInvalidOperationError; | 631 error_ = download_extension_errors::kInvalidOperationError; |
| 749 } else if (download_item->IsPaused()) { | 632 } else if (download_item->IsPaused()) { |
| 750 // If !download_item->IsPaused() already, then we treat it as a success. | 633 // If !download_item->IsPaused() already, then we treat it as a success. |
| 751 download_item->TogglePause(); | 634 download_item->TogglePause(); |
| 752 } | 635 } |
| 753 return error_.empty(); | 636 return error_.empty(); |
| 754 } | 637 } |
| 755 | 638 |
| 756 DownloadsCancelFunction::DownloadsCancelFunction() | 639 DownloadsCancelFunction::DownloadsCancelFunction() {} |
| 757 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL), | |
| 758 download_id_(DownloadId::Invalid().local()) { | |
| 759 } | |
| 760 | |
| 761 DownloadsCancelFunction::~DownloadsCancelFunction() {} | 640 DownloadsCancelFunction::~DownloadsCancelFunction() {} |
| 762 | 641 |
| 763 bool DownloadsCancelFunction::ParseArgs() { | 642 bool DownloadsCancelFunction::RunImpl() { |
| 764 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 643 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
| 765 return true; | 644 extensions::api::downloads::Resume::Params::Create(*args_)); |
| 766 } | 645 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 767 | 646 RecordApiFunctions(DOWNLOADS_FUNCTION_CANCEL); |
| 768 bool DownloadsCancelFunction::RunInternal() { | 647 DownloadItem* download_item = GetActiveItem( |
| 769 DownloadItem* download_item = GetActiveItem(download_id_); | 648 profile(), include_incognito(), params->download_id); |
| 770 if (download_item != NULL) | 649 if (download_item != NULL) |
| 771 download_item->Cancel(true); | 650 download_item->Cancel(true); |
| 772 // |download_item| can be NULL if the download ID was invalid or if the | 651 // |download_item| can be NULL if the download ID was invalid or if the |
| 773 // download is not currently active. Either way, we don't consider it a | 652 // download is not currently active. Either way, we don't consider it a |
| 774 // failure. | 653 // failure. |
| 775 return error_.empty(); | 654 return error_.empty(); |
| 776 } | 655 } |
| 777 | 656 |
| 778 DownloadsEraseFunction::DownloadsEraseFunction() | 657 DownloadsEraseFunction::DownloadsEraseFunction() {} |
| 779 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { | 658 DownloadsEraseFunction::~DownloadsEraseFunction() {} |
| 659 |
| 660 bool DownloadsEraseFunction::RunImpl() { |
| 661 scoped_ptr<extensions::api::downloads::Erase::Params> params( |
| 662 extensions::api::downloads::Erase::Params::Create(*args_)); |
| 663 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 664 RecordApiFunctions(DOWNLOADS_FUNCTION_ERASE); |
| 665 error_ = download_extension_errors::kNotImplementedError; |
| 666 return error_.empty(); |
| 780 } | 667 } |
| 781 | 668 |
| 782 DownloadsEraseFunction::~DownloadsEraseFunction() {} | 669 DownloadsSetDestinationFunction::DownloadsSetDestinationFunction() {} |
| 670 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {} |
| 783 | 671 |
| 784 bool DownloadsEraseFunction::ParseArgs() { | 672 bool DownloadsSetDestinationFunction::RunImpl() { |
| 785 base::DictionaryValue* query_json = NULL; | 673 scoped_ptr<extensions::api::downloads::SetDestination::Params> params( |
| 786 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); | 674 extensions::api::downloads::SetDestination::Params::Create(*args_)); |
| 675 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 676 RecordApiFunctions(DOWNLOADS_FUNCTION_SET_DESTINATION); |
| 787 error_ = download_extension_errors::kNotImplementedError; | 677 error_ = download_extension_errors::kNotImplementedError; |
| 788 return false; | 678 return error_.empty(); |
| 789 } | 679 } |
| 790 | 680 |
| 791 bool DownloadsEraseFunction::RunInternal() { | 681 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction() {} |
| 792 NOTIMPLEMENTED(); | 682 DownloadsAcceptDangerFunction::~DownloadsAcceptDangerFunction() {} |
| 793 return false; | 683 |
| 684 bool DownloadsAcceptDangerFunction::RunImpl() { |
| 685 scoped_ptr<extensions::api::downloads::AcceptDanger::Params> params( |
| 686 extensions::api::downloads::AcceptDanger::Params::Create(*args_)); |
| 687 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 688 RecordApiFunctions(DOWNLOADS_FUNCTION_ACCEPT_DANGER); |
| 689 error_ = download_extension_errors::kNotImplementedError; |
| 690 return error_.empty(); |
| 794 } | 691 } |
| 795 | 692 |
| 796 DownloadsSetDestinationFunction::DownloadsSetDestinationFunction() | 693 DownloadsShowFunction::DownloadsShowFunction() {} |
| 797 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SET_DESTINATION) { | 694 DownloadsShowFunction::~DownloadsShowFunction() {} |
| 695 |
| 696 bool DownloadsShowFunction::RunImpl() { |
| 697 scoped_ptr<extensions::api::downloads::Show::Params> params( |
| 698 extensions::api::downloads::Show::Params::Create(*args_)); |
| 699 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 700 RecordApiFunctions(DOWNLOADS_FUNCTION_SHOW); |
| 701 error_ = download_extension_errors::kNotImplementedError; |
| 702 return error_.empty(); |
| 798 } | 703 } |
| 799 | 704 |
| 800 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {} | 705 DownloadsOpenFunction::DownloadsOpenFunction() {} |
| 706 DownloadsOpenFunction::~DownloadsOpenFunction() {} |
| 801 | 707 |
| 802 bool DownloadsSetDestinationFunction::ParseArgs() { | 708 bool DownloadsOpenFunction::RunImpl() { |
| 803 int dl_id = 0; | 709 scoped_ptr<extensions::api::downloads::Open::Params> params( |
| 804 std::string path; | 710 extensions::api::downloads::Open::Params::Create(*args_)); |
| 805 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 711 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 806 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &path)); | 712 RecordApiFunctions(DOWNLOADS_FUNCTION_OPEN); |
| 807 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << &path; | |
| 808 error_ = download_extension_errors::kNotImplementedError; | 713 error_ = download_extension_errors::kNotImplementedError; |
| 809 return false; | 714 return error_.empty(); |
| 810 } | 715 } |
| 811 | 716 |
| 812 bool DownloadsSetDestinationFunction::RunInternal() { | 717 DownloadsDragFunction::DownloadsDragFunction() {} |
| 813 NOTIMPLEMENTED(); | |
| 814 return false; | |
| 815 } | |
| 816 | |
| 817 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction() | |
| 818 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ACCEPT_DANGER) { | |
| 819 } | |
| 820 | |
| 821 DownloadsAcceptDangerFunction::~DownloadsAcceptDangerFunction() {} | |
| 822 | |
| 823 bool DownloadsAcceptDangerFunction::ParseArgs() { | |
| 824 int dl_id = 0; | |
| 825 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | |
| 826 VLOG(1) << __FUNCTION__ << " " << dl_id; | |
| 827 error_ = download_extension_errors::kNotImplementedError; | |
| 828 return false; | |
| 829 } | |
| 830 | |
| 831 bool DownloadsAcceptDangerFunction::RunInternal() { | |
| 832 NOTIMPLEMENTED(); | |
| 833 return false; | |
| 834 } | |
| 835 | |
| 836 DownloadsShowFunction::DownloadsShowFunction() | |
| 837 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SHOW) { | |
| 838 } | |
| 839 | |
| 840 DownloadsShowFunction::~DownloadsShowFunction() {} | |
| 841 | |
| 842 bool DownloadsShowFunction::ParseArgs() { | |
| 843 int dl_id = 0; | |
| 844 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | |
| 845 VLOG(1) << __FUNCTION__ << " " << dl_id; | |
| 846 error_ = download_extension_errors::kNotImplementedError; | |
| 847 return false; | |
| 848 } | |
| 849 | |
| 850 bool DownloadsShowFunction::RunInternal() { | |
| 851 NOTIMPLEMENTED(); | |
| 852 return false; | |
| 853 } | |
| 854 | |
| 855 DownloadsDragFunction::DownloadsDragFunction() | |
| 856 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DRAG) { | |
| 857 } | |
| 858 | |
| 859 DownloadsDragFunction::~DownloadsDragFunction() {} | 718 DownloadsDragFunction::~DownloadsDragFunction() {} |
| 860 | 719 |
| 861 bool DownloadsDragFunction::ParseArgs() { | 720 bool DownloadsDragFunction::RunImpl() { |
| 862 int dl_id = 0; | 721 scoped_ptr<extensions::api::downloads::Drag::Params> params( |
| 863 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 722 extensions::api::downloads::Drag::Params::Create(*args_)); |
| 864 VLOG(1) << __FUNCTION__ << " " << dl_id; | 723 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 724 RecordApiFunctions(DOWNLOADS_FUNCTION_DRAG); |
| 865 error_ = download_extension_errors::kNotImplementedError; | 725 error_ = download_extension_errors::kNotImplementedError; |
| 866 return false; | 726 return error_.empty(); |
| 867 } | |
| 868 | |
| 869 bool DownloadsDragFunction::RunInternal() { | |
| 870 NOTIMPLEMENTED(); | |
| 871 return false; | |
| 872 } | 727 } |
| 873 | 728 |
| 874 DownloadsGetFileIconFunction::DownloadsGetFileIconFunction() | 729 DownloadsGetFileIconFunction::DownloadsGetFileIconFunction() |
| 875 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_GET_FILE_ICON), | 730 : icon_extractor_(new DownloadFileIconExtractorImpl()) { |
| 876 icon_size_(kDefaultIconSize), | |
| 877 icon_extractor_(new DownloadFileIconExtractorImpl()) { | |
| 878 } | 731 } |
| 879 | 732 |
| 880 DownloadsGetFileIconFunction::~DownloadsGetFileIconFunction() {} | 733 DownloadsGetFileIconFunction::~DownloadsGetFileIconFunction() {} |
| 881 | 734 |
| 882 void DownloadsGetFileIconFunction::SetIconExtractorForTesting( | 735 void DownloadsGetFileIconFunction::SetIconExtractorForTesting( |
| 883 DownloadFileIconExtractor* extractor) { | 736 DownloadFileIconExtractor* extractor) { |
| 884 DCHECK(extractor); | 737 DCHECK(extractor); |
| 885 icon_extractor_.reset(extractor); | 738 icon_extractor_.reset(extractor); |
| 886 } | 739 } |
| 887 | 740 |
| 888 bool DownloadsGetFileIconFunction::ParseArgs() { | 741 bool DownloadsGetFileIconFunction::RunImpl() { |
| 889 int dl_id = 0; | 742 scoped_ptr<extensions::api::downloads::GetFileIcon::Params> params( |
| 890 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 743 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); |
| 891 | 744 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 892 base::DictionaryValue* options = NULL; | 745 UMA_HISTOGRAM_ENUMERATION("Download.ApiFunctions", |
| 893 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | 746 DOWNLOADS_FUNCTION_GET_FILE_ICON, |
| 894 if (options->HasKey(kSizeKey)) { | 747 DOWNLOADS_FUNCTION_LAST); |
| 895 EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSizeKey, &icon_size_)); | 748 const extensions::api::downloads::GetFileIconOptions* options = |
| 896 // We only support 16px and 32px icons. This is enforced in | 749 params->options.get(); |
| 897 // experimental.downloads.json. | 750 int icon_size = kDefaultIconSize; |
| 898 DCHECK(icon_size_ == 16 || icon_size_ == 32); | 751 if (options && options->size.get()) |
| 899 } | 752 icon_size = *options->size.get(); |
| 900 | |
| 901 DownloadManager* manager = NULL; | 753 DownloadManager* manager = NULL; |
| 902 DownloadManager* incognito_manager = NULL; | 754 DownloadManager* incognito_manager = NULL; |
| 903 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | 755 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); |
| 904 DownloadItem* download_item = manager->GetDownloadItem(dl_id); | 756 DownloadItem* download_item = manager->GetDownloadItem(params->download_id); |
| 905 if (!download_item && incognito_manager) | 757 if (!download_item && incognito_manager) |
| 906 download_item = incognito_manager->GetDownloadItem(dl_id); | 758 download_item = incognito_manager->GetDownloadItem(params->download_id); |
| 907 if (!download_item) { | 759 if (!download_item) { |
| 908 // The DownloadItem is is added to history when the path is determined. If | 760 // The DownloadItem is is added to history when the path is determined. If |
| 909 // the download is not in history, then we don't have a path / final | 761 // the download is not in history, then we don't have a path / final |
| 910 // filename and no icon. | 762 // filename and no icon. |
| 911 error_ = download_extension_errors::kInvalidOperationError; | 763 error_ = download_extension_errors::kInvalidOperationError; |
| 912 return false; | 764 return false; |
| 913 } | 765 } |
| 914 // In-progress downloads return the intermediate filename for GetFullPath() | 766 // In-progress downloads return the intermediate filename for GetFullPath() |
| 915 // which doesn't have the final extension. Therefore we won't be able to | 767 // which doesn't have the final extension. Therefore we won't be able to |
| 916 // derive a good file icon for it. So we use GetTargetFilePath() instead. | 768 // derive a good file icon for it. So we use GetTargetFilePath() instead. |
| 917 path_ = download_item->GetTargetFilePath(); | 769 FilePath path = download_item->GetTargetFilePath(); |
| 918 DCHECK(!path_.empty()); | 770 DCHECK(!path.empty()); |
| 919 return true; | |
| 920 } | |
| 921 | |
| 922 bool DownloadsGetFileIconFunction::RunInternal() { | |
| 923 DCHECK(!path_.empty()); | |
| 924 DCHECK(icon_extractor_.get()); | 771 DCHECK(icon_extractor_.get()); |
| 772 DCHECK(icon_size == 16 || icon_size == 32); |
| 925 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( | 773 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( |
| 926 path_, IconLoaderSizeFromPixelSize(icon_size_), | 774 path, IconLoaderSizeFromPixelSize(icon_size), |
| 927 base::Bind(&DownloadsGetFileIconFunction::OnIconURLExtracted, this))); | 775 base::Bind(&DownloadsGetFileIconFunction::OnIconURLExtracted, this))); |
| 928 return true; | 776 return true; |
| 929 } | 777 } |
| 930 | 778 |
| 931 void DownloadsGetFileIconFunction::OnIconURLExtracted(const std::string& url) { | 779 void DownloadsGetFileIconFunction::OnIconURLExtracted(const std::string& url) { |
| 932 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 780 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 933 if (url.empty()) | 781 if (url.empty()) |
| 934 error_ = download_extension_errors::kIconNotFoundError; | 782 error_ = download_extension_errors::kIconNotFoundError; |
| 935 else | 783 else |
| 936 result_.reset(base::Value::CreateStringValue(url)); | 784 result_.reset(base::Value::CreateStringValue(url)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1011 // set the differences in the |delta| object and remember that something | 859 // set the differences in the |delta| object and remember that something |
| 1012 // significant changed. | 860 // significant changed. |
| 1013 for (base::DictionaryValue::Iterator iter(*new_json.get()); | 861 for (base::DictionaryValue::Iterator iter(*new_json.get()); |
| 1014 iter.HasNext(); iter.Advance()) { | 862 iter.HasNext(); iter.Advance()) { |
| 1015 new_fields.insert(iter.key()); | 863 new_fields.insert(iter.key()); |
| 1016 if (iter.key() != kBytesReceivedKey) { | 864 if (iter.key() != kBytesReceivedKey) { |
| 1017 base::Value* old_value = NULL; | 865 base::Value* old_value = NULL; |
| 1018 if (!old_json->HasKey(iter.key()) || | 866 if (!old_json->HasKey(iter.key()) || |
| 1019 (old_json->Get(iter.key(), &old_value) && | 867 (old_json->Get(iter.key(), &old_value) && |
| 1020 !iter.value().Equals(old_value))) { | 868 !iter.value().Equals(old_value))) { |
| 1021 delta->Set(iter.key() + ".new", iter.value().DeepCopy()); | 869 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); |
| 1022 if (old_value) | 870 if (old_value) |
| 1023 delta->Set(iter.key() + ".old", old_value->DeepCopy()); | 871 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); |
| 1024 changed = true; | 872 changed = true; |
| 1025 } | 873 } |
| 1026 } | 874 } |
| 1027 } | 875 } |
| 1028 | 876 |
| 1029 // If a field was in the previous json but is not in the new json, set the | 877 // If a field was in the previous json but is not in the new json, set the |
| 1030 // difference in |delta|. | 878 // difference in |delta|. |
| 1031 for (base::DictionaryValue::Iterator iter(*old_json); | 879 for (base::DictionaryValue::Iterator iter(*old_json); |
| 1032 iter.HasNext(); iter.Advance()) { | 880 iter.HasNext(); iter.Advance()) { |
| 1033 if (new_fields.find(iter.key()) == new_fields.end()) { | 881 if (new_fields.find(iter.key()) == new_fields.end()) { |
| 1034 delta->Set(iter.key() + ".old", iter.value().DeepCopy()); | 882 delta->Set(iter.key() + ".previous", iter.value().DeepCopy()); |
| 1035 changed = true; | 883 changed = true; |
| 1036 } | 884 } |
| 1037 } | 885 } |
| 1038 | 886 |
| 1039 // Update the OnChangedStat and dispatch the event if something significant | 887 // Update the OnChangedStat and dispatch the event if something significant |
| 1040 // changed. Replace the stored json with the new json. | 888 // changed. Replace the stored json with the new json. |
| 1041 ++(on_changed_stats_[download_id]->total); | 889 ++(on_changed_stats_[download_id]->total); |
| 1042 if (changed) { | 890 if (changed) { |
| 1043 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | 891 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
| 1044 ++(on_changed_stats_[download_id]->fires); | 892 ++(on_changed_stats_[download_id]->fires); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1113 ListValue args; | 961 ListValue args; |
| 1114 args.Append(arg); | 962 args.Append(arg); |
| 1115 std::string json_args; | 963 std::string json_args; |
| 1116 base::JSONWriter::Write(&args, &json_args); | 964 base::JSONWriter::Write(&args, &json_args); |
| 1117 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 965 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 1118 event_name, | 966 event_name, |
| 1119 json_args, | 967 json_args, |
| 1120 profile_, | 968 profile_, |
| 1121 GURL()); | 969 GURL()); |
| 1122 } | 970 } |
| OLD | NEW |