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_checker.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 | 12 |
13 namespace shell { | 13 namespace shell { |
14 | 14 |
15 class ShellConnectionRefImpl : public ShellConnectionRef { | 15 class ShellConnectionRefImpl : public ShellConnectionRef { |
16 public: | 16 public: |
17 ShellConnectionRefImpl( | 17 ShellConnectionRefImpl( |
18 base::WeakPtr<ShellConnectionRefFactory> factory, | 18 base::WeakPtr<ShellConnectionRefFactory> factory, |
19 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner) | 19 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner) |
20 : factory_(factory), | 20 : factory_(factory), |
21 shell_client_task_runner_(shell_client_task_runner) { | 21 service_task_runner_(service_task_runner) { |
22 // This object is not thread-safe but may be used exclusively on a different | 22 // This object is not thread-safe but may be used exclusively on a different |
23 // thread from the one which constructed it. | 23 // thread from the one which constructed it. |
24 thread_checker_.DetachFromThread(); | 24 thread_checker_.DetachFromThread(); |
25 } | 25 } |
26 | 26 |
27 ~ShellConnectionRefImpl() override { | 27 ~ShellConnectionRefImpl() override { |
28 DCHECK(thread_checker_.CalledOnValidThread()); | 28 DCHECK(thread_checker_.CalledOnValidThread()); |
29 | 29 |
30 if (shell_client_task_runner_->BelongsToCurrentThread() && factory_) { | 30 if (service_task_runner_->BelongsToCurrentThread() && factory_) { |
31 factory_->Release(); | 31 factory_->Release(); |
32 } else { | 32 } else { |
33 shell_client_task_runner_->PostTask( | 33 service_task_runner_->PostTask( |
34 FROM_HERE, | 34 FROM_HERE, |
35 base::Bind(&ShellConnectionRefFactory::Release, factory_)); | 35 base::Bind(&ShellConnectionRefFactory::Release, factory_)); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 // ShellConnectionRef: | 40 // ShellConnectionRef: |
41 std::unique_ptr<ShellConnectionRef> Clone() override { | 41 std::unique_ptr<ShellConnectionRef> Clone() override { |
42 DCHECK(thread_checker_.CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
43 | 43 |
44 if (shell_client_task_runner_->BelongsToCurrentThread() && factory_) { | 44 if (service_task_runner_->BelongsToCurrentThread() && factory_) { |
45 factory_->AddRef(); | 45 factory_->AddRef(); |
46 } else { | 46 } else { |
47 shell_client_task_runner_->PostTask( | 47 service_task_runner_->PostTask( |
48 FROM_HERE, | 48 FROM_HERE, |
49 base::Bind(&ShellConnectionRefFactory::AddRef, factory_)); | 49 base::Bind(&ShellConnectionRefFactory::AddRef, factory_)); |
50 } | 50 } |
51 | 51 |
52 return base::WrapUnique( | 52 return base::WrapUnique( |
53 new ShellConnectionRefImpl(factory_, shell_client_task_runner_)); | 53 new ShellConnectionRefImpl(factory_, service_task_runner_)); |
54 } | 54 } |
55 | 55 |
56 base::WeakPtr<ShellConnectionRefFactory> factory_; | 56 base::WeakPtr<ShellConnectionRefFactory> factory_; |
57 scoped_refptr<base::SingleThreadTaskRunner> shell_client_task_runner_; | 57 scoped_refptr<base::SingleThreadTaskRunner> service_task_runner_; |
58 base::ThreadChecker thread_checker_; | 58 base::ThreadChecker thread_checker_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(ShellConnectionRefImpl); | 60 DISALLOW_COPY_AND_ASSIGN(ShellConnectionRefImpl); |
61 }; | 61 }; |
62 | 62 |
63 ShellConnectionRefFactory::ShellConnectionRefFactory( | 63 ShellConnectionRefFactory::ShellConnectionRefFactory( |
64 const base::Closure& quit_closure) | 64 const base::Closure& quit_closure) |
65 : quit_closure_(quit_closure), weak_factory_(this) { | 65 : quit_closure_(quit_closure), weak_factory_(this) { |
66 DCHECK(!quit_closure_.is_null()); | 66 DCHECK(!quit_closure_.is_null()); |
67 } | 67 } |
(...skipping 10 matching lines...) Expand all Loading... |
78 void ShellConnectionRefFactory::AddRef() { | 78 void ShellConnectionRefFactory::AddRef() { |
79 ++ref_count_; | 79 ++ref_count_; |
80 } | 80 } |
81 | 81 |
82 void ShellConnectionRefFactory::Release() { | 82 void ShellConnectionRefFactory::Release() { |
83 if (!--ref_count_) | 83 if (!--ref_count_) |
84 quit_closure_.Run(); | 84 quit_closure_.Run(); |
85 } | 85 } |
86 | 86 |
87 } // namespace shell | 87 } // namespace shell |
OLD | NEW |