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.
4
5 // Win8 default browser registration utility.
6 //
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.
10 //
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.
15
16 #include <atlbase.h>
17 #include <atlcom.h>
18 #include <atlctl.h>
19 #include <shellapi.h>
20
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"
29
30 namespace {
31
32 const wchar_t kDelegateExecuteCLSID[] =
33 L"{FC0064A6-D1DE-4A83-92D2-5BB4EEBB70B5}";
34
InitializeCommandLineDefaultValues()35 void InitializeCommandLineDefaultValues() {
36 CommandLine& command_line = *CommandLine::ForCurrentProcess();
37
38 if (!command_line.HasSwitch(win8::test::kTestAppUserModelId))
39 command_line.AppendSwitchNative(win8::test::kTestAppUserModelId,
40 win8::test::kDefaultTestAppUserModelId);
41
42 if (!command_line.HasSwitch(win8::test::kTestExeName))
43 command_line.AppendSwitchNative(win8::test::kTestExeName,
44 win8::test::kDefaultTestExeName);
45
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);
50
51 command_line.AppendSwitchNative(win8::test::kTestExePath,
52 exe_path.value());
53 }
54
55 if (!command_line.HasSwitch(win8::test::kTestProgId))
56 command_line.AppendSwitchNative(win8::test::kTestProgId,
57 win8::test::kDefaultTestProgId);
58 }
59
60 } // namespace
61
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 > {
66 public :
67 typedef ATL::CAtlExeModuleT<TestDelegateExecuteModule> ParentClass;
68
69 DECLARE_REGISTRY_RESOURCEID(IDR_TESTDELEGATEEXECUTE);
70
RegisterServer(BOOL reg_type_lib)71 HRESULT RegisterServer(BOOL reg_type_lib) {
72 return ParentClass::RegisterServer(FALSE);
73 }
74
AddCommonRGSReplacements(IRegistrarBase * registrar)75 virtual HRESULT AddCommonRGSReplacements(IRegistrarBase* registrar) throw() {
76 AtlTrace(L"In %hs\n", __FUNCTION__);
77 HRESULT hr = ParentClass::AddCommonRGSReplacements(registrar);
78 if (FAILED(hr))
79 return hr;
80
81 registrar->AddReplacement(L"DELEGATE_EXECUTE_CLSID", kDelegateExecuteCLSID);
82
83 CommandLine& command_line = *CommandLine::ForCurrentProcess();
84
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());
94
95 base::string16 exe_path(
96 command_line.GetSwitchValueNative(win8::test::kTestExePath));
97
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());
101
102 base::string16 exe_icon(base::StringPrintf(L"%ls,0", exe_path.c_str()));
103 registrar->AddReplacement(L"EXE_ICON", exe_icon.c_str());
104
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());
109
110 ATLASSERT(SUCCEEDED(hr));
111 return hr;
112 }
113 };
114
115 TestDelegateExecuteModule _AtlModule;
116
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> {
123 public:
124 DECLARE_REGISTRY_RESOURCEID(IDR_TESTDELEGATEEXECUTE);
125
126 BEGIN_COM_MAP(TestDefaultBrowserRegistrar)
127 END_COM_MAP()
128 };
129
_tWinMain(HINSTANCE,HINSTANCE,LPTSTR,int nShowCmd)130 extern "C" int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int nShowCmd) {
131 base::AtExitManager exit_manager;
132 CommandLine::Init(0, NULL);
133 InitializeCommandLineDefaultValues();
134
135 HRESULT ret_code = _AtlModule.WinMain(nShowCmd);
136
137 return ret_code;
138 }
139
140 OBJECT_ENTRY_AUTO(__uuidof(TestDefaultBrowserRegistrar),
141 TestDefaultBrowserRegistrar);
142