feat: add app.getRecentDocuments() (#47924)

feat: add app.getRecentDocuments()

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot]
2025-08-06 19:35:04 +02:00
committed by GitHub
parent eb02db5185
commit 33f4808182
8 changed files with 142 additions and 11 deletions

View File

@@ -1720,6 +1720,8 @@ gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) {
base::BindRepeating(&Browser::AddRecentDocument, browser))
.SetMethod("clearRecentDocuments",
base::BindRepeating(&Browser::ClearRecentDocuments, browser))
.SetMethod("getRecentDocuments",
base::BindRepeating(&Browser::GetRecentDocuments, browser))
#if BUILDFLAG(IS_WIN)
.SetMethod("setAppUserModelId",
base::BindRepeating(&Browser::SetAppUserModelID, browser))

View File

@@ -125,6 +125,9 @@ class Browser : private WindowListObserver {
// Clear the recent documents list.
void ClearRecentDocuments();
// Return the recent documents list.
std::vector<std::string> GetRecentDocuments();
#if BUILDFLAG(IS_WIN)
// Set the application user model ID.
void SetAppUserModelID(const std::wstring& name);

View File

@@ -95,6 +95,10 @@ bool SetDefaultWebClient(const std::string& protocol) {
void Browser::AddRecentDocument(const base::FilePath& path) {}
std::vector<std::string> Browser::GetRecentDocuments() {
return std::vector<std::string>();
}
void Browser::ClearRecentDocuments() {}
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,

View File

@@ -162,19 +162,31 @@ void Browser::Show() {
}
void Browser::AddRecentDocument(const base::FilePath& path) {
NSString* path_string = base::apple::FilePathToNSString(path);
if (!path_string)
NSURL* url = base::apple::FilePathToNSURL(path);
if (!url) {
LOG(WARNING) << "Failed to convert file path " << path.value()
<< " to NSURL";
return;
NSURL* u = [NSURL fileURLWithPath:path_string];
if (!u)
return;
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:u];
}
[[NSDocumentController sharedDocumentController]
noteNewRecentDocumentURL:url];
}
void Browser::ClearRecentDocuments() {
[[NSDocumentController sharedDocumentController] clearRecentDocuments:nil];
}
std::vector<std::string> Browser::GetRecentDocuments() {
NSArray<NSURL*>* recentURLs =
[[NSDocumentController sharedDocumentController] recentDocumentURLs];
std::vector<std::string> documents;
documents.reserve([recentURLs count]);
for (NSURL* url in recentURLs)
documents.push_back(std::string([url.path UTF8String]));
return documents;
}
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
gin::Arguments* args) {
NSString* identifier = [base::apple::MainBundle() bundleIdentifier];

View File

@@ -17,6 +17,7 @@
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/file_version_info.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
@@ -315,14 +316,33 @@ void GetApplicationInfoForProtocolUsingAssocQuery(
app_display_name, std::move(promise));
}
std::string ResolveShortcut(const base::FilePath& lnk_path) {
std::string target_path;
CComPtr<IShellLink> shell_link;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&shell_link)))) {
CComPtr<IPersistFile> persist_file;
if (SUCCEEDED(shell_link->QueryInterface(IID_PPV_ARGS(&persist_file)))) {
if (SUCCEEDED(persist_file->Load(lnk_path.value().c_str(), STGM_READ))) {
WCHAR resolved_path[MAX_PATH];
if (SUCCEEDED(
shell_link->GetPath(resolved_path, MAX_PATH, nullptr, 0))) {
target_path = base::FilePath(resolved_path).MaybeAsASCII();
}
}
}
}
return target_path;
}
void Browser::AddRecentDocument(const base::FilePath& path) {
CComPtr<IShellItem> item;
HRESULT hr = SHCreateItemFromParsingName(path.value().c_str(), nullptr,
IID_PPV_ARGS(&item));
if (SUCCEEDED(hr)) {
SHARDAPPIDINFO info;
info.psi = item;
info.pszAppID = GetAppUserModelID();
SHARDAPPIDINFO info = {item, GetAppUserModelID()};
SHAddToRecentDocs(SHARD_APPIDINFO, &info);
}
}
@@ -331,6 +351,33 @@ void Browser::ClearRecentDocuments() {
SHAddToRecentDocs(SHARD_APPIDINFO, nullptr);
}
std::vector<std::string> Browser::GetRecentDocuments() {
ScopedAllowBlockingForElectron allow_blocking;
std::vector<std::string> docs;
PWSTR recent_path_ptr = nullptr;
HRESULT hr =
SHGetKnownFolderPath(FOLDERID_Recent, 0, nullptr, &recent_path_ptr);
if (SUCCEEDED(hr) && recent_path_ptr) {
base::FilePath recent_folder(recent_path_ptr);
CoTaskMemFree(recent_path_ptr);
base::FileEnumerator enumerator(recent_folder, /*recursive=*/false,
base::FileEnumerator::FILES,
FILE_PATH_LITERAL("*.lnk"));
for (base::FilePath file = enumerator.Next(); !file.empty();
file = enumerator.Next()) {
std::string resolved_path = ResolveShortcut(file);
if (!resolved_path.empty()) {
docs.push_back(resolved_path);
}
}
}
return docs;
}
void Browser::SetAppUserModelID(const std::wstring& name) {
electron::SetAppUserModelID(name);
}