Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

A download an extension's popup starts saves instead of being dropped by CEF (#218)

Victor7f3ba0071334
diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swift
index 1791e23..16e305f 100644
--- a/Sources/Cra/UI/Windows/MainWindowController.swift
+++ b/Sources/Cra/UI/Windows/MainWindowController.swift
@@ -628,6 +628,8 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
updateRecentDownload(download)
if download.state == .complete, !download.fullPath.isEmpty, finishedDownloads.insert(download.identifier).inserted {
DownloadHistory.shared.add(path: download.fullPath)
+ // The address's scheme only, never the file: scenarios wait for this line.
+ CraLog.note("Download finished from a \(URL(string: download.url)?.scheme ?? "unknown"): address")
let id = download.identifier
TidyDownloadsService.shared.finished(id, path: download.fullPath, downloadURL: download.url, window: windowID, store: store) { [weak self] path in
guard let self, var recent = self.sidebarUI.recentDownload, recent.id == id else { return }
diff --git a/Tests/Fixtures/DownloadExtension/ID b/Tests/Fixtures/DownloadExtension/ID
new file mode 100644
index 0000000..ecf7874
--- /dev/null
+++ b/Tests/Fixtures/DownloadExtension/ID
@@ -0,0 +1 @@
+apgalegecclidlofdknjminflfoegcpc
diff --git a/Tests/Fixtures/DownloadExtension/manifest.json b/Tests/Fixtures/DownloadExtension/manifest.json
new file mode 100644
index 0000000..4271e42
--- /dev/null
+++ b/Tests/Fixtures/DownloadExtension/manifest.json
@@ -0,0 +1,9 @@
+{
+ "manifest_version": 3,
+ "name": "Cra Download Fixture",
+ "version": "1.0",
+ "description": "Made up for scripts/scenario.sh: a popup that saves a file through chrome.downloads (GitHub #218).",
+ "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlRQ7Y3GkDvCSWuYjAN6SvAYdH++2MtBajyxWnzCMlQqV/j672uR6gp+2h2bVFavOOAKTNWIh5pqxNef+lYOTypnZdeHHKIxkkU399J700vF5BuaxbrhMjrZdtEnqDq2dEAq1IXT09yy0RG/S7Df8RezNQ/hmnJ9Lx/cUp5C3SflRkCAt6Rd0Ek/jQ9ixetOzvZ8nbIRWM+76qqTWjxa/3IS5kFTPC6Bn3RYN7/TVdyKOIGvLYQPwtTWMo/prZZNcdFg75CHUbNDlgyFg6klKdzhP74TGWRcL0ubmGZkJgakE/DBLmGH9yxapFfQ0HtSoMeepW0n9dGPt6JFFlPBSLwIDAQAB",
+ "permissions": ["downloads"],
+ "action": { "default_popup": "popup.html", "default_title": "Cra Download Fixture" }
+}
diff --git a/Tests/Fixtures/DownloadExtension/popup.html b/Tests/Fixtures/DownloadExtension/popup.html
new file mode 100644
index 0000000..eb297c6
--- /dev/null
+++ b/Tests/Fixtures/DownloadExtension/popup.html
@@ -0,0 +1 @@
+<!doctype html><meta charset="utf-8"><title>Download fixture popup</title><body style="width:200px;height:120px">Saving…<script src="popup.js"></script></body>
diff --git a/Tests/Fixtures/DownloadExtension/popup.js b/Tests/Fixtures/DownloadExtension/popup.js
new file mode 100644
index 0000000..27e99ae
--- /dev/null
+++ b/Tests/Fixtures/DownloadExtension/popup.js
@@ -0,0 +1,6 @@
+// Saves a file from the popup document as soon as it opens, the way an extension's
+// "Download" button does: chrome.downloads with a blob: address made here (GitHub #218).
+const url = URL.createObjectURL(new Blob(["Cra download fixture\n"], { type: "text/plain" }));
+chrome.downloads.download({ url, filename: "cra-popup-download.txt", saveAs: false })
+ .then(() => { document.body.textContent = "Saved"; })
+ .catch((e) => { document.body.textContent = "Failed: " + e.message; });
diff --git a/Tests/Scenarios/extension-popup-download.scenario b/Tests/Scenarios/extension-popup-download.scenario
new file mode 100644
index 0000000..422f5e1
--- /dev/null
+++ b/Tests/Scenarios/extension-popup-download.scenario
@@ -0,0 +1,14 @@
+# An extension's popup saves a file with chrome.downloads as it opens. With Chrome's own UI the
+# popup is a page CEF made no browser for, and CEF cancelled its downloads before Cra saw them
+# ("No CefBrowser for download item"); they now go to the profile's last-used page (GitHub #218).
+# promise: #218
+home
+budget rss=600 stalls50=5
+cra://debug?installExtension=${FIXTURES}/DownloadExtension
+relaunch
+cra://open?url=${PAGE}?n=download
+wait 3
+cra://debug?extensionAction=${DOWNLOAD_EXTENSION_ID}
+expect-log Download finished from a blob: address
+cra://dismiss
+cra://debug?removeExtension=${DOWNLOAD_EXTENSION_ID}
diff --git a/Tests/promises.lock b/Tests/promises.lock
index bcccc3f..165fd43 100644
--- a/Tests/promises.lock
+++ b/Tests/promises.lock
@@ -329,3 +329,4 @@
#209 Tests/Scenarios/accept-languages.scenario 6ca3402ebe15
#211 Packages/CraCore/Tests/CraCoreTests/FaviconKeyTests.swift::tabsOnOneHostKeepTheirOwnIcons 21d360f30e94
#215 Tests/StoreTests.swift::arcLiveFolderImportsLive a5c462b3a9c7
+#218 Tests/Scenarios/extension-popup-download.scenario ffb6285b0eee
diff --git a/scripts/cef-patches/downloads-without-cef-browser.patch b/scripts/cef-patches/downloads-without-cef-browser.patch
new file mode 100644
index 0000000..a27ad13
--- /dev/null
+++ b/scripts/cef-patches/downloads-without-cef-browser.patch
@@ -0,0 +1,108 @@
+diff --git a/cef/libcef/browser/download_manager_delegate_impl.cc b/cef/libcef/browser/download_manager_delegate_impl.cc
+index e5363cc46..4af0a70c3 100644
+--- a/cef/libcef/browser/download_manager_delegate_impl.cc
++++ b/cef/libcef/browser/download_manager_delegate_impl.cc
+@@ -14,6 +14,8 @@
+ #include "base/strings/utf_string_conversions.h"
+ #include "cef/include/cef_download_handler.h"
+ #include "cef/libcef/browser/browser_host_base.h"
++#include "cef/libcef/browser/browser_info.h"
++#include "cef/libcef/browser/browser_info_manager.h"
+ #include "cef/libcef/browser/context.h"
+ #include "cef/libcef/browser/download_item_impl.h"
+ #include "cef/libcef/browser/thread_util.h"
+@@ -27,6 +29,63 @@
+ #include "net/http/http_response_headers.h"
+ #include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h"
+
++namespace {
++
++// Cra (GitHub #203): the CRX a Web Store install downloads (WebstoreInstaller,
++// from the store page's "Add to Chrome" or Cra.installFromWebStore) is Chrome's
++// own: it goes to the path WebstoreInstaller set, under the profile's "Webstore
++// Downloads", and CrxInstaller takes it from there. The client's download
++// handler never sees it, so no save panel asks where to put it and it never
++// lands in the Downloads folder; and one from a page with no CefBrowser
++// (Cra.installFromWebStore's) isn't cancelled.
++bool IsCraExtensionInstall(download::DownloadItem* item) {
++ return item->GetDownloadSource() ==
++ download::DownloadSource::EXTENSION_INSTALLER;
++}
++
++
++// Cra (GitHub #218): a download started where CEF made no browser, from
++// Chrome's own extension popup bubble, side panel or DevTools (a WebContents
++// with no CefBrowser) or from an extension's service worker through
++// chrome.downloads (no WebContents at all), was cancelled before the client's
++// download handler saw it. It goes to the page used last in the same profile
++// instead: the handler only needs a browser of that profile to pick the
++// folder, ask where to save and list the download.
++CefRefPtr<CefBrowserHostBase> CraBrowserForUnownedDownload(
++ download::DownloadItem* item,
++ content::WebContents* contents) {
++ if (!contents && item->GetDownloadSource() !=
++ download::DownloadSource::EXTENSION_API) {
++ return nullptr;
++ }
++ content::BrowserContext* context =
++ content::DownloadItemUtils::GetBrowserContext(item);
++ if (!context) {
++ return nullptr;
++ }
++ CefRefPtr<CefBrowserHostBase> best;
++ base::TimeTicks best_active;
++ for (const auto& browser_info :
++ CefBrowserInfoManager::GetInstance()->GetBrowserInfoList()) {
++ auto browser = browser_info->browser();
++ if (!browser || browser->GetBrowserContext() != context) {
++ continue;
++ }
++ content::WebContents* web_contents = browser->GetWebContents();
++ if (!web_contents) {
++ continue;
++ }
++ base::TimeTicks active = web_contents->GetLastActiveTimeTicks();
++ if (!best || active > best_active) {
++ best = browser;
++ best_active = active;
++ }
++ }
++ return best;
++}
++
++} // namespace
++
+ using content::DownloadManager;
+ using content::WebContents;
+ using download::DownloadItem;
+@@ -379,6 +438,9 @@ void CefDownloadManagerDelegateImpl::OnDownloadCreated(DownloadManager* manager,
+ // This callback may arrive after DetermineDownloadTarget, so we allow
+ // association from either method.
+ CefRefPtr<CefBrowserHostBase> browser = GetOrAssociateBrowser(item);
++ if (!browser && IsCraExtensionInstall(item)) {
++ return;
++ }
+ if (!browser) {
+ // If the download is rejected (e.g. ALT+click on an invalid protocol link)
+ // then an "interrupted" download will be started via DownloadManagerImpl::
+@@ -487,13 +549,19 @@ CefDownloadManagerDelegateImpl::GetOrAssociateBrowser(
+ if (it != item_browser_map_.end()) {
+ return it->second.get();
+ }
++ if (IsCraExtensionInstall(item)) {
++ return nullptr;
++ }
+
+ CefRefPtr<CefBrowserHostBase> browser;
+ content::WebContents* contents =
+ content::DownloadItemUtils::GetWebContents(item);
+ if (contents) {
+ browser = CefBrowserHostBase::GetBrowserForContents(contents);
+- LOG_IF(WARNING, !browser) << "No CefBrowser for download item";
++ }
++ if (!browser) {
++ browser = CraBrowserForUnownedDownload(item, contents);
++ LOG_IF(WARNING, !browser && contents) << "No CefBrowser for download item";
+ }
+ if (!browser) {
+ return nullptr;
diff --git a/scripts/cef-patches/webstore-install-downloads.patch b/scripts/cef-patches/webstore-install-downloads.patch
deleted file mode 100644
index 80fba6e..0000000
--- a/scripts/cef-patches/webstore-install-downloads.patch
+++ /dev/null
@@ -1,47 +0,0 @@
-diff --git a/cef/libcef/browser/download_manager_delegate_impl.cc b/cef/libcef/browser/download_manager_delegate_impl.cc
-index e5363cc46..786bc5c86 100644
---- a/cef/libcef/browser/download_manager_delegate_impl.cc
-+++ b/cef/libcef/browser/download_manager_delegate_impl.cc
-@@ -27,6 +27,22 @@
- #include "net/http/http_response_headers.h"
- #include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h"
-
-+namespace {
-+
-+// Cra (GitHub #203): the CRX a Web Store install downloads (WebstoreInstaller,
-+// from the store page's "Add to Chrome" or Cra.installFromWebStore) is Chrome's
-+// own: it goes to the path WebstoreInstaller set, under the profile's "Webstore
-+// Downloads", and CrxInstaller takes it from there. The client's download
-+// handler never sees it, so no save panel asks where to put it and it never
-+// lands in the Downloads folder; and one from a page with no CefBrowser
-+// (Cra.installFromWebStore's) isn't cancelled.
-+bool IsCraExtensionInstall(download::DownloadItem* item) {
-+ return item->GetDownloadSource() ==
-+ download::DownloadSource::EXTENSION_INSTALLER;
-+}
-+
-+} // namespace
-+
- using content::DownloadManager;
- using content::WebContents;
- using download::DownloadItem;
-@@ -379,6 +395,9 @@ void CefDownloadManagerDelegateImpl::OnDownloadCreated(DownloadManager* manager,
- // This callback may arrive after DetermineDownloadTarget, so we allow
- // association from either method.
- CefRefPtr<CefBrowserHostBase> browser = GetOrAssociateBrowser(item);
-+ if (!browser && IsCraExtensionInstall(item)) {
-+ return;
-+ }
- if (!browser) {
- // If the download is rejected (e.g. ALT+click on an invalid protocol link)
- // then an "interrupted" download will be started via DownloadManagerImpl::
-@@ -487,6 +506,9 @@ CefDownloadManagerDelegateImpl::GetOrAssociateBrowser(
- if (it != item_browser_map_.end()) {
- return it->second.get();
- }
-+ if (IsCraExtensionInstall(item)) {
-+ return nullptr;
-+ }
-
- CefRefPtr<CefBrowserHostBase> browser;
- content::WebContents* contents =
diff --git a/scripts/fetch-cef.sh b/scripts/fetch-cef.sh
index 11fe17a..242eafc 100755
--- a/scripts/fetch-cef.sh
+++ b/scripts/fetch-cef.sh
@@ -13,7 +13,7 @@ CEF_VERSION="${CEF_VERSION:-156.0.15+gcf671c0+chromium-156.0.8078.80}"
ARCH="${CEF_ARCH:-macosarm64}"
NAME="cef_binary_${CEF_VERSION}_${ARCH}_minimal"
# Bump the suffix (codecs2, …) when the build changes but CEF_VERSION does not.
-CEF_RELEASE="${CEF_RELEASE:-cef-${CEF_VERSION%%+*}-codecs15}"
+CEF_RELEASE="${CEF_RELEASE:-cef-${CEF_VERSION%%+*}-codecs16}"
URL="${CEF_URL:-https://github.com/VictorKildahl/cra/releases/download/${CEF_RELEASE}/cef_binary_${ARCH}_minimal.tar.bz2}"
mkdir -p vendor
diff --git a/scripts/scenario.sh b/scripts/scenario.sh
index 0dc5dac..1df2b46 100755
--- a/scripts/scenario.sh
+++ b/scripts/scenario.sh
@@ -36,7 +36,8 @@
# ${FIXTURES}, ${PAGE} (Tests/Fixtures/Pages/page.html from a local server, so it has a host
# for developer mode and tab sleep), ${WEBKIT} (that server as http://localhost:<port>, a host the
# run treats as a DRM service, so its pages open in WebKit), ${EXTENSION_ID} (Tests/Fixtures/Extension) and
-# ${SIDE_PANEL_EXTENSION_ID} (Tests/Fixtures/SidePanelExtension) expand.
+# ${SIDE_PANEL_EXTENSION_ID} (Tests/Fixtures/SidePanelExtension) and ${DOWNLOAD_EXTENSION_ID}
+# (Tests/Fixtures/DownloadExtension) expand.
set -uo pipefail
cd "$(dirname "$0")/.."
ROOT="$PWD"
@@ -44,6 +45,7 @@ APP="${CRA_APP:-$ROOT/build/Cra.app}"
FIXTURES="$ROOT/Tests/Fixtures"
EXTENSION_ID="$(cat "$FIXTURES/Extension/ID")"
SIDE_PANEL_EXTENSION_ID="$(cat "$FIXTURES/SidePanelExtension/ID")"
+DOWNLOAD_EXTENSION_ID="$(cat "$FIXTURES/DownloadExtension/ID")"
if [ "${1:-}" = "all" ]; then
failed=()
@@ -152,7 +154,7 @@ sleep 2
PACE=0.2
RSS_BUDGET=1500 MAXSTALL=1500 STALLS50=10 LAUNCHSTALL=1000 PERFCPU=0.1
BASELINE=""
-expand() { local s="$1"; s="${s//\$\{FIXTURES\}/$FIXTURES}"; s="${s//\$\{PAGE\}/$PAGE}"; s="${s//\$\{WEBKIT\}/$WEBKIT}"; s="${s//\$\{EXTENSION_ID\}/$EXTENSION_ID}"; s="${s//\$\{SIDE_PANEL_EXTENSION_ID\}/$SIDE_PANEL_EXTENSION_ID}"; printf '%s' "$s"; }
+expand() { local s="$1"; s="${s//\$\{FIXTURES\}/$FIXTURES}"; s="${s//\$\{PAGE\}/$PAGE}"; s="${s//\$\{WEBKIT\}/$WEBKIT}"; s="${s//\$\{EXTENSION_ID\}/$EXTENSION_ID}"; s="${s//\$\{SIDE_PANEL_EXTENSION_ID\}/$SIDE_PANEL_EXTENSION_ID}"; s="${s//\$\{DOWNLOAD_EXTENSION_ID\}/$DOWNLOAD_EXTENSION_ID}"; printf '%s' "$s"; }
echo "$NAME (pid $PID, logs in ${OUT#$ROOT/})"
while IFS= read -r raw || [ -n "$raw" ]; do