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" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // TODO(hansmuller): need to funnel Run() failures back to the caller. | 40 // TODO(hansmuller): need to funnel Run() failures back to the caller. |
41 | 41 |
42 thread_.message_loop()->PostTask( | 42 thread_.message_loop()->PostTask( |
43 FROM_HERE, base::Bind(&JSApp::Run, base::Unretained(this))); | 43 FROM_HERE, base::Bind(&JSApp::Run, base::Unretained(this))); |
44 return true; | 44 return true; |
45 } | 45 } |
46 | 46 |
47 void JSApp::Quit() { | 47 void JSApp::Quit() { |
48 CHECK(on_js_app_thread()); | 48 CHECK(on_js_app_thread()); |
49 | 49 |
50 // The the terminate operation is posted to the message_loop so that | 50 // The terminate operation is posted to the message_loop so that |
51 // the shell_runner isn't destroyed before this JS function returns. | 51 // the shell_runner isn't destroyed before this JS function returns. |
52 thread_.message_loop()->PostTask( | 52 thread_.message_loop()->PostTask( |
53 FROM_HERE, base::Bind(&JSApp::Terminate, base::Unretained(this))); | 53 FROM_HERE, base::Bind(&JSApp::Terminate, base::Unretained(this))); |
54 } | 54 } |
55 | 55 |
56 Handle JSApp::ConnectToService(const std::string& application_url, | 56 Handle JSApp::ConnectToService(const std::string& application_url, |
57 const std::string& interface_name) { | 57 const std::string& interface_name) { |
58 CHECK(on_js_app_thread()); | 58 CHECK(on_js_app_thread()); |
59 MessagePipe pipe; | 59 MessagePipe pipe; |
60 | 60 |
(...skipping 12 matching lines...) Expand all Loading... |
73 CHECK(!js_app_task_runner_.get() && !on_content_handler_thread()); | 73 CHECK(!js_app_task_runner_.get() && !on_content_handler_thread()); |
74 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); | 74 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); |
75 | 75 |
76 // TODO(hansmuller): check the return value and fail gracefully. | 76 // TODO(hansmuller): check the return value and fail gracefully. |
77 std::string module; | 77 std::string module; |
78 common::BlockingCopyToString(content_->body.Pass(), &module); | 78 common::BlockingCopyToString(content_->body.Pass(), &module); |
79 | 79 |
80 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, | 80 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, |
81 gin::ArrayBufferAllocator::SharedInstance()); | 81 gin::ArrayBufferAllocator::SharedInstance()); |
82 isolate_holder_.reset(new gin::IsolateHolder()); | 82 isolate_holder_.reset(new gin::IsolateHolder()); |
| 83 isolate_holder_->AddRunMicrotasksObserver(); |
83 | 84 |
84 shell_runner_.reset( | 85 shell_runner_.reset( |
85 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); | 86 new gin::ShellRunner(&runner_delegate_, isolate_holder_->isolate())); |
86 | 87 |
87 // TODO(hansmuller): exiting this scope here is OK? | 88 // TODO(hansmuller): exiting this scope here is OK? |
88 gin::Runner::Scope scope(shell_runner_.get()); | 89 gin::Runner::Scope scope(shell_runner_.get()); |
89 shell_runner_->Run(module.c_str(), url_.c_str()); | 90 shell_runner_->Run(module.c_str(), url_.c_str()); |
90 } | 91 } |
91 | 92 |
92 void JSApp::Terminate() { | 93 void JSApp::Terminate() { |
| 94 isolate_holder_->RemoveRunMicrotasksObserver(); |
93 shell_runner_.reset(NULL); | 95 shell_runner_.reset(NULL); |
94 | 96 |
95 // This JSApp's thread must be stopped on the thread that started it. Ask the | 97 // This JSApp's thread must be stopped on the thread that started it. Ask the |
96 // content_handler_app_ to erase its AppVector entry for this app, which | 98 // content_handler_app_ to erase its AppVector entry for this app, which |
97 // implicitly destroys this JSApp and stops its thread. | 99 // implicitly destroys this JSApp and stops its thread. |
98 content_handler_task_runner_->PostTask( | 100 content_handler_task_runner_->PostTask( |
99 FROM_HERE, | 101 FROM_HERE, |
100 base::Bind(&ApplicationDelegateImpl::QuitJSApp, | 102 base::Bind(&ApplicationDelegateImpl::QuitJSApp, |
101 base::Unretained(content_handler_app_), | 103 base::Unretained(content_handler_app_), |
102 base::Unretained(this))); | 104 base::Unretained(this))); |
103 } | 105 } |
104 | 106 |
105 bool JSApp::on_content_handler_thread() const { | 107 bool JSApp::on_content_handler_thread() const { |
106 return content_handler_task_runner_.get() && | 108 return content_handler_task_runner_.get() && |
107 content_handler_task_runner_.get() == | 109 content_handler_task_runner_.get() == |
108 base::MessageLoop::current()->task_runner().get(); | 110 base::MessageLoop::current()->task_runner().get(); |
109 } | 111 } |
110 | 112 |
111 bool JSApp::on_js_app_thread() const { | 113 bool JSApp::on_js_app_thread() const { |
112 return js_app_task_runner_.get() && | 114 return js_app_task_runner_.get() && |
113 js_app_task_runner_.get() == | 115 js_app_task_runner_.get() == |
114 base::MessageLoop::current()->task_runner().get(); | 116 base::MessageLoop::current()->task_runner().get(); |
115 } | 117 } |
116 | 118 |
117 } // namespace apps | 119 } // namespace apps |
118 } // namespace mojo | 120 } // namespace mojo |
OLD | NEW |