| 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/common/handle_watcher.h" | 5 #include "mojo/common/handle_watcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // WatcherBackend -------------------------------------------------------------- | 56 // WatcherBackend -------------------------------------------------------------- |
| 57 | 57 |
| 58 // WatcherBackend is responsible for managing the requests and interacting with | 58 // WatcherBackend is responsible for managing the requests and interacting with |
| 59 // MessagePumpMojo. All access (outside of creation/destruction) is done on the | 59 // MessagePumpMojo. All access (outside of creation/destruction) is done on the |
| 60 // thread WatcherThreadManager creates. | 60 // thread WatcherThreadManager creates. |
| 61 class WatcherBackend : public MessagePumpMojoHandler { | 61 class WatcherBackend : public MessagePumpMojoHandler { |
| 62 public: | 62 public: |
| 63 WatcherBackend(); | 63 WatcherBackend(); |
| 64 virtual ~WatcherBackend(); | 64 ~WatcherBackend() override; |
| 65 | 65 |
| 66 void StartWatching(const WatchData& data); | 66 void StartWatching(const WatchData& data); |
| 67 | 67 |
| 68 // Cancels a previously scheduled request to start a watch. | 68 // Cancels a previously scheduled request to start a watch. |
| 69 void StopWatching(WatcherID watcher_id); | 69 void StopWatching(WatcherID watcher_id); |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 typedef std::map<Handle, WatchData> HandleToWatchDataMap; | 72 typedef std::map<Handle, WatchData> HandleToWatchDataMap; |
| 73 | 73 |
| 74 // Invoked when a handle needs to be removed and notified. | 74 // Invoked when a handle needs to be removed and notified. |
| 75 void RemoveAndNotify(const Handle& handle, MojoResult result); | 75 void RemoveAndNotify(const Handle& handle, MojoResult result); |
| 76 | 76 |
| 77 // Searches through |handle_to_data_| for |watcher_id|. Returns true if found | 77 // Searches through |handle_to_data_| for |watcher_id|. Returns true if found |
| 78 // and sets |handle| to the Handle. Returns false if not a known id. | 78 // and sets |handle| to the Handle. Returns false if not a known id. |
| 79 bool GetMojoHandleByWatcherID(WatcherID watcher_id, Handle* handle) const; | 79 bool GetMojoHandleByWatcherID(WatcherID watcher_id, Handle* handle) const; |
| 80 | 80 |
| 81 // MessagePumpMojoHandler overrides: | 81 // MessagePumpMojoHandler overrides: |
| 82 virtual void OnHandleReady(const Handle& handle) override; | 82 void OnHandleReady(const Handle& handle) override; |
| 83 virtual void OnHandleError(const Handle& handle, MojoResult result) override; | 83 void OnHandleError(const Handle& handle, MojoResult result) override; |
| 84 | 84 |
| 85 // Maps from assigned id to WatchData. | 85 // Maps from assigned id to WatchData. |
| 86 HandleToWatchDataMap handle_to_data_; | 86 HandleToWatchDataMap handle_to_data_; |
| 87 | 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(WatcherBackend); | 88 DISALLOW_COPY_AND_ASSIGN(WatcherBackend); |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 WatcherBackend::WatcherBackend() { | 91 WatcherBackend::WatcherBackend() { |
| 92 } | 92 } |
| 93 | 93 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 class HandleWatcher::StateBase : public base::MessageLoop::DestructionObserver { | 318 class HandleWatcher::StateBase : public base::MessageLoop::DestructionObserver { |
| 319 public: | 319 public: |
| 320 StateBase(HandleWatcher* watcher, | 320 StateBase(HandleWatcher* watcher, |
| 321 const base::Callback<void(MojoResult)>& callback) | 321 const base::Callback<void(MojoResult)>& callback) |
| 322 : watcher_(watcher), | 322 : watcher_(watcher), |
| 323 callback_(callback), | 323 callback_(callback), |
| 324 got_ready_(false) { | 324 got_ready_(false) { |
| 325 base::MessageLoop::current()->AddDestructionObserver(this); | 325 base::MessageLoop::current()->AddDestructionObserver(this); |
| 326 } | 326 } |
| 327 | 327 |
| 328 virtual ~StateBase() { | 328 ~StateBase() override { |
| 329 base::MessageLoop::current()->RemoveDestructionObserver(this); | 329 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 330 } | 330 } |
| 331 | 331 |
| 332 protected: | 332 protected: |
| 333 void NotifyHandleReady(MojoResult result) { | 333 void NotifyHandleReady(MojoResult result) { |
| 334 got_ready_ = true; | 334 got_ready_ = true; |
| 335 NotifyAndDestroy(result); | 335 NotifyAndDestroy(result); |
| 336 } | 336 } |
| 337 | 337 |
| 338 bool got_ready() const { return got_ready_; } | 338 bool got_ready() const { return got_ready_; } |
| 339 | 339 |
| 340 private: | 340 private: |
| 341 virtual void WillDestroyCurrentMessageLoop() override { | 341 void WillDestroyCurrentMessageLoop() override { |
| 342 // The current thread is exiting. Simulate a watch error. | 342 // The current thread is exiting. Simulate a watch error. |
| 343 NotifyAndDestroy(MOJO_RESULT_ABORTED); | 343 NotifyAndDestroy(MOJO_RESULT_ABORTED); |
| 344 } | 344 } |
| 345 | 345 |
| 346 void NotifyAndDestroy(MojoResult result) { | 346 void NotifyAndDestroy(MojoResult result) { |
| 347 base::Callback<void(MojoResult)> callback = callback_; | 347 base::Callback<void(MojoResult)> callback = callback_; |
| 348 watcher_->Stop(); // Destroys |this|. | 348 watcher_->Stop(); // Destroys |this|. |
| 349 | 349 |
| 350 callback.Run(result); | 350 callback.Run(result); |
| 351 } | 351 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 371 MojoDeadline deadline, | 371 MojoDeadline deadline, |
| 372 const base::Callback<void(MojoResult)>& callback) | 372 const base::Callback<void(MojoResult)>& callback) |
| 373 : StateBase(watcher, callback), | 373 : StateBase(watcher, callback), |
| 374 handle_(handle) { | 374 handle_(handle) { |
| 375 DCHECK(MessagePumpMojo::IsCurrent()); | 375 DCHECK(MessagePumpMojo::IsCurrent()); |
| 376 | 376 |
| 377 MessagePumpMojo::current()->AddHandler( | 377 MessagePumpMojo::current()->AddHandler( |
| 378 this, handle, handle_signals, MojoDeadlineToTimeTicks(deadline)); | 378 this, handle, handle_signals, MojoDeadlineToTimeTicks(deadline)); |
| 379 } | 379 } |
| 380 | 380 |
| 381 virtual ~SameThreadWatchingState() { | 381 ~SameThreadWatchingState() override { |
| 382 if (!got_ready()) | 382 if (!got_ready()) |
| 383 MessagePumpMojo::current()->RemoveHandler(handle_); | 383 MessagePumpMojo::current()->RemoveHandler(handle_); |
| 384 } | 384 } |
| 385 | 385 |
| 386 private: | 386 private: |
| 387 // MessagePumpMojoHandler overrides: | 387 // MessagePumpMojoHandler overrides: |
| 388 virtual void OnHandleReady(const Handle& handle) override { | 388 void OnHandleReady(const Handle& handle) override { |
| 389 StopWatchingAndNotifyReady(handle, MOJO_RESULT_OK); | 389 StopWatchingAndNotifyReady(handle, MOJO_RESULT_OK); |
| 390 } | 390 } |
| 391 | 391 |
| 392 virtual void OnHandleError(const Handle& handle, MojoResult result) override { | 392 void OnHandleError(const Handle& handle, MojoResult result) override { |
| 393 StopWatchingAndNotifyReady(handle, result); | 393 StopWatchingAndNotifyReady(handle, result); |
| 394 } | 394 } |
| 395 | 395 |
| 396 void StopWatchingAndNotifyReady(const Handle& handle, MojoResult result) { | 396 void StopWatchingAndNotifyReady(const Handle& handle, MojoResult result) { |
| 397 DCHECK_EQ(handle.value(), handle_.value()); | 397 DCHECK_EQ(handle.value(), handle_.value()); |
| 398 MessagePumpMojo::current()->RemoveHandler(handle_); | 398 MessagePumpMojo::current()->RemoveHandler(handle_); |
| 399 NotifyHandleReady(result); | 399 NotifyHandleReady(result); |
| 400 } | 400 } |
| 401 | 401 |
| 402 Handle handle_; | 402 Handle handle_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 417 : StateBase(watcher, callback), | 417 : StateBase(watcher, callback), |
| 418 weak_factory_(this) { | 418 weak_factory_(this) { |
| 419 watcher_id_ = WatcherThreadManager::GetInstance()->StartWatching( | 419 watcher_id_ = WatcherThreadManager::GetInstance()->StartWatching( |
| 420 handle, | 420 handle, |
| 421 handle_signals, | 421 handle_signals, |
| 422 MojoDeadlineToTimeTicks(deadline), | 422 MojoDeadlineToTimeTicks(deadline), |
| 423 base::Bind(&SecondaryThreadWatchingState::NotifyHandleReady, | 423 base::Bind(&SecondaryThreadWatchingState::NotifyHandleReady, |
| 424 weak_factory_.GetWeakPtr())); | 424 weak_factory_.GetWeakPtr())); |
| 425 } | 425 } |
| 426 | 426 |
| 427 virtual ~SecondaryThreadWatchingState() { | 427 ~SecondaryThreadWatchingState() override { |
| 428 // If we've been notified the handle is ready (|got_ready()| is true) then | 428 // If we've been notified the handle is ready (|got_ready()| is true) then |
| 429 // the watch has been implicitly removed by | 429 // the watch has been implicitly removed by |
| 430 // WatcherThreadManager/MessagePumpMojo and we don't have to call | 430 // WatcherThreadManager/MessagePumpMojo and we don't have to call |
| 431 // StopWatching(). To do so would needlessly entail posting a task and | 431 // StopWatching(). To do so would needlessly entail posting a task and |
| 432 // blocking until the background thread services it. | 432 // blocking until the background thread services it. |
| 433 if (!got_ready()) | 433 if (!got_ready()) |
| 434 WatcherThreadManager::GetInstance()->StopWatching(watcher_id_); | 434 WatcherThreadManager::GetInstance()->StopWatching(watcher_id_); |
| 435 } | 435 } |
| 436 | 436 |
| 437 private: | 437 private: |
| (...skipping 28 matching lines...) Expand all Loading... |
| 466 this, handle, handle_signals, deadline, callback)); | 466 this, handle, handle_signals, deadline, callback)); |
| 467 } | 467 } |
| 468 } | 468 } |
| 469 | 469 |
| 470 void HandleWatcher::Stop() { | 470 void HandleWatcher::Stop() { |
| 471 state_.reset(); | 471 state_.reset(); |
| 472 } | 472 } |
| 473 | 473 |
| 474 } // namespace common | 474 } // namespace common |
| 475 } // namespace mojo | 475 } // namespace mojo |
| OLD | NEW |