summaryrefslogtreecommitdiffstats
path: root/mojo/shell/out_of_process_dynamic_service_runner.cc
blob: a907c85312f8a49fa9755f8a2af1888b0ac12df5 (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
// 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 "mojo/shell/out_of_process_dynamic_service_runner.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/scoped_native_library.h"
#include "mojo/public/cpp/bindings/allocation_scope.h"

namespace mojo {
namespace shell {

OutOfProcessDynamicServiceRunner::OutOfProcessDynamicServiceRunner(
    Context* context)
    : context_(context) {
}

OutOfProcessDynamicServiceRunner::~OutOfProcessDynamicServiceRunner() {
  if (app_child_process_host_) {
    // TODO(vtl): Race condition: If |AppChildProcessHost::DidStart()| hasn't
    // been called yet, we shouldn't call |Join()| here. (Until |DidStart()|, we
    // may not have a child process to wait on.) Probably we should fix
    // |Join()|.
    app_child_process_host_->Join();
  }
}

void OutOfProcessDynamicServiceRunner::Start(
    const base::FilePath& app_path,
    ScopedShellHandle service_handle,
    const base::Closure& app_completed_callback) {
  app_path_ = app_path;

  DCHECK(!service_handle_.is_valid());
  service_handle_ = service_handle.Pass();

  DCHECK(app_completed_callback_.is_null());
  app_completed_callback_ = app_completed_callback;

  app_child_process_host_.reset(new AppChildProcessHost(context_, this));
  app_child_process_host_->Start();

  // TODO(vtl): Where should my allocation scope be?
  AllocationScope scope;
  // TODO(vtl): |app_path.AsUTF8Unsafe()| is unsafe.
  app_child_process_host_->controller()->StartApp(
      app_path.AsUTF8Unsafe(),
      ScopedMessagePipeHandle(MessagePipeHandle(
          service_handle.release().value())));
}

void OutOfProcessDynamicServiceRunner::AppCompleted(int32_t result) {
  DVLOG(2) << "OutOfProcessDynamicServiceRunner::AppCompleted(" << result
           << ")";

  app_completed_callback_.Run();
  app_completed_callback_.Reset();
  app_child_process_host_.reset();
}

}  // namespace shell
}  // namespace mojo