diff --git a/docs/api/app.md b/docs/api/app.md index cc19fae3f7..268b8f7b10 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -608,6 +608,7 @@ Returns `String` - The current application directory. * `music` Directory for a user's music. * `pictures` Directory for a user's pictures. * `videos` Directory for a user's videos. + * `recent` Directory for the user's recent files (Windows only). * `logs` Directory for your app's log folder. * `pepperFlashSystemPlugin` Full path to the system version of the Pepper Flash plugin. * `crashDumps` Directory where crash dumps are stored. diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 1ebe6211cf..e79fc439d4 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -54,6 +54,7 @@ #include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/node_includes.h" #include "shell/common/options_switches.h" +#include "shell/common/platform_util.h" #include "ui/gfx/image/image.h" #if defined(OS_WIN) @@ -425,6 +426,10 @@ int GetPathConstant(const std::string& name) { return chrome::DIR_USER_PICTURES; else if (name == "videos") return chrome::DIR_USER_VIDEOS; +#if defined(OS_WIN) + else if (name == "recent") + return electron::DIR_RECENT; +#endif else if (name == "pepperFlashSystemPlugin") return chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN; else @@ -885,6 +890,15 @@ base::FilePath App::GetPath(gin_helper::ErrorThrower thrower, SetAppLogsPath(thrower, base::Optional()); succeed = base::PathService::Get(key, &path); } + +#if defined(OS_WIN) + // If we get the "recent" path before setting it, set it + if (!succeed && name == "recent" && + platform_util::GetFolderPath(DIR_RECENT, &path)) { + base::ThreadRestrictions::ScopedAllowIO allow_io; + succeed = base::PathService::Override(DIR_RECENT, path); + } +#endif } if (!succeed) diff --git a/shell/common/electron_paths.h b/shell/common/electron_paths.h index 680287e2d0..dc07e3d6e7 100644 --- a/shell/common/electron_paths.h +++ b/shell/common/electron_paths.h @@ -26,6 +26,10 @@ enum { DIR_USER_CACHE, // Directory where user cache can be written. DIR_APP_LOGS, // Directory where app logs live +#if defined(OS_WIN) + DIR_RECENT, // Directory where recent files live +#endif + #if defined(OS_LINUX) DIR_APP_DATA, // Application Data directory under the user profile. #endif diff --git a/shell/common/platform_util.h b/shell/common/platform_util.h index 5af45651ac..2630b7dfeb 100644 --- a/shell/common/platform_util.h +++ b/shell/common/platform_util.h @@ -45,6 +45,11 @@ bool MoveItemToTrash(const base::FilePath& full_path, bool delete_on_fail); void Beep(); +#if defined(OS_WIN) +// SHGetFolderPath calls not covered by Chromium +bool GetFolderPath(int key, base::FilePath* result); +#endif + #if defined(OS_MACOSX) bool GetLoginItemEnabled(); bool SetLoginItemEnabled(bool enabled); diff --git a/shell/common/platform_util_win.cc b/shell/common/platform_util_win.cc index 0f18968c1d..edf6aaace3 100644 --- a/shell/common/platform_util_win.cc +++ b/shell/common/platform_util_win.cc @@ -32,6 +32,7 @@ #include "base/win/windows_version.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" +#include "shell/common/electron_paths.h" #include "ui/base/win/shell.h" #include "url/gurl.h" @@ -392,6 +393,22 @@ bool MoveItemToTrash(const base::FilePath& path, bool delete_on_fail) { SUCCEEDED(pfo->PerformOperations()); } +bool GetFolderPath(int key, base::FilePath* result) { + wchar_t system_buffer[MAX_PATH]; + + switch (key) { + case electron::DIR_RECENT: + if (FAILED(SHGetFolderPath(NULL, CSIDL_RECENT, NULL, SHGFP_TYPE_CURRENT, + system_buffer))) { + return false; + } + *result = base::FilePath(system_buffer); + break; + } + + return true; +} + void Beep() { MessageBeep(MB_OK); } diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index 57eb129a5d..8f96280745 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -737,6 +737,22 @@ describe('app module', () => { app.setPath('music', __dirname); expect(app.getPath('music')).to.equal(__dirname); }); + + if (process.platform === 'win32') { + it('gets the folder for recent files', () => { + const recent = app.getPath('recent'); + + // We expect that one of our test machines have overriden this + // to be something crazy, it'll always include the word "Recent" + // unless people have been registry-hacking like crazy + expect(recent).to.include('Recent'); + }); + + it('can override the recent files path', () => { + app.setPath('recent', 'C:\\fake-path'); + expect(app.getPath('recent')).to.equal('C:\\fake-path'); + }); + } }); describe('setPath(name, path)', () => {