This source file includes following definitions.
- AddShellLink
- icon_index_
- GetArguments
- GetCommandLine
- user_max_items_
- IsEnabled
- BeginUpdate
- CommitUpdate
- AddTasks
- AddCustomCategory
#include "chrome/browser/jumplist_updater_win.h"
#include <windows.h>
#include <propkey.h>
#include <shobjidl.h>
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
namespace {
bool AddShellLink(base::win::ScopedComPtr<IObjectCollection> collection,
const std::wstring& application_path,
scoped_refptr<ShellLinkItem> item) {
base::win::ScopedComPtr<IShellLink> link;
HRESULT result = link.CreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER);
if (FAILED(result))
return false;
result = link->SetPath(application_path.c_str());
if (FAILED(result))
return false;
std::wstring arguments(item->GetArguments());
if (!arguments.empty()) {
result = link->SetArguments(arguments.c_str());
if (FAILED(result))
return false;
}
if (!item->icon_path().empty())
link->SetIconLocation(item->icon_path().c_str(), item->icon_index());
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;
}
return SUCCEEDED(collection->AddObject(link));
}
}
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(const std::wstring& app_user_model_id)
: app_user_model_id_(app_user_model_id),
user_max_items_(0) {
}
JumpListUpdater::~JumpListUpdater() {
}
bool JumpListUpdater::IsEnabled() {
return base::win::GetVersion() >= base::win::VERSION_WIN7;
}
bool JumpListUpdater::BeginUpdate() {
DCHECK(!destination_list_.get());
if (!JumpListUpdater::IsEnabled() || app_user_model_id_.empty())
return false;
HRESULT result = destination_list_.CreateInstance(CLSID_DestinationList, NULL,
CLSCTX_INPROC_SERVER);
if (FAILED(result))
return false;
result = destination_list_->SetAppID(app_user_model_id_.c_str());
if (FAILED(result))
return false;
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;
return SUCCEEDED(destination_list_->CommitList());
}
bool JumpListUpdater::AddTasks(const ShellLinkItemList& link_items) {
if (!destination_list_.get())
return false;
base::FilePath application_path;
if (!PathService::Get(base::FILE_EXE, &application_path))
return false;
base::win::ScopedComPtr<IObjectCollection> collection;
HRESULT result = collection.CreateInstance(CLSID_EnumerableObjectCollection,
NULL, CLSCTX_INPROC_SERVER);
if (FAILED(result))
return false;
for (ShellLinkItemList::const_iterator it = link_items.begin();
it != link_items.end(); ++it) {
AddShellLink(collection, application_path.value(), *it);
}
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;
base::FilePath application_path;
if (!PathService::Get(base::FILE_EXE, &application_path))
return false;
if (link_items.empty() || !max_items)
return true;
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);
}
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));
}