jump_list.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/win/jump_list.h"
  5. #include <propkey.h> // for PKEY_* constants
  6. #include "base/win/scoped_co_mem.h"
  7. #include "base/win/scoped_propvariant.h"
  8. #include "base/win/win_util.h"
  9. namespace {
  10. using atom::JumpListCategory;
  11. using atom::JumpListItem;
  12. using atom::JumpListResult;
  13. bool AppendTask(const JumpListItem& item, IObjectCollection* collection) {
  14. DCHECK(collection);
  15. CComPtr<IShellLink> link;
  16. if (FAILED(link.CoCreateInstance(CLSID_ShellLink)) ||
  17. FAILED(link->SetPath(item.path.value().c_str())) ||
  18. FAILED(link->SetArguments(item.arguments.c_str())) ||
  19. FAILED(link->SetDescription(item.description.c_str())))
  20. return false;
  21. if (!item.icon_path.empty() &&
  22. FAILED(link->SetIconLocation(item.icon_path.value().c_str(),
  23. item.icon_index)))
  24. return false;
  25. CComQIPtr<IPropertyStore> property_store(link);
  26. if (!base::win::SetStringValueForPropertyStore(property_store, PKEY_Title,
  27. item.title.c_str()))
  28. return false;
  29. return SUCCEEDED(collection->AddObject(link));
  30. }
  31. bool AppendSeparator(IObjectCollection* collection) {
  32. DCHECK(collection);
  33. CComPtr<IShellLink> shell_link;
  34. if (SUCCEEDED(shell_link.CoCreateInstance(CLSID_ShellLink))) {
  35. CComQIPtr<IPropertyStore> property_store(shell_link);
  36. if (base::win::SetBooleanValueForPropertyStore(
  37. property_store, PKEY_AppUserModel_IsDestListSeparator, true))
  38. return SUCCEEDED(collection->AddObject(shell_link));
  39. }
  40. return false;
  41. }
  42. bool AppendFile(const JumpListItem& item, IObjectCollection* collection) {
  43. DCHECK(collection);
  44. CComPtr<IShellItem> file;
  45. if (SUCCEEDED(SHCreateItemFromParsingName(item.path.value().c_str(), NULL,
  46. IID_PPV_ARGS(&file))))
  47. return SUCCEEDED(collection->AddObject(file));
  48. return false;
  49. }
  50. bool GetShellItemFileName(IShellItem* shell_item, base::FilePath* file_name) {
  51. DCHECK(shell_item);
  52. DCHECK(file_name);
  53. base::win::ScopedCoMem<wchar_t> file_name_buffer;
  54. if (SUCCEEDED(
  55. shell_item->GetDisplayName(SIGDN_FILESYSPATH, &file_name_buffer))) {
  56. *file_name = base::FilePath(file_name_buffer.get());
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool ConvertShellLinkToJumpListItem(IShellLink* shell_link,
  62. JumpListItem* item) {
  63. DCHECK(shell_link);
  64. DCHECK(item);
  65. item->type = JumpListItem::Type::TASK;
  66. wchar_t path[MAX_PATH];
  67. if (FAILED(shell_link->GetPath(path, arraysize(path), nullptr, 0)))
  68. return false;
  69. CComQIPtr<IPropertyStore> property_store = shell_link;
  70. base::win::ScopedPropVariant prop;
  71. if (SUCCEEDED(
  72. property_store->GetValue(PKEY_Link_Arguments, prop.Receive())) &&
  73. (prop.get().vt == VT_LPWSTR)) {
  74. item->arguments = prop.get().pwszVal;
  75. }
  76. if (SUCCEEDED(property_store->GetValue(PKEY_Title, prop.Receive())) &&
  77. (prop.get().vt == VT_LPWSTR)) {
  78. item->title = prop.get().pwszVal;
  79. }
  80. int icon_index;
  81. if (SUCCEEDED(
  82. shell_link->GetIconLocation(path, arraysize(path), &icon_index))) {
  83. item->icon_path = base::FilePath(path);
  84. item->icon_index = icon_index;
  85. }
  86. wchar_t item_desc[INFOTIPSIZE];
  87. if (SUCCEEDED(shell_link->GetDescription(item_desc, arraysize(item_desc))))
  88. item->description = item_desc;
  89. return true;
  90. }
  91. // Convert IObjectArray of IShellLink & IShellItem to std::vector.
  92. void ConvertRemovedJumpListItems(IObjectArray* in,
  93. std::vector<JumpListItem>* out) {
  94. DCHECK(in);
  95. DCHECK(out);
  96. UINT removed_count;
  97. if (SUCCEEDED(in->GetCount(&removed_count) && (removed_count > 0))) {
  98. out->reserve(removed_count);
  99. JumpListItem item;
  100. IShellItem* shell_item;
  101. IShellLink* shell_link;
  102. for (UINT i = 0; i < removed_count; ++i) {
  103. if (SUCCEEDED(in->GetAt(i, IID_PPV_ARGS(&shell_item)))) {
  104. item.type = JumpListItem::Type::FILE;
  105. GetShellItemFileName(shell_item, &item.path);
  106. out->push_back(item);
  107. shell_item->Release();
  108. } else if (SUCCEEDED(in->GetAt(i, IID_PPV_ARGS(&shell_link)))) {
  109. if (ConvertShellLinkToJumpListItem(shell_link, &item))
  110. out->push_back(item);
  111. shell_link->Release();
  112. }
  113. }
  114. }
  115. }
  116. } // namespace
  117. namespace atom {
  118. JumpList::JumpList(const base::string16& app_id) : app_id_(app_id) {
  119. destinations_.CoCreateInstance(CLSID_DestinationList);
  120. }
  121. bool JumpList::Begin(int* min_items, std::vector<JumpListItem>* removed_items) {
  122. DCHECK(destinations_);
  123. if (!destinations_)
  124. return false;
  125. if (FAILED(destinations_->SetAppID(app_id_.c_str())))
  126. return false;
  127. UINT min_slots;
  128. CComPtr<IObjectArray> removed;
  129. if (FAILED(destinations_->BeginList(&min_slots, IID_PPV_ARGS(&removed))))
  130. return false;
  131. if (min_items)
  132. *min_items = min_slots;
  133. if (removed_items)
  134. ConvertRemovedJumpListItems(removed, removed_items);
  135. return true;
  136. }
  137. bool JumpList::Abort() {
  138. DCHECK(destinations_);
  139. if (!destinations_)
  140. return false;
  141. return SUCCEEDED(destinations_->AbortList());
  142. }
  143. bool JumpList::Commit() {
  144. DCHECK(destinations_);
  145. if (!destinations_)
  146. return false;
  147. return SUCCEEDED(destinations_->CommitList());
  148. }
  149. bool JumpList::Delete() {
  150. DCHECK(destinations_);
  151. if (!destinations_)
  152. return false;
  153. return SUCCEEDED(destinations_->DeleteList(app_id_.c_str()));
  154. }
  155. // This method will attempt to append as many items to the Jump List as
  156. // possible, and will return a single error code even if multiple things
  157. // went wrong in the process. To get detailed information about what went
  158. // wrong enable runtime logging.
  159. JumpListResult JumpList::AppendCategory(const JumpListCategory& category) {
  160. DCHECK(destinations_);
  161. if (!destinations_)
  162. return JumpListResult::GENERIC_ERROR;
  163. if (category.items.empty())
  164. return JumpListResult::SUCCESS;
  165. CComPtr<IObjectCollection> collection;
  166. if (FAILED(collection.CoCreateInstance(CLSID_EnumerableObjectCollection))) {
  167. return JumpListResult::GENERIC_ERROR;
  168. }
  169. auto result = JumpListResult::SUCCESS;
  170. // Keep track of how many items were actually appended to the category.
  171. int appended_count = 0;
  172. for (const auto& item : category.items) {
  173. switch (item.type) {
  174. case JumpListItem::Type::TASK:
  175. if (AppendTask(item, collection))
  176. ++appended_count;
  177. else
  178. LOG(ERROR) << "Failed to append task '" << item.title
  179. << "' "
  180. "to Jump List.";
  181. break;
  182. case JumpListItem::Type::SEPARATOR:
  183. if (category.type == JumpListCategory::Type::TASKS) {
  184. if (AppendSeparator(collection))
  185. ++appended_count;
  186. } else {
  187. LOG(ERROR) << "Can't append separator to Jump List category "
  188. << "'" << category.name << "'. "
  189. << "Separators are only allowed in the standard 'Tasks' "
  190. "Jump List category.";
  191. result = JumpListResult::CUSTOM_CATEGORY_SEPARATOR_ERROR;
  192. }
  193. break;
  194. case JumpListItem::Type::FILE:
  195. if (AppendFile(item, collection))
  196. ++appended_count;
  197. else
  198. LOG(ERROR) << "Failed to append '" << item.path.value()
  199. << "' "
  200. "to Jump List.";
  201. break;
  202. }
  203. }
  204. if (appended_count == 0)
  205. return result;
  206. if ((static_cast<size_t>(appended_count) < category.items.size()) &&
  207. (result == JumpListResult::SUCCESS)) {
  208. result = JumpListResult::GENERIC_ERROR;
  209. }
  210. CComQIPtr<IObjectArray> items(collection);
  211. if (category.type == JumpListCategory::Type::TASKS) {
  212. if (FAILED(destinations_->AddUserTasks(items))) {
  213. LOG(ERROR) << "Failed to append items to the standard Tasks category.";
  214. if (result == JumpListResult::SUCCESS)
  215. result = JumpListResult::GENERIC_ERROR;
  216. }
  217. } else {
  218. auto hr = destinations_->AppendCategory(category.name.c_str(), items);
  219. if (FAILED(hr)) {
  220. if (hr == 0x80040F03) {
  221. LOG(ERROR) << "Failed to append custom category "
  222. << "'" << category.name << "' "
  223. << "to Jump List due to missing file type registration.";
  224. result = JumpListResult::MISSING_FILE_TYPE_REGISTRATION_ERROR;
  225. } else if (hr == E_ACCESSDENIED) {
  226. LOG(ERROR) << "Failed to append custom category "
  227. << "'" << category.name << "' "
  228. << "to Jump List due to system privacy settings.";
  229. result = JumpListResult::CUSTOM_CATEGORY_ACCESS_DENIED_ERROR;
  230. } else {
  231. LOG(ERROR) << "Failed to append custom category "
  232. << "'" << category.name << "' to Jump List.";
  233. if (result == JumpListResult::SUCCESS)
  234. result = JumpListResult::GENERIC_ERROR;
  235. }
  236. }
  237. }
  238. return result;
  239. }
  240. // This method will attempt to append as many categories to the Jump List
  241. // as possible, and will return a single error code even if multiple things
  242. // went wrong in the process. To get detailed information about what went
  243. // wrong enable runtime logging.
  244. JumpListResult JumpList::AppendCategories(
  245. const std::vector<JumpListCategory>& categories) {
  246. DCHECK(destinations_);
  247. if (!destinations_)
  248. return JumpListResult::GENERIC_ERROR;
  249. auto result = JumpListResult::SUCCESS;
  250. for (const auto& category : categories) {
  251. auto latestResult = JumpListResult::SUCCESS;
  252. switch (category.type) {
  253. case JumpListCategory::Type::TASKS:
  254. case JumpListCategory::Type::CUSTOM:
  255. latestResult = AppendCategory(category);
  256. break;
  257. case JumpListCategory::Type::RECENT:
  258. if (FAILED(destinations_->AppendKnownCategory(KDC_RECENT))) {
  259. LOG(ERROR) << "Failed to append Recent category to Jump List.";
  260. latestResult = JumpListResult::GENERIC_ERROR;
  261. }
  262. break;
  263. case JumpListCategory::Type::FREQUENT:
  264. if (FAILED(destinations_->AppendKnownCategory(KDC_FREQUENT))) {
  265. LOG(ERROR) << "Failed to append Frequent category to Jump List.";
  266. latestResult = JumpListResult::GENERIC_ERROR;
  267. }
  268. break;
  269. }
  270. // Keep the first non-generic error code as only one can be returned from
  271. // the function (so try to make it the most useful one).
  272. if (((result == JumpListResult::SUCCESS) ||
  273. (result == JumpListResult::GENERIC_ERROR)) &&
  274. (latestResult != JumpListResult::SUCCESS))
  275. result = latestResult;
  276. }
  277. return result;
  278. }
  279. } // namespace atom