out_of_process_dynamic_service_runner.cc revision 0de6073388f4e2780db8536178b129cd8f6ab386
1// Copyright 2014 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#include "mojo/shell/out_of_process_dynamic_service_runner.h"
6
7#include "base/bind.h"
8#include "base/callback_helpers.h"
9#include "base/file_util.h"
10#include "base/logging.h"
11#include "base/scoped_native_library.h"
12#include "mojo/public/cpp/bindings/allocation_scope.h"
13
14namespace mojo {
15namespace shell {
16
17OutOfProcessDynamicServiceRunner::OutOfProcessDynamicServiceRunner(
18    Context* context)
19    : context_(context) {
20}
21
22OutOfProcessDynamicServiceRunner::~OutOfProcessDynamicServiceRunner() {
23  if (app_child_process_host_) {
24    // TODO(vtl): Race condition: If |AppChildProcessHost::DidStart()| hasn't
25    // been called yet, we shouldn't call |Join()| here. (Until |DidStart()|, we
26    // may not have a child process to wait on.) Probably we should fix
27    // |Join()|.
28    app_child_process_host_->Join();
29  }
30}
31
32void OutOfProcessDynamicServiceRunner::Start(
33    const base::FilePath& app_path,
34    ScopedMessagePipeHandle service_handle,
35    const base::Closure& app_completed_callback) {
36  app_path_ = app_path;
37
38  DCHECK(!service_handle_.is_valid());
39  service_handle_ = service_handle.Pass();
40
41  DCHECK(app_completed_callback_.is_null());
42  app_completed_callback_ = app_completed_callback;
43
44  app_child_process_host_.reset(new AppChildProcessHost(context_, this));
45  app_child_process_host_->Start();
46
47  // TODO(vtl): Where should my allocation scope be?
48  AllocationScope scope;
49  // TODO(vtl): |app_path.AsUTF8Unsafe()| is unsafe.
50  app_child_process_host_->controller()->StartApp(
51      app_path.AsUTF8Unsafe(),
52      ScopedMessagePipeHandle(MessagePipeHandle(
53          service_handle.release().value())));
54}
55
56void OutOfProcessDynamicServiceRunner::AppCompleted(int32_t result) {
57  DVLOG(2) << "OutOfProcessDynamicServiceRunner::AppCompleted(" << result
58           << ")";
59
60  app_completed_callback_.Run();
61  app_completed_callback_.Reset();
62  app_child_process_host_.reset();
63}
64
65}  // namespace shell
66}  // namespace mojo
67