Chromium Code Reviews| 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" | 10 #include "mojo/apps/js/application_delegate_impl.h" |
| 11 #include "mojo/apps/js/mojo_module.h" | 11 #include "mojo/apps/js/mojo_module.h" |
| 12 #include "mojo/common/data_pipe_utils.h" | |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 namespace apps { | 14 namespace apps { |
| 16 | 15 |
| 17 JSApp::JSApp(ApplicationDelegateImpl* content_handler_app, | 16 JSApp::JSApp(ApplicationDelegateImpl* app_delegate_impl) |
| 18 const std::string& url, | 17 : app_delegate_impl_(app_delegate_impl), |
| 19 URLResponsePtr content) | 18 thread_("Mojo JS "), |
|
Aaron Boodman
2014/10/01 04:25:44
Remove trailing space.
hansmuller
2014/10/01 15:40:12
Done.
| |
| 20 : content_handler_app_(content_handler_app), | 19 app_delegate_impl_task_runner_( |
| 21 url_(url), | |
| 22 content_(content.Pass()), | |
| 23 thread_("Mojo JS " + url), | |
| 24 content_handler_task_runner_( | |
| 25 base::MessageLoop::current()->task_runner()) { | 20 base::MessageLoop::current()->task_runner()) { |
| 26 CHECK(on_content_handler_thread()); | 21 CHECK(on_app_delegate_impl_thread()); |
| 27 runner_delegate_.AddBuiltinModule(Mojo::kModuleName, | 22 runner_delegate_.AddBuiltinModule(Mojo::kModuleName, |
| 28 base::Bind(Mojo::GetModule, this)); | 23 base::Bind(Mojo::GetModule, this)); |
| 29 } | 24 } |
| 30 | 25 |
| 31 JSApp::~JSApp() { | 26 JSApp::~JSApp() { |
| 32 } | 27 } |
| 33 | 28 |
| 34 bool JSApp::Start() { | 29 bool JSApp::Start() { |
| 35 CHECK(!js_app_task_runner_.get() && on_content_handler_thread()); | 30 CHECK(!js_app_task_runner_.get() && on_app_delegate_impl_thread()); |
| 36 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 31 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 37 thread_.StartWithOptions(thread_options); | 32 thread_.StartWithOptions(thread_options); |
| 38 | 33 |
| 39 // TODO(hansmuller): check thread_.StartWithOptions() return value. | 34 // TODO(hansmuller): check thread_.StartWithOptions() return value. |
| 40 // TODO(hansmuller): need to funnel Run() failures back to the caller. | 35 // TODO(hansmuller): need to funnel Run() failures back to the caller. |
| 41 | 36 |
| 42 thread_.message_loop()->PostTask( | 37 thread_.message_loop()->PostTask( |
| 43 FROM_HERE, base::Bind(&JSApp::Run, base::Unretained(this))); | 38 FROM_HERE, base::Bind(&JSApp::Run, base::Unretained(this))); |
| 44 return true; | 39 return true; |
| 45 } | 40 } |
| 46 | 41 |
| 47 void JSApp::Quit() { | 42 void JSApp::Quit() { |
| 48 CHECK(on_js_app_thread()); | 43 CHECK(on_js_app_thread()); |
| 49 | 44 |
| 50 // The terminate operation is posted to the message_loop so that | 45 // The terminate operation is posted to the message_loop so that |
| 51 // the shell_runner isn't destroyed before this JS function returns. | 46 // the shell_runner isn't destroyed before this JS function returns. |
| 52 thread_.message_loop()->PostTask( | 47 thread_.message_loop()->PostTask( |
| 53 FROM_HERE, base::Bind(&JSApp::Terminate, base::Unretained(this))); | 48 FROM_HERE, base::Bind(&JSApp::Terminate, base::Unretained(this))); |
| 54 } | 49 } |
| 55 | 50 |
| 56 Handle JSApp::ConnectToService(const std::string& application_url, | 51 Handle JSApp::ConnectToService(const std::string& application_url, |
| 57 const std::string& interface_name) { | 52 const std::string& interface_name) { |
| 58 CHECK(on_js_app_thread()); | 53 CHECK(on_js_app_thread()); |
| 59 MessagePipe pipe; | 54 MessagePipe pipe; |
| 60 | 55 |
| 61 content_handler_task_runner_->PostTask( | 56 app_delegate_impl_task_runner_->PostTask( |
| 62 FROM_HERE, | 57 FROM_HERE, |
| 63 base::Bind(&ApplicationDelegateImpl::ConnectToService, | 58 base::Bind(&ApplicationDelegateImpl::ConnectToService, |
| 64 base::Unretained(content_handler_app_), | 59 base::Unretained(app_delegate_impl_), |
| 65 base::Passed(pipe.handle1.Pass()), | 60 base::Passed(pipe.handle1.Pass()), |
| 66 application_url, | 61 application_url, |
| 67 interface_name)); | 62 interface_name)); |
| 68 | 63 |
| 69 return pipe.handle0.release(); | 64 return pipe.handle0.release(); |
| 70 } | 65 } |
| 71 | 66 |
| 72 void JSApp::Run() { | 67 void JSApp::Run() { |
| 73 CHECK(!js_app_task_runner_.get() && !on_content_handler_thread()); | 68 CHECK(!js_app_task_runner_.get() && !on_app_delegate_impl_thread()); |
| 74 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); | 69 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); |
| 75 | 70 |
| 76 // TODO(hansmuller): check the return value and fail gracefully. | 71 std::string source; |
| 77 std::string module; | 72 std::string file_name; |
| 78 common::BlockingCopyToString(content_->body.Pass(), &module); | 73 Load(&source, &file_name); // TODO(hansmuller): handle Load() failure. |
| 79 | 74 |
| 80 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, | 75 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, |
| 81 gin::ArrayBufferAllocator::SharedInstance()); | 76 gin::ArrayBufferAllocator::SharedInstance()); |
| 82 isolate_holder_.reset(new gin::IsolateHolder()); | 77 isolate_holder_.reset(new gin::IsolateHolder()); |
| 83 isolate_holder_->AddRunMicrotasksObserver(); | 78 isolate_holder_->AddRunMicrotasksObserver(); |
| 84 | 79 |
| 85 shell_runner_.reset( | 80 shell_runner_.reset( |
| 86 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); | 81 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); |
| 87 | 82 |
| 88 // TODO(hansmuller): exiting this scope here is OK? | |
| 89 gin::Runner::Scope scope(shell_runner_.get()); | 83 gin::Runner::Scope scope(shell_runner_.get()); |
| 90 shell_runner_->Run(module.c_str(), url_.c_str()); | 84 shell_runner_->Run(source.c_str(), file_name.c_str()); |
| 91 } | 85 } |
| 92 | 86 |
| 93 void JSApp::Terminate() { | 87 void JSApp::Terminate() { |
| 94 isolate_holder_->RemoveRunMicrotasksObserver(); | 88 isolate_holder_->RemoveRunMicrotasksObserver(); |
| 95 shell_runner_.reset(NULL); | 89 shell_runner_.reset(nullptr); |
| 96 | 90 |
| 97 // This JSApp's thread must be stopped on the thread that started it. Ask the | 91 // This JSApp's thread must be stopped on the thread that started it. Ask the |
| 98 // content_handler_app_ to erase its AppVector entry for this app, which | 92 // app_delegate_impl_ to erase its AppVector entry for this app, which |
| 99 // implicitly destroys this JSApp and stops its thread. | 93 // implicitly destroys this JSApp and stops its thread. |
| 100 content_handler_task_runner_->PostTask( | 94 app_delegate_impl_task_runner_->PostTask( |
| 101 FROM_HERE, | 95 FROM_HERE, |
| 102 base::Bind(&ApplicationDelegateImpl::QuitJSApp, | 96 base::Bind(&ApplicationDelegateImpl::QuitJSApp, |
| 103 base::Unretained(content_handler_app_), | 97 base::Unretained(app_delegate_impl_), |
| 104 base::Unretained(this))); | 98 base::Unretained(this))); |
| 105 } | 99 } |
| 106 | 100 |
| 107 bool JSApp::on_content_handler_thread() const { | 101 bool JSApp::on_app_delegate_impl_thread() const { |
| 108 return content_handler_task_runner_.get() && | 102 return app_delegate_impl_task_runner_.get() && |
| 109 content_handler_task_runner_.get() == | 103 app_delegate_impl_task_runner_.get() == |
| 110 base::MessageLoop::current()->task_runner().get(); | 104 base::MessageLoop::current()->task_runner().get(); |
| 111 } | 105 } |
| 112 | 106 |
| 113 bool JSApp::on_js_app_thread() const { | 107 bool JSApp::on_js_app_thread() const { |
| 114 return js_app_task_runner_.get() && | 108 return js_app_task_runner_.get() && |
| 115 js_app_task_runner_.get() == | 109 js_app_task_runner_.get() == |
| 116 base::MessageLoop::current()->task_runner().get(); | 110 base::MessageLoop::current()->task_runner().get(); |
| 117 } | 111 } |
| 118 | 112 |
| 119 } // namespace apps | 113 } // namespace apps |
| 120 } // namespace mojo | 114 } // namespace mojo |
| OLD | NEW |