Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_BINDINGS_JS_HANDLE_H_ | 5 #ifndef MOJO_BINDINGS_JS_HANDLE_H_ |
| 6 #define MOJO_BINDINGS_JS_HANDLE_H_ | 6 #define MOJO_BINDINGS_JS_HANDLE_H_ |
| 7 | 7 |
| 8 #include "base/observer_list.h" | |
| 8 #include "gin/converter.h" | 9 #include "gin/converter.h" |
| 9 #include "gin/handle.h" | 10 #include "gin/handle.h" |
| 10 #include "gin/wrappable.h" | 11 #include "gin/wrappable.h" |
| 11 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
| 12 | 13 |
| 13 namespace gin { | 14 namespace gin { |
| 14 | 15 |
| 15 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle | 16 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle |
| 16 // is Closed when its JS object is garbage collected. | 17 // is Closed when its JS object is garbage collected. |
| 17 class HandleWrapper : public gin::Wrappable<HandleWrapper> { | 18 class HandleWrapper : public gin::Wrappable<HandleWrapper> { |
| 18 public: | 19 public: |
| 20 class CloseObserver { | |
|
sky
2014/09/03 15:47:04
Move this into its own file and make the destructo
Sam McNally
2014/09/08 04:31:52
Done.
| |
| 21 public: | |
| 22 virtual ~CloseObserver() {} | |
| 23 virtual void OnHandleClosed() = 0; | |
|
sky
2014/09/03 15:47:04
Isn't this sent before the close? Wouldn't OnWillC
Sam McNally
2014/09/08 04:31:52
Done.
| |
| 24 }; | |
| 19 static gin::WrapperInfo kWrapperInfo; | 25 static gin::WrapperInfo kWrapperInfo; |
| 20 | 26 |
| 21 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, | 27 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, |
| 22 MojoHandle handle) { | 28 MojoHandle handle) { |
| 23 return gin::CreateHandle(isolate, new HandleWrapper(handle)); | 29 return gin::CreateHandle(isolate, new HandleWrapper(handle)); |
| 24 } | 30 } |
| 25 | 31 |
| 26 mojo::Handle get() const { return handle_.get(); } | 32 mojo::Handle get() const { return handle_.get(); } |
| 27 mojo::Handle release() { return handle_.release(); } | 33 mojo::Handle release() { return handle_.release(); } |
| 28 void Close() { handle_.reset(); } | 34 void Close(); |
| 35 | |
| 36 void AddCloseObserver(CloseObserver* observer); | |
| 37 void RemoveCloseObserver(CloseObserver* observer); | |
| 29 | 38 |
| 30 protected: | 39 protected: |
| 31 HandleWrapper(MojoHandle handle); | 40 HandleWrapper(MojoHandle handle); |
| 32 virtual ~HandleWrapper(); | 41 virtual ~HandleWrapper(); |
| 42 void NotifyCloseObservers(); | |
| 33 mojo::ScopedHandle handle_; | 43 mojo::ScopedHandle handle_; |
|
sky
2014/09/03 15:47:04
nit: newline between 42/43.
Sam McNally
2014/09/08 04:31:52
Done.
| |
| 44 ObserverList<CloseObserver> close_observers_; | |
| 34 }; | 45 }; |
| 35 | 46 |
| 36 // Note: It's important to use this converter rather than the one for | 47 // Note: It's important to use this converter rather than the one for |
| 37 // MojoHandle, since that will do a simple int32 conversion. It's unfortunate | 48 // MojoHandle, since that will do a simple int32 conversion. It's unfortunate |
| 38 // there's no way to prevent against accidental use. | 49 // there's no way to prevent against accidental use. |
| 39 // TODO(mpcomplete): define converters for all Handle subtypes. | 50 // TODO(mpcomplete): define converters for all Handle subtypes. |
| 40 template<> | 51 template<> |
| 41 struct Converter<mojo::Handle> { | 52 struct Converter<mojo::Handle> { |
| 42 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 53 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 43 const mojo::Handle& val); | 54 const mojo::Handle& val); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 66 return false; | 77 return false; |
| 67 } | 78 } |
| 68 *out = gin::Handle<gin::HandleWrapper>(val, object); | 79 *out = gin::Handle<gin::HandleWrapper>(val, object); |
| 69 return true; | 80 return true; |
| 70 } | 81 } |
| 71 }; | 82 }; |
| 72 | 83 |
| 73 } // namespace gin | 84 } // namespace gin |
| 74 | 85 |
| 75 #endif // MOJO_BINDINGS_JS_HANDLE_H_ | 86 #endif // MOJO_BINDINGS_JS_HANDLE_H_ |
| OLD | NEW |