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