| 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // Watches |message_pipe_| (as well as other handles registered to be watched | 131 // Watches |message_pipe_| (as well as other handles registered to be watched |
| 132 // together) synchronously. | 132 // together) synchronously. |
| 133 // This method: | 133 // This method: |
| 134 // - returns true when |should_stop| is set to true; | 134 // - returns true when |should_stop| is set to true; |
| 135 // - return false when any error occurs, including |message_pipe_| being | 135 // - return false when any error occurs, including |message_pipe_| being |
| 136 // closed. | 136 // closed. |
| 137 bool SyncWatch(const bool* should_stop); | 137 bool SyncWatch(const bool* should_stop); |
| 138 | 138 |
| 139 // Whether currently the control flow is inside the sync handle watcher | 139 // Whether currently the control flow is inside the sync handle watcher |
| 140 // callback. | 140 // callback. |
| 141 // It always returns false after CloseMessagePipe()/PassMessagePipe(). |
| 141 bool during_sync_handle_watcher_callback() const { | 142 bool during_sync_handle_watcher_callback() const { |
| 142 return sync_handle_watcher_callback_count_ > 0; | 143 return sync_handle_watcher_callback_count_ > 0; |
| 143 } | 144 } |
| 144 | 145 |
| 145 base::SingleThreadTaskRunner* task_runner() const { | 146 base::SingleThreadTaskRunner* task_runner() const { |
| 146 return task_runner_.get(); | 147 return task_runner_.get(); |
| 147 } | 148 } |
| 148 | 149 |
| 149 private: | 150 private: |
| 150 // Callback of mojo::Watcher. | 151 // Callback of mojo::Watcher. |
| 151 void OnWatcherHandleReady(MojoResult result); | 152 void OnWatcherHandleReady(MojoResult result); |
| 152 // Callback of SyncHandleWatcher. | 153 // Callback of SyncHandleWatcher. |
| 153 void OnSyncHandleWatcherHandleReady(MojoResult result); | 154 void OnSyncHandleWatcherHandleReady(MojoResult result); |
| 154 void OnHandleReadyInternal(MojoResult result); | 155 void OnHandleReadyInternal(MojoResult result); |
| 155 | 156 |
| 156 void WaitToReadMore(); | 157 void WaitToReadMore(); |
| 157 | 158 |
| 158 // Returns false if |this| was destroyed during message dispatch. | 159 // Returns false if it is impossible to receive more messages in the future. |
| 160 // |this| may have been destroyed in that case. |
| 159 WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); | 161 WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); |
| 160 | 162 |
| 161 // |this| can be destroyed during message dispatch. | 163 // |this| can be destroyed during message dispatch. |
| 162 void ReadAllAvailableMessages(); | 164 void ReadAllAvailableMessages(); |
| 163 | 165 |
| 164 // If |force_pipe_reset| is true, this method replaces the existing | 166 // If |force_pipe_reset| is true, this method replaces the existing |
| 165 // |message_pipe_| with a dummy message pipe handle (whose peer is closed). | 167 // |message_pipe_| with a dummy message pipe handle (whose peer is closed). |
| 166 // If |force_async_handler| is true, |connection_error_handler_| is called | 168 // If |force_async_handler| is true, |connection_error_handler_| is called |
| 167 // asynchronously. | 169 // asynchronously. |
| 168 void HandleError(bool force_pipe_reset, bool force_async_handler); | 170 void HandleError(bool force_pipe_reset, bool force_async_handler); |
| 169 | 171 |
| 170 // Cancels any calls made to |waiter_|. | 172 // Cancels any calls made to |waiter_|. |
| 171 void CancelWait(); | 173 void CancelWait(); |
| 172 | 174 |
| 173 void EnsureSyncWatcherExists(); | 175 void EnsureSyncWatcherExists(); |
| 174 | 176 |
| 175 base::Closure connection_error_handler_; | 177 base::Closure connection_error_handler_; |
| 176 | 178 |
| 177 ScopedMessagePipeHandle message_pipe_; | 179 ScopedMessagePipeHandle message_pipe_; |
| 178 MessageReceiver* incoming_receiver_ = nullptr; | 180 MessageReceiver* incoming_receiver_ = nullptr; |
| 179 | 181 |
| 180 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 182 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 181 Watcher handle_watcher_; | 183 std::unique_ptr<Watcher> handle_watcher_; |
| 182 | 184 |
| 183 bool error_ = false; | 185 bool error_ = false; |
| 184 bool drop_writes_ = false; | 186 bool drop_writes_ = false; |
| 185 bool enforce_errors_from_incoming_receiver_ = true; | 187 bool enforce_errors_from_incoming_receiver_ = true; |
| 186 | 188 |
| 187 bool paused_ = false; | 189 bool paused_ = false; |
| 188 | 190 |
| 189 // If sending messages is allowed from multiple threads, |lock_| is used to | 191 // If sending messages is allowed from multiple threads, |lock_| is used to |
| 190 // protect modifications to |message_pipe_| and |drop_writes_|. | 192 // protect modifications to |message_pipe_| and |drop_writes_|. |
| 191 std::unique_ptr<base::Lock> lock_; | 193 std::unique_ptr<base::Lock> lock_; |
| 192 | 194 |
| 193 std::unique_ptr<SyncHandleWatcher> sync_watcher_; | 195 std::unique_ptr<SyncHandleWatcher> sync_watcher_; |
| 194 bool allow_woken_up_by_others_ = false; | 196 bool allow_woken_up_by_others_ = false; |
| 195 // If non-zero, currently the control flow is inside the sync handle watcher | 197 // If non-zero, currently the control flow is inside the sync handle watcher |
| 196 // callback. | 198 // callback. |
| 197 size_t sync_handle_watcher_callback_count_ = 0; | 199 size_t sync_handle_watcher_callback_count_ = 0; |
| 198 | 200 |
| 199 base::ThreadChecker thread_checker_; | 201 base::ThreadChecker thread_checker_; |
| 200 | 202 |
| 201 base::Lock connected_lock_; | 203 base::Lock connected_lock_; |
| 202 bool connected_ = true; | 204 bool connected_ = true; |
| 203 | 205 |
| 204 // Create a single weak ptr and use it everywhere, to avoid the malloc/free | 206 // Create a single weak ptr and use it everywhere, to avoid the malloc/free |
| 205 // cost of creating a new weak ptr whenever it is needed. | 207 // cost of creating a new weak ptr whenever it is needed. |
| 208 // NOTE: This weak pointer is invalidated when the message pipe is closed or |
| 209 // transferred (i.e., when |connected_| is set to false). |
| 206 base::WeakPtr<Connector> weak_self_; | 210 base::WeakPtr<Connector> weak_self_; |
| 207 base::WeakPtrFactory<Connector> weak_factory_; | 211 base::WeakPtrFactory<Connector> weak_factory_; |
| 208 | 212 |
| 209 DISALLOW_COPY_AND_ASSIGN(Connector); | 213 DISALLOW_COPY_AND_ASSIGN(Connector); |
| 210 }; | 214 }; |
| 211 | 215 |
| 212 } // namespace mojo | 216 } // namespace mojo |
| 213 | 217 |
| 214 #endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | 218 #endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
| OLD | NEW |