2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-07-04 12:58:28 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2013-07-04 12:58:28 +00:00
|
|
|
|
2014-11-17 07:53:18 +00:00
|
|
|
#include <atlbase.h>
|
2014-11-17 09:19:41 +00:00
|
|
|
#include <propkey.h>
|
2013-07-04 12:58:28 +00:00
|
|
|
#include <windows.h>
|
2014-11-17 07:53:18 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shobjidl.h>
|
2013-07-04 12:58:28 +00:00
|
|
|
|
|
|
|
#include "base/base_paths.h"
|
|
|
|
#include "base/file_version_info.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "base/path_service.h"
|
2014-11-17 07:53:18 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2013-07-04 12:58:28 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2014-11-17 09:19:41 +00:00
|
|
|
#include "base/win/win_util.h"
|
2014-11-17 07:53:18 +00:00
|
|
|
#include "base/win/windows_version.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/atom_version.h"
|
2013-07-04 12:58:28 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) {
|
|
|
|
DWORD target_process_id = *reinterpret_cast<DWORD*>(param);
|
|
|
|
DWORD process_id = 0;
|
|
|
|
|
|
|
|
GetWindowThreadProcessId(hwnd, &process_id);
|
|
|
|
if (process_id == target_process_id) {
|
|
|
|
SetFocus(hwnd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Browser::Focus() {
|
|
|
|
// On Windows we just focus on the first window found for this process.
|
|
|
|
DWORD pid = GetCurrentProcessId();
|
|
|
|
EnumWindows(&WindowsEnumerationHandler, reinterpret_cast<LPARAM>(&pid));
|
|
|
|
}
|
|
|
|
|
2014-11-17 05:05:06 +00:00
|
|
|
void Browser::AddRecentDocument(const base::FilePath& path) {
|
2014-11-17 07:53:18 +00:00
|
|
|
if (base::win::GetVersion() < base::win::VERSION_WIN7)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CComPtr<IShellItem> item;
|
|
|
|
HRESULT hr = SHCreateItemFromParsingName(
|
|
|
|
path.value().c_str(), NULL, IID_PPV_ARGS(&item));
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
SHARDAPPIDINFO info;
|
|
|
|
info.psi = item;
|
2015-11-03 06:55:43 +00:00
|
|
|
info.pszAppID = GetAppUserModelID();
|
2014-11-17 07:53:18 +00:00
|
|
|
SHAddToRecentDocs(SHARD_APPIDINFO, &info);
|
|
|
|
}
|
2014-11-17 05:05:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 08:13:47 +00:00
|
|
|
void Browser::ClearRecentDocuments() {
|
|
|
|
CComPtr<IApplicationDestinations> destinations;
|
|
|
|
if (FAILED(destinations.CoCreateInstance(CLSID_ApplicationDestinations,
|
|
|
|
NULL, CLSCTX_INPROC_SERVER)))
|
|
|
|
return;
|
2015-11-03 06:55:43 +00:00
|
|
|
if (FAILED(destinations->SetAppID(GetAppUserModelID())))
|
2014-11-17 08:13:47 +00:00
|
|
|
return;
|
|
|
|
destinations->RemoveAllDestinations();
|
|
|
|
}
|
|
|
|
|
2014-11-17 11:32:11 +00:00
|
|
|
void Browser::SetUserTasks(const std::vector<UserTask>& tasks) {
|
2014-11-17 09:19:41 +00:00
|
|
|
CComPtr<ICustomDestinationList> destinations;
|
|
|
|
if (FAILED(destinations.CoCreateInstance(CLSID_DestinationList)))
|
|
|
|
return;
|
2015-11-03 06:55:43 +00:00
|
|
|
if (FAILED(destinations->SetAppID(GetAppUserModelID())))
|
2014-11-17 09:19:41 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Start a transaction that updates the JumpList of this application.
|
|
|
|
UINT max_slots;
|
|
|
|
CComPtr<IObjectArray> removed;
|
|
|
|
if (FAILED(destinations->BeginList(&max_slots, IID_PPV_ARGS(&removed))))
|
|
|
|
return;
|
|
|
|
|
|
|
|
CComPtr<IObjectCollection> collection;
|
|
|
|
if (FAILED(collection.CoCreateInstance(CLSID_EnumerableObjectCollection)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto& task : tasks) {
|
|
|
|
CComPtr<IShellLink> link;
|
|
|
|
if (FAILED(link.CoCreateInstance(CLSID_ShellLink)) ||
|
|
|
|
FAILED(link->SetPath(task.program.value().c_str())) ||
|
|
|
|
FAILED(link->SetArguments(task.arguments.c_str())) ||
|
|
|
|
FAILED(link->SetDescription(task.description.c_str())))
|
|
|
|
return;
|
|
|
|
|
2014-11-17 09:35:51 +00:00
|
|
|
if (!task.icon_path.empty() &&
|
|
|
|
FAILED(link->SetIconLocation(task.icon_path.value().c_str(),
|
|
|
|
task.icon_index)))
|
|
|
|
return;
|
|
|
|
|
2014-11-17 09:19:41 +00:00
|
|
|
CComQIPtr<IPropertyStore> property_store = link;
|
|
|
|
if (!base::win::SetStringValueForPropertyStore(property_store, PKEY_Title,
|
|
|
|
task.title.c_str()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FAILED(collection->AddObject(link)))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the list is empty "AddUserTasks" could fail, so we don't check return
|
|
|
|
// value for it.
|
|
|
|
CComQIPtr<IObjectArray> task_array = collection;
|
|
|
|
destinations->AddUserTasks(task_array);
|
|
|
|
destinations->CommitList();
|
|
|
|
}
|
|
|
|
|
2015-11-03 06:55:43 +00:00
|
|
|
void Browser::SetAppUserModelID(const base::string16& name) {
|
|
|
|
app_user_model_id_ = name;
|
2014-11-17 07:55:49 +00:00
|
|
|
SetCurrentProcessExplicitAppUserModelID(app_user_model_id_.c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-03 06:55:43 +00:00
|
|
|
PCWSTR Browser::GetAppUserModelID() {
|
|
|
|
if (app_user_model_id_.empty())
|
|
|
|
SetAppUserModelID(base::UTF8ToUTF16(GetName()));
|
|
|
|
|
|
|
|
return app_user_model_id_.c_str();
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:26:01 +00:00
|
|
|
std::string Browser::GetExecutableFileVersion() const {
|
2013-07-04 12:58:28 +00:00
|
|
|
base::FilePath path;
|
|
|
|
if (PathService::Get(base::FILE_EXE, &path)) {
|
|
|
|
scoped_ptr<FileVersionInfo> version_info(
|
|
|
|
FileVersionInfo::CreateFileVersionInfo(path));
|
2014-07-11 04:19:01 +00:00
|
|
|
return base::UTF16ToUTF8(version_info->product_version());
|
2013-07-04 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:42:04 +00:00
|
|
|
return ATOM_VERSION_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Browser::GetExecutableFileProductName() const {
|
|
|
|
base::FilePath path;
|
|
|
|
if (PathService::Get(base::FILE_EXE, &path)) {
|
|
|
|
scoped_ptr<FileVersionInfo> version_info(
|
|
|
|
FileVersionInfo::CreateFileVersionInfo(path));
|
2014-07-11 04:19:01 +00:00
|
|
|
return base::UTF16ToUTF8(version_info->product_name());
|
2013-12-05 02:42:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 07:55:41 +00:00
|
|
|
return ATOM_PRODUCT_NAME;
|
2013-07-04 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|