summaryrefslogtreecommitdiff
path: root/chromium/services/service_manager/standalone/context.cc
blob: 5b38d52888e4c0a89dd10eae64c18ad147b1530c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2013 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 "services/service_manager/standalone/context.h"

#include <stddef.h>
#include <stdint.h>

#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/process/process_info.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "components/tracing/common/tracing_switches.h"
#include "services/catalog/catalog.h"
#include "services/service_manager/connect_params.h"
#include "services/service_manager/connect_util.h"
#include "services/service_manager/runner/common/switches.h"
#include "services/service_manager/runner/host/service_process_launcher.h"
#include "services/service_manager/service_manager.h"
#include "services/service_manager/standalone/tracer.h"
#include "services/service_manager/switches.h"
#include "services/tracing/public/cpp/provider.h"
#include "services/tracing/public/cpp/switches.h"
#include "services/tracing/public/interfaces/constants.mojom.h"
#include "services/tracing/public/interfaces/tracing.mojom.h"

#if defined(OS_MACOSX)
#include "services/service_manager/public/cpp/standalone_service/mach_broker.h"
#endif

namespace base {
class TaskRunner;
}

namespace service_manager {
namespace {

// Used to ensure we only init once.
class ServiceProcessLauncherFactoryImpl : public ServiceProcessLauncherFactory {
 public:
  ServiceProcessLauncherFactoryImpl(base::TaskRunner* launch_process_runner,
                                    ServiceProcessLauncher::Delegate* delegate)
      : launch_process_runner_(launch_process_runner),
        delegate_(delegate) {
  }

 private:
   std::unique_ptr<ServiceProcessLauncher> Create(
      const base::FilePath& service_path) override {
    return base::MakeUnique<ServiceProcessLauncher>(
        launch_process_runner_, delegate_, service_path);
  }

  base::TaskRunner* launch_process_runner_;
  ServiceProcessLauncher::Delegate* delegate_;
};

void OnInstanceQuit(const std::string& name, const Identity& identity) {
  if (name == identity.name())
    base::MessageLoop::current()->QuitWhenIdle();
}

const char kService[] = "service";

}  // namespace

Context::Context(
    ServiceProcessLauncher::Delegate* service_process_launcher_delegate,
    std::unique_ptr<base::Value> catalog_contents)
    : main_entry_time_(base::Time::Now()) {
  TRACE_EVENT0("service_manager", "Context::Context");
  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();

  bool trace_startup = command_line.HasSwitch(::switches::kTraceStartup);
  if (trace_startup) {
    tracer_.Start(
        command_line.GetSwitchValueASCII(::switches::kTraceStartup),
        command_line.GetSwitchValueASCII(::switches::kTraceStartupDuration),
        "mojo_runner.trace");
  }

  blocking_pool_ = new base::SequencedWorkerPool(
      kThreadPoolMaxThreads, "blocking_pool", base::TaskPriority::USER_VISIBLE);

  std::unique_ptr<ServiceProcessLauncherFactory>
      service_process_launcher_factory =
          base::MakeUnique<ServiceProcessLauncherFactoryImpl>(
              blocking_pool_.get(),
              service_process_launcher_delegate);
  catalog_.reset(new catalog::Catalog(std::move(catalog_contents)));
  service_manager_.reset(
      new ServiceManager(std::move(service_process_launcher_factory),
                         catalog_->TakeService()));

  bool enable_stats_collection_bindings =
      command_line.HasSwitch(tracing::kEnableStatsCollectionBindings);

  if (enable_stats_collection_bindings ||
      command_line.HasSwitch(switches::kEnableTracing)) {
    Identity source_identity = CreateServiceManagerIdentity();
    Identity tracing_identity(tracing::mojom::kServiceName, mojom::kRootUserID);
    tracing::mojom::FactoryPtr factory;
    BindInterface(service_manager(), source_identity, tracing_identity,
                  &factory);
    provider_.InitializeWithFactory(&factory);

    if (command_line.HasSwitch(tracing::kTraceStartup)) {
      tracing::mojom::CollectorPtr coordinator;
      BindInterface(service_manager(), source_identity, tracing_identity,
                    &coordinator);
      tracer_.StartCollectingFromTracingService(std::move(coordinator));
    }

    // Record the service manager startup metrics used for performance testing.
    if (enable_stats_collection_bindings) {
      tracing::mojom::StartupPerformanceDataCollectorPtr collector;
      BindInterface(service_manager(), source_identity, tracing_identity,
                    &collector);
#if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
      // CurrentProcessInfo::CreationTime is only defined on some platforms.
      const base::Time creation_time = base::CurrentProcessInfo::CreationTime();
      collector->SetServiceManagerProcessCreationTime(
          creation_time.ToInternalValue());
#endif
      collector->SetServiceManagerMainEntryPointTime(
          main_entry_time_.ToInternalValue());
    }
  }
}

Context::~Context() { blocking_pool_->Shutdown(); }

void Context::RunCommandLineApplication() {
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(kService))
    Run(command_line->GetSwitchValueASCII(kService));
}

void Context::Run(const std::string& name) {
  service_manager_->SetInstanceQuitCallback(base::Bind(&OnInstanceQuit, name));

  mojom::InterfaceProviderPtr remote_interfaces;
  mojom::InterfaceProviderPtr local_interfaces;

  std::unique_ptr<ConnectParams> params(new ConnectParams);
  params->set_source(CreateServiceManagerIdentity());
  params->set_target(Identity(name, mojom::kRootUserID));
  params->set_remote_interfaces(mojo::MakeRequest(&remote_interfaces));
  service_manager_->Connect(std::move(params));
}

}  // namespace service_manager