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

Side by Side Diff: chrome/browser/jumplist_updater_win.cc

Issue 168583011: Move Windows Jump List boilerplate out of jumplist_win for reuse (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review comments Created 6 years, 10 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 2014 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/browser/jumplist_updater_win.h"
6
7 #include <windows.h>
8 #include <propkey.h>
9 #include <shobjidl.h>
10
11 #include "base/path_service.h"
12 #include "base/win/win_util.h"
13 #include "base/win/windows_version.h"
14
15 namespace {
16
17 // Creates an IShellLink object.
18 // An IShellLink object is almost the same as an application shortcut, and it
19 // requires three items: the absolute path to an application, an argument
20 // string, and a title string.
21 bool AddShellLink(base::win::ScopedComPtr<IObjectCollection> collection,
22 const std::wstring& application_path,
23 scoped_refptr<ShellLinkItem> item) {
24 // Create an IShellLink object.
25 base::win::ScopedComPtr<IShellLink> link;
26 HRESULT result = link.CreateInstance(CLSID_ShellLink, NULL,
27 CLSCTX_INPROC_SERVER);
28 if (FAILED(result))
29 return false;
30
31 // Set the application path.
32 // We should exit this function when this call fails because it doesn't make
33 // any sense to add a shortcut that we cannot execute.
34 result = link->SetPath(application_path.c_str());
35 if (FAILED(result))
36 return false;
37
38 // Attach the command-line switches of this process before the given
39 // arguments and set it as the arguments of this IShellLink object.
40 // We also exit this function when this call fails because it isn't useful to
41 // add a shortcut that cannot open the given page.
42 std::wstring arguments(item->GetArguments());
43 if (!arguments.empty()) {
44 result = link->SetArguments(arguments.c_str());
45 if (FAILED(result))
46 return false;
47 }
48
49 // Attach the given icon path to this IShellLink object.
50 // Since an icon is an optional item for an IShellLink object, so we don't
51 // have to exit even when it fails.
52 if (!item->icon_path().empty())
53 link->SetIconLocation(item->icon_path().c_str(), item->icon_index());
54
55 // Set the title of the IShellLink object.
56 // The IShellLink interface does not have any functions which update its
57 // title because this interface is originally for creating an application
58 // shortcut which doesn't have titles.
59 // So, we should use the IPropertyStore interface to set its title.
60 base::win::ScopedComPtr<IPropertyStore> property_store;
61 result = link.QueryInterface(property_store.Receive());
62 if (FAILED(result))
63 return false;
64
65 if (!base::win::SetStringValueForPropertyStore(
66 property_store.get(),
67 PKEY_Title,
68 item->title().c_str())) {
69 return false;
70 }
71
72 // Add this IShellLink object to the given collection.
73 return SUCCEEDED(collection->AddObject(link));
74 }
75
76 } // namespace
77
78
79 // ShellLinkItem
80
81 ShellLinkItem::ShellLinkItem()
82 : command_line_(CommandLine::NO_PROGRAM),
83 icon_index_(0) {
84 }
85
86 ShellLinkItem::~ShellLinkItem() {}
87
88 std::wstring ShellLinkItem::GetArguments() const {
89 return command_line_.GetArgumentsString();
90 }
91
92 CommandLine* ShellLinkItem::GetCommandLine() {
93 return &command_line_;
94 }
95
96
97 // JumpListUpdater
98
99 JumpListUpdater::JumpListUpdater(const std::wstring& app_user_model_id)
100 : app_user_model_id_(app_user_model_id),
101 user_max_items_(0) {
102 // Retrieve the absolute path to "chrome.exe".
103 PathService::Get(base::FILE_EXE, &application_path_);
104 }
105
106 JumpListUpdater::~JumpListUpdater() {
107 }
108
109 // static
110 bool JumpListUpdater::IsEnabled() {
111 // JumpList is implemented only on Windows 7 or later.
112 return base::win::GetVersion() >= base::win::VERSION_WIN7;
113 }
114
115 bool JumpListUpdater::BeginUpdate() {
116 // This instance is expected to be one-time-use only.
117 DCHECK(!destination_list_.get());
118
119 // Check preconditions.
120 if (!JumpListUpdater::IsEnabled() || application_path_.empty() ||
121 app_user_model_id_.empty()) {
122 return false;
123 }
124
125 // Create an ICustomDestinationList object and attach it to our application.
126 HRESULT result = destination_list_.CreateInstance(CLSID_DestinationList, NULL,
127 CLSCTX_INPROC_SERVER);
128 if (FAILED(result))
129 return false;
130
131 // Set the App ID for this JumpList.
132 result = destination_list_->SetAppID(app_user_model_id_.c_str());
133 if (FAILED(result))
134 return false;
135
136 // Start a transaction that updates the JumpList of this application.
137 // This implementation just replaces the all items in this JumpList, so
138 // we don't have to use the IObjectArray object returned from this call.
139 // It seems Windows 7 RC (Build 7100) automatically checks the items in this
140 // removed list and prevent us from adding the same item.
141 UINT max_slots;
142 base::win::ScopedComPtr<IObjectArray> removed;
143 result = destination_list_->BeginList(&max_slots, __uuidof(*removed),
144 removed.ReceiveVoid());
145 if (FAILED(result))
146 return false;
147
148 user_max_items_ = max_slots;
149
150 return true;
151 }
152
153 bool JumpListUpdater::CommitUpdate() {
154 if (!destination_list_.get())
155 return false;
156
157 // Commit this transaction and send the updated JumpList to Windows.
158 return SUCCEEDED(destination_list_->CommitList());
159 }
160
161 bool JumpListUpdater::AddTasks(const ShellLinkItemList& link_items) {
162 if (!destination_list_.get())
163 return false;
164
165 // Create an EnumerableObjectCollection object to be added items of the
166 // "Task" category.
167 base::win::ScopedComPtr<IObjectCollection> collection;
168 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
169 NULL, CLSCTX_INPROC_SERVER);
170 if (FAILED(result))
171 return false;
172
173 // Add items to the "Task" category.
174 for (ShellLinkItemList::const_iterator it = link_items.begin();
175 it != link_items.end(); ++it) {
176 AddShellLink(collection, application_path_.value(), *it);
177 }
178
179 // We can now add the new list to the JumpList.
180 // ICustomDestinationList::AddUserTasks() also uses the IObjectArray
181 // interface to retrieve each item in the list. So, we retrieve the
182 // IObjectArray interface from the EnumerableObjectCollection object.
183 base::win::ScopedComPtr<IObjectArray> object_array;
184 result = collection.QueryInterface(object_array.Receive());
185 if (FAILED(result))
186 return false;
187
188 return SUCCEEDED(destination_list_->AddUserTasks(object_array));
189 }
190
191 bool JumpListUpdater::AddCustomCategory(const std::wstring& category_name,
192 const ShellLinkItemList& link_items,
193 size_t max_items) {
194 if (!destination_list_.get())
195 return false;
196
197 // Exit this function when the given vector does not contain any items
198 // because an ICustomDestinationList::AppendCategory() call fails in this
199 // case.
200 if (link_items.empty() || !max_items)
201 return true;
202
203 // Create an EnumerableObjectCollection object.
204 // We once add the given items to this collection object and add this
205 // collection to the JumpList.
206 base::win::ScopedComPtr<IObjectCollection> collection;
207 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
208 NULL, CLSCTX_INPROC_SERVER);
209 if (FAILED(result))
210 return false;
211
212 for (ShellLinkItemList::const_iterator item = link_items.begin();
213 item != link_items.end() && max_items > 0; ++item, --max_items) {
214 scoped_refptr<ShellLinkItem> link(*item);
215 AddShellLink(collection, application_path_.value(), link);
216 }
217
218 // We can now add the new list to the JumpList.
219 // The ICustomDestinationList::AppendCategory() function needs the
220 // IObjectArray interface to retrieve each item in the list. So, we retrive
221 // the IObjectArray interface from the IEnumerableObjectCollection object
222 // and use it.
223 // It seems the ICustomDestinationList::AppendCategory() function just
224 // replaces all items in the given category with the ones in the new list.
225 base::win::ScopedComPtr<IObjectArray> object_array;
226 result = collection.QueryInterface(object_array.Receive());
227 if (FAILED(result))
228 return false;
229
230 return SUCCEEDED(destination_list_->AppendCategory(category_name.c_str(),
231 object_array));
232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698