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

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: Minor changes after self-review 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 // ShellLinkItem
79
80 ShellLinkItem::ShellLinkItem()
81 : command_line_(CommandLine::NO_PROGRAM),
82 icon_index_(0) {
83 }
84
85 ShellLinkItem::~ShellLinkItem() {}
86
87 std::wstring ShellLinkItem::GetArguments() const {
88 return command_line_.GetArgumentsString();
89 }
90
91 void ShellLinkItem::AppendArgument(const std::wstring& value) {
92 command_line_.AppendArgNative(value);
93 }
94
95 void ShellLinkItem::AppendSwitch(const std::string& name,
96 const std::wstring& value) {
97 command_line_.AppendSwitchNative(name, value);
98 }
99
100 void ShellLinkItem::CopySwitchesFrom(const CommandLine& source,
101 const char* const switches[],
102 size_t count) {
103 command_line_.CopySwitchesFrom(source, switches, count);
104 }
105
106
107 // JumpListUpdater
108
109 JumpListUpdater::JumpListUpdater(const std::wstring& app_user_model_id)
110 : app_user_model_id_(app_user_model_id),
111 user_max_items_(0) {
112 // Retrieve the absolute path to "chrome.exe".
113 PathService::Get(base::FILE_EXE, &application_path_);
114 }
115
116 JumpListUpdater::~JumpListUpdater() {
117 }
118
119 // static
120 bool JumpListUpdater::IsEnabled() {
121 // JumpList is implemented only on Windows 7 or later.
122 return base::win::GetVersion() >= base::win::VERSION_WIN7;
123 }
124
125 bool JumpListUpdater::BeginUpdate() {
126 // This instance is expected to be one-time-use only.
127 DCHECK(!destination_list_.get());
128
129 // Check preconditions.
130 if (!JumpListUpdater::IsEnabled() || application_path_.empty() ||
131 app_user_model_id_.empty()) {
132 return false;
133 }
134
135 // Create an ICustomDestinationList object and attach it to our application.
136 HRESULT result = destination_list_.CreateInstance(CLSID_DestinationList, NULL,
137 CLSCTX_INPROC_SERVER);
138 if (FAILED(result))
139 return false;
140
141 // Set the App ID for this JumpList.
142 result = destination_list_->SetAppID(app_user_model_id_.c_str());
143 if (FAILED(result))
144 return false;
145
146 // Start a transaction that updates the JumpList of this application.
147 // This implementation just replaces the all items in this JumpList, so
148 // we don't have to use the IObjectArray object returned from this call.
149 // It seems Windows 7 RC (Build 7100) automatically checks the items in this
150 // removed list and prevent us from adding the same item.
151 base::win::ScopedComPtr<IObjectArray> removed;
152 result = destination_list_->BeginList(&user_max_items_, __uuidof(*removed),
153 removed.ReceiveVoid());
154 if (FAILED(result))
155 return false;
156
157 return true;
158 }
159
160 bool JumpListUpdater::CommitUpdate() {
161 if (!destination_list_.get())
162 return false;
163
164 // Commit this transaction and send the updated JumpList to Windows.
165 return SUCCEEDED(destination_list_->CommitList());
166 }
167
168 bool JumpListUpdater::AddTasks(const ShellLinkItemList& link_items) {
169 if (!destination_list_.get())
170 return false;
171
172 // Create an EnumerableObjectCollection object to be added items of the
173 // "Task" category.
174 base::win::ScopedComPtr<IObjectCollection> collection;
175 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
176 NULL, CLSCTX_INPROC_SERVER);
177 if (FAILED(result))
178 return false;
179
180 // Add items to the "Task" category.
181 for (ShellLinkItemList::const_iterator it = link_items.begin();
182 it != link_items.end(); ++it) {
183 AddShellLink(collection, application_path_.value(), *it);
184 }
185
186 // We can now add the new list to the JumpList.
187 // ICustomDestinationList::AddUserTasks() also uses the IObjectArray
188 // interface to retrieve each item in the list. So, we retrieve the
189 // IObjectArray interface from the EnumerableObjectCollection object.
190 base::win::ScopedComPtr<IObjectArray> object_array;
191 result = collection.QueryInterface(object_array.Receive());
192 if (FAILED(result))
193 return false;
194
195 return SUCCEEDED(destination_list_->AddUserTasks(object_array));
196 }
197
198 bool JumpListUpdater::AddCustomCategory(const std::wstring& category_name,
199 const ShellLinkItemList& link_items,
200 size_t max_items) {
201 if (!destination_list_.get())
202 return false;
203
204 // Exit this function when the given vector does not contain any items
205 // because an ICustomDestinationList::AppendCategory() call fails in this
206 // case.
207 if (link_items.empty() || !max_items)
208 return true;
209
210 // Create an EnumerableObjectCollection object.
211 // We once add the given items to this collection object and add this
212 // collection to the JumpList.
213 base::win::ScopedComPtr<IObjectCollection> collection;
214 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
215 NULL, CLSCTX_INPROC_SERVER);
216 if (FAILED(result))
217 return false;
218
219 for (ShellLinkItemList::const_iterator item = link_items.begin();
220 item != link_items.end() && max_items > 0; ++item, --max_items) {
221 scoped_refptr<ShellLinkItem> link(*item);
222 AddShellLink(collection, application_path_.value(), link);
223 }
224
225 // We can now add the new list to the JumpList.
226 // The ICustomDestinationList::AppendCategory() function needs the
227 // IObjectArray interface to retrieve each item in the list. So, we retrive
228 // the IObjectArray interface from the IEnumerableObjectCollection object
229 // and use it.
230 // It seems the ICustomDestinationList::AppendCategory() function just
231 // replaces all items in the given category with the ones in the new list.
232 base::win::ScopedComPtr<IObjectArray> object_array;
233 result = collection.QueryInterface(object_array.Receive());
234 if (FAILED(result))
235 return false;
236
237 return SUCCEEDED(destination_list_->AppendCategory(category_name.c_str(),
238 object_array));
239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698