1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "mojo/apps/js/mojo_runner_delegate.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/bind.h"
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/path_service.h"
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/converter.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/modules/console.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/modules/module_registry.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "gin/modules/timer.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/try_catch.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/apps/js/bindings/gl/module.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/apps/js/bindings/monotonic_clock.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "mojo/apps/js/bindings/threading.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/bindings/js/core.h"
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "mojo/bindings/js/handle.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/bindings/js/support.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace mojo {
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace apps {
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace {
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// TODO(abarth): Rather than loading these modules from the file system, we
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// should load them from the network via Mojo IPC.
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)std::vector<base::FilePath> GetModuleSearchPaths() {
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::vector<base::FilePath> search_paths(2);
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PathService::Get(base::DIR_EXE, &search_paths[1]);
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  search_paths[1] = search_paths[1].AppendASCII("gen");
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return search_paths;
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void StartCallback(base::WeakPtr<gin::Runner> runner,
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                   MojoHandle pipe,
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                   v8::Handle<v8::Value> module) {
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  v8::Isolate* isolate = runner->GetContextHolder()->isolate();
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  v8::Handle<v8::Function> start;
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  CHECK(gin::ConvertFromV8(isolate, module, &start));
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  v8::Handle<v8::Value> args[] = {
44010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      gin::ConvertToV8(isolate, Handle(pipe)) };
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  runner->Call(start, runner->global(), 1, args);
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)MojoRunnerDelegate::MojoRunnerDelegate()
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : ModuleRunnerDelegate(GetModuleSearchPaths()) {
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule);
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule);
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule);
56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  AddBuiltinModule(js::gl::kModuleName, js::gl::GetModule);
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule);
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddBuiltinModule(Threading::kModuleName, Threading::GetModule);
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)MojoRunnerDelegate::~MojoRunnerDelegate() {
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void MojoRunnerDelegate::Start(gin::Runner* runner,
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               MojoHandle pipe,
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               const std::string& module) {
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gin::Runner::Scope scope(runner);
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gin::ModuleRegistry* registry =
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      gin::ModuleRegistry::From(runner->GetContextHolder()->context());
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  registry->LoadModule(runner->GetContextHolder()->isolate(), module,
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                       base::Bind(StartCallback, runner->GetWeakPtr(), pipe));
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  AttemptToLoadMoreModules(runner);
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void MojoRunnerDelegate::UnhandledException(gin::ShellRunner* runner,
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                            gin::TryCatch& try_catch) {
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch);
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  LOG(ERROR) << try_catch.GetStackTrace();
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace apps
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace mojo
83