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