Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1368)

Unified Diff: mojo/public/cpp/bindings/strong_binding.h

Issue 1913333002: Mojo C++ bindings: make StrongBinding a message loop destruction observer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/utility/image_decoder_impl_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/strong_binding.h
diff --git a/mojo/public/cpp/bindings/strong_binding.h b/mojo/public/cpp/bindings/strong_binding.h
index 3694c68082740332dbe310cec973183e430eec02..83edba697702de50aefefd10966431f3393dad01 100644
--- a/mojo/public/cpp/bindings/strong_binding.h
+++ b/mojo/public/cpp/bindings/strong_binding.h
@@ -8,20 +8,18 @@
#include <utility>
#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/bindings/interface_request.h"
-#include "mojo/public/cpp/bindings/lib/filter_chain.h"
-#include "mojo/public/cpp/bindings/lib/message_header_validator.h"
-#include "mojo/public/cpp/bindings/lib/router.h"
#include "mojo/public/cpp/system/core.h"
namespace mojo {
// This connects an interface implementation strongly to a pipe. When a
-// connection error is detected the implementation is deleted. Deleting the
-// connector also closes the pipe.
+// connection error is detected or the current message loop is destructed the
+// implementation is deleted.
//
// Example of an implementation that is always bound strongly to a pipe
//
@@ -47,11 +45,13 @@ namespace mojo {
// This class is thread hostile once it is bound to a message pipe. Until it is
// bound, it may be bound or destroyed on any thread.
template <typename Interface>
-class StrongBinding {
+class StrongBinding : public base::MessageLoop::DestructionObserver {
MOVE_ONLY_TYPE_FOR_CPP_03(StrongBinding);
public:
- explicit StrongBinding(Interface* impl) : binding_(impl) {}
+ explicit StrongBinding(Interface* impl) : binding_(impl), observing_(true) {
+ base::MessageLoop::current()->AddDestructionObserver(this);
+ }
StrongBinding(Interface* impl, ScopedMessagePipeHandle handle)
: StrongBinding(impl) {
@@ -68,7 +68,7 @@ class StrongBinding {
Bind(std::move(request));
}
- ~StrongBinding() {}
+ ~StrongBinding() override { StopObservingIfNecessary(); }
void Bind(ScopedMessagePipeHandle handle) {
DCHECK(!binding_.is_bound());
@@ -101,18 +101,31 @@ class StrongBinding {
connection_error_handler_ = error_handler;
}
- Interface* impl() { return binding_.impl(); }
- // Exposed for testing, should not generally be used.
- internal::Router* internal_router() { return binding_.internal_router(); }
-
void OnConnectionError() {
+ StopObservingIfNecessary();
connection_error_handler_.Run();
delete binding_.impl();
}
+ // base::MessageLoop::DestructionObserver:
+ void WillDestroyCurrentMessageLoop() override {
+ StopObservingIfNecessary();
+ binding_.Close();
+ delete binding_.impl();
+ }
+
private:
+ void StopObservingIfNecessary() {
+ if (observing_) {
+ observing_ = false;
+ base::MessageLoop::current()->RemoveDestructionObserver(this);
+ }
+ }
+
Closure connection_error_handler_;
Binding<Interface> binding_;
+ // Whether the object is observing message loop destruction.
+ bool observing_;
};
} // namespace mojo
« no previous file with comments | « chrome/utility/image_decoder_impl_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698