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/path_service.h"
|
2015-11-03 07:49:37 +00:00
|
|
|
#include "base/strings/string_util.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"
|
2016-03-21 18:24:25 +00:00
|
|
|
#include "base/win/registry.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 {
|
|
|
|
|
2015-11-03 07:49:37 +00:00
|
|
|
const wchar_t kAppUserModelIDFormat[] = L"electron.app.$1";
|
|
|
|
|
2013-07-04 12:58:28 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-11-03 07:09:31 +00:00
|
|
|
void Browser::SetAppUserModelID(const base::string16& name) {
|
|
|
|
app_user_model_id_ = name;
|
|
|
|
SetCurrentProcessExplicitAppUserModelID(app_user_model_id_.c_str());
|
|
|
|
}
|
|
|
|
|
2016-08-07 22:22:32 +00:00
|
|
|
bool Browser::SetUserTasks(const std::vector<UserTask>& tasks) {
|
2014-11-17 09:19:41 +00:00
|
|
|
CComPtr<ICustomDestinationList> destinations;
|
|
|
|
if (FAILED(destinations.CoCreateInstance(CLSID_DestinationList)))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2015-11-03 06:55:43 +00:00
|
|
|
if (FAILED(destinations->SetAppID(GetAppUserModelID())))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
|
|
|
|
// 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))))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
|
|
|
|
CComPtr<IObjectCollection> collection;
|
|
|
|
if (FAILED(collection.CoCreateInstance(CLSID_EnumerableObjectCollection)))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
|
|
|
|
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())))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
|
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)))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:35:51 +00:00
|
|
|
|
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()))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
|
|
|
|
if (FAILED(collection->AddObject(link)))
|
2016-08-07 22:22:32 +00:00
|
|
|
return false;
|
2014-11-17 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2016-08-07 22:22:32 +00:00
|
|
|
return SUCCEEDED(destinations->CommitList());
|
2014-11-17 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 08:02:13 +00:00
|
|
|
bool GetProtocolLaunchPath(std::string protocol, mate::Arguments* args, std::wstring* exe) {
|
2016-08-16 05:35:20 +00:00
|
|
|
// Read in optional exe path arg
|
|
|
|
std::wstring exePath;
|
|
|
|
std::string rawExePath;
|
2016-03-24 17:55:09 +00:00
|
|
|
|
|
|
|
base::FilePath path;
|
2016-08-16 05:35:20 +00:00
|
|
|
|
|
|
|
if (!args->GetNext(&rawExePath)) {
|
|
|
|
if (!PathService::Get(base::FILE_EXE, &path)) {
|
|
|
|
LOG(ERROR) << "Error getting app exe path";
|
2016-08-18 08:02:13 +00:00
|
|
|
return false;
|
2016-08-16 05:35:20 +00:00
|
|
|
}
|
|
|
|
// Executable Path
|
|
|
|
exePath = path.value();
|
|
|
|
} else {
|
|
|
|
exePath.assign(rawExePath.begin(), rawExePath.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in optional args arg
|
|
|
|
std::vector<std::string> launchArgs;
|
|
|
|
args->GetNext(&launchArgs);
|
|
|
|
|
2016-08-18 08:02:13 +00:00
|
|
|
*exe = L"\"" + exePath + L"\" ";
|
2016-08-16 05:35:20 +00:00
|
|
|
|
|
|
|
// Parse launch args into a string of space spearated args
|
|
|
|
if (launchArgs.size() != 0) {
|
2016-08-18 08:02:13 +00:00
|
|
|
std::string launchArgString = base::JoinString(launchArgs, " ");
|
2016-08-16 05:35:20 +00:00
|
|
|
std::wstring wLaunchArgString;
|
|
|
|
wLaunchArgString.assign(launchArgString.begin(), launchArgString.end());
|
2016-08-18 08:02:13 +00:00
|
|
|
*exe = *exe + L"\"" + wLaunchArgString + L"\"";
|
2016-03-24 17:55:09 +00:00
|
|
|
}
|
2016-08-18 08:02:13 +00:00
|
|
|
*exe = *exe + L"\"%1\"";
|
|
|
|
return true;
|
2016-08-16 05:35:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 05:54:30 +00:00
|
|
|
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
|
|
|
|
mate::Arguments* args) {
|
2016-08-16 05:35:20 +00:00
|
|
|
if (protocol.empty())
|
|
|
|
return false;
|
2016-03-24 17:55:09 +00:00
|
|
|
|
|
|
|
// Main Registry Key
|
|
|
|
HKEY root = HKEY_CURRENT_USER;
|
|
|
|
std::string keyPathStr = "Software\\Classes\\" + protocol;
|
|
|
|
std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end());
|
|
|
|
|
|
|
|
// Command Key
|
|
|
|
std::string cmdPathStr = keyPathStr + "\\shell\\open\\command";
|
|
|
|
std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end());
|
|
|
|
|
|
|
|
base::win::RegKey key;
|
|
|
|
base::win::RegKey commandKey;
|
|
|
|
if (FAILED(key.Open(root, keyPath.c_str(), KEY_ALL_ACCESS)))
|
|
|
|
// Key doesn't even exist, we can confirm that it is not set
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (FAILED(commandKey.Open(root, cmdPath.c_str(), KEY_ALL_ACCESS)))
|
|
|
|
// Key doesn't even exist, we can confirm that it is not set
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::wstring keyVal;
|
|
|
|
if (FAILED(commandKey.ReadValue(L"", &keyVal)))
|
|
|
|
// Default value not set, we can confirm that it is not set
|
|
|
|
return true;
|
|
|
|
|
2016-08-18 08:02:13 +00:00
|
|
|
std::wstring exe;
|
|
|
|
if (!GetProtocolLaunchPath(protocol, args, &exe)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-16 05:35:20 +00:00
|
|
|
if (exe == L"")
|
|
|
|
return false;
|
2016-03-24 17:55:09 +00:00
|
|
|
if (keyVal == exe) {
|
|
|
|
// Let's kill the key
|
|
|
|
if (FAILED(key.DeleteKey(L"shell")))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:54:30 +00:00
|
|
|
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
|
|
|
|
mate::Arguments* args) {
|
2016-03-21 18:24:25 +00:00
|
|
|
// HKEY_CLASSES_ROOT
|
|
|
|
// $PROTOCOL
|
|
|
|
// (Default) = "URL:$NAME"
|
|
|
|
// URL Protocol = ""
|
|
|
|
// shell
|
|
|
|
// open
|
|
|
|
// command
|
|
|
|
// (Default) = "$COMMAND" "%1"
|
|
|
|
//
|
|
|
|
// However, the "HKEY_CLASSES_ROOT" key can only be written by the
|
|
|
|
// Administrator user. So, we instead write to "HKEY_CURRENT_USER\
|
|
|
|
// Software\Classes", which is inherited by "HKEY_CLASSES_ROOT"
|
|
|
|
// anyway, and can be written by unprivileged users.
|
|
|
|
|
|
|
|
if (protocol.empty())
|
|
|
|
return false;
|
|
|
|
|
2016-08-18 08:02:13 +00:00
|
|
|
std::wstring exe;
|
|
|
|
if (!GetProtocolLaunchPath(protocol, args, &exe)) {
|
2016-03-21 18:24:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main Registry Key
|
|
|
|
HKEY root = HKEY_CURRENT_USER;
|
|
|
|
std::string keyPathStr = "Software\\Classes\\" + protocol;
|
|
|
|
std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end());
|
|
|
|
std::string urlDeclStr = "URL:" + protocol;
|
|
|
|
std::wstring urlDecl = std::wstring(urlDeclStr.begin(), urlDeclStr.end());
|
|
|
|
|
|
|
|
// Command Key
|
|
|
|
std::string cmdPathStr = keyPathStr + "\\shell\\open\\command";
|
|
|
|
std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end());
|
|
|
|
|
|
|
|
// Write information to registry
|
|
|
|
base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS);
|
|
|
|
if (FAILED(key.WriteValue(L"URL Protocol", L"")) ||
|
|
|
|
FAILED(key.WriteValue(L"", urlDecl.c_str())))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
base::win::RegKey commandKey(root, cmdPath.c_str(), KEY_ALL_ACCESS);
|
|
|
|
if (FAILED(commandKey.WriteValue(L"", exe.c_str())))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:54:30 +00:00
|
|
|
bool Browser::IsDefaultProtocolClient(const std::string& protocol,
|
|
|
|
mate::Arguments* args) {
|
2016-04-25 05:17:01 +00:00
|
|
|
if (protocol.empty())
|
|
|
|
return false;
|
|
|
|
|
2016-08-18 08:02:13 +00:00
|
|
|
std::wstring exe;
|
|
|
|
if (!GetProtocolLaunchPath(protocol, args, &exe)) {
|
2016-04-25 05:17:01 +00:00
|
|
|
return false;
|
2016-08-18 08:02:13 +00:00
|
|
|
}
|
2016-04-25 05:17:01 +00:00
|
|
|
|
|
|
|
// Main Registry Key
|
|
|
|
HKEY root = HKEY_CURRENT_USER;
|
|
|
|
std::string keyPathStr = "Software\\Classes\\" + protocol;
|
|
|
|
std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end());
|
|
|
|
|
|
|
|
// Command Key
|
|
|
|
std::string cmdPathStr = keyPathStr + "\\shell\\open\\command";
|
|
|
|
std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end());
|
|
|
|
|
|
|
|
base::win::RegKey key;
|
|
|
|
base::win::RegKey commandKey;
|
|
|
|
if (FAILED(key.Open(root, keyPath.c_str(), KEY_ALL_ACCESS)))
|
|
|
|
// Key doesn't exist, we can confirm that it is not set
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (FAILED(commandKey.Open(root, cmdPath.c_str(), KEY_ALL_ACCESS)))
|
|
|
|
// Key doesn't exist, we can confirm that it is not set
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::wstring keyVal;
|
|
|
|
if (FAILED(commandKey.ReadValue(L"", &keyVal)))
|
|
|
|
// Default value not set, we can confirm that it is not set
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (keyVal == exe) {
|
|
|
|
// Default value is the same as current file path
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 13:18:39 +00:00
|
|
|
bool Browser::SetBadgeCount(int count) {
|
|
|
|
return false;
|
2016-07-01 08:39:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 17:57:53 +00:00
|
|
|
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
2016-07-11 19:29:01 +00:00
|
|
|
std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
|
|
|
base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS);
|
|
|
|
|
|
|
|
if (settings.open_at_login) {
|
|
|
|
base::FilePath path;
|
|
|
|
if (PathService::Get(base::FILE_EXE, &path)) {
|
|
|
|
std::wstring exePath(path.value());
|
|
|
|
key.WriteValue(GetAppUserModelID(), exePath.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key.DeleteValue(GetAppUserModelID());
|
2016-07-11 18:12:26 +00:00
|
|
|
}
|
2016-07-11 17:57:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:29:01 +00:00
|
|
|
Browser::LoginItemSettings Browser::GetLoginItemSettings() {
|
2016-07-11 18:12:26 +00:00
|
|
|
LoginItemSettings settings;
|
2016-07-11 19:29:01 +00:00
|
|
|
std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
|
|
|
base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS);
|
|
|
|
std::wstring keyVal;
|
2016-07-11 18:12:26 +00:00
|
|
|
|
2016-07-11 19:29:01 +00:00
|
|
|
if (!FAILED(key.ReadValue(GetAppUserModelID(), &keyVal))) {
|
|
|
|
base::FilePath path;
|
|
|
|
if (PathService::Get(base::FILE_EXE, &path)) {
|
|
|
|
std::wstring exePath(path.value());
|
|
|
|
settings.open_at_login = keyVal == exePath;
|
|
|
|
}
|
2016-07-11 18:12:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:29:01 +00:00
|
|
|
return settings;
|
2016-07-11 17:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-03 06:55:43 +00:00
|
|
|
PCWSTR Browser::GetAppUserModelID() {
|
2015-11-03 07:49:37 +00:00
|
|
|
if (app_user_model_id_.empty()) {
|
2015-12-07 18:02:06 +00:00
|
|
|
SetAppUserModelID(base::ReplaceStringPlaceholders(
|
2015-11-03 07:49:37 +00:00
|
|
|
kAppUserModelIDFormat, base::UTF8ToUTF16(GetName()), nullptr));
|
|
|
|
}
|
2015-11-03 06:55:43 +00:00
|
|
|
|
|
|
|
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)) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<FileVersionInfo> version_info(
|
2013-07-04 12:58:28 +00:00
|
|
|
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)) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<FileVersionInfo> version_info(
|
2013-12-05 02:42:04 +00:00
|
|
|
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
|