#include "chrome/services/util_win/util_win_impl.h"
#include <objbase.h>
#include <shldisp.h>
#include <wrl/client.h>
#include <string>
#include <string_view>
#include <utility>
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/path_service.h"
#include "base/scoped_native_library.h"
#include "base/strings/utf_string_conversions.h"
#include "base/timer/elapsed_timer.h"
#include "base/win/com_init_util.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/scoped_variant.h"
#include "base/win/shortcut.h"
#include "base/win/win_util.h"
#include "chrome/browser/win/conflicts/module_info_util.h"
#include "chrome/installer/util/registry_util.h"
#include "chrome/installer/util/taskbar_util.h"
#include "chrome/services/util_win/av_products.h"
#include "chrome/services/util_win/tpm_metrics.h"
#include "third_party/metrics_proto/system_profile.pb.h"
#include "ui/shell_dialogs/execute_select_file_win.h"
namespace {
class IsPinnedToTaskbarHelper {
public:
IsPinnedToTaskbarHelper() {
base::win::AssertComApartmentType(base::win::ComApartmentType::STA);
}
IsPinnedToTaskbarHelper(const IsPinnedToTaskbarHelper&) = delete;
IsPinnedToTaskbarHelper& operator=(const IsPinnedToTaskbarHelper&) = delete;
bool GetResult();
bool error_occured() { return error_occured_; }
private:
std::wstring LoadShellResourceString(uint32_t resource_id);
bool ShortcutHasUnpinToTaskbarVerb(const base::FilePath& shortcut);
bool IsShortcutForProgram(const base::FilePath& shortcut,
const installer::ProgramCompare& program_compare);
bool DirectoryContainsPinnedShortcutForProgram(
const base::FilePath& directory,
const installer::ProgramCompare& program_compare);
bool error_occured_ = false;
};
std::wstring IsPinnedToTaskbarHelper::LoadShellResourceString(
uint32_t resource_id) {
base::ScopedNativeLibrary scoped_native_library(::LoadLibraryEx(
FILE_PATH_LITERAL("shell32.dll"), nullptr,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!scoped_native_library.is_valid())
return std::wstring();
const wchar_t* resource_ptr = nullptr;
int length = ::LoadStringW(scoped_native_library.get(), resource_id,
reinterpret_cast<wchar_t*>(&resource_ptr), 0);
if (!length || !resource_ptr)
return std::wstring();
return std::wstring(resource_ptr, length);
}
bool IsPinnedToTaskbarHelper::ShortcutHasUnpinToTaskbarVerb(
const base::FilePath& shortcut) {
constexpr uint32_t kUnpinFromTaskbarID = 5387;
std::wstring verb_name(LoadShellResourceString(kUnpinFromTaskbarID));
if (verb_name.empty()) {
error_occured_ = true;
return false;
}
Microsoft::WRL::ComPtr<IShellDispatch> shell_dispatch;
HRESULT hresult =
::CoCreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&shell_dispatch));
if (FAILED(hresult) || !shell_dispatch) {
error_occured_ = true;
return false;
}
Microsoft::WRL::ComPtr<Folder> folder;
hresult = shell_dispatch->NameSpace(
base::win::ScopedVariant(shortcut.DirName().value().c_str()), &folder);
if (FAILED(hresult) || !folder) {
error_occured_ = true;
return false;
}
Microsoft::WRL::ComPtr<FolderItem> item;
hresult = folder->ParseName(
base::win::ScopedBstr(shortcut.BaseName().value()).Get(), &item);
if (FAILED(hresult) || !item) {
error_occured_ = true;
return false;
}
Microsoft::WRL::ComPtr<FolderItemVerbs> verbs;
hresult = item->Verbs(&verbs);
if (FAILED(hresult) || !verbs) {
error_occured_ = true;
return false;
}
long verb_count = 0;
hresult = verbs->get_Count(&verb_count);
if (FAILED(hresult)) {
error_occured_ = true;
return false;
}
long error_count = 0;
for (long i = 0; i < verb_count; ++i) {
Microsoft::WRL::ComPtr<FolderItemVerb> verb;
hresult = verbs->Item(base::win::ScopedVariant(i, VT_I4), &verb);
if (FAILED(hresult) || !verb) {
error_count++;
continue;
}
base::win::ScopedBstr name;
hresult = verb->get_Name(name.Receive());
if (FAILED(hresult)) {
error_count++;
continue;
}
if (std::wstring_view(name.Get(), name.Length()) == verb_name) {
return true;
}
}
if (error_count == verb_count)
error_occured_ = true;
return false;
}
bool IsPinnedToTaskbarHelper::IsShortcutForProgram(
const base::FilePath& shortcut,
const installer::ProgramCompare& program_compare) {
base::win::ShortcutProperties shortcut_properties;
if (!ResolveShortcutProperties(
shortcut, base::win::ShortcutProperties::PROPERTIES_TARGET,
&shortcut_properties)) {
return false;
}
return program_compare.EvaluatePath(shortcut_properties.target);
}
bool IsPinnedToTaskbarHelper::DirectoryContainsPinnedShortcutForProgram(
const base::FilePath& directory,
const installer::ProgramCompare& program_compare) {
base::FileEnumerator shortcut_enum(directory, false,
base::FileEnumerator::FILES);
for (base::FilePath shortcut = shortcut_enum.Next(); !shortcut.empty();
shortcut = shortcut_enum.Next()) {
if (IsShortcutForProgram(shortcut, program_compare)) {
std::optional<bool> is_pinned = IsShortcutPinnedToTaskbar(shortcut);
if (is_pinned == true)
return true;
if (!is_pinned.has_value() && ShortcutHasUnpinToTaskbarVerb(shortcut))
return true;
}
}
return false;
}
bool IsPinnedToTaskbarHelper::GetResult() {
base::FilePath current_exe;
if (!base::PathService::Get(base::FILE_EXE, ¤t_exe))
return false;
installer::ProgramCompare current_exe_compare(current_exe);
base::FilePath taskbar_pins_dir;
if (base::PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pins_dir) &&
DirectoryContainsPinnedShortcutForProgram(taskbar_pins_dir,
current_exe_compare)) {
return true;
}
base::FilePath implicit_app_shortcuts_dir;
if (!base::PathService::Get(base::DIR_IMPLICIT_APP_SHORTCUTS,
&implicit_app_shortcuts_dir)) {
return false;
}
base::FileEnumerator directory_enum(implicit_app_shortcuts_dir, false,
base::FileEnumerator::DIRECTORIES);
for (base::FilePath directory = directory_enum.Next(); !directory.empty();
directory = directory_enum.Next()) {
if (DirectoryContainsPinnedShortcutForProgram(directory,
current_exe_compare)) {
return true;
}
}
return false;
}
}
UtilWinImpl::UtilWinImpl(mojo::PendingReceiver<chrome::mojom::UtilWin> receiver)
: receiver_(this, std::move(receiver)) {}
UtilWinImpl::~UtilWinImpl() = default;
void UtilWinImpl::IsPinnedToTaskbar(IsPinnedToTaskbarCallback callback) {
IsPinnedToTaskbarHelper helper;
bool is_pinned_to_taskbar = helper.GetResult();
std::move(callback).Run(!helper.error_occured(), is_pinned_to_taskbar);
}
void UtilWinImpl::UnpinShortcuts(
const std::vector<base::FilePath>& shortcut_paths,
UnpinShortcutsCallback callback) {
base::win::AssertComApartmentType(base::win::ComApartmentType::STA);
for (const auto& shortcut_path : shortcut_paths)
UnpinShortcutFromTaskbar(shortcut_path);
std::move(callback).Run();
}
void UtilWinImpl::CreateOrUpdateShortcuts(
const std::vector<base::FilePath>& shortcut_paths,
const std::vector<base::win::ShortcutProperties>& properties,
base::win::ShortcutOperation operation,
CreateOrUpdateShortcutsCallback callback) {
base::win::AssertComApartmentType(base::win::ComApartmentType::STA);
bool ret = true;
for (size_t i = 0; i < shortcut_paths.size(); ++i) {
ret &= base::win::CreateOrUpdateShortcutLink(shortcut_paths[i],
properties[i], operation);
}
std::move(callback).Run(ret);
}
void UtilWinImpl::CallExecuteSelectFile(
ui::SelectFileDialog::Type type,
uint32_t owner,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int32_t file_type_index,
const std::u16string& default_extension,
CallExecuteSelectFileCallback callback) {
base::win::AssertComApartmentType(base::win::ComApartmentType::STA);
base::win::EnableHighDPISupport();
base::ElapsedTimer elapsed_time;
ui::ExecuteSelectFile(
type, title, default_path, filter, file_type_index,
base::UTF16ToWide(default_extension),
reinterpret_cast<HWND>(base::win::Uint32ToHandle(owner)),
std::move(callback));
base::UmaHistogramMediumTimes("Windows.TimeInSelectFileDialog",
elapsed_time.Elapsed());
}
void UtilWinImpl::InspectModule(const base::FilePath& module_path,
InspectModuleCallback callback) {
std::move(callback).Run(::InspectModule(module_path));
}
void UtilWinImpl::GetAntiVirusProducts(bool report_full_names,
GetAntiVirusProductsCallback callback) {
base::win::AssertComApartmentType(base::win::ComApartmentType::STA);
std::move(callback).Run(::GetAntiVirusProducts(report_full_names));
}
void UtilWinImpl::GetTpmIdentifier(GetTpmIdentifierCallback callback) {
std::move(callback).Run(::GetTpmIdentifier());
}