Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/utility/shell_handler_impl_win.h" | |
| 6 | |
| 7 #include <shldisp.h> | |
| 8 | |
| 9 #include "base/files/file_enumerator.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/scoped_native_library.h" | |
| 13 #include "base/win/scoped_bstr.h" | |
| 14 #include "base/win/scoped_com_initializer.h" | |
| 15 #include "base/win/scoped_comptr.h" | |
| 16 #include "base/win/scoped_variant.h" | |
| 17 #include "base/win/shortcut.h" | |
| 18 #include "chrome/installer/util/install_util.h" | |
| 19 #include "content/public/utility/utility_thread.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Returns the shell resource string identified by |resource_id|, or an empty | |
| 24 // string on error. | |
| 25 base::string16 LoadShellResourceString(uint32_t resource_id) { | |
| 26 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
| |
| 27 base::FilePath(FILE_PATH_LITERAL("shell32.dll"))); | |
| 28 if (!scoped_native_library.is_valid()) | |
| 29 return base::string16(); | |
| 30 | |
| 31 const wchar_t* resource_ptr = nullptr; | |
| 32 int length = ::LoadStringW(scoped_native_library.get(), resource_id, | |
| 33 reinterpret_cast<wchar_t*>(&resource_ptr), 0); | |
| 34 if (!length || !resource_ptr) | |
| 35 return base::string16(); | |
| 36 return base::string16(resource_ptr, length); | |
| 37 } | |
| 38 | |
| 39 // Returns true if the "Unpin from taskbar" verb is available for |shortcut|, | |
| 40 // which means that the shortcut is pinned to the taskbar. | |
| 41 bool ShortcutHasUnpinToTaskbarVerb(const base::FilePath& shortcut) { | |
| 42 // Found inside shell32.dll's resouces. | |
| 43 constexpr uint32_t kUnpinFromTaskbarID = 5387; | |
| 44 | |
| 45 base::string16 verb_name(LoadShellResourceString(kUnpinFromTaskbarID)); | |
| 46 if (verb_name.empty()) | |
| 47 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.
| |
| 48 | |
| 49 base::win::ScopedComPtr<IShellDispatch> shell_dispatch; | |
| 50 HRESULT hresult = | |
| 51 shell_dispatch.CreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER); | |
| 52 if (FAILED(hresult) || !shell_dispatch) | |
| 53 return false; | |
| 54 | |
| 55 base::win::ScopedComPtr<Folder> folder; | |
| 56 hresult = shell_dispatch->NameSpace( | |
| 57 base::win::ScopedVariant(shortcut.DirName().value().c_str()), | |
| 58 folder.Receive()); | |
| 59 if (FAILED(hresult) || !folder) | |
| 60 return false; | |
| 61 | |
| 62 base::win::ScopedComPtr<FolderItem> item; | |
| 63 hresult = folder->ParseName( | |
| 64 base::win::ScopedBstr(shortcut.BaseName().value().c_str()), | |
| 65 item.Receive()); | |
| 66 if (FAILED(hresult) || !item) | |
| 67 return false; | |
| 68 | |
| 69 base::win::ScopedComPtr<FolderItemVerbs> verbs; | |
| 70 hresult = item->Verbs(verbs.Receive()); | |
| 71 if (FAILED(hresult) || !verbs) | |
| 72 return false; | |
| 73 | |
| 74 long verb_count = 0; | |
| 75 hresult = verbs->get_Count(&verb_count); | |
| 76 if (FAILED(hresult)) | |
| 77 return false; | |
| 78 | |
| 79 for (long i = 0; i < verb_count; ++i) { | |
| 80 base::win::ScopedComPtr<FolderItemVerb> verb; | |
| 81 hresult = verbs->Item(base::win::ScopedVariant(i, VT_I4), verb.Receive()); | |
| 82 if (FAILED(hresult) || !verb) | |
| 83 continue; | |
| 84 base::win::ScopedBstr name; | |
| 85 hresult = verb->get_Name(name.Receive()); | |
| 86 if (FAILED(hresult)) | |
| 87 continue; | |
| 88 if (base::StringPiece16(name, name.Length()) == verb_name) | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 // Returns true if the target parameter of the |shortcut| evaluates to | |
| 96 // |program_compare|. | |
| 97 bool IsShortcutForProgram(const base::FilePath& shortcut, | |
| 98 const InstallUtil::ProgramCompare& program_compare) { | |
| 99 base::win::ShortcutProperties shortcut_properties; | |
| 100 if (!ResolveShortcutProperties( | |
| 101 shortcut, base::win::ShortcutProperties::PROPERTIES_TARGET, | |
| 102 &shortcut_properties)) { | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 return program_compare.EvaluatePath(shortcut_properties.target); | |
| 107 } | |
| 108 | |
| 109 // Returns true if one of the shortcut inside the given |directory| evaluates to | |
| 110 // |program_compare| and is pinned to the taskbar. | |
| 111 bool DirectoryContainsPinnedShortcutForProgram( | |
| 112 const base::FilePath& directory, | |
| 113 const InstallUtil::ProgramCompare& program_compare) { | |
| 114 base::FileEnumerator shortcut_enum(directory, false, | |
| 115 base::FileEnumerator::FILES); | |
| 116 for (base::FilePath shortcut = shortcut_enum.Next(); !shortcut.empty(); | |
| 117 shortcut = shortcut_enum.Next()) { | |
| 118 if (IsShortcutForProgram(shortcut, program_compare) && | |
| 119 ShortcutHasUnpinToTaskbarVerb(shortcut)) { | |
| 120 return true; | |
| 121 } | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 // Returns true if the current executable is pinned to the taskbar. | |
| 127 bool IsPinnedToTaskbarImpl() { | |
| 128 base::FilePath current_exe; | |
| 129 if (!PathService::Get(base::FILE_EXE, ¤t_exe)) | |
| 130 return false; | |
| 131 | |
| 132 InstallUtil::ProgramCompare current_exe_compare(current_exe); | |
| 133 | |
| 134 // Look into the "Quick Launch\User Pinned\TaskBar" folder. | |
| 135 base::FilePath taskbar_pins_dir; | |
| 136 if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pins_dir) && | |
| 137 DirectoryContainsPinnedShortcutForProgram(taskbar_pins_dir, | |
| 138 current_exe_compare)) { | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 // Check all folders in ImplicitAppShortcuts. | |
| 143 base::FilePath implicit_app_shortcuts_dir; | |
| 144 if (PathService::Get(base::DIR_IMPLICIT_APP_SHORTCUTS, | |
| 145 &implicit_app_shortcuts_dir)) { | |
| 146 base::FileEnumerator directory_enum(implicit_app_shortcuts_dir, false, | |
| 147 base::FileEnumerator::DIRECTORIES); | |
| 148 for (base::FilePath directory = directory_enum.Next(); !directory.empty(); | |
| 149 directory = directory_enum.Next()) { | |
| 150 if (DirectoryContainsPinnedShortcutForProgram(directory, | |
| 151 current_exe_compare)) { | |
| 152 return true; | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 return false; | |
| 157 } | |
| 158 | |
| 159 } // namespace | |
| 160 | |
| 161 // static | |
| 162 void ShellHandlerImpl::Create(mojom::ShellHandlerRequest request) { | |
| 163 new ShellHandlerImpl(std::move(request)); | |
| 164 } | |
| 165 | |
| 166 ShellHandlerImpl::ShellHandlerImpl(mojom::ShellHandlerRequest request) | |
| 167 : binding_(this, std::move(request)) {} | |
| 168 | |
| 169 ShellHandlerImpl::~ShellHandlerImpl() = default; | |
| 170 | |
| 171 void ShellHandlerImpl::IsPinnedToTaskbar( | |
| 172 const IsPinnedToTaskbarCallback& callback) { | |
| 173 base::win::ScopedCOMInitializer scoped_com_initializer; | |
| 174 callback.Run(IsPinnedToTaskbarImpl()); | |
| 175 } | |
| OLD | NEW |