browser_win.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright (c) 2013 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/browser.h"
  5. #include <windows.h> // windows.h must be included first
  6. #include <atlbase.h>
  7. #include <shlobj.h>
  8. #include <shobjidl.h>
  9. #include "atom/browser/ui/win/jump_list.h"
  10. #include "atom/common/application_info.h"
  11. #include "atom/common/atom_version.h"
  12. #include "atom/common/native_mate_converters/string16_converter.h"
  13. #include "base/base_paths.h"
  14. #include "base/file_version_info.h"
  15. #include "base/files/file_path.h"
  16. #include "base/path_service.h"
  17. #include "base/strings/string_util.h"
  18. #include "base/strings/stringprintf.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "base/threading/thread_restrictions.h"
  21. #include "base/win/registry.h"
  22. #include "base/win/win_util.h"
  23. #include "base/win/windows_version.h"
  24. #include "ui/events/keycodes/keyboard_code_conversion_win.h"
  25. namespace atom {
  26. namespace {
  27. BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) {
  28. DWORD target_process_id = *reinterpret_cast<DWORD*>(param);
  29. DWORD process_id = 0;
  30. GetWindowThreadProcessId(hwnd, &process_id);
  31. if (process_id == target_process_id) {
  32. SetFocus(hwnd);
  33. return FALSE;
  34. }
  35. return TRUE;
  36. }
  37. bool GetProcessExecPath(base::string16* exe) {
  38. base::FilePath path;
  39. if (!base::PathService::Get(base::FILE_EXE, &path)) {
  40. LOG(ERROR) << "Error getting app exe path";
  41. return false;
  42. }
  43. *exe = path.value();
  44. return true;
  45. }
  46. bool GetProtocolLaunchPath(mate::Arguments* args, base::string16* exe) {
  47. if (!args->GetNext(exe) && !GetProcessExecPath(exe)) {
  48. return false;
  49. }
  50. // Read in optional args arg
  51. std::vector<base::string16> launch_args;
  52. if (args->GetNext(&launch_args) && !launch_args.empty())
  53. *exe = base::StringPrintf(L"\"%ls\" %ls \"%%1\"", exe->c_str(),
  54. base::JoinString(launch_args, L" ").c_str());
  55. else
  56. *exe = base::StringPrintf(L"\"%ls\" \"%%1\"", exe->c_str());
  57. return true;
  58. }
  59. bool FormatCommandLineString(base::string16* exe,
  60. const std::vector<base::string16>& launch_args) {
  61. if (exe->empty() && !GetProcessExecPath(exe)) {
  62. return false;
  63. }
  64. if (!launch_args.empty()) {
  65. *exe = base::StringPrintf(L"%ls %ls", exe->c_str(),
  66. base::JoinString(launch_args, L" ").c_str());
  67. }
  68. return true;
  69. }
  70. } // namespace
  71. Browser::UserTask::UserTask() = default;
  72. Browser::UserTask::UserTask(const UserTask&) = default;
  73. Browser::UserTask::~UserTask() = default;
  74. void Browser::Focus() {
  75. // On Windows we just focus on the first window found for this process.
  76. DWORD pid = GetCurrentProcessId();
  77. EnumWindows(&WindowsEnumerationHandler, reinterpret_cast<LPARAM>(&pid));
  78. }
  79. void Browser::AddRecentDocument(const base::FilePath& path) {
  80. CComPtr<IShellItem> item;
  81. HRESULT hr = SHCreateItemFromParsingName(path.value().c_str(), NULL,
  82. IID_PPV_ARGS(&item));
  83. if (SUCCEEDED(hr)) {
  84. SHARDAPPIDINFO info;
  85. info.psi = item;
  86. info.pszAppID = GetAppUserModelID();
  87. SHAddToRecentDocs(SHARD_APPIDINFO, &info);
  88. }
  89. }
  90. void Browser::ClearRecentDocuments() {
  91. SHAddToRecentDocs(SHARD_APPIDINFO, nullptr);
  92. }
  93. void Browser::SetAppUserModelID(const base::string16& name) {
  94. atom::SetAppUserModelID(name);
  95. }
  96. bool Browser::SetUserTasks(const std::vector<UserTask>& tasks) {
  97. JumpList jump_list(GetAppUserModelID());
  98. if (!jump_list.Begin())
  99. return false;
  100. JumpListCategory category;
  101. category.type = JumpListCategory::Type::TASKS;
  102. category.items.reserve(tasks.size());
  103. JumpListItem item;
  104. item.type = JumpListItem::Type::TASK;
  105. for (const auto& task : tasks) {
  106. item.title = task.title;
  107. item.path = task.program;
  108. item.arguments = task.arguments;
  109. item.icon_path = task.icon_path;
  110. item.icon_index = task.icon_index;
  111. item.description = task.description;
  112. item.working_dir = task.working_dir;
  113. category.items.push_back(item);
  114. }
  115. jump_list.AppendCategory(category);
  116. return jump_list.Commit();
  117. }
  118. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  119. mate::Arguments* args) {
  120. if (protocol.empty())
  121. return false;
  122. // Main Registry Key
  123. HKEY root = HKEY_CURRENT_USER;
  124. base::string16 keyPath = L"Software\\Classes\\";
  125. // Command Key
  126. base::string16 wprotocol = base::UTF8ToUTF16(protocol);
  127. base::string16 shellPath = wprotocol + L"\\shell";
  128. base::string16 cmdPath = keyPath + shellPath + L"\\open\\command";
  129. base::win::RegKey classesKey;
  130. base::win::RegKey commandKey;
  131. if (FAILED(classesKey.Open(root, keyPath.c_str(), KEY_ALL_ACCESS)))
  132. // Classes key doesn't exist, that's concerning, but I guess
  133. // we're not the default handler
  134. return true;
  135. if (FAILED(commandKey.Open(root, cmdPath.c_str(), KEY_ALL_ACCESS)))
  136. // Key doesn't even exist, we can confirm that it is not set
  137. return true;
  138. base::string16 keyVal;
  139. if (FAILED(commandKey.ReadValue(L"", &keyVal)))
  140. // Default value not set, we can confirm that it is not set
  141. return true;
  142. base::string16 exe;
  143. if (!GetProtocolLaunchPath(args, &exe))
  144. return false;
  145. if (keyVal == exe) {
  146. // Let's kill the key
  147. if (FAILED(classesKey.DeleteKey(shellPath.c_str())))
  148. return false;
  149. // Let's clean up after ourselves
  150. base::win::RegKey protocolKey;
  151. base::string16 protocolPath = keyPath + wprotocol;
  152. if (SUCCEEDED(
  153. protocolKey.Open(root, protocolPath.c_str(), KEY_ALL_ACCESS))) {
  154. protocolKey.DeleteValue(L"URL Protocol");
  155. // Overwrite the default value to be empty, we can't delete it right away
  156. protocolKey.WriteValue(L"", L"");
  157. protocolKey.DeleteValue(L"");
  158. }
  159. // If now empty, delete the whole key
  160. classesKey.DeleteEmptyKey(wprotocol.c_str());
  161. return true;
  162. } else {
  163. return true;
  164. }
  165. }
  166. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  167. mate::Arguments* args) {
  168. // HKEY_CLASSES_ROOT
  169. // $PROTOCOL
  170. // (Default) = "URL:$NAME"
  171. // URL Protocol = ""
  172. // shell
  173. // open
  174. // command
  175. // (Default) = "$COMMAND" "%1"
  176. //
  177. // However, the "HKEY_CLASSES_ROOT" key can only be written by the
  178. // Administrator user. So, we instead write to "HKEY_CURRENT_USER\
  179. // Software\Classes", which is inherited by "HKEY_CLASSES_ROOT"
  180. // anyway, and can be written by unprivileged users.
  181. if (protocol.empty())
  182. return false;
  183. base::string16 exe;
  184. if (!GetProtocolLaunchPath(args, &exe))
  185. return false;
  186. // Main Registry Key
  187. HKEY root = HKEY_CURRENT_USER;
  188. base::string16 keyPath = base::UTF8ToUTF16("Software\\Classes\\" + protocol);
  189. base::string16 urlDecl = base::UTF8ToUTF16("URL:" + protocol);
  190. // Command Key
  191. base::string16 cmdPath = keyPath + L"\\shell\\open\\command";
  192. // Write information to registry
  193. base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS);
  194. if (FAILED(key.WriteValue(L"URL Protocol", L"")) ||
  195. FAILED(key.WriteValue(L"", urlDecl.c_str())))
  196. return false;
  197. base::win::RegKey commandKey(root, cmdPath.c_str(), KEY_ALL_ACCESS);
  198. if (FAILED(commandKey.WriteValue(L"", exe.c_str())))
  199. return false;
  200. return true;
  201. }
  202. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  203. mate::Arguments* args) {
  204. if (protocol.empty())
  205. return false;
  206. base::string16 exe;
  207. if (!GetProtocolLaunchPath(args, &exe))
  208. return false;
  209. // Main Registry Key
  210. HKEY root = HKEY_CURRENT_USER;
  211. base::string16 keyPath = base::UTF8ToUTF16("Software\\Classes\\" + protocol);
  212. // Command Key
  213. base::string16 cmdPath = keyPath + L"\\shell\\open\\command";
  214. base::win::RegKey key;
  215. base::win::RegKey commandKey;
  216. if (FAILED(key.Open(root, keyPath.c_str(), KEY_ALL_ACCESS)))
  217. // Key doesn't exist, we can confirm that it is not set
  218. return false;
  219. if (FAILED(commandKey.Open(root, cmdPath.c_str(), KEY_ALL_ACCESS)))
  220. // Key doesn't exist, we can confirm that it is not set
  221. return false;
  222. base::string16 keyVal;
  223. if (FAILED(commandKey.ReadValue(L"", &keyVal)))
  224. // Default value not set, we can confirm that it is not set
  225. return false;
  226. // Default value is the same as current file path
  227. return keyVal == exe;
  228. }
  229. bool Browser::SetBadgeCount(int count) {
  230. return false;
  231. }
  232. void Browser::SetLoginItemSettings(LoginItemSettings settings) {
  233. base::string16 keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  234. base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS);
  235. if (settings.open_at_login) {
  236. base::string16 exe = settings.path;
  237. if (FormatCommandLineString(&exe, settings.args)) {
  238. key.WriteValue(GetAppUserModelID(), exe.c_str());
  239. }
  240. } else {
  241. key.DeleteValue(GetAppUserModelID());
  242. }
  243. }
  244. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  245. const LoginItemSettings& options) {
  246. LoginItemSettings settings;
  247. base::string16 keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  248. base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS);
  249. base::string16 keyVal;
  250. if (!FAILED(key.ReadValue(GetAppUserModelID(), &keyVal))) {
  251. base::string16 exe = options.path;
  252. if (FormatCommandLineString(&exe, options.args)) {
  253. settings.open_at_login = keyVal == exe;
  254. }
  255. }
  256. return settings;
  257. }
  258. PCWSTR Browser::GetAppUserModelID() {
  259. return GetRawAppUserModelID();
  260. }
  261. std::string Browser::GetExecutableFileVersion() const {
  262. base::FilePath path;
  263. if (base::PathService::Get(base::FILE_EXE, &path)) {
  264. base::ThreadRestrictions::ScopedAllowIO allow_io;
  265. std::unique_ptr<FileVersionInfo> version_info(
  266. FileVersionInfo::CreateFileVersionInfo(path));
  267. return base::UTF16ToUTF8(version_info->product_version());
  268. }
  269. return ATOM_VERSION_STRING;
  270. }
  271. std::string Browser::GetExecutableFileProductName() const {
  272. return GetApplicationName();
  273. }
  274. bool Browser::IsEmojiPanelSupported() {
  275. // emoji picker is supported on Windows 10's Spring 2018 update & above.
  276. return base::win::GetVersion() >= base::win::Version::WIN10_RS4;
  277. }
  278. void Browser::ShowEmojiPanel() {
  279. // This sends Windows Key + '.' (both keydown and keyup events).
  280. // "SendInput" is used because Windows needs to receive these events and
  281. // open the Emoji picker.
  282. INPUT input[4] = {};
  283. input[0].type = INPUT_KEYBOARD;
  284. input[0].ki.wVk = ui::WindowsKeyCodeForKeyboardCode(ui::VKEY_COMMAND);
  285. input[1].type = INPUT_KEYBOARD;
  286. input[1].ki.wVk = ui::WindowsKeyCodeForKeyboardCode(ui::VKEY_OEM_PERIOD);
  287. input[2].type = INPUT_KEYBOARD;
  288. input[2].ki.wVk = ui::WindowsKeyCodeForKeyboardCode(ui::VKEY_COMMAND);
  289. input[2].ki.dwFlags |= KEYEVENTF_KEYUP;
  290. input[3].type = INPUT_KEYBOARD;
  291. input[3].ki.wVk = ui::WindowsKeyCodeForKeyboardCode(ui::VKEY_OEM_PERIOD);
  292. input[3].ki.dwFlags |= KEYEVENTF_KEYUP;
  293. ::SendInput(4, input, sizeof(INPUT));
  294. }
  295. } // namespace atom