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/single_thread_task_runner.h" |
12 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
13 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
14 #include "services/shell/public/cpp/shell_connection.h" | 15 #include "services/shell/public/cpp/shell_connection.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 class EmbeddedApplicationRunner::Instance | 19 class EmbeddedApplicationRunner::Instance |
19 : public base::RefCountedThreadSafe<Instance> { | 20 : public base::RefCountedThreadSafe<Instance> { |
20 public: | 21 public: |
21 explicit Instance( | 22 explicit Instance( |
22 const EmbeddedApplicationRunner::FactoryCallback& callback) | 23 const EmbeddedApplicationRunner::FactoryCallback& callback, |
23 : factory_callback_(callback) { | 24 const base::Closure& quit_closure) |
| 25 : factory_callback_(callback), |
| 26 quit_closure_(quit_closure), |
| 27 quit_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
24 // This object may be used exclusively from a single thread which may be | 28 // This object may be used exclusively from a single thread which may be |
25 // different from the one that created it. | 29 // different from the one that created it. |
26 thread_checker_.DetachFromThread(); | 30 thread_checker_.DetachFromThread(); |
27 } | 31 } |
28 | 32 |
29 void BindShellClientRequest(shell::mojom::ShellClientRequest request) { | 33 void BindShellClientRequest(shell::mojom::ShellClientRequest request) { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
31 | 35 |
32 if (!shell_client_) | 36 if (!shell_client_) |
33 shell_client_ = factory_callback_.Run(); | 37 shell_client_ = factory_callback_.Run(); |
(...skipping 15 matching lines...) Expand all Loading... |
49 DCHECK(thread_checker_.CalledOnValidThread()); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
50 | 54 |
51 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); | 55 for (auto it = shell_connections_.begin(); it != shell_connections_.end(); |
52 ++it) { | 56 ++it) { |
53 if (it->get() == connection) { | 57 if (it->get() == connection) { |
54 shell_connections_.erase(it); | 58 shell_connections_.erase(it); |
55 break; | 59 break; |
56 } | 60 } |
57 } | 61 } |
58 | 62 |
59 if (shell_connections_.empty()) | 63 if (shell_connections_.empty()) { |
60 shell_client_.reset(); | 64 shell_client_.reset(); |
| 65 quit_task_runner_->PostTask(FROM_HERE, quit_closure_); |
| 66 } |
61 } | 67 } |
62 | 68 |
63 base::ThreadChecker thread_checker_; | 69 base::ThreadChecker thread_checker_; |
64 const FactoryCallback factory_callback_; | 70 const FactoryCallback factory_callback_; |
65 std::unique_ptr<shell::ShellClient> shell_client_; | 71 std::unique_ptr<shell::ShellClient> shell_client_; |
66 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; | 72 std::vector<std::unique_ptr<shell::ShellConnection>> shell_connections_; |
| 73 const base::Closure quit_closure_; |
| 74 const scoped_refptr<base::SingleThreadTaskRunner> quit_task_runner_; |
67 | 75 |
68 DISALLOW_COPY_AND_ASSIGN(Instance); | 76 DISALLOW_COPY_AND_ASSIGN(Instance); |
69 }; | 77 }; |
70 | 78 |
71 EmbeddedApplicationRunner::EmbeddedApplicationRunner( | 79 EmbeddedApplicationRunner::EmbeddedApplicationRunner( |
72 const FactoryCallback& callback, | 80 const FactoryCallback& callback, |
73 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 81 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
74 : application_task_runner_( | 82 : application_task_runner_( |
75 task_runner ? task_runner : base::ThreadTaskRunnerHandle::Get()), | 83 task_runner ? task_runner : base::ThreadTaskRunnerHandle::Get()), |
76 instance_(new Instance(callback)) { | 84 weak_factory_(this) { |
| 85 instance_ = new Instance(callback, |
| 86 base::Bind(&EmbeddedApplicationRunner::OnQuit, |
| 87 weak_factory_.GetWeakPtr())); |
77 } | 88 } |
78 | 89 |
79 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { | 90 EmbeddedApplicationRunner::~EmbeddedApplicationRunner() { |
80 } | 91 } |
81 | 92 |
82 void EmbeddedApplicationRunner::BindShellClientRequest( | 93 void EmbeddedApplicationRunner::BindShellClientRequest( |
83 shell::mojom::ShellClientRequest request) { | 94 shell::mojom::ShellClientRequest request) { |
84 application_task_runner_->PostTask( | 95 application_task_runner_->PostTask( |
85 FROM_HERE, | 96 FROM_HERE, |
86 base::Bind(&Instance::BindShellClientRequest, instance_, | 97 base::Bind(&Instance::BindShellClientRequest, instance_, |
87 base::Passed(&request))); | 98 base::Passed(&request))); |
88 } | 99 } |
89 | 100 |
| 101 void EmbeddedApplicationRunner::SetQuitClosure( |
| 102 const base::Closure& quit_closure) { |
| 103 quit_closure_ = quit_closure; |
| 104 } |
| 105 |
| 106 void EmbeddedApplicationRunner::OnQuit() { |
| 107 quit_closure_.Run(); |
| 108 } |
| 109 |
90 } // namespace content | 110 } // namespace content |
OLD | NEW |