Chromium Code Reviews| Index: mojo/shell/static_application_loader.cc |
| diff --git a/mojo/shell/static_application_loader.cc b/mojo/shell/static_application_loader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0760805c784158963ccd3f6ffcba519e0828a347 |
| --- /dev/null |
| +++ b/mojo/shell/static_application_loader.cc |
| @@ -0,0 +1,86 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/shell/static_application_loader.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/run_loop.h" |
| +#include "base/task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "base/threading/thread.h" |
| +#include "mojo/application/public/cpp/application_delegate.h" |
| +#include "mojo/application/public/cpp/application_impl.h" |
| +#include "mojo/application/public/interfaces/application.mojom.h" |
| +#include "mojo/common/message_pump_mojo.h" |
| +#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| + |
| +namespace mojo { |
| +namespace shell { |
| + |
| +namespace { |
| + |
| +void RunAppOnOwnThread( |
| + InterfaceRequest<Application> request, |
| + scoped_refptr<base::TaskRunner> exit_task_runner, |
| + const base::Closure& exit_callback, |
| + const StaticApplicationLoader::ApplicationFactory& factory) { |
| + base::RunLoop run_loop; |
|
jam
2015/05/29 01:48:54
this looks similar to the code in mojo/application
Ken Rockot(use gerrit already)
2015/05/29 03:20:40
I don't really think ApplicationRunner can be shar
jam
2015/05/29 15:11:50
really? I don't see that, i.e.right now mandoline
|
| + scoped_ptr<ApplicationDelegate> delegate(factory.Run()); |
| + scoped_ptr<ApplicationImpl> application(new ApplicationImpl( |
| + delegate.get(), request.Pass(), run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + exit_task_runner->PostTask(FROM_HERE, exit_callback); |
| +} |
| + |
| +} // namespace |
| + |
| +StaticApplicationLoader::StaticApplicationLoader( |
| + const ApplicationFactory& factory) |
| + : StaticApplicationLoader(factory, base::Closure()) { |
| +} |
| + |
| +StaticApplicationLoader::StaticApplicationLoader( |
| + const ApplicationFactory& factory, |
| + const base::Closure& quit_callback) |
| + : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) { |
| +} |
| + |
| +StaticApplicationLoader::~StaticApplicationLoader() { |
| + if (thread_) |
| + StopAppThread(); |
| +} |
| + |
| +void StaticApplicationLoader::Load(const GURL& url, |
| + InterfaceRequest<Application> request) { |
| + if (thread_) |
| + return; |
| + |
| + thread_.reset(new base::Thread("Mojo Application: " + url.spec())); |
| + |
| + base::Thread::Options options; |
| + options.message_pump_factory = |
| + base::Bind(&mojo::common::MessagePumpMojo::Create); |
| + thread_->StartWithOptions(options); |
| + |
| + // If the application's thread quits on its own before this loader dies, we |
| + // reset the Thread object, allowing future Load requests to be fulfilled |
| + // with a new app instance. |
| + auto exit_callback = base::Bind(&StaticApplicationLoader::StopAppThread, |
| + weak_factory_.GetWeakPtr()); |
| + thread_->task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&RunAppOnOwnThread, base::Passed(&request), |
| + base::ThreadTaskRunnerHandle::Get(), exit_callback, factory_)); |
| +} |
| + |
| +void StaticApplicationLoader::StopAppThread() { |
| + thread_.reset(); |
| + if (!quit_callback_.is_null()) |
| + quit_callback_.Run(); |
| +} |
| + |
| +} // namespace shell |
| +} // namespace mojo |