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 // This class checks if the current executable is pinned to the taskbar. It also | |
| 24 // keeps track of the errors that occurs that prevents it from getting a result. | |
| 25 class IsPinnedToTaskbarHelper { | |
| 26 public: | |
| 27 IsPinnedToTaskbarHelper() = default; | |
| 28 ~IsPinnedToTaskbarHelper() = default; | |
| 29 | |
| 30 // Returns true if the current executable is pinned to the taskbar. | |
| 31 bool GetResult(); | |
| 32 bool error_occured() { return error_occured_; } | |
|
gab
2016/06/28 20:59:18
nit: Empty line above
Patrick Monette
2016/06/28 23:22:24
Done.
| |
| 33 | |
| 34 private: | |
| 35 // Returns the shell resource string identified by |resource_id|, or an empty | |
| 36 // string on error. | |
| 37 base::string16 LoadShellResourceString(uint32_t resource_id); | |
| 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 | |
| 43 // Returns true if the target parameter of the |shortcut| evaluates to | |
| 44 // |program_compare|. | |
| 45 bool IsShortcutForProgram(const base::FilePath& shortcut, | |
| 46 const InstallUtil::ProgramCompare& program_compare); | |
| 47 | |
| 48 // Returns true if one of the shortcut inside the given |directory| evaluates | |
| 49 // to |program_compare| and is pinned to the taskbar. | |
| 50 bool DirectoryContainsPinnedShortcutForProgram( | |
| 51 const base::FilePath& directory, | |
| 52 const InstallUtil::ProgramCompare& program_compare); | |
| 53 | |
| 54 bool error_occured_ = false; | |
| 55 base::win::ScopedCOMInitializer scoped_com_initializer_; | |
| 56 }; | |
| 57 | |
| 58 base::string16 IsPinnedToTaskbarHelper::LoadShellResourceString( | |
|
gab
2016/06/28 20:59:18
Note: glanced over the code below but didn't fully
Patrick Monette
2016/06/28 23:22:25
Sounds good but maybe you could review everything
gab
2016/06/30 15:28:11
error_occurred_ lgtm
| |
| 59 uint32_t resource_id) { | |
| 60 base::ScopedNativeLibrary scoped_native_library(::LoadLibraryEx( | |
| 61 FILE_PATH_LITERAL("shell32.dll"), nullptr, LOAD_LIBRARY_AS_DATAFILE)); | |
| 62 if (!scoped_native_library.is_valid()) | |
| 63 return base::string16(); | |
| 64 | |
| 65 const wchar_t* resource_ptr = nullptr; | |
| 66 int length = ::LoadStringW(scoped_native_library.get(), resource_id, | |
| 67 reinterpret_cast<wchar_t*>(&resource_ptr), 0); | |
| 68 if (!length || !resource_ptr) | |
| 69 return base::string16(); | |
| 70 return base::string16(resource_ptr, length); | |
| 71 } | |
| 72 | |
| 73 bool IsPinnedToTaskbarHelper::ShortcutHasUnpinToTaskbarVerb( | |
| 74 const base::FilePath& shortcut) { | |
| 75 // Found inside shell32.dll's resources. | |
| 76 constexpr uint32_t kUnpinFromTaskbarID = 5387; | |
| 77 | |
| 78 base::string16 verb_name(LoadShellResourceString(kUnpinFromTaskbarID)); | |
| 79 if (verb_name.empty()) { | |
| 80 error_occured_ = true; | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 base::win::ScopedComPtr<IShellDispatch> shell_dispatch; | |
| 85 HRESULT hresult = | |
| 86 shell_dispatch.CreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER); | |
| 87 if (FAILED(hresult) || !shell_dispatch) { | |
| 88 error_occured_ = true; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 base::win::ScopedComPtr<Folder> folder; | |
| 93 hresult = shell_dispatch->NameSpace( | |
| 94 base::win::ScopedVariant(shortcut.DirName().value().c_str()), | |
| 95 folder.Receive()); | |
| 96 if (FAILED(hresult) || !folder) { | |
| 97 error_occured_ = true; | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 base::win::ScopedComPtr<FolderItem> item; | |
| 102 hresult = folder->ParseName( | |
| 103 base::win::ScopedBstr(shortcut.BaseName().value().c_str()), | |
| 104 item.Receive()); | |
| 105 if (FAILED(hresult) || !item) { | |
| 106 error_occured_ = true; | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 base::win::ScopedComPtr<FolderItemVerbs> verbs; | |
| 111 hresult = item->Verbs(verbs.Receive()); | |
| 112 if (FAILED(hresult) || !verbs) { | |
| 113 error_occured_ = true; | |
| 114 return false; | |
| 115 } | |
| 116 | |
| 117 long verb_count = 0; | |
| 118 hresult = verbs->get_Count(&verb_count); | |
| 119 if (FAILED(hresult)) { | |
| 120 error_occured_ = true; | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 long error_count = 0; | |
| 125 for (long i = 0; i < verb_count; ++i) { | |
| 126 base::win::ScopedComPtr<FolderItemVerb> verb; | |
| 127 hresult = verbs->Item(base::win::ScopedVariant(i, VT_I4), verb.Receive()); | |
| 128 if (FAILED(hresult) || !verb) { | |
| 129 error_count++; | |
| 130 continue; | |
| 131 } | |
| 132 base::win::ScopedBstr name; | |
| 133 hresult = verb->get_Name(name.Receive()); | |
| 134 if (FAILED(hresult)) { | |
| 135 error_count++; | |
| 136 continue; | |
| 137 } | |
| 138 if (base::StringPiece16(name, name.Length()) == verb_name) | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 142 if (error_count == verb_count) | |
| 143 error_occured_ = true; | |
| 144 | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 bool IsPinnedToTaskbarHelper::IsShortcutForProgram( | |
| 149 const base::FilePath& shortcut, | |
| 150 const InstallUtil::ProgramCompare& program_compare) { | |
| 151 base::win::ShortcutProperties shortcut_properties; | |
| 152 if (!ResolveShortcutProperties( | |
| 153 shortcut, base::win::ShortcutProperties::PROPERTIES_TARGET, | |
| 154 &shortcut_properties)) { | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 return program_compare.EvaluatePath(shortcut_properties.target); | |
| 159 } | |
| 160 | |
| 161 bool IsPinnedToTaskbarHelper::DirectoryContainsPinnedShortcutForProgram( | |
| 162 const base::FilePath& directory, | |
| 163 const InstallUtil::ProgramCompare& program_compare) { | |
| 164 base::FileEnumerator shortcut_enum(directory, false, | |
| 165 base::FileEnumerator::FILES); | |
| 166 for (base::FilePath shortcut = shortcut_enum.Next(); !shortcut.empty(); | |
| 167 shortcut = shortcut_enum.Next()) { | |
| 168 if (IsShortcutForProgram(shortcut, program_compare) && | |
| 169 ShortcutHasUnpinToTaskbarVerb(shortcut)) { | |
| 170 return true; | |
| 171 } | |
| 172 } | |
| 173 return false; | |
| 174 } | |
| 175 | |
| 176 bool IsPinnedToTaskbarHelper::GetResult() { | |
| 177 base::FilePath current_exe; | |
| 178 PathService::Get(base::FILE_EXE, ¤t_exe); | |
| 179 | |
| 180 InstallUtil::ProgramCompare current_exe_compare(current_exe); | |
| 181 // Look into the "Quick Launch\User Pinned\TaskBar" folder. | |
| 182 base::FilePath taskbar_pins_dir; | |
| 183 PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pins_dir); | |
| 184 if (DirectoryContainsPinnedShortcutForProgram(taskbar_pins_dir, | |
| 185 current_exe_compare)) { | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 // Check all folders in ImplicitAppShortcuts. | |
| 190 base::FilePath implicit_app_shortcuts_dir; | |
| 191 PathService::Get(base::DIR_IMPLICIT_APP_SHORTCUTS, | |
| 192 &implicit_app_shortcuts_dir); | |
| 193 base::FileEnumerator directory_enum(implicit_app_shortcuts_dir, false, | |
| 194 base::FileEnumerator::DIRECTORIES); | |
| 195 for (base::FilePath directory = directory_enum.Next(); !directory.empty(); | |
| 196 directory = directory_enum.Next()) { | |
| 197 if (DirectoryContainsPinnedShortcutForProgram(directory, | |
| 198 current_exe_compare)) { | |
| 199 return true; | |
| 200 } | |
| 201 } | |
| 202 return false; | |
| 203 } | |
| 204 | |
| 205 } // namespace | |
| 206 | |
| 207 // static | |
| 208 void ShellHandlerImpl::Create(mojom::ShellHandlerRequest request) { | |
| 209 new ShellHandlerImpl(std::move(request)); | |
| 210 } | |
| 211 | |
| 212 ShellHandlerImpl::ShellHandlerImpl(mojom::ShellHandlerRequest request) | |
| 213 : binding_(this, std::move(request)) {} | |
| 214 | |
| 215 ShellHandlerImpl::~ShellHandlerImpl() = default; | |
| 216 | |
| 217 void ShellHandlerImpl::IsPinnedToTaskbar( | |
| 218 const IsPinnedToTaskbarCallback& callback) { | |
| 219 IsPinnedToTaskbarHelper helper; | |
| 220 bool is_pinned_to_taskbar = helper.GetResult(); | |
| 221 callback.Run(!helper.error_occured(), is_pinned_to_taskbar); | |
| 222 } | |
| OLD | NEW |