OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/shell/static_application_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "mojo/application/public/cpp/application_delegate.h" |
| 15 #include "mojo/application/public/cpp/application_impl.h" |
| 16 #include "mojo/application/public/interfaces/application.mojom.h" |
| 17 #include "mojo/common/message_pump_mojo.h" |
| 18 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 19 |
| 20 namespace mojo { |
| 21 namespace shell { |
| 22 |
| 23 namespace { |
| 24 |
| 25 void RunAppOnOwnThread( |
| 26 InterfaceRequest<Application> request, |
| 27 scoped_refptr<base::TaskRunner> exit_task_runner, |
| 28 const base::Closure& exit_callback, |
| 29 const StaticApplicationLoader::ApplicationFactory& factory) { |
| 30 base::RunLoop run_loop; |
| 31 scoped_ptr<ApplicationDelegate> delegate(factory.Run()); |
| 32 scoped_ptr<ApplicationImpl> application(new ApplicationImpl( |
| 33 delegate.get(), request.Pass(), run_loop.QuitClosure())); |
| 34 run_loop.Run(); |
| 35 exit_task_runner->PostTask(FROM_HERE, exit_callback); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 StaticApplicationLoader::StaticApplicationLoader( |
| 41 const ApplicationFactory& factory) |
| 42 : StaticApplicationLoader(factory, base::Closure()) { |
| 43 } |
| 44 |
| 45 StaticApplicationLoader::StaticApplicationLoader( |
| 46 const ApplicationFactory& factory, |
| 47 const base::Closure& quit_callback) |
| 48 : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) { |
| 49 } |
| 50 |
| 51 StaticApplicationLoader::~StaticApplicationLoader() { |
| 52 if (thread_) |
| 53 StopAppThread(); |
| 54 } |
| 55 |
| 56 void StaticApplicationLoader::Load(const GURL& url, |
| 57 InterfaceRequest<Application> request) { |
| 58 if (thread_) |
| 59 return; |
| 60 |
| 61 thread_.reset(new base::Thread("Mojo Application: " + url.spec())); |
| 62 |
| 63 base::Thread::Options options; |
| 64 options.message_pump_factory = |
| 65 base::Bind(&mojo::common::MessagePumpMojo::Create); |
| 66 thread_->StartWithOptions(options); |
| 67 |
| 68 // If the application's thread quits on its own before this loader dies, we |
| 69 // reset the Thread object, allowing future Load requests to be fulfilled |
| 70 // with a new app instance. |
| 71 auto exit_callback = base::Bind(&StaticApplicationLoader::StopAppThread, |
| 72 weak_factory_.GetWeakPtr()); |
| 73 thread_->task_runner()->PostTask( |
| 74 FROM_HERE, |
| 75 base::Bind(&RunAppOnOwnThread, base::Passed(&request), |
| 76 base::ThreadTaskRunnerHandle::Get(), exit_callback, factory_)); |
| 77 } |
| 78 |
| 79 void StaticApplicationLoader::StopAppThread() { |
| 80 thread_.reset(); |
| 81 if (!quit_callback_.is_null()) |
| 82 quit_callback_.Run(); |
| 83 } |
| 84 |
| 85 } // namespace shell |
| 86 } // namespace mojo |
OLD | NEW |