| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/mojo/static_loader.h" | 5 #include "content/common/mojo/static_loader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/simple_thread.h" | 14 #include "base/threading/simple_thread.h" |
| 15 #include "mojo/public/cpp/bindings/interface_request.h" | 15 #include "mojo/public/cpp/bindings/interface_request.h" |
| 16 #include "services/shell/public/cpp/application_runner.h" | 16 #include "services/shell/public/cpp/application_runner.h" |
| 17 #include "services/shell/public/cpp/shell_client.h" | 17 #include "services/shell/public/cpp/shell_client.h" |
| 18 #include "services/shell/public/interfaces/shell_client.mojom.h" | 18 #include "services/shell/public/interfaces/shell_client.mojom.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 class RunnerThread : public base::SimpleThread { | 24 class RunnerThread : public base::SimpleThread { |
| 25 public: | 25 public: |
| 26 RunnerThread(const std::string& name, | 26 RunnerThread(const std::string& name, |
| 27 mojo::shell::mojom::ShellClientRequest request, | 27 shell::mojom::ShellClientRequest request, |
| 28 scoped_refptr<base::TaskRunner> exit_task_runner, | 28 scoped_refptr<base::TaskRunner> exit_task_runner, |
| 29 const base::Closure& exit_callback, | 29 const base::Closure& exit_callback, |
| 30 const StaticLoader::ApplicationFactory& factory) | 30 const StaticLoader::ApplicationFactory& factory) |
| 31 : base::SimpleThread("Mojo Application: " + name), | 31 : base::SimpleThread("Mojo Application: " + name), |
| 32 request_(std::move(request)), | 32 request_(std::move(request)), |
| 33 exit_task_runner_(exit_task_runner), | 33 exit_task_runner_(exit_task_runner), |
| 34 exit_callback_(exit_callback), | 34 exit_callback_(exit_callback), |
| 35 factory_(factory) {} | 35 factory_(factory) {} |
| 36 | 36 |
| 37 void Run() override { | 37 void Run() override { |
| 38 std::unique_ptr<mojo::ApplicationRunner> runner( | 38 std::unique_ptr<shell::ApplicationRunner> runner( |
| 39 new mojo::ApplicationRunner(factory_.Run().release())); | 39 new shell::ApplicationRunner(factory_.Run().release())); |
| 40 runner->Run(request_.PassMessagePipe().release().value(), | 40 runner->Run(request_.PassMessagePipe().release().value(), |
| 41 false /* init_base */); | 41 false /* init_base */); |
| 42 exit_task_runner_->PostTask(FROM_HERE, exit_callback_); | 42 exit_task_runner_->PostTask(FROM_HERE, exit_callback_); |
| 43 } | 43 } |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 mojo::shell::mojom::ShellClientRequest request_; | 46 shell::mojom::ShellClientRequest request_; |
| 47 scoped_refptr<base::TaskRunner> exit_task_runner_; | 47 scoped_refptr<base::TaskRunner> exit_task_runner_; |
| 48 base::Closure exit_callback_; | 48 base::Closure exit_callback_; |
| 49 StaticLoader::ApplicationFactory factory_; | 49 StaticLoader::ApplicationFactory factory_; |
| 50 | 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(RunnerThread); | 51 DISALLOW_COPY_AND_ASSIGN(RunnerThread); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 StaticLoader::StaticLoader(const ApplicationFactory& factory) | 56 StaticLoader::StaticLoader(const ApplicationFactory& factory) |
| 57 : StaticLoader(factory, base::Closure()) { | 57 : StaticLoader(factory, base::Closure()) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 StaticLoader::StaticLoader(const ApplicationFactory& factory, | 60 StaticLoader::StaticLoader(const ApplicationFactory& factory, |
| 61 const base::Closure& quit_callback) | 61 const base::Closure& quit_callback) |
| 62 : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) { | 62 : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) { |
| 63 } | 63 } |
| 64 | 64 |
| 65 StaticLoader::~StaticLoader() { | 65 StaticLoader::~StaticLoader() { |
| 66 if (thread_) | 66 if (thread_) |
| 67 StopAppThread(); | 67 StopAppThread(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void StaticLoader::Load(const std::string& name, | 70 void StaticLoader::Load(const std::string& name, |
| 71 mojo::shell::mojom::ShellClientRequest request) { | 71 shell::mojom::ShellClientRequest request) { |
| 72 if (thread_) | 72 if (thread_) |
| 73 return; | 73 return; |
| 74 | 74 |
| 75 // If the application's thread quits on its own before this loader dies, we | 75 // If the application's thread quits on its own before this loader dies, we |
| 76 // reset the Thread object, allowing future Load requests to be fulfilled | 76 // reset the Thread object, allowing future Load requests to be fulfilled |
| 77 // with a new app instance. | 77 // with a new app instance. |
| 78 auto exit_callback = base::Bind(&StaticLoader::StopAppThread, | 78 auto exit_callback = base::Bind(&StaticLoader::StopAppThread, |
| 79 weak_factory_.GetWeakPtr()); | 79 weak_factory_.GetWeakPtr()); |
| 80 thread_.reset(new RunnerThread(name, std::move(request), | 80 thread_.reset(new RunnerThread(name, std::move(request), |
| 81 base::ThreadTaskRunnerHandle::Get(), | 81 base::ThreadTaskRunnerHandle::Get(), |
| 82 exit_callback, factory_)); | 82 exit_callback, factory_)); |
| 83 thread_->Start(); | 83 thread_->Start(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void StaticLoader::StopAppThread() { | 86 void StaticLoader::StopAppThread() { |
| 87 thread_->Join(); | 87 thread_->Join(); |
| 88 thread_.reset(); | 88 thread_.reset(); |
| 89 if (!quit_callback_.is_null()) | 89 if (!quit_callback_.is_null()) |
| 90 quit_callback_.Run(); | 90 quit_callback_.Run(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 } // namespace content | 93 } // namespace content |
| OLD | NEW |