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