jump_list.cc 11 KB

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