summaryrefslogtreecommitdiffstats
path: root/chrome/app_shim/win/app_shim_main.cc
blob: 5c6c863d079ba1320eda8a50521ef52fc651d8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>

#include <string>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/process/launch.h"
#include "chrome/installer/launcher_support/chrome_launcher_support.h"

namespace {

// Return codes.
const int kOK = 0;
const int kNoProgram = 1;
const int kLaunchFailure = 2;

// Command-line switches.
const char kChromeSxS[] = "chrome-sxs";

base::FilePath GetChromeExePath(bool is_canary) {
  return is_canary ? chrome_launcher_support::GetAnyChromeSxSPath()
                   : chrome_launcher_support::GetAnyChromePath();
}

}  // namespace

// This program runs chrome.exe, passing its arguments on to the Chrome binary.
// It uses the Windows registry to find chrome.exe (and hence it only works if
// Chrome/Chromium has been properly installed). It terminates as soon as the
// program is launched. It is intended to allow file types to be associated with
// Chrome apps, with a custom name (and in some cases, icon) rather than simply
// the name of the Chrome binary.
//
// Usage: app_shim_win [--chrome-sxs] -- [CHROME_ARGS...]
//
// The -- is required if switches are to be passed to Chrome; any switches
// before the -- will be interpreted by app_shim_win, and not passed to Chrome.
//
// If --chrome-sxs is specified, looks for the SxS (Canary) build of Chrome.
// This will always fail for Chromium.
int WINAPI wWinMain(HINSTANCE instance,
                    HINSTANCE prev_instance,
                    wchar_t* /*command_line*/,
                    int show_command) {
  CommandLine::Init(0, nullptr);

  // Log to stderr. Otherwise it will log to a file by default.
  logging::LoggingSettings logging_settings;
  logging_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  logging::InitLogging(logging_settings);

  // Get the command-line for the Chrome binary.
  // --chrome-sxs on the command line means we should run the canary binary.
  bool is_canary =
      base::CommandLine::ForCurrentProcess()->HasSwitch(kChromeSxS);
  base::FilePath chrome_path = GetChromeExePath(is_canary);
  if (chrome_path.empty()) {
    LOG(ERROR) << "Could not find chrome.exe path in the registry.";
    return kNoProgram;
  }
  CommandLine command_line(chrome_path);

  // Get the command-line arguments for the subprocess, consisting of the
  // arguments (but not switches) to this binary. This gets everything after the
  // "--".
  for (const auto& arg : CommandLine::ForCurrentProcess()->GetArgs())
    command_line.AppendArgNative(arg);

  if (!base::LaunchProcess(command_line, base::LaunchOptions(), nullptr)) {
    LOG(ERROR) << "Could not run chrome.exe.";
    return kLaunchFailure;
  }

  return kOK;
}