mojo_runner_delegate.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright 2013 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/apps/js/mojo_runner_delegate.h"
6
7#include "base/bind.h"
8#include "base/path_service.h"
9#include "gin/converter.h"
10#include "gin/modules/console.h"
11#include "gin/modules/module_registry.h"
12#include "gin/modules/timer.h"
13#include "gin/try_catch.h"
14#include "mojo/apps/js/bindings/gl/module.h"
15#include "mojo/apps/js/bindings/monotonic_clock.h"
16#include "mojo/apps/js/bindings/threading.h"
17#include "mojo/bindings/js/core.h"
18#include "mojo/bindings/js/handle.h"
19#include "mojo/bindings/js/support.h"
20
21namespace mojo {
22namespace apps {
23
24namespace {
25
26// TODO(abarth): Rather than loading these modules from the file system, we
27// should load them from the network via Mojo IPC.
28std::vector<base::FilePath> GetModuleSearchPaths() {
29  std::vector<base::FilePath> search_paths(2);
30  PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
31  PathService::Get(base::DIR_EXE, &search_paths[1]);
32  search_paths[1] = search_paths[1].AppendASCII("gen");
33  return search_paths;
34}
35
36void StartCallback(base::WeakPtr<gin::Runner> runner,
37                   MojoHandle pipe,
38                   v8::Handle<v8::Value> module) {
39  v8::Isolate* isolate = runner->GetContextHolder()->isolate();
40  v8::Handle<v8::Function> start;
41  CHECK(gin::ConvertFromV8(isolate, module, &start));
42
43  v8::Handle<v8::Value> args[] = {
44      gin::ConvertToV8(isolate, mojo::Handle(pipe)) };
45  runner->Call(start, runner->global(), 1, args);
46}
47
48}  // namespace
49
50MojoRunnerDelegate::MojoRunnerDelegate()
51    : ModuleRunnerDelegate(GetModuleSearchPaths()) {
52  AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
53  AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
54  AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule);
55  AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule);
56  AddBuiltinModule(mojo::js::gl::kModuleName, mojo::js::gl::GetModule);
57  AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule);
58  AddBuiltinModule(Threading::kModuleName, Threading::GetModule);
59}
60
61MojoRunnerDelegate::~MojoRunnerDelegate() {
62}
63
64void MojoRunnerDelegate::Start(gin::Runner* runner,
65                               MojoHandle pipe,
66                               const std::string& module) {
67  gin::Runner::Scope scope(runner);
68  gin::ModuleRegistry* registry =
69      gin::ModuleRegistry::From(runner->GetContextHolder()->context());
70  registry->LoadModule(runner->GetContextHolder()->isolate(), module,
71                       base::Bind(StartCallback, runner->GetWeakPtr(), pipe));
72  AttemptToLoadMoreModules(runner);
73}
74
75void MojoRunnerDelegate::UnhandledException(gin::ShellRunner* runner,
76                                            gin::TryCatch& try_catch) {
77  gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch);
78  LOG(ERROR) << try_catch.GetStackTrace();
79}
80
81}  // namespace apps
82}  // namespace mojo
83