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 "services/shell/public/cpp/shell_connection_ref.h" | 5 #include "services/shell/public/cpp/shell_connection_ref.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/threading/thread_checker.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
11 | 12 |
12 namespace shell { | 13 namespace shell { |
13 | 14 |
14 class ShellConnectionRefImpl : public ShellConnectionRef { | 15 class ShellConnectionRefImpl : public ShellConnectionRef { |
15 public: | 16 public: |
16 ShellConnectionRefImpl( | 17 ShellConnectionRefImpl( |
17 ShellConnectionRefFactory* factory, | 18 base::WeakPtr<ShellConnectionRefFactory> factory, |
18 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner) | 19 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner) |
19 : factory_(factory), | 20 : factory_(factory), |
20 shell_client_task_runner_(shell_client_task_runner) {} | 21 shell_client_task_runner_(shell_client_task_runner) { |
| 22 // This object is not thread-safe but may be used exclusively on a different |
| 23 // thread from the one which constructed it. |
| 24 thread_checker_.DetachFromThread(); |
| 25 } |
| 26 |
21 ~ShellConnectionRefImpl() override { | 27 ~ShellConnectionRefImpl() override { |
22 #ifndef NDEBUG | 28 DCHECK(thread_checker_.CalledOnValidThread()); |
23 // Ensure that this object is used on only one thread at a time, or else | |
24 // there could be races where the object is being reset on one thread and | |
25 // cloned on another. | |
26 if (clone_task_runner_) | |
27 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | |
28 #endif | |
29 | 29 |
30 if (shell_client_task_runner_->BelongsToCurrentThread()) { | 30 if (shell_client_task_runner_->BelongsToCurrentThread() && factory_) { |
31 factory_->Release(); | 31 factory_->Release(); |
32 } else { | 32 } else { |
33 shell_client_task_runner_->PostTask( | 33 shell_client_task_runner_->PostTask( |
34 FROM_HERE, | 34 FROM_HERE, |
35 base::Bind(&ShellConnectionRefFactory::Release, | 35 base::Bind(&ShellConnectionRefFactory::Release, factory_)); |
36 base::Unretained(factory_))); | |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 private: | 39 private: |
41 // ShellConnectionRef: | 40 // ShellConnectionRef: |
42 std::unique_ptr<ShellConnectionRef> Clone() override { | 41 std::unique_ptr<ShellConnectionRef> Clone() override { |
43 if (shell_client_task_runner_->BelongsToCurrentThread()) { | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 |
| 44 if (shell_client_task_runner_->BelongsToCurrentThread() && factory_) { |
44 factory_->AddRef(); | 45 factory_->AddRef(); |
45 } else { | 46 } else { |
46 shell_client_task_runner_->PostTask( | 47 shell_client_task_runner_->PostTask( |
47 FROM_HERE, | 48 FROM_HERE, |
48 base::Bind(&ShellConnectionRefFactory::AddRef, | 49 base::Bind(&ShellConnectionRefFactory::AddRef, factory_)); |
49 base::Unretained(factory_))); | |
50 } | 50 } |
51 | 51 |
52 #ifndef NDEBUG | |
53 // Ensure that this object is used on only one thread at a time, or else | |
54 // there could be races where the object is being reset on one thread and | |
55 // cloned on another. | |
56 if (clone_task_runner_) { | |
57 DCHECK(clone_task_runner_->BelongsToCurrentThread()); | |
58 } else { | |
59 clone_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
60 } | |
61 #endif | |
62 | |
63 return base::WrapUnique( | 52 return base::WrapUnique( |
64 new ShellConnectionRefImpl(factory_, shell_client_task_runner_)); | 53 new ShellConnectionRefImpl(factory_, shell_client_task_runner_)); |
65 } | 54 } |
66 | 55 |
67 ShellConnectionRefFactory* factory_; | 56 base::WeakPtr<ShellConnectionRefFactory> factory_; |
68 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner_; | 57 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner_; |
69 | 58 base::ThreadChecker thread_checker_; |
70 #ifndef NDEBUG | |
71 scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; | |
72 #endif | |
73 | 59 |
74 DISALLOW_COPY_AND_ASSIGN(ShellConnectionRefImpl); | 60 DISALLOW_COPY_AND_ASSIGN(ShellConnectionRefImpl); |
75 }; | 61 }; |
76 | 62 |
77 ShellConnectionRefFactory::ShellConnectionRefFactory( | 63 ShellConnectionRefFactory::ShellConnectionRefFactory( |
78 const base::Closure& quit_closure) : quit_closure_(quit_closure) { | 64 const base::Closure& quit_closure) |
| 65 : quit_closure_(quit_closure), weak_factory_(this) { |
79 DCHECK(!quit_closure_.is_null()); | 66 DCHECK(!quit_closure_.is_null()); |
80 } | 67 } |
81 | 68 |
82 ShellConnectionRefFactory::~ShellConnectionRefFactory() {} | 69 ShellConnectionRefFactory::~ShellConnectionRefFactory() {} |
83 | 70 |
84 std::unique_ptr<ShellConnectionRef> ShellConnectionRefFactory::CreateRef() { | 71 std::unique_ptr<ShellConnectionRef> ShellConnectionRefFactory::CreateRef() { |
85 AddRef(); | 72 AddRef(); |
86 return base::WrapUnique( | 73 return base::WrapUnique( |
87 new ShellConnectionRefImpl(this, base::ThreadTaskRunnerHandle::Get())); | 74 new ShellConnectionRefImpl(weak_factory_.GetWeakPtr(), |
| 75 base::ThreadTaskRunnerHandle::Get())); |
88 } | 76 } |
89 | 77 |
90 void ShellConnectionRefFactory::AddRef() { | 78 void ShellConnectionRefFactory::AddRef() { |
91 ++ref_count_; | 79 ++ref_count_; |
92 } | 80 } |
93 | 81 |
94 void ShellConnectionRefFactory::Release() { | 82 void ShellConnectionRefFactory::Release() { |
95 if (!--ref_count_) | 83 if (!--ref_count_) |
96 quit_closure_.Run(); | 84 quit_closure_.Run(); |
97 } | 85 } |
98 | 86 |
99 } // namespace shell | 87 } // namespace shell |
OLD | NEW |