OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "win8/test/metro_registration_helper.h" | 5 #include "win8/test/metro_registration_helper.h" |
6 | 6 |
7 #include <shlobj.h> | |
8 | |
9 #include <vector> | |
10 | |
7 #include "base/command_line.h" | 11 #include "base/command_line.h" |
8 #include "base/file_util.h" | 12 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 14 #include "base/logging.h" |
11 #include "base/path_service.h" | 15 #include "base/path_service.h" |
12 #include "base/process.h" | 16 #include "base/process.h" |
13 #include "base/process_util.h" | 17 #include "base/process_util.h" |
18 #include "base/string16.h" | |
19 #include "base/win/scoped_co_mem.h" | |
20 #include "base/win/scoped_comptr.h" | |
14 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
22 #include "ui/base/win/atl_module.h" | |
23 #include "win8/test/open_with_dialog_controller.h" | |
15 #include "win8/test/test_registrar_constants.h" | 24 #include "win8/test/test_registrar_constants.h" |
16 | 25 |
17 namespace { | 26 namespace { |
18 | 27 |
19 const int kRegistrationTimeoutSeconds = 30; | 28 const int kRegistrationTimeoutSeconds = 30; |
20 | 29 |
21 // Copied from util_constants.cc to avoid taking a dependency on installer_util. | 30 // Copied from util_constants.cc to avoid taking a dependency on installer_util. |
22 const wchar_t kChromeExe[] = L"chrome.exe"; | 31 const wchar_t kChromeExe[] = L"chrome.exe"; |
23 const wchar_t kRegistrar[] = L"test_registrar.exe"; | 32 const wchar_t kRegistrar[] = L"test_registrar.exe"; |
24 | 33 |
25 } | 34 // Registers chrome.exe as a potential Win8 default browser. It will then show |
26 | 35 // up in the default browser selection dialog as |viewer_process_name|. Intended |
grt (UTC plus 2)
2013/05/27 16:11:14
please update comment (there is no |viewer_process
gab
2013/05/27 16:47:58
Done.
| |
27 namespace win8 { | 36 // to be used by a test binary in the build output directory and assumes the |
28 | 37 // presence of test_registrar.exe, a viewer process and all needed DLLs in the |
grt (UTC plus 2)
2013/05/27 16:11:14
nit: comma before " and"
gab
2013/05/27 16:47:58
Done.
| |
29 bool RegisterTestDefaultBrowser(const string16& app_user_model_id, | 38 // same directory as the calling module. |
30 const string16& viewer_process_name) { | 39 bool RegisterTestDefaultBrowser() { |
31 base::FilePath dir; | 40 base::FilePath dir; |
32 if (!PathService::Get(base::DIR_EXE, &dir)) | 41 if (!PathService::Get(base::DIR_EXE, &dir)) |
33 return false; | 42 return false; |
34 | 43 |
35 base::FilePath chrome_exe(dir.Append(kChromeExe)); | 44 base::FilePath chrome_exe(dir.Append(kChromeExe)); |
36 base::FilePath registrar(dir.Append(kRegistrar)); | 45 base::FilePath registrar(dir.Append(kRegistrar)); |
37 | 46 |
38 if (!file_util::PathExists(chrome_exe) || !file_util::PathExists(registrar)) { | 47 if (!file_util::PathExists(chrome_exe) || !file_util::PathExists(registrar)) { |
39 LOG(ERROR) << "Could not locate " << kChromeExe << " or " << kRegistrar; | 48 LOG(ERROR) << "Could not locate " << kChromeExe << " or " << kRegistrar; |
40 return false; | 49 return false; |
41 } | 50 } |
42 | 51 |
43 // Perform the registration by invoking test_registrar.exe with the | 52 // Perform the registration by invoking test_registrar.exe. |
44 // necessary flags: | |
45 // test_registrar.exe /RegServer --appid=<appid> --exe-name=<name> | |
46 CommandLine register_command(registrar); | 53 CommandLine register_command(registrar); |
47 register_command.AppendArg("/RegServer"); | 54 register_command.AppendArg("/RegServer"); |
48 register_command.AppendSwitchNative(win8::test::kTestAppUserModelId, | |
gab
2013/05/27 15:55:11
test_registrar already uses these as default value
grt (UTC plus 2)
2013/05/27 16:11:14
Thanks for clarifying. I was just about to ask tha
| |
49 app_user_model_id); | |
50 register_command.AppendSwitchNative(win8::test::kTestExeName, | |
51 viewer_process_name); | |
52 | 55 |
53 base::win::ScopedHandle register_handle; | 56 base::win::ScopedHandle register_handle; |
54 if (base::LaunchProcess(register_command, base::LaunchOptions(), | 57 if (base::LaunchProcess(register_command, base::LaunchOptions(), |
55 register_handle.Receive())) { | 58 register_handle.Receive())) { |
56 int ret = 0; | 59 int ret = 0; |
57 if (base::WaitForExitCodeWithTimeout( | 60 if (base::WaitForExitCodeWithTimeout( |
58 register_handle, &ret, | 61 register_handle, &ret, |
59 base::TimeDelta::FromSeconds(kRegistrationTimeoutSeconds))) { | 62 base::TimeDelta::FromSeconds(kRegistrationTimeoutSeconds))) { |
60 if (ret == 0) { | 63 if (ret == 0) { |
61 return true; | 64 return true; |
62 } else { | 65 } else { |
63 LOG(ERROR) << "Win8 registration using " | 66 LOG(ERROR) << "Win8 registration using " |
64 << register_command.GetCommandLineString() | 67 << register_command.GetCommandLineString() |
65 << " failed with error code " << ret; | 68 << " failed with error code " << ret; |
66 } | 69 } |
67 } else { | 70 } else { |
68 LOG(ERROR) << "Win8 registration using " | 71 LOG(ERROR) << "Win8 registration using " |
69 << register_command.GetCommandLineString() << " timed out."; | 72 << register_command.GetCommandLineString() << " timed out."; |
70 } | 73 } |
71 } | 74 } |
72 | 75 |
73 PLOG(ERROR) << "Failed to launch Win8 registration utility using " | 76 PLOG(ERROR) << "Failed to launch Win8 registration utility using " |
74 << register_command.GetCommandLineString(); | 77 << register_command.GetCommandLineString(); |
75 return false; | 78 return false; |
76 } | 79 } |
77 | 80 |
81 // Returns true if the test viewer's progid is the default handler for | |
82 // |protocol|. | |
83 bool IsTestDefaultForProtocol(const wchar_t protocol[]) { | |
grt (UTC plus 2)
2013/05/27 16:11:14
const wchar_t* protocol
gab
2013/05/27 16:47:58
Done.
| |
84 base::win::ScopedComPtr<IApplicationAssociationRegistration> registration; | |
85 HRESULT hr = registration.CreateInstance( | |
86 CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC); | |
87 if (FAILED(hr)) { | |
88 LOG(ERROR) << std::hex << hr; | |
89 return false; | |
90 } | |
91 | |
92 base::win::ScopedCoMem<wchar_t> current_app; | |
93 hr = registration->QueryCurrentDefault(protocol, AT_URLPROTOCOL, | |
94 AL_EFFECTIVE, ¤t_app); | |
95 if (FAILED(hr)) { | |
96 LOG(ERROR) << std::hex << hr; | |
97 return false; | |
98 } | |
99 | |
100 return string16(win8::test::kDefaultTestProgId).compare(current_app) == 0; | |
101 } | |
102 | |
103 } // namespace | |
104 | |
105 namespace win8 { | |
106 | |
107 bool MakeTestDefaultBrowserSynchronously() { | |
108 static const wchar_t kDefaultBrowserProtocol[] = L"http"; | |
109 | |
110 if (!RegisterTestDefaultBrowser()) { | |
grt (UTC plus 2)
2013/05/27 16:11:14
nit: no braces
gab
2013/05/27 16:47:58
Done.
| |
111 return false; | |
112 } | |
113 | |
114 // Make sure the registration changes have been acknowledged by the shell | |
115 // before querying for the current default. | |
116 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSH, NULL, NULL); | |
117 | |
118 // OpenWithDialogController will fail if the Test Runner is already default | |
119 // since it will not show up verbatim in the dialog (e.g., in EN-US, it will | |
120 // be prefixed by "Keep using"). | |
grt (UTC plus 2)
2013/05/27 16:11:14
space after "using"? :-)
gab
2013/05/27 16:47:58
Done.
| |
121 if (IsTestDefaultForProtocol(kDefaultBrowserProtocol)) | |
122 return true; | |
123 | |
124 ui::win::CreateATLModuleIfNeeded(); | |
grt (UTC plus 2)
2013/05/27 16:11:14
i don't this this should be done within a helper f
gab
2013/05/27 16:47:58
Ok, I'll take your word for it :)!
| |
125 | |
126 std::vector<base::string16> choices; | |
127 OpenWithDialogController controller; | |
128 HRESULT hr = controller.RunSynchronously( | |
129 NULL, kDefaultBrowserProtocol, win8::test::kDefaultTestExeName, &choices); | |
130 LOG_IF(ERROR, FAILED(hr)) << std::hex << hr; | |
131 DCHECK(IsTestDefaultForProtocol(kDefaultBrowserProtocol)); | |
132 return SUCCEEDED(hr); | |
133 } | |
134 | |
78 } // namespace win8 | 135 } // namespace win8 |
OLD | NEW |