Chromium Code Reviews| Index: chrome/utility/shell_handler_impl_win.cc |
| diff --git a/chrome/utility/shell_handler_impl_win.cc b/chrome/utility/shell_handler_impl_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3e9bb2a8df1338d02739b6dd202fb9d7f62ca855 |
| --- /dev/null |
| +++ b/chrome/utility/shell_handler_impl_win.cc |
| @@ -0,0 +1,175 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/utility/shell_handler_impl_win.h" |
| + |
| +#include <shldisp.h> |
| + |
| +#include "base/files/file_enumerator.h" |
| +#include "base/files/file_path.h" |
| +#include "base/path_service.h" |
| +#include "base/scoped_native_library.h" |
| +#include "base/win/scoped_bstr.h" |
| +#include "base/win/scoped_com_initializer.h" |
| +#include "base/win/scoped_comptr.h" |
| +#include "base/win/scoped_variant.h" |
| +#include "base/win/shortcut.h" |
| +#include "chrome/installer/util/install_util.h" |
| +#include "content/public/utility/utility_thread.h" |
| + |
| +namespace { |
| + |
| +// Returns the shell resource string identified by |resource_id|, or an empty |
| +// string on error. |
| +base::string16 LoadShellResourceString(uint32_t resource_id) { |
| + base::ScopedNativeLibrary scoped_native_library( |
|
grt (UTC plus 2)
2016/06/22 13:58:50
is it possible to load it as a data file (LOAD_LIB
Patrick Monette
2016/06/23 22:58:53
Done. I am not sure of the difference between LOAD
grt (UTC plus 2)
2016/07/14 07:39:35
Apologies for not being more clear: my suggestion
|
| + base::FilePath(FILE_PATH_LITERAL("shell32.dll"))); |
| + if (!scoped_native_library.is_valid()) |
| + return base::string16(); |
| + |
| + 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 base::string16(); |
| + return base::string16(resource_ptr, length); |
| +} |
| + |
| +// Returns true if the "Unpin from taskbar" verb is available for |shortcut|, |
| +// which means that the shortcut is pinned to the taskbar. |
| +bool ShortcutHasUnpinToTaskbarVerb(const base::FilePath& shortcut) { |
| + // Found inside shell32.dll's resouces. |
| + constexpr uint32_t kUnpinFromTaskbarID = 5387; |
| + |
| + base::string16 verb_name(LoadShellResourceString(kUnpinFromTaskbarID)); |
| + if (verb_name.empty()) |
| + return false; |
|
grt (UTC plus 2)
2016/06/22 13:58:50
it seems that we won't be able to distinguish "not
Patrick Monette
2016/06/23 22:58:52
Done.
|
| + |
| + base::win::ScopedComPtr<IShellDispatch> shell_dispatch; |
| + HRESULT hresult = |
| + shell_dispatch.CreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER); |
| + if (FAILED(hresult) || !shell_dispatch) |
| + return false; |
| + |
| + base::win::ScopedComPtr<Folder> folder; |
| + hresult = shell_dispatch->NameSpace( |
| + base::win::ScopedVariant(shortcut.DirName().value().c_str()), |
| + folder.Receive()); |
| + if (FAILED(hresult) || !folder) |
| + return false; |
| + |
| + base::win::ScopedComPtr<FolderItem> item; |
| + hresult = folder->ParseName( |
| + base::win::ScopedBstr(shortcut.BaseName().value().c_str()), |
| + item.Receive()); |
| + if (FAILED(hresult) || !item) |
| + return false; |
| + |
| + base::win::ScopedComPtr<FolderItemVerbs> verbs; |
| + hresult = item->Verbs(verbs.Receive()); |
| + if (FAILED(hresult) || !verbs) |
| + return false; |
| + |
| + long verb_count = 0; |
| + hresult = verbs->get_Count(&verb_count); |
| + if (FAILED(hresult)) |
| + return false; |
| + |
| + for (long i = 0; i < verb_count; ++i) { |
| + base::win::ScopedComPtr<FolderItemVerb> verb; |
| + hresult = verbs->Item(base::win::ScopedVariant(i, VT_I4), verb.Receive()); |
| + if (FAILED(hresult) || !verb) |
| + continue; |
| + base::win::ScopedBstr name; |
| + hresult = verb->get_Name(name.Receive()); |
| + if (FAILED(hresult)) |
| + continue; |
| + if (base::StringPiece16(name, name.Length()) == verb_name) |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +// Returns true if the target parameter of the |shortcut| evaluates to |
| +// |program_compare|. |
| +bool IsShortcutForProgram(const base::FilePath& shortcut, |
| + const InstallUtil::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); |
| +} |
| + |
| +// Returns true if one of the shortcut inside the given |directory| evaluates to |
| +// |program_compare| and is pinned to the taskbar. |
| +bool DirectoryContainsPinnedShortcutForProgram( |
| + const base::FilePath& directory, |
| + const InstallUtil::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) && |
| + ShortcutHasUnpinToTaskbarVerb(shortcut)) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +// Returns true if the current executable is pinned to the taskbar. |
| +bool IsPinnedToTaskbarImpl() { |
| + base::FilePath current_exe; |
| + if (!PathService::Get(base::FILE_EXE, ¤t_exe)) |
| + return false; |
| + |
| + InstallUtil::ProgramCompare current_exe_compare(current_exe); |
| + |
| + // Look into the "Quick Launch\User Pinned\TaskBar" folder. |
| + base::FilePath taskbar_pins_dir; |
| + if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pins_dir) && |
| + DirectoryContainsPinnedShortcutForProgram(taskbar_pins_dir, |
| + current_exe_compare)) { |
| + return true; |
| + } |
| + |
| + // Check all folders in ImplicitAppShortcuts. |
| + base::FilePath implicit_app_shortcuts_dir; |
| + if (PathService::Get(base::DIR_IMPLICIT_APP_SHORTCUTS, |
| + &implicit_app_shortcuts_dir)) { |
| + 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; |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void ShellHandlerImpl::Create(mojom::ShellHandlerRequest request) { |
| + new ShellHandlerImpl(std::move(request)); |
| +} |
| + |
| +ShellHandlerImpl::ShellHandlerImpl(mojom::ShellHandlerRequest request) |
| + : binding_(this, std::move(request)) {} |
| + |
| +ShellHandlerImpl::~ShellHandlerImpl() = default; |
| + |
| +void ShellHandlerImpl::IsPinnedToTaskbar( |
| + const IsPinnedToTaskbarCallback& callback) { |
| + base::win::ScopedCOMInitializer scoped_com_initializer; |
| + callback.Run(IsPinnedToTaskbarImpl()); |
| +} |