| OLD | NEW |
| 1 // Copyright 2014 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 "services/js/js_app.h" | 5 #include "services/js/js_app.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "gin/converter.h" | 9 #include "gin/converter.h" |
| 10 #include "gin/modules/module_registry.h" | 10 #include "gin/modules/module_registry.h" |
| 11 #include "gin/try_catch.h" | 11 #include "gin/try_catch.h" |
| 12 #include "mojo/common/data_pipe_utils.h" | 12 #include "mojo/common/data_pipe_utils.h" |
| 13 #include "mojo/edk/js/core.h" | 13 #include "mojo/edk/js/core.h" |
| 14 #include "mojo/edk/js/handle.h" | 14 #include "mojo/edk/js/handle.h" |
| 15 #include "mojo/edk/js/support.h" | 15 #include "mojo/edk/js/support.h" |
| 16 #include "mojo/public/cpp/bindings/interface_request.h" | 16 #include "mojo/public/cpp/bindings/interface_request.h" |
| 17 #include "services/js/js_app_bridge_module.h" |
| 17 #include "services/js/js_app_message_loop_observers.h" | 18 #include "services/js/js_app_message_loop_observers.h" |
| 18 #include "services/js/js_app_shell.h" | 19 #include "services/js/js_app_shell.h" |
| 19 #include "services/js/mojo_bridge_module.h" | |
| 20 | 20 |
| 21 namespace mojo { | 21 namespace mojo { |
| 22 namespace js { | 22 namespace js { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // If true then result is the value of value.property_name. | 26 // If true then result is the value of value.property_name. |
| 27 bool GetValueProperty(v8::Isolate* isolate, | 27 bool GetValueProperty(v8::Isolate* isolate, |
| 28 v8::Handle<v8::Value> value, | 28 v8::Handle<v8::Value> value, |
| 29 const std::string& property_name, | 29 const std::string& property_name, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 JSApp::JSApp(ShellPtr shell, URLResponsePtr response) : shell_(shell.Pass()) { | 83 JSApp::JSApp(ShellPtr shell, URLResponsePtr response) : shell_(shell.Pass()) { |
| 84 v8::Isolate* isolate = isolate_holder_.isolate(); | 84 v8::Isolate* isolate = isolate_holder_.isolate(); |
| 85 message_loop_observers_.reset(new JSAppMessageLoopObservers(isolate)); | 85 message_loop_observers_.reset(new JSAppMessageLoopObservers(isolate)); |
| 86 | 86 |
| 87 DCHECK(!response.is_null()); | 87 DCHECK(!response.is_null()); |
| 88 std::string url(response->url); | 88 std::string url(response->url); |
| 89 std::string source; | 89 std::string source; |
| 90 CHECK(common::BlockingCopyToString(response->body.Pass(), &source)); | 90 CHECK(common::BlockingCopyToString(response->body.Pass(), &source)); |
| 91 | 91 |
| 92 runner_delegate_.AddBuiltinModule(AppBridge::kModuleName, |
| 93 base::Bind(&AppBridge::GetModule, base::Unretained(this))); |
| 94 |
| 92 shell_runner_.reset(new gin::ShellRunner(&runner_delegate_, isolate)); | 95 shell_runner_.reset(new gin::ShellRunner(&runner_delegate_, isolate)); |
| 93 gin::Runner::Scope scope(shell_runner_.get()); | 96 gin::Runner::Scope scope(shell_runner_.get()); |
| 94 shell_runner_->Run(source.c_str(), kMainModuleName); | 97 shell_runner_->Run(source.c_str(), kMainModuleName); |
| 95 | 98 |
| 96 gin::ModuleRegistry* registry = | 99 gin::ModuleRegistry* registry = |
| 97 gin::ModuleRegistry::From(shell_runner_->GetContextHolder()->context()); | 100 gin::ModuleRegistry::From(shell_runner_->GetContextHolder()->context()); |
| 98 registry->LoadModule( | 101 registry->LoadModule( |
| 99 isolate, | 102 isolate, |
| 100 kMainModuleName, | 103 kMainModuleName, |
| 101 base::Bind(&JSApp::OnAppLoaded, base::Unretained(this), url)); | 104 base::Bind(&JSApp::OnAppLoaded, base::Unretained(this), url)); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 }; | 161 }; |
| 159 CallAppInstanceMethod("initialize", 1, argv); | 162 CallAppInstanceMethod("initialize", 1, argv); |
| 160 } | 163 } |
| 161 | 164 |
| 162 void JSApp::AcceptConnection(const String& requestor_url, | 165 void JSApp::AcceptConnection(const String& requestor_url, |
| 163 ServiceProviderPtr provider) { | 166 ServiceProviderPtr provider) { |
| 164 gin::Runner::Scope scope(shell_runner_.get()); | 167 gin::Runner::Scope scope(shell_runner_.get()); |
| 165 v8::Isolate* isolate = isolate_holder_.isolate(); | 168 v8::Isolate* isolate = isolate_holder_.isolate(); |
| 166 v8::Handle<v8::Value> argv[] = { | 169 v8::Handle<v8::Value> argv[] = { |
| 167 gin::ConvertToV8(isolate, requestor_url.To<std::string>()), | 170 gin::ConvertToV8(isolate, requestor_url.To<std::string>()), |
| 168 gin::ConvertToV8(isolate, provider.PassMessagePipe().get()), | 171 gin::ConvertToV8(isolate, provider.PassMessagePipe().release()), |
| 169 }; | 172 }; |
| 170 CallAppInstanceMethod("acceptConnection", arraysize(argv), argv); | 173 CallAppInstanceMethod("acceptConnection", arraysize(argv), argv); |
| 171 } | 174 } |
| 172 | 175 |
| 173 void JSApp::Quit() { | 176 void JSApp::Quit() { |
| 174 base::MessageLoop::current()->PostTask( | 177 base::MessageLoop::current()->PostTask( |
| 175 FROM_HERE, base::Bind(&JSApp::QuitInternal, base::Unretained(this))); | 178 FROM_HERE, base::Bind(&JSApp::QuitInternal, base::Unretained(this))); |
| 176 } | 179 } |
| 177 | 180 |
| 178 void JSApp::QuitInternal() { | 181 void JSApp::QuitInternal() { |
| 179 shell_runner_.reset(); | 182 shell_runner_.reset(); |
| 180 base::MessageLoop::current()->QuitWhenIdle(); | 183 base::MessageLoop::current()->QuitWhenIdle(); |
| 181 } | 184 } |
| 182 | 185 |
| 183 } // namespace js | 186 } // namespace js |
| 184 } // namespace mojo | 187 } // namespace mojo |
| 185 | 188 |
| OLD | NEW |