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

Unified 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: Addressed review comments 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/jumplist_updater_win.cc
diff --git a/chrome/browser/jumplist_updater_win.cc b/chrome/browser/jumplist_updater_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..96ccddb8e81d5af4836e24645d4dbafe62b7d385
--- /dev/null
+++ b/chrome/browser/jumplist_updater_win.cc
@@ -0,0 +1,232 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/jumplist_updater_win.h"
+
+#include <windows.h>
+#include <propkey.h>
+#include <shobjidl.h>
+
+#include "base/path_service.h"
+#include "base/win/win_util.h"
+#include "base/win/windows_version.h"
+
+namespace {
+
+// Creates an IShellLink object.
+// An IShellLink object is almost the same as an application shortcut, and it
+// requires three items: the absolute path to an application, an argument
+// string, and a title string.
+bool AddShellLink(base::win::ScopedComPtr<IObjectCollection> collection,
+ const std::wstring& application_path,
+ scoped_refptr<ShellLinkItem> item) {
+ // Create an IShellLink object.
+ base::win::ScopedComPtr<IShellLink> link;
+ HRESULT result = link.CreateInstance(CLSID_ShellLink, NULL,
+ CLSCTX_INPROC_SERVER);
+ if (FAILED(result))
+ return false;
+
+ // Set the application path.
+ // We should exit this function when this call fails because it doesn't make
+ // any sense to add a shortcut that we cannot execute.
+ result = link->SetPath(application_path.c_str());
+ if (FAILED(result))
+ return false;
+
+ // Attach the command-line switches of this process before the given
+ // arguments and set it as the arguments of this IShellLink object.
+ // We also exit this function when this call fails because it isn't useful to
+ // add a shortcut that cannot open the given page.
+ std::wstring arguments(item->GetArguments());
+ if (!arguments.empty()) {
+ result = link->SetArguments(arguments.c_str());
+ if (FAILED(result))
+ return false;
+ }
+
+ // Attach the given icon path to this IShellLink object.
+ // Since an icon is an optional item for an IShellLink object, so we don't
+ // have to exit even when it fails.
+ if (!item->icon_path().empty())
+ link->SetIconLocation(item->icon_path().c_str(), item->icon_index());
+
+ // Set the title of the IShellLink object.
+ // The IShellLink interface does not have any functions which update its
+ // title because this interface is originally for creating an application
+ // shortcut which doesn't have titles.
+ // So, we should use the IPropertyStore interface to set its title.
+ base::win::ScopedComPtr<IPropertyStore> property_store;
+ result = link.QueryInterface(property_store.Receive());
+ if (FAILED(result))
+ return false;
+
+ if (!base::win::SetStringValueForPropertyStore(
+ property_store.get(),
+ PKEY_Title,
+ item->title().c_str())) {
+ return false;
+ }
+
+ // Add this IShellLink object to the given collection.
+ return SUCCEEDED(collection->AddObject(link));
+}
+
+} // namespace
+
+
+// ShellLinkItem
+
+ShellLinkItem::ShellLinkItem()
+ : command_line_(CommandLine::NO_PROGRAM),
+ icon_index_(0) {
+}
+
+ShellLinkItem::~ShellLinkItem() {}
+
+std::wstring ShellLinkItem::GetArguments() const {
+ return command_line_.GetArgumentsString();
+}
+
+CommandLine* ShellLinkItem::GetCommandLine() {
+ return &command_line_;
+}
+
+
+// JumpListUpdater
+
+JumpListUpdater::JumpListUpdater(const std::wstring& app_user_model_id)
+ : app_user_model_id_(app_user_model_id),
+ user_max_items_(0) {
+ // Retrieve the absolute path to "chrome.exe".
+ PathService::Get(base::FILE_EXE, &application_path_);
+}
+
+JumpListUpdater::~JumpListUpdater() {
+}
+
+// static
+bool JumpListUpdater::IsEnabled() {
+ // JumpList is implemented only on Windows 7 or later.
+ return base::win::GetVersion() >= base::win::VERSION_WIN7;
+}
+
+bool JumpListUpdater::BeginUpdate() {
+ // This instance is expected to be one-time-use only.
+ DCHECK(!destination_list_.get());
+
+ // Check preconditions.
+ if (!JumpListUpdater::IsEnabled() || application_path_.empty() ||
+ app_user_model_id_.empty()) {
+ return false;
+ }
+
+ // Create an ICustomDestinationList object and attach it to our application.
+ HRESULT result = destination_list_.CreateInstance(CLSID_DestinationList, NULL,
+ CLSCTX_INPROC_SERVER);
+ if (FAILED(result))
+ return false;
+
+ // Set the App ID for this JumpList.
+ result = destination_list_->SetAppID(app_user_model_id_.c_str());
+ if (FAILED(result))
+ return false;
+
+ // Start a transaction that updates the JumpList of this application.
+ // This implementation just replaces the all items in this JumpList, so
+ // we don't have to use the IObjectArray object returned from this call.
+ // It seems Windows 7 RC (Build 7100) automatically checks the items in this
+ // removed list and prevent us from adding the same item.
+ UINT max_slots;
+ base::win::ScopedComPtr<IObjectArray> removed;
+ result = destination_list_->BeginList(&max_slots, __uuidof(*removed),
+ removed.ReceiveVoid());
+ if (FAILED(result))
+ return false;
+
+ user_max_items_ = max_slots;
+
+ return true;
+}
+
+bool JumpListUpdater::CommitUpdate() {
+ if (!destination_list_.get())
+ return false;
+
+ // Commit this transaction and send the updated JumpList to Windows.
+ return SUCCEEDED(destination_list_->CommitList());
+}
+
+bool JumpListUpdater::AddTasks(const ShellLinkItemList& link_items) {
+ if (!destination_list_.get())
+ return false;
+
+ // Create an EnumerableObjectCollection object to be added items of the
+ // "Task" category.
+ base::win::ScopedComPtr<IObjectCollection> collection;
+ HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
+ NULL, CLSCTX_INPROC_SERVER);
+ if (FAILED(result))
+ return false;
+
+ // Add items to the "Task" category.
+ for (ShellLinkItemList::const_iterator it = link_items.begin();
+ it != link_items.end(); ++it) {
+ AddShellLink(collection, application_path_.value(), *it);
+ }
+
+ // We can now add the new list to the JumpList.
+ // ICustomDestinationList::AddUserTasks() also uses the IObjectArray
+ // interface to retrieve each item in the list. So, we retrieve the
+ // IObjectArray interface from the EnumerableObjectCollection object.
+ base::win::ScopedComPtr<IObjectArray> object_array;
+ result = collection.QueryInterface(object_array.Receive());
+ if (FAILED(result))
+ return false;
+
+ return SUCCEEDED(destination_list_->AddUserTasks(object_array));
+}
+
+bool JumpListUpdater::AddCustomCategory(const std::wstring& category_name,
+ const ShellLinkItemList& link_items,
+ size_t max_items) {
+ if (!destination_list_.get())
+ return false;
+
+ // Exit this function when the given vector does not contain any items
+ // because an ICustomDestinationList::AppendCategory() call fails in this
+ // case.
+ if (link_items.empty() || !max_items)
+ return true;
+
+ // Create an EnumerableObjectCollection object.
+ // We once add the given items to this collection object and add this
+ // collection to the JumpList.
+ base::win::ScopedComPtr<IObjectCollection> collection;
+ HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
+ NULL, CLSCTX_INPROC_SERVER);
+ if (FAILED(result))
+ return false;
+
+ for (ShellLinkItemList::const_iterator item = link_items.begin();
+ item != link_items.end() && max_items > 0; ++item, --max_items) {
+ scoped_refptr<ShellLinkItem> link(*item);
+ AddShellLink(collection, application_path_.value(), link);
+ }
+
+ // We can now add the new list to the JumpList.
+ // The ICustomDestinationList::AppendCategory() function needs the
+ // IObjectArray interface to retrieve each item in the list. So, we retrive
+ // the IObjectArray interface from the IEnumerableObjectCollection object
+ // and use it.
+ // It seems the ICustomDestinationList::AppendCategory() function just
+ // replaces all items in the given category with the ones in the new list.
+ base::win::ScopedComPtr<IObjectArray> object_array;
+ result = collection.QueryInterface(object_array.Receive());
+ if (FAILED(result))
+ return false;
+
+ return SUCCEEDED(destination_list_->AppendCategory(category_name.c_str(),
+ object_array));
+}

Powered by Google App Engine
This is Rietveld 408576698