OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stdio.h> |
| 6 |
| 7 #include "base/at_exit.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/i18n/icu_util.h" |
5 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/path_service.h" |
| 13 #include "gin/converter.h" |
| 14 #include "gin/modules/console.h" |
| 15 #include "gin/modules/module_registry.h" |
| 16 #include "gin/modules/module_runner_delegate.h" |
6 #include "gin/public/isolate_holder.h" | 17 #include "gin/public/isolate_holder.h" |
7 #include "mojo/apps/js/mojo_runner_delegate.h" | 18 #include "gin/test/file_runner.h" |
8 #include "mojo/public/cpp/system/core.h" | 19 #include "gin/try_catch.h" |
9 #include "mojo/public/cpp/system/macros.h" | 20 #include "mojo/apps/js/bindings/gl/module.h" |
10 | 21 #include "mojo/apps/js/bindings/threading.h" |
11 #if defined(WIN32) | 22 #include "mojo/bindings/js/core.h" |
12 #if !defined(CDECL) | 23 #include "mojo/bindings/js/handle.h" |
13 #define CDECL __cdecl | 24 #include "mojo/bindings/js/support.h" |
14 #endif | 25 #include "mojo/common/data_pipe_utils.h" |
15 #define MOJO_APPS_JS_EXPORT __declspec(dllexport) | 26 #include "mojo/public/c/system/main.h" |
16 #else | 27 #include "mojo/public/cpp/application/application_delegate.h" |
17 #define CDECL | 28 #include "mojo/public/cpp/application/application_impl.h" |
18 #define MOJO_APPS_JS_EXPORT __attribute__((visibility("default"))) | 29 #include "mojo/public/cpp/application/application_runner_chromium.h" |
19 #endif | 30 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 31 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 32 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" |
20 | 33 |
21 namespace mojo { | 34 namespace mojo { |
22 namespace apps { | 35 namespace apps { |
| 36 namespace { |
23 | 37 |
24 void Start(MojoHandle pipe, const std::string& module) { | 38 std::vector<base::FilePath> GetModuleSearchPaths() { |
25 base::MessageLoop loop; | 39 std::vector<base::FilePath> module_base(1); |
| 40 CHECK(base::GetCurrentDirectory(&module_base[0])); |
| 41 return module_base; |
| 42 } |
26 | 43 |
27 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); | 44 class MojoRunnerDelegate : public gin::ModuleRunnerDelegate { |
28 MojoRunnerDelegate delegate; | 45 public: |
29 gin::ShellRunner runner(&delegate, instance.isolate()); | 46 MojoRunnerDelegate() : gin::ModuleRunnerDelegate(GetModuleSearchPaths()) { |
30 delegate.Start(&runner, pipe, module); | 47 AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule); |
| 48 } |
31 | 49 |
32 base::MessageLoop::current()->Run(); | 50 virtual void UnhandledException(gin::ShellRunner* runner, |
| 51 gin::TryCatch& try_catch) OVERRIDE { |
| 52 gin::ModuleRunnerDelegate::UnhandledException(runner, try_catch); |
| 53 LOG(ERROR) << try_catch.GetStackTrace(); |
| 54 } |
| 55 |
| 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(MojoRunnerDelegate); |
| 58 }; |
| 59 |
| 60 } // namespace |
| 61 |
| 62 |
| 63 class JSApp; |
| 64 |
| 65 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
| 66 public: |
| 67 explicit ContentHandlerImpl(JSApp* app): app_(app) { |
| 68 } |
| 69 virtual ~ContentHandlerImpl() {} |
| 70 |
| 71 private: |
| 72 virtual void OnConnect( |
| 73 const mojo::String& url, |
| 74 URLResponsePtr content, |
| 75 ServiceProviderPtr service_provider) MOJO_OVERRIDE; |
| 76 |
| 77 JSApp* app_; |
| 78 }; |
| 79 |
| 80 // TODO(hansmuller); does this class define the right lifetime |
| 81 // for its gin members? |
| 82 class JSApp : public ApplicationDelegate { |
| 83 public: |
| 84 JSApp() |
| 85 : isolate_holder_(gin::IsolateHolder::kStrictMode), |
| 86 content_handler_factory_(this) { |
| 87 base::i18n::InitializeICU(); |
| 88 shell_runner_.reset( |
| 89 new gin::ShellRunner(&runner_delegate_, isolate_holder_.isolate())); |
| 90 } |
| 91 |
| 92 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 93 MOJO_OVERRIDE { |
| 94 connection->AddService(&content_handler_factory_); |
| 95 return true; |
| 96 } |
| 97 |
| 98 void LoadMainJSModule(const std::string& module, const std::string& path) { |
| 99 gin::Runner::Scope scope(shell_runner_.get()); |
| 100 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 101 shell_runner_->Run(module.c_str(), path.c_str()); |
| 102 } |
| 103 |
| 104 private: |
| 105 gin::IsolateHolder isolate_holder_; |
| 106 scoped_ptr<gin::ShellRunner> shell_runner_; |
| 107 apps::MojoRunnerDelegate runner_delegate_; |
| 108 InterfaceFactoryImplWithContext< |
| 109 ContentHandlerImpl, JSApp> content_handler_factory_; |
| 110 }; |
| 111 |
| 112 void ContentHandlerImpl::OnConnect( |
| 113 const mojo::String& url, |
| 114 URLResponsePtr content, |
| 115 ServiceProviderPtr service_provider) { |
| 116 |
| 117 std::string path(url.To<std::string>()); |
| 118 std::string module; |
| 119 // TODO(hansmuller): load the initial module asynchronously. |
| 120 // TODO(hansmuller): check the return value and fail gracefully. |
| 121 common::BlockingCopyToString(content->body.Pass(), &module); |
| 122 |
| 123 app_->LoadMainJSModule(module, path); |
33 } | 124 } |
34 | 125 |
35 } // namespace apps | 126 } // namespace apps |
36 } // namespace mojo | 127 } // namespace mojo |
37 | 128 |
38 extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { | 129 MojoResult MojoMain(MojoHandle shell_handle) { |
39 mojo::apps::Start(pipe, "mojo/apps/js/main"); | 130 mojo::ApplicationRunnerChromium runner(new mojo::apps::JSApp()); |
40 return MOJO_RESULT_OK; | 131 return runner.Run(shell_handle); |
41 } | 132 } |
| 133 |
OLD | NEW |