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

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/scoped_native_library.h"
#include "mojo/public/platform/native/system_thunks.h"

namespace mojo {
namespace shell {

InProcessDynamicServiceRunner::InProcessDynamicServiceRunner(
    Context* /*context*/)
    : thread_(this, "app_thread") {
}

InProcessDynamicServiceRunner::~InProcessDynamicServiceRunner() {
  if (thread_.HasBeenStarted()) {
    DCHECK(!thread_.HasBeenJoined());
    thread_.Join();
  }
}

void InProcessDynamicServiceRunner::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_runner_.is_null());
  app_completed_callback_runner_ = base::Bind(&base::TaskRunner::PostTask,
                                              base::MessageLoopProxy::current(),
                                              FROM_HERE,
                                              app_completed_callback);

  DCHECK(!thread_.HasBeenStarted());
  thread_.Start();
}

void InProcessDynamicServiceRunner::Run() {
  DVLOG(2) << "Loading/running Mojo app from " << app_path_.value()
           << " in process";

  base::ScopedClosureRunner app_deleter(
      base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false));

  do {
    base::NativeLibraryLoadError error;
    base::ScopedNativeLibrary app_library(
        base::LoadNativeLibrary(app_path_, &error));
    if (!app_library.is_valid()) {
      LOG(ERROR) << "Failed to load library (error: " << error.ToString()
                 << ")";
      break;
    }

    MojoSetSystemThunksFn mojo_set_system_thunks_fn =
        reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer(
            "MojoSetSystemThunks"));
    if (mojo_set_system_thunks_fn) {
      MojoSystemThunks system_thunks = MojoMakeSystemThunks();
      size_t expected_size = mojo_set_system_thunks_fn(&system_thunks);
      if (expected_size > sizeof(MojoSystemThunks)) {
        LOG(ERROR)
            << "Invalid DSO. Expected MojoSystemThunks size: "
            << expected_size;
        break;
      }
    }

    typedef MojoResult (*MojoMainFunction)(MojoHandle);
    MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>(
        app_library.GetFunctionPointer("MojoMain"));
    if (!main_function) {
      LOG(ERROR) << "Entrypoint MojoMain not found";
      break;
    }

    // |MojoMain()| takes ownership of the service handle.
    MojoResult result = main_function(service_handle_.release().value());
    if (result < MOJO_RESULT_OK)
      LOG(ERROR) << "MojoMain returned an error: " << result;
  } while (false);

  bool success = app_completed_callback_runner_.Run();
  app_completed_callback_runner_.Reset();
  LOG_IF(ERROR, !success) << "Failed post run app_completed_callback";
}

}  // namespace shell
}  // namespace mojo