url_forwarder_configurator_main_win.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <objbase.h>
  5. #include <shlobj.h>
  6. #include <shobjidl.h>
  7. #include <wrl/client.h>
  8. #include <cwchar>
  9. #include <memory>
  10. #include <string>
  11. #include "base/bind.h"
  12. #include "base/callback_forward.h"
  13. #include "base/command_line.h"
  14. #include "base/logging.h"
  15. #include "base/message_loop/message_pump_type.h"
  16. #include "base/notreached.h"
  17. #include "base/run_loop.h"
  18. #include "base/strings/utf_string_conversions.h"
  19. #include "base/task/single_thread_task_executor.h"
  20. #include "base/task/thread_pool/thread_pool_instance.h"
  21. #include "base/threading/sequenced_task_runner_handle.h"
  22. #include "base/time/time.h"
  23. #include "base/win/default_apps_util.h"
  24. #include "base/win/scoped_co_mem.h"
  25. #include "base/win/scoped_com_initializer.h"
  26. #include "base/win/windows_types.h"
  27. #include "remoting/base/logging.h"
  28. #include "remoting/base/user_settings.h"
  29. #include "remoting/host/base/switches.h"
  30. #include "remoting/host/remote_open_url/remote_open_url_constants.h"
  31. #include "remoting/host/user_setting_keys.h"
  32. #include "remoting/host/win/core_resource.h"
  33. #include "remoting/host/win/simple_task_dialog.h"
  34. namespace remoting {
  35. namespace {
  36. constexpr wchar_t kProtocolToTestSetup[] = L"http";
  37. constexpr base::TimeDelta kPollingInterval = base::Milliseconds(500);
  38. constexpr base::TimeDelta kPollingTimeout = base::Minutes(1);
  39. // Returns the current default browser's ProgID, or an empty string if failed.
  40. std::wstring GetDefaultBrowserProgId() {
  41. // This method is modified from chrome/installer/util/shell_util.cc
  42. Microsoft::WRL::ComPtr<IApplicationAssociationRegistration> registration;
  43. HRESULT hr =
  44. ::CoCreateInstance(CLSID_ApplicationAssociationRegistration, nullptr,
  45. CLSCTX_INPROC, IID_PPV_ARGS(&registration));
  46. if (FAILED(hr)) {
  47. PLOG(ERROR) << "Failed to create IApplicationAssociationRegistration";
  48. return std::wstring();
  49. }
  50. base::win::ScopedCoMem<wchar_t> current_app;
  51. hr = registration->QueryCurrentDefault(kProtocolToTestSetup, AT_URLPROTOCOL,
  52. AL_EFFECTIVE, &current_app);
  53. if (FAILED(hr)) {
  54. PLOG(ERROR) << "Failed to query default app for protocol "
  55. << kProtocolToTestSetup;
  56. return std::wstring();
  57. }
  58. return current_app.get();
  59. }
  60. // |log_current_default_app| logs the current default app if it is not the CRD
  61. // URL forwarder.
  62. bool IsUrlForwarderSetUp(bool log_current_default_app = false) {
  63. std::wstring current_app = GetDefaultBrowserProgId();
  64. if (current_app.empty()) {
  65. return false;
  66. }
  67. if (current_app != kUrlForwarderProgId) {
  68. if (log_current_default_app) {
  69. HOST_LOG << "Current default app for " << kProtocolToTestSetup << " is "
  70. << current_app << " instead of " << kUrlForwarderProgId;
  71. }
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool ShowSetUpUrlForwarderDialog() {
  77. // |resource_module| does not need to be freed as GetModuleHandle() does not
  78. // increment the refcount for the module. This DLL is not unloaded until the
  79. // process exits so using a stored handle is safe.
  80. HMODULE resource_module = GetModuleHandle(L"remoting_core.dll");
  81. if (resource_module == nullptr) {
  82. PLOG(ERROR) << "GetModuleHandle() failed";
  83. return false;
  84. }
  85. SimpleTaskDialog task_dialog(resource_module);
  86. if (!task_dialog.SetTitleTextWithStringId(IDS_URL_FORWARDER_NAME) ||
  87. !task_dialog.SetMessageTextWithStringId(
  88. IDS_SET_UP_URL_FORWARDER_MESSAGE) ||
  89. !task_dialog.AppendButtonWithStringId(
  90. IDOK, IDS_OPEN_DEFAULT_APPS_SETTINGS_BUTTON) ||
  91. !task_dialog.AppendButtonWithStringId(IDCANCEL, IDS_CANCEL)) {
  92. LOG(ERROR) << "Failed to load text for the setup dialog.";
  93. return false;
  94. }
  95. task_dialog.set_default_button(IDOK);
  96. absl::optional<int> button_result = task_dialog.Show();
  97. if (!button_result.has_value()) {
  98. LOG(ERROR) << "Failed to show the setup dialog.";
  99. return false;
  100. }
  101. switch (*button_result) {
  102. case IDOK:
  103. return true;
  104. case IDCANCEL:
  105. return false;
  106. default:
  107. NOTREACHED() << "Unknown button: " << *button_result;
  108. return false;
  109. }
  110. }
  111. // Class for running the setup process.
  112. class SetUpProcess {
  113. public:
  114. SetUpProcess();
  115. ~SetUpProcess();
  116. // Starts the setup process and calls |done_callback| once done.
  117. void Start(base::OnceCallback<void(bool)> done_callback);
  118. private:
  119. void OnSetUpDialogContinue();
  120. void OnSetUpDialogCancel();
  121. void PollUrlForwarderSetupState();
  122. base::OnceCallback<void(bool)> done_callback_;
  123. base::TimeDelta total_poll_time_;
  124. };
  125. SetUpProcess::SetUpProcess() = default;
  126. SetUpProcess::~SetUpProcess() {
  127. DCHECK(!done_callback_);
  128. }
  129. void SetUpProcess::Start(base::OnceCallback<void(bool)> done_callback) {
  130. DCHECK(!done_callback_);
  131. done_callback_ = std::move(done_callback);
  132. if (IsUrlForwarderSetUp()) {
  133. HOST_LOG << "URL forwarder has already been set up.";
  134. std::move(done_callback_).Run(true);
  135. return;
  136. }
  137. std::wstring prog_id = GetDefaultBrowserProgId();
  138. LOG(INFO) << "Setting previous default browser to " << prog_id;
  139. UserSettings::GetInstance()->SetString(kWinPreviousDefaultWebBrowserProgId,
  140. base::WideToUTF8(prog_id));
  141. if (ShowSetUpUrlForwarderDialog()) {
  142. OnSetUpDialogContinue();
  143. } else {
  144. OnSetUpDialogCancel();
  145. }
  146. }
  147. void SetUpProcess::OnSetUpDialogContinue() {
  148. // Windows does not pick up changes in RegisteredApplications until
  149. // SHChangeNotify() is called, so we call it here in case the user has just
  150. // added the URL forwarder entry to the registry.
  151. SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
  152. HOST_LOG << "Launching default apps settings dialog";
  153. if (!base::win::LaunchDefaultAppsSettingsModernDialog(
  154. /*protocol=*/std::wstring())) {
  155. std::move(done_callback_).Run(false);
  156. return;
  157. }
  158. DCHECK(total_poll_time_.is_zero());
  159. HOST_LOG << "Polling default app for protocol " << kProtocolToTestSetup;
  160. PollUrlForwarderSetupState();
  161. }
  162. void SetUpProcess::OnSetUpDialogCancel() {
  163. HOST_LOG << "User canceled the setup process";
  164. std::move(done_callback_).Run(false);
  165. }
  166. void SetUpProcess::PollUrlForwarderSetupState() {
  167. if (IsUrlForwarderSetUp()) {
  168. std::move(done_callback_).Run(true);
  169. return;
  170. }
  171. if (total_poll_time_ >= kPollingTimeout) {
  172. LOG(ERROR)
  173. << "Timed out waiting for the URL forwarder to become the default app";
  174. std::move(done_callback_).Run(false);
  175. return;
  176. }
  177. total_poll_time_ += kPollingInterval;
  178. base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
  179. FROM_HERE,
  180. base::BindOnce(&SetUpProcess::PollUrlForwarderSetupState,
  181. base::Unretained(this)),
  182. kPollingInterval);
  183. }
  184. } // namespace
  185. int UrlForwarderConfiguratorMain() {
  186. base::ThreadPoolInstance::CreateAndStartWithDefaultParams(
  187. "UrlForwarderConfigurator");
  188. base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::UI);
  189. base::win::ScopedCOMInitializer com;
  190. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  191. kSetUpUrlForwarderSwitchName)) {
  192. base::RunLoop run_loop;
  193. SetUpProcess set_up_process;
  194. bool success = false;
  195. set_up_process.Start(base::BindOnce(
  196. [](base::OnceClosure quit_closure, bool& out_success, bool success) {
  197. out_success = success;
  198. std::move(quit_closure).Run();
  199. },
  200. run_loop.QuitClosure(), std::ref(success)));
  201. run_loop.Run();
  202. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  203. }
  204. // The default action is to check if the URL forwarder has been properly set
  205. // up.
  206. return IsUrlForwarderSetUp(/* log_current_default_app= */ true)
  207. ? EXIT_SUCCESS
  208. : EXIT_FAILURE;
  209. }
  210. } // namespace remoting