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/array_buffer.h" | |
9 #include "gin/converter.h" | |
10 #include "mojo/apps/js/application_delegate_impl.h" | |
11 #include "mojo/apps/js/mojo_bridge_module.h" | |
12 | |
13 namespace mojo { | |
14 namespace apps { | |
15 | |
16 JSApp::JSApp(ApplicationDelegateImpl* app_delegate_impl) | |
17 : app_delegate_impl_(app_delegate_impl), | |
18 thread_("Mojo JS"), | |
19 app_delegate_impl_task_runner_( | |
20 base::MessageLoop::current()->task_runner()) { | |
21 CHECK(on_app_delegate_impl_thread()); | |
22 runner_delegate_.AddBuiltinModule(MojoInternals::kModuleName, | |
23 base::Bind(MojoInternals::GetModule, this)); | |
24 } | |
25 | |
26 JSApp::~JSApp() { | |
27 } | |
28 | |
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() { | |
43 CHECK(on_js_app_thread()); | |
44 | |
45 // The terminate operation is posted to the message_loop so that | |
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 } | |
50 | |
51 Handle JSApp::ConnectToService(const std::string& application_url, | |
52 const std::string& interface_name) { | |
53 CHECK(on_js_app_thread()); | |
54 MessagePipe pipe; | |
55 | |
56 app_delegate_impl_task_runner_->PostTask( | |
57 FROM_HERE, | |
58 base::Bind(&ApplicationDelegateImpl::ConnectToService, | |
59 base::Unretained(app_delegate_impl_), | |
60 base::Passed(pipe.handle1.Pass()), | |
61 application_url, | |
62 interface_name)); | |
63 | |
64 return pipe.handle0.release(); | |
65 } | |
66 | |
67 void JSApp::Run() { | |
68 CHECK(!js_app_task_runner_.get() && !on_app_delegate_impl_thread()); | |
69 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); | |
70 | |
71 std::string source; | |
72 std::string file_name; | |
73 Load(&source, &file_name); // TODO(hansmuller): handle Load() failure. | |
74 | |
75 isolate_holder_.reset(new gin::IsolateHolder()); | |
76 isolate_holder_->AddRunMicrotasksObserver(); | |
77 | |
78 shell_runner_.reset( | |
79 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); | |
80 | |
81 gin::Runner::Scope scope(shell_runner_.get()); | |
82 shell_runner_->Run(source.c_str(), file_name.c_str()); | |
83 } | |
84 | |
85 void JSApp::Terminate() { | |
86 isolate_holder_->RemoveRunMicrotasksObserver(); | |
87 shell_runner_.reset(nullptr); | |
88 | |
89 // This JSApp's thread must be stopped on the thread that started it. Ask the | |
90 // app_delegate_impl_ to erase its AppVector entry for this app, which | |
91 // implicitly destroys this JSApp and stops its thread. | |
92 app_delegate_impl_task_runner_->PostTask( | |
93 FROM_HERE, | |
94 base::Bind(&ApplicationDelegateImpl::QuitJSApp, | |
95 base::Unretained(app_delegate_impl_), | |
96 base::Unretained(this))); | |
97 } | |
98 | |
99 bool JSApp::on_app_delegate_impl_thread() const { | |
100 return app_delegate_impl_task_runner_.get() && | |
101 app_delegate_impl_task_runner_.get() == | |
102 base::MessageLoop::current()->task_runner().get(); | |
103 } | |
104 | |
105 bool JSApp::on_js_app_thread() const { | |
106 return js_app_task_runner_.get() && | |
107 js_app_task_runner_.get() == | |
108 base::MessageLoop::current()->task_runner().get(); | |
109 } | |
110 | |
111 } // namespace apps | |
112 } // namespace mojo | |
OLD | NEW |