browser_win.cc 10 KB

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