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 { |
| 15 class HandleCloseObserver; |
14 | 16 |
15 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle | 17 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle |
16 // is Closed when its JS object is garbage collected. | 18 // is Closed when its JS object is garbage collected. |
17 class HandleWrapper : public gin::Wrappable<HandleWrapper> { | 19 class HandleWrapper : public gin::Wrappable<HandleWrapper> { |
18 public: | 20 public: |
19 static gin::WrapperInfo kWrapperInfo; | 21 static gin::WrapperInfo kWrapperInfo; |
20 | 22 |
21 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, | 23 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, |
22 MojoHandle handle) { | 24 MojoHandle handle) { |
23 return gin::CreateHandle(isolate, new HandleWrapper(handle)); | 25 return gin::CreateHandle(isolate, new HandleWrapper(handle)); |
24 } | 26 } |
25 | 27 |
26 mojo::Handle get() const { return handle_.get(); } | 28 mojo::Handle get() const { return handle_.get(); } |
27 mojo::Handle release() { return handle_.release(); } | 29 mojo::Handle release() { return handle_.release(); } |
28 void Close() { handle_.reset(); } | 30 void Close(); |
| 31 |
| 32 void AddCloseObserver(HandleCloseObserver* observer); |
| 33 void RemoveCloseObserver(HandleCloseObserver* observer); |
29 | 34 |
30 protected: | 35 protected: |
31 HandleWrapper(MojoHandle handle); | 36 HandleWrapper(MojoHandle handle); |
32 virtual ~HandleWrapper(); | 37 virtual ~HandleWrapper(); |
| 38 void NotifyCloseObservers(); |
| 39 |
33 mojo::ScopedHandle handle_; | 40 mojo::ScopedHandle handle_; |
| 41 ObserverList<HandleCloseObserver> close_observers_; |
34 }; | 42 }; |
35 | 43 |
36 // Note: It's important to use this converter rather than the one for | 44 // 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 | 45 // MojoHandle, since that will do a simple int32 conversion. It's unfortunate |
38 // there's no way to prevent against accidental use. | 46 // there's no way to prevent against accidental use. |
39 // TODO(mpcomplete): define converters for all Handle subtypes. | 47 // TODO(mpcomplete): define converters for all Handle subtypes. |
40 template<> | 48 template<> |
41 struct Converter<mojo::Handle> { | 49 struct Converter<mojo::Handle> { |
42 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 50 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
43 const mojo::Handle& val); | 51 const mojo::Handle& val); |
(...skipping 22 matching lines...) Expand all Loading... |
66 return false; | 74 return false; |
67 } | 75 } |
68 *out = gin::Handle<gin::HandleWrapper>(val, object); | 76 *out = gin::Handle<gin::HandleWrapper>(val, object); |
69 return true; | 77 return true; |
70 } | 78 } |
71 }; | 79 }; |
72 | 80 |
73 } // namespace gin | 81 } // namespace gin |
74 | 82 |
75 #endif // MOJO_BINDINGS_JS_HANDLE_H_ | 83 #endif // MOJO_BINDINGS_JS_HANDLE_H_ |
OLD | NEW |