#include "remoting/host/file_transfer/directory_helpers.h"
#include <windows.h>
#include <shlobj.h>
#include "base/check_is_test.h"
#include "base/logging.h"
#include "base/win/scoped_handle.h"
namespace remoting {
static base::FilePath* g_upload_directory_for_testing = nullptr;
protocol::FileTransferResult<base::FilePath> GetFileUploadDirectory() {
if (g_upload_directory_for_testing) {
CHECK_IS_TEST();
return *g_upload_directory_for_testing;
}
HANDLE user_token = nullptr;
if (!OpenThreadToken(GetCurrentThread(),
TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, TRUE,
&user_token)) {
PLOG(ERROR) << "Failed to open thread token";
return protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_UNEXPECTED_ERROR,
GetLastError());
}
base::win::ScopedHandle scoped_user_token(user_token);
wchar_t buffer[MAX_PATH];
buffer[0] = 0;
HRESULT hr =
SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, scoped_user_token.Get(),
SHGFP_TYPE_CURRENT, buffer);
if (FAILED(hr)) {
LOG(ERROR) << "Failed to get desktop directory: " << hr;
return protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_UNEXPECTED_ERROR, hr);
}
return {kSuccessTag, buffer};
}
void SetFileUploadDirectoryForTesting(base::FilePath dir) {
if (g_upload_directory_for_testing) {
delete g_upload_directory_for_testing;
}
g_upload_directory_for_testing = new base::FilePath(dir);
}
}