diff options
Diffstat (limited to 'mojo/apps')
-rw-r--r-- | mojo/apps/js/DEPS | 1 | ||||
-rw-r--r-- | mojo/apps/js/bootstrap.cc | 54 | ||||
-rw-r--r-- | mojo/apps/js/bootstrap.h | 26 | ||||
-rw-r--r-- | mojo/apps/js/main.cc | 32 | ||||
-rw-r--r-- | mojo/apps/js/main.js | 29 | ||||
-rw-r--r-- | mojo/apps/js/mojo_runner_delegate.cc | 50 | ||||
-rw-r--r-- | mojo/apps/js/mojo_runner_delegate.h | 30 |
7 files changed, 219 insertions, 3 deletions
diff --git a/mojo/apps/js/DEPS b/mojo/apps/js/DEPS index 0b70f92..d974b68 100644 --- a/mojo/apps/js/DEPS +++ b/mojo/apps/js/DEPS @@ -1,5 +1,4 @@ include_rules = [ "+gin", "+v8", - "-base", ] diff --git a/mojo/apps/js/bootstrap.cc b/mojo/apps/js/bootstrap.cc new file mode 100644 index 0000000..f724b2d --- /dev/null +++ b/mojo/apps/js/bootstrap.cc @@ -0,0 +1,54 @@ +// 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/bootstrap.h" + +#include "base/message_loop/message_loop.h" +#include "gin/per_isolate_data.h" +#include "mojo/public/bindings/js/handle.h" + +namespace mojo { +namespace apps { + +namespace { + +void Quit(const v8::FunctionCallbackInfo<v8::Value>& info) { + base::MessageLoop::current()->QuitNow(); +} + +MojoHandle g_initial_handle = MOJO_HANDLE_INVALID; + +gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; + +} // namespace + +const char Bootstrap::kModuleName[] = "mojo/apps/js/bootstrap"; + +v8::Local<v8::ObjectTemplate> Bootstrap::GetTemplate(v8::Isolate* isolate) { + gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); + v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( + &g_wrapper_info); + + if (templ.IsEmpty()) { + templ = v8::ObjectTemplate::New(); + templ->Set(gin::StringToSymbol(isolate, "quit"), + v8::FunctionTemplate::New(Quit)); + + // Don't forget to call SetInitialHandle before getting the template. + DCHECK(g_initial_handle != MOJO_HANDLE_INVALID); + templ->Set(gin::StringToSymbol(isolate, "initialHandle"), + gin::ConvertToV8(isolate, g_initial_handle)); + + data->SetObjectTemplate(&g_wrapper_info, templ); + } + + return templ; +} + +void Bootstrap::SetInitialHandle(MojoHandle pipe) { + g_initial_handle = pipe; +} + +} // namespace apps +} // namespace mojo diff --git a/mojo/apps/js/bootstrap.h b/mojo/apps/js/bootstrap.h new file mode 100644 index 0000000..4355760a --- /dev/null +++ b/mojo/apps/js/bootstrap.h @@ -0,0 +1,26 @@ +// 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. + +#ifndef MOJO_APPS_JS_BOOTSTRAP_H_ +#define MOJO_APPS_JS_BOOTSTRAP_H_ + +#include "mojo/public/system/core.h" +#include "v8/include/v8.h" + +namespace mojo { +namespace apps { + +class Bootstrap { + public: + static const char kModuleName[]; + static v8::Local<v8::ObjectTemplate> GetTemplate(v8::Isolate* isolate); + + // Must be called before the first call to GetTemplate. + static void SetInitialHandle(MojoHandle handle); +}; + +} // namespace apps +} // namespace mojo + +#endif // MOJO_APPS_JS_BOOTSTRAP_H_ diff --git a/mojo/apps/js/main.cc b/mojo/apps/js/main.cc index 1ad084b..0706182 100644 --- a/mojo/apps/js/main.cc +++ b/mojo/apps/js/main.cc @@ -2,7 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/message_loop/message_loop.h" #include "gin/public/isolate_holder.h" +#include "mojo/apps/js/bootstrap.h" +#include "mojo/apps/js/mojo_runner_delegate.h" +#include "mojo/common/bindings_support_impl.h" #include "mojo/public/system/core_cpp.h" #include "mojo/public/system/macros.h" @@ -16,8 +20,32 @@ #define MOJO_APPS_JS_EXPORT __attribute__((visibility("default"))) #endif -extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { +namespace mojo { +namespace apps { + +void RunMojoJS(MojoHandle pipe) { gin::IsolateHolder instance; - // TODO(abarth): Load JS off the network and execute it. + Bootstrap::SetInitialHandle(pipe); + + MojoRunnerDelegate delegate; + gin::Runner runner(&delegate, instance.isolate()); + + { + gin::Runner::Scope scope(&runner); + runner.Run("define(['mojo/apps/js/main'], function(main) {});"); + } + + base::MessageLoop::current()->Run(); +} + +} // namespace apps +} // namespace mojo + +extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { + base::MessageLoop loop; + mojo::common::BindingsSupportImpl bindings_support; + mojo::BindingsSupport::Set(&bindings_support); + mojo::apps::RunMojoJS(pipe); + mojo::BindingsSupport::Set(NULL); return MOJO_RESULT_OK; } diff --git a/mojo/apps/js/main.js b/mojo/apps/js/main.js new file mode 100644 index 0000000..a04048a --- /dev/null +++ b/mojo/apps/js/main.js @@ -0,0 +1,29 @@ +// 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. + +define([ + "console", + "mojo/apps/js/bootstrap", + "mojo/public/bindings/js/connector", + "mojom/hello_world_service", +], function(console, bootstrap, connector, hello) { + + function HelloWorldClientImpl() { + } + + HelloWorldClientImpl.prototype = + Object.create(hello.HelloWorldClientStub.prototype); + + HelloWorldClientImpl.prototype.didReceiveGreeting = function(result) { + console.log("DidReceiveGreeting from pipe: " + result); + connection.close(); + bootstrap.quit(); + }; + + var connection = new connector.Connection(bootstrap.initialHandle, + HelloWorldClientImpl, + hello.HelloWorldServiceProxy); + + connection.remote.greeting("hello, world!"); +}); diff --git a/mojo/apps/js/mojo_runner_delegate.cc b/mojo/apps/js/mojo_runner_delegate.cc new file mode 100644 index 0000000..ba085e5 --- /dev/null +++ b/mojo/apps/js/mojo_runner_delegate.cc @@ -0,0 +1,50 @@ +// 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/path_service.h" +#include "gin/modules/console.h" +#include "gin/modules/module_registry.h" +#include "gin/try_catch.h" +#include "mojo/apps/js/bootstrap.h" +#include "mojo/public/bindings/js/core.h" +#include "mojo/public/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; +} + +} // namespace + +MojoRunnerDelegate::MojoRunnerDelegate() + : ModuleRunnerDelegate(GetModuleSearchPaths()) { + AddBuiltinModule(Bootstrap::kModuleName, Bootstrap::GetTemplate); + AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetTemplate); + AddBuiltinModule(js::Core::kModuleName, js::Core::GetTemplate); + AddBuiltinModule(js::Support::kModuleName, js::Support::GetTemplate); +} + +MojoRunnerDelegate::~MojoRunnerDelegate() { +} + +void MojoRunnerDelegate::UnhandledException(gin::Runner* runner, + gin::TryCatch& try_catch) { + gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch); + LOG(ERROR) << try_catch.GetPrettyMessage(); +} + +} // namespace apps +} // namespace mojo diff --git a/mojo/apps/js/mojo_runner_delegate.h b/mojo/apps/js/mojo_runner_delegate.h new file mode 100644 index 0000000..899ef01 --- /dev/null +++ b/mojo/apps/js/mojo_runner_delegate.h @@ -0,0 +1,30 @@ +// 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. + +#ifndef MOJO_APPS_JS_MOJO_RUNNER_DELEGATE_H_ +#define MOJO_APPS_JS_MOJO_RUNNER_DELEGATE_H_ + +#include "base/compiler_specific.h" +#include "gin/modules/module_runner_delegate.h" + +namespace mojo { +namespace apps { + +class MojoRunnerDelegate : public gin::ModuleRunnerDelegate { + public: + MojoRunnerDelegate(); + virtual ~MojoRunnerDelegate(); + + private: + // From ModuleRunnerDelegate: + virtual void UnhandledException(gin::Runner* runner, + gin::TryCatch& try_catch) OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(MojoRunnerDelegate); +}; + +} // namespace apps +} // namespace mojo + +#endif // MOJO_APPS_JS_MOJO_RUNNER_DELEGATE_H_ |