blob: 895f2e1044fd1956fb4a6c37b99076214680fa0a [file] [log] [blame]
// Copyright 2015 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/ui/launcher/launcher_app.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "mojo/application/application_runner_chromium.h"
#include "mojo/common/tracing_impl.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/public/cpp/application/application_impl.h"
namespace launcher {
LauncherApp::LauncherApp() : app_impl_(nullptr), next_id_(0u) {}
LauncherApp::~LauncherApp() {}
void LauncherApp::Initialize(mojo::ApplicationImpl* app_impl) {
app_impl_ = app_impl;
auto command_line = base::CommandLine::ForCurrentProcess();
command_line->InitFromArgv(app_impl_->args());
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings);
tracing_.Initialize(app_impl_);
TRACE_EVENT0("launcher", __func__);
for (size_t i = 0; i < command_line->GetArgs().size(); ++i) {
Launch(command_line->GetArgs()[i]);
}
}
bool LauncherApp::ConfigureIncomingConnection(
mojo::ApplicationConnection* connection) {
// Only present the launcher interface to the shell.
if (connection->GetRemoteApplicationURL().empty()) {
connection->AddService<Launcher>(this);
}
return true;
}
void LauncherApp::Create(mojo::ApplicationConnection* connection,
mojo::InterfaceRequest<Launcher> request) {
bindings_.AddBinding(this, request.Pass());
}
void LauncherApp::Launch(const mojo::String& application_url) {
uint32_t next_id = next_id_++;
std::unique_ptr<LaunchInstance> instance(new LaunchInstance(
app_impl_, application_url, base::Bind(&LauncherApp::OnLaunchTermination,
base::Unretained(this), next_id)));
instance->Launch();
launch_instances_.emplace(next_id, std::move(instance));
}
void LauncherApp::OnLaunchTermination(uint32_t id) {
launch_instances_.erase(id);
if (launch_instances_.empty()) {
app_impl_->Terminate();
}
}
} // namespace launcher