| 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 "mojo/apps/js/js_app.h" | 5 #include "mojo/apps/js/js_app.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "gin/array_buffer.h" | 8 #include "gin/array_buffer.h" |
| 9 #include "gin/converter.h" | 9 #include "gin/converter.h" |
| 10 #include "mojo/apps/js/application_delegate_impl.h" | |
| 11 #include "mojo/apps/js/mojo_bridge_module.h" | 10 #include "mojo/apps/js/mojo_bridge_module.h" |
| 11 #include "mojo/common/data_pipe_utils.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace apps { | 14 namespace apps { |
| 15 | 15 |
| 16 JSApp::JSApp(ApplicationDelegateImpl* app_delegate_impl) | 16 JSApp::JSApp(ShellPtr shell, URLResponsePtr response) : shell_(shell.Pass()) { |
| 17 : app_delegate_impl_(app_delegate_impl), | 17 // TODO(hansmuller): handle load failure here and below. |
| 18 thread_("Mojo JS"), | 18 DCHECK(!response.is_null()); |
| 19 app_delegate_impl_task_runner_( | 19 file_name_ = response->url; |
| 20 base::MessageLoop::current()->task_runner()) { | 20 bool result = common::BlockingCopyToString(response->body.Pass(), &source_); |
| 21 CHECK(on_app_delegate_impl_thread()); | 21 DCHECK(result); |
| 22 runner_delegate_.AddBuiltinModule(MojoInternals::kModuleName, | 22 |
| 23 base::Bind(MojoInternals::GetModule, this)); | 23 runner_delegate.AddBuiltinModule(MojoInternals::kModuleName, |
| 24 base::Bind(MojoInternals::GetModule, this)); |
| 25 shell_.set_client(this); |
| 24 } | 26 } |
| 25 | 27 |
| 26 JSApp::~JSApp() { | 28 JSApp::~JSApp() { |
| 27 } | 29 } |
| 28 | 30 |
| 29 bool JSApp::Start() { | |
| 30 CHECK(!js_app_task_runner_.get() && on_app_delegate_impl_thread()); | |
| 31 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
| 32 thread_.StartWithOptions(thread_options); | |
| 33 | |
| 34 // TODO(hansmuller): check thread_.StartWithOptions() return value. | |
| 35 // TODO(hansmuller): need to funnel Run() failures back to the caller. | |
| 36 | |
| 37 thread_.message_loop()->PostTask( | |
| 38 FROM_HERE, base::Bind(&JSApp::Run, base::Unretained(this))); | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 void JSApp::Quit() { | 31 void JSApp::Quit() { |
| 43 CHECK(on_js_app_thread()); | 32 isolate_holder_.RemoveRunMicrotasksObserver(); |
| 44 | 33 base::MessageLoop::current()->PostTask( |
| 45 // The terminate operation is posted to the message_loop so that | 34 FROM_HERE, base::Bind(&JSApp::QuitInternal, base::Unretained(this))); |
| 46 // the shell_runner isn't destroyed before this JS function returns. | |
| 47 thread_.message_loop()->PostTask( | |
| 48 FROM_HERE, base::Bind(&JSApp::Terminate, base::Unretained(this))); | |
| 49 } | 35 } |
| 50 | 36 |
| 51 MessagePipeHandle JSApp::ConnectToApplication( | 37 MessagePipeHandle JSApp::ConnectToApplication( |
| 52 const std::string& application_url) { | 38 const std::string& application_url) { |
| 53 CHECK(on_js_app_thread()); | |
| 54 MessagePipe pipe; | 39 MessagePipe pipe; |
| 55 InterfaceRequest<ServiceProvider> request = | 40 InterfaceRequest<ServiceProvider> request = |
| 56 MakeRequest<ServiceProvider>(pipe.handle1.Pass()); | 41 MakeRequest<ServiceProvider>(pipe.handle1.Pass()); |
| 57 | 42 shell_->ConnectToApplication(application_url, request.Pass()); |
| 58 app_delegate_impl_task_runner_->PostTask( | |
| 59 FROM_HERE, | |
| 60 base::Bind(&ApplicationDelegateImpl::ConnectToApplication, | |
| 61 base::Unretained(app_delegate_impl_), | |
| 62 application_url, | |
| 63 base::Passed(request.Pass()))); | |
| 64 | |
| 65 return pipe.handle0.Pass().release(); | 43 return pipe.handle0.Pass().release(); |
| 66 } | 44 } |
| 67 | 45 |
| 68 | 46 MessagePipeHandle JSApp::RequestorMessagePipeHandle() { |
| 69 void JSApp::Run() { | 47 return requestor_handle_.get(); |
| 70 CHECK(!js_app_task_runner_.get() && !on_app_delegate_impl_thread()); | |
| 71 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); | |
| 72 | |
| 73 std::string source; | |
| 74 std::string file_name; | |
| 75 Load(&source, &file_name); // TODO(hansmuller): handle Load() failure. | |
| 76 | |
| 77 isolate_holder_.reset(new gin::IsolateHolder()); | |
| 78 isolate_holder_->AddRunMicrotasksObserver(); | |
| 79 | |
| 80 shell_runner_.reset( | |
| 81 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); | |
| 82 | |
| 83 gin::Runner::Scope scope(shell_runner_.get()); | |
| 84 shell_runner_->Run(source.c_str(), file_name.c_str()); | |
| 85 } | 48 } |
| 86 | 49 |
| 87 void JSApp::Terminate() { | 50 void JSApp::AcceptConnection(const String& requestor_url, |
| 88 isolate_holder_->RemoveRunMicrotasksObserver(); | 51 ServiceProviderPtr provider) { |
| 89 shell_runner_.reset(nullptr); | 52 requestor_handle_ = provider.PassMessagePipe(); |
| 90 | 53 |
| 91 // This JSApp's thread must be stopped on the thread that started it. Ask the | 54 isolate_holder_.AddRunMicrotasksObserver(); |
| 92 // app_delegate_impl_ to erase its AppVector entry for this app, which | 55 shell_runner_.reset( |
| 93 // implicitly destroys this JSApp and stops its thread. | 56 new gin::ShellRunner(&runner_delegate, isolate_holder_.isolate())); |
| 94 app_delegate_impl_task_runner_->PostTask( | 57 gin::Runner::Scope scope(shell_runner_.get()); |
| 95 FROM_HERE, | 58 shell_runner_->Run(source_.c_str(), file_name_.c_str()); |
| 96 base::Bind(&ApplicationDelegateImpl::QuitJSApp, | |
| 97 base::Unretained(app_delegate_impl_), | |
| 98 base::Unretained(this))); | |
| 99 } | 59 } |
| 100 | 60 |
| 101 bool JSApp::on_app_delegate_impl_thread() const { | 61 void JSApp::Initialize(Array<String> args) { |
| 102 return app_delegate_impl_task_runner_.get() && | |
| 103 app_delegate_impl_task_runner_.get() == | |
| 104 base::MessageLoop::current()->task_runner().get(); | |
| 105 } | 62 } |
| 106 | 63 |
| 107 bool JSApp::on_js_app_thread() const { | 64 void JSApp::QuitInternal() { |
| 108 return js_app_task_runner_.get() && | 65 shell_runner_.reset(); |
| 109 js_app_task_runner_.get() == | 66 base::MessageLoop::current()->QuitWhenIdle(); |
| 110 base::MessageLoop::current()->task_runner().get(); | |
| 111 } | 67 } |
| 112 | 68 |
| 113 } // namespace apps | 69 } // namespace apps |
| 114 } // namespace mojo | 70 } // namespace mojo |
| OLD | NEW |