This source file includes following definitions.
- RegisterTestDefaultBrowser
- IsTestDefaultForProtocol
- MakeTestDefaultBrowserSynchronously
#include "win8/test/metro_registration_helper.h"
#include <shlobj.h>
#include <vector>
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/strings/string16.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_handle.h"
#include "win8/test/open_with_dialog_controller.h"
#include "win8/test/test_registrar_constants.h"
namespace {
const int kRegistrationTimeoutSeconds = 30;
const wchar_t kChromeExe[] = L"chrome.exe";
const wchar_t kRegistrar[] = L"test_registrar.exe";
bool RegisterTestDefaultBrowser() {
base::FilePath dir;
if (!PathService::Get(base::DIR_EXE, &dir))
return false;
base::FilePath chrome_exe(dir.Append(kChromeExe));
base::FilePath registrar(dir.Append(kRegistrar));
if (!base::PathExists(chrome_exe) || !base::PathExists(registrar)) {
LOG(ERROR) << "Could not locate " << kChromeExe << " or " << kRegistrar;
return false;
}
CommandLine register_command(registrar);
register_command.AppendArg("/RegServer");
base::win::ScopedHandle register_handle;
if (base::LaunchProcess(register_command.GetCommandLineString(),
base::LaunchOptions(),
®ister_handle)) {
int ret = 0;
if (base::WaitForExitCodeWithTimeout(
register_handle.Get(), &ret,
base::TimeDelta::FromSeconds(kRegistrationTimeoutSeconds))) {
if (ret == 0) {
return true;
} else {
LOG(ERROR) << "Win8 registration using "
<< register_command.GetCommandLineString()
<< " failed with error code " << ret;
}
} else {
LOG(ERROR) << "Win8 registration using "
<< register_command.GetCommandLineString() << " timed out.";
}
}
PLOG(ERROR) << "Failed to launch Win8 registration utility using "
<< register_command.GetCommandLineString();
return false;
}
bool IsTestDefaultForProtocol(const wchar_t* protocol) {
base::win::ScopedComPtr<IApplicationAssociationRegistration> registration;
HRESULT hr = registration.CreateInstance(
CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC);
if (FAILED(hr)) {
LOG(ERROR) << std::hex << hr;
return false;
}
base::win::ScopedCoMem<wchar_t> current_app;
hr = registration->QueryCurrentDefault(protocol, AT_URLPROTOCOL,
AL_EFFECTIVE, ¤t_app);
if (FAILED(hr)) {
LOG(ERROR) << std::hex << hr;
return false;
}
return !base::string16(win8::test::kDefaultTestProgId).compare(current_app);
}
}
namespace win8 {
bool MakeTestDefaultBrowserSynchronously() {
static const wchar_t kDefaultBrowserProtocol[] = L"http";
if (!RegisterTestDefaultBrowser())
return false;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSH, NULL, NULL);
if (IsTestDefaultForProtocol(kDefaultBrowserProtocol))
return true;
std::vector<base::string16> choices;
OpenWithDialogController controller;
HRESULT hr = controller.RunSynchronously(
NULL, kDefaultBrowserProtocol, win8::test::kDefaultTestExeName, &choices);
LOG_IF(ERROR, FAILED(hr)) << std::hex << hr;
DCHECK(IsTestDefaultForProtocol(kDefaultBrowserProtocol));
return SUCCEEDED(hr);
}
}