browser_win.cc 9.7 KB

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