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
3 // found in the LICENSE file.
5 // Win8 default browser registration utility.
7 // This tool can register and unregister a given exe as a potential default
8 // metro browser on Win8. It does not make the exe become THE default browser,
9 // for a mechnism to do this please see open_with_dialog_controller.h.
11 // TODO(robertshield): By default, this creates a run-time dependency on
12 // chrome.exe since it's the only thing we have right now that works as a
13 // default viewer process. Investigate extracting the metro init code and
14 // building them into a standalone viewer process.
21 #include "base/at_exit.h"
22 #include "base/command_line.h"
23 #include "base/files/file_path.h"
24 #include "base/memory/scoped_ptr.h"
25 #include "base/path_service.h"
26 #include "base/strings/stringprintf.h"
27 #include "win8/test/test_registrar_constants.h"
28 #include "win8/test/test_registrar_resource.h"
32 const wchar_t kDelegateExecuteCLSID
[] =
33 L
"{FC0064A6-D1DE-4A83-92D2-5BB4EEBB70B5}";
35 void InitializeCommandLineDefaultValues() {
36 CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
38 if (!command_line
.HasSwitch(win8::test::kTestAppUserModelId
))
39 command_line
.AppendSwitchNative(win8::test::kTestAppUserModelId
,
40 win8::test::kDefaultTestAppUserModelId
);
42 if (!command_line
.HasSwitch(win8::test::kTestExeName
))
43 command_line
.AppendSwitchNative(win8::test::kTestExeName
,
44 win8::test::kDefaultTestExeName
);
46 if (!command_line
.HasSwitch(win8::test::kTestExePath
)) {
47 base::FilePath exe_path
;
48 PathService::Get(base::DIR_EXE
, &exe_path
);
49 exe_path
= exe_path
.Append(win8::test::kDefaultTestExePath
);
51 command_line
.AppendSwitchNative(win8::test::kTestExePath
,
55 if (!command_line
.HasSwitch(win8::test::kTestProgId
))
56 command_line
.AppendSwitchNative(win8::test::kTestProgId
,
57 win8::test::kDefaultTestProgId
);
62 // Implementation of an ATL module that provides the necessary replacement
63 // values for the default browser .rgs script.
64 class TestDelegateExecuteModule
65 : public ATL::CAtlExeModuleT
< TestDelegateExecuteModule
> {
67 typedef ATL::CAtlExeModuleT
<TestDelegateExecuteModule
> ParentClass
;
69 DECLARE_REGISTRY_RESOURCEID(IDR_TESTDELEGATEEXECUTE
);
71 HRESULT
RegisterServer(BOOL reg_type_lib
) {
72 return ParentClass::RegisterServer(FALSE
);
75 virtual HRESULT
AddCommonRGSReplacements(IRegistrarBase
* registrar
) throw() {
76 AtlTrace(L
"In %hs\n", __FUNCTION__
);
77 HRESULT hr
= ParentClass::AddCommonRGSReplacements(registrar
);
81 registrar
->AddReplacement(L
"DELEGATE_EXECUTE_CLSID", kDelegateExecuteCLSID
);
83 CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
85 registrar
->AddReplacement(L
"APP_USER_MODEL_ID",
86 command_line
.GetSwitchValueNative(
87 win8::test::kTestAppUserModelId
).c_str());
88 registrar
->AddReplacement(L
"EXE_NAME",
89 command_line
.GetSwitchValueNative(
90 win8::test::kTestExeName
).c_str());
91 registrar
->AddReplacement(L
"PROG_ID",
92 command_line
.GetSwitchValueNative(
93 win8::test::kTestProgId
).c_str());
95 base::string16
exe_path(
96 command_line
.GetSwitchValueNative(win8::test::kTestExePath
));
98 base::string16
exe_open_command(
99 base::StringPrintf(L
"\"%ls\" -- %%*", exe_path
.c_str()));
100 registrar
->AddReplacement(L
"EXE_OPEN_COMMAND", exe_open_command
.c_str());
102 base::string16
exe_icon(base::StringPrintf(L
"%ls,0", exe_path
.c_str()));
103 registrar
->AddReplacement(L
"EXE_ICON", exe_icon
.c_str());
105 base::string16
prog_id_open_command(
106 base::StringPrintf(L
"\"%ls\" -- \"%%1\"", exe_path
.c_str()));
107 registrar
->AddReplacement(L
"PROG_ID_OPEN_COMMAND",
108 prog_id_open_command
.c_str());
110 ATLASSERT(SUCCEEDED(hr
));
115 TestDelegateExecuteModule _AtlModule
;
117 EXTERN_C
const GUID CLSID_TestDefaultBrowserRegistrar
;
118 class ATL_NO_VTABLE
DECLSPEC_UUID("FC0064A6-D1DE-4A83-92D2-5BB4EEBB70B5")
119 TestDefaultBrowserRegistrar
120 : public CComObjectRootEx
<CComSingleThreadModel
>,
121 public CComCoClass
<TestDefaultBrowserRegistrar
,
122 &CLSID_TestDefaultBrowserRegistrar
> {
124 DECLARE_REGISTRY_RESOURCEID(IDR_TESTDELEGATEEXECUTE
);
126 BEGIN_COM_MAP(TestDefaultBrowserRegistrar
)
130 extern "C" int WINAPI
_tWinMain(HINSTANCE
, HINSTANCE
, LPTSTR
, int nShowCmd
) {
131 base::AtExitManager exit_manager
;
132 CommandLine::Init(0, NULL
);
133 InitializeCommandLineDefaultValues();
135 HRESULT ret_code
= _AtlModule
.WinMain(nShowCmd
);
140 OBJECT_ENTRY_AUTO(__uuidof(TestDefaultBrowserRegistrar
),
141 TestDefaultBrowserRegistrar
);