Index: mojo/apps/js/main.cc |
diff --git a/mojo/apps/js/main.cc b/mojo/apps/js/main.cc |
index b5d4486736d60b2666b176880310c3922d8e8146..d1d0880638bc5277b0a27c3fa811121d49a3204f 100644 |
--- a/mojo/apps/js/main.cc |
+++ b/mojo/apps/js/main.cc |
@@ -1,41 +1,133 @@ |
-// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Copyright 2014 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 <stdio.h> |
+ |
+#include "base/at_exit.h" |
+#include "base/bind.h" |
+#include "base/file_util.h" |
+#include "base/i18n/icu_util.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/path_service.h" |
+#include "gin/converter.h" |
+#include "gin/modules/console.h" |
+#include "gin/modules/module_registry.h" |
+#include "gin/modules/module_runner_delegate.h" |
#include "gin/public/isolate_holder.h" |
-#include "mojo/apps/js/mojo_runner_delegate.h" |
-#include "mojo/public/cpp/system/core.h" |
-#include "mojo/public/cpp/system/macros.h" |
- |
-#if defined(WIN32) |
-#if !defined(CDECL) |
-#define CDECL __cdecl |
-#endif |
-#define MOJO_APPS_JS_EXPORT __declspec(dllexport) |
-#else |
-#define CDECL |
-#define MOJO_APPS_JS_EXPORT __attribute__((visibility("default"))) |
-#endif |
+#include "gin/test/file_runner.h" |
+#include "gin/try_catch.h" |
+#include "mojo/apps/js/bindings/gl/module.h" |
+#include "mojo/apps/js/bindings/threading.h" |
+#include "mojo/bindings/js/core.h" |
+#include "mojo/bindings/js/handle.h" |
+#include "mojo/bindings/js/support.h" |
+#include "mojo/common/data_pipe_utils.h" |
+#include "mojo/public/c/system/main.h" |
+#include "mojo/public/cpp/application/application_delegate.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_runner_chromium.h" |
+#include "mojo/public/cpp/application/interface_factory_impl.h" |
+#include "mojo/public/cpp/application/service_provider_impl.h" |
+#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h" |
namespace mojo { |
namespace apps { |
+namespace { |
+ |
+std::vector<base::FilePath> GetModuleSearchPaths() { |
+ std::vector<base::FilePath> module_base(1); |
+ CHECK(base::GetCurrentDirectory(&module_base[0])); |
+ return module_base; |
+} |
+ |
+class MojoRunnerDelegate : public gin::ModuleRunnerDelegate { |
+ public: |
+ MojoRunnerDelegate() : gin::ModuleRunnerDelegate(GetModuleSearchPaths()) { |
+ AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule); |
+ } |
+ |
+ virtual void UnhandledException(gin::ShellRunner* runner, |
+ gin::TryCatch& try_catch) OVERRIDE { |
+ gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch); |
+ LOG(ERROR) << try_catch.GetStackTrace(); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MojoRunnerDelegate); |
+}; |
+ |
+} // namespace |
+ |
+ |
+class JSApp; |
-void Start(MojoHandle pipe, const std::string& module) { |
- base::MessageLoop loop; |
+class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
+ public: |
+ explicit ContentHandlerImpl(JSApp* app): app_(app) { |
+ } |
+ virtual ~ContentHandlerImpl() {} |
- gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); |
- MojoRunnerDelegate delegate; |
- gin::ShellRunner runner(&delegate, instance.isolate()); |
- delegate.Start(&runner, pipe, module); |
+ private: |
+ virtual void OnConnect( |
+ const mojo::String& url, |
+ URLResponsePtr content, |
+ ServiceProviderPtr service_provider) MOJO_OVERRIDE; |
- base::MessageLoop::current()->Run(); |
+ JSApp* app_; |
+}; |
+ |
+// TODO(hansmuller); does this class define the right lifetime |
+// for its gin members? |
+class JSApp : public ApplicationDelegate { |
+ public: |
+ JSApp() |
+ : isolate_holder_(gin::IsolateHolder::kStrictMode), |
+ content_handler_factory_(this) { |
+ base::i18n::InitializeICU(); |
+ shell_runner_.reset( |
+ new gin::ShellRunner(&runner_delegate_, isolate_holder_.isolate())); |
+ } |
+ |
+ virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
+ MOJO_OVERRIDE { |
+ connection->AddService(&content_handler_factory_); |
+ return true; |
+ } |
+ |
+ void LoadMainJSModule(const std::string& module, const std::string& path) { |
+ gin::Runner::Scope scope(shell_runner_.get()); |
+ v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
+ shell_runner_->Run(module.c_str(), path.c_str()); |
+ } |
+ |
+ private: |
+ gin::IsolateHolder isolate_holder_; |
+ scoped_ptr<gin::ShellRunner> shell_runner_; |
+ apps::MojoRunnerDelegate runner_delegate_; |
+ InterfaceFactoryImplWithContext< |
+ ContentHandlerImpl, JSApp> content_handler_factory_; |
+}; |
+ |
+void ContentHandlerImpl::OnConnect( |
+ const mojo::String& url, |
+ URLResponsePtr content, |
+ ServiceProviderPtr service_provider) { |
+ |
+ std::string path(url.To<std::string>()); |
+ std::string module; |
+ // TODO(hansmuller): load the initial module asynchronously. |
+ // TODO(hansmuller): check the return value and fail gracefully. |
+ common::BlockingCopyToString(content->body.Pass(), &module); |
+ |
+ app_->LoadMainJSModule(module, path); |
} |
} // namespace apps |
} // namespace mojo |
-extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { |
- mojo::apps::Start(pipe, "mojo/apps/js/main"); |
- return MOJO_RESULT_OK; |
+MojoResult MojoMain(MojoHandle shell_handle) { |
+ mojo::ApplicationRunnerChromium runner(new mojo::apps::JSApp()); |
+ return runner.Run(shell_handle); |
} |
+ |