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