Return boolean from shell.showItemInFolder

This commit is contained in:
Kevin Sawicki 2016-09-12 14:22:29 -07:00
parent 05043432dd
commit 72558654ef
5 changed files with 38 additions and 37 deletions

View file

@ -21,7 +21,7 @@ namespace platform_util {
// Show the given file in a file manager. If possible, select the file. // Show the given file in a file manager. If possible, select the file.
// Must be called from the UI thread. // Must be called from the UI thread.
void ShowItemInFolder(const base::FilePath& full_path); bool ShowItemInFolder(const base::FilePath& full_path);
// Open the given file in the desktop's default manner. // Open the given file in the desktop's default manner.
// Must be called from the UI thread. // Must be called from the UI thread.

View file

@ -59,12 +59,12 @@ namespace platform_util {
// TODO(estade): It would be nice to be able to select the file in the file // TODO(estade): It would be nice to be able to select the file in the file
// manager, but that probably requires extending xdg-open. For now just // manager, but that probably requires extending xdg-open. For now just
// show the folder. // show the folder.
void ShowItemInFolder(const base::FilePath& full_path) { bool ShowItemInFolder(const base::FilePath& full_path) {
base::FilePath dir = full_path.DirName(); base::FilePath dir = full_path.DirName();
if (!base::DirectoryExists(dir)) if (!base::DirectoryExists(dir))
return; return false;
XDGOpen(dir.value(), true); return XDGOpen(dir.value(), true);
} }
void OpenItem(const base::FilePath& full_path) { void OpenItem(const base::FilePath& full_path) {

View file

@ -18,7 +18,7 @@
namespace platform_util { namespace platform_util {
void ShowItemInFolder(const base::FilePath& path) { bool ShowItemInFolder(const base::FilePath& path) {
// The API only takes absolute path. // The API only takes absolute path.
base::FilePath full_path = base::FilePath full_path =
path.IsAbsolute() ? path : base::MakeAbsoluteFilePath(path); path.IsAbsolute() ? path : base::MakeAbsoluteFilePath(path);
@ -26,8 +26,11 @@ void ShowItemInFolder(const base::FilePath& path) {
DCHECK([NSThread isMainThread]); DCHECK([NSThread isMainThread]);
NSString* path_string = base::SysUTF8ToNSString(full_path.value()); NSString* path_string = base::SysUTF8ToNSString(full_path.value());
if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string if (!path_string || ![[NSWorkspace sharedWorkspace] selectFile:path_string
inFileViewerRootedAtPath:@""]) inFileViewerRootedAtPath:@""]) {
LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value(); LOG(WARNING) << "NSWorkspace failed to select file " << full_path.value();
return false;
}
return true;
} }
// This function opens a file. This doesn't use LaunchServices or NSWorkspace // This function opens a file. This doesn't use LaunchServices or NSWorkspace

View file

@ -203,7 +203,7 @@ HRESULT DeleteFileProgressSink::ResumeTimer() {
namespace platform_util { namespace platform_util {
void ShowItemInFolder(const base::FilePath& full_path) { bool ShowItemInFolder(const base::FilePath& full_path) {
base::win::ScopedCOMInitializer com_initializer; base::win::ScopedCOMInitializer com_initializer;
if (!com_initializer.succeeded()) if (!com_initializer.succeeded())
return; return;
@ -211,7 +211,7 @@ void ShowItemInFolder(const base::FilePath& full_path) {
base::FilePath dir = full_path.DirName().AsEndingWithSeparator(); base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
// ParseDisplayName will fail if the directory is "C:", it must be "C:\\". // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
if (dir.empty()) if (dir.empty())
return; return false;
typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)( typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
PCIDLIST_ABSOLUTE pidl_Folder, PCIDLIST_ABSOLUTE pidl_Folder,
@ -232,29 +232,27 @@ void ShowItemInFolder(const base::FilePath& full_path) {
HMODULE shell32_base = GetModuleHandle(L"shell32.dll"); HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
if (!shell32_base) { if (!shell32_base) {
NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll"; NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
return; return false;
} }
open_folder_and_select_itemsPtr = open_folder_and_select_itemsPtr =
reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr> reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
(GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems")); (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
} }
if (!open_folder_and_select_itemsPtr) { if (!open_folder_and_select_itemsPtr) {
ui::win::OpenFolderViaShell(dir); return ui::win::OpenFolderViaShell(dir);
return;
} }
base::win::ScopedComPtr<IShellFolder> desktop; base::win::ScopedComPtr<IShellFolder> desktop;
HRESULT hr = SHGetDesktopFolder(desktop.Receive()); HRESULT hr = SHGetDesktopFolder(desktop.Receive());
if (FAILED(hr)) if (FAILED(hr))
return; return false;
base::win::ScopedCoMem<ITEMIDLIST> dir_item; base::win::ScopedCoMem<ITEMIDLIST> dir_item;
hr = desktop->ParseDisplayName(NULL, NULL, hr = desktop->ParseDisplayName(NULL, NULL,
const_cast<wchar_t *>(dir.value().c_str()), const_cast<wchar_t *>(dir.value().c_str()),
NULL, &dir_item, NULL); NULL, &dir_item, NULL);
if (FAILED(hr)) { if (FAILED(hr)) {
ui::win::OpenFolderViaShell(dir); return ui::win::OpenFolderViaShell(dir);
return;
} }
base::win::ScopedCoMem<ITEMIDLIST> file_item; base::win::ScopedCoMem<ITEMIDLIST> file_item;
@ -262,21 +260,21 @@ void ShowItemInFolder(const base::FilePath& full_path) {
const_cast<wchar_t *>(full_path.value().c_str()), const_cast<wchar_t *>(full_path.value().c_str()),
NULL, &file_item, NULL); NULL, &file_item, NULL);
if (FAILED(hr)) { if (FAILED(hr)) {
ui::win::OpenFolderViaShell(dir); return ui::win::OpenFolderViaShell(dir);
return;
} }
const ITEMIDLIST* highlight[] = { file_item }; const ITEMIDLIST* highlight[] = { file_item };
hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight), hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
highlight, NULL); highlight, NULL);
if (!FAILED(hr))
return true;
if (FAILED(hr)) {
// On some systems, the above call mysteriously fails with "file not // On some systems, the above call mysteriously fails with "file not
// found" even though the file is there. In these cases, ShellExecute() // found" even though the file is there. In these cases, ShellExecute()
// seems to work as a fallback (although it won't select the file). // seems to work as a fallback (although it won't select the file).
if (hr == ERROR_FILE_NOT_FOUND) { if (hr == ERROR_FILE_NOT_FOUND) {
ui::win::OpenFolderViaShell(dir); return ui::win::OpenFolderViaShell(dir);
} else { } else {
LPTSTR message = NULL; LPTSTR message = NULL;
DWORD message_length = FormatMessage( DWORD message_length = FormatMessage(
@ -290,8 +288,7 @@ void ShowItemInFolder(const base::FilePath& full_path) {
if (message) if (message)
LocalFree(message); LocalFree(message);
ui::win::OpenFolderViaShell(dir); return ui::win::OpenFolderViaShell(dir);
}
} }
} }

View file

@ -20,7 +20,8 @@ The `shell` module has the following methods:
* `fullPath` String * `fullPath` String
Show the given file in a file manager. If possible, select the file. Show the given file in a file manager. If possible, select the file. Returns
true if the item was successfully shown, false otherwse.
### `shell.openItem(fullPath)` ### `shell.openItem(fullPath)`