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// Makes a given program ("Google Chrome" by default) the default handler for
6// some URL protocol ("http" by default) on Windows 8. These defaults can be
7// overridden via the --program and --protocol command line switches.
8
9#include <windows.h>
10
11#include "base/at_exit.h"
12#include "base/command_line.h"
13#include "base/logging.h"
14#include "base/strings/string16.h"
15#include "base/strings/string_util.h"
16#include "ui/base/win/atl_module.h"
17#include "win8/test/open_with_dialog_controller.h"
18
19namespace {
20
21const char kSwitchProgram[] = "program";
22const char kSwitchProtocol[] = "protocol";
23const wchar_t kDefaultProgram[] = L"Google Chrome";
24const wchar_t kDefaultProtocol[] = L"http";
25
26}  // namespace
27
28extern "C"
29int wmain(int argc, wchar_t* argv[]) {
30  // Initialize the commandline singleton from the environment.
31  CommandLine::Init(0, NULL);
32  // The exit manager is in charge of calling the dtors of singletons.
33  base::AtExitManager exit_manager;
34  logging::LoggingSettings settings;
35  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
36  logging::InitLogging(settings);
37  logging::SetMinLogLevel(logging::LOG_VERBOSE);
38
39  ui::win::CreateATLModuleIfNeeded();
40
41  CommandLine* command_line = CommandLine::ForCurrentProcess();
42  base::string16 protocol(command_line->GetSwitchValueNative(kSwitchProtocol));
43  if (protocol.empty())
44    protocol = kDefaultProtocol;
45
46  base::string16 program(command_line->GetSwitchValueNative(kSwitchProgram));
47  if (program.empty())
48    program = kDefaultProgram;
49
50  std::vector<base::string16> choices;
51  HRESULT result = S_OK;
52  win8::OpenWithDialogController controller;
53  result = controller.RunSynchronously(NULL, protocol, program, &choices);
54
55  if (SUCCEEDED(result)) {
56    printf("success\n");
57  } else if (!choices.empty()) {
58    printf("failed to set program. possible choices: %ls\n",
59           JoinString(choices, L", ").c_str());
60  } else {
61    printf("failed with HRESULT: %0x08X\n", result);
62  }
63
64  return FAILED(result);
65}
66