#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/extensions/api/downloads/downloads_api.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <optional>
#include <string_view>
#include "base/containers/circular_deque.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/bubble/download_display_controller.h"
#include "chrome/browser/download/download_browsertest_utils.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_file_icon_extractor.h"
#include "chrome/browser/download/download_open_prompt.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_test_file_activity_observer.h"
#include "chrome/browser/extensions/api/downloads_internal/downloads_internal_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/platform_util_internal.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/download/download_display.h"
#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
#include "chrome/common/extensions/api/downloads.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/download/public/common/download_item.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/common/file_type_policies_test_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/storage_partition_config.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/download_test_observer.h"
#include "content/public/test/slow_download_http_response.h"
#include "content/public/test/test_download_http_response.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "net/base/data_url.h"
#include "net/base/mime_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/cpp/features.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/file_system/file_system_url.h"
#include "ui/base/page_transition_types.h"
#include "url/origin.h"
BrowserContext;
BrowserThread;
DownloadManager;
DownloadItem;
errors;
namespace extensions {
downloads;
namespace {
const char kFirstDownloadUrl[] = …;
const char kSecondDownloadUrl[] = …;
const int kDownloadSize = …;
void OnFileDeleted(bool success) { … }
struct DownloadIdComparator { … };
bool IsDownloadExternallyRemoved(download::DownloadItem* item) { … }
void OnOpenPromptCreated(download::DownloadItem* item,
DownloadOpenPrompt* prompt) { … }
class DownloadsEventsListener : public EventRouter::TestObserver { … };
class DownloadOpenObserver : public download::DownloadItem::Observer { … };
}
class DownloadExtensionTest : public ExtensionApiTest { … };
namespace {
class MockIconExtractorImpl : public DownloadFileIconExtractor { … };
bool ItemNotInProgress(DownloadItem* item) { … }
class ScopedCancellingItem { … };
class ScopedItemVectorCanceller { … };
class HTML5FileWriter { … };
class JustInProgressDownloadObserver
: public content::DownloadTestObserverInProgress { … };
bool ItemIsInterrupted(DownloadItem* item) { … }
}
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, DownloadExtensionTest_Open) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_PauseResumeCancelErase) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Open_Remove_Open) { … }
scoped_refptr<ExtensionFunction> MockedGetFileIconFunction(
const base::FilePath& expected_path,
IconLoader::IconSize icon_size,
const std::string& response) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_FileIcon_Active) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_FileIcon_History) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchEmptyQuery) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, FileExistenceCheckAfterSearch) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadsShowFunction) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadsShowDefaultFolderFunction) { … }
#endif
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchFilenameRegex) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, DownloadExtensionTest_SearchId) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchIdAndFilename) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchOrderBy) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchOrderByEmpty) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchDanger) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchState) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchLimit) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchInvalid) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SearchPlural) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_SearchPauseResumeCancelGetFileIconIncognito) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Basic) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Redirect) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Incognito) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Disallowed_Character_In_Filename) { … }
namespace {
class CustomResponse : public net::test_server::HttpResponse { … };
}
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_InterruptAndResume) { … }
#if (BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER))
#define MAYBE_DownloadExtensionTest_Download_UnsafeHeaders …
#else
#define MAYBE_DownloadExtensionTest_Download_UnsafeHeaders …
#endif
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
MAYBE_DownloadExtensionTest_Download_UnsafeHeaders) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_InvalidHeaders) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_DownloadExtensionTest_Download_Subdirectory …
#else
#define MAYBE_DownloadExtensionTest_Download_Subdirectory …
#endif
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
MAYBE_DownloadExtensionTest_Download_Subdirectory) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_InvalidFilename) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_InvalidURLs1) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_InvalidURLs2) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_URLFragment) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_ConflictAction) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_DataURL) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_File) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_AuthBasic_Fail) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Headers) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Headers_Fail) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_AuthBasic) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Post) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Post_Get) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Post_NoBody) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_Cancel) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_DownloadExtensionTest_Download_FileSystemURL …
#else
#define MAYBE_DownloadExtensionTest_Download_FileSystemURL …
#endif
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
MAYBE_DownloadExtensionTest_Download_FileSystemURL) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_NoChange) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DISABLED_DownloadExtensionTest_OnDeterminingFilename_Timeout) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_Twice) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadsInternalDetermineFilename) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_SafeOverride) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_IllegalFilename) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_IllegalFilenameExtension) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_ReservedFilename) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DISABLED_DownloadExtensionTest_OnDeterminingFilename_EmptyBasenameInvalid) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_Overwrite) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_Override) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer …
#else
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer …
#endif
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
MAYBE_DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) { … }
#if ((BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && \
defined(ADDRESS_SANITIZER))
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit …
#else
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit …
#endif
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
MAYBE_DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DownloadExtensionTest_OnDeterminingFilename_IncognitoSpanning) { … }
IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest,
DISABLED_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SetShelfEnabled) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_SetUiOptions) { … }
void OnDangerPromptCreated(DownloadDangerPrompt* prompt) { … }
#if (BUILDFLAG(IS_MAC) && !defined(NDEBUG))
#define MAYBE_DownloadExtensionTest_AcceptDanger …
#else
#define MAYBE_DownloadExtensionTest_AcceptDanger …
#endif
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
MAYBE_DownloadExtensionTest_AcceptDanger) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_DeleteFileAfterCompletion) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
class DownloadExtensionBubbleEnabledTest : public DownloadExtensionTest { … };
IN_PROC_BROWSER_TEST_F(DownloadExtensionBubbleEnabledTest, SetUiOptions) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionBubbleEnabledTest,
SetUiOptionsBeforeDownloadStart) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionBubbleEnabledTest,
SetUiOptionsOffTheRecord) { … }
IN_PROC_BROWSER_TEST_F(DownloadExtensionBubbleEnabledTest,
SetUiOptionsMultipleExtensions) { … }
#endif
class DownloadsApiTest : public ExtensionApiTest { … };
IN_PROC_BROWSER_TEST_F(DownloadsApiTest, DownloadsApiTest) { … }
TEST(ExtensionDetermineDownloadFilenameInternal,
ExtensionDetermineDownloadFilenameInternal) { … }
}