summaryrefslogtreecommitdiffstats
path: root/mojo/apps/js/mojo_runner_delegate.cc
blob: 2a6c1bd5d9ee66479b6210f766f8a16d17faa959 (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
// 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 "mojo/apps/js/mojo_runner_delegate.h"

#include "base/bind.h"
#include "base/path_service.h"
#include "gin/converter.h"
#include "gin/modules/console.h"
#include "gin/modules/module_registry.h"
#include "gin/modules/timer.h"
#include "gin/try_catch.h"
#include "mojo/apps/js/bindings/gl/module.h"
#include "mojo/apps/js/bindings/monotonic_clock.h"
#include "mojo/apps/js/bindings/threading.h"
#include "mojo/bindings/js/core.h"
#include "mojo/bindings/js/support.h"

namespace mojo {
namespace apps {

namespace {

// TODO(abarth): Rather than loading these modules from the file system, we
// should load them from the network via Mojo IPC.
std::vector<base::FilePath> GetModuleSearchPaths() {
  std::vector<base::FilePath> search_paths(2);
  PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
  PathService::Get(base::DIR_EXE, &search_paths[1]);
  search_paths[1] = search_paths[1].AppendASCII("gen");
  return search_paths;
}

void StartCallback(base::WeakPtr<gin::Runner> runner,
                   MojoHandle pipe,
                   v8::Handle<v8::Value> module) {
  v8::Isolate* isolate = runner->GetContextHolder()->isolate();
  v8::Handle<v8::Function> start;
  CHECK(gin::ConvertFromV8(isolate, module, &start));

  v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, pipe) };
  runner->Call(start, runner->global(), 1, args);
}

}  // namespace

MojoRunnerDelegate::MojoRunnerDelegate()
    : ModuleRunnerDelegate(GetModuleSearchPaths()) {
  AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
  AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
  AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule);
  AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule);
  AddBuiltinModule(mojo::js::gl::kModuleName, mojo::js::gl::GetModule);
  AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule);
  AddBuiltinModule(Threading::kModuleName, Threading::GetModule);
}

MojoRunnerDelegate::~MojoRunnerDelegate() {
}

void MojoRunnerDelegate::Start(gin::Runner* runner,
                               MojoHandle pipe,
                               const std::string& module) {
  gin::Runner::Scope scope(runner);
  gin::ModuleRegistry* registry =
      gin::ModuleRegistry::From(runner->GetContextHolder()->context());
  registry->LoadModule(runner->GetContextHolder()->isolate(), module,
                       base::Bind(StartCallback, runner->GetWeakPtr(), pipe));
  AttemptToLoadMoreModules(runner);
}

void MojoRunnerDelegate::UnhandledException(gin::ShellRunner* runner,
                                            gin::TryCatch& try_catch) {
  gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch);
  LOG(ERROR) << try_catch.GetStackTrace();
}

}  // namespace apps
}  // namespace mojo