jump_list.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, MAX_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(shell_link->GetIconLocation(path, MAX_PATH, &icon_index))) {
  82. item->icon_path = base::FilePath(path);
  83. item->icon_index = icon_index;
  84. }
  85. wchar_t item_desc[INFOTIPSIZE];
  86. if (SUCCEEDED(shell_link->GetDescription(item_desc, INFOTIPSIZE)))
  87. item->description = item_desc;
  88. return true;
  89. }
  90. // Convert IObjectArray of IShellLink & IShellItem to std::vector.
  91. void ConvertRemovedJumpListItems(IObjectArray* in,
  92. std::vector<JumpListItem>* out) {
  93. DCHECK(in);
  94. DCHECK(out);
  95. UINT removed_count;
  96. if (SUCCEEDED(in->GetCount(&removed_count) && (removed_count > 0))) {
  97. out->reserve(removed_count);
  98. JumpListItem item;
  99. IShellItem* shell_item;
  100. IShellLink* shell_link;
  101. for (UINT i = 0; i < removed_count; ++i) {
  102. if (SUCCEEDED(in->GetAt(i, IID_PPV_ARGS(&shell_item)))) {
  103. item.type = JumpListItem::Type::FILE;
  104. GetShellItemFileName(shell_item, &item.path);
  105. out->push_back(item);
  106. shell_item->Release();
  107. } else if (SUCCEEDED(in->GetAt(i, IID_PPV_ARGS(&shell_link)))) {
  108. if (ConvertShellLinkToJumpListItem(shell_link, &item))
  109. out->push_back(item);
  110. shell_link->Release();
  111. }
  112. }
  113. }
  114. }
  115. } // namespace
  116. namespace atom {
  117. JumpListItem::JumpListItem() = default;
  118. JumpListItem::JumpListItem(const JumpListItem&) = default;
  119. JumpListItem::~JumpListItem() = default;
  120. JumpListCategory::JumpListCategory() = default;
  121. JumpListCategory::JumpListCategory(const JumpListCategory&) = default;
  122. JumpListCategory::~JumpListCategory() = default;
  123. JumpList::JumpList(const base::string16& app_id) : app_id_(app_id) {
  124. destinations_.CoCreateInstance(CLSID_DestinationList);
  125. }
  126. JumpList::~JumpList() = default;
  127. bool JumpList::Begin(int* min_items, std::vector<JumpListItem>* removed_items) {
  128. DCHECK(destinations_);
  129. if (!destinations_)
  130. return false;
  131. if (FAILED(destinations_->SetAppID(app_id_.c_str())))
  132. return false;
  133. UINT min_slots;
  134. CComPtr<IObjectArray> removed;
  135. if (FAILED(destinations_->BeginList(&min_slots, IID_PPV_ARGS(&removed))))
  136. return false;
  137. if (min_items)
  138. *min_items = min_slots;
  139. if (removed_items)
  140. ConvertRemovedJumpListItems(removed, removed_items);
  141. return true;
  142. }
  143. bool JumpList::Abort() {
  144. DCHECK(destinations_);
  145. if (!destinations_)
  146. return false;
  147. return SUCCEEDED(destinations_->AbortList());
  148. }
  149. bool JumpList::Commit() {
  150. DCHECK(destinations_);
  151. if (!destinations_)
  152. return false;
  153. return SUCCEEDED(destinations_->CommitList());
  154. }
  155. bool JumpList::Delete() {
  156. DCHECK(destinations_);
  157. if (!destinations_)
  158. return false;
  159. return SUCCEEDED(destinations_->DeleteList(app_id_.c_str()));
  160. }
  161. // This method will attempt to append as many items to the Jump List as
  162. // possible, and will return a single error code even if multiple things
  163. // went wrong in the process. To get detailed information about what went
  164. // wrong enable runtime logging.
  165. JumpListResult JumpList::AppendCategory(const JumpListCategory& category) {
  166. DCHECK(destinations_);
  167. if (!destinations_)
  168. return JumpListResult::GENERIC_ERROR;
  169. if (category.items.empty())
  170. return JumpListResult::SUCCESS;
  171. CComPtr<IObjectCollection> collection;
  172. if (FAILED(collection.CoCreateInstance(CLSID_EnumerableObjectCollection))) {
  173. return JumpListResult::GENERIC_ERROR;
  174. }
  175. auto result = JumpListResult::SUCCESS;
  176. // Keep track of how many items were actually appended to the category.
  177. int appended_count = 0;
  178. for (const auto& item : category.items) {
  179. switch (item.type) {
  180. case JumpListItem::Type::TASK:
  181. if (AppendTask(item, collection))
  182. ++appended_count;
  183. else
  184. LOG(ERROR) << "Failed to append task '" << item.title
  185. << "' "
  186. "to Jump List.";
  187. break;
  188. case JumpListItem::Type::SEPARATOR:
  189. if (category.type == JumpListCategory::Type::TASKS) {
  190. if (AppendSeparator(collection))
  191. ++appended_count;
  192. } else {
  193. LOG(ERROR) << "Can't append separator to Jump List category "
  194. << "'" << category.name << "'. "
  195. << "Separators are only allowed in the standard 'Tasks' "
  196. "Jump List category.";
  197. result = JumpListResult::CUSTOM_CATEGORY_SEPARATOR_ERROR;
  198. }
  199. break;
  200. case JumpListItem::Type::FILE:
  201. if (AppendFile(item, collection))
  202. ++appended_count;
  203. else
  204. LOG(ERROR) << "Failed to append '" << item.path.value()
  205. << "' "
  206. "to Jump List.";
  207. break;
  208. }
  209. }
  210. if (appended_count == 0)
  211. return result;
  212. if ((static_cast<size_t>(appended_count) < category.items.size()) &&
  213. (result == JumpListResult::SUCCESS)) {
  214. result = JumpListResult::GENERIC_ERROR;
  215. }
  216. CComQIPtr<IObjectArray> items(collection);
  217. if (category.type == JumpListCategory::Type::TASKS) {
  218. if (FAILED(destinations_->AddUserTasks(items))) {
  219. LOG(ERROR) << "Failed to append items to the standard Tasks category.";
  220. if (result == JumpListResult::SUCCESS)
  221. result = JumpListResult::GENERIC_ERROR;
  222. }
  223. } else {
  224. HRESULT hr = destinations_->AppendCategory(category.name.c_str(), items);
  225. if (FAILED(hr)) {
  226. if (hr == static_cast<HRESULT>(0x80040F03)) {
  227. LOG(ERROR) << "Failed to append custom category "
  228. << "'" << category.name << "' "
  229. << "to Jump List due to missing file type registration.";
  230. result = JumpListResult::MISSING_FILE_TYPE_REGISTRATION_ERROR;
  231. } else if (hr == E_ACCESSDENIED) {
  232. LOG(ERROR) << "Failed to append custom category "
  233. << "'" << category.name << "' "
  234. << "to Jump List due to system privacy settings.";
  235. result = JumpListResult::CUSTOM_CATEGORY_ACCESS_DENIED_ERROR;
  236. } else {
  237. LOG(ERROR) << "Failed to append custom category "
  238. << "'" << category.name << "' to Jump List.";
  239. if (result == JumpListResult::SUCCESS)
  240. result = JumpListResult::GENERIC_ERROR;
  241. }
  242. }
  243. }
  244. return result;
  245. }
  246. // This method will attempt to append as many categories to the Jump List
  247. // as possible, and will return a single error code even if multiple things
  248. // went wrong in the process. To get detailed information about what went
  249. // wrong enable runtime logging.
  250. JumpListResult JumpList::AppendCategories(
  251. const std::vector<JumpListCategory>& categories) {
  252. DCHECK(destinations_);
  253. if (!destinations_)
  254. return JumpListResult::GENERIC_ERROR;
  255. auto result = JumpListResult::SUCCESS;
  256. for (const auto& category : categories) {
  257. auto latestResult = JumpListResult::SUCCESS;
  258. switch (category.type) {
  259. case JumpListCategory::Type::TASKS:
  260. case JumpListCategory::Type::CUSTOM:
  261. latestResult = AppendCategory(category);
  262. break;
  263. case JumpListCategory::Type::RECENT:
  264. if (FAILED(destinations_->AppendKnownCategory(KDC_RECENT))) {
  265. LOG(ERROR) << "Failed to append Recent category to Jump List.";
  266. latestResult = JumpListResult::GENERIC_ERROR;
  267. }
  268. break;
  269. case JumpListCategory::Type::FREQUENT:
  270. if (FAILED(destinations_->AppendKnownCategory(KDC_FREQUENT))) {
  271. LOG(ERROR) << "Failed to append Frequent category to Jump List.";
  272. latestResult = JumpListResult::GENERIC_ERROR;
  273. }
  274. break;
  275. }
  276. // Keep the first non-generic error code as only one can be returned from
  277. // the function (so try to make it the most useful one).
  278. if (((result == JumpListResult::SUCCESS) ||
  279. (result == JumpListResult::GENERIC_ERROR)) &&
  280. (latestResult != JumpListResult::SUCCESS))
  281. result = latestResult;
  282. }
  283. return result;
  284. }
  285. } // namespace atom