Chromium Code Reviews| 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/public/gin_embedders.h" | |
| 9 #include "mojo/apps/js/js_content_handler_app.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() { | |
| 17 AddBuiltinModule(Mojo::kModuleName, Mojo::GetModule); | |
|
Aaron Boodman
2014/09/10 02:22:09
So there is a lot of legwork in here to route thes
hansmuller
2014/09/11 00:26:16
I've updated gin::ModuleRunnerDelegate() as we dis
| |
| 18 } | |
| 19 | |
| 20 JSApp::JSApp(JSContentHandlerApp* content_handler_app, | |
| 21 const std::string& url, | |
|
Aaron Boodman
2014/09/10 02:22:09
You should run `git cl format` on this entire patc
| |
| 22 URLResponsePtr content) | |
| 23 : content_handler_app_(content_handler_app), | |
| 24 url_(url), | |
| 25 content_(content.Pass()), | |
| 26 thread_("Mojo JS " + url), | |
|
Aaron Boodman
2014/09/10 02:22:09
Is there a naming convention for threads in Mojo o
hansmuller
2014/09/11 00:26:16
I haven't observed one, but I'll ask around.
| |
| 27 content_handler_task_runner_( | |
| 28 base::MessageLoop::current()->task_runner()) { | |
| 29 | |
| 30 CHECK(on_content_handler_thread()); | |
| 31 } | |
| 32 | |
| 33 JSApp::~JSApp() { | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 JSApp* JSApp::From(v8::Isolate* isolate) { | |
| 38 return static_cast<JSApp*>(isolate->GetData(gin::kEmbedderMojo)); | |
| 39 } | |
| 40 | |
| 41 ScopedMessagePipeHandle JSApp::ConnectToService( | |
| 42 const std::string& application_url, const std::string& interface_name) { | |
| 43 CHECK(on_js_app_thread()); | |
| 44 MessagePipe pipe; | |
| 45 | |
| 46 // The lifetime of content_handler_app_ is managed by the ApplicationRunner. | |
| 47 content_handler_task_runner_->PostTask(FROM_HERE, | |
| 48 base::Bind(&JSContentHandlerApp::ConnectToService, | |
| 49 base::Unretained(content_handler_app_), | |
| 50 base::Passed(pipe.handle1.Pass()), application_url, interface_name)); | |
| 51 | |
| 52 return pipe.handle0.Pass(); | |
| 53 } | |
| 54 | |
| 55 void JSApp::Run() { | |
| 56 CHECK(!js_app_task_runner_.get() && !on_content_handler_thread()); | |
| 57 js_app_task_runner_ = base::MessageLoop::current()->task_runner(); | |
| 58 | |
| 59 // TODO(hansmuller): check the return value and fail gracefully. | |
| 60 std::string module; | |
| 61 common::BlockingCopyToString(content_->body.Pass(), &module); | |
| 62 | |
| 63 isolate_holder_.reset( | |
| 64 new gin::IsolateHolder(gin::IsolateHolder::kStrictMode)); | |
| 65 // Record the mapping from this JS thread's isolate to this JSApp | |
| 66 isolate_holder_->isolate()->SetData(gin::kEmbedderMojo, this); | |
| 67 | |
| 68 shell_runner_.reset(new gin::ShellRunner(&runner_delegate_, | |
| 69 isolate_holder_->isolate())); | |
| 70 // TODO(hansmuller): exiting this scope here is OK? | |
| 71 gin::Runner::Scope scope(shell_runner_.get()); | |
| 72 shell_runner_->Run(module.c_str(), url_.c_str()); | |
| 73 } | |
| 74 | |
| 75 | |
| 76 bool JSApp::Start() { | |
| 77 CHECK(!js_app_task_runner_.get() && on_content_handler_thread()); | |
| 78 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | |
| 79 thread_.StartWithOptions(thread_options); | |
| 80 | |
| 81 // TODO(hansmuller): check thread_.StartWithOptions() return value. | |
| 82 // TODO(hansmuller): need to funnel Run() failures back to the caller. | |
| 83 | |
| 84 // The lifetime of the this JSApp is managed by the content_handler_app_. | |
|
Aaron Boodman
2014/09/11 05:24:52
I understand you're trying to explain the Unretain
hansmuller
2014/09/11 16:44:43
Done.
| |
| 85 thread_.message_loop()->PostTask(FROM_HERE, | |
| 86 base::Bind(&JSApp::Run, base::Unretained(this))); | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 void JSApp::Terminate() { | |
| 91 shell_runner_.reset(NULL); | |
| 92 | |
| 93 // This JSApp's thread must be stopped on the thread that started it. Ask the | |
| 94 // content_handler_app_ to drop its reference to this app, which implicitly | |
|
Aaron Boodman
2014/09/11 05:24:53
Since this class is no longer ref-counted "drop it
hansmuller
2014/09/11 16:44:43
I'll reword this.
| |
| 95 // destroys this JSApp and stops its thread. | |
| 96 // The lifetime of content_handler_app_ is managed by the ApplicationRunner. | |
| 97 // The lifetime of the this JSApp is managed by the content_handler_app_. | |
| 98 content_handler_task_runner_->PostTask(FROM_HERE, | |
| 99 base::Bind(&JSContentHandlerApp::QuitJSApp, | |
| 100 base::Unretained(content_handler_app_), base::Unretained(this))); | |
| 101 } | |
| 102 | |
| 103 void JSApp::Quit() { | |
| 104 CHECK(on_js_app_thread()); | |
| 105 | |
| 106 // The lifetime of the this JSApp is managed by the content_handler_app_. | |
| 107 thread_.message_loop()->PostTask(FROM_HERE, | |
|
Aaron Boodman
2014/09/10 02:22:08
This PostTask() doesn't seem necessary. We're post
hansmuller
2014/09/11 00:26:16
Sorry, I should have included a comment to explain
Aaron Boodman
2014/09/11 05:24:52
I see. I was thinking that since the only reason t
| |
| 108 base::Bind(&JSApp::Terminate, base::Unretained(this))); | |
| 109 } | |
| 110 | |
| 111 bool JSApp::on_content_handler_thread() const { | |
| 112 return content_handler_task_runner_.get() | |
| 113 && content_handler_task_runner_.get() == | |
| 114 base::MessageLoop::current()->task_runner().get(); | |
| 115 } | |
| 116 | |
| 117 bool JSApp::on_js_app_thread() const { | |
| 118 return js_app_task_runner_.get() && | |
| 119 js_app_task_runner_.get() == | |
| 120 base::MessageLoop::current()->task_runner().get(); | |
| 121 } | |
| 122 | |
| 123 } // namespace apps | |
| 124 } // namespace mojo | |
| 125 | |
| OLD | NEW |