Chromium Code Reviews| Index: chrome/browser/download/download_extension_api.cc |
| diff --git a/chrome/browser/download/download_extension_api.cc b/chrome/browser/download/download_extension_api.cc |
| index cdf0193ff2ef33882e4c8323ea762f597ede8def..a5fa47d18fb472603de53b2823ec5ff1f0103f21 100644 |
| --- a/chrome/browser/download/download_extension_api.cc |
| +++ b/chrome/browser/download/download_extension_api.cc |
| @@ -93,6 +93,9 @@ const char kHeaderValueKey[] = "value"; |
| const char kHeaderBinaryValueKey[] = "binaryValue"; |
| const char kHeadersKey[] = "headers"; |
| const char kIdKey[] = "id"; |
| +const char kIncognito[] = "incognito"; |
| +const char kIncognitoNone[] = "none"; |
| +const char kIncognitoOnly[] = "only"; |
|
Matt Perry
2012/05/15 19:02:23
This can go away.
benjhayden
2012/05/15 21:05:05
Done.
|
| const char kLimitKey[] = "limit"; |
| const char kMethodKey[] = "method"; |
| const char kMimeKey[] = "mime"; |
| @@ -196,6 +199,7 @@ scoped_ptr<base::DictionaryValue> DownloadItemToJSON(DownloadItem* item) { |
| (item->GetStartTime() - base::Time::UnixEpoch()).InMilliseconds()); |
| json->SetInteger(kBytesReceivedKey, item->GetReceivedBytes()); |
| json->SetInteger(kTotalBytesKey, item->GetTotalBytes()); |
| + json->SetBoolean(kIncognito, item->IsOtr()); |
| if (item->GetState() == DownloadItem::INTERRUPTED) { |
| json->SetInteger(kErrorKey, static_cast<int>(item->GetLastReason())); |
| } else if (item->GetState() == DownloadItem::CANCELLED) { |
| @@ -307,6 +311,29 @@ bool IsNotTemporaryDownloadFilter(const DownloadItem& item) { |
| return !item.IsTemporary(); |
| } |
| +DownloadItem* GetActiveItemInternal( |
| + Profile* profile, |
| + bool spanning_mode, |
| + bool include_incognito, |
| + int id) { |
| + // Spanning-mode extensions should set profile() to the on-record profile, but |
| + // this does not appear to be the case for extension_function_test_utils, so |
|
Matt Perry
2012/05/15 19:02:23
I'd rather fix extension_function_test_utils if th
benjhayden
2012/05/15 21:05:05
Found another way around; see download_extension_t
|
| + // grab the on-record profile manually in this case. |
| + Profile* current_profile = (spanning_mode && |
| + profile->IsOffTheRecord()) ? |
| + profile->GetOriginalProfile() : profile; |
| + DownloadManager* manager = DownloadServiceFactory::GetForProfile( |
| + current_profile)->GetDownloadManager(); |
| + DownloadItem* download_item = manager->GetActiveDownloadItem(id); |
| + DownloadManager* incognito_manager = (include_incognito && |
| + profile->HasOffTheRecordProfile()) ? |
| + DownloadServiceFactory::GetForProfile( |
| + profile->GetOffTheRecordProfile())->GetDownloadManager() : NULL; |
| + if (!download_item && incognito_manager) |
| + download_item = incognito_manager->GetActiveDownloadItem(id); |
| + return download_item; |
| +} |
| + |
| } // namespace |
| bool DownloadsFunctionInterface::RunImplImpl( |
| @@ -334,6 +361,14 @@ SyncDownloadsFunction::function() const { |
| return function_; |
| } |
| +DownloadItem* SyncDownloadsFunction::GetActiveItem(int download_id) { |
| + return GetActiveItemInternal( |
| + profile(), |
| + !GetExtension() || !GetExtension()->incognito_split_mode(), |
| + include_incognito(), |
| + download_id); |
| +} |
| + |
| AsyncDownloadsFunction::AsyncDownloadsFunction( |
| DownloadsFunctionInterface::DownloadsFunctionName function) |
| : function_(function) { |
| @@ -350,6 +385,14 @@ AsyncDownloadsFunction::function() const { |
| return function_; |
| } |
| +DownloadItem* AsyncDownloadsFunction::GetActiveItem(int download_id) { |
| + return GetActiveItemInternal( |
| + profile(), |
| + !GetExtension() || !GetExtension()->incognito_split_mode(), |
| + include_incognito(), |
| + download_id); |
| +} |
| + |
| DownloadsDownloadFunction::DownloadsDownloadFunction() |
| : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) { |
| } |
| @@ -532,6 +575,8 @@ void DownloadsDownloadFunction::OnStarted(DownloadId dl_id, net::Error error) { |
| DownloadsSearchFunction::DownloadsSearchFunction() |
| : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), |
| query_(new DownloadQuery()), |
| + include_on_the_record_(true), |
| + include_incognito_(true), |
| get_id_(0), |
| has_get_id_(false) { |
| } |
| @@ -560,6 +605,17 @@ bool DownloadsSearchFunction::ParseArgs() { |
| EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( |
| &get_id_)); |
| has_get_id_ = true; |
| + } else if (query_json_field.key() == kIncognito) { |
|
Matt Perry
2012/05/15 19:02:23
This is obsolete.
benjhayden
2012/05/15 21:05:05
Done.
|
| + std::string incognito; |
| + EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( |
| + &incognito)); |
| + if (incognito == kIncognitoNone) { |
| + include_incognito_ = false; |
| + } else if (incognito == kIncognitoOnly) { |
| + include_on_the_record_ = false; |
| + } else { |
| + EXTENSION_FUNCTION_VALIDATE(false); |
| + } |
| } else if (query_json_field.key() == kOrderByKey) { |
| if (!ParseOrderBy(query_json_field.value())) |
| return false; |
| @@ -632,15 +688,31 @@ bool DownloadsSearchFunction::ParseOrderBy(const base::Value& order_by_value) { |
| } |
| bool DownloadsSearchFunction::RunInternal() { |
| + // Spanning-mode extensions should set profile() to the on-record profile, but |
| + // this does not appear to be the case for extension_function_test_utils, so |
| + // grab the on-record profile manually in this case. |
| + Profile* current_profile = ((!GetExtension() || |
| + !GetExtension()->incognito_split_mode()) && |
| + profile()->IsOffTheRecord()) ? |
| + profile()->GetOriginalProfile() : profile(); |
| + DownloadManager* manager = DownloadServiceFactory::GetForProfile( |
| + current_profile)->GetDownloadManager(); |
| + DownloadManager* incognito_manager = (include_incognito() && |
| + profile()->HasOffTheRecordProfile()) ? |
| + DownloadServiceFactory::GetForProfile( |
| + profile()->GetOffTheRecordProfile())->GetDownloadManager() : NULL; |
| DownloadQuery::DownloadVector all_items, cpp_results; |
| - DownloadManager* manager = DownloadServiceFactory::GetForProfile(profile()) |
| - ->GetDownloadManager(); |
| if (has_get_id_) { |
| DownloadItem* item = manager->GetDownloadItem(get_id_); |
| - if (item != NULL) |
| + if (!item && incognito_manager) |
| + item = incognito_manager->GetDownloadItem(get_id_); |
| + if (item) |
| all_items.push_back(item); |
| } else { |
| manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); |
| + if (incognito_manager) |
| + incognito_manager->GetAllDownloads( |
| + FilePath(FILE_PATH_LITERAL("")), &all_items); |
| } |
| query_->Search(all_items.begin(), all_items.end(), &cpp_results); |
| base::ListValue* json_results = new base::ListValue(); |
| @@ -666,13 +738,8 @@ bool DownloadsPauseFunction::ParseArgs() { |
| } |
| bool DownloadsPauseFunction::RunInternal() { |
| - DownloadManager* download_manager = |
| - DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); |
| - DownloadItem* download_item = |
| - download_manager->GetActiveDownloadItem(download_id_); |
| - DCHECK(!download_item || download_item->IsInProgress()); |
| - |
| - if (!download_item) { |
| + DownloadItem* download_item = GetActiveItem(download_id_); |
| + if ((download_item == NULL) || !download_item->IsInProgress()) { |
| // This could be due to an invalid download ID, or it could be due to the |
| // download not being currently active. |
| error_ = download_extension_errors::kInvalidOperationError; |
| @@ -696,13 +763,8 @@ bool DownloadsResumeFunction::ParseArgs() { |
| } |
| bool DownloadsResumeFunction::RunInternal() { |
| - DownloadManager* download_manager = |
| - DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); |
| - DownloadItem* download_item = |
| - download_manager->GetActiveDownloadItem(download_id_); |
| - DCHECK(!download_item || download_item->IsInProgress()); |
| - |
| - if (!download_item) { |
| + DownloadItem* download_item = GetActiveItem(download_id_); |
| + if (download_item == NULL) { |
| // This could be due to an invalid download ID, or it could be due to the |
| // download not being currently active. |
| error_ = download_extension_errors::kInvalidOperationError; |
| @@ -726,12 +788,8 @@ bool DownloadsCancelFunction::ParseArgs() { |
| } |
| bool DownloadsCancelFunction::RunInternal() { |
| - DownloadManager* download_manager = |
| - DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); |
| - DownloadItem* download_item = |
| - download_manager->GetActiveDownloadItem(download_id_); |
| - |
| - if (download_item) |
| + DownloadItem* download_item = GetActiveItem(download_id_); |
| + if (download_item != NULL) |
| download_item->Cancel(true); |
| // |download_item| can be NULL if the download ID was invalid or if the |
| // download is not currently active. Either way, we don't consider it a |