OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/win/jumplist_updater.h" | 5 #include "chrome/browser/win/jumplist_updater.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <objbase.h> | 8 #include <objbase.h> |
9 #include <propkey.h> | 9 #include <propkey.h> |
10 #include <shobjidl.h> | 10 #include <shobjidl.h> |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 // Creates an IShellLink object. | 22 // Creates an IShellLink object. |
23 // An IShellLink object is almost the same as an application shortcut, and it | 23 // An IShellLink object is almost the same as an application shortcut, and it |
24 // requires three items: the absolute path to an application, an argument | 24 // requires three items: the absolute path to an application, an argument |
25 // string, and a title string. | 25 // string, and a title string. |
26 bool AddShellLink(base::win::ScopedComPtr<IObjectCollection> collection, | 26 bool AddShellLink(base::win::ScopedComPtr<IObjectCollection> collection, |
27 const base::string16& application_path, | 27 const base::string16& application_path, |
28 scoped_refptr<ShellLinkItem> item) { | 28 scoped_refptr<ShellLinkItem> item) { |
29 // Create an IShellLink object. | 29 // Create an IShellLink object. |
30 base::win::ScopedComPtr<IShellLink> link; | 30 base::win::ScopedComPtr<IShellLink> link; |
31 HRESULT result = link.CreateInstance(CLSID_ShellLink, NULL, | 31 HRESULT result = ::CoCreateInstance( |
32 CLSCTX_INPROC_SERVER); | 32 CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&link)); |
33 if (FAILED(result)) | 33 if (FAILED(result)) |
34 return false; | 34 return false; |
35 | 35 |
36 // Set the application path. | 36 // Set the application path. |
37 // We should exit this function when this call fails because it doesn't make | 37 // We should exit this function when this call fails because it doesn't make |
38 // any sense to add a shortcut that we cannot execute. | 38 // any sense to add a shortcut that we cannot execute. |
39 result = link->SetPath(application_path.c_str()); | 39 result = link->SetPath(application_path.c_str()); |
40 if (FAILED(result)) | 40 if (FAILED(result)) |
41 return false; | 41 return false; |
42 | 42 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplistUpdater.BeginUpdateDuration"); | 118 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplistUpdater.BeginUpdateDuration"); |
119 | 119 |
120 // This instance is expected to be one-time-use only. | 120 // This instance is expected to be one-time-use only. |
121 DCHECK(!destination_list_.Get()); | 121 DCHECK(!destination_list_.Get()); |
122 | 122 |
123 // Check preconditions. | 123 // Check preconditions. |
124 if (!JumpListUpdater::IsEnabled() || app_user_model_id_.empty()) | 124 if (!JumpListUpdater::IsEnabled() || app_user_model_id_.empty()) |
125 return false; | 125 return false; |
126 | 126 |
127 // Create an ICustomDestinationList object and attach it to our application. | 127 // Create an ICustomDestinationList object and attach it to our application. |
128 HRESULT result = destination_list_.CreateInstance(CLSID_DestinationList, NULL, | 128 HRESULT result = |
129 CLSCTX_INPROC_SERVER); | 129 ::CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER, |
| 130 IID_PPV_ARGS(&destination_list_)); |
130 if (FAILED(result)) | 131 if (FAILED(result)) |
131 return false; | 132 return false; |
132 | 133 |
133 // Set the App ID for this JumpList. | 134 // Set the App ID for this JumpList. |
134 result = destination_list_->SetAppID(app_user_model_id_.c_str()); | 135 result = destination_list_->SetAppID(app_user_model_id_.c_str()); |
135 if (FAILED(result)) | 136 if (FAILED(result)) |
136 return false; | 137 return false; |
137 | 138 |
138 // Start a transaction that updates the JumpList of this application. | 139 // Start a transaction that updates the JumpList of this application. |
139 // This implementation just replaces the all items in this JumpList, so | 140 // This implementation just replaces the all items in this JumpList, so |
(...skipping 30 matching lines...) Expand all Loading... |
170 return false; | 171 return false; |
171 | 172 |
172 // Retrieve the absolute path to "chrome.exe". | 173 // Retrieve the absolute path to "chrome.exe". |
173 base::FilePath application_path; | 174 base::FilePath application_path; |
174 if (!PathService::Get(base::FILE_EXE, &application_path)) | 175 if (!PathService::Get(base::FILE_EXE, &application_path)) |
175 return false; | 176 return false; |
176 | 177 |
177 // Create an EnumerableObjectCollection object to be added items of the | 178 // Create an EnumerableObjectCollection object to be added items of the |
178 // "Task" category. | 179 // "Task" category. |
179 base::win::ScopedComPtr<IObjectCollection> collection; | 180 base::win::ScopedComPtr<IObjectCollection> collection; |
180 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection, | 181 HRESULT result = |
181 NULL, CLSCTX_INPROC_SERVER); | 182 ::CoCreateInstance(CLSID_EnumerableObjectCollection, NULL, |
| 183 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&collection)); |
182 if (FAILED(result)) | 184 if (FAILED(result)) |
183 return false; | 185 return false; |
184 | 186 |
185 // Add items to the "Task" category. | 187 // Add items to the "Task" category. |
186 for (ShellLinkItemList::const_iterator it = link_items.begin(); | 188 for (ShellLinkItemList::const_iterator it = link_items.begin(); |
187 it != link_items.end(); ++it) { | 189 it != link_items.end(); ++it) { |
188 if (!AddShellLink(collection, application_path.value(), *it)) | 190 if (!AddShellLink(collection, application_path.value(), *it)) |
189 return false; | 191 return false; |
190 } | 192 } |
191 | 193 |
(...skipping 26 matching lines...) Expand all Loading... |
218 // Exit this function when the given vector does not contain any items | 220 // Exit this function when the given vector does not contain any items |
219 // because an ICustomDestinationList::AppendCategory() call fails in this | 221 // because an ICustomDestinationList::AppendCategory() call fails in this |
220 // case. | 222 // case. |
221 if (link_items.empty() || !max_items) | 223 if (link_items.empty() || !max_items) |
222 return true; | 224 return true; |
223 | 225 |
224 // Create an EnumerableObjectCollection object. | 226 // Create an EnumerableObjectCollection object. |
225 // We once add the given items to this collection object and add this | 227 // We once add the given items to this collection object and add this |
226 // collection to the JumpList. | 228 // collection to the JumpList. |
227 base::win::ScopedComPtr<IObjectCollection> collection; | 229 base::win::ScopedComPtr<IObjectCollection> collection; |
228 HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection, | 230 HRESULT result = |
229 NULL, CLSCTX_INPROC_SERVER); | 231 ::CoCreateInstance(CLSID_EnumerableObjectCollection, NULL, |
| 232 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&collection)); |
230 if (FAILED(result)) | 233 if (FAILED(result)) |
231 return false; | 234 return false; |
232 | 235 |
233 for (ShellLinkItemList::const_iterator item = link_items.begin(); | 236 for (ShellLinkItemList::const_iterator item = link_items.begin(); |
234 item != link_items.end() && max_items > 0; ++item, --max_items) { | 237 item != link_items.end() && max_items > 0; ++item, --max_items) { |
235 if (!AddShellLink(collection, application_path.value(), *item)) | 238 if (!AddShellLink(collection, application_path.value(), *item)) |
236 return false; | 239 return false; |
237 } | 240 } |
238 | 241 |
239 // We can now add the new list to the JumpList. | 242 // We can now add the new list to the JumpList. |
240 // The ICustomDestinationList::AppendCategory() function needs the | 243 // The ICustomDestinationList::AppendCategory() function needs the |
241 // IObjectArray interface to retrieve each item in the list. So, we retrive | 244 // IObjectArray interface to retrieve each item in the list. So, we retrive |
242 // the IObjectArray interface from the IEnumerableObjectCollection object | 245 // the IObjectArray interface from the IEnumerableObjectCollection object |
243 // and use it. | 246 // and use it. |
244 // It seems the ICustomDestinationList::AppendCategory() function just | 247 // It seems the ICustomDestinationList::AppendCategory() function just |
245 // replaces all items in the given category with the ones in the new list. | 248 // replaces all items in the given category with the ones in the new list. |
246 base::win::ScopedComPtr<IObjectArray> object_array; | 249 base::win::ScopedComPtr<IObjectArray> object_array; |
247 result = collection.CopyTo(object_array.GetAddressOf()); | 250 result = collection.CopyTo(object_array.GetAddressOf()); |
248 if (FAILED(result)) | 251 if (FAILED(result)) |
249 return false; | 252 return false; |
250 | 253 |
251 return SUCCEEDED(destination_list_->AppendCategory(category_name.c_str(), | 254 return SUCCEEDED(destination_list_->AppendCategory(category_name.c_str(), |
252 object_array.Get())); | 255 object_array.Get())); |
253 } | 256 } |
OLD | NEW |