OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/public/cpp/bindings/connector.h" | 5 #include "mojo/public/cpp/bindings/connector.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "base/threading/thread_local.h" | 19 #include "base/threading/thread_local.h" |
19 #include "mojo/public/cpp/bindings/lib/may_auto_lock.h" | 20 #include "mojo/public/cpp/bindings/lib/may_auto_lock.h" |
20 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" | 21 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" |
21 #include "mojo/public/cpp/system/wait.h" | 22 #include "mojo/public/cpp/system/wait.h" |
22 | 23 |
23 namespace mojo { | 24 namespace mojo { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // The NestingObserver for each thread. Note that this is always a | 28 // The NestingObserver for each thread. Note that this is always a |
28 // Connector::MessageLoopNestingObserver; we use the base type here because that | 29 // Connector::MessageLoopNestingObserver; we use the base type here because that |
29 // subclass is private to Connector. | 30 // subclass is private to Connector. |
30 base::LazyInstance< | 31 base::LazyInstance<base::ThreadLocalPointer<base::RunLoop::NestingObserver>>:: |
31 base::ThreadLocalPointer<base::MessageLoop::NestingObserver>>::Leaky | 32 Leaky g_tls_nesting_observer = LAZY_INSTANCE_INITIALIZER; |
32 g_tls_nesting_observer = LAZY_INSTANCE_INITIALIZER; | |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 // Used to efficiently maintain a doubly-linked list of all Connectors | 36 // Used to efficiently maintain a doubly-linked list of all Connectors |
37 // currently dispatching on any given thread. | 37 // currently dispatching on any given thread. |
38 class Connector::ActiveDispatchTracker { | 38 class Connector::ActiveDispatchTracker { |
39 public: | 39 public: |
40 explicit ActiveDispatchTracker(const base::WeakPtr<Connector>& connector); | 40 explicit ActiveDispatchTracker(const base::WeakPtr<Connector>& connector); |
41 ~ActiveDispatchTracker(); | 41 ~ActiveDispatchTracker(); |
42 | 42 |
43 void NotifyBeginNesting(); | 43 void NotifyBeginNesting(); |
44 | 44 |
45 private: | 45 private: |
46 const base::WeakPtr<Connector> connector_; | 46 const base::WeakPtr<Connector> connector_; |
47 MessageLoopNestingObserver* const nesting_observer_; | 47 MessageLoopNestingObserver* const nesting_observer_; |
48 ActiveDispatchTracker* outer_tracker_ = nullptr; | 48 ActiveDispatchTracker* outer_tracker_ = nullptr; |
49 ActiveDispatchTracker* inner_tracker_ = nullptr; | 49 ActiveDispatchTracker* inner_tracker_ = nullptr; |
50 | 50 |
51 DISALLOW_COPY_AND_ASSIGN(ActiveDispatchTracker); | 51 DISALLOW_COPY_AND_ASSIGN(ActiveDispatchTracker); |
52 }; | 52 }; |
53 | 53 |
54 // Watches the MessageLoop on the current thread. Notifies the current chain of | 54 // Watches the MessageLoop on the current thread. Notifies the current chain of |
55 // ActiveDispatchTrackers when a nested message loop is started. | 55 // ActiveDispatchTrackers when a nested message loop is started. |
56 class Connector::MessageLoopNestingObserver | 56 class Connector::MessageLoopNestingObserver |
57 : public base::MessageLoop::NestingObserver, | 57 : public base::RunLoop::NestingObserver, |
58 public base::MessageLoop::DestructionObserver { | 58 public base::MessageLoop::DestructionObserver { |
59 public: | 59 public: |
60 MessageLoopNestingObserver() { | 60 MessageLoopNestingObserver() { |
61 base::MessageLoop::current()->AddNestingObserver(this); | 61 base::RunLoop::AddNestingObserverOnCurrentThread(this); |
62 base::MessageLoop::current()->AddDestructionObserver(this); | 62 base::MessageLoop::current()->AddDestructionObserver(this); |
63 } | 63 } |
64 | 64 |
65 ~MessageLoopNestingObserver() override {} | 65 ~MessageLoopNestingObserver() override {} |
66 | 66 |
67 // base::MessageLoop::NestingObserver: | 67 // base::RunLoop::NestingObserver: |
68 void OnBeginNestedMessageLoop() override { | 68 void OnBeginNestedRunLoop() override { |
69 if (top_tracker_) | 69 if (top_tracker_) |
70 top_tracker_->NotifyBeginNesting(); | 70 top_tracker_->NotifyBeginNesting(); |
71 } | 71 } |
72 | 72 |
73 // base::MessageLoop::DestructionObserver: | 73 // base::MessageLoop::DestructionObserver: |
74 void WillDestroyCurrentMessageLoop() override { | 74 void WillDestroyCurrentMessageLoop() override { |
75 base::MessageLoop::current()->RemoveNestingObserver(this); | 75 base::RunLoop::RemoveNestingObserverOnCurrentThread(this); |
76 base::MessageLoop::current()->RemoveDestructionObserver(this); | 76 base::MessageLoop::current()->RemoveDestructionObserver(this); |
77 DCHECK_EQ(this, g_tls_nesting_observer.Get().Get()); | 77 DCHECK_EQ(this, g_tls_nesting_observer.Get().Get()); |
78 g_tls_nesting_observer.Get().Set(nullptr); | 78 g_tls_nesting_observer.Get().Set(nullptr); |
79 delete this; | 79 delete this; |
80 } | 80 } |
81 | 81 |
82 static MessageLoopNestingObserver* GetForThread() { | 82 static MessageLoopNestingObserver* GetForThread() { |
83 if (!base::MessageLoop::current() || | 83 if (!base::RunLoop::IsNestingAllowedOnCurrentThread()) |
84 !base::MessageLoop::current()->nesting_allowed()) | |
85 return nullptr; | 84 return nullptr; |
86 auto* observer = static_cast<MessageLoopNestingObserver*>( | 85 auto* observer = static_cast<MessageLoopNestingObserver*>( |
87 g_tls_nesting_observer.Get().Get()); | 86 g_tls_nesting_observer.Get().Get()); |
88 if (!observer) { | 87 if (!observer) { |
89 observer = new MessageLoopNestingObserver; | 88 observer = new MessageLoopNestingObserver; |
90 g_tls_nesting_observer.Get().Set(observer); | 89 g_tls_nesting_observer.Get().Set(observer); |
91 } | 90 } |
92 return observer; | 91 return observer; |
93 } | 92 } |
94 | 93 |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 void Connector::EnsureSyncWatcherExists() { | 483 void Connector::EnsureSyncWatcherExists() { |
485 if (sync_watcher_) | 484 if (sync_watcher_) |
486 return; | 485 return; |
487 sync_watcher_.reset(new SyncHandleWatcher( | 486 sync_watcher_.reset(new SyncHandleWatcher( |
488 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, | 487 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
489 base::Bind(&Connector::OnSyncHandleWatcherHandleReady, | 488 base::Bind(&Connector::OnSyncHandleWatcherHandleReady, |
490 base::Unretained(this)))); | 489 base::Unretained(this)))); |
491 } | 490 } |
492 | 491 |
493 } // namespace mojo | 492 } // namespace mojo |
OLD | NEW |