summaryrefslogtreecommitdiff
path: root/chromium/mash/runner/main.cc
blob: 83d4e761e8eee870155447d7ddc26bfbb9371410 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2016 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 <memory>
#include <string>
#include <utility>

#include "base/at_exit.h"
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/i18n/icu_util.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/scoped_ipc_support.h"
#include "services/service_manager/runner/init.h"
#include "services/service_manager/standalone/context.h"
#include "services/service_manager/switches.h"

namespace {

const base::FilePath::CharType kMashCatalogFilename[] =
    FILE_PATH_LITERAL("mash_catalog.json");

}  // namespace

int main(int argc, char** argv) {
  base::CommandLine::Init(argc, argv);

  base::AtExitManager at_exit;
  service_manager::InitializeLogging();
  service_manager::WaitForDebuggerIfNecessary();

#if !defined(OFFICIAL_BUILD) && defined(OS_WIN)
  base::RouteStdioToConsole(false);
#endif

#if !defined(OFFICIAL_BUILD)
  base::debug::EnableInProcessStackDumping();
#endif
  base::PlatformThread::SetName("service_manager");

  std::string catalog_contents;
  base::FilePath exe_path;
  base::PathService::Get(base::DIR_EXE, &exe_path);
  base::FilePath catalog_path = exe_path.Append(kMashCatalogFilename);
  bool result = base::ReadFileToString(catalog_path, &catalog_contents);
  DCHECK(result);
  std::unique_ptr<base::Value> manifest_value =
      base::JSONReader::Read(catalog_contents);
  DCHECK(manifest_value);

#if defined(OS_WIN) && defined(COMPONENT_BUILD)
  // In Windows component builds, ensure that loaded service binaries always
  // search this exe's dir for DLLs.
  SetDllDirectory(exe_path.value().c_str());
#endif

  base::i18n::InitializeICU();

  mojo::edk::Init();

  base::Thread ipc_thread("IPC thread");
  ipc_thread.StartWithOptions(
      base::Thread::Options(base::MessageLoop::TYPE_IO, 0));

  // We can use fast IPC shutdown here since service manager termination must
  // effectively bring down all services as well.
  mojo::edk::ScopedIPCSupport ipc_support(
      ipc_thread.task_runner(),
      mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST);

  base::MessageLoop message_loop;
  service_manager::Context service_manager_context(nullptr,
                                                   std::move(manifest_value));
  message_loop.task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&service_manager::Context::RunCommandLineApplication,
                 base::Unretained(&service_manager_context)));
  base::RunLoop().Run();
  return 0;
}