Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(635)

Side by Side Diff: chrome/utility/shell_handler_impl_win.cc

Issue 2079233004: Add the Windows.IsPinnedToTaskbar metric. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: asvitkine comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
Nico 2016/06/29 20:02:31 why these two lines? that's what happens if you om
Patrick Monette 2016/06/29 21:14:53 Removed the destructor. The constructor is needed
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
58 DISALLOW_COPY_AND_ASSIGN(IsPinnedToTaskbarHelper);
59 };
60
61 base::string16 IsPinnedToTaskbarHelper::LoadShellResourceString(
62 uint32_t resource_id) {
63 base::ScopedNativeLibrary scoped_native_library(::LoadLibraryEx(
64 FILE_PATH_LITERAL("shell32.dll"), nullptr, LOAD_LIBRARY_AS_DATAFILE));
65 if (!scoped_native_library.is_valid())
66 return base::string16();
67
68 const wchar_t* resource_ptr = nullptr;
69 int length = ::LoadStringW(scoped_native_library.get(), resource_id,
70 reinterpret_cast<wchar_t*>(&resource_ptr), 0);
71 if (!length || !resource_ptr)
72 return base::string16();
73 return base::string16(resource_ptr, length);
74 }
75
76 bool IsPinnedToTaskbarHelper::ShortcutHasUnpinToTaskbarVerb(
77 const base::FilePath& shortcut) {
78 // Found inside shell32.dll's resources.
79 constexpr uint32_t kUnpinFromTaskbarID = 5387;
80
81 base::string16 verb_name(LoadShellResourceString(kUnpinFromTaskbarID));
82 if (verb_name.empty()) {
83 error_occured_ = true;
84 return false;
85 }
86
87 base::win::ScopedComPtr<IShellDispatch> shell_dispatch;
88 HRESULT hresult =
89 shell_dispatch.CreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER);
90 if (FAILED(hresult) || !shell_dispatch) {
91 error_occured_ = true;
92 return false;
93 }
94
95 base::win::ScopedComPtr<Folder> folder;
96 hresult = shell_dispatch->NameSpace(
97 base::win::ScopedVariant(shortcut.DirName().value().c_str()),
98 folder.Receive());
99 if (FAILED(hresult) || !folder) {
100 error_occured_ = true;
101 return false;
102 }
103
104 base::win::ScopedComPtr<FolderItem> item;
105 hresult = folder->ParseName(
106 base::win::ScopedBstr(shortcut.BaseName().value().c_str()),
107 item.Receive());
108 if (FAILED(hresult) || !item) {
109 error_occured_ = true;
110 return false;
111 }
112
113 base::win::ScopedComPtr<FolderItemVerbs> verbs;
114 hresult = item->Verbs(verbs.Receive());
115 if (FAILED(hresult) || !verbs) {
116 error_occured_ = true;
117 return false;
118 }
119
120 long verb_count = 0;
121 hresult = verbs->get_Count(&verb_count);
122 if (FAILED(hresult)) {
123 error_occured_ = true;
124 return false;
125 }
126
127 long error_count = 0;
128 for (long i = 0; i < verb_count; ++i) {
129 base::win::ScopedComPtr<FolderItemVerb> verb;
130 hresult = verbs->Item(base::win::ScopedVariant(i, VT_I4), verb.Receive());
131 if (FAILED(hresult) || !verb) {
132 error_count++;
133 continue;
134 }
135 base::win::ScopedBstr name;
136 hresult = verb->get_Name(name.Receive());
137 if (FAILED(hresult)) {
138 error_count++;
139 continue;
140 }
141 if (base::StringPiece16(name, name.Length()) == verb_name)
142 return true;
143 }
144
145 if (error_count == verb_count)
146 error_occured_ = true;
147
148 return false;
149 }
150
151 bool IsPinnedToTaskbarHelper::IsShortcutForProgram(
152 const base::FilePath& shortcut,
153 const InstallUtil::ProgramCompare& program_compare) {
154 base::win::ShortcutProperties shortcut_properties;
155 if (!ResolveShortcutProperties(
156 shortcut, base::win::ShortcutProperties::PROPERTIES_TARGET,
157 &shortcut_properties)) {
158 return false;
159 }
160
161 return program_compare.EvaluatePath(shortcut_properties.target);
162 }
163
164 bool IsPinnedToTaskbarHelper::DirectoryContainsPinnedShortcutForProgram(
165 const base::FilePath& directory,
166 const InstallUtil::ProgramCompare& program_compare) {
167 base::FileEnumerator shortcut_enum(directory, false,
168 base::FileEnumerator::FILES);
169 for (base::FilePath shortcut = shortcut_enum.Next(); !shortcut.empty();
170 shortcut = shortcut_enum.Next()) {
171 if (IsShortcutForProgram(shortcut, program_compare) &&
172 ShortcutHasUnpinToTaskbarVerb(shortcut)) {
173 return true;
174 }
175 }
176 return false;
177 }
178
179 bool IsPinnedToTaskbarHelper::GetResult() {
180 base::FilePath current_exe;
181 PathService::Get(base::FILE_EXE, &current_exe);
182
183 InstallUtil::ProgramCompare current_exe_compare(current_exe);
184 // Look into the "Quick Launch\User Pinned\TaskBar" folder.
185 base::FilePath taskbar_pins_dir;
186 PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pins_dir);
187 if (DirectoryContainsPinnedShortcutForProgram(taskbar_pins_dir,
188 current_exe_compare)) {
189 return true;
190 }
191
192 // Check all folders in ImplicitAppShortcuts.
193 base::FilePath implicit_app_shortcuts_dir;
194 PathService::Get(base::DIR_IMPLICIT_APP_SHORTCUTS,
195 &implicit_app_shortcuts_dir);
196 base::FileEnumerator directory_enum(implicit_app_shortcuts_dir, false,
197 base::FileEnumerator::DIRECTORIES);
198 for (base::FilePath directory = directory_enum.Next(); !directory.empty();
199 directory = directory_enum.Next()) {
200 if (DirectoryContainsPinnedShortcutForProgram(directory,
201 current_exe_compare)) {
202 return true;
203 }
204 }
205 return false;
206 }
207
208 } // namespace
209
210 // static
211 void ShellHandlerImpl::Create(mojom::ShellHandlerRequest request) {
212 new ShellHandlerImpl(std::move(request));
213 }
214
215 ShellHandlerImpl::ShellHandlerImpl(mojom::ShellHandlerRequest request)
216 : binding_(this, std::move(request)) {}
217
218 ShellHandlerImpl::~ShellHandlerImpl() = default;
219
220 void ShellHandlerImpl::IsPinnedToTaskbar(
221 const IsPinnedToTaskbarCallback& callback) {
222 IsPinnedToTaskbarHelper helper;
223 bool is_pinned_to_taskbar = helper.GetResult();
224 callback.Run(!helper.error_occured(), is_pinned_to_taskbar);
225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698