OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_EDK_JS_HANDLE_H_ | |
6 #define MOJO_EDK_JS_HANDLE_H_ | |
7 | |
8 #include "base/observer_list.h" | |
9 #include "gin/converter.h" | |
10 #include "gin/handle.h" | |
11 #include "gin/wrappable.h" | |
12 #include "mojo/public/cpp/system/core.h" | |
13 | |
14 namespace mojo { | |
15 namespace js { | |
16 class HandleCloseObserver; | |
17 | |
18 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle | |
19 // is Closed when its JS object is garbage collected. | |
20 class HandleWrapper : public gin::Wrappable<HandleWrapper> { | |
21 public: | |
22 static gin::WrapperInfo kWrapperInfo; | |
23 | |
24 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, | |
25 MojoHandle handle) { | |
26 return gin::CreateHandle(isolate, new HandleWrapper(handle)); | |
27 } | |
28 | |
29 mojo::Handle get() const { return handle_.get(); } | |
30 mojo::Handle release() { return handle_.release(); } | |
31 void Close(); | |
32 | |
33 void AddCloseObserver(HandleCloseObserver* observer); | |
34 void RemoveCloseObserver(HandleCloseObserver* observer); | |
35 | |
36 protected: | |
37 HandleWrapper(MojoHandle handle); | |
38 ~HandleWrapper() override; | |
39 void NotifyCloseObservers(); | |
40 | |
41 mojo::ScopedHandle handle_; | |
42 ObserverList<HandleCloseObserver> close_observers_; | |
43 }; | |
44 | |
45 } // namespace js | |
46 } // namespace mojo | |
47 | |
48 namespace gin { | |
49 | |
50 // Note: It's important to use this converter rather than the one for | |
51 // MojoHandle, since that will do a simple int32 conversion. It's unfortunate | |
52 // there's no way to prevent against accidental use. | |
53 // TODO(mpcomplete): define converters for all Handle subtypes. | |
54 template<> | |
55 struct Converter<mojo::Handle> { | |
56 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
57 const mojo::Handle& val); | |
58 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
59 mojo::Handle* out); | |
60 }; | |
61 | |
62 template<> | |
63 struct Converter<mojo::MessagePipeHandle> { | |
64 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
65 mojo::MessagePipeHandle val); | |
66 static bool FromV8(v8::Isolate* isolate, | |
67 v8::Handle<v8::Value> val, | |
68 mojo::MessagePipeHandle* out); | |
69 }; | |
70 | |
71 // We need to specialize the normal gin::Handle converter in order to handle | |
72 // converting |null| to a wrapper for an empty mojo::Handle. | |
73 template<> | |
74 struct Converter<gin::Handle<mojo::js::HandleWrapper> > { | |
75 static v8::Handle<v8::Value> ToV8( | |
76 v8::Isolate* isolate, const gin::Handle<mojo::js::HandleWrapper>& val) { | |
77 return val.ToV8(); | |
78 } | |
79 | |
80 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
81 gin::Handle<mojo::js::HandleWrapper>* out) { | |
82 if (val->IsNull()) { | |
83 *out = mojo::js::HandleWrapper::Create(isolate, MOJO_HANDLE_INVALID); | |
84 return true; | |
85 } | |
86 | |
87 mojo::js::HandleWrapper* object = NULL; | |
88 if (!Converter<mojo::js::HandleWrapper*>::FromV8(isolate, val, &object)) { | |
89 return false; | |
90 } | |
91 *out = gin::Handle<mojo::js::HandleWrapper>(val, object); | |
92 return true; | |
93 } | |
94 }; | |
95 | |
96 } // namespace gin | |
97 | |
98 #endif // MOJO_EDK_JS_HANDLE_H_ | |
OLD | NEW |