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