| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "content/common/mojo/embedded_application_runner.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "services/shell/public/cpp/shell_connection.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 class EmbeddedApplicationRunner::Instance |
| 19 : public base::RefCountedThreadSafe<Instance> { |
| 20 public: |
| 21 explicit Instance( |
| 22 const EmbeddedApplicationRunner::FactoryCallback& callback) |
| 23 : factory_callback_(callback) { |
| 24 // This object may be used exclusively from a single thread which may be |
| 25 // different from the one that created it. |
| 26 thread_checker_.DetachFromThread(); |
| 27 } |
| 28 |
| 29 void BindShellClientRequest(mojo::shell::mojom::ShellClientRequest request) { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 |
| 32 if (!shell_client_) |
| 33 shell_client_ = factory_callback_.Run(); |
| 34 |
| 35 std::unique_ptr<mojo::ShellConnection> new_connection( |
| 36 new mojo::ShellConnection(shell_client_.get(), std::move(request))); |
| 37 new_connection->set_connection_lost_closure( |
| 38 base::Bind(&Instance::OnShellConnectionLost, |
| 39 base::Unretained(this), new_connection.get())); |
| 40 shell_connections_.push_back(std::move(new_connection)); |
| 41 } |
| 42 |
| 43 private: |
| 44 friend class base::RefCountedThreadSafe<Instance>; |
| 45 |
| 46 ~Instance() { DCHECK(thread_checker_.CalledOnValidThread()); } |
| 47 |
| 48 void OnShellConnectionLost(mojo::ShellConnection* connection) { |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 50 |
| 51 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
| 52 ++it) { |
| 53 if (it->get() == connection) { |
| 54 shell_connections_.erase(it); |
| 55 break; |
| 56 } |
| 57 } |
| 58 |
| 59 if (shell_connections_.empty()) |
| 60 shell_client_.reset(); |
| 61 } |
| 62 |
| 63 base::ThreadChecker thread_checker_; |
| 64 const FactoryCallback factory_callback_; |
| 65 std::unique_ptr<mojo::ShellClient> shell_client_; |
| 66 std::vector<std::unique_ptr<mojo::ShellConnection>> shell_connections_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 69 }; |
| 70 |
| 71 EmbeddedApplicationRunner::EmbeddedApplicationRunner( |
| 72 const FactoryCallback& callback, |
| 73 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 74 : application_task_runner_( |
| 75 task_runner ? task_runner : base::ThreadTaskRunnerHandle::Get()), |
| 76 instance_(new Instance(callback)) { |
| 77 } |
| 78 |
| 79 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { |
| 80 } |
| 81 |
| 82 void EmbeddedApplicationRunner::BindShellClientRequest( |
| 83 mojo::shell::mojom::ShellClientRequest request) { |
| 84 application_task_runner_->PostTask( |
| 85 FROM_HERE, |
| 86 base::Bind(&Instance::BindShellClientRequest, instance_, |
| 87 base::Passed(&request))); |
| 88 } |
| 89 |
| 90 } // namespace content |
| OLD | NEW |